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
20 #include <linux/sched.h>
21 #include <linux/slab.h>
23 static u64
adjust_bix(u64 bix
, level_t level
)
29 return max_t(u64
, bix
, I0_BLOCKS
);
31 return max_t(u64
, bix
, I1_BLOCKS
);
33 return max_t(u64
, bix
, I2_BLOCKS
);
35 return max_t(u64
, bix
, I3_BLOCKS
);
37 return max_t(u64
, bix
, I4_BLOCKS
);
44 static inline u64
maxbix(u8 height
)
46 return 1ULL << (LOGFS_BLOCK_BITS
* height
);
50 * The inode address space is cut in two halves. Lower half belongs to data
51 * pages, upper half to indirect blocks. If the high bit (INDIRECT_BIT) is
52 * set, the actual block index (bix) and level can be derived from the page
55 * The lowest three bits of the block index are set to 0 after packing and
56 * unpacking. Since the lowest n bits (9 for 4KiB blocksize) are ignored
57 * anyway this is harmless.
59 #define ARCH_SHIFT (BITS_PER_LONG - 32)
60 #define INDIRECT_BIT (0x80000000UL << ARCH_SHIFT)
61 #define LEVEL_SHIFT (28 + ARCH_SHIFT)
62 static inline pgoff_t
first_indirect_block(void)
64 return INDIRECT_BIT
| (1ULL << LEVEL_SHIFT
);
67 pgoff_t
logfs_pack_index(u64 bix
, level_t level
)
71 BUG_ON(bix
>= INDIRECT_BIT
);
76 index
|= (__force
long)level
<< LEVEL_SHIFT
;
77 index
|= bix
>> ((__force u8
)level
* LOGFS_BLOCK_BITS
);
81 void logfs_unpack_index(pgoff_t index
, u64
*bix
, level_t
*level
)
85 if (!(index
& INDIRECT_BIT
)) {
91 __level
= (index
& ~INDIRECT_BIT
) >> LEVEL_SHIFT
;
92 *level
= LEVEL(__level
);
93 *bix
= (index
<< (__level
* LOGFS_BLOCK_BITS
)) & ~INDIRECT_BIT
;
94 *bix
= adjust_bix(*bix
, *level
);
102 * Time is stored as nanoseconds since the epoch.
104 static struct timespec
be64_to_timespec(__be64 betime
)
106 return ns_to_timespec(be64_to_cpu(betime
));
109 static __be64
timespec_to_be64(struct timespec tsp
)
111 return cpu_to_be64((u64
)tsp
.tv_sec
* NSEC_PER_SEC
+ tsp
.tv_nsec
);
114 static void logfs_disk_to_inode(struct logfs_disk_inode
*di
, struct inode
*inode
)
116 struct logfs_inode
*li
= logfs_inode(inode
);
119 inode
->i_mode
= be16_to_cpu(di
->di_mode
);
120 li
->li_height
= di
->di_height
;
121 li
->li_flags
= be32_to_cpu(di
->di_flags
);
122 i_uid_write(inode
, be32_to_cpu(di
->di_uid
));
123 i_gid_write(inode
, be32_to_cpu(di
->di_gid
));
124 inode
->i_size
= be64_to_cpu(di
->di_size
);
125 logfs_set_blocks(inode
, be64_to_cpu(di
->di_used_bytes
));
126 inode
->i_atime
= be64_to_timespec(di
->di_atime
);
127 inode
->i_ctime
= be64_to_timespec(di
->di_ctime
);
128 inode
->i_mtime
= be64_to_timespec(di
->di_mtime
);
129 set_nlink(inode
, be32_to_cpu(di
->di_refcount
));
130 inode
->i_generation
= be32_to_cpu(di
->di_generation
);
132 switch (inode
->i_mode
& S_IFMT
) {
133 case S_IFSOCK
: /* fall through */
134 case S_IFBLK
: /* fall through */
135 case S_IFCHR
: /* fall through */
137 inode
->i_rdev
= be64_to_cpu(di
->di_data
[0]);
139 case S_IFDIR
: /* fall through */
140 case S_IFREG
: /* fall through */
142 for (i
= 0; i
< LOGFS_EMBEDDED_FIELDS
; i
++)
143 li
->li_data
[i
] = be64_to_cpu(di
->di_data
[i
]);
150 static void logfs_inode_to_disk(struct inode
*inode
, struct logfs_disk_inode
*di
)
152 struct logfs_inode
*li
= logfs_inode(inode
);
155 di
->di_mode
= cpu_to_be16(inode
->i_mode
);
156 di
->di_height
= li
->li_height
;
158 di
->di_flags
= cpu_to_be32(li
->li_flags
);
159 di
->di_uid
= cpu_to_be32(i_uid_read(inode
));
160 di
->di_gid
= cpu_to_be32(i_gid_read(inode
));
161 di
->di_size
= cpu_to_be64(i_size_read(inode
));
162 di
->di_used_bytes
= cpu_to_be64(li
->li_used_bytes
);
163 di
->di_atime
= timespec_to_be64(inode
->i_atime
);
164 di
->di_ctime
= timespec_to_be64(inode
->i_ctime
);
165 di
->di_mtime
= timespec_to_be64(inode
->i_mtime
);
166 di
->di_refcount
= cpu_to_be32(inode
->i_nlink
);
167 di
->di_generation
= cpu_to_be32(inode
->i_generation
);
169 switch (inode
->i_mode
& S_IFMT
) {
170 case S_IFSOCK
: /* fall through */
171 case S_IFBLK
: /* fall through */
172 case S_IFCHR
: /* fall through */
174 di
->di_data
[0] = cpu_to_be64(inode
->i_rdev
);
176 case S_IFDIR
: /* fall through */
177 case S_IFREG
: /* fall through */
179 for (i
= 0; i
< LOGFS_EMBEDDED_FIELDS
; i
++)
180 di
->di_data
[i
] = cpu_to_be64(li
->li_data
[i
]);
187 static void __logfs_set_blocks(struct inode
*inode
)
189 struct super_block
*sb
= inode
->i_sb
;
190 struct logfs_inode
*li
= logfs_inode(inode
);
192 inode
->i_blocks
= ULONG_MAX
;
193 if (li
->li_used_bytes
>> sb
->s_blocksize_bits
< ULONG_MAX
)
194 inode
->i_blocks
= ALIGN(li
->li_used_bytes
, 512) >> 9;
197 void logfs_set_blocks(struct inode
*inode
, u64 bytes
)
199 struct logfs_inode
*li
= logfs_inode(inode
);
201 li
->li_used_bytes
= bytes
;
202 __logfs_set_blocks(inode
);
205 static void prelock_page(struct super_block
*sb
, struct page
*page
, int lock
)
207 struct logfs_super
*super
= logfs_super(sb
);
209 BUG_ON(!PageLocked(page
));
211 BUG_ON(PagePreLocked(page
));
212 SetPagePreLocked(page
);
214 /* We are in GC path. */
215 if (PagePreLocked(page
))
216 super
->s_lock_count
++;
218 SetPagePreLocked(page
);
222 static void preunlock_page(struct super_block
*sb
, struct page
*page
, int lock
)
224 struct logfs_super
*super
= logfs_super(sb
);
226 BUG_ON(!PageLocked(page
));
228 ClearPagePreLocked(page
);
230 /* We are in GC path. */
231 BUG_ON(!PagePreLocked(page
));
232 if (super
->s_lock_count
)
233 super
->s_lock_count
--;
235 ClearPagePreLocked(page
);
240 * Logfs is prone to an AB-BA deadlock where one task tries to acquire
241 * s_write_mutex with a locked page and GC tries to get that page while holding
243 * To solve this issue logfs will ignore the page lock iff the page in question
244 * is waiting for s_write_mutex. We annotate this fact by setting PG_pre_locked
245 * in addition to PG_locked.
247 void logfs_get_wblocks(struct super_block
*sb
, struct page
*page
, int lock
)
249 struct logfs_super
*super
= logfs_super(sb
);
252 prelock_page(sb
, page
, lock
);
255 mutex_lock(&super
->s_write_mutex
);
257 /* FIXME: We also have to check for shadowed space
258 * and mempool fill grade */
262 void logfs_put_wblocks(struct super_block
*sb
, struct page
*page
, int lock
)
264 struct logfs_super
*super
= logfs_super(sb
);
267 preunlock_page(sb
, page
, lock
);
268 /* Order matters - we must clear PG_pre_locked before releasing
269 * s_write_mutex or we could race against another task. */
271 mutex_unlock(&super
->s_write_mutex
);
274 static struct page
*logfs_get_read_page(struct inode
*inode
, u64 bix
,
277 return find_or_create_page(inode
->i_mapping
,
278 logfs_pack_index(bix
, level
), GFP_NOFS
);
281 static void logfs_put_read_page(struct page
*page
)
287 static void logfs_lock_write_page(struct page
*page
)
291 while (unlikely(!trylock_page(page
))) {
292 if (loop
++ > 0x1000) {
293 /* Has been observed once so far... */
294 printk(KERN_ERR
"stack at %p\n", &loop
);
297 if (PagePreLocked(page
)) {
298 /* Holder of page lock is waiting for us, it
299 * is safe to use this page. */
302 /* Some other process has this page locked and has
303 * nothing to do with us. Wait for it to finish.
307 BUG_ON(!PageLocked(page
));
310 static struct page
*logfs_get_write_page(struct inode
*inode
, u64 bix
,
313 struct address_space
*mapping
= inode
->i_mapping
;
314 pgoff_t index
= logfs_pack_index(bix
, level
);
319 page
= find_get_page(mapping
, index
);
321 page
= __page_cache_alloc(GFP_NOFS
);
324 err
= add_to_page_cache_lru(page
, mapping
, index
, GFP_NOFS
);
331 } else logfs_lock_write_page(page
);
332 BUG_ON(!PageLocked(page
));
336 static void logfs_unlock_write_page(struct page
*page
)
338 if (!PagePreLocked(page
))
342 static void logfs_put_write_page(struct page
*page
)
344 logfs_unlock_write_page(page
);
348 static struct page
*logfs_get_page(struct inode
*inode
, u64 bix
, level_t level
,
352 return logfs_get_read_page(inode
, bix
, level
);
354 return logfs_get_write_page(inode
, bix
, level
);
357 static void logfs_put_page(struct page
*page
, int rw
)
360 logfs_put_read_page(page
);
362 logfs_put_write_page(page
);
365 static unsigned long __get_bits(u64 val
, int skip
, int no
)
375 static unsigned long get_bits(u64 val
, level_t skip
)
377 return __get_bits(val
, (__force
int)skip
, LOGFS_BLOCK_BITS
);
380 static inline void init_shadow_tree(struct super_block
*sb
,
381 struct shadow_tree
*tree
)
383 struct logfs_super
*super
= logfs_super(sb
);
385 btree_init_mempool64(&tree
->new, super
->s_btree_pool
);
386 btree_init_mempool64(&tree
->old
, super
->s_btree_pool
);
389 static void indirect_write_block(struct logfs_block
*block
)
396 inode
= page
->mapping
->host
;
397 logfs_lock_write_page(page
);
398 ret
= logfs_write_buf(inode
, page
, 0);
399 logfs_unlock_write_page(page
);
401 * This needs some rework. Unless you want your filesystem to run
402 * completely synchronously (you don't), the filesystem will always
403 * report writes as 'successful' before the actual work has been
404 * done. The actual work gets done here and this is where any errors
405 * will show up. And there isn't much we can do about it, really.
407 * Some attempts to fix the errors (move from bad blocks, retry io,...)
408 * have already been done, so anything left should be either a broken
409 * device or a bug somewhere in logfs itself. Being relatively new,
410 * the odds currently favor a bug, so for now the line below isn't
416 static void inode_write_block(struct logfs_block
*block
)
421 inode
= block
->inode
;
422 if (inode
->i_ino
== LOGFS_INO_MASTER
)
423 logfs_write_anchor(inode
->i_sb
);
425 ret
= __logfs_write_inode(inode
, NULL
, 0);
426 /* see indirect_write_block comment */
432 * This silences a false, yet annoying gcc warning. I hate it when my editor
433 * jumps into bitops.h each time I recompile this file.
434 * TODO: Complain to gcc folks about this and upgrade compiler.
436 static unsigned long fnb(const unsigned long *addr
,
437 unsigned long size
, unsigned long offset
)
439 return find_next_bit(addr
, size
, offset
);
442 static __be64
inode_val0(struct inode
*inode
)
444 struct logfs_inode
*li
= logfs_inode(inode
);
448 * Explicit shifting generates good code, but must match the format
449 * of the structure. Add some paranoia just in case.
451 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_mode
) != 0);
452 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_height
) != 2);
453 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_flags
) != 4);
455 val
= (u64
)inode
->i_mode
<< 48 |
456 (u64
)li
->li_height
<< 40 |
458 return cpu_to_be64(val
);
461 static int inode_write_alias(struct super_block
*sb
,
462 struct logfs_block
*block
, write_alias_t
*write_one_alias
)
464 struct inode
*inode
= block
->inode
;
465 struct logfs_inode
*li
= logfs_inode(inode
);
472 for (pos
= 0; ; pos
++) {
473 pos
= fnb(block
->alias_map
, LOGFS_BLOCK_FACTOR
, pos
);
474 if (pos
>= LOGFS_EMBEDDED_FIELDS
+ INODE_POINTER_OFS
)
478 case INODE_HEIGHT_OFS
:
479 val
= inode_val0(inode
);
482 val
= cpu_to_be64(li
->li_used_bytes
);
485 val
= cpu_to_be64(i_size_read(inode
));
487 case INODE_POINTER_OFS
... INODE_POINTER_OFS
+ LOGFS_EMBEDDED_FIELDS
- 1:
488 val
= cpu_to_be64(li
->li_data
[pos
- INODE_POINTER_OFS
]);
494 ino
= LOGFS_INO_MASTER
;
497 err
= write_one_alias(sb
, ino
, bix
, level
, pos
, val
);
503 static int indirect_write_alias(struct super_block
*sb
,
504 struct logfs_block
*block
, write_alias_t
*write_one_alias
)
507 struct page
*page
= block
->page
;
513 for (pos
= 0; ; pos
++) {
514 pos
= fnb(block
->alias_map
, LOGFS_BLOCK_FACTOR
, pos
);
515 if (pos
>= LOGFS_BLOCK_FACTOR
)
518 ino
= page
->mapping
->host
->i_ino
;
519 logfs_unpack_index(page
->index
, &bix
, &level
);
520 child
= kmap_atomic(page
);
522 kunmap_atomic(child
);
523 err
= write_one_alias(sb
, ino
, bix
, level
, pos
, val
);
529 int logfs_write_obj_aliases_pagecache(struct super_block
*sb
)
531 struct logfs_super
*super
= logfs_super(sb
);
532 struct logfs_block
*block
;
535 list_for_each_entry(block
, &super
->s_object_alias
, alias_list
) {
536 err
= block
->ops
->write_alias(sb
, block
, write_alias_journal
);
543 void __free_block(struct super_block
*sb
, struct logfs_block
*block
)
545 BUG_ON(!list_empty(&block
->item_list
));
546 list_del(&block
->alias_list
);
547 mempool_free(block
, logfs_super(sb
)->s_block_pool
);
550 static void inode_free_block(struct super_block
*sb
, struct logfs_block
*block
)
552 struct inode
*inode
= block
->inode
;
554 logfs_inode(inode
)->li_block
= NULL
;
555 __free_block(sb
, block
);
558 static void indirect_free_block(struct super_block
*sb
,
559 struct logfs_block
*block
)
561 struct page
*page
= block
->page
;
563 if (PagePrivate(page
)) {
564 ClearPagePrivate(page
);
566 set_page_private(page
, 0);
568 __free_block(sb
, block
);
572 static const struct logfs_block_ops inode_block_ops
= {
573 .write_block
= inode_write_block
,
574 .free_block
= inode_free_block
,
575 .write_alias
= inode_write_alias
,
578 const struct logfs_block_ops indirect_block_ops
= {
579 .write_block
= indirect_write_block
,
580 .free_block
= indirect_free_block
,
581 .write_alias
= indirect_write_alias
,
584 struct logfs_block
*__alloc_block(struct super_block
*sb
,
585 u64 ino
, u64 bix
, level_t level
)
587 struct logfs_super
*super
= logfs_super(sb
);
588 struct logfs_block
*block
;
590 block
= mempool_alloc(super
->s_block_pool
, GFP_NOFS
);
591 memset(block
, 0, sizeof(*block
));
592 INIT_LIST_HEAD(&block
->alias_list
);
593 INIT_LIST_HEAD(&block
->item_list
);
597 block
->level
= level
;
601 static void alloc_inode_block(struct inode
*inode
)
603 struct logfs_inode
*li
= logfs_inode(inode
);
604 struct logfs_block
*block
;
609 block
= __alloc_block(inode
->i_sb
, LOGFS_INO_MASTER
, inode
->i_ino
, 0);
610 block
->inode
= inode
;
611 li
->li_block
= block
;
612 block
->ops
= &inode_block_ops
;
615 void initialize_block_counters(struct page
*page
, struct logfs_block
*block
,
616 __be64
*array
, int page_is_empty
)
624 if (page
->index
< first_indirect_block()) {
625 /* Counters are pointless on level 0 */
628 if (page
->index
== first_indirect_block()) {
629 /* Skip unused pointers */
631 block
->full
= I0_BLOCKS
;
633 if (!page_is_empty
) {
634 for (i
= start
; i
< LOGFS_BLOCK_FACTOR
; i
++) {
635 ptr
= be64_to_cpu(array
[i
]);
638 if (ptr
& LOGFS_FULLY_POPULATED
)
644 static void alloc_data_block(struct inode
*inode
, struct page
*page
)
646 struct logfs_block
*block
;
650 if (PagePrivate(page
))
653 logfs_unpack_index(page
->index
, &bix
, &level
);
654 block
= __alloc_block(inode
->i_sb
, inode
->i_ino
, bix
, level
);
657 SetPagePrivate(page
);
659 set_page_private(page
, (unsigned long) block
);
661 block
->ops
= &indirect_block_ops
;
664 static void alloc_indirect_block(struct inode
*inode
, struct page
*page
,
667 struct logfs_block
*block
;
670 if (PagePrivate(page
))
673 alloc_data_block(inode
, page
);
675 block
= logfs_block(page
);
676 array
= kmap_atomic(page
);
677 initialize_block_counters(page
, block
, array
, page_is_empty
);
678 kunmap_atomic(array
);
681 static void block_set_pointer(struct page
*page
, int index
, u64 ptr
)
683 struct logfs_block
*block
= logfs_block(page
);
688 array
= kmap_atomic(page
);
689 oldptr
= be64_to_cpu(array
[index
]);
690 array
[index
] = cpu_to_be64(ptr
);
691 kunmap_atomic(array
);
692 SetPageUptodate(page
);
694 block
->full
+= !!(ptr
& LOGFS_FULLY_POPULATED
)
695 - !!(oldptr
& LOGFS_FULLY_POPULATED
);
696 block
->partial
+= !!ptr
- !!oldptr
;
699 static u64
block_get_pointer(struct page
*page
, int index
)
704 block
= kmap_atomic(page
);
705 ptr
= be64_to_cpu(block
[index
]);
706 kunmap_atomic(block
);
710 static int logfs_read_empty(struct page
*page
)
712 zero_user_segment(page
, 0, PAGE_SIZE
);
716 static int logfs_read_direct(struct inode
*inode
, struct page
*page
)
718 struct logfs_inode
*li
= logfs_inode(inode
);
719 pgoff_t index
= page
->index
;
722 block
= li
->li_data
[index
];
724 return logfs_read_empty(page
);
726 return logfs_segment_read(inode
, page
, block
, index
, 0);
729 static int logfs_read_loop(struct inode
*inode
, struct page
*page
,
732 struct logfs_inode
*li
= logfs_inode(inode
);
733 u64 bix
, bofs
= li
->li_data
[INDIRECT_INDEX
];
734 level_t level
, target_level
;
738 logfs_unpack_index(page
->index
, &bix
, &target_level
);
740 return logfs_read_empty(page
);
742 if (bix
>= maxbix(li
->li_height
))
743 return logfs_read_empty(page
);
745 for (level
= LEVEL(li
->li_height
);
746 (__force u8
)level
> (__force u8
)target_level
;
747 level
= SUBLEVEL(level
)){
748 ipage
= logfs_get_page(inode
, bix
, level
, rw_context
);
752 ret
= logfs_segment_read(inode
, ipage
, bofs
, bix
, level
);
754 logfs_put_read_page(ipage
);
758 bofs
= block_get_pointer(ipage
, get_bits(bix
, SUBLEVEL(level
)));
759 logfs_put_page(ipage
, rw_context
);
761 return logfs_read_empty(page
);
764 return logfs_segment_read(inode
, page
, bofs
, bix
, 0);
767 static int logfs_read_block(struct inode
*inode
, struct page
*page
,
770 pgoff_t index
= page
->index
;
772 if (index
< I0_BLOCKS
)
773 return logfs_read_direct(inode
, page
);
774 return logfs_read_loop(inode
, page
, rw_context
);
777 static int logfs_exist_loop(struct inode
*inode
, u64 bix
)
779 struct logfs_inode
*li
= logfs_inode(inode
);
780 u64 bofs
= li
->li_data
[INDIRECT_INDEX
];
787 if (bix
>= maxbix(li
->li_height
))
790 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)) {
791 ipage
= logfs_get_read_page(inode
, bix
, level
);
795 ret
= logfs_segment_read(inode
, ipage
, bofs
, bix
, level
);
797 logfs_put_read_page(ipage
);
801 bofs
= block_get_pointer(ipage
, get_bits(bix
, SUBLEVEL(level
)));
802 logfs_put_read_page(ipage
);
810 int logfs_exist_block(struct inode
*inode
, u64 bix
)
812 struct logfs_inode
*li
= logfs_inode(inode
);
815 return !!li
->li_data
[bix
];
816 return logfs_exist_loop(inode
, bix
);
819 static u64
seek_holedata_direct(struct inode
*inode
, u64 bix
, int data
)
821 struct logfs_inode
*li
= logfs_inode(inode
);
823 for (; bix
< I0_BLOCKS
; bix
++)
824 if (data
^ (li
->li_data
[bix
] == 0))
829 static u64
seek_holedata_loop(struct inode
*inode
, u64 bix
, int data
)
831 struct logfs_inode
*li
= logfs_inode(inode
);
833 u64 increment
, bofs
= li
->li_data
[INDIRECT_INDEX
];
840 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)) {
841 increment
= 1 << (LOGFS_BLOCK_BITS
* ((__force u8
)level
-1));
842 page
= logfs_get_read_page(inode
, bix
, level
);
846 ret
= logfs_segment_read(inode
, page
, bofs
, bix
, level
);
848 logfs_put_read_page(page
);
852 slot
= get_bits(bix
, SUBLEVEL(level
));
853 rblock
= kmap_atomic(page
);
854 while (slot
< LOGFS_BLOCK_FACTOR
) {
855 if (data
&& (rblock
[slot
] != 0))
857 if (!data
&& !(be64_to_cpu(rblock
[slot
]) & LOGFS_FULLY_POPULATED
))
861 bix
&= ~(increment
- 1);
863 if (slot
>= LOGFS_BLOCK_FACTOR
) {
864 kunmap_atomic(rblock
);
865 logfs_put_read_page(page
);
868 bofs
= be64_to_cpu(rblock
[slot
]);
869 kunmap_atomic(rblock
);
870 logfs_put_read_page(page
);
880 * logfs_seek_hole - find next hole starting at a given block index
881 * @inode: inode to search in
882 * @bix: block index to start searching
884 * Returns next hole. If the file doesn't contain any further holes, the
885 * block address next to eof is returned instead.
887 u64
logfs_seek_hole(struct inode
*inode
, u64 bix
)
889 struct logfs_inode
*li
= logfs_inode(inode
);
891 if (bix
< I0_BLOCKS
) {
892 bix
= seek_holedata_direct(inode
, bix
, 0);
897 if (!li
->li_data
[INDIRECT_INDEX
])
899 else if (li
->li_data
[INDIRECT_INDEX
] & LOGFS_FULLY_POPULATED
)
900 bix
= maxbix(li
->li_height
);
901 else if (bix
>= maxbix(li
->li_height
))
904 bix
= seek_holedata_loop(inode
, bix
, 0);
905 if (bix
< maxbix(li
->li_height
))
907 /* Should not happen anymore. But if some port writes semi-
908 * corrupt images (as this one used to) we might run into it.
910 WARN_ON_ONCE(bix
== maxbix(li
->li_height
));
916 static u64
__logfs_seek_data(struct inode
*inode
, u64 bix
)
918 struct logfs_inode
*li
= logfs_inode(inode
);
920 if (bix
< I0_BLOCKS
) {
921 bix
= seek_holedata_direct(inode
, bix
, 1);
926 if (bix
< maxbix(li
->li_height
)) {
927 if (!li
->li_data
[INDIRECT_INDEX
])
928 bix
= maxbix(li
->li_height
);
930 return seek_holedata_loop(inode
, bix
, 1);
937 * logfs_seek_data - find next data block after a given block index
938 * @inode: inode to search in
939 * @bix: block index to start searching
941 * Returns next data block. If the file doesn't contain any further data
942 * blocks, the last block in the file is returned instead.
944 u64
logfs_seek_data(struct inode
*inode
, u64 bix
)
946 struct super_block
*sb
= inode
->i_sb
;
949 ret
= __logfs_seek_data(inode
, bix
);
950 end
= i_size_read(inode
) >> sb
->s_blocksize_bits
;
956 static int logfs_is_valid_direct(struct logfs_inode
*li
, u64 bix
, u64 ofs
)
958 return pure_ofs(li
->li_data
[bix
]) == ofs
;
961 static int __logfs_is_valid_loop(struct inode
*inode
, u64 bix
,
964 struct logfs_inode
*li
= logfs_inode(inode
);
969 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)){
970 page
= logfs_get_write_page(inode
, bix
, level
);
973 ret
= logfs_segment_read(inode
, page
, bofs
, bix
, level
);
975 logfs_put_write_page(page
);
979 bofs
= block_get_pointer(page
, get_bits(bix
, SUBLEVEL(level
)));
980 logfs_put_write_page(page
);
984 if (pure_ofs(bofs
) == ofs
)
990 static int logfs_is_valid_loop(struct inode
*inode
, u64 bix
, u64 ofs
)
992 struct logfs_inode
*li
= logfs_inode(inode
);
993 u64 bofs
= li
->li_data
[INDIRECT_INDEX
];
998 if (bix
>= maxbix(li
->li_height
))
1001 if (pure_ofs(bofs
) == ofs
)
1004 return __logfs_is_valid_loop(inode
, bix
, ofs
, bofs
);
1007 static int __logfs_is_valid_block(struct inode
*inode
, u64 bix
, u64 ofs
)
1009 struct logfs_inode
*li
= logfs_inode(inode
);
1011 if ((inode
->i_nlink
== 0) && atomic_read(&inode
->i_count
) == 1)
1014 if (bix
< I0_BLOCKS
)
1015 return logfs_is_valid_direct(li
, bix
, ofs
);
1016 return logfs_is_valid_loop(inode
, bix
, ofs
);
1020 * logfs_is_valid_block - check whether this block is still valid
1023 * @ofs: block physical offset
1024 * @ino: block inode number
1026 * @gc_level: block level
1028 * Returns 0 if the block is invalid, 1 if it is valid and 2 if it will
1029 * become invalid once the journal is written.
1031 int logfs_is_valid_block(struct super_block
*sb
, u64 ofs
, u64 ino
, u64 bix
,
1032 gc_level_t gc_level
)
1034 struct logfs_super
*super
= logfs_super(sb
);
1035 struct inode
*inode
;
1038 /* Umount closes a segment with free blocks remaining. Those
1039 * blocks are by definition invalid. */
1043 LOGFS_BUG_ON((u64
)(u_long
)ino
!= ino
, sb
);
1045 inode
= logfs_safe_iget(sb
, ino
, &cookie
);
1049 ret
= __logfs_is_valid_block(inode
, bix
, ofs
);
1050 logfs_safe_iput(inode
, cookie
);
1055 /* Block is nominally invalid, but may still sit in the shadow tree,
1056 * waiting for a journal commit.
1058 if (btree_lookup64(&super
->s_shadow_tree
.old
, ofs
))
1063 int logfs_readpage_nolock(struct page
*page
)
1065 struct inode
*inode
= page
->mapping
->host
;
1068 ret
= logfs_read_block(inode
, page
, READ
);
1071 ClearPageUptodate(page
);
1074 SetPageUptodate(page
);
1075 ClearPageError(page
);
1077 flush_dcache_page(page
);
1082 static int logfs_reserve_bytes(struct inode
*inode
, int bytes
)
1084 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1085 u64 available
= super
->s_free_bytes
+ super
->s_dirty_free_bytes
1086 - super
->s_dirty_used_bytes
- super
->s_dirty_pages
;
1091 if (available
< bytes
)
1094 if (available
< bytes
+ super
->s_root_reserve
&&
1095 !capable(CAP_SYS_RESOURCE
))
1101 int get_page_reserve(struct inode
*inode
, struct page
*page
)
1103 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1104 struct logfs_block
*block
= logfs_block(page
);
1107 if (block
&& block
->reserved_bytes
)
1110 logfs_get_wblocks(inode
->i_sb
, page
, WF_LOCK
);
1111 while ((ret
= logfs_reserve_bytes(inode
, 6 * LOGFS_MAX_OBJECTSIZE
)) &&
1112 !list_empty(&super
->s_writeback_list
)) {
1113 block
= list_entry(super
->s_writeback_list
.next
,
1114 struct logfs_block
, alias_list
);
1115 block
->ops
->write_block(block
);
1118 alloc_data_block(inode
, page
);
1119 block
= logfs_block(page
);
1120 block
->reserved_bytes
+= 6 * LOGFS_MAX_OBJECTSIZE
;
1121 super
->s_dirty_pages
+= 6 * LOGFS_MAX_OBJECTSIZE
;
1122 list_move_tail(&block
->alias_list
, &super
->s_writeback_list
);
1124 logfs_put_wblocks(inode
->i_sb
, page
, WF_LOCK
);
1129 * We are protected by write lock. Push victims up to superblock level
1130 * and release transaction when appropriate.
1132 /* FIXME: This is currently called from the wrong spots. */
1133 static void logfs_handle_transaction(struct inode
*inode
,
1134 struct logfs_transaction
*ta
)
1136 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1140 logfs_inode(inode
)->li_block
->ta
= NULL
;
1142 if (inode
->i_ino
!= LOGFS_INO_MASTER
) {
1143 BUG(); /* FIXME: Yes, this needs more thought */
1144 /* just remember the transaction until inode is written */
1145 //BUG_ON(logfs_inode(inode)->li_transaction);
1146 //logfs_inode(inode)->li_transaction = ta;
1150 switch (ta
->state
) {
1151 case CREATE_1
: /* fall through */
1153 BUG_ON(super
->s_victim_ino
);
1154 super
->s_victim_ino
= ta
->ino
;
1156 case CREATE_2
: /* fall through */
1158 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1159 super
->s_victim_ino
= 0;
1160 /* transaction ends here - free it */
1163 case CROSS_RENAME_1
:
1164 BUG_ON(super
->s_rename_dir
);
1165 BUG_ON(super
->s_rename_pos
);
1166 super
->s_rename_dir
= ta
->dir
;
1167 super
->s_rename_pos
= ta
->pos
;
1169 case CROSS_RENAME_2
:
1170 BUG_ON(super
->s_rename_dir
!= ta
->dir
);
1171 BUG_ON(super
->s_rename_pos
!= ta
->pos
);
1172 super
->s_rename_dir
= 0;
1173 super
->s_rename_pos
= 0;
1176 case TARGET_RENAME_1
:
1177 BUG_ON(super
->s_rename_dir
);
1178 BUG_ON(super
->s_rename_pos
);
1179 BUG_ON(super
->s_victim_ino
);
1180 super
->s_rename_dir
= ta
->dir
;
1181 super
->s_rename_pos
= ta
->pos
;
1182 super
->s_victim_ino
= ta
->ino
;
1184 case TARGET_RENAME_2
:
1185 BUG_ON(super
->s_rename_dir
!= ta
->dir
);
1186 BUG_ON(super
->s_rename_pos
!= ta
->pos
);
1187 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1188 super
->s_rename_dir
= 0;
1189 super
->s_rename_pos
= 0;
1191 case TARGET_RENAME_3
:
1192 BUG_ON(super
->s_rename_dir
);
1193 BUG_ON(super
->s_rename_pos
);
1194 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1195 super
->s_victim_ino
= 0;
1204 * Not strictly a reservation, but rather a check that we still have enough
1205 * space to satisfy the write.
1207 static int logfs_reserve_blocks(struct inode
*inode
, int blocks
)
1209 return logfs_reserve_bytes(inode
, blocks
* LOGFS_MAX_OBJECTSIZE
);
1212 struct write_control
{
1217 static struct logfs_shadow
*alloc_shadow(struct inode
*inode
, u64 bix
,
1218 level_t level
, u64 old_ofs
)
1220 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1221 struct logfs_shadow
*shadow
;
1223 shadow
= mempool_alloc(super
->s_shadow_pool
, GFP_NOFS
);
1224 memset(shadow
, 0, sizeof(*shadow
));
1225 shadow
->ino
= inode
->i_ino
;
1227 shadow
->gc_level
= expand_level(inode
->i_ino
, level
);
1228 shadow
->old_ofs
= old_ofs
& ~LOGFS_FULLY_POPULATED
;
1232 static void free_shadow(struct inode
*inode
, struct logfs_shadow
*shadow
)
1234 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1236 mempool_free(shadow
, super
->s_shadow_pool
);
1239 static void mark_segment(struct shadow_tree
*tree
, u32 segno
)
1243 if (!btree_lookup32(&tree
->segment_map
, segno
)) {
1244 err
= btree_insert32(&tree
->segment_map
, segno
, (void *)1,
1247 tree
->no_shadowed_segments
++;
1252 * fill_shadow_tree - Propagate shadow tree changes due to a write
1253 * @inode: Inode owning the page
1254 * @page: Struct page that was written
1255 * @shadow: Shadow for the current write
1257 * Writes in logfs can result in two semi-valid objects. The old object
1258 * is still valid as long as it can be reached by following pointers on
1259 * the medium. Only when writes propagate all the way up to the journal
1260 * has the new object safely replaced the old one.
1262 * To handle this problem, a struct logfs_shadow is used to represent
1263 * every single write. It is attached to the indirect block, which is
1264 * marked dirty. When the indirect block is written, its shadows are
1265 * handed up to the next indirect block (or inode). Untimately they
1266 * will reach the master inode and be freed upon journal commit.
1268 * This function handles a single step in the propagation. It adds the
1269 * shadow for the current write to the tree, along with any shadows in
1270 * the page's tree, in case it was an indirect block. If a page is
1271 * written, the inode parameter is left NULL, if an inode is written,
1272 * the page parameter is left NULL.
1274 static void fill_shadow_tree(struct inode
*inode
, struct page
*page
,
1275 struct logfs_shadow
*shadow
)
1277 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1278 struct logfs_block
*block
= logfs_block(page
);
1279 struct shadow_tree
*tree
= &super
->s_shadow_tree
;
1281 if (PagePrivate(page
)) {
1282 if (block
->alias_map
)
1283 super
->s_no_object_aliases
-= bitmap_weight(
1284 block
->alias_map
, LOGFS_BLOCK_FACTOR
);
1285 logfs_handle_transaction(inode
, block
->ta
);
1286 block
->ops
->free_block(inode
->i_sb
, block
);
1289 if (shadow
->old_ofs
)
1290 btree_insert64(&tree
->old
, shadow
->old_ofs
, shadow
,
1293 btree_insert64(&tree
->new, shadow
->new_ofs
, shadow
,
1296 super
->s_dirty_used_bytes
+= shadow
->new_len
;
1297 super
->s_dirty_free_bytes
+= shadow
->old_len
;
1298 mark_segment(tree
, shadow
->old_ofs
>> super
->s_segshift
);
1299 mark_segment(tree
, shadow
->new_ofs
>> super
->s_segshift
);
1303 static void logfs_set_alias(struct super_block
*sb
, struct logfs_block
*block
,
1306 struct logfs_super
*super
= logfs_super(sb
);
1308 if (block
->inode
&& block
->inode
->i_ino
== LOGFS_INO_MASTER
) {
1309 /* Aliases in the master inode are pointless. */
1313 if (!test_bit(child_no
, block
->alias_map
)) {
1314 set_bit(child_no
, block
->alias_map
);
1315 super
->s_no_object_aliases
++;
1317 list_move_tail(&block
->alias_list
, &super
->s_object_alias
);
1321 * Object aliases can and often do change the size and occupied space of a
1322 * file. So not only do we have to change the pointers, we also have to
1323 * change inode->i_size and li->li_used_bytes. Which is done by setting
1324 * another two object aliases for the inode itself.
1326 static void set_iused(struct inode
*inode
, struct logfs_shadow
*shadow
)
1328 struct logfs_inode
*li
= logfs_inode(inode
);
1330 if (shadow
->new_len
== shadow
->old_len
)
1333 alloc_inode_block(inode
);
1334 li
->li_used_bytes
+= shadow
->new_len
- shadow
->old_len
;
1335 __logfs_set_blocks(inode
);
1336 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_USED_OFS
);
1337 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_SIZE_OFS
);
1340 static int logfs_write_i0(struct inode
*inode
, struct page
*page
,
1341 struct write_control
*wc
)
1343 struct logfs_shadow
*shadow
;
1348 logfs_unpack_index(page
->index
, &bix
, &level
);
1350 if (logfs_reserve_blocks(inode
, 1))
1353 shadow
= alloc_shadow(inode
, bix
, level
, wc
->ofs
);
1354 if (wc
->flags
& WF_WRITE
)
1355 err
= logfs_segment_write(inode
, page
, shadow
);
1356 if (wc
->flags
& WF_DELETE
)
1357 logfs_segment_delete(inode
, shadow
);
1359 free_shadow(inode
, shadow
);
1363 set_iused(inode
, shadow
);
1366 alloc_indirect_block(inode
, page
, 0);
1367 full
= logfs_block(page
)->full
== LOGFS_BLOCK_FACTOR
;
1369 fill_shadow_tree(inode
, page
, shadow
);
1370 wc
->ofs
= shadow
->new_ofs
;
1371 if (wc
->ofs
&& full
)
1372 wc
->ofs
|= LOGFS_FULLY_POPULATED
;
1376 static int logfs_write_direct(struct inode
*inode
, struct page
*page
,
1379 struct logfs_inode
*li
= logfs_inode(inode
);
1380 struct write_control wc
= {
1381 .ofs
= li
->li_data
[page
->index
],
1386 alloc_inode_block(inode
);
1388 err
= logfs_write_i0(inode
, page
, &wc
);
1392 li
->li_data
[page
->index
] = wc
.ofs
;
1393 logfs_set_alias(inode
->i_sb
, li
->li_block
,
1394 page
->index
+ INODE_POINTER_OFS
);
1398 static int ptr_change(u64 ofs
, struct page
*page
)
1400 struct logfs_block
*block
= logfs_block(page
);
1401 int empty0
, empty1
, full0
, full1
;
1404 empty1
= block
->partial
== 0;
1405 if (empty0
!= empty1
)
1408 /* The !! is necessary to shrink result to int */
1409 full0
= !!(ofs
& LOGFS_FULLY_POPULATED
);
1410 full1
= block
->full
== LOGFS_BLOCK_FACTOR
;
1416 static int __logfs_write_rec(struct inode
*inode
, struct page
*page
,
1417 struct write_control
*this_wc
,
1418 pgoff_t bix
, level_t target_level
, level_t level
)
1420 int ret
, page_empty
= 0;
1421 int child_no
= get_bits(bix
, SUBLEVEL(level
));
1423 struct write_control child_wc
= {
1424 .flags
= this_wc
->flags
,
1427 ipage
= logfs_get_write_page(inode
, bix
, level
);
1432 ret
= logfs_segment_read(inode
, ipage
, this_wc
->ofs
, bix
, level
);
1435 } else if (!PageUptodate(ipage
)) {
1437 logfs_read_empty(ipage
);
1440 child_wc
.ofs
= block_get_pointer(ipage
, child_no
);
1442 if ((__force u8
)level
-1 > (__force u8
)target_level
)
1443 ret
= __logfs_write_rec(inode
, page
, &child_wc
, bix
,
1444 target_level
, SUBLEVEL(level
));
1446 ret
= logfs_write_i0(inode
, page
, &child_wc
);
1451 alloc_indirect_block(inode
, ipage
, page_empty
);
1452 block_set_pointer(ipage
, child_no
, child_wc
.ofs
);
1453 /* FIXME: first condition seems superfluous */
1454 if (child_wc
.ofs
|| logfs_block(ipage
)->partial
)
1455 this_wc
->flags
|= WF_WRITE
;
1456 /* the condition on this_wc->ofs ensures that we won't consume extra
1457 * space for indirect blocks in the future, which we cannot reserve */
1458 if (!this_wc
->ofs
|| ptr_change(this_wc
->ofs
, ipage
))
1459 ret
= logfs_write_i0(inode
, ipage
, this_wc
);
1461 logfs_set_alias(inode
->i_sb
, logfs_block(ipage
), child_no
);
1463 logfs_put_write_page(ipage
);
1467 static int logfs_write_rec(struct inode
*inode
, struct page
*page
,
1468 pgoff_t bix
, level_t target_level
, long flags
)
1470 struct logfs_inode
*li
= logfs_inode(inode
);
1471 struct write_control wc
= {
1472 .ofs
= li
->li_data
[INDIRECT_INDEX
],
1477 alloc_inode_block(inode
);
1479 if (li
->li_height
> (__force u8
)target_level
)
1480 ret
= __logfs_write_rec(inode
, page
, &wc
, bix
, target_level
,
1481 LEVEL(li
->li_height
));
1483 ret
= logfs_write_i0(inode
, page
, &wc
);
1487 if (li
->li_data
[INDIRECT_INDEX
] != wc
.ofs
) {
1488 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1489 logfs_set_alias(inode
->i_sb
, li
->li_block
,
1490 INDIRECT_INDEX
+ INODE_POINTER_OFS
);
1495 void logfs_add_transaction(struct inode
*inode
, struct logfs_transaction
*ta
)
1497 alloc_inode_block(inode
);
1498 logfs_inode(inode
)->li_block
->ta
= ta
;
1501 void logfs_del_transaction(struct inode
*inode
, struct logfs_transaction
*ta
)
1503 struct logfs_block
*block
= logfs_inode(inode
)->li_block
;
1505 if (block
&& block
->ta
)
1509 static int grow_inode(struct inode
*inode
, u64 bix
, level_t level
)
1511 struct logfs_inode
*li
= logfs_inode(inode
);
1512 u8 height
= (__force u8
)level
;
1514 struct write_control wc
= {
1519 BUG_ON(height
> 5 || li
->li_height
> 5);
1520 while (height
> li
->li_height
|| bix
>= maxbix(li
->li_height
)) {
1521 page
= logfs_get_write_page(inode
, I0_BLOCKS
+ 1,
1522 LEVEL(li
->li_height
+ 1));
1525 logfs_read_empty(page
);
1526 alloc_indirect_block(inode
, page
, 1);
1527 block_set_pointer(page
, 0, li
->li_data
[INDIRECT_INDEX
]);
1528 err
= logfs_write_i0(inode
, page
, &wc
);
1529 logfs_put_write_page(page
);
1532 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1535 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_HEIGHT_OFS
);
1540 static int __logfs_write_buf(struct inode
*inode
, struct page
*page
, long flags
)
1542 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1543 pgoff_t index
= page
->index
;
1548 flags
|= WF_WRITE
| WF_DELETE
;
1549 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
1551 logfs_unpack_index(index
, &bix
, &level
);
1552 if (logfs_block(page
) && logfs_block(page
)->reserved_bytes
)
1553 super
->s_dirty_pages
-= logfs_block(page
)->reserved_bytes
;
1555 if (index
< I0_BLOCKS
)
1556 return logfs_write_direct(inode
, page
, flags
);
1558 bix
= adjust_bix(bix
, level
);
1559 err
= grow_inode(inode
, bix
, level
);
1562 return logfs_write_rec(inode
, page
, bix
, level
, flags
);
1565 int logfs_write_buf(struct inode
*inode
, struct page
*page
, long flags
)
1567 struct super_block
*sb
= inode
->i_sb
;
1570 logfs_get_wblocks(sb
, page
, flags
& WF_LOCK
);
1571 ret
= __logfs_write_buf(inode
, page
, flags
);
1572 logfs_put_wblocks(sb
, page
, flags
& WF_LOCK
);
1576 static int __logfs_delete(struct inode
*inode
, struct page
*page
)
1578 long flags
= WF_DELETE
;
1581 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
1583 if (page
->index
< I0_BLOCKS
)
1584 return logfs_write_direct(inode
, page
, flags
);
1585 err
= grow_inode(inode
, page
->index
, 0);
1588 return logfs_write_rec(inode
, page
, page
->index
, 0, flags
);
1591 int logfs_delete(struct inode
*inode
, pgoff_t index
,
1592 struct shadow_tree
*shadow_tree
)
1594 struct super_block
*sb
= inode
->i_sb
;
1598 page
= logfs_get_read_page(inode
, index
, 0);
1602 logfs_get_wblocks(sb
, page
, 1);
1603 ret
= __logfs_delete(inode
, page
);
1604 logfs_put_wblocks(sb
, page
, 1);
1606 logfs_put_read_page(page
);
1611 int logfs_rewrite_block(struct inode
*inode
, u64 bix
, u64 ofs
,
1612 gc_level_t gc_level
, long flags
)
1614 level_t level
= shrink_level(gc_level
);
1618 page
= logfs_get_write_page(inode
, bix
, level
);
1622 err
= logfs_segment_read(inode
, page
, ofs
, bix
, level
);
1625 alloc_indirect_block(inode
, page
, 0);
1626 err
= logfs_write_buf(inode
, page
, flags
);
1627 if (!err
&& shrink_level(gc_level
) == 0) {
1628 /* Rewrite cannot mark the inode dirty but has to
1629 * write it immediately.
1630 * Q: Can't we just create an alias for the inode
1631 * instead? And if not, why not?
1633 if (inode
->i_ino
== LOGFS_INO_MASTER
)
1634 logfs_write_anchor(inode
->i_sb
);
1636 err
= __logfs_write_inode(inode
, page
, flags
);
1640 logfs_put_write_page(page
);
1644 static int truncate_data_block(struct inode
*inode
, struct page
*page
,
1645 u64 ofs
, struct logfs_shadow
*shadow
, u64 size
)
1647 loff_t pageofs
= page
->index
<< inode
->i_sb
->s_blocksize_bits
;
1652 /* Does truncation happen within this page? */
1653 if (size
<= pageofs
|| size
- pageofs
>= PAGE_SIZE
)
1656 logfs_unpack_index(page
->index
, &bix
, &level
);
1659 err
= logfs_segment_read(inode
, page
, ofs
, bix
, level
);
1663 zero_user_segment(page
, size
- pageofs
, PAGE_SIZE
);
1664 return logfs_segment_write(inode
, page
, shadow
);
1667 static int logfs_truncate_i0(struct inode
*inode
, struct page
*page
,
1668 struct write_control
*wc
, u64 size
)
1670 struct logfs_shadow
*shadow
;
1675 logfs_unpack_index(page
->index
, &bix
, &level
);
1677 shadow
= alloc_shadow(inode
, bix
, level
, wc
->ofs
);
1679 err
= truncate_data_block(inode
, page
, wc
->ofs
, shadow
, size
);
1681 free_shadow(inode
, shadow
);
1685 logfs_segment_delete(inode
, shadow
);
1686 set_iused(inode
, shadow
);
1687 fill_shadow_tree(inode
, page
, shadow
);
1688 wc
->ofs
= shadow
->new_ofs
;
1692 static int logfs_truncate_direct(struct inode
*inode
, u64 size
)
1694 struct logfs_inode
*li
= logfs_inode(inode
);
1695 struct write_control wc
;
1700 alloc_inode_block(inode
);
1702 for (e
= I0_BLOCKS
- 1; e
>= 0; e
--) {
1703 if (size
> (e
+1) * LOGFS_BLOCKSIZE
)
1706 wc
.ofs
= li
->li_data
[e
];
1710 page
= logfs_get_write_page(inode
, e
, 0);
1713 err
= logfs_segment_read(inode
, page
, wc
.ofs
, e
, 0);
1715 logfs_put_write_page(page
);
1718 err
= logfs_truncate_i0(inode
, page
, &wc
, size
);
1719 logfs_put_write_page(page
);
1723 li
->li_data
[e
] = wc
.ofs
;
1728 /* FIXME: these need to become per-sb once we support different blocksizes */
1729 static u64 __logfs_step
[] = {
1736 static u64 __logfs_start_index
[] = {
1743 static inline u64
logfs_step(level_t level
)
1745 return __logfs_step
[(__force u8
)level
];
1748 static inline u64
logfs_factor(u8 level
)
1750 return __logfs_step
[level
] * LOGFS_BLOCKSIZE
;
1753 static inline u64
logfs_start_index(level_t level
)
1755 return __logfs_start_index
[(__force u8
)level
];
1758 static void logfs_unpack_raw_index(pgoff_t index
, u64
*bix
, level_t
*level
)
1760 logfs_unpack_index(index
, bix
, level
);
1761 if (*bix
<= logfs_start_index(SUBLEVEL(*level
)))
1765 static int __logfs_truncate_rec(struct inode
*inode
, struct page
*ipage
,
1766 struct write_control
*this_wc
, u64 size
)
1768 int truncate_happened
= 0;
1770 u64 bix
, child_bix
, next_bix
;
1773 struct write_control child_wc
= { /* FIXME: flags */ };
1775 logfs_unpack_raw_index(ipage
->index
, &bix
, &level
);
1776 err
= logfs_segment_read(inode
, ipage
, this_wc
->ofs
, bix
, level
);
1780 for (e
= LOGFS_BLOCK_FACTOR
- 1; e
>= 0; e
--) {
1781 child_bix
= bix
+ e
* logfs_step(SUBLEVEL(level
));
1782 next_bix
= child_bix
+ logfs_step(SUBLEVEL(level
));
1783 if (size
> next_bix
* LOGFS_BLOCKSIZE
)
1786 child_wc
.ofs
= pure_ofs(block_get_pointer(ipage
, e
));
1790 page
= logfs_get_write_page(inode
, child_bix
, SUBLEVEL(level
));
1794 if ((__force u8
)level
> 1)
1795 err
= __logfs_truncate_rec(inode
, page
, &child_wc
, size
);
1797 err
= logfs_truncate_i0(inode
, page
, &child_wc
, size
);
1798 logfs_put_write_page(page
);
1802 truncate_happened
= 1;
1803 alloc_indirect_block(inode
, ipage
, 0);
1804 block_set_pointer(ipage
, e
, child_wc
.ofs
);
1807 if (!truncate_happened
) {
1808 printk("ineffectual truncate (%lx, %lx, %llx)\n", inode
->i_ino
, ipage
->index
, size
);
1812 this_wc
->flags
= WF_DELETE
;
1813 if (logfs_block(ipage
)->partial
)
1814 this_wc
->flags
|= WF_WRITE
;
1816 return logfs_write_i0(inode
, ipage
, this_wc
);
1819 static int logfs_truncate_rec(struct inode
*inode
, u64 size
)
1821 struct logfs_inode
*li
= logfs_inode(inode
);
1822 struct write_control wc
= {
1823 .ofs
= li
->li_data
[INDIRECT_INDEX
],
1828 alloc_inode_block(inode
);
1833 page
= logfs_get_write_page(inode
, 0, LEVEL(li
->li_height
));
1837 err
= __logfs_truncate_rec(inode
, page
, &wc
, size
);
1838 logfs_put_write_page(page
);
1842 if (li
->li_data
[INDIRECT_INDEX
] != wc
.ofs
)
1843 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1847 static int __logfs_truncate(struct inode
*inode
, u64 size
)
1851 if (size
>= logfs_factor(logfs_inode(inode
)->li_height
))
1854 ret
= logfs_truncate_rec(inode
, size
);
1858 return logfs_truncate_direct(inode
, size
);
1862 * Truncate, by changing the segment file, can consume a fair amount
1863 * of resources. So back off from time to time and do some GC.
1864 * 8 or 2048 blocks should be well within safety limits even if
1865 * every single block resided in a different segment.
1867 #define TRUNCATE_STEP (8 * 1024 * 1024)
1868 int logfs_truncate(struct inode
*inode
, u64 target
)
1870 struct super_block
*sb
= inode
->i_sb
;
1871 u64 size
= i_size_read(inode
);
1874 size
= ALIGN(size
, TRUNCATE_STEP
);
1875 while (size
> target
) {
1876 if (size
> TRUNCATE_STEP
)
1877 size
-= TRUNCATE_STEP
;
1883 logfs_get_wblocks(sb
, NULL
, 1);
1884 err
= __logfs_truncate(inode
, size
);
1886 err
= __logfs_write_inode(inode
, NULL
, 0);
1887 logfs_put_wblocks(sb
, NULL
, 1);
1891 err
= inode_newsize_ok(inode
, target
);
1895 truncate_setsize(inode
, target
);
1899 /* I don't trust error recovery yet. */
1904 static void move_page_to_inode(struct inode
*inode
, struct page
*page
)
1906 struct logfs_inode
*li
= logfs_inode(inode
);
1907 struct logfs_block
*block
= logfs_block(page
);
1912 log_blockmove("move_page_to_inode(%llx, %llx, %x)\n",
1913 block
->ino
, block
->bix
, block
->level
);
1914 BUG_ON(li
->li_block
);
1915 block
->ops
= &inode_block_ops
;
1916 block
->inode
= inode
;
1917 li
->li_block
= block
;
1920 if (PagePrivate(page
)) {
1921 ClearPagePrivate(page
);
1923 set_page_private(page
, 0);
1927 static void move_inode_to_page(struct page
*page
, struct inode
*inode
)
1929 struct logfs_inode
*li
= logfs_inode(inode
);
1930 struct logfs_block
*block
= li
->li_block
;
1935 log_blockmove("move_inode_to_page(%llx, %llx, %x)\n",
1936 block
->ino
, block
->bix
, block
->level
);
1937 BUG_ON(PagePrivate(page
));
1938 block
->ops
= &indirect_block_ops
;
1941 if (!PagePrivate(page
)) {
1942 SetPagePrivate(page
);
1944 set_page_private(page
, (unsigned long) block
);
1947 block
->inode
= NULL
;
1948 li
->li_block
= NULL
;
1951 int logfs_read_inode(struct inode
*inode
)
1953 struct super_block
*sb
= inode
->i_sb
;
1954 struct logfs_super
*super
= logfs_super(sb
);
1955 struct inode
*master_inode
= super
->s_master_inode
;
1957 struct logfs_disk_inode
*di
;
1958 u64 ino
= inode
->i_ino
;
1960 if (ino
<< sb
->s_blocksize_bits
> i_size_read(master_inode
))
1962 if (!logfs_exist_block(master_inode
, ino
))
1965 page
= read_cache_page(master_inode
->i_mapping
, ino
,
1966 (filler_t
*)logfs_readpage
, NULL
);
1968 return PTR_ERR(page
);
1970 di
= kmap_atomic(page
);
1971 logfs_disk_to_inode(di
, inode
);
1973 move_page_to_inode(inode
, page
);
1978 /* Caller must logfs_put_write_page(page); */
1979 static struct page
*inode_to_page(struct inode
*inode
)
1981 struct inode
*master_inode
= logfs_super(inode
->i_sb
)->s_master_inode
;
1982 struct logfs_disk_inode
*di
;
1985 BUG_ON(inode
->i_ino
== LOGFS_INO_MASTER
);
1987 page
= logfs_get_write_page(master_inode
, inode
->i_ino
, 0);
1991 di
= kmap_atomic(page
);
1992 logfs_inode_to_disk(inode
, di
);
1994 move_inode_to_page(page
, inode
);
1998 static int do_write_inode(struct inode
*inode
)
2000 struct super_block
*sb
= inode
->i_sb
;
2001 struct inode
*master_inode
= logfs_super(sb
)->s_master_inode
;
2002 loff_t size
= (inode
->i_ino
+ 1) << inode
->i_sb
->s_blocksize_bits
;
2006 BUG_ON(inode
->i_ino
== LOGFS_INO_MASTER
);
2007 /* FIXME: lock inode */
2009 if (i_size_read(master_inode
) < size
)
2010 i_size_write(master_inode
, size
);
2012 /* TODO: Tell vfs this inode is clean now */
2014 page
= inode_to_page(inode
);
2018 /* FIXME: transaction is part of logfs_block now. Is that enough? */
2019 err
= logfs_write_buf(master_inode
, page
, 0);
2021 move_page_to_inode(inode
, page
);
2023 logfs_put_write_page(page
);
2027 static void logfs_mod_segment_entry(struct super_block
*sb
, u32 segno
,
2029 void (*change_se
)(struct logfs_segment_entry
*, long),
2032 struct logfs_super
*super
= logfs_super(sb
);
2033 struct inode
*inode
;
2035 struct logfs_segment_entry
*se
;
2039 page_no
= segno
>> (sb
->s_blocksize_bits
- 3);
2040 child_no
= segno
& ((sb
->s_blocksize
>> 3) - 1);
2042 inode
= super
->s_segfile_inode
;
2043 page
= logfs_get_write_page(inode
, page_no
, 0);
2044 BUG_ON(!page
); /* FIXME: We need some reserve page for this case */
2045 if (!PageUptodate(page
))
2046 logfs_read_block(inode
, page
, WRITE
);
2049 alloc_indirect_block(inode
, page
, 0);
2050 se
= kmap_atomic(page
);
2051 change_se(se
+ child_no
, arg
);
2053 logfs_set_alias(sb
, logfs_block(page
), child_no
);
2054 BUG_ON((int)be32_to_cpu(se
[child_no
].valid
) > super
->s_segsize
);
2058 logfs_put_write_page(page
);
2061 static void __get_segment_entry(struct logfs_segment_entry
*se
, long _target
)
2063 struct logfs_segment_entry
*target
= (void *)_target
;
2068 void logfs_get_segment_entry(struct super_block
*sb
, u32 segno
,
2069 struct logfs_segment_entry
*se
)
2071 logfs_mod_segment_entry(sb
, segno
, 0, __get_segment_entry
, (long)se
);
2074 static void __set_segment_used(struct logfs_segment_entry
*se
, long increment
)
2078 valid
= be32_to_cpu(se
->valid
);
2080 se
->valid
= cpu_to_be32(valid
);
2083 void logfs_set_segment_used(struct super_block
*sb
, u64 ofs
, int increment
)
2085 struct logfs_super
*super
= logfs_super(sb
);
2086 u32 segno
= ofs
>> super
->s_segshift
;
2091 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_used
, increment
);
2094 static void __set_segment_erased(struct logfs_segment_entry
*se
, long ec_level
)
2096 se
->ec_level
= cpu_to_be32(ec_level
);
2099 void logfs_set_segment_erased(struct super_block
*sb
, u32 segno
, u32 ec
,
2100 gc_level_t gc_level
)
2102 u32 ec_level
= ec
<< 4 | (__force u8
)gc_level
;
2104 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_erased
, ec_level
);
2107 static void __set_segment_reserved(struct logfs_segment_entry
*se
, long ignore
)
2109 se
->valid
= cpu_to_be32(RESERVED
);
2112 void logfs_set_segment_reserved(struct super_block
*sb
, u32 segno
)
2114 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_reserved
, 0);
2117 static void __set_segment_unreserved(struct logfs_segment_entry
*se
,
2121 se
->ec_level
= cpu_to_be32(ec_level
);
2124 void logfs_set_segment_unreserved(struct super_block
*sb
, u32 segno
, u32 ec
)
2126 u32 ec_level
= ec
<< 4;
2128 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_unreserved
,
2132 int __logfs_write_inode(struct inode
*inode
, struct page
*page
, long flags
)
2134 struct super_block
*sb
= inode
->i_sb
;
2137 logfs_get_wblocks(sb
, page
, flags
& WF_LOCK
);
2138 ret
= do_write_inode(inode
);
2139 logfs_put_wblocks(sb
, page
, flags
& WF_LOCK
);
2143 static int do_delete_inode(struct inode
*inode
)
2145 struct super_block
*sb
= inode
->i_sb
;
2146 struct inode
*master_inode
= logfs_super(sb
)->s_master_inode
;
2150 page
= logfs_get_write_page(master_inode
, inode
->i_ino
, 0);
2154 move_inode_to_page(page
, inode
);
2156 logfs_get_wblocks(sb
, page
, 1);
2157 ret
= __logfs_delete(master_inode
, page
);
2158 logfs_put_wblocks(sb
, page
, 1);
2160 logfs_put_write_page(page
);
2165 * ZOMBIE inodes have already been deleted before and should remain dead,
2166 * if it weren't for valid checking. No need to kill them again here.
2168 void logfs_evict_inode(struct inode
*inode
)
2170 struct super_block
*sb
= inode
->i_sb
;
2171 struct logfs_inode
*li
= logfs_inode(inode
);
2172 struct logfs_block
*block
= li
->li_block
;
2175 if (!inode
->i_nlink
) {
2176 if (!(li
->li_flags
& LOGFS_IF_ZOMBIE
)) {
2177 li
->li_flags
|= LOGFS_IF_ZOMBIE
;
2178 if (i_size_read(inode
) > 0)
2179 logfs_truncate(inode
, 0);
2180 do_delete_inode(inode
);
2183 truncate_inode_pages_final(&inode
->i_data
);
2186 /* Cheaper version of write_inode. All changes are concealed in
2187 * aliases, which are moved back. No write to the medium happens.
2189 /* Only deleted files may be dirty at this point */
2190 BUG_ON(inode
->i_state
& I_DIRTY
&& inode
->i_nlink
);
2193 if ((logfs_super(sb
)->s_flags
& LOGFS_SB_FLAG_SHUTDOWN
)) {
2194 block
->ops
->free_block(inode
->i_sb
, block
);
2198 page
= inode_to_page(inode
);
2199 BUG_ON(!page
); /* FIXME: Use emergency page */
2200 logfs_put_write_page(page
);
2203 void btree_write_block(struct logfs_block
*block
)
2205 struct inode
*inode
;
2209 inode
= logfs_safe_iget(block
->sb
, block
->ino
, &cookie
);
2210 page
= logfs_get_write_page(inode
, block
->bix
, block
->level
);
2212 err
= logfs_readpage_nolock(page
);
2214 BUG_ON(!PagePrivate(page
));
2215 BUG_ON(logfs_block(page
) != block
);
2216 err
= __logfs_write_buf(inode
, page
, 0);
2218 BUG_ON(PagePrivate(page
) || page
->private);
2220 logfs_put_write_page(page
);
2221 logfs_safe_iput(inode
, cookie
);
2225 * logfs_inode_write - write inode or dentry objects
2227 * @inode: parent inode (ifile or directory)
2228 * @buf: object to write (inode or dentry)
2229 * @count: object size
2231 * @flags: write flags
2232 * @shadow_tree: shadow below this inode
2234 * FIXME: All caller of this put a 200-300 byte variable on the stack,
2235 * only to call here and do a memcpy from that stack variable. A good
2236 * example of wasted performance and stack space.
2238 int logfs_inode_write(struct inode
*inode
, const void *buf
, size_t count
,
2239 loff_t bix
, long flags
, struct shadow_tree
*shadow_tree
)
2241 loff_t pos
= bix
<< inode
->i_sb
->s_blocksize_bits
;
2246 BUG_ON(pos
& (LOGFS_BLOCKSIZE
-1));
2247 BUG_ON(count
> LOGFS_BLOCKSIZE
);
2248 page
= logfs_get_write_page(inode
, bix
, 0);
2252 pagebuf
= kmap_atomic(page
);
2253 memcpy(pagebuf
, buf
, count
);
2254 flush_dcache_page(page
);
2255 kunmap_atomic(pagebuf
);
2257 if (i_size_read(inode
) < pos
+ LOGFS_BLOCKSIZE
)
2258 i_size_write(inode
, pos
+ LOGFS_BLOCKSIZE
);
2260 err
= logfs_write_buf(inode
, page
, flags
);
2261 logfs_put_write_page(page
);
2265 int logfs_open_segfile(struct super_block
*sb
)
2267 struct logfs_super
*super
= logfs_super(sb
);
2268 struct inode
*inode
;
2270 inode
= logfs_read_meta_inode(sb
, LOGFS_INO_SEGFILE
);
2272 return PTR_ERR(inode
);
2273 super
->s_segfile_inode
= inode
;
2277 int logfs_init_rw(struct super_block
*sb
)
2279 struct logfs_super
*super
= logfs_super(sb
);
2280 int min_fill
= 3 * super
->s_no_blocks
;
2282 INIT_LIST_HEAD(&super
->s_object_alias
);
2283 INIT_LIST_HEAD(&super
->s_writeback_list
);
2284 mutex_init(&super
->s_write_mutex
);
2285 super
->s_block_pool
= mempool_create_kmalloc_pool(min_fill
,
2286 sizeof(struct logfs_block
));
2287 super
->s_shadow_pool
= mempool_create_kmalloc_pool(min_fill
,
2288 sizeof(struct logfs_shadow
));
2292 void logfs_cleanup_rw(struct super_block
*sb
)
2294 struct logfs_super
*super
= logfs_super(sb
);
2296 logfs_mempool_destroy(super
->s_block_pool
);
2297 logfs_mempool_destroy(super
->s_shadow_pool
);