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" |
| 15 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 16 | bool f2fs_may_inline(struct inode *inode) |
| 17 | { |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 18 | block_t nr_blocks; |
| 19 | loff_t i_size; |
| 20 | |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 21 | if (!test_opt(F2FS_I_SB(inode), INLINE_DATA)) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 22 | return false; |
| 23 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 24 | if (f2fs_is_atomic_file(inode)) |
| 25 | return false; |
| 26 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 27 | nr_blocks = F2FS_I(inode)->i_xattr_nid ? 3 : 2; |
| 28 | if (inode->i_blocks > nr_blocks) |
| 29 | return false; |
| 30 | |
| 31 | i_size = i_size_read(inode); |
| 32 | if (i_size > MAX_INLINE_DATA) |
| 33 | return false; |
| 34 | |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | int f2fs_read_inline_data(struct inode *inode, struct page *page) |
| 39 | { |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 40 | struct page *ipage; |
| 41 | void *src_addr, *dst_addr; |
| 42 | |
Chao Yu | 04a17fb | 2013-12-30 18:36:23 +0800 | [diff] [blame] | 43 | if (page->index) { |
| 44 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 45 | goto out; |
| 46 | } |
| 47 | |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 48 | ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 49 | if (IS_ERR(ipage)) { |
| 50 | unlock_page(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 51 | return PTR_ERR(ipage); |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 52 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 53 | |
Chao Yu | 18309aa | 2013-12-30 09:29:06 +0800 | [diff] [blame] | 54 | zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 55 | |
| 56 | /* Copy the whole inline data block */ |
| 57 | src_addr = inline_data_addr(ipage); |
| 58 | dst_addr = kmap(page); |
| 59 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
| 60 | kunmap(page); |
| 61 | f2fs_put_page(ipage, 1); |
| 62 | |
Chao Yu | 04a17fb | 2013-12-30 18:36:23 +0800 | [diff] [blame] | 63 | out: |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 64 | SetPageUptodate(page); |
| 65 | unlock_page(page); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int __f2fs_convert_inline_data(struct inode *inode, struct page *page) |
| 71 | { |
Jaegeuk Kim | ec4e7af | 2014-08-18 14:41:11 -0700 | [diff] [blame] | 72 | int err = 0; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 73 | struct page *ipage; |
| 74 | struct dnode_of_data dn; |
| 75 | void *src_addr, *dst_addr; |
| 76 | block_t new_blk_addr; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 77 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 78 | struct f2fs_io_info fio = { |
| 79 | .type = DATA, |
| 80 | .rw = WRITE_SYNC | REQ_PRIO, |
| 81 | }; |
| 82 | |
| 83 | f2fs_lock_op(sbi); |
| 84 | ipage = get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 15c6e3a | 2014-04-16 14:22:50 +0900 | [diff] [blame] | 85 | if (IS_ERR(ipage)) { |
| 86 | err = PTR_ERR(ipage); |
| 87 | goto out; |
| 88 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 89 | |
Jaegeuk Kim | ec4e7af | 2014-08-18 14:41:11 -0700 | [diff] [blame] | 90 | /* someone else converted inline_data already */ |
| 91 | if (!f2fs_has_inline_data(inode)) |
| 92 | goto out; |
| 93 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 94 | /* |
| 95 | * i_addr[0] is not used for inline data, |
| 96 | * so reserving new block will not destroy inline data |
| 97 | */ |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 98 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 99 | err = f2fs_reserve_block(&dn, 0); |
Jaegeuk Kim | 15c6e3a | 2014-04-16 14:22:50 +0900 | [diff] [blame] | 100 | if (err) |
| 101 | goto out; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 102 | |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 103 | f2fs_wait_on_page_writeback(page, DATA); |
Chao Yu | 18309aa | 2013-12-30 09:29:06 +0800 | [diff] [blame] | 104 | zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 105 | |
| 106 | /* Copy the whole inline data block */ |
| 107 | src_addr = inline_data_addr(ipage); |
| 108 | dst_addr = kmap(page); |
| 109 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
| 110 | kunmap(page); |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 111 | SetPageUptodate(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 112 | |
| 113 | /* write data page to try to make data consistent */ |
| 114 | set_page_writeback(page); |
| 115 | write_data_page(page, &dn, &new_blk_addr, &fio); |
| 116 | update_extent_cache(new_blk_addr, &dn); |
Yuan Zhong | 5514f0a | 2014-01-10 07:26:14 +0000 | [diff] [blame] | 117 | f2fs_wait_on_page_writeback(page, DATA); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 118 | |
| 119 | /* clear inline data and flag after data writeback */ |
| 120 | zero_user_segment(ipage, INLINE_DATA_OFFSET, |
| 121 | INLINE_DATA_OFFSET + MAX_INLINE_DATA); |
| 122 | clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
Jaegeuk Kim | 0dbdc2a | 2013-11-26 11:08:57 +0900 | [diff] [blame] | 123 | stat_dec_inline_inode(inode); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 124 | |
| 125 | sync_inode_page(&dn); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 126 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 15c6e3a | 2014-04-16 14:22:50 +0900 | [diff] [blame] | 127 | out: |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 128 | f2fs_unlock_op(sbi); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 129 | return err; |
| 130 | } |
| 131 | |
Jaegeuk Kim | b067ba1 | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 132 | int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size, |
| 133 | struct page *page) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 134 | { |
Jaegeuk Kim | b067ba1 | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 135 | struct page *new_page = page; |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 136 | int err; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 137 | |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 138 | if (!f2fs_has_inline_data(inode)) |
| 139 | return 0; |
| 140 | else if (to_size <= MAX_INLINE_DATA) |
| 141 | return 0; |
| 142 | |
Jaegeuk Kim | b067ba1 | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 143 | if (!page || page->index != 0) { |
| 144 | new_page = grab_cache_page(inode->i_mapping, 0); |
| 145 | if (!new_page) |
| 146 | return -ENOMEM; |
| 147 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 148 | |
Jaegeuk Kim | b067ba1 | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 149 | err = __f2fs_convert_inline_data(inode, new_page); |
| 150 | if (!page || page->index != 0) |
| 151 | f2fs_put_page(new_page, 1); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 152 | return err; |
| 153 | } |
| 154 | |
| 155 | int f2fs_write_inline_data(struct inode *inode, |
Jaegeuk Kim | b067ba1 | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 156 | struct page *page, unsigned size) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 157 | { |
| 158 | void *src_addr, *dst_addr; |
| 159 | struct page *ipage; |
| 160 | struct dnode_of_data dn; |
| 161 | int err; |
| 162 | |
| 163 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 164 | err = get_dnode_of_data(&dn, 0, LOOKUP_NODE); |
| 165 | if (err) |
| 166 | return err; |
| 167 | ipage = dn.inode_page; |
| 168 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 169 | f2fs_wait_on_page_writeback(ipage, NODE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 170 | zero_user_segment(ipage, INLINE_DATA_OFFSET, |
| 171 | INLINE_DATA_OFFSET + MAX_INLINE_DATA); |
| 172 | src_addr = kmap(page); |
| 173 | dst_addr = inline_data_addr(ipage); |
| 174 | memcpy(dst_addr, src_addr, size); |
| 175 | kunmap(page); |
| 176 | |
| 177 | /* Release the first data block if it is allocated */ |
| 178 | if (!f2fs_has_inline_data(inode)) { |
| 179 | truncate_data_blocks_range(&dn, 1); |
| 180 | set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
Jaegeuk Kim | 0dbdc2a | 2013-11-26 11:08:57 +0900 | [diff] [blame] | 181 | stat_inc_inline_inode(inode); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 184 | set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 185 | sync_inode_page(&dn); |
| 186 | f2fs_put_dnode(&dn); |
| 187 | |
| 188 | return 0; |
| 189 | } |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 190 | |
Chao Yu | 8aa6f1c | 2014-04-29 09:03:03 +0800 | [diff] [blame] | 191 | void truncate_inline_data(struct inode *inode, u64 from) |
| 192 | { |
Chao Yu | 8aa6f1c | 2014-04-29 09:03:03 +0800 | [diff] [blame] | 193 | struct page *ipage; |
| 194 | |
| 195 | if (from >= MAX_INLINE_DATA) |
| 196 | return; |
| 197 | |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 198 | ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Chao Yu | 8aa6f1c | 2014-04-29 09:03:03 +0800 | [diff] [blame] | 199 | if (IS_ERR(ipage)) |
| 200 | return; |
| 201 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 202 | f2fs_wait_on_page_writeback(ipage, NODE); |
| 203 | |
Chao Yu | 8aa6f1c | 2014-04-29 09:03:03 +0800 | [diff] [blame] | 204 | zero_user_segment(ipage, INLINE_DATA_OFFSET + from, |
| 205 | INLINE_DATA_OFFSET + MAX_INLINE_DATA); |
| 206 | set_page_dirty(ipage); |
| 207 | f2fs_put_page(ipage, 1); |
| 208 | } |
| 209 | |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 210 | bool recover_inline_data(struct inode *inode, struct page *npage) |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 211 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 212 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 213 | struct f2fs_inode *ri = NULL; |
| 214 | void *src_addr, *dst_addr; |
| 215 | struct page *ipage; |
| 216 | |
| 217 | /* |
| 218 | * The inline_data recovery policy is as follows. |
| 219 | * [prev.] [next] of inline_data flag |
| 220 | * o o -> recover inline_data |
| 221 | * o x -> remove inline_data, and then recover data blocks |
| 222 | * x o -> remove inline_data, and then recover inline_data |
| 223 | * x x -> recover data blocks |
| 224 | */ |
| 225 | if (IS_INODE(npage)) |
| 226 | ri = F2FS_INODE(npage); |
| 227 | |
| 228 | if (f2fs_has_inline_data(inode) && |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 229 | ri && (ri->i_inline & F2FS_INLINE_DATA)) { |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 230 | process_inline: |
| 231 | ipage = get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 232 | f2fs_bug_on(sbi, IS_ERR(ipage)); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 233 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 234 | f2fs_wait_on_page_writeback(ipage, NODE); |
| 235 | |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 236 | src_addr = inline_data_addr(npage); |
| 237 | dst_addr = inline_data_addr(ipage); |
| 238 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
| 239 | update_inode(inode, ipage); |
| 240 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 241 | return true; |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if (f2fs_has_inline_data(inode)) { |
| 245 | ipage = get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 246 | f2fs_bug_on(sbi, IS_ERR(ipage)); |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 247 | f2fs_wait_on_page_writeback(ipage, NODE); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 248 | zero_user_segment(ipage, INLINE_DATA_OFFSET, |
| 249 | INLINE_DATA_OFFSET + MAX_INLINE_DATA); |
| 250 | clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
| 251 | update_inode(inode, ipage); |
| 252 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 253 | } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 254 | truncate_blocks(inode, 0, false); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 255 | set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
| 256 | goto process_inline; |
| 257 | } |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 258 | return false; |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 259 | } |