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 inode
->i_uid
= be32_to_cpu(di
->di_uid
);
123 inode
->i_gid
= 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 inode
->i_nlink
= 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(inode
->i_uid
);
160 di
->di_gid
= cpu_to_be32(inode
->i_gid
);
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 static void logfs_get_wblocks(struct super_block
*sb
, struct page
*page
,
250 struct logfs_super
*super
= logfs_super(sb
);
253 prelock_page(sb
, page
, lock
);
256 mutex_lock(&super
->s_write_mutex
);
258 /* FIXME: We also have to check for shadowed space
259 * and mempool fill grade */
263 static void logfs_put_wblocks(struct super_block
*sb
, struct page
*page
,
266 struct logfs_super
*super
= logfs_super(sb
);
269 preunlock_page(sb
, page
, lock
);
270 /* Order matters - we must clear PG_pre_locked before releasing
271 * s_write_mutex or we could race against another task. */
273 mutex_unlock(&super
->s_write_mutex
);
276 static struct page
*logfs_get_read_page(struct inode
*inode
, u64 bix
,
279 return find_or_create_page(inode
->i_mapping
,
280 logfs_pack_index(bix
, level
), GFP_NOFS
);
283 static void logfs_put_read_page(struct page
*page
)
286 page_cache_release(page
);
289 static void logfs_lock_write_page(struct page
*page
)
293 while (unlikely(!trylock_page(page
))) {
294 if (loop
++ > 0x1000) {
295 /* Has been observed once so far... */
296 printk(KERN_ERR
"stack at %p\n", &loop
);
299 if (PagePreLocked(page
)) {
300 /* Holder of page lock is waiting for us, it
301 * is safe to use this page. */
304 /* Some other process has this page locked and has
305 * nothing to do with us. Wait for it to finish.
309 BUG_ON(!PageLocked(page
));
312 static struct page
*logfs_get_write_page(struct inode
*inode
, u64 bix
,
315 struct address_space
*mapping
= inode
->i_mapping
;
316 pgoff_t index
= logfs_pack_index(bix
, level
);
321 page
= find_get_page(mapping
, index
);
323 page
= __page_cache_alloc(GFP_NOFS
);
326 err
= add_to_page_cache_lru(page
, mapping
, index
, GFP_NOFS
);
328 page_cache_release(page
);
333 } else logfs_lock_write_page(page
);
334 BUG_ON(!PageLocked(page
));
338 static void logfs_unlock_write_page(struct page
*page
)
340 if (!PagePreLocked(page
))
344 static void logfs_put_write_page(struct page
*page
)
346 logfs_unlock_write_page(page
);
347 page_cache_release(page
);
350 static struct page
*logfs_get_page(struct inode
*inode
, u64 bix
, level_t level
,
354 return logfs_get_read_page(inode
, bix
, level
);
356 return logfs_get_write_page(inode
, bix
, level
);
359 static void logfs_put_page(struct page
*page
, int rw
)
362 logfs_put_read_page(page
);
364 logfs_put_write_page(page
);
367 static unsigned long __get_bits(u64 val
, int skip
, int no
)
377 static unsigned long get_bits(u64 val
, level_t skip
)
379 return __get_bits(val
, (__force
int)skip
, LOGFS_BLOCK_BITS
);
382 static inline void init_shadow_tree(struct super_block
*sb
,
383 struct shadow_tree
*tree
)
385 struct logfs_super
*super
= logfs_super(sb
);
387 btree_init_mempool64(&tree
->new, super
->s_btree_pool
);
388 btree_init_mempool64(&tree
->old
, super
->s_btree_pool
);
391 static void indirect_write_block(struct logfs_block
*block
)
398 inode
= page
->mapping
->host
;
399 logfs_lock_write_page(page
);
400 ret
= logfs_write_buf(inode
, page
, 0);
401 logfs_unlock_write_page(page
);
403 * This needs some rework. Unless you want your filesystem to run
404 * completely synchronously (you don't), the filesystem will always
405 * report writes as 'successful' before the actual work has been
406 * done. The actual work gets done here and this is where any errors
407 * will show up. And there isn't much we can do about it, really.
409 * Some attempts to fix the errors (move from bad blocks, retry io,...)
410 * have already been done, so anything left should be either a broken
411 * device or a bug somewhere in logfs itself. Being relatively new,
412 * the odds currently favor a bug, so for now the line below isn't
418 static void inode_write_block(struct logfs_block
*block
)
423 inode
= block
->inode
;
424 if (inode
->i_ino
== LOGFS_INO_MASTER
)
425 logfs_write_anchor(inode
->i_sb
);
427 ret
= __logfs_write_inode(inode
, 0);
428 /* see indirect_write_block comment */
434 * This silences a false, yet annoying gcc warning. I hate it when my editor
435 * jumps into bitops.h each time I recompile this file.
436 * TODO: Complain to gcc folks about this and upgrade compiler.
438 static unsigned long fnb(const unsigned long *addr
,
439 unsigned long size
, unsigned long offset
)
441 return find_next_bit(addr
, size
, offset
);
444 static __be64
inode_val0(struct inode
*inode
)
446 struct logfs_inode
*li
= logfs_inode(inode
);
450 * Explicit shifting generates good code, but must match the format
451 * of the structure. Add some paranoia just in case.
453 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_mode
) != 0);
454 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_height
) != 2);
455 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_flags
) != 4);
457 val
= (u64
)inode
->i_mode
<< 48 |
458 (u64
)li
->li_height
<< 40 |
460 return cpu_to_be64(val
);
463 static int inode_write_alias(struct super_block
*sb
,
464 struct logfs_block
*block
, write_alias_t
*write_one_alias
)
466 struct inode
*inode
= block
->inode
;
467 struct logfs_inode
*li
= logfs_inode(inode
);
474 for (pos
= 0; ; pos
++) {
475 pos
= fnb(block
->alias_map
, LOGFS_BLOCK_FACTOR
, pos
);
476 if (pos
>= LOGFS_EMBEDDED_FIELDS
+ INODE_POINTER_OFS
)
480 case INODE_HEIGHT_OFS
:
481 val
= inode_val0(inode
);
484 val
= cpu_to_be64(li
->li_used_bytes
);;
487 val
= cpu_to_be64(i_size_read(inode
));
489 case INODE_POINTER_OFS
... INODE_POINTER_OFS
+ LOGFS_EMBEDDED_FIELDS
- 1:
490 val
= cpu_to_be64(li
->li_data
[pos
- INODE_POINTER_OFS
]);
496 ino
= LOGFS_INO_MASTER
;
499 err
= write_one_alias(sb
, ino
, bix
, level
, pos
, val
);
505 static int indirect_write_alias(struct super_block
*sb
,
506 struct logfs_block
*block
, write_alias_t
*write_one_alias
)
509 struct page
*page
= block
->page
;
515 for (pos
= 0; ; pos
++) {
516 pos
= fnb(block
->alias_map
, LOGFS_BLOCK_FACTOR
, pos
);
517 if (pos
>= LOGFS_BLOCK_FACTOR
)
520 ino
= page
->mapping
->host
->i_ino
;
521 logfs_unpack_index(page
->index
, &bix
, &level
);
522 child
= kmap_atomic(page
, KM_USER0
);
524 kunmap_atomic(child
, KM_USER0
);
525 err
= write_one_alias(sb
, ino
, bix
, level
, pos
, val
);
531 int logfs_write_obj_aliases_pagecache(struct super_block
*sb
)
533 struct logfs_super
*super
= logfs_super(sb
);
534 struct logfs_block
*block
;
537 list_for_each_entry(block
, &super
->s_object_alias
, alias_list
) {
538 err
= block
->ops
->write_alias(sb
, block
, write_alias_journal
);
545 void __free_block(struct super_block
*sb
, struct logfs_block
*block
)
547 BUG_ON(!list_empty(&block
->item_list
));
548 list_del(&block
->alias_list
);
549 mempool_free(block
, logfs_super(sb
)->s_block_pool
);
552 static void inode_free_block(struct super_block
*sb
, struct logfs_block
*block
)
554 struct inode
*inode
= block
->inode
;
556 logfs_inode(inode
)->li_block
= NULL
;
557 __free_block(sb
, block
);
560 static void indirect_free_block(struct super_block
*sb
,
561 struct logfs_block
*block
)
563 ClearPagePrivate(block
->page
);
564 block
->page
->private = 0;
565 __free_block(sb
, block
);
569 static struct logfs_block_ops inode_block_ops
= {
570 .write_block
= inode_write_block
,
571 .free_block
= inode_free_block
,
572 .write_alias
= inode_write_alias
,
575 struct logfs_block_ops indirect_block_ops
= {
576 .write_block
= indirect_write_block
,
577 .free_block
= indirect_free_block
,
578 .write_alias
= indirect_write_alias
,
581 struct logfs_block
*__alloc_block(struct super_block
*sb
,
582 u64 ino
, u64 bix
, level_t level
)
584 struct logfs_super
*super
= logfs_super(sb
);
585 struct logfs_block
*block
;
587 block
= mempool_alloc(super
->s_block_pool
, GFP_NOFS
);
588 memset(block
, 0, sizeof(*block
));
589 INIT_LIST_HEAD(&block
->alias_list
);
590 INIT_LIST_HEAD(&block
->item_list
);
594 block
->level
= level
;
598 static void alloc_inode_block(struct inode
*inode
)
600 struct logfs_inode
*li
= logfs_inode(inode
);
601 struct logfs_block
*block
;
606 block
= __alloc_block(inode
->i_sb
, LOGFS_INO_MASTER
, inode
->i_ino
, 0);
607 block
->inode
= inode
;
608 li
->li_block
= block
;
609 block
->ops
= &inode_block_ops
;
612 void initialize_block_counters(struct page
*page
, struct logfs_block
*block
,
613 __be64
*array
, int page_is_empty
)
621 if (page
->index
< first_indirect_block()) {
622 /* Counters are pointless on level 0 */
625 if (page
->index
== first_indirect_block()) {
626 /* Skip unused pointers */
628 block
->full
= I0_BLOCKS
;
630 if (!page_is_empty
) {
631 for (i
= start
; i
< LOGFS_BLOCK_FACTOR
; i
++) {
632 ptr
= be64_to_cpu(array
[i
]);
635 if (ptr
& LOGFS_FULLY_POPULATED
)
641 static void alloc_data_block(struct inode
*inode
, struct page
*page
)
643 struct logfs_block
*block
;
647 if (PagePrivate(page
))
650 logfs_unpack_index(page
->index
, &bix
, &level
);
651 block
= __alloc_block(inode
->i_sb
, inode
->i_ino
, bix
, level
);
653 SetPagePrivate(page
);
654 page
->private = (unsigned long)block
;
655 block
->ops
= &indirect_block_ops
;
658 static void alloc_indirect_block(struct inode
*inode
, struct page
*page
,
661 struct logfs_block
*block
;
664 if (PagePrivate(page
))
667 alloc_data_block(inode
, page
);
669 block
= logfs_block(page
);
670 array
= kmap_atomic(page
, KM_USER0
);
671 initialize_block_counters(page
, block
, array
, page_is_empty
);
672 kunmap_atomic(array
, KM_USER0
);
675 static void block_set_pointer(struct page
*page
, int index
, u64 ptr
)
677 struct logfs_block
*block
= logfs_block(page
);
682 array
= kmap_atomic(page
, KM_USER0
);
683 oldptr
= be64_to_cpu(array
[index
]);
684 array
[index
] = cpu_to_be64(ptr
);
685 kunmap_atomic(array
, KM_USER0
);
686 SetPageUptodate(page
);
688 block
->full
+= !!(ptr
& LOGFS_FULLY_POPULATED
)
689 - !!(oldptr
& LOGFS_FULLY_POPULATED
);
690 block
->partial
+= !!ptr
- !!oldptr
;
693 static u64
block_get_pointer(struct page
*page
, int index
)
698 block
= kmap_atomic(page
, KM_USER0
);
699 ptr
= be64_to_cpu(block
[index
]);
700 kunmap_atomic(block
, KM_USER0
);
704 static int logfs_read_empty(struct page
*page
)
706 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
710 static int logfs_read_direct(struct inode
*inode
, struct page
*page
)
712 struct logfs_inode
*li
= logfs_inode(inode
);
713 pgoff_t index
= page
->index
;
716 block
= li
->li_data
[index
];
718 return logfs_read_empty(page
);
720 return logfs_segment_read(inode
, page
, block
, index
, 0);
723 static int logfs_read_loop(struct inode
*inode
, struct page
*page
,
726 struct logfs_inode
*li
= logfs_inode(inode
);
727 u64 bix
, bofs
= li
->li_data
[INDIRECT_INDEX
];
728 level_t level
, target_level
;
732 logfs_unpack_index(page
->index
, &bix
, &target_level
);
734 return logfs_read_empty(page
);
736 if (bix
>= maxbix(li
->li_height
))
737 return logfs_read_empty(page
);
739 for (level
= LEVEL(li
->li_height
);
740 (__force u8
)level
> (__force u8
)target_level
;
741 level
= SUBLEVEL(level
)){
742 ipage
= logfs_get_page(inode
, bix
, level
, rw_context
);
746 ret
= logfs_segment_read(inode
, ipage
, bofs
, bix
, level
);
748 logfs_put_read_page(ipage
);
752 bofs
= block_get_pointer(ipage
, get_bits(bix
, SUBLEVEL(level
)));
753 logfs_put_page(ipage
, rw_context
);
755 return logfs_read_empty(page
);
758 return logfs_segment_read(inode
, page
, bofs
, bix
, 0);
761 static int logfs_read_block(struct inode
*inode
, struct page
*page
,
764 pgoff_t index
= page
->index
;
766 if (index
< I0_BLOCKS
)
767 return logfs_read_direct(inode
, page
);
768 return logfs_read_loop(inode
, page
, rw_context
);
771 static int logfs_exist_loop(struct inode
*inode
, u64 bix
)
773 struct logfs_inode
*li
= logfs_inode(inode
);
774 u64 bofs
= li
->li_data
[INDIRECT_INDEX
];
781 if (bix
>= maxbix(li
->li_height
))
784 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)) {
785 ipage
= logfs_get_read_page(inode
, bix
, level
);
789 ret
= logfs_segment_read(inode
, ipage
, bofs
, bix
, level
);
791 logfs_put_read_page(ipage
);
795 bofs
= block_get_pointer(ipage
, get_bits(bix
, SUBLEVEL(level
)));
796 logfs_put_read_page(ipage
);
804 int logfs_exist_block(struct inode
*inode
, u64 bix
)
806 struct logfs_inode
*li
= logfs_inode(inode
);
809 return !!li
->li_data
[bix
];
810 return logfs_exist_loop(inode
, bix
);
813 static u64
seek_holedata_direct(struct inode
*inode
, u64 bix
, int data
)
815 struct logfs_inode
*li
= logfs_inode(inode
);
817 for (; bix
< I0_BLOCKS
; bix
++)
818 if (data
^ (li
->li_data
[bix
] == 0))
823 static u64
seek_holedata_loop(struct inode
*inode
, u64 bix
, int data
)
825 struct logfs_inode
*li
= logfs_inode(inode
);
827 u64 increment
, bofs
= li
->li_data
[INDIRECT_INDEX
];
834 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)) {
835 increment
= 1 << (LOGFS_BLOCK_BITS
* ((__force u8
)level
-1));
836 page
= logfs_get_read_page(inode
, bix
, level
);
840 ret
= logfs_segment_read(inode
, page
, bofs
, bix
, level
);
842 logfs_put_read_page(page
);
846 slot
= get_bits(bix
, SUBLEVEL(level
));
847 rblock
= kmap_atomic(page
, KM_USER0
);
848 while (slot
< LOGFS_BLOCK_FACTOR
) {
849 if (data
&& (rblock
[slot
] != 0))
851 if (!data
&& !(be64_to_cpu(rblock
[slot
]) & LOGFS_FULLY_POPULATED
))
855 bix
&= ~(increment
- 1);
857 if (slot
>= LOGFS_BLOCK_FACTOR
) {
858 kunmap_atomic(rblock
, KM_USER0
);
859 logfs_put_read_page(page
);
862 bofs
= be64_to_cpu(rblock
[slot
]);
863 kunmap_atomic(rblock
, KM_USER0
);
864 logfs_put_read_page(page
);
874 * logfs_seek_hole - find next hole starting at a given block index
875 * @inode: inode to search in
876 * @bix: block index to start searching
878 * Returns next hole. If the file doesn't contain any further holes, the
879 * block address next to eof is returned instead.
881 u64
logfs_seek_hole(struct inode
*inode
, u64 bix
)
883 struct logfs_inode
*li
= logfs_inode(inode
);
885 if (bix
< I0_BLOCKS
) {
886 bix
= seek_holedata_direct(inode
, bix
, 0);
891 if (!li
->li_data
[INDIRECT_INDEX
])
893 else if (li
->li_data
[INDIRECT_INDEX
] & LOGFS_FULLY_POPULATED
)
894 bix
= maxbix(li
->li_height
);
896 bix
= seek_holedata_loop(inode
, bix
, 0);
897 if (bix
< maxbix(li
->li_height
))
899 /* Should not happen anymore. But if some port writes semi-
900 * corrupt images (as this one used to) we might run into it.
902 WARN_ON_ONCE(bix
== maxbix(li
->li_height
));
908 static u64
__logfs_seek_data(struct inode
*inode
, u64 bix
)
910 struct logfs_inode
*li
= logfs_inode(inode
);
912 if (bix
< I0_BLOCKS
) {
913 bix
= seek_holedata_direct(inode
, bix
, 1);
918 if (bix
< maxbix(li
->li_height
)) {
919 if (!li
->li_data
[INDIRECT_INDEX
])
920 bix
= maxbix(li
->li_height
);
922 return seek_holedata_loop(inode
, bix
, 1);
929 * logfs_seek_data - find next data block after a given block index
930 * @inode: inode to search in
931 * @bix: block index to start searching
933 * Returns next data block. If the file doesn't contain any further data
934 * blocks, the last block in the file is returned instead.
936 u64
logfs_seek_data(struct inode
*inode
, u64 bix
)
938 struct super_block
*sb
= inode
->i_sb
;
941 ret
= __logfs_seek_data(inode
, bix
);
942 end
= i_size_read(inode
) >> sb
->s_blocksize_bits
;
948 static int logfs_is_valid_direct(struct logfs_inode
*li
, u64 bix
, u64 ofs
)
950 return pure_ofs(li
->li_data
[bix
]) == ofs
;
953 static int __logfs_is_valid_loop(struct inode
*inode
, u64 bix
,
956 struct logfs_inode
*li
= logfs_inode(inode
);
961 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)){
962 page
= logfs_get_write_page(inode
, bix
, level
);
965 ret
= logfs_segment_read(inode
, page
, bofs
, bix
, level
);
967 logfs_put_write_page(page
);
971 bofs
= block_get_pointer(page
, get_bits(bix
, SUBLEVEL(level
)));
972 logfs_put_write_page(page
);
976 if (pure_ofs(bofs
) == ofs
)
982 static int logfs_is_valid_loop(struct inode
*inode
, u64 bix
, u64 ofs
)
984 struct logfs_inode
*li
= logfs_inode(inode
);
985 u64 bofs
= li
->li_data
[INDIRECT_INDEX
];
990 if (bix
>= maxbix(li
->li_height
))
993 if (pure_ofs(bofs
) == ofs
)
996 return __logfs_is_valid_loop(inode
, bix
, ofs
, bofs
);
999 static int __logfs_is_valid_block(struct inode
*inode
, u64 bix
, u64 ofs
)
1001 struct logfs_inode
*li
= logfs_inode(inode
);
1003 if ((inode
->i_nlink
== 0) && atomic_read(&inode
->i_count
) == 1)
1006 if (bix
< I0_BLOCKS
)
1007 return logfs_is_valid_direct(li
, bix
, ofs
);
1008 return logfs_is_valid_loop(inode
, bix
, ofs
);
1012 * logfs_is_valid_block - check whether this block is still valid
1015 * @ofs - block physical offset
1016 * @ino - block inode number
1017 * @bix - block index
1018 * @level - block level
1020 * Returns 0 if the block is invalid, 1 if it is valid and 2 if it will
1021 * become invalid once the journal is written.
1023 int logfs_is_valid_block(struct super_block
*sb
, u64 ofs
, u64 ino
, u64 bix
,
1024 gc_level_t gc_level
)
1026 struct logfs_super
*super
= logfs_super(sb
);
1027 struct inode
*inode
;
1030 /* Umount closes a segment with free blocks remaining. Those
1031 * blocks are by definition invalid. */
1035 LOGFS_BUG_ON((u64
)(u_long
)ino
!= ino
, sb
);
1037 inode
= logfs_safe_iget(sb
, ino
, &cookie
);
1041 ret
= __logfs_is_valid_block(inode
, bix
, ofs
);
1042 logfs_safe_iput(inode
, cookie
);
1047 /* Block is nominally invalid, but may still sit in the shadow tree,
1048 * waiting for a journal commit.
1050 if (btree_lookup64(&super
->s_shadow_tree
.old
, ofs
))
1055 int logfs_readpage_nolock(struct page
*page
)
1057 struct inode
*inode
= page
->mapping
->host
;
1060 ret
= logfs_read_block(inode
, page
, READ
);
1063 ClearPageUptodate(page
);
1066 SetPageUptodate(page
);
1067 ClearPageError(page
);
1069 flush_dcache_page(page
);
1074 static int logfs_reserve_bytes(struct inode
*inode
, int bytes
)
1076 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1077 u64 available
= super
->s_free_bytes
+ super
->s_dirty_free_bytes
1078 - super
->s_dirty_used_bytes
- super
->s_dirty_pages
;
1083 if (available
< bytes
)
1086 if (available
< bytes
+ super
->s_root_reserve
&&
1087 !capable(CAP_SYS_RESOURCE
))
1093 int get_page_reserve(struct inode
*inode
, struct page
*page
)
1095 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1098 if (logfs_block(page
) && logfs_block(page
)->reserved_bytes
)
1101 logfs_get_wblocks(inode
->i_sb
, page
, WF_LOCK
);
1102 ret
= logfs_reserve_bytes(inode
, 6 * LOGFS_MAX_OBJECTSIZE
);
1104 alloc_data_block(inode
, page
);
1105 logfs_block(page
)->reserved_bytes
+= 6 * LOGFS_MAX_OBJECTSIZE
;
1106 super
->s_dirty_pages
+= 6 * LOGFS_MAX_OBJECTSIZE
;
1108 logfs_put_wblocks(inode
->i_sb
, page
, WF_LOCK
);
1113 * We are protected by write lock. Push victims up to superblock level
1114 * and release transaction when appropriate.
1116 /* FIXME: This is currently called from the wrong spots. */
1117 static void logfs_handle_transaction(struct inode
*inode
,
1118 struct logfs_transaction
*ta
)
1120 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1124 logfs_inode(inode
)->li_block
->ta
= NULL
;
1126 if (inode
->i_ino
!= LOGFS_INO_MASTER
) {
1127 BUG(); /* FIXME: Yes, this needs more thought */
1128 /* just remember the transaction until inode is written */
1129 //BUG_ON(logfs_inode(inode)->li_transaction);
1130 //logfs_inode(inode)->li_transaction = ta;
1134 switch (ta
->state
) {
1135 case CREATE_1
: /* fall through */
1137 BUG_ON(super
->s_victim_ino
);
1138 super
->s_victim_ino
= ta
->ino
;
1140 case CREATE_2
: /* fall through */
1142 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1143 super
->s_victim_ino
= 0;
1144 /* transaction ends here - free it */
1147 case CROSS_RENAME_1
:
1148 BUG_ON(super
->s_rename_dir
);
1149 BUG_ON(super
->s_rename_pos
);
1150 super
->s_rename_dir
= ta
->dir
;
1151 super
->s_rename_pos
= ta
->pos
;
1153 case CROSS_RENAME_2
:
1154 BUG_ON(super
->s_rename_dir
!= ta
->dir
);
1155 BUG_ON(super
->s_rename_pos
!= ta
->pos
);
1156 super
->s_rename_dir
= 0;
1157 super
->s_rename_pos
= 0;
1160 case TARGET_RENAME_1
:
1161 BUG_ON(super
->s_rename_dir
);
1162 BUG_ON(super
->s_rename_pos
);
1163 BUG_ON(super
->s_victim_ino
);
1164 super
->s_rename_dir
= ta
->dir
;
1165 super
->s_rename_pos
= ta
->pos
;
1166 super
->s_victim_ino
= ta
->ino
;
1168 case TARGET_RENAME_2
:
1169 BUG_ON(super
->s_rename_dir
!= ta
->dir
);
1170 BUG_ON(super
->s_rename_pos
!= ta
->pos
);
1171 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1172 super
->s_rename_dir
= 0;
1173 super
->s_rename_pos
= 0;
1175 case TARGET_RENAME_3
:
1176 BUG_ON(super
->s_rename_dir
);
1177 BUG_ON(super
->s_rename_pos
);
1178 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1179 super
->s_victim_ino
= 0;
1188 * Not strictly a reservation, but rather a check that we still have enough
1189 * space to satisfy the write.
1191 static int logfs_reserve_blocks(struct inode
*inode
, int blocks
)
1193 return logfs_reserve_bytes(inode
, blocks
* LOGFS_MAX_OBJECTSIZE
);
1196 struct write_control
{
1201 static struct logfs_shadow
*alloc_shadow(struct inode
*inode
, u64 bix
,
1202 level_t level
, u64 old_ofs
)
1204 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1205 struct logfs_shadow
*shadow
;
1207 shadow
= mempool_alloc(super
->s_shadow_pool
, GFP_NOFS
);
1208 memset(shadow
, 0, sizeof(*shadow
));
1209 shadow
->ino
= inode
->i_ino
;
1211 shadow
->gc_level
= expand_level(inode
->i_ino
, level
);
1212 shadow
->old_ofs
= old_ofs
& ~LOGFS_FULLY_POPULATED
;
1216 static void free_shadow(struct inode
*inode
, struct logfs_shadow
*shadow
)
1218 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1220 mempool_free(shadow
, super
->s_shadow_pool
);
1223 static void mark_segment(struct shadow_tree
*tree
, u32 segno
)
1227 if (!btree_lookup32(&tree
->segment_map
, segno
)) {
1228 err
= btree_insert32(&tree
->segment_map
, segno
, (void *)1,
1231 tree
->no_shadowed_segments
++;
1236 * fill_shadow_tree - Propagate shadow tree changes due to a write
1237 * @inode: Inode owning the page
1238 * @page: Struct page that was written
1239 * @shadow: Shadow for the current write
1241 * Writes in logfs can result in two semi-valid objects. The old object
1242 * is still valid as long as it can be reached by following pointers on
1243 * the medium. Only when writes propagate all the way up to the journal
1244 * has the new object safely replaced the old one.
1246 * To handle this problem, a struct logfs_shadow is used to represent
1247 * every single write. It is attached to the indirect block, which is
1248 * marked dirty. When the indirect block is written, its shadows are
1249 * handed up to the next indirect block (or inode). Untimately they
1250 * will reach the master inode and be freed upon journal commit.
1252 * This function handles a single step in the propagation. It adds the
1253 * shadow for the current write to the tree, along with any shadows in
1254 * the page's tree, in case it was an indirect block. If a page is
1255 * written, the inode parameter is left NULL, if an inode is written,
1256 * the page parameter is left NULL.
1258 static void fill_shadow_tree(struct inode
*inode
, struct page
*page
,
1259 struct logfs_shadow
*shadow
)
1261 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1262 struct logfs_block
*block
= logfs_block(page
);
1263 struct shadow_tree
*tree
= &super
->s_shadow_tree
;
1265 if (PagePrivate(page
)) {
1266 if (block
->alias_map
)
1267 super
->s_no_object_aliases
-= bitmap_weight(
1268 block
->alias_map
, LOGFS_BLOCK_FACTOR
);
1269 logfs_handle_transaction(inode
, block
->ta
);
1270 block
->ops
->free_block(inode
->i_sb
, block
);
1273 if (shadow
->old_ofs
)
1274 btree_insert64(&tree
->old
, shadow
->old_ofs
, shadow
,
1277 btree_insert64(&tree
->new, shadow
->new_ofs
, shadow
,
1280 super
->s_dirty_used_bytes
+= shadow
->new_len
;
1281 super
->s_dirty_free_bytes
+= shadow
->old_len
;
1282 mark_segment(tree
, shadow
->old_ofs
>> super
->s_segshift
);
1283 mark_segment(tree
, shadow
->new_ofs
>> super
->s_segshift
);
1287 static void logfs_set_alias(struct super_block
*sb
, struct logfs_block
*block
,
1290 struct logfs_super
*super
= logfs_super(sb
);
1292 if (block
->inode
&& block
->inode
->i_ino
== LOGFS_INO_MASTER
) {
1293 /* Aliases in the master inode are pointless. */
1297 if (!test_bit(child_no
, block
->alias_map
)) {
1298 set_bit(child_no
, block
->alias_map
);
1299 super
->s_no_object_aliases
++;
1301 list_move_tail(&block
->alias_list
, &super
->s_object_alias
);
1305 * Object aliases can and often do change the size and occupied space of a
1306 * file. So not only do we have to change the pointers, we also have to
1307 * change inode->i_size and li->li_used_bytes. Which is done by setting
1308 * another two object aliases for the inode itself.
1310 static void set_iused(struct inode
*inode
, struct logfs_shadow
*shadow
)
1312 struct logfs_inode
*li
= logfs_inode(inode
);
1314 if (shadow
->new_len
== shadow
->old_len
)
1317 alloc_inode_block(inode
);
1318 li
->li_used_bytes
+= shadow
->new_len
- shadow
->old_len
;
1319 __logfs_set_blocks(inode
);
1320 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_USED_OFS
);
1321 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_SIZE_OFS
);
1324 static int logfs_write_i0(struct inode
*inode
, struct page
*page
,
1325 struct write_control
*wc
)
1327 struct logfs_shadow
*shadow
;
1332 logfs_unpack_index(page
->index
, &bix
, &level
);
1334 if (logfs_reserve_blocks(inode
, 1))
1337 shadow
= alloc_shadow(inode
, bix
, level
, wc
->ofs
);
1338 if (wc
->flags
& WF_WRITE
)
1339 err
= logfs_segment_write(inode
, page
, shadow
);
1340 if (wc
->flags
& WF_DELETE
)
1341 logfs_segment_delete(inode
, shadow
);
1343 free_shadow(inode
, shadow
);
1347 set_iused(inode
, shadow
);
1350 alloc_indirect_block(inode
, page
, 0);
1351 full
= logfs_block(page
)->full
== LOGFS_BLOCK_FACTOR
;
1353 fill_shadow_tree(inode
, page
, shadow
);
1354 wc
->ofs
= shadow
->new_ofs
;
1355 if (wc
->ofs
&& full
)
1356 wc
->ofs
|= LOGFS_FULLY_POPULATED
;
1360 static int logfs_write_direct(struct inode
*inode
, struct page
*page
,
1363 struct logfs_inode
*li
= logfs_inode(inode
);
1364 struct write_control wc
= {
1365 .ofs
= li
->li_data
[page
->index
],
1370 alloc_inode_block(inode
);
1372 err
= logfs_write_i0(inode
, page
, &wc
);
1376 li
->li_data
[page
->index
] = wc
.ofs
;
1377 logfs_set_alias(inode
->i_sb
, li
->li_block
,
1378 page
->index
+ INODE_POINTER_OFS
);
1382 static int ptr_change(u64 ofs
, struct page
*page
)
1384 struct logfs_block
*block
= logfs_block(page
);
1385 int empty0
, empty1
, full0
, full1
;
1388 empty1
= block
->partial
== 0;
1389 if (empty0
!= empty1
)
1392 /* The !! is necessary to shrink result to int */
1393 full0
= !!(ofs
& LOGFS_FULLY_POPULATED
);
1394 full1
= block
->full
== LOGFS_BLOCK_FACTOR
;
1400 static int __logfs_write_rec(struct inode
*inode
, struct page
*page
,
1401 struct write_control
*this_wc
,
1402 pgoff_t bix
, level_t target_level
, level_t level
)
1404 int ret
, page_empty
= 0;
1405 int child_no
= get_bits(bix
, SUBLEVEL(level
));
1407 struct write_control child_wc
= {
1408 .flags
= this_wc
->flags
,
1411 ipage
= logfs_get_write_page(inode
, bix
, level
);
1416 ret
= logfs_segment_read(inode
, ipage
, this_wc
->ofs
, bix
, level
);
1419 } else if (!PageUptodate(ipage
)) {
1421 logfs_read_empty(ipage
);
1424 child_wc
.ofs
= block_get_pointer(ipage
, child_no
);
1426 if ((__force u8
)level
-1 > (__force u8
)target_level
)
1427 ret
= __logfs_write_rec(inode
, page
, &child_wc
, bix
,
1428 target_level
, SUBLEVEL(level
));
1430 ret
= logfs_write_i0(inode
, page
, &child_wc
);
1435 alloc_indirect_block(inode
, ipage
, page_empty
);
1436 block_set_pointer(ipage
, child_no
, child_wc
.ofs
);
1437 /* FIXME: first condition seems superfluous */
1438 if (child_wc
.ofs
|| logfs_block(ipage
)->partial
)
1439 this_wc
->flags
|= WF_WRITE
;
1440 /* the condition on this_wc->ofs ensures that we won't consume extra
1441 * space for indirect blocks in the future, which we cannot reserve */
1442 if (!this_wc
->ofs
|| ptr_change(this_wc
->ofs
, ipage
))
1443 ret
= logfs_write_i0(inode
, ipage
, this_wc
);
1445 logfs_set_alias(inode
->i_sb
, logfs_block(ipage
), child_no
);
1447 logfs_put_write_page(ipage
);
1451 static int logfs_write_rec(struct inode
*inode
, struct page
*page
,
1452 pgoff_t bix
, level_t target_level
, long flags
)
1454 struct logfs_inode
*li
= logfs_inode(inode
);
1455 struct write_control wc
= {
1456 .ofs
= li
->li_data
[INDIRECT_INDEX
],
1461 alloc_inode_block(inode
);
1463 if (li
->li_height
> (__force u8
)target_level
)
1464 ret
= __logfs_write_rec(inode
, page
, &wc
, bix
, target_level
,
1465 LEVEL(li
->li_height
));
1467 ret
= logfs_write_i0(inode
, page
, &wc
);
1471 if (li
->li_data
[INDIRECT_INDEX
] != wc
.ofs
) {
1472 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1473 logfs_set_alias(inode
->i_sb
, li
->li_block
,
1474 INDIRECT_INDEX
+ INODE_POINTER_OFS
);
1479 void logfs_add_transaction(struct inode
*inode
, struct logfs_transaction
*ta
)
1481 alloc_inode_block(inode
);
1482 logfs_inode(inode
)->li_block
->ta
= ta
;
1485 void logfs_del_transaction(struct inode
*inode
, struct logfs_transaction
*ta
)
1487 struct logfs_block
*block
= logfs_inode(inode
)->li_block
;
1489 if (block
&& block
->ta
)
1493 static int grow_inode(struct inode
*inode
, u64 bix
, level_t level
)
1495 struct logfs_inode
*li
= logfs_inode(inode
);
1496 u8 height
= (__force u8
)level
;
1498 struct write_control wc
= {
1503 BUG_ON(height
> 5 || li
->li_height
> 5);
1504 while (height
> li
->li_height
|| bix
>= maxbix(li
->li_height
)) {
1505 page
= logfs_get_write_page(inode
, I0_BLOCKS
+ 1,
1506 LEVEL(li
->li_height
+ 1));
1509 logfs_read_empty(page
);
1510 alloc_indirect_block(inode
, page
, 1);
1511 block_set_pointer(page
, 0, li
->li_data
[INDIRECT_INDEX
]);
1512 err
= logfs_write_i0(inode
, page
, &wc
);
1513 logfs_put_write_page(page
);
1516 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1519 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_HEIGHT_OFS
);
1524 static int __logfs_write_buf(struct inode
*inode
, struct page
*page
, long flags
)
1526 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1527 pgoff_t index
= page
->index
;
1532 flags
|= WF_WRITE
| WF_DELETE
;
1533 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
1535 logfs_unpack_index(index
, &bix
, &level
);
1536 if (logfs_block(page
) && logfs_block(page
)->reserved_bytes
)
1537 super
->s_dirty_pages
-= logfs_block(page
)->reserved_bytes
;
1539 if (index
< I0_BLOCKS
)
1540 return logfs_write_direct(inode
, page
, flags
);
1542 bix
= adjust_bix(bix
, level
);
1543 err
= grow_inode(inode
, bix
, level
);
1546 return logfs_write_rec(inode
, page
, bix
, level
, flags
);
1549 int logfs_write_buf(struct inode
*inode
, struct page
*page
, long flags
)
1551 struct super_block
*sb
= inode
->i_sb
;
1554 logfs_get_wblocks(sb
, page
, flags
& WF_LOCK
);
1555 ret
= __logfs_write_buf(inode
, page
, flags
);
1556 logfs_put_wblocks(sb
, page
, flags
& WF_LOCK
);
1560 static int __logfs_delete(struct inode
*inode
, struct page
*page
)
1562 long flags
= WF_DELETE
;
1564 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
1566 if (page
->index
< I0_BLOCKS
)
1567 return logfs_write_direct(inode
, page
, flags
);
1568 return logfs_write_rec(inode
, page
, page
->index
, 0, flags
);
1571 int logfs_delete(struct inode
*inode
, pgoff_t index
,
1572 struct shadow_tree
*shadow_tree
)
1574 struct super_block
*sb
= inode
->i_sb
;
1578 page
= logfs_get_read_page(inode
, index
, 0);
1582 logfs_get_wblocks(sb
, page
, 1);
1583 ret
= __logfs_delete(inode
, page
);
1584 logfs_put_wblocks(sb
, page
, 1);
1586 logfs_put_read_page(page
);
1591 int logfs_rewrite_block(struct inode
*inode
, u64 bix
, u64 ofs
,
1592 gc_level_t gc_level
, long flags
)
1594 level_t level
= shrink_level(gc_level
);
1598 page
= logfs_get_write_page(inode
, bix
, level
);
1602 err
= logfs_segment_read(inode
, page
, ofs
, bix
, level
);
1605 alloc_indirect_block(inode
, page
, 0);
1606 err
= logfs_write_buf(inode
, page
, flags
);
1607 if (!err
&& shrink_level(gc_level
) == 0) {
1608 /* Rewrite cannot mark the inode dirty but has to
1609 * write it immediatly.
1610 * Q: Can't we just create an alias for the inode
1611 * instead? And if not, why not?
1613 if (inode
->i_ino
== LOGFS_INO_MASTER
)
1614 logfs_write_anchor(inode
->i_sb
);
1616 err
= __logfs_write_inode(inode
, flags
);
1620 logfs_put_write_page(page
);
1624 static int truncate_data_block(struct inode
*inode
, struct page
*page
,
1625 u64 ofs
, struct logfs_shadow
*shadow
, u64 size
)
1627 loff_t pageofs
= page
->index
<< inode
->i_sb
->s_blocksize_bits
;
1632 /* Does truncation happen within this page? */
1633 if (size
<= pageofs
|| size
- pageofs
>= PAGE_SIZE
)
1636 logfs_unpack_index(page
->index
, &bix
, &level
);
1639 err
= logfs_segment_read(inode
, page
, ofs
, bix
, level
);
1643 zero_user_segment(page
, size
- pageofs
, PAGE_CACHE_SIZE
);
1644 return logfs_segment_write(inode
, page
, shadow
);
1647 static int logfs_truncate_i0(struct inode
*inode
, struct page
*page
,
1648 struct write_control
*wc
, u64 size
)
1650 struct logfs_shadow
*shadow
;
1655 logfs_unpack_index(page
->index
, &bix
, &level
);
1657 shadow
= alloc_shadow(inode
, bix
, level
, wc
->ofs
);
1659 err
= truncate_data_block(inode
, page
, wc
->ofs
, shadow
, size
);
1661 free_shadow(inode
, shadow
);
1665 logfs_segment_delete(inode
, shadow
);
1666 set_iused(inode
, shadow
);
1667 fill_shadow_tree(inode
, page
, shadow
);
1668 wc
->ofs
= shadow
->new_ofs
;
1672 static int logfs_truncate_direct(struct inode
*inode
, u64 size
)
1674 struct logfs_inode
*li
= logfs_inode(inode
);
1675 struct write_control wc
;
1680 alloc_inode_block(inode
);
1682 for (e
= I0_BLOCKS
- 1; e
>= 0; e
--) {
1683 if (size
> (e
+1) * LOGFS_BLOCKSIZE
)
1686 wc
.ofs
= li
->li_data
[e
];
1690 page
= logfs_get_write_page(inode
, e
, 0);
1693 err
= logfs_segment_read(inode
, page
, wc
.ofs
, e
, 0);
1695 logfs_put_write_page(page
);
1698 err
= logfs_truncate_i0(inode
, page
, &wc
, size
);
1699 logfs_put_write_page(page
);
1703 li
->li_data
[e
] = wc
.ofs
;
1708 /* FIXME: these need to become per-sb once we support different blocksizes */
1709 static u64 __logfs_step
[] = {
1716 static u64 __logfs_start_index
[] = {
1723 static inline u64
logfs_step(level_t level
)
1725 return __logfs_step
[(__force u8
)level
];
1728 static inline u64
logfs_factor(u8 level
)
1730 return __logfs_step
[level
] * LOGFS_BLOCKSIZE
;
1733 static inline u64
logfs_start_index(level_t level
)
1735 return __logfs_start_index
[(__force u8
)level
];
1738 static void logfs_unpack_raw_index(pgoff_t index
, u64
*bix
, level_t
*level
)
1740 logfs_unpack_index(index
, bix
, level
);
1741 if (*bix
<= logfs_start_index(SUBLEVEL(*level
)))
1745 static int __logfs_truncate_rec(struct inode
*inode
, struct page
*ipage
,
1746 struct write_control
*this_wc
, u64 size
)
1748 int truncate_happened
= 0;
1750 u64 bix
, child_bix
, next_bix
;
1753 struct write_control child_wc
= { /* FIXME: flags */ };
1755 logfs_unpack_raw_index(ipage
->index
, &bix
, &level
);
1756 err
= logfs_segment_read(inode
, ipage
, this_wc
->ofs
, bix
, level
);
1760 for (e
= LOGFS_BLOCK_FACTOR
- 1; e
>= 0; e
--) {
1761 child_bix
= bix
+ e
* logfs_step(SUBLEVEL(level
));
1762 next_bix
= child_bix
+ logfs_step(SUBLEVEL(level
));
1763 if (size
> next_bix
* LOGFS_BLOCKSIZE
)
1766 child_wc
.ofs
= pure_ofs(block_get_pointer(ipage
, e
));
1770 page
= logfs_get_write_page(inode
, child_bix
, SUBLEVEL(level
));
1774 if ((__force u8
)level
> 1)
1775 err
= __logfs_truncate_rec(inode
, page
, &child_wc
, size
);
1777 err
= logfs_truncate_i0(inode
, page
, &child_wc
, size
);
1778 logfs_put_write_page(page
);
1782 truncate_happened
= 1;
1783 alloc_indirect_block(inode
, ipage
, 0);
1784 block_set_pointer(ipage
, e
, child_wc
.ofs
);
1787 if (!truncate_happened
) {
1788 printk("ineffectual truncate (%lx, %lx, %llx)\n", inode
->i_ino
, ipage
->index
, size
);
1792 this_wc
->flags
= WF_DELETE
;
1793 if (logfs_block(ipage
)->partial
)
1794 this_wc
->flags
|= WF_WRITE
;
1796 return logfs_write_i0(inode
, ipage
, this_wc
);
1799 static int logfs_truncate_rec(struct inode
*inode
, u64 size
)
1801 struct logfs_inode
*li
= logfs_inode(inode
);
1802 struct write_control wc
= {
1803 .ofs
= li
->li_data
[INDIRECT_INDEX
],
1808 alloc_inode_block(inode
);
1813 page
= logfs_get_write_page(inode
, 0, LEVEL(li
->li_height
));
1817 err
= __logfs_truncate_rec(inode
, page
, &wc
, size
);
1818 logfs_put_write_page(page
);
1822 if (li
->li_data
[INDIRECT_INDEX
] != wc
.ofs
)
1823 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1827 static int __logfs_truncate(struct inode
*inode
, u64 size
)
1831 if (size
>= logfs_factor(logfs_inode(inode
)->li_height
))
1834 ret
= logfs_truncate_rec(inode
, size
);
1838 return logfs_truncate_direct(inode
, size
);
1842 * Truncate, by changing the segment file, can consume a fair amount
1843 * of resources. So back off from time to time and do some GC.
1844 * 8 or 2048 blocks should be well within safety limits even if
1845 * every single block resided in a different segment.
1847 #define TRUNCATE_STEP (8 * 1024 * 1024)
1848 int logfs_truncate(struct inode
*inode
, u64 target
)
1850 struct super_block
*sb
= inode
->i_sb
;
1851 u64 size
= i_size_read(inode
);
1854 size
= ALIGN(size
, TRUNCATE_STEP
);
1855 while (size
> target
) {
1856 if (size
> TRUNCATE_STEP
)
1857 size
-= TRUNCATE_STEP
;
1863 logfs_get_wblocks(sb
, NULL
, 1);
1864 err
= __logfs_truncate(inode
, size
);
1866 err
= __logfs_write_inode(inode
, 0);
1867 logfs_put_wblocks(sb
, NULL
, 1);
1871 err
= vmtruncate(inode
, target
);
1873 /* I don't trust error recovery yet. */
1878 static void move_page_to_inode(struct inode
*inode
, struct page
*page
)
1880 struct logfs_inode
*li
= logfs_inode(inode
);
1881 struct logfs_block
*block
= logfs_block(page
);
1886 log_blockmove("move_page_to_inode(%llx, %llx, %x)\n",
1887 block
->ino
, block
->bix
, block
->level
);
1888 BUG_ON(li
->li_block
);
1889 block
->ops
= &inode_block_ops
;
1890 block
->inode
= inode
;
1891 li
->li_block
= block
;
1895 ClearPagePrivate(page
);
1898 static void move_inode_to_page(struct page
*page
, struct inode
*inode
)
1900 struct logfs_inode
*li
= logfs_inode(inode
);
1901 struct logfs_block
*block
= li
->li_block
;
1906 log_blockmove("move_inode_to_page(%llx, %llx, %x)\n",
1907 block
->ino
, block
->bix
, block
->level
);
1908 BUG_ON(PagePrivate(page
));
1909 block
->ops
= &indirect_block_ops
;
1911 page
->private = (unsigned long)block
;
1912 SetPagePrivate(page
);
1914 block
->inode
= NULL
;
1915 li
->li_block
= NULL
;
1918 int logfs_read_inode(struct inode
*inode
)
1920 struct super_block
*sb
= inode
->i_sb
;
1921 struct logfs_super
*super
= logfs_super(sb
);
1922 struct inode
*master_inode
= super
->s_master_inode
;
1924 struct logfs_disk_inode
*di
;
1925 u64 ino
= inode
->i_ino
;
1927 if (ino
<< sb
->s_blocksize_bits
> i_size_read(master_inode
))
1929 if (!logfs_exist_block(master_inode
, ino
))
1932 page
= read_cache_page(master_inode
->i_mapping
, ino
,
1933 (filler_t
*)logfs_readpage
, NULL
);
1935 return PTR_ERR(page
);
1937 di
= kmap_atomic(page
, KM_USER0
);
1938 logfs_disk_to_inode(di
, inode
);
1939 kunmap_atomic(di
, KM_USER0
);
1940 move_page_to_inode(inode
, page
);
1941 page_cache_release(page
);
1945 /* Caller must logfs_put_write_page(page); */
1946 static struct page
*inode_to_page(struct inode
*inode
)
1948 struct inode
*master_inode
= logfs_super(inode
->i_sb
)->s_master_inode
;
1949 struct logfs_disk_inode
*di
;
1952 BUG_ON(inode
->i_ino
== LOGFS_INO_MASTER
);
1954 page
= logfs_get_write_page(master_inode
, inode
->i_ino
, 0);
1958 di
= kmap_atomic(page
, KM_USER0
);
1959 logfs_inode_to_disk(inode
, di
);
1960 kunmap_atomic(di
, KM_USER0
);
1961 move_inode_to_page(page
, inode
);
1965 /* Cheaper version of write_inode. All changes are concealed in
1966 * aliases, which are moved back. No write to the medium happens.
1968 void logfs_clear_inode(struct inode
*inode
)
1970 struct super_block
*sb
= inode
->i_sb
;
1971 struct logfs_inode
*li
= logfs_inode(inode
);
1972 struct logfs_block
*block
= li
->li_block
;
1975 /* Only deleted files may be dirty at this point */
1976 BUG_ON(inode
->i_state
& I_DIRTY
&& inode
->i_nlink
);
1979 if ((logfs_super(sb
)->s_flags
& LOGFS_SB_FLAG_SHUTDOWN
)) {
1980 block
->ops
->free_block(inode
->i_sb
, block
);
1984 BUG_ON(inode
->i_ino
< LOGFS_RESERVED_INOS
);
1985 page
= inode_to_page(inode
);
1986 BUG_ON(!page
); /* FIXME: Use emergency page */
1987 logfs_put_write_page(page
);
1990 static int do_write_inode(struct inode
*inode
)
1992 struct super_block
*sb
= inode
->i_sb
;
1993 struct inode
*master_inode
= logfs_super(sb
)->s_master_inode
;
1994 loff_t size
= (inode
->i_ino
+ 1) << inode
->i_sb
->s_blocksize_bits
;
1998 BUG_ON(inode
->i_ino
== LOGFS_INO_MASTER
);
1999 /* FIXME: lock inode */
2001 if (i_size_read(master_inode
) < size
)
2002 i_size_write(master_inode
, size
);
2004 /* TODO: Tell vfs this inode is clean now */
2006 page
= inode_to_page(inode
);
2010 /* FIXME: transaction is part of logfs_block now. Is that enough? */
2011 err
= logfs_write_buf(master_inode
, page
, 0);
2012 logfs_put_write_page(page
);
2016 static void logfs_mod_segment_entry(struct super_block
*sb
, u32 segno
,
2018 void (*change_se
)(struct logfs_segment_entry
*, long),
2021 struct logfs_super
*super
= logfs_super(sb
);
2022 struct inode
*inode
;
2024 struct logfs_segment_entry
*se
;
2028 page_no
= segno
>> (sb
->s_blocksize_bits
- 3);
2029 child_no
= segno
& ((sb
->s_blocksize
>> 3) - 1);
2031 inode
= super
->s_segfile_inode
;
2032 page
= logfs_get_write_page(inode
, page_no
, 0);
2033 BUG_ON(!page
); /* FIXME: We need some reserve page for this case */
2034 if (!PageUptodate(page
))
2035 logfs_read_block(inode
, page
, WRITE
);
2038 alloc_indirect_block(inode
, page
, 0);
2039 se
= kmap_atomic(page
, KM_USER0
);
2040 change_se(se
+ child_no
, arg
);
2042 logfs_set_alias(sb
, logfs_block(page
), child_no
);
2043 BUG_ON((int)be32_to_cpu(se
[child_no
].valid
) > super
->s_segsize
);
2045 kunmap_atomic(se
, KM_USER0
);
2047 logfs_put_write_page(page
);
2050 static void __get_segment_entry(struct logfs_segment_entry
*se
, long _target
)
2052 struct logfs_segment_entry
*target
= (void *)_target
;
2057 void logfs_get_segment_entry(struct super_block
*sb
, u32 segno
,
2058 struct logfs_segment_entry
*se
)
2060 logfs_mod_segment_entry(sb
, segno
, 0, __get_segment_entry
, (long)se
);
2063 static void __set_segment_used(struct logfs_segment_entry
*se
, long increment
)
2067 valid
= be32_to_cpu(se
->valid
);
2069 se
->valid
= cpu_to_be32(valid
);
2072 void logfs_set_segment_used(struct super_block
*sb
, u64 ofs
, int increment
)
2074 struct logfs_super
*super
= logfs_super(sb
);
2075 u32 segno
= ofs
>> super
->s_segshift
;
2080 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_used
, increment
);
2083 static void __set_segment_erased(struct logfs_segment_entry
*se
, long ec_level
)
2085 se
->ec_level
= cpu_to_be32(ec_level
);
2088 void logfs_set_segment_erased(struct super_block
*sb
, u32 segno
, u32 ec
,
2089 gc_level_t gc_level
)
2091 u32 ec_level
= ec
<< 4 | (__force u8
)gc_level
;
2093 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_erased
, ec_level
);
2096 static void __set_segment_reserved(struct logfs_segment_entry
*se
, long ignore
)
2098 se
->valid
= cpu_to_be32(RESERVED
);
2101 void logfs_set_segment_reserved(struct super_block
*sb
, u32 segno
)
2103 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_reserved
, 0);
2106 static void __set_segment_unreserved(struct logfs_segment_entry
*se
,
2110 se
->ec_level
= cpu_to_be32(ec_level
);
2113 void logfs_set_segment_unreserved(struct super_block
*sb
, u32 segno
, u32 ec
)
2115 u32 ec_level
= ec
<< 4;
2117 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_unreserved
,
2121 int __logfs_write_inode(struct inode
*inode
, long flags
)
2123 struct super_block
*sb
= inode
->i_sb
;
2126 logfs_get_wblocks(sb
, NULL
, flags
& WF_LOCK
);
2127 ret
= do_write_inode(inode
);
2128 logfs_put_wblocks(sb
, NULL
, flags
& WF_LOCK
);
2132 static int do_delete_inode(struct inode
*inode
)
2134 struct super_block
*sb
= inode
->i_sb
;
2135 struct inode
*master_inode
= logfs_super(sb
)->s_master_inode
;
2139 page
= logfs_get_write_page(master_inode
, inode
->i_ino
, 0);
2143 move_inode_to_page(page
, inode
);
2145 logfs_get_wblocks(sb
, page
, 1);
2146 ret
= __logfs_delete(master_inode
, page
);
2147 logfs_put_wblocks(sb
, page
, 1);
2149 logfs_put_write_page(page
);
2154 * ZOMBIE inodes have already been deleted before and should remain dead,
2155 * if it weren't for valid checking. No need to kill them again here.
2157 void logfs_delete_inode(struct inode
*inode
)
2159 struct logfs_inode
*li
= logfs_inode(inode
);
2161 if (!(li
->li_flags
& LOGFS_IF_ZOMBIE
)) {
2162 li
->li_flags
|= LOGFS_IF_ZOMBIE
;
2163 if (i_size_read(inode
) > 0)
2164 logfs_truncate(inode
, 0);
2165 do_delete_inode(inode
);
2167 truncate_inode_pages(&inode
->i_data
, 0);
2171 void btree_write_block(struct logfs_block
*block
)
2173 struct inode
*inode
;
2177 inode
= logfs_safe_iget(block
->sb
, block
->ino
, &cookie
);
2178 page
= logfs_get_write_page(inode
, block
->bix
, block
->level
);
2180 err
= logfs_readpage_nolock(page
);
2182 BUG_ON(!PagePrivate(page
));
2183 BUG_ON(logfs_block(page
) != block
);
2184 err
= __logfs_write_buf(inode
, page
, 0);
2186 BUG_ON(PagePrivate(page
) || page
->private);
2188 logfs_put_write_page(page
);
2189 logfs_safe_iput(inode
, cookie
);
2193 * logfs_inode_write - write inode or dentry objects
2195 * @inode: parent inode (ifile or directory)
2196 * @buf: object to write (inode or dentry)
2198 * @_pos: object number (file position in blocks/objects)
2199 * @flags: write flags
2200 * @lock: 0 if write lock is already taken, 1 otherwise
2201 * @shadow_tree: shadow below this inode
2203 * FIXME: All caller of this put a 200-300 byte variable on the stack,
2204 * only to call here and do a memcpy from that stack variable. A good
2205 * example of wasted performance and stack space.
2207 int logfs_inode_write(struct inode
*inode
, const void *buf
, size_t count
,
2208 loff_t bix
, long flags
, struct shadow_tree
*shadow_tree
)
2210 loff_t pos
= bix
<< inode
->i_sb
->s_blocksize_bits
;
2215 BUG_ON(pos
& (LOGFS_BLOCKSIZE
-1));
2216 BUG_ON(count
> LOGFS_BLOCKSIZE
);
2217 page
= logfs_get_write_page(inode
, bix
, 0);
2221 pagebuf
= kmap_atomic(page
, KM_USER0
);
2222 memcpy(pagebuf
, buf
, count
);
2223 flush_dcache_page(page
);
2224 kunmap_atomic(pagebuf
, KM_USER0
);
2226 if (i_size_read(inode
) < pos
+ LOGFS_BLOCKSIZE
)
2227 i_size_write(inode
, pos
+ LOGFS_BLOCKSIZE
);
2229 err
= logfs_write_buf(inode
, page
, flags
);
2230 logfs_put_write_page(page
);
2234 int logfs_open_segfile(struct super_block
*sb
)
2236 struct logfs_super
*super
= logfs_super(sb
);
2237 struct inode
*inode
;
2239 inode
= logfs_read_meta_inode(sb
, LOGFS_INO_SEGFILE
);
2241 return PTR_ERR(inode
);
2242 super
->s_segfile_inode
= inode
;
2246 int logfs_init_rw(struct super_block
*sb
)
2248 struct logfs_super
*super
= logfs_super(sb
);
2249 int min_fill
= 3 * super
->s_no_blocks
;
2251 INIT_LIST_HEAD(&super
->s_object_alias
);
2252 mutex_init(&super
->s_write_mutex
);
2253 super
->s_block_pool
= mempool_create_kmalloc_pool(min_fill
,
2254 sizeof(struct logfs_block
));
2255 super
->s_shadow_pool
= mempool_create_kmalloc_pool(min_fill
,
2256 sizeof(struct logfs_shadow
));
2260 void logfs_cleanup_rw(struct super_block
*sb
)
2262 struct logfs_super
*super
= logfs_super(sb
);
2264 destroy_meta_inode(super
->s_segfile_inode
);
2265 logfs_mempool_destroy(super
->s_block_pool
);
2266 logfs_mempool_destroy(super
->s_shadow_pool
);