3 * Copyright (c) 2013, Intel Corporation
4 * Authors: Huajun Li <huajun.li@intel.com>
5 * Haicheng Li <haicheng.li@intel.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
17 bool f2fs_may_inline_data(struct inode
*inode
)
19 if (f2fs_is_atomic_file(inode
))
22 if (!S_ISREG(inode
->i_mode
) && !S_ISLNK(inode
->i_mode
))
25 if (i_size_read(inode
) > MAX_INLINE_DATA(inode
))
28 if (f2fs_post_read_required(inode
))
34 bool f2fs_may_inline_dentry(struct inode
*inode
)
36 if (!test_opt(F2FS_I_SB(inode
), INLINE_DENTRY
))
39 if (!S_ISDIR(inode
->i_mode
))
45 void f2fs_do_read_inline_data(struct page
*page
, struct page
*ipage
)
47 struct inode
*inode
= page
->mapping
->host
;
48 void *src_addr
, *dst_addr
;
50 if (PageUptodate(page
))
53 f2fs_bug_on(F2FS_P_SB(page
), page
->index
);
55 zero_user_segment(page
, MAX_INLINE_DATA(inode
), PAGE_SIZE
);
57 /* Copy the whole inline data block */
58 src_addr
= inline_data_addr(inode
, ipage
);
59 dst_addr
= kmap_atomic(page
);
60 memcpy(dst_addr
, src_addr
, MAX_INLINE_DATA(inode
));
61 flush_dcache_page(page
);
62 kunmap_atomic(dst_addr
);
63 if (!PageUptodate(page
))
64 SetPageUptodate(page
);
67 void f2fs_truncate_inline_inode(struct inode
*inode
,
68 struct page
*ipage
, u64 from
)
72 if (from
>= MAX_INLINE_DATA(inode
))
75 addr
= inline_data_addr(inode
, ipage
);
77 f2fs_wait_on_page_writeback(ipage
, NODE
, true);
78 memset(addr
+ from
, 0, MAX_INLINE_DATA(inode
) - from
);
79 set_page_dirty(ipage
);
82 clear_inode_flag(inode
, FI_DATA_EXIST
);
85 int f2fs_read_inline_data(struct inode
*inode
, struct page
*page
)
89 ipage
= f2fs_get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
92 return PTR_ERR(ipage
);
95 if (!f2fs_has_inline_data(inode
)) {
96 f2fs_put_page(ipage
, 1);
101 zero_user_segment(page
, 0, PAGE_SIZE
);
103 f2fs_do_read_inline_data(page
, ipage
);
105 if (!PageUptodate(page
))
106 SetPageUptodate(page
);
107 f2fs_put_page(ipage
, 1);
112 int f2fs_convert_inline_page(struct dnode_of_data
*dn
, struct page
*page
)
114 struct f2fs_io_info fio
= {
115 .sbi
= F2FS_I_SB(dn
->inode
),
116 .ino
= dn
->inode
->i_ino
,
119 .op_flags
= REQ_SYNC
| REQ_PRIO
,
121 .encrypted_page
= NULL
,
122 .io_type
= FS_DATA_IO
,
127 if (!f2fs_exist_data(dn
->inode
))
130 err
= f2fs_reserve_block(dn
, 0);
134 err
= f2fs_get_node_info(fio
.sbi
, dn
->nid
, &ni
);
140 fio
.version
= ni
.version
;
142 if (unlikely(dn
->data_blkaddr
!= NEW_ADDR
)) {
144 set_sbi_flag(fio
.sbi
, SBI_NEED_FSCK
);
145 f2fs_msg(fio
.sbi
->sb
, KERN_WARNING
,
146 "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, "
148 __func__
, dn
->inode
->i_ino
, dn
->data_blkaddr
);
152 f2fs_bug_on(F2FS_P_SB(page
), PageWriteback(page
));
154 f2fs_do_read_inline_data(page
, dn
->inode_page
);
155 set_page_dirty(page
);
157 /* clear dirty state */
158 dirty
= clear_page_dirty_for_io(page
);
160 /* write data page to try to make data consistent */
161 set_page_writeback(page
);
162 ClearPageError(page
);
163 fio
.old_blkaddr
= dn
->data_blkaddr
;
164 set_inode_flag(dn
->inode
, FI_HOT_DATA
);
165 f2fs_outplace_write_data(dn
, &fio
);
166 f2fs_wait_on_page_writeback(page
, DATA
, true);
168 inode_dec_dirty_pages(dn
->inode
);
169 f2fs_remove_dirty_inode(dn
->inode
);
172 /* this converted inline_data should be recovered. */
173 set_inode_flag(dn
->inode
, FI_APPEND_WRITE
);
175 /* clear inline data and flag after data writeback */
176 f2fs_truncate_inline_inode(dn
->inode
, dn
->inode_page
, 0);
177 clear_inline_node(dn
->inode_page
);
179 stat_dec_inline_inode(dn
->inode
);
180 clear_inode_flag(dn
->inode
, FI_INLINE_DATA
);
185 int f2fs_convert_inline_inode(struct inode
*inode
)
187 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
188 struct dnode_of_data dn
;
189 struct page
*ipage
, *page
;
192 if (!f2fs_has_inline_data(inode
))
195 page
= f2fs_grab_cache_page(inode
->i_mapping
, 0, false);
201 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
203 err
= PTR_ERR(ipage
);
207 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
209 if (f2fs_has_inline_data(inode
))
210 err
= f2fs_convert_inline_page(&dn
, page
);
216 f2fs_put_page(page
, 1);
218 f2fs_balance_fs(sbi
, dn
.node_changed
);
223 int f2fs_write_inline_data(struct inode
*inode
, struct page
*page
)
225 void *src_addr
, *dst_addr
;
226 struct dnode_of_data dn
;
229 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
230 err
= f2fs_get_dnode_of_data(&dn
, 0, LOOKUP_NODE
);
234 if (!f2fs_has_inline_data(inode
)) {
239 f2fs_bug_on(F2FS_I_SB(inode
), page
->index
);
241 f2fs_wait_on_page_writeback(dn
.inode_page
, NODE
, true);
242 src_addr
= kmap_atomic(page
);
243 dst_addr
= inline_data_addr(inode
, dn
.inode_page
);
244 memcpy(dst_addr
, src_addr
, MAX_INLINE_DATA(inode
));
245 kunmap_atomic(src_addr
);
246 set_page_dirty(dn
.inode_page
);
248 f2fs_clear_radix_tree_dirty_tag(page
);
250 set_inode_flag(inode
, FI_APPEND_WRITE
);
251 set_inode_flag(inode
, FI_DATA_EXIST
);
253 clear_inline_node(dn
.inode_page
);
258 bool f2fs_recover_inline_data(struct inode
*inode
, struct page
*npage
)
260 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
261 struct f2fs_inode
*ri
= NULL
;
262 void *src_addr
, *dst_addr
;
266 * The inline_data recovery policy is as follows.
267 * [prev.] [next] of inline_data flag
268 * o o -> recover inline_data
269 * o x -> remove inline_data, and then recover data blocks
270 * x o -> remove inline_data, and then recover inline_data
271 * x x -> recover data blocks
274 ri
= F2FS_INODE(npage
);
276 if (f2fs_has_inline_data(inode
) &&
277 ri
&& (ri
->i_inline
& F2FS_INLINE_DATA
)) {
279 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
280 f2fs_bug_on(sbi
, IS_ERR(ipage
));
282 f2fs_wait_on_page_writeback(ipage
, NODE
, true);
284 src_addr
= inline_data_addr(inode
, npage
);
285 dst_addr
= inline_data_addr(inode
, ipage
);
286 memcpy(dst_addr
, src_addr
, MAX_INLINE_DATA(inode
));
288 set_inode_flag(inode
, FI_INLINE_DATA
);
289 set_inode_flag(inode
, FI_DATA_EXIST
);
291 set_page_dirty(ipage
);
292 f2fs_put_page(ipage
, 1);
296 if (f2fs_has_inline_data(inode
)) {
297 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
298 f2fs_bug_on(sbi
, IS_ERR(ipage
));
299 f2fs_truncate_inline_inode(inode
, ipage
, 0);
300 clear_inode_flag(inode
, FI_INLINE_DATA
);
301 f2fs_put_page(ipage
, 1);
302 } else if (ri
&& (ri
->i_inline
& F2FS_INLINE_DATA
)) {
303 if (f2fs_truncate_blocks(inode
, 0, false))
310 struct f2fs_dir_entry
*f2fs_find_in_inline_dir(struct inode
*dir
,
311 struct fscrypt_name
*fname
, struct page
**res_page
)
313 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
314 struct qstr name
= FSTR_TO_QSTR(&fname
->disk_name
);
315 struct f2fs_dir_entry
*de
;
316 struct f2fs_dentry_ptr d
;
319 f2fs_hash_t namehash
;
321 ipage
= f2fs_get_node_page(sbi
, dir
->i_ino
);
327 namehash
= f2fs_dentry_hash(&name
, fname
);
329 inline_dentry
= inline_data_addr(dir
, ipage
);
331 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
332 de
= f2fs_find_target_dentry(fname
, namehash
, NULL
, &d
);
337 f2fs_put_page(ipage
, 0);
342 int f2fs_make_empty_inline_dir(struct inode
*inode
, struct inode
*parent
,
345 struct f2fs_dentry_ptr d
;
348 inline_dentry
= inline_data_addr(inode
, ipage
);
350 make_dentry_ptr_inline(inode
, &d
, inline_dentry
);
351 f2fs_do_make_empty_dir(inode
, parent
, &d
);
353 set_page_dirty(ipage
);
355 /* update i_size to MAX_INLINE_DATA */
356 if (i_size_read(inode
) < MAX_INLINE_DATA(inode
))
357 f2fs_i_size_write(inode
, MAX_INLINE_DATA(inode
));
362 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
363 * release ipage in this function.
365 static int f2fs_move_inline_dirents(struct inode
*dir
, struct page
*ipage
,
369 struct dnode_of_data dn
;
370 struct f2fs_dentry_block
*dentry_blk
;
371 struct f2fs_dentry_ptr src
, dst
;
374 page
= f2fs_grab_cache_page(dir
->i_mapping
, 0, false);
376 f2fs_put_page(ipage
, 1);
380 set_new_dnode(&dn
, dir
, ipage
, NULL
, 0);
381 err
= f2fs_reserve_block(&dn
, 0);
385 if (unlikely(dn
.data_blkaddr
!= NEW_ADDR
)) {
387 set_sbi_flag(F2FS_P_SB(page
), SBI_NEED_FSCK
);
388 f2fs_msg(F2FS_P_SB(page
)->sb
, KERN_WARNING
,
389 "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, "
391 __func__
, dir
->i_ino
, dn
.data_blkaddr
);
396 f2fs_wait_on_page_writeback(page
, DATA
, true);
398 dentry_blk
= page_address(page
);
400 make_dentry_ptr_inline(dir
, &src
, inline_dentry
);
401 make_dentry_ptr_block(dir
, &dst
, dentry_blk
);
403 /* copy data from inline dentry block to new dentry block */
404 memcpy(dst
.bitmap
, src
.bitmap
, src
.nr_bitmap
);
405 memset(dst
.bitmap
+ src
.nr_bitmap
, 0, dst
.nr_bitmap
- src
.nr_bitmap
);
407 * we do not need to zero out remainder part of dentry and filename
408 * field, since we have used bitmap for marking the usage status of
409 * them, besides, we can also ignore copying/zeroing reserved space
410 * of dentry block, because them haven't been used so far.
412 memcpy(dst
.dentry
, src
.dentry
, SIZE_OF_DIR_ENTRY
* src
.max
);
413 memcpy(dst
.filename
, src
.filename
, src
.max
* F2FS_SLOT_LEN
);
415 if (!PageUptodate(page
))
416 SetPageUptodate(page
);
417 set_page_dirty(page
);
419 /* clear inline dir and flag after data writeback */
420 f2fs_truncate_inline_inode(dir
, ipage
, 0);
422 stat_dec_inline_dir(dir
);
423 clear_inode_flag(dir
, FI_INLINE_DENTRY
);
425 f2fs_i_depth_write(dir
, 1);
426 if (i_size_read(dir
) < PAGE_SIZE
)
427 f2fs_i_size_write(dir
, PAGE_SIZE
);
429 f2fs_put_page(page
, 1);
433 static int f2fs_add_inline_entries(struct inode
*dir
, void *inline_dentry
)
435 struct f2fs_dentry_ptr d
;
436 unsigned long bit_pos
= 0;
439 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
441 while (bit_pos
< d
.max
) {
442 struct f2fs_dir_entry
*de
;
443 struct qstr new_name
;
447 if (!test_bit_le(bit_pos
, d
.bitmap
)) {
452 de
= &d
.dentry
[bit_pos
];
454 if (unlikely(!de
->name_len
)) {
459 new_name
.name
= d
.filename
[bit_pos
];
460 new_name
.len
= le16_to_cpu(de
->name_len
);
462 ino
= le32_to_cpu(de
->ino
);
463 fake_mode
= f2fs_get_de_type(de
) << S_SHIFT
;
465 err
= f2fs_add_regular_entry(dir
, &new_name
, NULL
, NULL
,
468 goto punch_dentry_pages
;
470 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
474 truncate_inode_pages(&dir
->i_data
, 0);
475 f2fs_truncate_blocks(dir
, 0, false);
476 f2fs_remove_dirty_inode(dir
);
480 static int f2fs_move_rehashed_dirents(struct inode
*dir
, struct page
*ipage
,
486 backup_dentry
= f2fs_kmalloc(F2FS_I_SB(dir
),
487 MAX_INLINE_DATA(dir
), GFP_F2FS_ZERO
);
488 if (!backup_dentry
) {
489 f2fs_put_page(ipage
, 1);
493 memcpy(backup_dentry
, inline_dentry
, MAX_INLINE_DATA(dir
));
494 f2fs_truncate_inline_inode(dir
, ipage
, 0);
498 err
= f2fs_add_inline_entries(dir
, backup_dentry
);
504 stat_dec_inline_dir(dir
);
505 clear_inode_flag(dir
, FI_INLINE_DENTRY
);
506 kfree(backup_dentry
);
510 f2fs_wait_on_page_writeback(ipage
, NODE
, true);
511 memcpy(inline_dentry
, backup_dentry
, MAX_INLINE_DATA(dir
));
512 f2fs_i_depth_write(dir
, 0);
513 f2fs_i_size_write(dir
, MAX_INLINE_DATA(dir
));
514 set_page_dirty(ipage
);
515 f2fs_put_page(ipage
, 1);
517 kfree(backup_dentry
);
521 static int f2fs_convert_inline_dir(struct inode
*dir
, struct page
*ipage
,
524 if (!F2FS_I(dir
)->i_dir_level
)
525 return f2fs_move_inline_dirents(dir
, ipage
, inline_dentry
);
527 return f2fs_move_rehashed_dirents(dir
, ipage
, inline_dentry
);
530 int f2fs_add_inline_entry(struct inode
*dir
, const struct qstr
*new_name
,
531 const struct qstr
*orig_name
,
532 struct inode
*inode
, nid_t ino
, umode_t mode
)
534 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
536 unsigned int bit_pos
;
537 f2fs_hash_t name_hash
;
538 void *inline_dentry
= NULL
;
539 struct f2fs_dentry_ptr d
;
540 int slots
= GET_DENTRY_SLOTS(new_name
->len
);
541 struct page
*page
= NULL
;
544 ipage
= f2fs_get_node_page(sbi
, dir
->i_ino
);
546 return PTR_ERR(ipage
);
548 inline_dentry
= inline_data_addr(dir
, ipage
);
549 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
551 bit_pos
= f2fs_room_for_filename(d
.bitmap
, slots
, d
.max
);
552 if (bit_pos
>= d
.max
) {
553 err
= f2fs_convert_inline_dir(dir
, ipage
, inline_dentry
);
561 down_write(&F2FS_I(inode
)->i_sem
);
562 page
= f2fs_init_inode_metadata(inode
, dir
, new_name
,
570 f2fs_wait_on_page_writeback(ipage
, NODE
, true);
572 name_hash
= f2fs_dentry_hash(new_name
, NULL
);
573 f2fs_update_dentry(ino
, mode
, &d
, new_name
, name_hash
, bit_pos
);
575 set_page_dirty(ipage
);
577 /* we don't need to mark_inode_dirty now */
579 f2fs_i_pino_write(inode
, dir
->i_ino
);
580 f2fs_put_page(page
, 1);
583 f2fs_update_parent_metadata(dir
, inode
, 0);
586 up_write(&F2FS_I(inode
)->i_sem
);
588 f2fs_put_page(ipage
, 1);
592 void f2fs_delete_inline_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
593 struct inode
*dir
, struct inode
*inode
)
595 struct f2fs_dentry_ptr d
;
597 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
598 unsigned int bit_pos
;
602 f2fs_wait_on_page_writeback(page
, NODE
, true);
604 inline_dentry
= inline_data_addr(dir
, page
);
605 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
607 bit_pos
= dentry
- d
.dentry
;
608 for (i
= 0; i
< slots
; i
++)
609 __clear_bit_le(bit_pos
+ i
, d
.bitmap
);
611 set_page_dirty(page
);
612 f2fs_put_page(page
, 1);
614 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
615 f2fs_mark_inode_dirty_sync(dir
, false);
618 f2fs_drop_nlink(dir
, inode
);
621 bool f2fs_empty_inline_dir(struct inode
*dir
)
623 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
625 unsigned int bit_pos
= 2;
627 struct f2fs_dentry_ptr d
;
629 ipage
= f2fs_get_node_page(sbi
, dir
->i_ino
);
633 inline_dentry
= inline_data_addr(dir
, ipage
);
634 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
636 bit_pos
= find_next_bit_le(d
.bitmap
, d
.max
, bit_pos
);
638 f2fs_put_page(ipage
, 1);
646 int f2fs_read_inline_dir(struct file
*file
, struct dir_context
*ctx
,
647 struct fscrypt_str
*fstr
)
649 struct inode
*inode
= file_inode(file
);
650 struct page
*ipage
= NULL
;
651 struct f2fs_dentry_ptr d
;
652 void *inline_dentry
= NULL
;
655 make_dentry_ptr_inline(inode
, &d
, inline_dentry
);
657 if (ctx
->pos
== d
.max
)
660 ipage
= f2fs_get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
662 return PTR_ERR(ipage
);
664 inline_dentry
= inline_data_addr(inode
, ipage
);
666 make_dentry_ptr_inline(inode
, &d
, inline_dentry
);
668 err
= f2fs_fill_dentries(ctx
, &d
, 0, fstr
);
672 f2fs_put_page(ipage
, 1);
673 return err
< 0 ? err
: 0;
676 int f2fs_inline_data_fiemap(struct inode
*inode
,
677 struct fiemap_extent_info
*fieinfo
, __u64 start
, __u64 len
)
679 __u64 byteaddr
, ilen
;
680 __u32 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
|
686 ipage
= f2fs_get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
688 return PTR_ERR(ipage
);
690 if (!f2fs_has_inline_data(inode
)) {
695 ilen
= min_t(size_t, MAX_INLINE_DATA(inode
), i_size_read(inode
));
698 if (start
+ len
< ilen
)
702 err
= f2fs_get_node_info(F2FS_I_SB(inode
), inode
->i_ino
, &ni
);
706 byteaddr
= (__u64
)ni
.blk_addr
<< inode
->i_sb
->s_blocksize_bits
;
707 byteaddr
+= (char *)inline_data_addr(inode
, ipage
) -
708 (char *)F2FS_INODE(ipage
);
709 err
= fiemap_fill_next_extent(fieinfo
, start
, byteaddr
, ilen
, flags
);
711 f2fs_put_page(ipage
, 1);