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); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame^] | 58 | dst_addr = kmap_atomic(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 59 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame^] | 60 | kunmap_atomic(dst_addr); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 61 | f2fs_put_page(ipage, 1); |
Chao Yu | 04a17fb | 2013-12-30 18:36:23 +0800 | [diff] [blame] | 62 | out: |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 63 | SetPageUptodate(page); |
| 64 | unlock_page(page); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int __f2fs_convert_inline_data(struct inode *inode, struct page *page) |
| 70 | { |
Jaegeuk Kim | ec4e7af | 2014-08-18 14:41:11 -0700 | [diff] [blame] | 71 | int err = 0; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 72 | struct page *ipage; |
| 73 | struct dnode_of_data dn; |
| 74 | void *src_addr, *dst_addr; |
| 75 | block_t new_blk_addr; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 76 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 77 | struct f2fs_io_info fio = { |
| 78 | .type = DATA, |
| 79 | .rw = WRITE_SYNC | REQ_PRIO, |
| 80 | }; |
| 81 | |
| 82 | f2fs_lock_op(sbi); |
| 83 | ipage = get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 15c6e3a | 2014-04-16 14:22:50 +0900 | [diff] [blame] | 84 | if (IS_ERR(ipage)) { |
| 85 | err = PTR_ERR(ipage); |
| 86 | goto out; |
| 87 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 88 | |
Jaegeuk Kim | ec4e7af | 2014-08-18 14:41:11 -0700 | [diff] [blame] | 89 | /* someone else converted inline_data already */ |
| 90 | if (!f2fs_has_inline_data(inode)) |
| 91 | goto out; |
| 92 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 93 | /* |
| 94 | * i_addr[0] is not used for inline data, |
| 95 | * so reserving new block will not destroy inline data |
| 96 | */ |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 97 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 98 | err = f2fs_reserve_block(&dn, 0); |
Jaegeuk Kim | 15c6e3a | 2014-04-16 14:22:50 +0900 | [diff] [blame] | 99 | if (err) |
| 100 | goto out; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 101 | |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 102 | f2fs_wait_on_page_writeback(page, DATA); |
Chao Yu | 18309aa | 2013-12-30 09:29:06 +0800 | [diff] [blame] | 103 | zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 104 | |
| 105 | /* Copy the whole inline data block */ |
| 106 | src_addr = inline_data_addr(ipage); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame^] | 107 | dst_addr = kmap_atomic(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 108 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame^] | 109 | kunmap_atomic(dst_addr); |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 110 | SetPageUptodate(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 111 | |
| 112 | /* write data page to try to make data consistent */ |
| 113 | set_page_writeback(page); |
| 114 | write_data_page(page, &dn, &new_blk_addr, &fio); |
| 115 | update_extent_cache(new_blk_addr, &dn); |
Yuan Zhong | 5514f0a | 2014-01-10 07:26:14 +0000 | [diff] [blame] | 116 | f2fs_wait_on_page_writeback(page, DATA); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 117 | |
| 118 | /* clear inline data and flag after data writeback */ |
| 119 | zero_user_segment(ipage, INLINE_DATA_OFFSET, |
| 120 | INLINE_DATA_OFFSET + MAX_INLINE_DATA); |
| 121 | clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
Jaegeuk Kim | 0dbdc2a | 2013-11-26 11:08:57 +0900 | [diff] [blame] | 122 | stat_dec_inline_inode(inode); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 123 | |
| 124 | sync_inode_page(&dn); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 125 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 15c6e3a | 2014-04-16 14:22:50 +0900 | [diff] [blame] | 126 | out: |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 127 | f2fs_unlock_op(sbi); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 128 | return err; |
| 129 | } |
| 130 | |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 131 | int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size, |
| 132 | struct page *page) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 133 | { |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 134 | struct page *new_page = page; |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 135 | int err; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 136 | |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 137 | if (!f2fs_has_inline_data(inode)) |
| 138 | return 0; |
| 139 | else if (to_size <= MAX_INLINE_DATA) |
| 140 | return 0; |
| 141 | |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 142 | if (!page || page->index != 0) { |
| 143 | new_page = grab_cache_page(inode->i_mapping, 0); |
| 144 | if (!new_page) |
| 145 | return -ENOMEM; |
| 146 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 147 | |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 148 | err = __f2fs_convert_inline_data(inode, new_page); |
| 149 | if (!page || page->index != 0) |
| 150 | f2fs_put_page(new_page, 1); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 151 | return err; |
| 152 | } |
| 153 | |
| 154 | int f2fs_write_inline_data(struct inode *inode, |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 155 | struct page *page, unsigned size) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 156 | { |
| 157 | void *src_addr, *dst_addr; |
| 158 | struct page *ipage; |
| 159 | struct dnode_of_data dn; |
| 160 | int err; |
| 161 | |
| 162 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 163 | err = get_dnode_of_data(&dn, 0, LOOKUP_NODE); |
| 164 | if (err) |
| 165 | return err; |
| 166 | ipage = dn.inode_page; |
| 167 | |
Jaegeuk Kim | c08a690 | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 168 | /* Release any data block if it is allocated */ |
| 169 | if (!f2fs_has_inline_data(inode)) { |
| 170 | int count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
| 171 | truncate_data_blocks_range(&dn, count); |
| 172 | set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
| 173 | stat_inc_inline_inode(inode); |
| 174 | } |
| 175 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 176 | f2fs_wait_on_page_writeback(ipage, NODE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 177 | zero_user_segment(ipage, INLINE_DATA_OFFSET, |
| 178 | INLINE_DATA_OFFSET + MAX_INLINE_DATA); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame^] | 179 | src_addr = kmap_atomic(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 180 | dst_addr = inline_data_addr(ipage); |
| 181 | memcpy(dst_addr, src_addr, size); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame^] | 182 | kunmap_atomic(src_addr); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 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 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 260 | |
| 261 | struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir, |
| 262 | struct qstr *name, struct page **res_page) |
| 263 | { |
| 264 | struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 265 | struct f2fs_inline_dentry *inline_dentry; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 266 | struct f2fs_dir_entry *de; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 267 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 268 | struct page *ipage; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 269 | |
| 270 | ipage = get_node_page(sbi, dir->i_ino); |
| 271 | if (IS_ERR(ipage)) |
| 272 | return NULL; |
| 273 | |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 274 | inline_dentry = inline_data_addr(ipage); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 275 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 276 | make_dentry_ptr(&d, (void *)inline_dentry, 2); |
| 277 | de = find_target_dentry(name, NULL, &d); |
| 278 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 279 | unlock_page(ipage); |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 280 | if (de) |
| 281 | *res_page = ipage; |
| 282 | else |
| 283 | f2fs_put_page(ipage, 0); |
| 284 | |
| 285 | /* |
| 286 | * For the most part, it should be a bug when name_len is zero. |
| 287 | * We stop here for figuring out where the bugs has occurred. |
| 288 | */ |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 289 | f2fs_bug_on(sbi, d.max < 0); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 290 | return de; |
| 291 | } |
| 292 | |
| 293 | struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir, |
| 294 | struct page **p) |
| 295 | { |
| 296 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 297 | struct page *ipage; |
| 298 | struct f2fs_dir_entry *de; |
| 299 | struct f2fs_inline_dentry *dentry_blk; |
| 300 | |
| 301 | ipage = get_node_page(sbi, dir->i_ino); |
| 302 | if (IS_ERR(ipage)) |
| 303 | return NULL; |
| 304 | |
| 305 | dentry_blk = inline_data_addr(ipage); |
| 306 | de = &dentry_blk->dentry[1]; |
| 307 | *p = ipage; |
| 308 | unlock_page(ipage); |
| 309 | return de; |
| 310 | } |
| 311 | |
| 312 | int make_empty_inline_dir(struct inode *inode, struct inode *parent, |
| 313 | struct page *ipage) |
| 314 | { |
| 315 | struct f2fs_inline_dentry *dentry_blk; |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 316 | struct f2fs_dentry_ptr d; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 317 | |
| 318 | dentry_blk = inline_data_addr(ipage); |
| 319 | |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 320 | make_dentry_ptr(&d, (void *)dentry_blk, 2); |
| 321 | do_make_empty_dir(inode, parent, &d); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 322 | |
| 323 | set_page_dirty(ipage); |
| 324 | |
| 325 | /* update i_size to MAX_INLINE_DATA */ |
| 326 | if (i_size_read(inode) < MAX_INLINE_DATA) { |
| 327 | i_size_write(inode, MAX_INLINE_DATA); |
| 328 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 333 | int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, |
| 334 | struct f2fs_inline_dentry *inline_dentry) |
| 335 | { |
| 336 | struct page *page; |
| 337 | struct dnode_of_data dn; |
| 338 | struct f2fs_dentry_block *dentry_blk; |
| 339 | int err; |
| 340 | |
| 341 | page = grab_cache_page(dir->i_mapping, 0); |
| 342 | if (!page) |
| 343 | return -ENOMEM; |
| 344 | |
| 345 | set_new_dnode(&dn, dir, ipage, NULL, 0); |
| 346 | err = f2fs_reserve_block(&dn, 0); |
| 347 | if (err) |
| 348 | goto out; |
| 349 | |
| 350 | f2fs_wait_on_page_writeback(page, DATA); |
| 351 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 352 | |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame^] | 353 | dentry_blk = kmap_atomic(page); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 354 | |
| 355 | /* copy data from inline dentry block to new dentry block */ |
| 356 | memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap, |
| 357 | INLINE_DENTRY_BITMAP_SIZE); |
| 358 | memcpy(dentry_blk->dentry, inline_dentry->dentry, |
| 359 | sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY); |
| 360 | memcpy(dentry_blk->filename, inline_dentry->filename, |
| 361 | NR_INLINE_DENTRY * F2FS_SLOT_LEN); |
| 362 | |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame^] | 363 | kunmap_atomic(dentry_blk); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 364 | SetPageUptodate(page); |
| 365 | set_page_dirty(page); |
| 366 | |
| 367 | /* clear inline dir and flag after data writeback */ |
| 368 | zero_user_segment(ipage, INLINE_DATA_OFFSET, |
| 369 | INLINE_DATA_OFFSET + MAX_INLINE_DATA); |
Jaegeuk Kim | 3289c06 | 2014-10-13 20:00:16 -0700 | [diff] [blame] | 370 | stat_dec_inline_dir(dir); |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 371 | clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 372 | |
| 373 | if (i_size_read(dir) < PAGE_CACHE_SIZE) { |
| 374 | i_size_write(dir, PAGE_CACHE_SIZE); |
| 375 | set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
| 376 | } |
| 377 | |
| 378 | sync_inode_page(&dn); |
| 379 | out: |
| 380 | f2fs_put_page(page, 1); |
| 381 | return err; |
| 382 | } |
| 383 | |
| 384 | int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name, |
| 385 | struct inode *inode) |
| 386 | { |
| 387 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 388 | struct page *ipage; |
| 389 | unsigned int bit_pos; |
| 390 | f2fs_hash_t name_hash; |
| 391 | struct f2fs_dir_entry *de; |
| 392 | size_t namelen = name->len; |
| 393 | struct f2fs_inline_dentry *dentry_blk = NULL; |
| 394 | int slots = GET_DENTRY_SLOTS(namelen); |
| 395 | struct page *page; |
| 396 | int err = 0; |
| 397 | int i; |
| 398 | |
| 399 | name_hash = f2fs_dentry_hash(name); |
| 400 | |
| 401 | ipage = get_node_page(sbi, dir->i_ino); |
| 402 | if (IS_ERR(ipage)) |
| 403 | return PTR_ERR(ipage); |
| 404 | |
| 405 | dentry_blk = inline_data_addr(ipage); |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 406 | bit_pos = room_for_filename(&dentry_blk->dentry_bitmap, |
| 407 | slots, NR_INLINE_DENTRY); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 408 | if (bit_pos >= NR_INLINE_DENTRY) { |
| 409 | err = f2fs_convert_inline_dir(dir, ipage, dentry_blk); |
| 410 | if (!err) |
| 411 | err = -EAGAIN; |
| 412 | goto out; |
| 413 | } |
| 414 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 415 | down_write(&F2FS_I(inode)->i_sem); |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 416 | page = init_inode_metadata(inode, dir, name, ipage); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 417 | if (IS_ERR(page)) { |
| 418 | err = PTR_ERR(page); |
| 419 | goto fail; |
| 420 | } |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 421 | |
| 422 | f2fs_wait_on_page_writeback(ipage, NODE); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 423 | de = &dentry_blk->dentry[bit_pos]; |
| 424 | de->hash_code = name_hash; |
| 425 | de->name_len = cpu_to_le16(namelen); |
| 426 | memcpy(dentry_blk->filename[bit_pos], name->name, name->len); |
| 427 | de->ino = cpu_to_le32(inode->i_ino); |
| 428 | set_de_type(de, inode); |
| 429 | for (i = 0; i < slots; i++) |
| 430 | test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); |
| 431 | set_page_dirty(ipage); |
| 432 | |
| 433 | /* we don't need to mark_inode_dirty now */ |
| 434 | F2FS_I(inode)->i_pino = dir->i_ino; |
| 435 | update_inode(inode, page); |
| 436 | f2fs_put_page(page, 1); |
| 437 | |
| 438 | update_parent_metadata(dir, inode, 0); |
| 439 | fail: |
| 440 | up_write(&F2FS_I(inode)->i_sem); |
| 441 | |
| 442 | if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) { |
| 443 | update_inode(dir, ipage); |
| 444 | clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
| 445 | } |
| 446 | out: |
| 447 | f2fs_put_page(ipage, 1); |
| 448 | return err; |
| 449 | } |
| 450 | |
| 451 | void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, |
| 452 | struct inode *dir, struct inode *inode) |
| 453 | { |
| 454 | struct f2fs_inline_dentry *inline_dentry; |
| 455 | int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); |
| 456 | unsigned int bit_pos; |
| 457 | int i; |
| 458 | |
| 459 | lock_page(page); |
Jaegeuk Kim | 59a0615 | 2014-10-13 19:34:26 -0700 | [diff] [blame] | 460 | f2fs_wait_on_page_writeback(page, NODE); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 461 | |
| 462 | inline_dentry = inline_data_addr(page); |
| 463 | bit_pos = dentry - inline_dentry->dentry; |
| 464 | for (i = 0; i < slots; i++) |
| 465 | test_and_clear_bit_le(bit_pos + i, |
| 466 | &inline_dentry->dentry_bitmap); |
| 467 | |
| 468 | set_page_dirty(page); |
| 469 | |
| 470 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 471 | |
| 472 | if (inode) |
| 473 | f2fs_drop_nlink(dir, inode, page); |
| 474 | |
| 475 | f2fs_put_page(page, 1); |
| 476 | } |
| 477 | |
| 478 | bool f2fs_empty_inline_dir(struct inode *dir) |
| 479 | { |
| 480 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 481 | struct page *ipage; |
| 482 | unsigned int bit_pos = 2; |
| 483 | struct f2fs_inline_dentry *dentry_blk; |
| 484 | |
| 485 | ipage = get_node_page(sbi, dir->i_ino); |
| 486 | if (IS_ERR(ipage)) |
| 487 | return false; |
| 488 | |
| 489 | dentry_blk = inline_data_addr(ipage); |
| 490 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 491 | NR_INLINE_DENTRY, |
| 492 | bit_pos); |
| 493 | |
| 494 | f2fs_put_page(ipage, 1); |
| 495 | |
| 496 | if (bit_pos < NR_INLINE_DENTRY) |
| 497 | return false; |
| 498 | |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx) |
| 503 | { |
| 504 | struct inode *inode = file_inode(file); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 505 | struct f2fs_inline_dentry *inline_dentry = NULL; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 506 | struct page *ipage = NULL; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 507 | struct f2fs_dentry_ptr d; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 508 | |
| 509 | if (ctx->pos == NR_INLINE_DENTRY) |
| 510 | return 0; |
| 511 | |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 512 | ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 513 | if (IS_ERR(ipage)) |
| 514 | return PTR_ERR(ipage); |
| 515 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 516 | inline_dentry = inline_data_addr(ipage); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 517 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 518 | make_dentry_ptr(&d, (void *)inline_dentry, 2); |
| 519 | |
| 520 | if (!f2fs_fill_dentries(ctx, &d, 0)) |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 521 | ctx->pos = NR_INLINE_DENTRY; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 522 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 523 | f2fs_put_page(ipage, 1); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 524 | return 0; |
| 525 | } |