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