eCryptfs: Copy lower directory inode times and size on link
[linux/fpc-iii.git] / fs / logfs / readwrite.c
blob7a23b3e7c0a798c3b750d96f08cf1351950cefde
1 /*
2 * fs/logfs/readwrite.c
4 * As should be obvious for Linux kernel code, license is GPLv2
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
9 * Actually contains five sets of very similar functions:
10 * read read blocks from a file
11 * seek_hole find next hole
12 * seek_data find next data block
13 * valid check whether a block still belongs to a file
14 * write write blocks to a file
15 * delete delete a block (for directories and ifile)
16 * rewrite move existing blocks of a file to a new location (gc helper)
17 * truncate truncate a file
19 #include "logfs.h"
20 #include <linux/sched.h>
22 static u64 adjust_bix(u64 bix, level_t level)
24 switch (level) {
25 case 0:
26 return bix;
27 case LEVEL(1):
28 return max_t(u64, bix, I0_BLOCKS);
29 case LEVEL(2):
30 return max_t(u64, bix, I1_BLOCKS);
31 case LEVEL(3):
32 return max_t(u64, bix, I2_BLOCKS);
33 case LEVEL(4):
34 return max_t(u64, bix, I3_BLOCKS);
35 case LEVEL(5):
36 return max_t(u64, bix, I4_BLOCKS);
37 default:
38 WARN_ON(1);
39 return bix;
43 static inline u64 maxbix(u8 height)
45 return 1ULL << (LOGFS_BLOCK_BITS * height);
48 /**
49 * The inode address space is cut in two halves. Lower half belongs to data
50 * pages, upper half to indirect blocks. If the high bit (INDIRECT_BIT) is
51 * set, the actual block index (bix) and level can be derived from the page
52 * index.
54 * The lowest three bits of the block index are set to 0 after packing and
55 * unpacking. Since the lowest n bits (9 for 4KiB blocksize) are ignored
56 * anyway this is harmless.
58 #define ARCH_SHIFT (BITS_PER_LONG - 32)
59 #define INDIRECT_BIT (0x80000000UL << ARCH_SHIFT)
60 #define LEVEL_SHIFT (28 + ARCH_SHIFT)
61 static inline pgoff_t first_indirect_block(void)
63 return INDIRECT_BIT | (1ULL << LEVEL_SHIFT);
66 pgoff_t logfs_pack_index(u64 bix, level_t level)
68 pgoff_t index;
70 BUG_ON(bix >= INDIRECT_BIT);
71 if (level == 0)
72 return bix;
74 index = INDIRECT_BIT;
75 index |= (__force long)level << LEVEL_SHIFT;
76 index |= bix >> ((__force u8)level * LOGFS_BLOCK_BITS);
77 return index;
80 void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level)
82 u8 __level;
84 if (!(index & INDIRECT_BIT)) {
85 *bix = index;
86 *level = 0;
87 return;
90 __level = (index & ~INDIRECT_BIT) >> LEVEL_SHIFT;
91 *level = LEVEL(__level);
92 *bix = (index << (__level * LOGFS_BLOCK_BITS)) & ~INDIRECT_BIT;
93 *bix = adjust_bix(*bix, *level);
94 return;
96 #undef ARCH_SHIFT
97 #undef INDIRECT_BIT
98 #undef LEVEL_SHIFT
101 * Time is stored as nanoseconds since the epoch.
103 static struct timespec be64_to_timespec(__be64 betime)
105 return ns_to_timespec(be64_to_cpu(betime));
108 static __be64 timespec_to_be64(struct timespec tsp)
110 return cpu_to_be64((u64)tsp.tv_sec * NSEC_PER_SEC + tsp.tv_nsec);
113 static void logfs_disk_to_inode(struct logfs_disk_inode *di, struct inode*inode)
115 struct logfs_inode *li = logfs_inode(inode);
116 int i;
118 inode->i_mode = be16_to_cpu(di->di_mode);
119 li->li_height = di->di_height;
120 li->li_flags = be32_to_cpu(di->di_flags);
121 inode->i_uid = be32_to_cpu(di->di_uid);
122 inode->i_gid = be32_to_cpu(di->di_gid);
123 inode->i_size = be64_to_cpu(di->di_size);
124 logfs_set_blocks(inode, be64_to_cpu(di->di_used_bytes));
125 inode->i_atime = be64_to_timespec(di->di_atime);
126 inode->i_ctime = be64_to_timespec(di->di_ctime);
127 inode->i_mtime = be64_to_timespec(di->di_mtime);
128 inode->i_nlink = be32_to_cpu(di->di_refcount);
129 inode->i_generation = be32_to_cpu(di->di_generation);
131 switch (inode->i_mode & S_IFMT) {
132 case S_IFSOCK: /* fall through */
133 case S_IFBLK: /* fall through */
134 case S_IFCHR: /* fall through */
135 case S_IFIFO:
136 inode->i_rdev = be64_to_cpu(di->di_data[0]);
137 break;
138 case S_IFDIR: /* fall through */
139 case S_IFREG: /* fall through */
140 case S_IFLNK:
141 for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
142 li->li_data[i] = be64_to_cpu(di->di_data[i]);
143 break;
144 default:
145 BUG();
149 static void logfs_inode_to_disk(struct inode *inode, struct logfs_disk_inode*di)
151 struct logfs_inode *li = logfs_inode(inode);
152 int i;
154 di->di_mode = cpu_to_be16(inode->i_mode);
155 di->di_height = li->li_height;
156 di->di_pad = 0;
157 di->di_flags = cpu_to_be32(li->li_flags);
158 di->di_uid = cpu_to_be32(inode->i_uid);
159 di->di_gid = cpu_to_be32(inode->i_gid);
160 di->di_size = cpu_to_be64(i_size_read(inode));
161 di->di_used_bytes = cpu_to_be64(li->li_used_bytes);
162 di->di_atime = timespec_to_be64(inode->i_atime);
163 di->di_ctime = timespec_to_be64(inode->i_ctime);
164 di->di_mtime = timespec_to_be64(inode->i_mtime);
165 di->di_refcount = cpu_to_be32(inode->i_nlink);
166 di->di_generation = cpu_to_be32(inode->i_generation);
168 switch (inode->i_mode & S_IFMT) {
169 case S_IFSOCK: /* fall through */
170 case S_IFBLK: /* fall through */
171 case S_IFCHR: /* fall through */
172 case S_IFIFO:
173 di->di_data[0] = cpu_to_be64(inode->i_rdev);
174 break;
175 case S_IFDIR: /* fall through */
176 case S_IFREG: /* fall through */
177 case S_IFLNK:
178 for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
179 di->di_data[i] = cpu_to_be64(li->li_data[i]);
180 break;
181 default:
182 BUG();
186 static void __logfs_set_blocks(struct inode *inode)
188 struct super_block *sb = inode->i_sb;
189 struct logfs_inode *li = logfs_inode(inode);
191 inode->i_blocks = ULONG_MAX;
192 if (li->li_used_bytes >> sb->s_blocksize_bits < ULONG_MAX)
193 inode->i_blocks = ALIGN(li->li_used_bytes, 512) >> 9;
196 void logfs_set_blocks(struct inode *inode, u64 bytes)
198 struct logfs_inode *li = logfs_inode(inode);
200 li->li_used_bytes = bytes;
201 __logfs_set_blocks(inode);
204 static void prelock_page(struct super_block *sb, struct page *page, int lock)
206 struct logfs_super *super = logfs_super(sb);
208 BUG_ON(!PageLocked(page));
209 if (lock) {
210 BUG_ON(PagePreLocked(page));
211 SetPagePreLocked(page);
212 } else {
213 /* We are in GC path. */
214 if (PagePreLocked(page))
215 super->s_lock_count++;
216 else
217 SetPagePreLocked(page);
221 static void preunlock_page(struct super_block *sb, struct page *page, int lock)
223 struct logfs_super *super = logfs_super(sb);
225 BUG_ON(!PageLocked(page));
226 if (lock)
227 ClearPagePreLocked(page);
228 else {
229 /* We are in GC path. */
230 BUG_ON(!PagePreLocked(page));
231 if (super->s_lock_count)
232 super->s_lock_count--;
233 else
234 ClearPagePreLocked(page);
239 * Logfs is prone to an AB-BA deadlock where one task tries to acquire
240 * s_write_mutex with a locked page and GC tries to get that page while holding
241 * s_write_mutex.
242 * To solve this issue logfs will ignore the page lock iff the page in question
243 * is waiting for s_write_mutex. We annotate this fact by setting PG_pre_locked
244 * in addition to PG_locked.
246 static void logfs_get_wblocks(struct super_block *sb, struct page *page,
247 int lock)
249 struct logfs_super *super = logfs_super(sb);
251 if (page)
252 prelock_page(sb, page, lock);
254 if (lock) {
255 mutex_lock(&super->s_write_mutex);
256 logfs_gc_pass(sb);
257 /* FIXME: We also have to check for shadowed space
258 * and mempool fill grade */
262 static void logfs_put_wblocks(struct super_block *sb, struct page *page,
263 int lock)
265 struct logfs_super *super = logfs_super(sb);
267 if (page)
268 preunlock_page(sb, page, lock);
269 /* Order matters - we must clear PG_pre_locked before releasing
270 * s_write_mutex or we could race against another task. */
271 if (lock)
272 mutex_unlock(&super->s_write_mutex);
275 static struct page *logfs_get_read_page(struct inode *inode, u64 bix,
276 level_t level)
278 return find_or_create_page(inode->i_mapping,
279 logfs_pack_index(bix, level), GFP_NOFS);
282 static void logfs_put_read_page(struct page *page)
284 unlock_page(page);
285 page_cache_release(page);
288 static void logfs_lock_write_page(struct page *page)
290 int loop = 0;
292 while (unlikely(!trylock_page(page))) {
293 if (loop++ > 0x1000) {
294 /* Has been observed once so far... */
295 printk(KERN_ERR "stack at %p\n", &loop);
296 BUG();
298 if (PagePreLocked(page)) {
299 /* Holder of page lock is waiting for us, it
300 * is safe to use this page. */
301 break;
303 /* Some other process has this page locked and has
304 * nothing to do with us. Wait for it to finish.
306 schedule();
308 BUG_ON(!PageLocked(page));
311 static struct page *logfs_get_write_page(struct inode *inode, u64 bix,
312 level_t level)
314 struct address_space *mapping = inode->i_mapping;
315 pgoff_t index = logfs_pack_index(bix, level);
316 struct page *page;
317 int err;
319 repeat:
320 page = find_get_page(mapping, index);
321 if (!page) {
322 page = __page_cache_alloc(GFP_NOFS);
323 if (!page)
324 return NULL;
325 err = add_to_page_cache_lru(page, mapping, index, GFP_NOFS);
326 if (unlikely(err)) {
327 page_cache_release(page);
328 if (err == -EEXIST)
329 goto repeat;
330 return NULL;
332 } else logfs_lock_write_page(page);
333 BUG_ON(!PageLocked(page));
334 return page;
337 static void logfs_unlock_write_page(struct page *page)
339 if (!PagePreLocked(page))
340 unlock_page(page);
343 static void logfs_put_write_page(struct page *page)
345 logfs_unlock_write_page(page);
346 page_cache_release(page);
349 static struct page *logfs_get_page(struct inode *inode, u64 bix, level_t level,
350 int rw)
352 if (rw == READ)
353 return logfs_get_read_page(inode, bix, level);
354 else
355 return logfs_get_write_page(inode, bix, level);
358 static void logfs_put_page(struct page *page, int rw)
360 if (rw == READ)
361 logfs_put_read_page(page);
362 else
363 logfs_put_write_page(page);
366 static unsigned long __get_bits(u64 val, int skip, int no)
368 u64 ret = val;
370 ret >>= skip * no;
371 ret <<= 64 - no;
372 ret >>= 64 - no;
373 return ret;
376 static unsigned long get_bits(u64 val, level_t skip)
378 return __get_bits(val, (__force int)skip, LOGFS_BLOCK_BITS);
381 static inline void init_shadow_tree(struct super_block *sb,
382 struct shadow_tree *tree)
384 struct logfs_super *super = logfs_super(sb);
386 btree_init_mempool64(&tree->new, super->s_btree_pool);
387 btree_init_mempool64(&tree->old, super->s_btree_pool);
390 static void indirect_write_block(struct logfs_block *block)
392 struct page *page;
393 struct inode *inode;
394 int ret;
396 page = block->page;
397 inode = page->mapping->host;
398 logfs_lock_write_page(page);
399 ret = logfs_write_buf(inode, page, 0);
400 logfs_unlock_write_page(page);
402 * This needs some rework. Unless you want your filesystem to run
403 * completely synchronously (you don't), the filesystem will always
404 * report writes as 'successful' before the actual work has been
405 * done. The actual work gets done here and this is where any errors
406 * will show up. And there isn't much we can do about it, really.
408 * Some attempts to fix the errors (move from bad blocks, retry io,...)
409 * have already been done, so anything left should be either a broken
410 * device or a bug somewhere in logfs itself. Being relatively new,
411 * the odds currently favor a bug, so for now the line below isn't
412 * entirely tasteles.
414 BUG_ON(ret);
417 static void inode_write_block(struct logfs_block *block)
419 struct inode *inode;
420 int ret;
422 inode = block->inode;
423 if (inode->i_ino == LOGFS_INO_MASTER)
424 logfs_write_anchor(inode->i_sb);
425 else {
426 ret = __logfs_write_inode(inode, 0);
427 /* see indirect_write_block comment */
428 BUG_ON(ret);
432 static gc_level_t inode_block_level(struct logfs_block *block)
434 BUG_ON(block->inode->i_ino == LOGFS_INO_MASTER);
435 return GC_LEVEL(LOGFS_MAX_LEVELS);
438 static gc_level_t indirect_block_level(struct logfs_block *block)
440 struct page *page;
441 struct inode *inode;
442 u64 bix;
443 level_t level;
445 page = block->page;
446 inode = page->mapping->host;
447 logfs_unpack_index(page->index, &bix, &level);
448 return expand_level(inode->i_ino, level);
452 * This silences a false, yet annoying gcc warning. I hate it when my editor
453 * jumps into bitops.h each time I recompile this file.
454 * TODO: Complain to gcc folks about this and upgrade compiler.
456 static unsigned long fnb(const unsigned long *addr,
457 unsigned long size, unsigned long offset)
459 return find_next_bit(addr, size, offset);
462 static __be64 inode_val0(struct inode *inode)
464 struct logfs_inode *li = logfs_inode(inode);
465 u64 val;
468 * Explicit shifting generates good code, but must match the format
469 * of the structure. Add some paranoia just in case.
471 BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_mode) != 0);
472 BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_height) != 2);
473 BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_flags) != 4);
475 val = (u64)inode->i_mode << 48 |
476 (u64)li->li_height << 40 |
477 (u64)li->li_flags;
478 return cpu_to_be64(val);
481 static int inode_write_alias(struct super_block *sb,
482 struct logfs_block *block, write_alias_t *write_one_alias)
484 struct inode *inode = block->inode;
485 struct logfs_inode *li = logfs_inode(inode);
486 unsigned long pos;
487 u64 ino , bix;
488 __be64 val;
489 level_t level;
490 int err;
492 for (pos = 0; ; pos++) {
493 pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
494 if (pos >= LOGFS_EMBEDDED_FIELDS + INODE_POINTER_OFS)
495 return 0;
497 switch (pos) {
498 case INODE_HEIGHT_OFS:
499 val = inode_val0(inode);
500 break;
501 case INODE_USED_OFS:
502 val = cpu_to_be64(li->li_used_bytes);;
503 break;
504 case INODE_SIZE_OFS:
505 val = cpu_to_be64(i_size_read(inode));
506 break;
507 case INODE_POINTER_OFS ... INODE_POINTER_OFS + LOGFS_EMBEDDED_FIELDS - 1:
508 val = cpu_to_be64(li->li_data[pos - INODE_POINTER_OFS]);
509 break;
510 default:
511 BUG();
514 ino = LOGFS_INO_MASTER;
515 bix = inode->i_ino;
516 level = LEVEL(0);
517 err = write_one_alias(sb, ino, bix, level, pos, val);
518 if (err)
519 return err;
523 static int indirect_write_alias(struct super_block *sb,
524 struct logfs_block *block, write_alias_t *write_one_alias)
526 unsigned long pos;
527 struct page *page = block->page;
528 u64 ino , bix;
529 __be64 *child, val;
530 level_t level;
531 int err;
533 for (pos = 0; ; pos++) {
534 pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
535 if (pos >= LOGFS_BLOCK_FACTOR)
536 return 0;
538 ino = page->mapping->host->i_ino;
539 logfs_unpack_index(page->index, &bix, &level);
540 child = kmap_atomic(page, KM_USER0);
541 val = child[pos];
542 kunmap_atomic(child, KM_USER0);
543 err = write_one_alias(sb, ino, bix, level, pos, val);
544 if (err)
545 return err;
549 int logfs_write_obj_aliases_pagecache(struct super_block *sb)
551 struct logfs_super *super = logfs_super(sb);
552 struct logfs_block *block;
553 int err;
555 list_for_each_entry(block, &super->s_object_alias, alias_list) {
556 err = block->ops->write_alias(sb, block, write_alias_journal);
557 if (err)
558 return err;
560 return 0;
563 void __free_block(struct super_block *sb, struct logfs_block *block)
565 BUG_ON(!list_empty(&block->item_list));
566 list_del(&block->alias_list);
567 mempool_free(block, logfs_super(sb)->s_block_pool);
570 static void inode_free_block(struct super_block *sb, struct logfs_block *block)
572 struct inode *inode = block->inode;
574 logfs_inode(inode)->li_block = NULL;
575 __free_block(sb, block);
578 static void indirect_free_block(struct super_block *sb,
579 struct logfs_block *block)
581 ClearPagePrivate(block->page);
582 block->page->private = 0;
583 __free_block(sb, block);
587 static struct logfs_block_ops inode_block_ops = {
588 .write_block = inode_write_block,
589 .block_level = inode_block_level,
590 .free_block = inode_free_block,
591 .write_alias = inode_write_alias,
594 struct logfs_block_ops indirect_block_ops = {
595 .write_block = indirect_write_block,
596 .block_level = indirect_block_level,
597 .free_block = indirect_free_block,
598 .write_alias = indirect_write_alias,
601 struct logfs_block *__alloc_block(struct super_block *sb,
602 u64 ino, u64 bix, level_t level)
604 struct logfs_super *super = logfs_super(sb);
605 struct logfs_block *block;
607 block = mempool_alloc(super->s_block_pool, GFP_NOFS);
608 memset(block, 0, sizeof(*block));
609 INIT_LIST_HEAD(&block->alias_list);
610 INIT_LIST_HEAD(&block->item_list);
611 block->sb = sb;
612 block->ino = ino;
613 block->bix = bix;
614 block->level = level;
615 return block;
618 static void alloc_inode_block(struct inode *inode)
620 struct logfs_inode *li = logfs_inode(inode);
621 struct logfs_block *block;
623 if (li->li_block)
624 return;
626 block = __alloc_block(inode->i_sb, LOGFS_INO_MASTER, inode->i_ino, 0);
627 block->inode = inode;
628 li->li_block = block;
629 block->ops = &inode_block_ops;
632 void initialize_block_counters(struct page *page, struct logfs_block *block,
633 __be64 *array, int page_is_empty)
635 u64 ptr;
636 int i, start;
638 block->partial = 0;
639 block->full = 0;
640 start = 0;
641 if (page->index < first_indirect_block()) {
642 /* Counters are pointless on level 0 */
643 return;
645 if (page->index == first_indirect_block()) {
646 /* Skip unused pointers */
647 start = I0_BLOCKS;
648 block->full = I0_BLOCKS;
650 if (!page_is_empty) {
651 for (i = start; i < LOGFS_BLOCK_FACTOR; i++) {
652 ptr = be64_to_cpu(array[i]);
653 if (ptr)
654 block->partial++;
655 if (ptr & LOGFS_FULLY_POPULATED)
656 block->full++;
661 static void alloc_data_block(struct inode *inode, struct page *page)
663 struct logfs_block *block;
664 u64 bix;
665 level_t level;
667 if (PagePrivate(page))
668 return;
670 logfs_unpack_index(page->index, &bix, &level);
671 block = __alloc_block(inode->i_sb, inode->i_ino, bix, level);
672 block->page = page;
673 SetPagePrivate(page);
674 page->private = (unsigned long)block;
675 block->ops = &indirect_block_ops;
678 static void alloc_indirect_block(struct inode *inode, struct page *page,
679 int page_is_empty)
681 struct logfs_block *block;
682 __be64 *array;
684 if (PagePrivate(page))
685 return;
687 alloc_data_block(inode, page);
689 block = logfs_block(page);
690 array = kmap_atomic(page, KM_USER0);
691 initialize_block_counters(page, block, array, page_is_empty);
692 kunmap_atomic(array, KM_USER0);
695 static void block_set_pointer(struct page *page, int index, u64 ptr)
697 struct logfs_block *block = logfs_block(page);
698 __be64 *array;
699 u64 oldptr;
701 BUG_ON(!block);
702 array = kmap_atomic(page, KM_USER0);
703 oldptr = be64_to_cpu(array[index]);
704 array[index] = cpu_to_be64(ptr);
705 kunmap_atomic(array, KM_USER0);
706 SetPageUptodate(page);
708 block->full += !!(ptr & LOGFS_FULLY_POPULATED)
709 - !!(oldptr & LOGFS_FULLY_POPULATED);
710 block->partial += !!ptr - !!oldptr;
713 static u64 block_get_pointer(struct page *page, int index)
715 __be64 *block;
716 u64 ptr;
718 block = kmap_atomic(page, KM_USER0);
719 ptr = be64_to_cpu(block[index]);
720 kunmap_atomic(block, KM_USER0);
721 return ptr;
724 static int logfs_read_empty(struct page *page)
726 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
727 return 0;
730 static int logfs_read_direct(struct inode *inode, struct page *page)
732 struct logfs_inode *li = logfs_inode(inode);
733 pgoff_t index = page->index;
734 u64 block;
736 block = li->li_data[index];
737 if (!block)
738 return logfs_read_empty(page);
740 return logfs_segment_read(inode, page, block, index, 0);
743 static int logfs_read_loop(struct inode *inode, struct page *page,
744 int rw_context)
746 struct logfs_inode *li = logfs_inode(inode);
747 u64 bix, bofs = li->li_data[INDIRECT_INDEX];
748 level_t level, target_level;
749 int ret;
750 struct page *ipage;
752 logfs_unpack_index(page->index, &bix, &target_level);
753 if (!bofs)
754 return logfs_read_empty(page);
756 if (bix >= maxbix(li->li_height))
757 return logfs_read_empty(page);
759 for (level = LEVEL(li->li_height);
760 (__force u8)level > (__force u8)target_level;
761 level = SUBLEVEL(level)){
762 ipage = logfs_get_page(inode, bix, level, rw_context);
763 if (!ipage)
764 return -ENOMEM;
766 ret = logfs_segment_read(inode, ipage, bofs, bix, level);
767 if (ret) {
768 logfs_put_read_page(ipage);
769 return ret;
772 bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
773 logfs_put_page(ipage, rw_context);
774 if (!bofs)
775 return logfs_read_empty(page);
778 return logfs_segment_read(inode, page, bofs, bix, 0);
781 static int logfs_read_block(struct inode *inode, struct page *page,
782 int rw_context)
784 pgoff_t index = page->index;
786 if (index < I0_BLOCKS)
787 return logfs_read_direct(inode, page);
788 return logfs_read_loop(inode, page, rw_context);
791 static int logfs_exist_loop(struct inode *inode, u64 bix)
793 struct logfs_inode *li = logfs_inode(inode);
794 u64 bofs = li->li_data[INDIRECT_INDEX];
795 level_t level;
796 int ret;
797 struct page *ipage;
799 if (!bofs)
800 return 0;
801 if (bix >= maxbix(li->li_height))
802 return 0;
804 for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
805 ipage = logfs_get_read_page(inode, bix, level);
806 if (!ipage)
807 return -ENOMEM;
809 ret = logfs_segment_read(inode, ipage, bofs, bix, level);
810 if (ret) {
811 logfs_put_read_page(ipage);
812 return ret;
815 bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
816 logfs_put_read_page(ipage);
817 if (!bofs)
818 return 0;
821 return 1;
824 int logfs_exist_block(struct inode *inode, u64 bix)
826 struct logfs_inode *li = logfs_inode(inode);
828 if (bix < I0_BLOCKS)
829 return !!li->li_data[bix];
830 return logfs_exist_loop(inode, bix);
833 static u64 seek_holedata_direct(struct inode *inode, u64 bix, int data)
835 struct logfs_inode *li = logfs_inode(inode);
837 for (; bix < I0_BLOCKS; bix++)
838 if (data ^ (li->li_data[bix] == 0))
839 return bix;
840 return I0_BLOCKS;
843 static u64 seek_holedata_loop(struct inode *inode, u64 bix, int data)
845 struct logfs_inode *li = logfs_inode(inode);
846 __be64 *rblock;
847 u64 increment, bofs = li->li_data[INDIRECT_INDEX];
848 level_t level;
849 int ret, slot;
850 struct page *page;
852 BUG_ON(!bofs);
854 for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
855 increment = 1 << (LOGFS_BLOCK_BITS * ((__force u8)level-1));
856 page = logfs_get_read_page(inode, bix, level);
857 if (!page)
858 return bix;
860 ret = logfs_segment_read(inode, page, bofs, bix, level);
861 if (ret) {
862 logfs_put_read_page(page);
863 return bix;
866 slot = get_bits(bix, SUBLEVEL(level));
867 rblock = kmap_atomic(page, KM_USER0);
868 while (slot < LOGFS_BLOCK_FACTOR) {
869 if (data && (rblock[slot] != 0))
870 break;
871 if (!data && !(be64_to_cpu(rblock[slot]) & LOGFS_FULLY_POPULATED))
872 break;
873 slot++;
874 bix += increment;
875 bix &= ~(increment - 1);
877 if (slot >= LOGFS_BLOCK_FACTOR) {
878 kunmap_atomic(rblock, KM_USER0);
879 logfs_put_read_page(page);
880 return bix;
882 bofs = be64_to_cpu(rblock[slot]);
883 kunmap_atomic(rblock, KM_USER0);
884 logfs_put_read_page(page);
885 if (!bofs) {
886 BUG_ON(data);
887 return bix;
890 return bix;
894 * logfs_seek_hole - find next hole starting at a given block index
895 * @inode: inode to search in
896 * @bix: block index to start searching
898 * Returns next hole. If the file doesn't contain any further holes, the
899 * block address next to eof is returned instead.
901 u64 logfs_seek_hole(struct inode *inode, u64 bix)
903 struct logfs_inode *li = logfs_inode(inode);
905 if (bix < I0_BLOCKS) {
906 bix = seek_holedata_direct(inode, bix, 0);
907 if (bix < I0_BLOCKS)
908 return bix;
911 if (!li->li_data[INDIRECT_INDEX])
912 return bix;
913 else if (li->li_data[INDIRECT_INDEX] & LOGFS_FULLY_POPULATED)
914 bix = maxbix(li->li_height);
915 else {
916 bix = seek_holedata_loop(inode, bix, 0);
917 if (bix < maxbix(li->li_height))
918 return bix;
919 /* Should not happen anymore. But if some port writes semi-
920 * corrupt images (as this one used to) we might run into it.
922 WARN_ON_ONCE(bix == maxbix(li->li_height));
925 return bix;
928 static u64 __logfs_seek_data(struct inode *inode, u64 bix)
930 struct logfs_inode *li = logfs_inode(inode);
932 if (bix < I0_BLOCKS) {
933 bix = seek_holedata_direct(inode, bix, 1);
934 if (bix < I0_BLOCKS)
935 return bix;
938 if (bix < maxbix(li->li_height)) {
939 if (!li->li_data[INDIRECT_INDEX])
940 bix = maxbix(li->li_height);
941 else
942 return seek_holedata_loop(inode, bix, 1);
945 return bix;
949 * logfs_seek_data - find next data block after a given block index
950 * @inode: inode to search in
951 * @bix: block index to start searching
953 * Returns next data block. If the file doesn't contain any further data
954 * blocks, the last block in the file is returned instead.
956 u64 logfs_seek_data(struct inode *inode, u64 bix)
958 struct super_block *sb = inode->i_sb;
959 u64 ret, end;
961 ret = __logfs_seek_data(inode, bix);
962 end = i_size_read(inode) >> sb->s_blocksize_bits;
963 if (ret >= end)
964 ret = max(bix, end);
965 return ret;
968 static int logfs_is_valid_direct(struct logfs_inode *li, u64 bix, u64 ofs)
970 return pure_ofs(li->li_data[bix]) == ofs;
973 static int __logfs_is_valid_loop(struct inode *inode, u64 bix,
974 u64 ofs, u64 bofs)
976 struct logfs_inode *li = logfs_inode(inode);
977 level_t level;
978 int ret;
979 struct page *page;
981 for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)){
982 page = logfs_get_write_page(inode, bix, level);
983 BUG_ON(!page);
985 ret = logfs_segment_read(inode, page, bofs, bix, level);
986 if (ret) {
987 logfs_put_write_page(page);
988 return 0;
991 bofs = block_get_pointer(page, get_bits(bix, SUBLEVEL(level)));
992 logfs_put_write_page(page);
993 if (!bofs)
994 return 0;
996 if (pure_ofs(bofs) == ofs)
997 return 1;
999 return 0;
1002 static int logfs_is_valid_loop(struct inode *inode, u64 bix, u64 ofs)
1004 struct logfs_inode *li = logfs_inode(inode);
1005 u64 bofs = li->li_data[INDIRECT_INDEX];
1007 if (!bofs)
1008 return 0;
1010 if (bix >= maxbix(li->li_height))
1011 return 0;
1013 if (pure_ofs(bofs) == ofs)
1014 return 1;
1016 return __logfs_is_valid_loop(inode, bix, ofs, bofs);
1019 static int __logfs_is_valid_block(struct inode *inode, u64 bix, u64 ofs)
1021 struct logfs_inode *li = logfs_inode(inode);
1023 if ((inode->i_nlink == 0) && atomic_read(&inode->i_count) == 1)
1024 return 0;
1026 if (bix < I0_BLOCKS)
1027 return logfs_is_valid_direct(li, bix, ofs);
1028 return logfs_is_valid_loop(inode, bix, ofs);
1032 * logfs_is_valid_block - check whether this block is still valid
1034 * @sb - superblock
1035 * @ofs - block physical offset
1036 * @ino - block inode number
1037 * @bix - block index
1038 * @level - block level
1040 * Returns 0 if the block is invalid, 1 if it is valid and 2 if it will
1041 * become invalid once the journal is written.
1043 int logfs_is_valid_block(struct super_block *sb, u64 ofs, u64 ino, u64 bix,
1044 gc_level_t gc_level)
1046 struct logfs_super *super = logfs_super(sb);
1047 struct inode *inode;
1048 int ret, cookie;
1050 /* Umount closes a segment with free blocks remaining. Those
1051 * blocks are by definition invalid. */
1052 if (ino == -1)
1053 return 0;
1055 LOGFS_BUG_ON((u64)(u_long)ino != ino, sb);
1057 inode = logfs_safe_iget(sb, ino, &cookie);
1058 if (IS_ERR(inode))
1059 goto invalid;
1061 ret = __logfs_is_valid_block(inode, bix, ofs);
1062 logfs_safe_iput(inode, cookie);
1063 if (ret)
1064 return ret;
1066 invalid:
1067 /* Block is nominally invalid, but may still sit in the shadow tree,
1068 * waiting for a journal commit.
1070 if (btree_lookup64(&super->s_shadow_tree.old, ofs))
1071 return 2;
1072 return 0;
1075 int logfs_readpage_nolock(struct page *page)
1077 struct inode *inode = page->mapping->host;
1078 int ret = -EIO;
1080 ret = logfs_read_block(inode, page, READ);
1082 if (ret) {
1083 ClearPageUptodate(page);
1084 SetPageError(page);
1085 } else {
1086 SetPageUptodate(page);
1087 ClearPageError(page);
1089 flush_dcache_page(page);
1091 return ret;
1094 static int logfs_reserve_bytes(struct inode *inode, int bytes)
1096 struct logfs_super *super = logfs_super(inode->i_sb);
1097 u64 available = super->s_free_bytes + super->s_dirty_free_bytes
1098 - super->s_dirty_used_bytes - super->s_dirty_pages;
1100 if (!bytes)
1101 return 0;
1103 if (available < bytes)
1104 return -ENOSPC;
1106 if (available < bytes + super->s_root_reserve &&
1107 !capable(CAP_SYS_RESOURCE))
1108 return -ENOSPC;
1110 return 0;
1113 int get_page_reserve(struct inode *inode, struct page *page)
1115 struct logfs_super *super = logfs_super(inode->i_sb);
1116 int ret;
1118 if (logfs_block(page) && logfs_block(page)->reserved_bytes)
1119 return 0;
1121 logfs_get_wblocks(inode->i_sb, page, WF_LOCK);
1122 ret = logfs_reserve_bytes(inode, 6 * LOGFS_MAX_OBJECTSIZE);
1123 if (!ret) {
1124 alloc_data_block(inode, page);
1125 logfs_block(page)->reserved_bytes += 6 * LOGFS_MAX_OBJECTSIZE;
1126 super->s_dirty_pages += 6 * LOGFS_MAX_OBJECTSIZE;
1128 logfs_put_wblocks(inode->i_sb, page, WF_LOCK);
1129 return ret;
1133 * We are protected by write lock. Push victims up to superblock level
1134 * and release transaction when appropriate.
1136 /* FIXME: This is currently called from the wrong spots. */
1137 static void logfs_handle_transaction(struct inode *inode,
1138 struct logfs_transaction *ta)
1140 struct logfs_super *super = logfs_super(inode->i_sb);
1142 if (!ta)
1143 return;
1144 logfs_inode(inode)->li_block->ta = NULL;
1146 if (inode->i_ino != LOGFS_INO_MASTER) {
1147 BUG(); /* FIXME: Yes, this needs more thought */
1148 /* just remember the transaction until inode is written */
1149 //BUG_ON(logfs_inode(inode)->li_transaction);
1150 //logfs_inode(inode)->li_transaction = ta;
1151 return;
1154 switch (ta->state) {
1155 case CREATE_1: /* fall through */
1156 case UNLINK_1:
1157 BUG_ON(super->s_victim_ino);
1158 super->s_victim_ino = ta->ino;
1159 break;
1160 case CREATE_2: /* fall through */
1161 case UNLINK_2:
1162 BUG_ON(super->s_victim_ino != ta->ino);
1163 super->s_victim_ino = 0;
1164 /* transaction ends here - free it */
1165 kfree(ta);
1166 break;
1167 case CROSS_RENAME_1:
1168 BUG_ON(super->s_rename_dir);
1169 BUG_ON(super->s_rename_pos);
1170 super->s_rename_dir = ta->dir;
1171 super->s_rename_pos = ta->pos;
1172 break;
1173 case CROSS_RENAME_2:
1174 BUG_ON(super->s_rename_dir != ta->dir);
1175 BUG_ON(super->s_rename_pos != ta->pos);
1176 super->s_rename_dir = 0;
1177 super->s_rename_pos = 0;
1178 kfree(ta);
1179 break;
1180 case TARGET_RENAME_1:
1181 BUG_ON(super->s_rename_dir);
1182 BUG_ON(super->s_rename_pos);
1183 BUG_ON(super->s_victim_ino);
1184 super->s_rename_dir = ta->dir;
1185 super->s_rename_pos = ta->pos;
1186 super->s_victim_ino = ta->ino;
1187 break;
1188 case TARGET_RENAME_2:
1189 BUG_ON(super->s_rename_dir != ta->dir);
1190 BUG_ON(super->s_rename_pos != ta->pos);
1191 BUG_ON(super->s_victim_ino != ta->ino);
1192 super->s_rename_dir = 0;
1193 super->s_rename_pos = 0;
1194 break;
1195 case TARGET_RENAME_3:
1196 BUG_ON(super->s_rename_dir);
1197 BUG_ON(super->s_rename_pos);
1198 BUG_ON(super->s_victim_ino != ta->ino);
1199 super->s_victim_ino = 0;
1200 kfree(ta);
1201 break;
1202 default:
1203 BUG();
1208 * Not strictly a reservation, but rather a check that we still have enough
1209 * space to satisfy the write.
1211 static int logfs_reserve_blocks(struct inode *inode, int blocks)
1213 return logfs_reserve_bytes(inode, blocks * LOGFS_MAX_OBJECTSIZE);
1216 struct write_control {
1217 u64 ofs;
1218 long flags;
1221 static struct logfs_shadow *alloc_shadow(struct inode *inode, u64 bix,
1222 level_t level, u64 old_ofs)
1224 struct logfs_super *super = logfs_super(inode->i_sb);
1225 struct logfs_shadow *shadow;
1227 shadow = mempool_alloc(super->s_shadow_pool, GFP_NOFS);
1228 memset(shadow, 0, sizeof(*shadow));
1229 shadow->ino = inode->i_ino;
1230 shadow->bix = bix;
1231 shadow->gc_level = expand_level(inode->i_ino, level);
1232 shadow->old_ofs = old_ofs & ~LOGFS_FULLY_POPULATED;
1233 return shadow;
1236 static void free_shadow(struct inode *inode, struct logfs_shadow *shadow)
1238 struct logfs_super *super = logfs_super(inode->i_sb);
1240 mempool_free(shadow, super->s_shadow_pool);
1244 * fill_shadow_tree - Propagate shadow tree changes due to a write
1245 * @inode: Inode owning the page
1246 * @page: Struct page that was written
1247 * @shadow: Shadow for the current write
1249 * Writes in logfs can result in two semi-valid objects. The old object
1250 * is still valid as long as it can be reached by following pointers on
1251 * the medium. Only when writes propagate all the way up to the journal
1252 * has the new object safely replaced the old one.
1254 * To handle this problem, a struct logfs_shadow is used to represent
1255 * every single write. It is attached to the indirect block, which is
1256 * marked dirty. When the indirect block is written, its shadows are
1257 * handed up to the next indirect block (or inode). Untimately they
1258 * will reach the master inode and be freed upon journal commit.
1260 * This function handles a single step in the propagation. It adds the
1261 * shadow for the current write to the tree, along with any shadows in
1262 * the page's tree, in case it was an indirect block. If a page is
1263 * written, the inode parameter is left NULL, if an inode is written,
1264 * the page parameter is left NULL.
1266 static void fill_shadow_tree(struct inode *inode, struct page *page,
1267 struct logfs_shadow *shadow)
1269 struct logfs_super *super = logfs_super(inode->i_sb);
1270 struct logfs_block *block = logfs_block(page);
1271 struct shadow_tree *tree = &super->s_shadow_tree;
1273 if (PagePrivate(page)) {
1274 if (block->alias_map)
1275 super->s_no_object_aliases -= bitmap_weight(
1276 block->alias_map, LOGFS_BLOCK_FACTOR);
1277 logfs_handle_transaction(inode, block->ta);
1278 block->ops->free_block(inode->i_sb, block);
1280 if (shadow) {
1281 if (shadow->old_ofs)
1282 btree_insert64(&tree->old, shadow->old_ofs, shadow,
1283 GFP_NOFS);
1284 else
1285 btree_insert64(&tree->new, shadow->new_ofs, shadow,
1286 GFP_NOFS);
1288 super->s_dirty_used_bytes += shadow->new_len;
1289 super->s_dirty_free_bytes += shadow->old_len;
1293 static void logfs_set_alias(struct super_block *sb, struct logfs_block *block,
1294 long child_no)
1296 struct logfs_super *super = logfs_super(sb);
1298 if (block->inode && block->inode->i_ino == LOGFS_INO_MASTER) {
1299 /* Aliases in the master inode are pointless. */
1300 return;
1303 if (!test_bit(child_no, block->alias_map)) {
1304 set_bit(child_no, block->alias_map);
1305 super->s_no_object_aliases++;
1307 list_move_tail(&block->alias_list, &super->s_object_alias);
1311 * Object aliases can and often do change the size and occupied space of a
1312 * file. So not only do we have to change the pointers, we also have to
1313 * change inode->i_size and li->li_used_bytes. Which is done by setting
1314 * another two object aliases for the inode itself.
1316 static void set_iused(struct inode *inode, struct logfs_shadow *shadow)
1318 struct logfs_inode *li = logfs_inode(inode);
1320 if (shadow->new_len == shadow->old_len)
1321 return;
1323 alloc_inode_block(inode);
1324 li->li_used_bytes += shadow->new_len - shadow->old_len;
1325 __logfs_set_blocks(inode);
1326 logfs_set_alias(inode->i_sb, li->li_block, INODE_USED_OFS);
1327 logfs_set_alias(inode->i_sb, li->li_block, INODE_SIZE_OFS);
1330 static int logfs_write_i0(struct inode *inode, struct page *page,
1331 struct write_control *wc)
1333 struct logfs_shadow *shadow;
1334 u64 bix;
1335 level_t level;
1336 int full, err = 0;
1338 logfs_unpack_index(page->index, &bix, &level);
1339 if (wc->ofs == 0)
1340 if (logfs_reserve_blocks(inode, 1))
1341 return -ENOSPC;
1343 shadow = alloc_shadow(inode, bix, level, wc->ofs);
1344 if (wc->flags & WF_WRITE)
1345 err = logfs_segment_write(inode, page, shadow);
1346 if (wc->flags & WF_DELETE)
1347 logfs_segment_delete(inode, shadow);
1348 if (err) {
1349 free_shadow(inode, shadow);
1350 return err;
1353 set_iused(inode, shadow);
1354 full = 1;
1355 if (level != 0) {
1356 alloc_indirect_block(inode, page, 0);
1357 full = logfs_block(page)->full == LOGFS_BLOCK_FACTOR;
1359 fill_shadow_tree(inode, page, shadow);
1360 wc->ofs = shadow->new_ofs;
1361 if (wc->ofs && full)
1362 wc->ofs |= LOGFS_FULLY_POPULATED;
1363 return 0;
1366 static int logfs_write_direct(struct inode *inode, struct page *page,
1367 long flags)
1369 struct logfs_inode *li = logfs_inode(inode);
1370 struct write_control wc = {
1371 .ofs = li->li_data[page->index],
1372 .flags = flags,
1374 int err;
1376 alloc_inode_block(inode);
1378 err = logfs_write_i0(inode, page, &wc);
1379 if (err)
1380 return err;
1382 li->li_data[page->index] = wc.ofs;
1383 logfs_set_alias(inode->i_sb, li->li_block,
1384 page->index + INODE_POINTER_OFS);
1385 return 0;
1388 static int ptr_change(u64 ofs, struct page *page)
1390 struct logfs_block *block = logfs_block(page);
1391 int empty0, empty1, full0, full1;
1393 empty0 = ofs == 0;
1394 empty1 = block->partial == 0;
1395 if (empty0 != empty1)
1396 return 1;
1398 /* The !! is necessary to shrink result to int */
1399 full0 = !!(ofs & LOGFS_FULLY_POPULATED);
1400 full1 = block->full == LOGFS_BLOCK_FACTOR;
1401 if (full0 != full1)
1402 return 1;
1403 return 0;
1406 static int __logfs_write_rec(struct inode *inode, struct page *page,
1407 struct write_control *this_wc,
1408 pgoff_t bix, level_t target_level, level_t level)
1410 int ret, page_empty = 0;
1411 int child_no = get_bits(bix, SUBLEVEL(level));
1412 struct page *ipage;
1413 struct write_control child_wc = {
1414 .flags = this_wc->flags,
1417 ipage = logfs_get_write_page(inode, bix, level);
1418 if (!ipage)
1419 return -ENOMEM;
1421 if (this_wc->ofs) {
1422 ret = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level);
1423 if (ret)
1424 goto out;
1425 } else if (!PageUptodate(ipage)) {
1426 page_empty = 1;
1427 logfs_read_empty(ipage);
1430 child_wc.ofs = block_get_pointer(ipage, child_no);
1432 if ((__force u8)level-1 > (__force u8)target_level)
1433 ret = __logfs_write_rec(inode, page, &child_wc, bix,
1434 target_level, SUBLEVEL(level));
1435 else
1436 ret = logfs_write_i0(inode, page, &child_wc);
1438 if (ret)
1439 goto out;
1441 alloc_indirect_block(inode, ipage, page_empty);
1442 block_set_pointer(ipage, child_no, child_wc.ofs);
1443 /* FIXME: first condition seems superfluous */
1444 if (child_wc.ofs || logfs_block(ipage)->partial)
1445 this_wc->flags |= WF_WRITE;
1446 /* the condition on this_wc->ofs ensures that we won't consume extra
1447 * space for indirect blocks in the future, which we cannot reserve */
1448 if (!this_wc->ofs || ptr_change(this_wc->ofs, ipage))
1449 ret = logfs_write_i0(inode, ipage, this_wc);
1450 else
1451 logfs_set_alias(inode->i_sb, logfs_block(ipage), child_no);
1452 out:
1453 logfs_put_write_page(ipage);
1454 return ret;
1457 static int logfs_write_rec(struct inode *inode, struct page *page,
1458 pgoff_t bix, level_t target_level, long flags)
1460 struct logfs_inode *li = logfs_inode(inode);
1461 struct write_control wc = {
1462 .ofs = li->li_data[INDIRECT_INDEX],
1463 .flags = flags,
1465 int ret;
1467 alloc_inode_block(inode);
1469 if (li->li_height > (__force u8)target_level)
1470 ret = __logfs_write_rec(inode, page, &wc, bix, target_level,
1471 LEVEL(li->li_height));
1472 else
1473 ret = logfs_write_i0(inode, page, &wc);
1474 if (ret)
1475 return ret;
1477 if (li->li_data[INDIRECT_INDEX] != wc.ofs) {
1478 li->li_data[INDIRECT_INDEX] = wc.ofs;
1479 logfs_set_alias(inode->i_sb, li->li_block,
1480 INDIRECT_INDEX + INODE_POINTER_OFS);
1482 return ret;
1485 void logfs_add_transaction(struct inode *inode, struct logfs_transaction *ta)
1487 alloc_inode_block(inode);
1488 logfs_inode(inode)->li_block->ta = ta;
1491 void logfs_del_transaction(struct inode *inode, struct logfs_transaction *ta)
1493 struct logfs_block *block = logfs_inode(inode)->li_block;
1495 if (block && block->ta)
1496 block->ta = NULL;
1499 static int grow_inode(struct inode *inode, u64 bix, level_t level)
1501 struct logfs_inode *li = logfs_inode(inode);
1502 u8 height = (__force u8)level;
1503 struct page *page;
1504 struct write_control wc = {
1505 .flags = WF_WRITE,
1507 int err;
1509 BUG_ON(height > 5 || li->li_height > 5);
1510 while (height > li->li_height || bix >= maxbix(li->li_height)) {
1511 page = logfs_get_write_page(inode, I0_BLOCKS + 1,
1512 LEVEL(li->li_height + 1));
1513 if (!page)
1514 return -ENOMEM;
1515 logfs_read_empty(page);
1516 alloc_indirect_block(inode, page, 1);
1517 block_set_pointer(page, 0, li->li_data[INDIRECT_INDEX]);
1518 err = logfs_write_i0(inode, page, &wc);
1519 logfs_put_write_page(page);
1520 if (err)
1521 return err;
1522 li->li_data[INDIRECT_INDEX] = wc.ofs;
1523 wc.ofs = 0;
1524 li->li_height++;
1525 logfs_set_alias(inode->i_sb, li->li_block, INODE_HEIGHT_OFS);
1527 return 0;
1530 static int __logfs_write_buf(struct inode *inode, struct page *page, long flags)
1532 struct logfs_super *super = logfs_super(inode->i_sb);
1533 pgoff_t index = page->index;
1534 u64 bix;
1535 level_t level;
1536 int err;
1538 flags |= WF_WRITE | WF_DELETE;
1539 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1541 logfs_unpack_index(index, &bix, &level);
1542 if (logfs_block(page) && logfs_block(page)->reserved_bytes)
1543 super->s_dirty_pages -= logfs_block(page)->reserved_bytes;
1545 if (index < I0_BLOCKS)
1546 return logfs_write_direct(inode, page, flags);
1548 bix = adjust_bix(bix, level);
1549 err = grow_inode(inode, bix, level);
1550 if (err)
1551 return err;
1552 return logfs_write_rec(inode, page, bix, level, flags);
1555 int logfs_write_buf(struct inode *inode, struct page *page, long flags)
1557 struct super_block *sb = inode->i_sb;
1558 int ret;
1560 logfs_get_wblocks(sb, page, flags & WF_LOCK);
1561 ret = __logfs_write_buf(inode, page, flags);
1562 logfs_put_wblocks(sb, page, flags & WF_LOCK);
1563 return ret;
1566 static int __logfs_delete(struct inode *inode, struct page *page)
1568 long flags = WF_DELETE;
1570 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1572 if (page->index < I0_BLOCKS)
1573 return logfs_write_direct(inode, page, flags);
1574 return logfs_write_rec(inode, page, page->index, 0, flags);
1577 int logfs_delete(struct inode *inode, pgoff_t index,
1578 struct shadow_tree *shadow_tree)
1580 struct super_block *sb = inode->i_sb;
1581 struct page *page;
1582 int ret;
1584 page = logfs_get_read_page(inode, index, 0);
1585 if (!page)
1586 return -ENOMEM;
1588 logfs_get_wblocks(sb, page, 1);
1589 ret = __logfs_delete(inode, page);
1590 logfs_put_wblocks(sb, page, 1);
1592 logfs_put_read_page(page);
1594 return ret;
1597 /* Rewrite cannot mark the inode dirty but has to write it immediatly. */
1598 int logfs_rewrite_block(struct inode *inode, u64 bix, u64 ofs,
1599 gc_level_t gc_level, long flags)
1601 level_t level = shrink_level(gc_level);
1602 struct page *page;
1603 int err;
1605 page = logfs_get_write_page(inode, bix, level);
1606 if (!page)
1607 return -ENOMEM;
1609 err = logfs_segment_read(inode, page, ofs, bix, level);
1610 if (!err) {
1611 if (level != 0)
1612 alloc_indirect_block(inode, page, 0);
1613 err = logfs_write_buf(inode, page, flags);
1615 logfs_put_write_page(page);
1616 return err;
1619 static int truncate_data_block(struct inode *inode, struct page *page,
1620 u64 ofs, struct logfs_shadow *shadow, u64 size)
1622 loff_t pageofs = page->index << inode->i_sb->s_blocksize_bits;
1623 u64 bix;
1624 level_t level;
1625 int err;
1627 /* Does truncation happen within this page? */
1628 if (size <= pageofs || size - pageofs >= PAGE_SIZE)
1629 return 0;
1631 logfs_unpack_index(page->index, &bix, &level);
1632 BUG_ON(level != 0);
1634 err = logfs_segment_read(inode, page, ofs, bix, level);
1635 if (err)
1636 return err;
1638 zero_user_segment(page, size - pageofs, PAGE_CACHE_SIZE);
1639 return logfs_segment_write(inode, page, shadow);
1642 static int logfs_truncate_i0(struct inode *inode, struct page *page,
1643 struct write_control *wc, u64 size)
1645 struct logfs_shadow *shadow;
1646 u64 bix;
1647 level_t level;
1648 int err = 0;
1650 logfs_unpack_index(page->index, &bix, &level);
1651 BUG_ON(level != 0);
1652 shadow = alloc_shadow(inode, bix, level, wc->ofs);
1654 err = truncate_data_block(inode, page, wc->ofs, shadow, size);
1655 if (err) {
1656 free_shadow(inode, shadow);
1657 return err;
1660 logfs_segment_delete(inode, shadow);
1661 set_iused(inode, shadow);
1662 fill_shadow_tree(inode, page, shadow);
1663 wc->ofs = shadow->new_ofs;
1664 return 0;
1667 static int logfs_truncate_direct(struct inode *inode, u64 size)
1669 struct logfs_inode *li = logfs_inode(inode);
1670 struct write_control wc;
1671 struct page *page;
1672 int e;
1673 int err;
1675 alloc_inode_block(inode);
1677 for (e = I0_BLOCKS - 1; e >= 0; e--) {
1678 if (size > (e+1) * LOGFS_BLOCKSIZE)
1679 break;
1681 wc.ofs = li->li_data[e];
1682 if (!wc.ofs)
1683 continue;
1685 page = logfs_get_write_page(inode, e, 0);
1686 if (!page)
1687 return -ENOMEM;
1688 err = logfs_segment_read(inode, page, wc.ofs, e, 0);
1689 if (err) {
1690 logfs_put_write_page(page);
1691 return err;
1693 err = logfs_truncate_i0(inode, page, &wc, size);
1694 logfs_put_write_page(page);
1695 if (err)
1696 return err;
1698 li->li_data[e] = wc.ofs;
1700 return 0;
1703 /* FIXME: these need to become per-sb once we support different blocksizes */
1704 static u64 __logfs_step[] = {
1706 I1_BLOCKS,
1707 I2_BLOCKS,
1708 I3_BLOCKS,
1711 static u64 __logfs_start_index[] = {
1712 I0_BLOCKS,
1713 I1_BLOCKS,
1714 I2_BLOCKS,
1715 I3_BLOCKS
1718 static inline u64 logfs_step(level_t level)
1720 return __logfs_step[(__force u8)level];
1723 static inline u64 logfs_factor(u8 level)
1725 return __logfs_step[level] * LOGFS_BLOCKSIZE;
1728 static inline u64 logfs_start_index(level_t level)
1730 return __logfs_start_index[(__force u8)level];
1733 static void logfs_unpack_raw_index(pgoff_t index, u64 *bix, level_t *level)
1735 logfs_unpack_index(index, bix, level);
1736 if (*bix <= logfs_start_index(SUBLEVEL(*level)))
1737 *bix = 0;
1740 static int __logfs_truncate_rec(struct inode *inode, struct page *ipage,
1741 struct write_control *this_wc, u64 size)
1743 int truncate_happened = 0;
1744 int e, err = 0;
1745 u64 bix, child_bix, next_bix;
1746 level_t level;
1747 struct page *page;
1748 struct write_control child_wc = { /* FIXME: flags */ };
1750 logfs_unpack_raw_index(ipage->index, &bix, &level);
1751 err = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level);
1752 if (err)
1753 return err;
1755 for (e = LOGFS_BLOCK_FACTOR - 1; e >= 0; e--) {
1756 child_bix = bix + e * logfs_step(SUBLEVEL(level));
1757 next_bix = child_bix + logfs_step(SUBLEVEL(level));
1758 if (size > next_bix * LOGFS_BLOCKSIZE)
1759 break;
1761 child_wc.ofs = pure_ofs(block_get_pointer(ipage, e));
1762 if (!child_wc.ofs)
1763 continue;
1765 page = logfs_get_write_page(inode, child_bix, SUBLEVEL(level));
1766 if (!page)
1767 return -ENOMEM;
1769 if ((__force u8)level > 1)
1770 err = __logfs_truncate_rec(inode, page, &child_wc, size);
1771 else
1772 err = logfs_truncate_i0(inode, page, &child_wc, size);
1773 logfs_put_write_page(page);
1774 if (err)
1775 return err;
1777 truncate_happened = 1;
1778 alloc_indirect_block(inode, ipage, 0);
1779 block_set_pointer(ipage, e, child_wc.ofs);
1782 if (!truncate_happened) {
1783 printk("ineffectual truncate (%lx, %lx, %llx)\n", inode->i_ino, ipage->index, size);
1784 return 0;
1787 this_wc->flags = WF_DELETE;
1788 if (logfs_block(ipage)->partial)
1789 this_wc->flags |= WF_WRITE;
1791 return logfs_write_i0(inode, ipage, this_wc);
1794 static int logfs_truncate_rec(struct inode *inode, u64 size)
1796 struct logfs_inode *li = logfs_inode(inode);
1797 struct write_control wc = {
1798 .ofs = li->li_data[INDIRECT_INDEX],
1800 struct page *page;
1801 int err;
1803 alloc_inode_block(inode);
1805 if (!wc.ofs)
1806 return 0;
1808 page = logfs_get_write_page(inode, 0, LEVEL(li->li_height));
1809 if (!page)
1810 return -ENOMEM;
1812 err = __logfs_truncate_rec(inode, page, &wc, size);
1813 logfs_put_write_page(page);
1814 if (err)
1815 return err;
1817 if (li->li_data[INDIRECT_INDEX] != wc.ofs)
1818 li->li_data[INDIRECT_INDEX] = wc.ofs;
1819 return 0;
1822 static int __logfs_truncate(struct inode *inode, u64 size)
1824 int ret;
1826 if (size >= logfs_factor(logfs_inode(inode)->li_height))
1827 return 0;
1829 ret = logfs_truncate_rec(inode, size);
1830 if (ret)
1831 return ret;
1833 return logfs_truncate_direct(inode, size);
1836 int logfs_truncate(struct inode *inode, u64 size)
1838 struct super_block *sb = inode->i_sb;
1839 int err;
1841 logfs_get_wblocks(sb, NULL, 1);
1842 err = __logfs_truncate(inode, size);
1843 if (!err)
1844 err = __logfs_write_inode(inode, 0);
1845 logfs_put_wblocks(sb, NULL, 1);
1847 if (!err)
1848 err = vmtruncate(inode, size);
1850 /* I don't trust error recovery yet. */
1851 WARN_ON(err);
1852 return err;
1855 static void move_page_to_inode(struct inode *inode, struct page *page)
1857 struct logfs_inode *li = logfs_inode(inode);
1858 struct logfs_block *block = logfs_block(page);
1860 if (!block)
1861 return;
1863 log_blockmove("move_page_to_inode(%llx, %llx, %x)\n",
1864 block->ino, block->bix, block->level);
1865 BUG_ON(li->li_block);
1866 block->ops = &inode_block_ops;
1867 block->inode = inode;
1868 li->li_block = block;
1870 block->page = NULL;
1871 page->private = 0;
1872 ClearPagePrivate(page);
1875 static void move_inode_to_page(struct page *page, struct inode *inode)
1877 struct logfs_inode *li = logfs_inode(inode);
1878 struct logfs_block *block = li->li_block;
1880 if (!block)
1881 return;
1883 log_blockmove("move_inode_to_page(%llx, %llx, %x)\n",
1884 block->ino, block->bix, block->level);
1885 BUG_ON(PagePrivate(page));
1886 block->ops = &indirect_block_ops;
1887 block->page = page;
1888 page->private = (unsigned long)block;
1889 SetPagePrivate(page);
1891 block->inode = NULL;
1892 li->li_block = NULL;
1895 int logfs_read_inode(struct inode *inode)
1897 struct super_block *sb = inode->i_sb;
1898 struct logfs_super *super = logfs_super(sb);
1899 struct inode *master_inode = super->s_master_inode;
1900 struct page *page;
1901 struct logfs_disk_inode *di;
1902 u64 ino = inode->i_ino;
1904 if (ino << sb->s_blocksize_bits > i_size_read(master_inode))
1905 return -ENODATA;
1906 if (!logfs_exist_block(master_inode, ino))
1907 return -ENODATA;
1909 page = read_cache_page(master_inode->i_mapping, ino,
1910 (filler_t *)logfs_readpage, NULL);
1911 if (IS_ERR(page))
1912 return PTR_ERR(page);
1914 di = kmap_atomic(page, KM_USER0);
1915 logfs_disk_to_inode(di, inode);
1916 kunmap_atomic(di, KM_USER0);
1917 move_page_to_inode(inode, page);
1918 page_cache_release(page);
1919 return 0;
1922 /* Caller must logfs_put_write_page(page); */
1923 static struct page *inode_to_page(struct inode *inode)
1925 struct inode *master_inode = logfs_super(inode->i_sb)->s_master_inode;
1926 struct logfs_disk_inode *di;
1927 struct page *page;
1929 BUG_ON(inode->i_ino == LOGFS_INO_MASTER);
1931 page = logfs_get_write_page(master_inode, inode->i_ino, 0);
1932 if (!page)
1933 return NULL;
1935 di = kmap_atomic(page, KM_USER0);
1936 logfs_inode_to_disk(inode, di);
1937 kunmap_atomic(di, KM_USER0);
1938 move_inode_to_page(page, inode);
1939 return page;
1942 /* Cheaper version of write_inode. All changes are concealed in
1943 * aliases, which are moved back. No write to the medium happens.
1945 void logfs_clear_inode(struct inode *inode)
1947 struct super_block *sb = inode->i_sb;
1948 struct logfs_inode *li = logfs_inode(inode);
1949 struct logfs_block *block = li->li_block;
1950 struct page *page;
1952 /* Only deleted files may be dirty at this point */
1953 BUG_ON(inode->i_state & I_DIRTY && inode->i_nlink);
1954 if (!block)
1955 return;
1956 if ((logfs_super(sb)->s_flags & LOGFS_SB_FLAG_SHUTDOWN)) {
1957 block->ops->free_block(inode->i_sb, block);
1958 return;
1961 BUG_ON(inode->i_ino < LOGFS_RESERVED_INOS);
1962 page = inode_to_page(inode);
1963 BUG_ON(!page); /* FIXME: Use emergency page */
1964 logfs_put_write_page(page);
1967 static int do_write_inode(struct inode *inode)
1969 struct super_block *sb = inode->i_sb;
1970 struct inode *master_inode = logfs_super(sb)->s_master_inode;
1971 loff_t size = (inode->i_ino + 1) << inode->i_sb->s_blocksize_bits;
1972 struct page *page;
1973 int err;
1975 BUG_ON(inode->i_ino == LOGFS_INO_MASTER);
1976 /* FIXME: lock inode */
1978 if (i_size_read(master_inode) < size)
1979 i_size_write(master_inode, size);
1981 /* TODO: Tell vfs this inode is clean now */
1983 page = inode_to_page(inode);
1984 if (!page)
1985 return -ENOMEM;
1987 /* FIXME: transaction is part of logfs_block now. Is that enough? */
1988 err = logfs_write_buf(master_inode, page, 0);
1989 logfs_put_write_page(page);
1990 return err;
1993 static void logfs_mod_segment_entry(struct super_block *sb, u32 segno,
1994 int write,
1995 void (*change_se)(struct logfs_segment_entry *, long),
1996 long arg)
1998 struct logfs_super *super = logfs_super(sb);
1999 struct inode *inode;
2000 struct page *page;
2001 struct logfs_segment_entry *se;
2002 pgoff_t page_no;
2003 int child_no;
2005 page_no = segno >> (sb->s_blocksize_bits - 3);
2006 child_no = segno & ((sb->s_blocksize >> 3) - 1);
2008 inode = super->s_segfile_inode;
2009 page = logfs_get_write_page(inode, page_no, 0);
2010 BUG_ON(!page); /* FIXME: We need some reserve page for this case */
2011 if (!PageUptodate(page))
2012 logfs_read_block(inode, page, WRITE);
2014 if (write)
2015 alloc_indirect_block(inode, page, 0);
2016 se = kmap_atomic(page, KM_USER0);
2017 change_se(se + child_no, arg);
2018 if (write) {
2019 logfs_set_alias(sb, logfs_block(page), child_no);
2020 BUG_ON((int)be32_to_cpu(se[child_no].valid) > super->s_segsize);
2022 kunmap_atomic(se, KM_USER0);
2024 logfs_put_write_page(page);
2027 static void __get_segment_entry(struct logfs_segment_entry *se, long _target)
2029 struct logfs_segment_entry *target = (void *)_target;
2031 *target = *se;
2034 void logfs_get_segment_entry(struct super_block *sb, u32 segno,
2035 struct logfs_segment_entry *se)
2037 logfs_mod_segment_entry(sb, segno, 0, __get_segment_entry, (long)se);
2040 static void __set_segment_used(struct logfs_segment_entry *se, long increment)
2042 u32 valid;
2044 valid = be32_to_cpu(se->valid);
2045 valid += increment;
2046 se->valid = cpu_to_be32(valid);
2049 void logfs_set_segment_used(struct super_block *sb, u64 ofs, int increment)
2051 struct logfs_super *super = logfs_super(sb);
2052 u32 segno = ofs >> super->s_segshift;
2054 if (!increment)
2055 return;
2057 logfs_mod_segment_entry(sb, segno, 1, __set_segment_used, increment);
2060 static void __set_segment_erased(struct logfs_segment_entry *se, long ec_level)
2062 se->ec_level = cpu_to_be32(ec_level);
2065 void logfs_set_segment_erased(struct super_block *sb, u32 segno, u32 ec,
2066 gc_level_t gc_level)
2068 u32 ec_level = ec << 4 | (__force u8)gc_level;
2070 logfs_mod_segment_entry(sb, segno, 1, __set_segment_erased, ec_level);
2073 static void __set_segment_reserved(struct logfs_segment_entry *se, long ignore)
2075 se->valid = cpu_to_be32(RESERVED);
2078 void logfs_set_segment_reserved(struct super_block *sb, u32 segno)
2080 logfs_mod_segment_entry(sb, segno, 1, __set_segment_reserved, 0);
2083 static void __set_segment_unreserved(struct logfs_segment_entry *se,
2084 long ec_level)
2086 se->valid = 0;
2087 se->ec_level = cpu_to_be32(ec_level);
2090 void logfs_set_segment_unreserved(struct super_block *sb, u32 segno, u32 ec)
2092 u32 ec_level = ec << 4;
2094 logfs_mod_segment_entry(sb, segno, 1, __set_segment_unreserved,
2095 ec_level);
2098 int __logfs_write_inode(struct inode *inode, long flags)
2100 struct super_block *sb = inode->i_sb;
2101 int ret;
2103 logfs_get_wblocks(sb, NULL, flags & WF_LOCK);
2104 ret = do_write_inode(inode);
2105 logfs_put_wblocks(sb, NULL, flags & WF_LOCK);
2106 return ret;
2109 static int do_delete_inode(struct inode *inode)
2111 struct super_block *sb = inode->i_sb;
2112 struct inode *master_inode = logfs_super(sb)->s_master_inode;
2113 struct page *page;
2114 int ret;
2116 page = logfs_get_write_page(master_inode, inode->i_ino, 0);
2117 if (!page)
2118 return -ENOMEM;
2120 move_inode_to_page(page, inode);
2122 logfs_get_wblocks(sb, page, 1);
2123 ret = __logfs_delete(master_inode, page);
2124 logfs_put_wblocks(sb, page, 1);
2126 logfs_put_write_page(page);
2127 return ret;
2131 * ZOMBIE inodes have already been deleted before and should remain dead,
2132 * if it weren't for valid checking. No need to kill them again here.
2134 void logfs_delete_inode(struct inode *inode)
2136 struct logfs_inode *li = logfs_inode(inode);
2138 if (!(li->li_flags & LOGFS_IF_ZOMBIE)) {
2139 li->li_flags |= LOGFS_IF_ZOMBIE;
2140 if (i_size_read(inode) > 0)
2141 logfs_truncate(inode, 0);
2142 do_delete_inode(inode);
2144 truncate_inode_pages(&inode->i_data, 0);
2145 clear_inode(inode);
2148 void btree_write_block(struct logfs_block *block)
2150 struct inode *inode;
2151 struct page *page;
2152 int err, cookie;
2154 inode = logfs_safe_iget(block->sb, block->ino, &cookie);
2155 page = logfs_get_write_page(inode, block->bix, block->level);
2157 err = logfs_readpage_nolock(page);
2158 BUG_ON(err);
2159 BUG_ON(!PagePrivate(page));
2160 BUG_ON(logfs_block(page) != block);
2161 err = __logfs_write_buf(inode, page, 0);
2162 BUG_ON(err);
2163 BUG_ON(PagePrivate(page) || page->private);
2165 logfs_put_write_page(page);
2166 logfs_safe_iput(inode, cookie);
2170 * logfs_inode_write - write inode or dentry objects
2172 * @inode: parent inode (ifile or directory)
2173 * @buf: object to write (inode or dentry)
2174 * @n: object size
2175 * @_pos: object number (file position in blocks/objects)
2176 * @flags: write flags
2177 * @lock: 0 if write lock is already taken, 1 otherwise
2178 * @shadow_tree: shadow below this inode
2180 * FIXME: All caller of this put a 200-300 byte variable on the stack,
2181 * only to call here and do a memcpy from that stack variable. A good
2182 * example of wasted performance and stack space.
2184 int logfs_inode_write(struct inode *inode, const void *buf, size_t count,
2185 loff_t bix, long flags, struct shadow_tree *shadow_tree)
2187 loff_t pos = bix << inode->i_sb->s_blocksize_bits;
2188 int err;
2189 struct page *page;
2190 void *pagebuf;
2192 BUG_ON(pos & (LOGFS_BLOCKSIZE-1));
2193 BUG_ON(count > LOGFS_BLOCKSIZE);
2194 page = logfs_get_write_page(inode, bix, 0);
2195 if (!page)
2196 return -ENOMEM;
2198 pagebuf = kmap_atomic(page, KM_USER0);
2199 memcpy(pagebuf, buf, count);
2200 flush_dcache_page(page);
2201 kunmap_atomic(pagebuf, KM_USER0);
2203 if (i_size_read(inode) < pos + LOGFS_BLOCKSIZE)
2204 i_size_write(inode, pos + LOGFS_BLOCKSIZE);
2206 err = logfs_write_buf(inode, page, flags);
2207 logfs_put_write_page(page);
2208 return err;
2211 int logfs_open_segfile(struct super_block *sb)
2213 struct logfs_super *super = logfs_super(sb);
2214 struct inode *inode;
2216 inode = logfs_read_meta_inode(sb, LOGFS_INO_SEGFILE);
2217 if (IS_ERR(inode))
2218 return PTR_ERR(inode);
2219 super->s_segfile_inode = inode;
2220 return 0;
2223 int logfs_init_rw(struct super_block *sb)
2225 struct logfs_super *super = logfs_super(sb);
2226 int min_fill = 3 * super->s_no_blocks;
2228 INIT_LIST_HEAD(&super->s_object_alias);
2229 mutex_init(&super->s_write_mutex);
2230 super->s_block_pool = mempool_create_kmalloc_pool(min_fill,
2231 sizeof(struct logfs_block));
2232 super->s_shadow_pool = mempool_create_kmalloc_pool(min_fill,
2233 sizeof(struct logfs_shadow));
2234 return 0;
2237 void logfs_cleanup_rw(struct super_block *sb)
2239 struct logfs_super *super = logfs_super(sb);
2241 destroy_meta_inode(super->s_segfile_inode);
2242 if (super->s_block_pool)
2243 mempool_destroy(super->s_block_pool);
2244 if (super->s_shadow_pool)
2245 mempool_destroy(super->s_shadow_pool);