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>
22 static u64
adjust_bix(u64 bix
, level_t level
)
28 return max_t(u64
, bix
, I0_BLOCKS
);
30 return max_t(u64
, bix
, I1_BLOCKS
);
32 return max_t(u64
, bix
, I2_BLOCKS
);
34 return max_t(u64
, bix
, I3_BLOCKS
);
36 return max_t(u64
, bix
, I4_BLOCKS
);
43 static inline u64
maxbix(u8 height
)
45 return 1ULL << (LOGFS_BLOCK_BITS
* height
);
49 * The inode address space is cut in two halves. Lower half belongs to data
50 * pages, upper half to indirect blocks. If the high bit (INDIRECT_BIT) is
51 * set, the actual block index (bix) and level can be derived from the page
54 * The lowest three bits of the block index are set to 0 after packing and
55 * unpacking. Since the lowest n bits (9 for 4KiB blocksize) are ignored
56 * anyway this is harmless.
58 #define ARCH_SHIFT (BITS_PER_LONG - 32)
59 #define INDIRECT_BIT (0x80000000UL << ARCH_SHIFT)
60 #define LEVEL_SHIFT (28 + ARCH_SHIFT)
61 static inline pgoff_t
first_indirect_block(void)
63 return INDIRECT_BIT
| (1ULL << LEVEL_SHIFT
);
66 pgoff_t
logfs_pack_index(u64 bix
, level_t level
)
70 BUG_ON(bix
>= INDIRECT_BIT
);
75 index
|= (__force
long)level
<< LEVEL_SHIFT
;
76 index
|= bix
>> ((__force u8
)level
* LOGFS_BLOCK_BITS
);
80 void logfs_unpack_index(pgoff_t index
, u64
*bix
, level_t
*level
)
84 if (!(index
& INDIRECT_BIT
)) {
90 __level
= (index
& ~INDIRECT_BIT
) >> LEVEL_SHIFT
;
91 *level
= LEVEL(__level
);
92 *bix
= (index
<< (__level
* LOGFS_BLOCK_BITS
)) & ~INDIRECT_BIT
;
93 *bix
= adjust_bix(*bix
, *level
);
101 * Time is stored as nanoseconds since the epoch.
103 static struct timespec
be64_to_timespec(__be64 betime
)
105 return ns_to_timespec(be64_to_cpu(betime
));
108 static __be64
timespec_to_be64(struct timespec tsp
)
110 return cpu_to_be64((u64
)tsp
.tv_sec
* NSEC_PER_SEC
+ tsp
.tv_nsec
);
113 static void logfs_disk_to_inode(struct logfs_disk_inode
*di
, struct inode
*inode
)
115 struct logfs_inode
*li
= logfs_inode(inode
);
118 inode
->i_mode
= be16_to_cpu(di
->di_mode
);
119 li
->li_height
= di
->di_height
;
120 li
->li_flags
= be32_to_cpu(di
->di_flags
);
121 inode
->i_uid
= be32_to_cpu(di
->di_uid
);
122 inode
->i_gid
= be32_to_cpu(di
->di_gid
);
123 inode
->i_size
= be64_to_cpu(di
->di_size
);
124 logfs_set_blocks(inode
, be64_to_cpu(di
->di_used_bytes
));
125 inode
->i_atime
= be64_to_timespec(di
->di_atime
);
126 inode
->i_ctime
= be64_to_timespec(di
->di_ctime
);
127 inode
->i_mtime
= be64_to_timespec(di
->di_mtime
);
128 inode
->i_nlink
= be32_to_cpu(di
->di_refcount
);
129 inode
->i_generation
= be32_to_cpu(di
->di_generation
);
131 switch (inode
->i_mode
& S_IFMT
) {
132 case S_IFSOCK
: /* fall through */
133 case S_IFBLK
: /* fall through */
134 case S_IFCHR
: /* fall through */
136 inode
->i_rdev
= be64_to_cpu(di
->di_data
[0]);
138 case S_IFDIR
: /* fall through */
139 case S_IFREG
: /* fall through */
141 for (i
= 0; i
< LOGFS_EMBEDDED_FIELDS
; i
++)
142 li
->li_data
[i
] = be64_to_cpu(di
->di_data
[i
]);
149 static void logfs_inode_to_disk(struct inode
*inode
, struct logfs_disk_inode
*di
)
151 struct logfs_inode
*li
= logfs_inode(inode
);
154 di
->di_mode
= cpu_to_be16(inode
->i_mode
);
155 di
->di_height
= li
->li_height
;
157 di
->di_flags
= cpu_to_be32(li
->li_flags
);
158 di
->di_uid
= cpu_to_be32(inode
->i_uid
);
159 di
->di_gid
= cpu_to_be32(inode
->i_gid
);
160 di
->di_size
= cpu_to_be64(i_size_read(inode
));
161 di
->di_used_bytes
= cpu_to_be64(li
->li_used_bytes
);
162 di
->di_atime
= timespec_to_be64(inode
->i_atime
);
163 di
->di_ctime
= timespec_to_be64(inode
->i_ctime
);
164 di
->di_mtime
= timespec_to_be64(inode
->i_mtime
);
165 di
->di_refcount
= cpu_to_be32(inode
->i_nlink
);
166 di
->di_generation
= cpu_to_be32(inode
->i_generation
);
168 switch (inode
->i_mode
& S_IFMT
) {
169 case S_IFSOCK
: /* fall through */
170 case S_IFBLK
: /* fall through */
171 case S_IFCHR
: /* fall through */
173 di
->di_data
[0] = cpu_to_be64(inode
->i_rdev
);
175 case S_IFDIR
: /* fall through */
176 case S_IFREG
: /* fall through */
178 for (i
= 0; i
< LOGFS_EMBEDDED_FIELDS
; i
++)
179 di
->di_data
[i
] = cpu_to_be64(li
->li_data
[i
]);
186 static void __logfs_set_blocks(struct inode
*inode
)
188 struct super_block
*sb
= inode
->i_sb
;
189 struct logfs_inode
*li
= logfs_inode(inode
);
191 inode
->i_blocks
= ULONG_MAX
;
192 if (li
->li_used_bytes
>> sb
->s_blocksize_bits
< ULONG_MAX
)
193 inode
->i_blocks
= ALIGN(li
->li_used_bytes
, 512) >> 9;
196 void logfs_set_blocks(struct inode
*inode
, u64 bytes
)
198 struct logfs_inode
*li
= logfs_inode(inode
);
200 li
->li_used_bytes
= bytes
;
201 __logfs_set_blocks(inode
);
204 static void prelock_page(struct super_block
*sb
, struct page
*page
, int lock
)
206 struct logfs_super
*super
= logfs_super(sb
);
208 BUG_ON(!PageLocked(page
));
210 BUG_ON(PagePreLocked(page
));
211 SetPagePreLocked(page
);
213 /* We are in GC path. */
214 if (PagePreLocked(page
))
215 super
->s_lock_count
++;
217 SetPagePreLocked(page
);
221 static void preunlock_page(struct super_block
*sb
, struct page
*page
, int lock
)
223 struct logfs_super
*super
= logfs_super(sb
);
225 BUG_ON(!PageLocked(page
));
227 ClearPagePreLocked(page
);
229 /* We are in GC path. */
230 BUG_ON(!PagePreLocked(page
));
231 if (super
->s_lock_count
)
232 super
->s_lock_count
--;
234 ClearPagePreLocked(page
);
239 * Logfs is prone to an AB-BA deadlock where one task tries to acquire
240 * s_write_mutex with a locked page and GC tries to get that page while holding
242 * To solve this issue logfs will ignore the page lock iff the page in question
243 * is waiting for s_write_mutex. We annotate this fact by setting PG_pre_locked
244 * in addition to PG_locked.
246 static void logfs_get_wblocks(struct super_block
*sb
, struct page
*page
,
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 static void logfs_put_wblocks(struct super_block
*sb
, struct page
*page
,
265 struct logfs_super
*super
= logfs_super(sb
);
268 preunlock_page(sb
, page
, lock
);
269 /* Order matters - we must clear PG_pre_locked before releasing
270 * s_write_mutex or we could race against another task. */
272 mutex_unlock(&super
->s_write_mutex
);
275 static struct page
*logfs_get_read_page(struct inode
*inode
, u64 bix
,
278 return find_or_create_page(inode
->i_mapping
,
279 logfs_pack_index(bix
, level
), GFP_NOFS
);
282 static void logfs_put_read_page(struct page
*page
)
285 page_cache_release(page
);
288 static void logfs_lock_write_page(struct page
*page
)
292 while (unlikely(!trylock_page(page
))) {
293 if (loop
++ > 0x1000) {
294 /* Has been observed once so far... */
295 printk(KERN_ERR
"stack at %p\n", &loop
);
298 if (PagePreLocked(page
)) {
299 /* Holder of page lock is waiting for us, it
300 * is safe to use this page. */
303 /* Some other process has this page locked and has
304 * nothing to do with us. Wait for it to finish.
308 BUG_ON(!PageLocked(page
));
311 static struct page
*logfs_get_write_page(struct inode
*inode
, u64 bix
,
314 struct address_space
*mapping
= inode
->i_mapping
;
315 pgoff_t index
= logfs_pack_index(bix
, level
);
320 page
= find_get_page(mapping
, index
);
322 page
= __page_cache_alloc(GFP_NOFS
);
325 err
= add_to_page_cache_lru(page
, mapping
, index
, GFP_NOFS
);
327 page_cache_release(page
);
332 } else logfs_lock_write_page(page
);
333 BUG_ON(!PageLocked(page
));
337 static void logfs_unlock_write_page(struct page
*page
)
339 if (!PagePreLocked(page
))
343 static void logfs_put_write_page(struct page
*page
)
345 logfs_unlock_write_page(page
);
346 page_cache_release(page
);
349 static struct page
*logfs_get_page(struct inode
*inode
, u64 bix
, level_t level
,
353 return logfs_get_read_page(inode
, bix
, level
);
355 return logfs_get_write_page(inode
, bix
, level
);
358 static void logfs_put_page(struct page
*page
, int rw
)
361 logfs_put_read_page(page
);
363 logfs_put_write_page(page
);
366 static unsigned long __get_bits(u64 val
, int skip
, int no
)
376 static unsigned long get_bits(u64 val
, level_t skip
)
378 return __get_bits(val
, (__force
int)skip
, LOGFS_BLOCK_BITS
);
381 static inline void init_shadow_tree(struct super_block
*sb
,
382 struct shadow_tree
*tree
)
384 struct logfs_super
*super
= logfs_super(sb
);
386 btree_init_mempool64(&tree
->new, super
->s_btree_pool
);
387 btree_init_mempool64(&tree
->old
, super
->s_btree_pool
);
390 static void indirect_write_block(struct logfs_block
*block
)
397 inode
= page
->mapping
->host
;
398 logfs_lock_write_page(page
);
399 ret
= logfs_write_buf(inode
, page
, 0);
400 logfs_unlock_write_page(page
);
402 * This needs some rework. Unless you want your filesystem to run
403 * completely synchronously (you don't), the filesystem will always
404 * report writes as 'successful' before the actual work has been
405 * done. The actual work gets done here and this is where any errors
406 * will show up. And there isn't much we can do about it, really.
408 * Some attempts to fix the errors (move from bad blocks, retry io,...)
409 * have already been done, so anything left should be either a broken
410 * device or a bug somewhere in logfs itself. Being relatively new,
411 * the odds currently favor a bug, so for now the line below isn't
417 static void inode_write_block(struct logfs_block
*block
)
422 inode
= block
->inode
;
423 if (inode
->i_ino
== LOGFS_INO_MASTER
)
424 logfs_write_anchor(inode
->i_sb
);
426 ret
= __logfs_write_inode(inode
, 0);
427 /* see indirect_write_block comment */
432 static gc_level_t
inode_block_level(struct logfs_block
*block
)
434 BUG_ON(block
->inode
->i_ino
== LOGFS_INO_MASTER
);
435 return GC_LEVEL(LOGFS_MAX_LEVELS
);
438 static gc_level_t
indirect_block_level(struct logfs_block
*block
)
446 inode
= page
->mapping
->host
;
447 logfs_unpack_index(page
->index
, &bix
, &level
);
448 return expand_level(inode
->i_ino
, level
);
452 * This silences a false, yet annoying gcc warning. I hate it when my editor
453 * jumps into bitops.h each time I recompile this file.
454 * TODO: Complain to gcc folks about this and upgrade compiler.
456 static unsigned long fnb(const unsigned long *addr
,
457 unsigned long size
, unsigned long offset
)
459 return find_next_bit(addr
, size
, offset
);
462 static __be64
inode_val0(struct inode
*inode
)
464 struct logfs_inode
*li
= logfs_inode(inode
);
468 * Explicit shifting generates good code, but must match the format
469 * of the structure. Add some paranoia just in case.
471 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_mode
) != 0);
472 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_height
) != 2);
473 BUILD_BUG_ON(offsetof(struct logfs_disk_inode
, di_flags
) != 4);
475 val
= (u64
)inode
->i_mode
<< 48 |
476 (u64
)li
->li_height
<< 40 |
478 return cpu_to_be64(val
);
481 static int inode_write_alias(struct super_block
*sb
,
482 struct logfs_block
*block
, write_alias_t
*write_one_alias
)
484 struct inode
*inode
= block
->inode
;
485 struct logfs_inode
*li
= logfs_inode(inode
);
492 for (pos
= 0; ; pos
++) {
493 pos
= fnb(block
->alias_map
, LOGFS_BLOCK_FACTOR
, pos
);
494 if (pos
>= LOGFS_EMBEDDED_FIELDS
+ INODE_POINTER_OFS
)
498 case INODE_HEIGHT_OFS
:
499 val
= inode_val0(inode
);
502 val
= cpu_to_be64(li
->li_used_bytes
);;
505 val
= cpu_to_be64(i_size_read(inode
));
507 case INODE_POINTER_OFS
... INODE_POINTER_OFS
+ LOGFS_EMBEDDED_FIELDS
- 1:
508 val
= cpu_to_be64(li
->li_data
[pos
- INODE_POINTER_OFS
]);
514 ino
= LOGFS_INO_MASTER
;
517 err
= write_one_alias(sb
, ino
, bix
, level
, pos
, val
);
523 static int indirect_write_alias(struct super_block
*sb
,
524 struct logfs_block
*block
, write_alias_t
*write_one_alias
)
527 struct page
*page
= block
->page
;
533 for (pos
= 0; ; pos
++) {
534 pos
= fnb(block
->alias_map
, LOGFS_BLOCK_FACTOR
, pos
);
535 if (pos
>= LOGFS_BLOCK_FACTOR
)
538 ino
= page
->mapping
->host
->i_ino
;
539 logfs_unpack_index(page
->index
, &bix
, &level
);
540 child
= kmap_atomic(page
, KM_USER0
);
542 kunmap_atomic(child
, KM_USER0
);
543 err
= write_one_alias(sb
, ino
, bix
, level
, pos
, val
);
549 int logfs_write_obj_aliases_pagecache(struct super_block
*sb
)
551 struct logfs_super
*super
= logfs_super(sb
);
552 struct logfs_block
*block
;
555 list_for_each_entry(block
, &super
->s_object_alias
, alias_list
) {
556 err
= block
->ops
->write_alias(sb
, block
, write_alias_journal
);
563 void __free_block(struct super_block
*sb
, struct logfs_block
*block
)
565 BUG_ON(!list_empty(&block
->item_list
));
566 list_del(&block
->alias_list
);
567 mempool_free(block
, logfs_super(sb
)->s_block_pool
);
570 static void inode_free_block(struct super_block
*sb
, struct logfs_block
*block
)
572 struct inode
*inode
= block
->inode
;
574 logfs_inode(inode
)->li_block
= NULL
;
575 __free_block(sb
, block
);
578 static void indirect_free_block(struct super_block
*sb
,
579 struct logfs_block
*block
)
581 ClearPagePrivate(block
->page
);
582 block
->page
->private = 0;
583 __free_block(sb
, block
);
587 static struct logfs_block_ops inode_block_ops
= {
588 .write_block
= inode_write_block
,
589 .block_level
= inode_block_level
,
590 .free_block
= inode_free_block
,
591 .write_alias
= inode_write_alias
,
594 struct logfs_block_ops indirect_block_ops
= {
595 .write_block
= indirect_write_block
,
596 .block_level
= indirect_block_level
,
597 .free_block
= indirect_free_block
,
598 .write_alias
= indirect_write_alias
,
601 struct logfs_block
*__alloc_block(struct super_block
*sb
,
602 u64 ino
, u64 bix
, level_t level
)
604 struct logfs_super
*super
= logfs_super(sb
);
605 struct logfs_block
*block
;
607 block
= mempool_alloc(super
->s_block_pool
, GFP_NOFS
);
608 memset(block
, 0, sizeof(*block
));
609 INIT_LIST_HEAD(&block
->alias_list
);
610 INIT_LIST_HEAD(&block
->item_list
);
614 block
->level
= level
;
618 static void alloc_inode_block(struct inode
*inode
)
620 struct logfs_inode
*li
= logfs_inode(inode
);
621 struct logfs_block
*block
;
626 block
= __alloc_block(inode
->i_sb
, LOGFS_INO_MASTER
, inode
->i_ino
, 0);
627 block
->inode
= inode
;
628 li
->li_block
= block
;
629 block
->ops
= &inode_block_ops
;
632 void initialize_block_counters(struct page
*page
, struct logfs_block
*block
,
633 __be64
*array
, int page_is_empty
)
641 if (page
->index
< first_indirect_block()) {
642 /* Counters are pointless on level 0 */
645 if (page
->index
== first_indirect_block()) {
646 /* Skip unused pointers */
648 block
->full
= I0_BLOCKS
;
650 if (!page_is_empty
) {
651 for (i
= start
; i
< LOGFS_BLOCK_FACTOR
; i
++) {
652 ptr
= be64_to_cpu(array
[i
]);
655 if (ptr
& LOGFS_FULLY_POPULATED
)
661 static void alloc_data_block(struct inode
*inode
, struct page
*page
)
663 struct logfs_block
*block
;
667 if (PagePrivate(page
))
670 logfs_unpack_index(page
->index
, &bix
, &level
);
671 block
= __alloc_block(inode
->i_sb
, inode
->i_ino
, bix
, level
);
673 SetPagePrivate(page
);
674 page
->private = (unsigned long)block
;
675 block
->ops
= &indirect_block_ops
;
678 static void alloc_indirect_block(struct inode
*inode
, struct page
*page
,
681 struct logfs_block
*block
;
684 if (PagePrivate(page
))
687 alloc_data_block(inode
, page
);
689 block
= logfs_block(page
);
690 array
= kmap_atomic(page
, KM_USER0
);
691 initialize_block_counters(page
, block
, array
, page_is_empty
);
692 kunmap_atomic(array
, KM_USER0
);
695 static void block_set_pointer(struct page
*page
, int index
, u64 ptr
)
697 struct logfs_block
*block
= logfs_block(page
);
702 array
= kmap_atomic(page
, KM_USER0
);
703 oldptr
= be64_to_cpu(array
[index
]);
704 array
[index
] = cpu_to_be64(ptr
);
705 kunmap_atomic(array
, KM_USER0
);
706 SetPageUptodate(page
);
708 block
->full
+= !!(ptr
& LOGFS_FULLY_POPULATED
)
709 - !!(oldptr
& LOGFS_FULLY_POPULATED
);
710 block
->partial
+= !!ptr
- !!oldptr
;
713 static u64
block_get_pointer(struct page
*page
, int index
)
718 block
= kmap_atomic(page
, KM_USER0
);
719 ptr
= be64_to_cpu(block
[index
]);
720 kunmap_atomic(block
, KM_USER0
);
724 static int logfs_read_empty(struct page
*page
)
726 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
730 static int logfs_read_direct(struct inode
*inode
, struct page
*page
)
732 struct logfs_inode
*li
= logfs_inode(inode
);
733 pgoff_t index
= page
->index
;
736 block
= li
->li_data
[index
];
738 return logfs_read_empty(page
);
740 return logfs_segment_read(inode
, page
, block
, index
, 0);
743 static int logfs_read_loop(struct inode
*inode
, struct page
*page
,
746 struct logfs_inode
*li
= logfs_inode(inode
);
747 u64 bix
, bofs
= li
->li_data
[INDIRECT_INDEX
];
748 level_t level
, target_level
;
752 logfs_unpack_index(page
->index
, &bix
, &target_level
);
754 return logfs_read_empty(page
);
756 if (bix
>= maxbix(li
->li_height
))
757 return logfs_read_empty(page
);
759 for (level
= LEVEL(li
->li_height
);
760 (__force u8
)level
> (__force u8
)target_level
;
761 level
= SUBLEVEL(level
)){
762 ipage
= logfs_get_page(inode
, bix
, level
, rw_context
);
766 ret
= logfs_segment_read(inode
, ipage
, bofs
, bix
, level
);
768 logfs_put_read_page(ipage
);
772 bofs
= block_get_pointer(ipage
, get_bits(bix
, SUBLEVEL(level
)));
773 logfs_put_page(ipage
, rw_context
);
775 return logfs_read_empty(page
);
778 return logfs_segment_read(inode
, page
, bofs
, bix
, 0);
781 static int logfs_read_block(struct inode
*inode
, struct page
*page
,
784 pgoff_t index
= page
->index
;
786 if (index
< I0_BLOCKS
)
787 return logfs_read_direct(inode
, page
);
788 return logfs_read_loop(inode
, page
, rw_context
);
791 static int logfs_exist_loop(struct inode
*inode
, u64 bix
)
793 struct logfs_inode
*li
= logfs_inode(inode
);
794 u64 bofs
= li
->li_data
[INDIRECT_INDEX
];
801 if (bix
>= maxbix(li
->li_height
))
804 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)) {
805 ipage
= logfs_get_read_page(inode
, bix
, level
);
809 ret
= logfs_segment_read(inode
, ipage
, bofs
, bix
, level
);
811 logfs_put_read_page(ipage
);
815 bofs
= block_get_pointer(ipage
, get_bits(bix
, SUBLEVEL(level
)));
816 logfs_put_read_page(ipage
);
824 int logfs_exist_block(struct inode
*inode
, u64 bix
)
826 struct logfs_inode
*li
= logfs_inode(inode
);
829 return !!li
->li_data
[bix
];
830 return logfs_exist_loop(inode
, bix
);
833 static u64
seek_holedata_direct(struct inode
*inode
, u64 bix
, int data
)
835 struct logfs_inode
*li
= logfs_inode(inode
);
837 for (; bix
< I0_BLOCKS
; bix
++)
838 if (data
^ (li
->li_data
[bix
] == 0))
843 static u64
seek_holedata_loop(struct inode
*inode
, u64 bix
, int data
)
845 struct logfs_inode
*li
= logfs_inode(inode
);
847 u64 increment
, bofs
= li
->li_data
[INDIRECT_INDEX
];
854 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)) {
855 increment
= 1 << (LOGFS_BLOCK_BITS
* ((__force u8
)level
-1));
856 page
= logfs_get_read_page(inode
, bix
, level
);
860 ret
= logfs_segment_read(inode
, page
, bofs
, bix
, level
);
862 logfs_put_read_page(page
);
866 slot
= get_bits(bix
, SUBLEVEL(level
));
867 rblock
= kmap_atomic(page
, KM_USER0
);
868 while (slot
< LOGFS_BLOCK_FACTOR
) {
869 if (data
&& (rblock
[slot
] != 0))
871 if (!data
&& !(be64_to_cpu(rblock
[slot
]) & LOGFS_FULLY_POPULATED
))
875 bix
&= ~(increment
- 1);
877 if (slot
>= LOGFS_BLOCK_FACTOR
) {
878 kunmap_atomic(rblock
, KM_USER0
);
879 logfs_put_read_page(page
);
882 bofs
= be64_to_cpu(rblock
[slot
]);
883 kunmap_atomic(rblock
, KM_USER0
);
884 logfs_put_read_page(page
);
894 * logfs_seek_hole - find next hole starting at a given block index
895 * @inode: inode to search in
896 * @bix: block index to start searching
898 * Returns next hole. If the file doesn't contain any further holes, the
899 * block address next to eof is returned instead.
901 u64
logfs_seek_hole(struct inode
*inode
, u64 bix
)
903 struct logfs_inode
*li
= logfs_inode(inode
);
905 if (bix
< I0_BLOCKS
) {
906 bix
= seek_holedata_direct(inode
, bix
, 0);
911 if (!li
->li_data
[INDIRECT_INDEX
])
913 else if (li
->li_data
[INDIRECT_INDEX
] & LOGFS_FULLY_POPULATED
)
914 bix
= maxbix(li
->li_height
);
916 bix
= seek_holedata_loop(inode
, bix
, 0);
917 if (bix
< maxbix(li
->li_height
))
919 /* Should not happen anymore. But if some port writes semi-
920 * corrupt images (as this one used to) we might run into it.
922 WARN_ON_ONCE(bix
== maxbix(li
->li_height
));
928 static u64
__logfs_seek_data(struct inode
*inode
, u64 bix
)
930 struct logfs_inode
*li
= logfs_inode(inode
);
932 if (bix
< I0_BLOCKS
) {
933 bix
= seek_holedata_direct(inode
, bix
, 1);
938 if (bix
< maxbix(li
->li_height
)) {
939 if (!li
->li_data
[INDIRECT_INDEX
])
940 bix
= maxbix(li
->li_height
);
942 return seek_holedata_loop(inode
, bix
, 1);
949 * logfs_seek_data - find next data block after a given block index
950 * @inode: inode to search in
951 * @bix: block index to start searching
953 * Returns next data block. If the file doesn't contain any further data
954 * blocks, the last block in the file is returned instead.
956 u64
logfs_seek_data(struct inode
*inode
, u64 bix
)
958 struct super_block
*sb
= inode
->i_sb
;
961 ret
= __logfs_seek_data(inode
, bix
);
962 end
= i_size_read(inode
) >> sb
->s_blocksize_bits
;
968 static int logfs_is_valid_direct(struct logfs_inode
*li
, u64 bix
, u64 ofs
)
970 return pure_ofs(li
->li_data
[bix
]) == ofs
;
973 static int __logfs_is_valid_loop(struct inode
*inode
, u64 bix
,
976 struct logfs_inode
*li
= logfs_inode(inode
);
981 for (level
= LEVEL(li
->li_height
); level
!= 0; level
= SUBLEVEL(level
)){
982 page
= logfs_get_write_page(inode
, bix
, level
);
985 ret
= logfs_segment_read(inode
, page
, bofs
, bix
, level
);
987 logfs_put_write_page(page
);
991 bofs
= block_get_pointer(page
, get_bits(bix
, SUBLEVEL(level
)));
992 logfs_put_write_page(page
);
996 if (pure_ofs(bofs
) == ofs
)
1002 static int logfs_is_valid_loop(struct inode
*inode
, u64 bix
, u64 ofs
)
1004 struct logfs_inode
*li
= logfs_inode(inode
);
1005 u64 bofs
= li
->li_data
[INDIRECT_INDEX
];
1010 if (bix
>= maxbix(li
->li_height
))
1013 if (pure_ofs(bofs
) == ofs
)
1016 return __logfs_is_valid_loop(inode
, bix
, ofs
, bofs
);
1019 static int __logfs_is_valid_block(struct inode
*inode
, u64 bix
, u64 ofs
)
1021 struct logfs_inode
*li
= logfs_inode(inode
);
1023 if ((inode
->i_nlink
== 0) && atomic_read(&inode
->i_count
) == 1)
1026 if (bix
< I0_BLOCKS
)
1027 return logfs_is_valid_direct(li
, bix
, ofs
);
1028 return logfs_is_valid_loop(inode
, bix
, ofs
);
1032 * logfs_is_valid_block - check whether this block is still valid
1035 * @ofs - block physical offset
1036 * @ino - block inode number
1037 * @bix - block index
1038 * @level - block level
1040 * Returns 0 if the block is invalid, 1 if it is valid and 2 if it will
1041 * become invalid once the journal is written.
1043 int logfs_is_valid_block(struct super_block
*sb
, u64 ofs
, u64 ino
, u64 bix
,
1044 gc_level_t gc_level
)
1046 struct logfs_super
*super
= logfs_super(sb
);
1047 struct inode
*inode
;
1050 /* Umount closes a segment with free blocks remaining. Those
1051 * blocks are by definition invalid. */
1055 LOGFS_BUG_ON((u64
)(u_long
)ino
!= ino
, sb
);
1057 inode
= logfs_safe_iget(sb
, ino
, &cookie
);
1061 ret
= __logfs_is_valid_block(inode
, bix
, ofs
);
1062 logfs_safe_iput(inode
, cookie
);
1067 /* Block is nominally invalid, but may still sit in the shadow tree,
1068 * waiting for a journal commit.
1070 if (btree_lookup64(&super
->s_shadow_tree
.old
, ofs
))
1075 int logfs_readpage_nolock(struct page
*page
)
1077 struct inode
*inode
= page
->mapping
->host
;
1080 ret
= logfs_read_block(inode
, page
, READ
);
1083 ClearPageUptodate(page
);
1086 SetPageUptodate(page
);
1087 ClearPageError(page
);
1089 flush_dcache_page(page
);
1094 static int logfs_reserve_bytes(struct inode
*inode
, int bytes
)
1096 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1097 u64 available
= super
->s_free_bytes
+ super
->s_dirty_free_bytes
1098 - super
->s_dirty_used_bytes
- super
->s_dirty_pages
;
1103 if (available
< bytes
)
1106 if (available
< bytes
+ super
->s_root_reserve
&&
1107 !capable(CAP_SYS_RESOURCE
))
1113 int get_page_reserve(struct inode
*inode
, struct page
*page
)
1115 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1118 if (logfs_block(page
) && logfs_block(page
)->reserved_bytes
)
1121 logfs_get_wblocks(inode
->i_sb
, page
, WF_LOCK
);
1122 ret
= logfs_reserve_bytes(inode
, 6 * LOGFS_MAX_OBJECTSIZE
);
1124 alloc_data_block(inode
, page
);
1125 logfs_block(page
)->reserved_bytes
+= 6 * LOGFS_MAX_OBJECTSIZE
;
1126 super
->s_dirty_pages
+= 6 * LOGFS_MAX_OBJECTSIZE
;
1128 logfs_put_wblocks(inode
->i_sb
, page
, WF_LOCK
);
1133 * We are protected by write lock. Push victims up to superblock level
1134 * and release transaction when appropriate.
1136 /* FIXME: This is currently called from the wrong spots. */
1137 static void logfs_handle_transaction(struct inode
*inode
,
1138 struct logfs_transaction
*ta
)
1140 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1144 logfs_inode(inode
)->li_block
->ta
= NULL
;
1146 if (inode
->i_ino
!= LOGFS_INO_MASTER
) {
1147 BUG(); /* FIXME: Yes, this needs more thought */
1148 /* just remember the transaction until inode is written */
1149 //BUG_ON(logfs_inode(inode)->li_transaction);
1150 //logfs_inode(inode)->li_transaction = ta;
1154 switch (ta
->state
) {
1155 case CREATE_1
: /* fall through */
1157 BUG_ON(super
->s_victim_ino
);
1158 super
->s_victim_ino
= ta
->ino
;
1160 case CREATE_2
: /* fall through */
1162 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1163 super
->s_victim_ino
= 0;
1164 /* transaction ends here - free it */
1167 case CROSS_RENAME_1
:
1168 BUG_ON(super
->s_rename_dir
);
1169 BUG_ON(super
->s_rename_pos
);
1170 super
->s_rename_dir
= ta
->dir
;
1171 super
->s_rename_pos
= ta
->pos
;
1173 case CROSS_RENAME_2
:
1174 BUG_ON(super
->s_rename_dir
!= ta
->dir
);
1175 BUG_ON(super
->s_rename_pos
!= ta
->pos
);
1176 super
->s_rename_dir
= 0;
1177 super
->s_rename_pos
= 0;
1180 case TARGET_RENAME_1
:
1181 BUG_ON(super
->s_rename_dir
);
1182 BUG_ON(super
->s_rename_pos
);
1183 BUG_ON(super
->s_victim_ino
);
1184 super
->s_rename_dir
= ta
->dir
;
1185 super
->s_rename_pos
= ta
->pos
;
1186 super
->s_victim_ino
= ta
->ino
;
1188 case TARGET_RENAME_2
:
1189 BUG_ON(super
->s_rename_dir
!= ta
->dir
);
1190 BUG_ON(super
->s_rename_pos
!= ta
->pos
);
1191 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1192 super
->s_rename_dir
= 0;
1193 super
->s_rename_pos
= 0;
1195 case TARGET_RENAME_3
:
1196 BUG_ON(super
->s_rename_dir
);
1197 BUG_ON(super
->s_rename_pos
);
1198 BUG_ON(super
->s_victim_ino
!= ta
->ino
);
1199 super
->s_victim_ino
= 0;
1208 * Not strictly a reservation, but rather a check that we still have enough
1209 * space to satisfy the write.
1211 static int logfs_reserve_blocks(struct inode
*inode
, int blocks
)
1213 return logfs_reserve_bytes(inode
, blocks
* LOGFS_MAX_OBJECTSIZE
);
1216 struct write_control
{
1221 static struct logfs_shadow
*alloc_shadow(struct inode
*inode
, u64 bix
,
1222 level_t level
, u64 old_ofs
)
1224 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1225 struct logfs_shadow
*shadow
;
1227 shadow
= mempool_alloc(super
->s_shadow_pool
, GFP_NOFS
);
1228 memset(shadow
, 0, sizeof(*shadow
));
1229 shadow
->ino
= inode
->i_ino
;
1231 shadow
->gc_level
= expand_level(inode
->i_ino
, level
);
1232 shadow
->old_ofs
= old_ofs
& ~LOGFS_FULLY_POPULATED
;
1236 static void free_shadow(struct inode
*inode
, struct logfs_shadow
*shadow
)
1238 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1240 mempool_free(shadow
, super
->s_shadow_pool
);
1244 * fill_shadow_tree - Propagate shadow tree changes due to a write
1245 * @inode: Inode owning the page
1246 * @page: Struct page that was written
1247 * @shadow: Shadow for the current write
1249 * Writes in logfs can result in two semi-valid objects. The old object
1250 * is still valid as long as it can be reached by following pointers on
1251 * the medium. Only when writes propagate all the way up to the journal
1252 * has the new object safely replaced the old one.
1254 * To handle this problem, a struct logfs_shadow is used to represent
1255 * every single write. It is attached to the indirect block, which is
1256 * marked dirty. When the indirect block is written, its shadows are
1257 * handed up to the next indirect block (or inode). Untimately they
1258 * will reach the master inode and be freed upon journal commit.
1260 * This function handles a single step in the propagation. It adds the
1261 * shadow for the current write to the tree, along with any shadows in
1262 * the page's tree, in case it was an indirect block. If a page is
1263 * written, the inode parameter is left NULL, if an inode is written,
1264 * the page parameter is left NULL.
1266 static void fill_shadow_tree(struct inode
*inode
, struct page
*page
,
1267 struct logfs_shadow
*shadow
)
1269 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1270 struct logfs_block
*block
= logfs_block(page
);
1271 struct shadow_tree
*tree
= &super
->s_shadow_tree
;
1273 if (PagePrivate(page
)) {
1274 if (block
->alias_map
)
1275 super
->s_no_object_aliases
-= bitmap_weight(
1276 block
->alias_map
, LOGFS_BLOCK_FACTOR
);
1277 logfs_handle_transaction(inode
, block
->ta
);
1278 block
->ops
->free_block(inode
->i_sb
, block
);
1281 if (shadow
->old_ofs
)
1282 btree_insert64(&tree
->old
, shadow
->old_ofs
, shadow
,
1285 btree_insert64(&tree
->new, shadow
->new_ofs
, shadow
,
1288 super
->s_dirty_used_bytes
+= shadow
->new_len
;
1289 super
->s_dirty_free_bytes
+= shadow
->old_len
;
1293 static void logfs_set_alias(struct super_block
*sb
, struct logfs_block
*block
,
1296 struct logfs_super
*super
= logfs_super(sb
);
1298 if (block
->inode
&& block
->inode
->i_ino
== LOGFS_INO_MASTER
) {
1299 /* Aliases in the master inode are pointless. */
1303 if (!test_bit(child_no
, block
->alias_map
)) {
1304 set_bit(child_no
, block
->alias_map
);
1305 super
->s_no_object_aliases
++;
1307 list_move_tail(&block
->alias_list
, &super
->s_object_alias
);
1311 * Object aliases can and often do change the size and occupied space of a
1312 * file. So not only do we have to change the pointers, we also have to
1313 * change inode->i_size and li->li_used_bytes. Which is done by setting
1314 * another two object aliases for the inode itself.
1316 static void set_iused(struct inode
*inode
, struct logfs_shadow
*shadow
)
1318 struct logfs_inode
*li
= logfs_inode(inode
);
1320 if (shadow
->new_len
== shadow
->old_len
)
1323 alloc_inode_block(inode
);
1324 li
->li_used_bytes
+= shadow
->new_len
- shadow
->old_len
;
1325 __logfs_set_blocks(inode
);
1326 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_USED_OFS
);
1327 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_SIZE_OFS
);
1330 static int logfs_write_i0(struct inode
*inode
, struct page
*page
,
1331 struct write_control
*wc
)
1333 struct logfs_shadow
*shadow
;
1338 logfs_unpack_index(page
->index
, &bix
, &level
);
1340 if (logfs_reserve_blocks(inode
, 1))
1343 shadow
= alloc_shadow(inode
, bix
, level
, wc
->ofs
);
1344 if (wc
->flags
& WF_WRITE
)
1345 err
= logfs_segment_write(inode
, page
, shadow
);
1346 if (wc
->flags
& WF_DELETE
)
1347 logfs_segment_delete(inode
, shadow
);
1349 free_shadow(inode
, shadow
);
1353 set_iused(inode
, shadow
);
1356 alloc_indirect_block(inode
, page
, 0);
1357 full
= logfs_block(page
)->full
== LOGFS_BLOCK_FACTOR
;
1359 fill_shadow_tree(inode
, page
, shadow
);
1360 wc
->ofs
= shadow
->new_ofs
;
1361 if (wc
->ofs
&& full
)
1362 wc
->ofs
|= LOGFS_FULLY_POPULATED
;
1366 static int logfs_write_direct(struct inode
*inode
, struct page
*page
,
1369 struct logfs_inode
*li
= logfs_inode(inode
);
1370 struct write_control wc
= {
1371 .ofs
= li
->li_data
[page
->index
],
1376 alloc_inode_block(inode
);
1378 err
= logfs_write_i0(inode
, page
, &wc
);
1382 li
->li_data
[page
->index
] = wc
.ofs
;
1383 logfs_set_alias(inode
->i_sb
, li
->li_block
,
1384 page
->index
+ INODE_POINTER_OFS
);
1388 static int ptr_change(u64 ofs
, struct page
*page
)
1390 struct logfs_block
*block
= logfs_block(page
);
1391 int empty0
, empty1
, full0
, full1
;
1394 empty1
= block
->partial
== 0;
1395 if (empty0
!= empty1
)
1398 /* The !! is necessary to shrink result to int */
1399 full0
= !!(ofs
& LOGFS_FULLY_POPULATED
);
1400 full1
= block
->full
== LOGFS_BLOCK_FACTOR
;
1406 static int __logfs_write_rec(struct inode
*inode
, struct page
*page
,
1407 struct write_control
*this_wc
,
1408 pgoff_t bix
, level_t target_level
, level_t level
)
1410 int ret
, page_empty
= 0;
1411 int child_no
= get_bits(bix
, SUBLEVEL(level
));
1413 struct write_control child_wc
= {
1414 .flags
= this_wc
->flags
,
1417 ipage
= logfs_get_write_page(inode
, bix
, level
);
1422 ret
= logfs_segment_read(inode
, ipage
, this_wc
->ofs
, bix
, level
);
1425 } else if (!PageUptodate(ipage
)) {
1427 logfs_read_empty(ipage
);
1430 child_wc
.ofs
= block_get_pointer(ipage
, child_no
);
1432 if ((__force u8
)level
-1 > (__force u8
)target_level
)
1433 ret
= __logfs_write_rec(inode
, page
, &child_wc
, bix
,
1434 target_level
, SUBLEVEL(level
));
1436 ret
= logfs_write_i0(inode
, page
, &child_wc
);
1441 alloc_indirect_block(inode
, ipage
, page_empty
);
1442 block_set_pointer(ipage
, child_no
, child_wc
.ofs
);
1443 /* FIXME: first condition seems superfluous */
1444 if (child_wc
.ofs
|| logfs_block(ipage
)->partial
)
1445 this_wc
->flags
|= WF_WRITE
;
1446 /* the condition on this_wc->ofs ensures that we won't consume extra
1447 * space for indirect blocks in the future, which we cannot reserve */
1448 if (!this_wc
->ofs
|| ptr_change(this_wc
->ofs
, ipage
))
1449 ret
= logfs_write_i0(inode
, ipage
, this_wc
);
1451 logfs_set_alias(inode
->i_sb
, logfs_block(ipage
), child_no
);
1453 logfs_put_write_page(ipage
);
1457 static int logfs_write_rec(struct inode
*inode
, struct page
*page
,
1458 pgoff_t bix
, level_t target_level
, long flags
)
1460 struct logfs_inode
*li
= logfs_inode(inode
);
1461 struct write_control wc
= {
1462 .ofs
= li
->li_data
[INDIRECT_INDEX
],
1467 alloc_inode_block(inode
);
1469 if (li
->li_height
> (__force u8
)target_level
)
1470 ret
= __logfs_write_rec(inode
, page
, &wc
, bix
, target_level
,
1471 LEVEL(li
->li_height
));
1473 ret
= logfs_write_i0(inode
, page
, &wc
);
1477 if (li
->li_data
[INDIRECT_INDEX
] != wc
.ofs
) {
1478 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1479 logfs_set_alias(inode
->i_sb
, li
->li_block
,
1480 INDIRECT_INDEX
+ INODE_POINTER_OFS
);
1485 void logfs_add_transaction(struct inode
*inode
, struct logfs_transaction
*ta
)
1487 alloc_inode_block(inode
);
1488 logfs_inode(inode
)->li_block
->ta
= ta
;
1491 void logfs_del_transaction(struct inode
*inode
, struct logfs_transaction
*ta
)
1493 struct logfs_block
*block
= logfs_inode(inode
)->li_block
;
1495 if (block
&& block
->ta
)
1499 static int grow_inode(struct inode
*inode
, u64 bix
, level_t level
)
1501 struct logfs_inode
*li
= logfs_inode(inode
);
1502 u8 height
= (__force u8
)level
;
1504 struct write_control wc
= {
1509 BUG_ON(height
> 5 || li
->li_height
> 5);
1510 while (height
> li
->li_height
|| bix
>= maxbix(li
->li_height
)) {
1511 page
= logfs_get_write_page(inode
, I0_BLOCKS
+ 1,
1512 LEVEL(li
->li_height
+ 1));
1515 logfs_read_empty(page
);
1516 alloc_indirect_block(inode
, page
, 1);
1517 block_set_pointer(page
, 0, li
->li_data
[INDIRECT_INDEX
]);
1518 err
= logfs_write_i0(inode
, page
, &wc
);
1519 logfs_put_write_page(page
);
1522 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1525 logfs_set_alias(inode
->i_sb
, li
->li_block
, INODE_HEIGHT_OFS
);
1530 static int __logfs_write_buf(struct inode
*inode
, struct page
*page
, long flags
)
1532 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
1533 pgoff_t index
= page
->index
;
1538 flags
|= WF_WRITE
| WF_DELETE
;
1539 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
1541 logfs_unpack_index(index
, &bix
, &level
);
1542 if (logfs_block(page
) && logfs_block(page
)->reserved_bytes
)
1543 super
->s_dirty_pages
-= logfs_block(page
)->reserved_bytes
;
1545 if (index
< I0_BLOCKS
)
1546 return logfs_write_direct(inode
, page
, flags
);
1548 bix
= adjust_bix(bix
, level
);
1549 err
= grow_inode(inode
, bix
, level
);
1552 return logfs_write_rec(inode
, page
, bix
, level
, flags
);
1555 int logfs_write_buf(struct inode
*inode
, struct page
*page
, long flags
)
1557 struct super_block
*sb
= inode
->i_sb
;
1560 logfs_get_wblocks(sb
, page
, flags
& WF_LOCK
);
1561 ret
= __logfs_write_buf(inode
, page
, flags
);
1562 logfs_put_wblocks(sb
, page
, flags
& WF_LOCK
);
1566 static int __logfs_delete(struct inode
*inode
, struct page
*page
)
1568 long flags
= WF_DELETE
;
1570 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
1572 if (page
->index
< I0_BLOCKS
)
1573 return logfs_write_direct(inode
, page
, flags
);
1574 return logfs_write_rec(inode
, page
, page
->index
, 0, flags
);
1577 int logfs_delete(struct inode
*inode
, pgoff_t index
,
1578 struct shadow_tree
*shadow_tree
)
1580 struct super_block
*sb
= inode
->i_sb
;
1584 page
= logfs_get_read_page(inode
, index
, 0);
1588 logfs_get_wblocks(sb
, page
, 1);
1589 ret
= __logfs_delete(inode
, page
);
1590 logfs_put_wblocks(sb
, page
, 1);
1592 logfs_put_read_page(page
);
1597 /* Rewrite cannot mark the inode dirty but has to write it immediatly. */
1598 int logfs_rewrite_block(struct inode
*inode
, u64 bix
, u64 ofs
,
1599 gc_level_t gc_level
, long flags
)
1601 level_t level
= shrink_level(gc_level
);
1605 page
= logfs_get_write_page(inode
, bix
, level
);
1609 err
= logfs_segment_read(inode
, page
, ofs
, bix
, level
);
1612 alloc_indirect_block(inode
, page
, 0);
1613 err
= logfs_write_buf(inode
, page
, flags
);
1615 logfs_put_write_page(page
);
1619 static int truncate_data_block(struct inode
*inode
, struct page
*page
,
1620 u64 ofs
, struct logfs_shadow
*shadow
, u64 size
)
1622 loff_t pageofs
= page
->index
<< inode
->i_sb
->s_blocksize_bits
;
1627 /* Does truncation happen within this page? */
1628 if (size
<= pageofs
|| size
- pageofs
>= PAGE_SIZE
)
1631 logfs_unpack_index(page
->index
, &bix
, &level
);
1634 err
= logfs_segment_read(inode
, page
, ofs
, bix
, level
);
1638 zero_user_segment(page
, size
- pageofs
, PAGE_CACHE_SIZE
);
1639 return logfs_segment_write(inode
, page
, shadow
);
1642 static int logfs_truncate_i0(struct inode
*inode
, struct page
*page
,
1643 struct write_control
*wc
, u64 size
)
1645 struct logfs_shadow
*shadow
;
1650 logfs_unpack_index(page
->index
, &bix
, &level
);
1652 shadow
= alloc_shadow(inode
, bix
, level
, wc
->ofs
);
1654 err
= truncate_data_block(inode
, page
, wc
->ofs
, shadow
, size
);
1656 free_shadow(inode
, shadow
);
1660 logfs_segment_delete(inode
, shadow
);
1661 set_iused(inode
, shadow
);
1662 fill_shadow_tree(inode
, page
, shadow
);
1663 wc
->ofs
= shadow
->new_ofs
;
1667 static int logfs_truncate_direct(struct inode
*inode
, u64 size
)
1669 struct logfs_inode
*li
= logfs_inode(inode
);
1670 struct write_control wc
;
1675 alloc_inode_block(inode
);
1677 for (e
= I0_BLOCKS
- 1; e
>= 0; e
--) {
1678 if (size
> (e
+1) * LOGFS_BLOCKSIZE
)
1681 wc
.ofs
= li
->li_data
[e
];
1685 page
= logfs_get_write_page(inode
, e
, 0);
1688 err
= logfs_segment_read(inode
, page
, wc
.ofs
, e
, 0);
1690 logfs_put_write_page(page
);
1693 err
= logfs_truncate_i0(inode
, page
, &wc
, size
);
1694 logfs_put_write_page(page
);
1698 li
->li_data
[e
] = wc
.ofs
;
1703 /* FIXME: these need to become per-sb once we support different blocksizes */
1704 static u64 __logfs_step
[] = {
1711 static u64 __logfs_start_index
[] = {
1718 static inline u64
logfs_step(level_t level
)
1720 return __logfs_step
[(__force u8
)level
];
1723 static inline u64
logfs_factor(u8 level
)
1725 return __logfs_step
[level
] * LOGFS_BLOCKSIZE
;
1728 static inline u64
logfs_start_index(level_t level
)
1730 return __logfs_start_index
[(__force u8
)level
];
1733 static void logfs_unpack_raw_index(pgoff_t index
, u64
*bix
, level_t
*level
)
1735 logfs_unpack_index(index
, bix
, level
);
1736 if (*bix
<= logfs_start_index(SUBLEVEL(*level
)))
1740 static int __logfs_truncate_rec(struct inode
*inode
, struct page
*ipage
,
1741 struct write_control
*this_wc
, u64 size
)
1743 int truncate_happened
= 0;
1745 u64 bix
, child_bix
, next_bix
;
1748 struct write_control child_wc
= { /* FIXME: flags */ };
1750 logfs_unpack_raw_index(ipage
->index
, &bix
, &level
);
1751 err
= logfs_segment_read(inode
, ipage
, this_wc
->ofs
, bix
, level
);
1755 for (e
= LOGFS_BLOCK_FACTOR
- 1; e
>= 0; e
--) {
1756 child_bix
= bix
+ e
* logfs_step(SUBLEVEL(level
));
1757 next_bix
= child_bix
+ logfs_step(SUBLEVEL(level
));
1758 if (size
> next_bix
* LOGFS_BLOCKSIZE
)
1761 child_wc
.ofs
= pure_ofs(block_get_pointer(ipage
, e
));
1765 page
= logfs_get_write_page(inode
, child_bix
, SUBLEVEL(level
));
1769 if ((__force u8
)level
> 1)
1770 err
= __logfs_truncate_rec(inode
, page
, &child_wc
, size
);
1772 err
= logfs_truncate_i0(inode
, page
, &child_wc
, size
);
1773 logfs_put_write_page(page
);
1777 truncate_happened
= 1;
1778 alloc_indirect_block(inode
, ipage
, 0);
1779 block_set_pointer(ipage
, e
, child_wc
.ofs
);
1782 if (!truncate_happened
) {
1783 printk("ineffectual truncate (%lx, %lx, %llx)\n", inode
->i_ino
, ipage
->index
, size
);
1787 this_wc
->flags
= WF_DELETE
;
1788 if (logfs_block(ipage
)->partial
)
1789 this_wc
->flags
|= WF_WRITE
;
1791 return logfs_write_i0(inode
, ipage
, this_wc
);
1794 static int logfs_truncate_rec(struct inode
*inode
, u64 size
)
1796 struct logfs_inode
*li
= logfs_inode(inode
);
1797 struct write_control wc
= {
1798 .ofs
= li
->li_data
[INDIRECT_INDEX
],
1803 alloc_inode_block(inode
);
1808 page
= logfs_get_write_page(inode
, 0, LEVEL(li
->li_height
));
1812 err
= __logfs_truncate_rec(inode
, page
, &wc
, size
);
1813 logfs_put_write_page(page
);
1817 if (li
->li_data
[INDIRECT_INDEX
] != wc
.ofs
)
1818 li
->li_data
[INDIRECT_INDEX
] = wc
.ofs
;
1822 static int __logfs_truncate(struct inode
*inode
, u64 size
)
1826 if (size
>= logfs_factor(logfs_inode(inode
)->li_height
))
1829 ret
= logfs_truncate_rec(inode
, size
);
1833 return logfs_truncate_direct(inode
, size
);
1836 int logfs_truncate(struct inode
*inode
, u64 size
)
1838 struct super_block
*sb
= inode
->i_sb
;
1841 logfs_get_wblocks(sb
, NULL
, 1);
1842 err
= __logfs_truncate(inode
, size
);
1844 err
= __logfs_write_inode(inode
, 0);
1845 logfs_put_wblocks(sb
, NULL
, 1);
1848 err
= vmtruncate(inode
, size
);
1850 /* I don't trust error recovery yet. */
1855 static void move_page_to_inode(struct inode
*inode
, struct page
*page
)
1857 struct logfs_inode
*li
= logfs_inode(inode
);
1858 struct logfs_block
*block
= logfs_block(page
);
1863 log_blockmove("move_page_to_inode(%llx, %llx, %x)\n",
1864 block
->ino
, block
->bix
, block
->level
);
1865 BUG_ON(li
->li_block
);
1866 block
->ops
= &inode_block_ops
;
1867 block
->inode
= inode
;
1868 li
->li_block
= block
;
1872 ClearPagePrivate(page
);
1875 static void move_inode_to_page(struct page
*page
, struct inode
*inode
)
1877 struct logfs_inode
*li
= logfs_inode(inode
);
1878 struct logfs_block
*block
= li
->li_block
;
1883 log_blockmove("move_inode_to_page(%llx, %llx, %x)\n",
1884 block
->ino
, block
->bix
, block
->level
);
1885 BUG_ON(PagePrivate(page
));
1886 block
->ops
= &indirect_block_ops
;
1888 page
->private = (unsigned long)block
;
1889 SetPagePrivate(page
);
1891 block
->inode
= NULL
;
1892 li
->li_block
= NULL
;
1895 int logfs_read_inode(struct inode
*inode
)
1897 struct super_block
*sb
= inode
->i_sb
;
1898 struct logfs_super
*super
= logfs_super(sb
);
1899 struct inode
*master_inode
= super
->s_master_inode
;
1901 struct logfs_disk_inode
*di
;
1902 u64 ino
= inode
->i_ino
;
1904 if (ino
<< sb
->s_blocksize_bits
> i_size_read(master_inode
))
1906 if (!logfs_exist_block(master_inode
, ino
))
1909 page
= read_cache_page(master_inode
->i_mapping
, ino
,
1910 (filler_t
*)logfs_readpage
, NULL
);
1912 return PTR_ERR(page
);
1914 di
= kmap_atomic(page
, KM_USER0
);
1915 logfs_disk_to_inode(di
, inode
);
1916 kunmap_atomic(di
, KM_USER0
);
1917 move_page_to_inode(inode
, page
);
1918 page_cache_release(page
);
1922 /* Caller must logfs_put_write_page(page); */
1923 static struct page
*inode_to_page(struct inode
*inode
)
1925 struct inode
*master_inode
= logfs_super(inode
->i_sb
)->s_master_inode
;
1926 struct logfs_disk_inode
*di
;
1929 BUG_ON(inode
->i_ino
== LOGFS_INO_MASTER
);
1931 page
= logfs_get_write_page(master_inode
, inode
->i_ino
, 0);
1935 di
= kmap_atomic(page
, KM_USER0
);
1936 logfs_inode_to_disk(inode
, di
);
1937 kunmap_atomic(di
, KM_USER0
);
1938 move_inode_to_page(page
, inode
);
1942 /* Cheaper version of write_inode. All changes are concealed in
1943 * aliases, which are moved back. No write to the medium happens.
1945 void logfs_clear_inode(struct inode
*inode
)
1947 struct super_block
*sb
= inode
->i_sb
;
1948 struct logfs_inode
*li
= logfs_inode(inode
);
1949 struct logfs_block
*block
= li
->li_block
;
1952 /* Only deleted files may be dirty at this point */
1953 BUG_ON(inode
->i_state
& I_DIRTY
&& inode
->i_nlink
);
1956 if ((logfs_super(sb
)->s_flags
& LOGFS_SB_FLAG_SHUTDOWN
)) {
1957 block
->ops
->free_block(inode
->i_sb
, block
);
1961 BUG_ON(inode
->i_ino
< LOGFS_RESERVED_INOS
);
1962 page
= inode_to_page(inode
);
1963 BUG_ON(!page
); /* FIXME: Use emergency page */
1964 logfs_put_write_page(page
);
1967 static int do_write_inode(struct inode
*inode
)
1969 struct super_block
*sb
= inode
->i_sb
;
1970 struct inode
*master_inode
= logfs_super(sb
)->s_master_inode
;
1971 loff_t size
= (inode
->i_ino
+ 1) << inode
->i_sb
->s_blocksize_bits
;
1975 BUG_ON(inode
->i_ino
== LOGFS_INO_MASTER
);
1976 /* FIXME: lock inode */
1978 if (i_size_read(master_inode
) < size
)
1979 i_size_write(master_inode
, size
);
1981 /* TODO: Tell vfs this inode is clean now */
1983 page
= inode_to_page(inode
);
1987 /* FIXME: transaction is part of logfs_block now. Is that enough? */
1988 err
= logfs_write_buf(master_inode
, page
, 0);
1989 logfs_put_write_page(page
);
1993 static void logfs_mod_segment_entry(struct super_block
*sb
, u32 segno
,
1995 void (*change_se
)(struct logfs_segment_entry
*, long),
1998 struct logfs_super
*super
= logfs_super(sb
);
1999 struct inode
*inode
;
2001 struct logfs_segment_entry
*se
;
2005 page_no
= segno
>> (sb
->s_blocksize_bits
- 3);
2006 child_no
= segno
& ((sb
->s_blocksize
>> 3) - 1);
2008 inode
= super
->s_segfile_inode
;
2009 page
= logfs_get_write_page(inode
, page_no
, 0);
2010 BUG_ON(!page
); /* FIXME: We need some reserve page for this case */
2011 if (!PageUptodate(page
))
2012 logfs_read_block(inode
, page
, WRITE
);
2015 alloc_indirect_block(inode
, page
, 0);
2016 se
= kmap_atomic(page
, KM_USER0
);
2017 change_se(se
+ child_no
, arg
);
2019 logfs_set_alias(sb
, logfs_block(page
), child_no
);
2020 BUG_ON((int)be32_to_cpu(se
[child_no
].valid
) > super
->s_segsize
);
2022 kunmap_atomic(se
, KM_USER0
);
2024 logfs_put_write_page(page
);
2027 static void __get_segment_entry(struct logfs_segment_entry
*se
, long _target
)
2029 struct logfs_segment_entry
*target
= (void *)_target
;
2034 void logfs_get_segment_entry(struct super_block
*sb
, u32 segno
,
2035 struct logfs_segment_entry
*se
)
2037 logfs_mod_segment_entry(sb
, segno
, 0, __get_segment_entry
, (long)se
);
2040 static void __set_segment_used(struct logfs_segment_entry
*se
, long increment
)
2044 valid
= be32_to_cpu(se
->valid
);
2046 se
->valid
= cpu_to_be32(valid
);
2049 void logfs_set_segment_used(struct super_block
*sb
, u64 ofs
, int increment
)
2051 struct logfs_super
*super
= logfs_super(sb
);
2052 u32 segno
= ofs
>> super
->s_segshift
;
2057 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_used
, increment
);
2060 static void __set_segment_erased(struct logfs_segment_entry
*se
, long ec_level
)
2062 se
->ec_level
= cpu_to_be32(ec_level
);
2065 void logfs_set_segment_erased(struct super_block
*sb
, u32 segno
, u32 ec
,
2066 gc_level_t gc_level
)
2068 u32 ec_level
= ec
<< 4 | (__force u8
)gc_level
;
2070 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_erased
, ec_level
);
2073 static void __set_segment_reserved(struct logfs_segment_entry
*se
, long ignore
)
2075 se
->valid
= cpu_to_be32(RESERVED
);
2078 void logfs_set_segment_reserved(struct super_block
*sb
, u32 segno
)
2080 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_reserved
, 0);
2083 static void __set_segment_unreserved(struct logfs_segment_entry
*se
,
2087 se
->ec_level
= cpu_to_be32(ec_level
);
2090 void logfs_set_segment_unreserved(struct super_block
*sb
, u32 segno
, u32 ec
)
2092 u32 ec_level
= ec
<< 4;
2094 logfs_mod_segment_entry(sb
, segno
, 1, __set_segment_unreserved
,
2098 int __logfs_write_inode(struct inode
*inode
, long flags
)
2100 struct super_block
*sb
= inode
->i_sb
;
2103 logfs_get_wblocks(sb
, NULL
, flags
& WF_LOCK
);
2104 ret
= do_write_inode(inode
);
2105 logfs_put_wblocks(sb
, NULL
, flags
& WF_LOCK
);
2109 static int do_delete_inode(struct inode
*inode
)
2111 struct super_block
*sb
= inode
->i_sb
;
2112 struct inode
*master_inode
= logfs_super(sb
)->s_master_inode
;
2116 page
= logfs_get_write_page(master_inode
, inode
->i_ino
, 0);
2120 move_inode_to_page(page
, inode
);
2122 logfs_get_wblocks(sb
, page
, 1);
2123 ret
= __logfs_delete(master_inode
, page
);
2124 logfs_put_wblocks(sb
, page
, 1);
2126 logfs_put_write_page(page
);
2131 * ZOMBIE inodes have already been deleted before and should remain dead,
2132 * if it weren't for valid checking. No need to kill them again here.
2134 void logfs_delete_inode(struct inode
*inode
)
2136 struct logfs_inode
*li
= logfs_inode(inode
);
2138 if (!(li
->li_flags
& LOGFS_IF_ZOMBIE
)) {
2139 li
->li_flags
|= LOGFS_IF_ZOMBIE
;
2140 if (i_size_read(inode
) > 0)
2141 logfs_truncate(inode
, 0);
2142 do_delete_inode(inode
);
2144 truncate_inode_pages(&inode
->i_data
, 0);
2148 void btree_write_block(struct logfs_block
*block
)
2150 struct inode
*inode
;
2154 inode
= logfs_safe_iget(block
->sb
, block
->ino
, &cookie
);
2155 page
= logfs_get_write_page(inode
, block
->bix
, block
->level
);
2157 err
= logfs_readpage_nolock(page
);
2159 BUG_ON(!PagePrivate(page
));
2160 BUG_ON(logfs_block(page
) != block
);
2161 err
= __logfs_write_buf(inode
, page
, 0);
2163 BUG_ON(PagePrivate(page
) || page
->private);
2165 logfs_put_write_page(page
);
2166 logfs_safe_iput(inode
, cookie
);
2170 * logfs_inode_write - write inode or dentry objects
2172 * @inode: parent inode (ifile or directory)
2173 * @buf: object to write (inode or dentry)
2175 * @_pos: object number (file position in blocks/objects)
2176 * @flags: write flags
2177 * @lock: 0 if write lock is already taken, 1 otherwise
2178 * @shadow_tree: shadow below this inode
2180 * FIXME: All caller of this put a 200-300 byte variable on the stack,
2181 * only to call here and do a memcpy from that stack variable. A good
2182 * example of wasted performance and stack space.
2184 int logfs_inode_write(struct inode
*inode
, const void *buf
, size_t count
,
2185 loff_t bix
, long flags
, struct shadow_tree
*shadow_tree
)
2187 loff_t pos
= bix
<< inode
->i_sb
->s_blocksize_bits
;
2192 BUG_ON(pos
& (LOGFS_BLOCKSIZE
-1));
2193 BUG_ON(count
> LOGFS_BLOCKSIZE
);
2194 page
= logfs_get_write_page(inode
, bix
, 0);
2198 pagebuf
= kmap_atomic(page
, KM_USER0
);
2199 memcpy(pagebuf
, buf
, count
);
2200 flush_dcache_page(page
);
2201 kunmap_atomic(pagebuf
, KM_USER0
);
2203 if (i_size_read(inode
) < pos
+ LOGFS_BLOCKSIZE
)
2204 i_size_write(inode
, pos
+ LOGFS_BLOCKSIZE
);
2206 err
= logfs_write_buf(inode
, page
, flags
);
2207 logfs_put_write_page(page
);
2211 int logfs_open_segfile(struct super_block
*sb
)
2213 struct logfs_super
*super
= logfs_super(sb
);
2214 struct inode
*inode
;
2216 inode
= logfs_read_meta_inode(sb
, LOGFS_INO_SEGFILE
);
2218 return PTR_ERR(inode
);
2219 super
->s_segfile_inode
= inode
;
2223 int logfs_init_rw(struct super_block
*sb
)
2225 struct logfs_super
*super
= logfs_super(sb
);
2226 int min_fill
= 3 * super
->s_no_blocks
;
2228 INIT_LIST_HEAD(&super
->s_object_alias
);
2229 mutex_init(&super
->s_write_mutex
);
2230 super
->s_block_pool
= mempool_create_kmalloc_pool(min_fill
,
2231 sizeof(struct logfs_block
));
2232 super
->s_shadow_pool
= mempool_create_kmalloc_pool(min_fill
,
2233 sizeof(struct logfs_shadow
));
2237 void logfs_cleanup_rw(struct super_block
*sb
)
2239 struct logfs_super
*super
= logfs_super(sb
);
2241 destroy_meta_inode(super
->s_segfile_inode
);
2242 if (super
->s_block_pool
)
2243 mempool_destroy(super
->s_block_pool
);
2244 if (super
->s_shadow_pool
)
2245 mempool_destroy(super
->s_shadow_pool
);