Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * fs/f2fs/inline.c |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | |
| 14 | #include "f2fs.h" |
Jaegeuk Kim | 67f8cf3c | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 15 | #include "node.h" |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 16 | #include <trace/events/android_fs.h> |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 17 | |
Jaegeuk Kim | 01b960e | 2015-04-23 10:27:21 -0700 | [diff] [blame] | 18 | bool f2fs_may_inline_data(struct inode *inode) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 19 | { |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 20 | if (f2fs_is_atomic_file(inode)) |
| 21 | return false; |
| 22 | |
Wanpeng Li | 368a0e4 | 2015-03-19 13:23:48 +0800 | [diff] [blame] | 23 | if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode)) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 24 | return false; |
| 25 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 26 | if (i_size_read(inode) > MAX_INLINE_DATA(inode)) |
Jaegeuk Kim | 92dffd0 | 2014-11-11 14:10:01 -0800 | [diff] [blame] | 27 | return false; |
| 28 | |
Eric Biggers | c629974 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 29 | if (f2fs_post_read_required(inode)) |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 30 | return false; |
| 31 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 32 | return true; |
| 33 | } |
| 34 | |
Jaegeuk Kim | 01b960e | 2015-04-23 10:27:21 -0700 | [diff] [blame] | 35 | bool f2fs_may_inline_dentry(struct inode *inode) |
| 36 | { |
| 37 | if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY)) |
| 38 | return false; |
| 39 | |
| 40 | if (!S_ISDIR(inode->i_mode)) |
| 41 | return false; |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 46 | void f2fs_do_read_inline_data(struct page *page, struct page *ipage) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 47 | { |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 48 | struct inode *inode = page->mapping->host; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 49 | void *src_addr, *dst_addr; |
| 50 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 51 | if (PageUptodate(page)) |
| 52 | return; |
Chao Yu | 04a17fb | 2013-12-30 18:36:23 +0800 | [diff] [blame] | 53 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 54 | f2fs_bug_on(F2FS_P_SB(page), page->index); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 55 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 56 | zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 57 | |
| 58 | /* Copy the whole inline data block */ |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 59 | src_addr = inline_data_addr(inode, ipage); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 60 | dst_addr = kmap_atomic(page); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 61 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); |
Jaegeuk Kim | 427a45c | 2014-10-26 22:59:27 -0700 | [diff] [blame] | 62 | flush_dcache_page(page); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 63 | kunmap_atomic(dst_addr); |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 64 | if (!PageUptodate(page)) |
| 65 | SetPageUptodate(page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 66 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 67 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 68 | void f2fs_truncate_inline_inode(struct inode *inode, |
| 69 | struct page *ipage, u64 from) |
Chao Yu | feeb0de | 2015-01-26 20:22:55 +0800 | [diff] [blame] | 70 | { |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 71 | void *addr; |
| 72 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 73 | if (from >= MAX_INLINE_DATA(inode)) |
| 74 | return; |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 75 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 76 | addr = inline_data_addr(inode, ipage); |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 77 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 78 | f2fs_wait_on_page_writeback(ipage, NODE, true); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 79 | memset(addr + from, 0, MAX_INLINE_DATA(inode) - from); |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 80 | set_page_dirty(ipage); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 81 | |
| 82 | if (from == 0) |
| 83 | clear_inode_flag(inode, FI_DATA_EXIST); |
Chao Yu | feeb0de | 2015-01-26 20:22:55 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 86 | int f2fs_read_inline_data(struct inode *inode, struct page *page) |
| 87 | { |
| 88 | struct page *ipage; |
| 89 | |
Mohan Srinivasan | 009e608 | 2017-02-10 14:26:23 -0800 | [diff] [blame] | 90 | if (trace_android_fs_dataread_start_enabled()) { |
| 91 | char *path, pathbuf[MAX_TRACE_PATHBUF_LEN]; |
| 92 | |
| 93 | path = android_fstrace_get_pathname(pathbuf, |
| 94 | MAX_TRACE_PATHBUF_LEN, |
| 95 | inode); |
| 96 | trace_android_fs_dataread_start(inode, page_offset(page), |
| 97 | PAGE_SIZE, current->pid, |
| 98 | path, current->comm); |
| 99 | } |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 100 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 101 | ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 102 | if (IS_ERR(ipage)) { |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 103 | trace_android_fs_dataread_end(inode, page_offset(page), |
| 104 | PAGE_SIZE); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 105 | unlock_page(page); |
| 106 | return PTR_ERR(ipage); |
| 107 | } |
| 108 | |
| 109 | if (!f2fs_has_inline_data(inode)) { |
| 110 | f2fs_put_page(ipage, 1); |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 111 | trace_android_fs_dataread_end(inode, page_offset(page), |
| 112 | PAGE_SIZE); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 113 | return -EAGAIN; |
| 114 | } |
| 115 | |
| 116 | if (page->index) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 117 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 118 | else |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 119 | f2fs_do_read_inline_data(page, ipage); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 120 | |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 121 | if (!PageUptodate(page)) |
| 122 | SetPageUptodate(page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 123 | f2fs_put_page(ipage, 1); |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 124 | trace_android_fs_dataread_end(inode, page_offset(page), |
| 125 | PAGE_SIZE); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 126 | unlock_page(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 130 | int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 131 | { |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 132 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 133 | .sbi = F2FS_I_SB(dn->inode), |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 134 | .ino = dn->inode->i_ino, |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 135 | .type = DATA, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 136 | .op = REQ_OP_WRITE, |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 137 | .op_flags = REQ_SYNC | REQ_PRIO, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 138 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 139 | .encrypted_page = NULL, |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 140 | .io_type = FS_DATA_IO, |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 141 | }; |
Jaegeuk Kim | 158c194 | 2014-11-25 11:34:02 -0800 | [diff] [blame] | 142 | int dirty, err; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 143 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 144 | if (!f2fs_exist_data(dn->inode)) |
| 145 | goto clear_out; |
Jaegeuk Kim | ec4e7af | 2014-08-18 14:41:11 -0700 | [diff] [blame] | 146 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 147 | err = f2fs_reserve_block(dn, 0); |
Jaegeuk Kim | 15c6e3a | 2014-04-16 14:22:50 +0900 | [diff] [blame] | 148 | if (err) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 149 | return err; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 150 | |
Chao Yu | 7e0782c | 2018-06-30 18:13:40 +0800 | [diff] [blame] | 151 | if (unlikely(dn->data_blkaddr != NEW_ADDR)) { |
| 152 | f2fs_put_dnode(dn); |
| 153 | set_sbi_flag(fio.sbi, SBI_NEED_FSCK); |
| 154 | f2fs_msg(fio.sbi->sb, KERN_WARNING, |
| 155 | "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, " |
| 156 | "run fsck to fix.", |
| 157 | __func__, dn->inode->i_ino, dn->data_blkaddr); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
Yunlei He | 85ead81 | 2016-02-03 10:26:13 +0800 | [diff] [blame] | 161 | f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page)); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 162 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 163 | f2fs_do_read_inline_data(page, dn->inode_page); |
Jaegeuk Kim | 6282adb | 2015-07-25 00:29:17 -0700 | [diff] [blame] | 164 | set_page_dirty(page); |
| 165 | |
Jaegeuk Kim | 158c194 | 2014-11-25 11:34:02 -0800 | [diff] [blame] | 166 | /* clear dirty state */ |
| 167 | dirty = clear_page_dirty_for_io(page); |
| 168 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 169 | /* write data page to try to make data consistent */ |
| 170 | set_page_writeback(page); |
Jaegeuk Kim | 9731a2d | 2018-04-11 23:09:04 -0700 | [diff] [blame] | 171 | ClearPageError(page); |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 172 | fio.old_blkaddr = dn->data_blkaddr; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 173 | set_inode_flag(dn->inode, FI_HOT_DATA); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 174 | f2fs_outplace_write_data(dn, &fio); |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 175 | f2fs_wait_on_page_writeback(page, DATA, true); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 176 | if (dirty) { |
Jaegeuk Kim | 158c194 | 2014-11-25 11:34:02 -0800 | [diff] [blame] | 177 | inode_dec_dirty_pages(dn->inode); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 178 | f2fs_remove_dirty_inode(dn->inode); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 179 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 180 | |
Jaegeuk Kim | 95f5b0f | 2014-11-25 17:27:38 -0800 | [diff] [blame] | 181 | /* this converted inline_data should be recovered. */ |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 182 | set_inode_flag(dn->inode, FI_APPEND_WRITE); |
Jaegeuk Kim | 95f5b0f | 2014-11-25 17:27:38 -0800 | [diff] [blame] | 183 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 184 | /* clear inline data and flag after data writeback */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 185 | f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0); |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 186 | clear_inline_node(dn->inode_page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 187 | clear_out: |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 188 | stat_dec_inline_inode(dn->inode); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 189 | clear_inode_flag(dn->inode, FI_INLINE_DATA); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 190 | f2fs_put_dnode(dn); |
| 191 | return 0; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 192 | } |
| 193 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 194 | int f2fs_convert_inline_inode(struct inode *inode) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 195 | { |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 196 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 197 | struct dnode_of_data dn; |
| 198 | struct page *ipage, *page; |
| 199 | int err = 0; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 200 | |
Jaegeuk Kim | b9d777b | 2015-12-22 11:09:35 -0800 | [diff] [blame] | 201 | if (!f2fs_has_inline_data(inode)) |
| 202 | return 0; |
| 203 | |
Jaegeuk Kim | 300e129 | 2016-04-29 16:11:53 -0700 | [diff] [blame] | 204 | page = f2fs_grab_cache_page(inode->i_mapping, 0, false); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 205 | if (!page) |
| 206 | return -ENOMEM; |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 207 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 208 | f2fs_lock_op(sbi); |
| 209 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 210 | ipage = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 211 | if (IS_ERR(ipage)) { |
Jaegeuk Kim | 6d20aff | 2014-11-17 16:06:55 -0800 | [diff] [blame] | 212 | err = PTR_ERR(ipage); |
| 213 | goto out; |
Jaegeuk Kim | b067ba1 | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 214 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 215 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 216 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
| 217 | |
| 218 | if (f2fs_has_inline_data(inode)) |
| 219 | err = f2fs_convert_inline_page(&dn, page); |
| 220 | |
| 221 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 6d20aff | 2014-11-17 16:06:55 -0800 | [diff] [blame] | 222 | out: |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 223 | f2fs_unlock_op(sbi); |
| 224 | |
| 225 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 226 | |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 227 | f2fs_balance_fs(sbi, dn.node_changed); |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 228 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 229 | return err; |
| 230 | } |
| 231 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 232 | int f2fs_write_inline_data(struct inode *inode, struct page *page) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 233 | { |
| 234 | void *src_addr, *dst_addr; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 235 | struct dnode_of_data dn; |
| 236 | int err; |
| 237 | |
| 238 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 239 | err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 240 | if (err) |
| 241 | return err; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 242 | |
Jaegeuk Kim | c08a690 | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 243 | if (!f2fs_has_inline_data(inode)) { |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 244 | f2fs_put_dnode(&dn); |
| 245 | return -EAGAIN; |
Jaegeuk Kim | c08a690 | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 248 | f2fs_bug_on(F2FS_I_SB(inode), page->index); |
| 249 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 250 | f2fs_wait_on_page_writeback(dn.inode_page, NODE, true); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 251 | src_addr = kmap_atomic(page); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 252 | dst_addr = inline_data_addr(inode, dn.inode_page); |
| 253 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 254 | kunmap_atomic(src_addr); |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 255 | set_page_dirty(dn.inode_page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 256 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 257 | f2fs_clear_radix_tree_dirty_tag(page); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 258 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 259 | set_inode_flag(inode, FI_APPEND_WRITE); |
| 260 | set_inode_flag(inode, FI_DATA_EXIST); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 261 | |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 262 | clear_inline_node(dn.inode_page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 263 | f2fs_put_dnode(&dn); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 264 | return 0; |
| 265 | } |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 266 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 267 | bool f2fs_recover_inline_data(struct inode *inode, struct page *npage) |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 268 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 269 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 270 | struct f2fs_inode *ri = NULL; |
| 271 | void *src_addr, *dst_addr; |
| 272 | struct page *ipage; |
| 273 | |
| 274 | /* |
| 275 | * The inline_data recovery policy is as follows. |
| 276 | * [prev.] [next] of inline_data flag |
| 277 | * o o -> recover inline_data |
| 278 | * o x -> remove inline_data, and then recover data blocks |
| 279 | * x o -> remove inline_data, and then recover inline_data |
| 280 | * x x -> recover data blocks |
| 281 | */ |
| 282 | if (IS_INODE(npage)) |
| 283 | ri = F2FS_INODE(npage); |
| 284 | |
| 285 | if (f2fs_has_inline_data(inode) && |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 286 | ri && (ri->i_inline & F2FS_INLINE_DATA)) { |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 287 | process_inline: |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 288 | ipage = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 289 | f2fs_bug_on(sbi, IS_ERR(ipage)); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 290 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 291 | f2fs_wait_on_page_writeback(ipage, NODE, true); |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 292 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 293 | src_addr = inline_data_addr(inode, npage); |
| 294 | dst_addr = inline_data_addr(inode, ipage); |
| 295 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 296 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 297 | set_inode_flag(inode, FI_INLINE_DATA); |
| 298 | set_inode_flag(inode, FI_DATA_EXIST); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 299 | |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 300 | set_page_dirty(ipage); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 301 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 302 | return true; |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if (f2fs_has_inline_data(inode)) { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 306 | ipage = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 307 | f2fs_bug_on(sbi, IS_ERR(ipage)); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 308 | f2fs_truncate_inline_inode(inode, ipage, 0); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 309 | clear_inode_flag(inode, FI_INLINE_DATA); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 310 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 311 | } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 312 | if (f2fs_truncate_blocks(inode, 0, false)) |
Nicholas Krause | 545fe42 | 2015-09-21 18:55:49 -0400 | [diff] [blame] | 313 | return false; |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 314 | goto process_inline; |
| 315 | } |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 316 | return false; |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 317 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 318 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 319 | struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 320 | struct fscrypt_name *fname, struct page **res_page) |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 321 | { |
| 322 | struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 323 | struct qstr name = FSTR_TO_QSTR(&fname->disk_name); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 324 | struct f2fs_dir_entry *de; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 325 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 326 | struct page *ipage; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 327 | void *inline_dentry; |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 328 | f2fs_hash_t namehash; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 329 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 330 | ipage = f2fs_get_node_page(sbi, dir->i_ino); |
Jaegeuk Kim | 42d9640 | 2016-05-25 14:29:11 -0700 | [diff] [blame] | 331 | if (IS_ERR(ipage)) { |
| 332 | *res_page = ipage; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 333 | return NULL; |
Jaegeuk Kim | 42d9640 | 2016-05-25 14:29:11 -0700 | [diff] [blame] | 334 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 335 | |
Jaegeuk Kim | 8daed21 | 2017-04-24 10:00:08 -0700 | [diff] [blame] | 336 | namehash = f2fs_dentry_hash(&name, fname); |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 337 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 338 | inline_dentry = inline_data_addr(dir, ipage); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 339 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 340 | make_dentry_ptr_inline(dir, &d, inline_dentry); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 341 | de = f2fs_find_target_dentry(fname, namehash, NULL, &d); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 342 | unlock_page(ipage); |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 343 | if (de) |
| 344 | *res_page = ipage; |
| 345 | else |
| 346 | f2fs_put_page(ipage, 0); |
| 347 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 348 | return de; |
| 349 | } |
| 350 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 351 | int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent, |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 352 | struct page *ipage) |
| 353 | { |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 354 | struct f2fs_dentry_ptr d; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 355 | void *inline_dentry; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 356 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 357 | inline_dentry = inline_data_addr(inode, ipage); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 358 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 359 | make_dentry_ptr_inline(inode, &d, inline_dentry); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 360 | f2fs_do_make_empty_dir(inode, parent, &d); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 361 | |
| 362 | set_page_dirty(ipage); |
| 363 | |
| 364 | /* update i_size to MAX_INLINE_DATA */ |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 365 | if (i_size_read(inode) < MAX_INLINE_DATA(inode)) |
| 366 | f2fs_i_size_write(inode, MAX_INLINE_DATA(inode)); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 370 | /* |
| 371 | * NOTE: ipage is grabbed by caller, but if any error occurs, we should |
| 372 | * release ipage in this function. |
| 373 | */ |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 374 | static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage, |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 375 | void *inline_dentry) |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 376 | { |
| 377 | struct page *page; |
| 378 | struct dnode_of_data dn; |
| 379 | struct f2fs_dentry_block *dentry_blk; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 380 | struct f2fs_dentry_ptr src, dst; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 381 | int err; |
| 382 | |
Jaegeuk Kim | 300e129 | 2016-04-29 16:11:53 -0700 | [diff] [blame] | 383 | page = f2fs_grab_cache_page(dir->i_mapping, 0, false); |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 384 | if (!page) { |
| 385 | f2fs_put_page(ipage, 1); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 386 | return -ENOMEM; |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 387 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 388 | |
| 389 | set_new_dnode(&dn, dir, ipage, NULL, 0); |
| 390 | err = f2fs_reserve_block(&dn, 0); |
| 391 | if (err) |
| 392 | goto out; |
| 393 | |
Chao Yu | 7e0782c | 2018-06-30 18:13:40 +0800 | [diff] [blame] | 394 | if (unlikely(dn.data_blkaddr != NEW_ADDR)) { |
| 395 | f2fs_put_dnode(&dn); |
| 396 | set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK); |
| 397 | f2fs_msg(F2FS_P_SB(page)->sb, KERN_WARNING, |
| 398 | "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, " |
| 399 | "run fsck to fix.", |
| 400 | __func__, dir->i_ino, dn.data_blkaddr); |
| 401 | err = -EINVAL; |
| 402 | goto out; |
| 403 | } |
| 404 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 405 | f2fs_wait_on_page_writeback(page, DATA, true); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 406 | |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 407 | dentry_blk = page_address(page); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 408 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 409 | make_dentry_ptr_inline(dir, &src, inline_dentry); |
| 410 | make_dentry_ptr_block(dir, &dst, dentry_blk); |
| 411 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 412 | /* copy data from inline dentry block to new dentry block */ |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 413 | memcpy(dst.bitmap, src.bitmap, src.nr_bitmap); |
| 414 | memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap); |
Chao Yu | 4ec17d6 | 2015-08-24 17:36:25 +0800 | [diff] [blame] | 415 | /* |
| 416 | * we do not need to zero out remainder part of dentry and filename |
| 417 | * field, since we have used bitmap for marking the usage status of |
| 418 | * them, besides, we can also ignore copying/zeroing reserved space |
| 419 | * of dentry block, because them haven't been used so far. |
| 420 | */ |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 421 | memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max); |
| 422 | memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 423 | |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 424 | if (!PageUptodate(page)) |
| 425 | SetPageUptodate(page); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 426 | set_page_dirty(page); |
| 427 | |
| 428 | /* clear inline dir and flag after data writeback */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 429 | f2fs_truncate_inline_inode(dir, ipage, 0); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 430 | |
Jaegeuk Kim | 3289c06 | 2014-10-13 20:00:16 -0700 | [diff] [blame] | 431 | stat_dec_inline_dir(dir); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 432 | clear_inode_flag(dir, FI_INLINE_DENTRY); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 433 | |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 434 | f2fs_i_depth_write(dir, 1); |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 435 | if (i_size_read(dir) < PAGE_SIZE) |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 436 | f2fs_i_size_write(dir, PAGE_SIZE); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 437 | out: |
| 438 | f2fs_put_page(page, 1); |
| 439 | return err; |
| 440 | } |
| 441 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 442 | static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry) |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 443 | { |
| 444 | struct f2fs_dentry_ptr d; |
| 445 | unsigned long bit_pos = 0; |
| 446 | int err = 0; |
| 447 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 448 | make_dentry_ptr_inline(dir, &d, inline_dentry); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 449 | |
| 450 | while (bit_pos < d.max) { |
| 451 | struct f2fs_dir_entry *de; |
| 452 | struct qstr new_name; |
| 453 | nid_t ino; |
| 454 | umode_t fake_mode; |
| 455 | |
| 456 | if (!test_bit_le(bit_pos, d.bitmap)) { |
| 457 | bit_pos++; |
| 458 | continue; |
| 459 | } |
| 460 | |
| 461 | de = &d.dentry[bit_pos]; |
Chao Yu | a4a13f5 | 2016-04-27 22:22:20 +0800 | [diff] [blame] | 462 | |
| 463 | if (unlikely(!de->name_len)) { |
| 464 | bit_pos++; |
| 465 | continue; |
| 466 | } |
| 467 | |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 468 | new_name.name = d.filename[bit_pos]; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 469 | new_name.len = le16_to_cpu(de->name_len); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 470 | |
| 471 | ino = le32_to_cpu(de->ino); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 472 | fake_mode = f2fs_get_de_type(de) << S_SHIFT; |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 473 | |
Chao Yu | 9421d57 | 2016-08-28 18:57:55 +0800 | [diff] [blame] | 474 | err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL, |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 475 | ino, fake_mode); |
| 476 | if (err) |
| 477 | goto punch_dentry_pages; |
| 478 | |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 479 | bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); |
| 480 | } |
| 481 | return 0; |
| 482 | punch_dentry_pages: |
| 483 | truncate_inode_pages(&dir->i_data, 0); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 484 | f2fs_truncate_blocks(dir, 0, false); |
| 485 | f2fs_remove_dirty_inode(dir); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 486 | return err; |
| 487 | } |
| 488 | |
| 489 | static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage, |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 490 | void *inline_dentry) |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 491 | { |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 492 | void *backup_dentry; |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 493 | int err; |
| 494 | |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 495 | backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir), |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 496 | MAX_INLINE_DATA(dir), GFP_F2FS_ZERO); |
Chao Yu | 8975bdf | 2016-05-14 19:03:53 +0800 | [diff] [blame] | 497 | if (!backup_dentry) { |
| 498 | f2fs_put_page(ipage, 1); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 499 | return -ENOMEM; |
Chao Yu | 8975bdf | 2016-05-14 19:03:53 +0800 | [diff] [blame] | 500 | } |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 501 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 502 | memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir)); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 503 | f2fs_truncate_inline_inode(dir, ipage, 0); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 504 | |
| 505 | unlock_page(ipage); |
| 506 | |
| 507 | err = f2fs_add_inline_entries(dir, backup_dentry); |
| 508 | if (err) |
| 509 | goto recover; |
| 510 | |
| 511 | lock_page(ipage); |
| 512 | |
| 513 | stat_dec_inline_dir(dir); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 514 | clear_inode_flag(dir, FI_INLINE_DENTRY); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 515 | kfree(backup_dentry); |
| 516 | return 0; |
| 517 | recover: |
| 518 | lock_page(ipage); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 519 | memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir)); |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 520 | f2fs_i_depth_write(dir, 0); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 521 | f2fs_i_size_write(dir, MAX_INLINE_DATA(dir)); |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 522 | set_page_dirty(ipage); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 523 | f2fs_put_page(ipage, 1); |
| 524 | |
| 525 | kfree(backup_dentry); |
| 526 | return err; |
| 527 | } |
| 528 | |
| 529 | static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 530 | void *inline_dentry) |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 531 | { |
| 532 | if (!F2FS_I(dir)->i_dir_level) |
| 533 | return f2fs_move_inline_dirents(dir, ipage, inline_dentry); |
| 534 | else |
| 535 | return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry); |
| 536 | } |
| 537 | |
Chao Yu | 9421d57 | 2016-08-28 18:57:55 +0800 | [diff] [blame] | 538 | int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name, |
| 539 | const struct qstr *orig_name, |
| 540 | struct inode *inode, nid_t ino, umode_t mode) |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 541 | { |
| 542 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 543 | struct page *ipage; |
| 544 | unsigned int bit_pos; |
| 545 | f2fs_hash_t name_hash; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 546 | void *inline_dentry = NULL; |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 547 | struct f2fs_dentry_ptr d; |
Chao Yu | 9421d57 | 2016-08-28 18:57:55 +0800 | [diff] [blame] | 548 | int slots = GET_DENTRY_SLOTS(new_name->len); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 549 | struct page *page = NULL; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 550 | int err = 0; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 551 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 552 | ipage = f2fs_get_node_page(sbi, dir->i_ino); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 553 | if (IS_ERR(ipage)) |
| 554 | return PTR_ERR(ipage); |
| 555 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 556 | inline_dentry = inline_data_addr(dir, ipage); |
| 557 | make_dentry_ptr_inline(dir, &d, inline_dentry); |
| 558 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 559 | bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 560 | if (bit_pos >= d.max) { |
| 561 | err = f2fs_convert_inline_dir(dir, ipage, inline_dentry); |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 562 | if (err) |
| 563 | return err; |
| 564 | err = -EAGAIN; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 565 | goto out; |
| 566 | } |
| 567 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 568 | if (inode) { |
| 569 | down_write(&F2FS_I(inode)->i_sem); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 570 | page = f2fs_init_inode_metadata(inode, dir, new_name, |
Chao Yu | 9421d57 | 2016-08-28 18:57:55 +0800 | [diff] [blame] | 571 | orig_name, ipage); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 572 | if (IS_ERR(page)) { |
| 573 | err = PTR_ERR(page); |
| 574 | goto fail; |
| 575 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 576 | } |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 577 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 578 | f2fs_wait_on_page_writeback(ipage, NODE, true); |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 579 | |
Jaegeuk Kim | 8daed21 | 2017-04-24 10:00:08 -0700 | [diff] [blame] | 580 | name_hash = f2fs_dentry_hash(new_name, NULL); |
Chao Yu | 9421d57 | 2016-08-28 18:57:55 +0800 | [diff] [blame] | 581 | f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos); |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 582 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 583 | set_page_dirty(ipage); |
| 584 | |
| 585 | /* we don't need to mark_inode_dirty now */ |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 586 | if (inode) { |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 587 | f2fs_i_pino_write(inode, dir->i_ino); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 588 | f2fs_put_page(page, 1); |
| 589 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 590 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 591 | f2fs_update_parent_metadata(dir, inode, 0); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 592 | fail: |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 593 | if (inode) |
| 594 | up_write(&F2FS_I(inode)->i_sem); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 595 | out: |
| 596 | f2fs_put_page(ipage, 1); |
| 597 | return err; |
| 598 | } |
| 599 | |
| 600 | void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, |
| 601 | struct inode *dir, struct inode *inode) |
| 602 | { |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 603 | struct f2fs_dentry_ptr d; |
| 604 | void *inline_dentry; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 605 | int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); |
| 606 | unsigned int bit_pos; |
| 607 | int i; |
| 608 | |
| 609 | lock_page(page); |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 610 | f2fs_wait_on_page_writeback(page, NODE, true); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 611 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 612 | inline_dentry = inline_data_addr(dir, page); |
| 613 | make_dentry_ptr_inline(dir, &d, inline_dentry); |
| 614 | |
| 615 | bit_pos = dentry - d.dentry; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 616 | for (i = 0; i < slots; i++) |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 617 | __clear_bit_le(bit_pos + i, d.bitmap); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 618 | |
| 619 | set_page_dirty(page); |
Jaegeuk Kim | 9f7c45c | 2016-06-01 21:18:25 -0700 | [diff] [blame] | 620 | f2fs_put_page(page, 1); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 621 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 622 | dir->i_ctime = dir->i_mtime = current_time(dir); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 623 | f2fs_mark_inode_dirty_sync(dir, false); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 624 | |
| 625 | if (inode) |
Jaegeuk Kim | 9f7c45c | 2016-06-01 21:18:25 -0700 | [diff] [blame] | 626 | f2fs_drop_nlink(dir, inode); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | bool f2fs_empty_inline_dir(struct inode *dir) |
| 630 | { |
| 631 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 632 | struct page *ipage; |
| 633 | unsigned int bit_pos = 2; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 634 | void *inline_dentry; |
| 635 | struct f2fs_dentry_ptr d; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 636 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 637 | ipage = f2fs_get_node_page(sbi, dir->i_ino); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 638 | if (IS_ERR(ipage)) |
| 639 | return false; |
| 640 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 641 | inline_dentry = inline_data_addr(dir, ipage); |
| 642 | make_dentry_ptr_inline(dir, &d, inline_dentry); |
| 643 | |
| 644 | bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 645 | |
| 646 | f2fs_put_page(ipage, 1); |
| 647 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 648 | if (bit_pos < d.max) |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 649 | return false; |
| 650 | |
| 651 | return true; |
| 652 | } |
| 653 | |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 654 | int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 655 | struct fscrypt_str *fstr) |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 656 | { |
| 657 | struct inode *inode = file_inode(file); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 658 | struct page *ipage = NULL; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 659 | struct f2fs_dentry_ptr d; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 660 | void *inline_dentry = NULL; |
| 661 | int err; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 662 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 663 | make_dentry_ptr_inline(inode, &d, inline_dentry); |
| 664 | |
| 665 | if (ctx->pos == d.max) |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 666 | return 0; |
| 667 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 668 | ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 669 | if (IS_ERR(ipage)) |
| 670 | return PTR_ERR(ipage); |
| 671 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 672 | inline_dentry = inline_data_addr(inode, ipage); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 673 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 674 | make_dentry_ptr_inline(inode, &d, inline_dentry); |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 675 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 676 | err = f2fs_fill_dentries(ctx, &d, 0, fstr); |
| 677 | if (!err) |
| 678 | ctx->pos = d.max; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 679 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 680 | f2fs_put_page(ipage, 1); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 681 | return err < 0 ? err : 0; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 682 | } |
Jaegeuk Kim | 67f8cf3c | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 683 | |
| 684 | int f2fs_inline_data_fiemap(struct inode *inode, |
| 685 | struct fiemap_extent_info *fieinfo, __u64 start, __u64 len) |
| 686 | { |
| 687 | __u64 byteaddr, ilen; |
| 688 | __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED | |
| 689 | FIEMAP_EXTENT_LAST; |
| 690 | struct node_info ni; |
| 691 | struct page *ipage; |
| 692 | int err = 0; |
| 693 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 694 | ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Jaegeuk Kim | 67f8cf3c | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 695 | if (IS_ERR(ipage)) |
| 696 | return PTR_ERR(ipage); |
| 697 | |
| 698 | if (!f2fs_has_inline_data(inode)) { |
| 699 | err = -EAGAIN; |
| 700 | goto out; |
| 701 | } |
| 702 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 703 | ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode)); |
Jaegeuk Kim | 67f8cf3c | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 704 | if (start >= ilen) |
| 705 | goto out; |
| 706 | if (start + len < ilen) |
| 707 | ilen = start + len; |
| 708 | ilen -= start; |
| 709 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 710 | f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni); |
Jaegeuk Kim | 67f8cf3c | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 711 | byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 712 | byteaddr += (char *)inline_data_addr(inode, ipage) - |
| 713 | (char *)F2FS_INODE(ipage); |
Jaegeuk Kim | 67f8cf3c | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 714 | err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags); |
| 715 | out: |
| 716 | f2fs_put_page(ipage, 1); |
| 717 | return err; |
| 718 | } |