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 | |
Jaegeuk Kim | 01b960e | 2015-04-23 10:27:21 -0700 | [diff] [blame^] | 16 | bool f2fs_may_inline_data(struct inode *inode) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 17 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 18 | if (!test_opt(F2FS_I_SB(inode), INLINE_DATA)) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 19 | return false; |
| 20 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 21 | if (f2fs_is_atomic_file(inode)) |
| 22 | return false; |
| 23 | |
Wanpeng Li | 368a0e4 | 2015-03-19 13:23:48 +0800 | [diff] [blame] | 24 | if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode)) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 25 | return false; |
| 26 | |
Jaegeuk Kim | 92dffd0 | 2014-11-11 14:10:01 -0800 | [diff] [blame] | 27 | if (i_size_read(inode) > MAX_INLINE_DATA) |
| 28 | return false; |
| 29 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 30 | return true; |
| 31 | } |
| 32 | |
Jaegeuk Kim | 01b960e | 2015-04-23 10:27:21 -0700 | [diff] [blame^] | 33 | bool f2fs_may_inline_dentry(struct inode *inode) |
| 34 | { |
| 35 | if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY)) |
| 36 | return false; |
| 37 | |
| 38 | if (!S_ISDIR(inode->i_mode)) |
| 39 | return false; |
| 40 | |
| 41 | return true; |
| 42 | } |
| 43 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 44 | void read_inline_data(struct page *page, struct page *ipage) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 45 | { |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 46 | void *src_addr, *dst_addr; |
| 47 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 48 | if (PageUptodate(page)) |
| 49 | return; |
Chao Yu | 04a17fb | 2013-12-30 18:36:23 +0800 | [diff] [blame] | 50 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 51 | f2fs_bug_on(F2FS_P_SB(page), page->index); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 52 | |
Chao Yu | 18309aa | 2013-12-30 09:29:06 +0800 | [diff] [blame] | 53 | zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 54 | |
| 55 | /* Copy the whole inline data block */ |
| 56 | src_addr = inline_data_addr(ipage); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 57 | dst_addr = kmap_atomic(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 58 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
Jaegeuk Kim | 427a45c | 2014-10-26 22:59:27 -0700 | [diff] [blame] | 59 | flush_dcache_page(page); |
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 | SetPageUptodate(page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 62 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 63 | |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 64 | bool truncate_inline_inode(struct page *ipage, u64 from) |
Chao Yu | feeb0de | 2015-01-26 20:22:55 +0800 | [diff] [blame] | 65 | { |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 66 | void *addr; |
| 67 | |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 68 | if (from >= MAX_INLINE_DATA) |
| 69 | return false; |
| 70 | |
| 71 | addr = inline_data_addr(ipage); |
| 72 | |
Chao Yu | feeb0de | 2015-01-26 20:22:55 +0800 | [diff] [blame] | 73 | f2fs_wait_on_page_writeback(ipage, NODE); |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 74 | memset(addr + from, 0, MAX_INLINE_DATA - from); |
| 75 | |
| 76 | return true; |
Chao Yu | feeb0de | 2015-01-26 20:22:55 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 79 | int f2fs_read_inline_data(struct inode *inode, struct page *page) |
| 80 | { |
| 81 | struct page *ipage; |
| 82 | |
| 83 | ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); |
| 84 | if (IS_ERR(ipage)) { |
| 85 | unlock_page(page); |
| 86 | return PTR_ERR(ipage); |
| 87 | } |
| 88 | |
| 89 | if (!f2fs_has_inline_data(inode)) { |
| 90 | f2fs_put_page(ipage, 1); |
| 91 | return -EAGAIN; |
| 92 | } |
| 93 | |
| 94 | if (page->index) |
| 95 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 96 | else |
| 97 | read_inline_data(page, ipage); |
| 98 | |
| 99 | SetPageUptodate(page); |
| 100 | f2fs_put_page(ipage, 1); |
| 101 | unlock_page(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 105 | 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] | 106 | { |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 107 | void *src_addr, *dst_addr; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 108 | struct f2fs_io_info fio = { |
| 109 | .type = DATA, |
| 110 | .rw = WRITE_SYNC | REQ_PRIO, |
| 111 | }; |
Jaegeuk Kim | 158c194c | 2014-11-25 11:34:02 -0800 | [diff] [blame] | 112 | int dirty, err; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 113 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 114 | f2fs_bug_on(F2FS_I_SB(dn->inode), page->index); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 115 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 116 | if (!f2fs_exist_data(dn->inode)) |
| 117 | goto clear_out; |
Jaegeuk Kim | ec4e7af | 2014-08-18 14:41:11 -0700 | [diff] [blame] | 118 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 119 | err = f2fs_reserve_block(dn, 0); |
Jaegeuk Kim | 15c6e3a | 2014-04-16 14:22:50 +0900 | [diff] [blame] | 120 | if (err) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 121 | return err; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 122 | |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 123 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 124 | |
| 125 | if (PageUptodate(page)) |
| 126 | goto no_update; |
| 127 | |
Chao Yu | 18309aa | 2013-12-30 09:29:06 +0800 | [diff] [blame] | 128 | zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 129 | |
| 130 | /* Copy the whole inline data block */ |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 131 | src_addr = inline_data_addr(dn->inode_page); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 132 | dst_addr = kmap_atomic(page); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 133 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
Jaegeuk Kim | 09b8b3c | 2014-11-18 10:50:21 -0800 | [diff] [blame] | 134 | flush_dcache_page(page); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 135 | kunmap_atomic(dst_addr); |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 136 | SetPageUptodate(page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 137 | no_update: |
Jaegeuk Kim | 158c194c | 2014-11-25 11:34:02 -0800 | [diff] [blame] | 138 | /* clear dirty state */ |
| 139 | dirty = clear_page_dirty_for_io(page); |
| 140 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 141 | /* write data page to try to make data consistent */ |
| 142 | set_page_writeback(page); |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 143 | fio.blk_addr = dn->data_blkaddr; |
| 144 | write_data_page(page, dn, &fio); |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 145 | set_data_blkaddr(dn); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 146 | f2fs_update_extent_cache(dn); |
Yuan Zhong | 5514f0a | 2014-01-10 07:26:14 +0000 | [diff] [blame] | 147 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | 158c194c | 2014-11-25 11:34:02 -0800 | [diff] [blame] | 148 | if (dirty) |
| 149 | inode_dec_dirty_pages(dn->inode); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 150 | |
Jaegeuk Kim | 95f5b0f | 2014-11-25 17:27:38 -0800 | [diff] [blame] | 151 | /* this converted inline_data should be recovered. */ |
| 152 | set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE); |
| 153 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 154 | /* clear inline data and flag after data writeback */ |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 155 | truncate_inline_inode(dn->inode_page, 0); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 156 | clear_out: |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 157 | stat_dec_inline_inode(dn->inode); |
Jaegeuk Kim | 57e2a2c | 2014-11-10 16:29:14 -0800 | [diff] [blame] | 158 | f2fs_clear_inline_inode(dn->inode); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 159 | sync_inode_page(dn); |
| 160 | f2fs_put_dnode(dn); |
| 161 | return 0; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 162 | } |
| 163 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 164 | int f2fs_convert_inline_inode(struct inode *inode) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 165 | { |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 166 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 167 | struct dnode_of_data dn; |
| 168 | struct page *ipage, *page; |
| 169 | int err = 0; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 170 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 171 | page = grab_cache_page(inode->i_mapping, 0); |
| 172 | if (!page) |
| 173 | return -ENOMEM; |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 174 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 175 | f2fs_lock_op(sbi); |
| 176 | |
| 177 | ipage = get_node_page(sbi, inode->i_ino); |
| 178 | if (IS_ERR(ipage)) { |
Jaegeuk Kim | 6d20aff | 2014-11-17 16:06:55 -0800 | [diff] [blame] | 179 | err = PTR_ERR(ipage); |
| 180 | goto out; |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 181 | } |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 182 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 183 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
| 184 | |
| 185 | if (f2fs_has_inline_data(inode)) |
| 186 | err = f2fs_convert_inline_page(&dn, page); |
| 187 | |
| 188 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 6d20aff | 2014-11-17 16:06:55 -0800 | [diff] [blame] | 189 | out: |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 190 | f2fs_unlock_op(sbi); |
| 191 | |
| 192 | f2fs_put_page(page, 1); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 193 | return err; |
| 194 | } |
| 195 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 196 | int f2fs_write_inline_data(struct inode *inode, struct page *page) |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 197 | { |
| 198 | void *src_addr, *dst_addr; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 199 | struct dnode_of_data dn; |
| 200 | int err; |
| 201 | |
| 202 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 203 | err = get_dnode_of_data(&dn, 0, LOOKUP_NODE); |
| 204 | if (err) |
| 205 | return err; |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 206 | |
Jaegeuk Kim | c08a690 | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 207 | if (!f2fs_has_inline_data(inode)) { |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 208 | f2fs_put_dnode(&dn); |
| 209 | return -EAGAIN; |
Jaegeuk Kim | c08a690 | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 212 | f2fs_bug_on(F2FS_I_SB(inode), page->index); |
| 213 | |
| 214 | f2fs_wait_on_page_writeback(dn.inode_page, NODE); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 215 | src_addr = kmap_atomic(page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 216 | dst_addr = inline_data_addr(dn.inode_page); |
| 217 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 218 | kunmap_atomic(src_addr); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 219 | |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 220 | set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 221 | set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); |
| 222 | |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 223 | sync_inode_page(&dn); |
| 224 | f2fs_put_dnode(&dn); |
Huajun Li | e18c65b | 2013-11-10 23:13:19 +0800 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 227 | |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 228 | bool recover_inline_data(struct inode *inode, struct page *npage) |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 229 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 230 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 231 | struct f2fs_inode *ri = NULL; |
| 232 | void *src_addr, *dst_addr; |
| 233 | struct page *ipage; |
| 234 | |
| 235 | /* |
| 236 | * The inline_data recovery policy is as follows. |
| 237 | * [prev.] [next] of inline_data flag |
| 238 | * o o -> recover inline_data |
| 239 | * o x -> remove inline_data, and then recover data blocks |
| 240 | * x o -> remove inline_data, and then recover inline_data |
| 241 | * x x -> recover data blocks |
| 242 | */ |
| 243 | if (IS_INODE(npage)) |
| 244 | ri = F2FS_INODE(npage); |
| 245 | |
| 246 | if (f2fs_has_inline_data(inode) && |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 247 | ri && (ri->i_inline & F2FS_INLINE_DATA)) { |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 248 | process_inline: |
| 249 | ipage = get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 250 | f2fs_bug_on(sbi, IS_ERR(ipage)); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 251 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 252 | f2fs_wait_on_page_writeback(ipage, NODE); |
| 253 | |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 254 | src_addr = inline_data_addr(npage); |
| 255 | dst_addr = inline_data_addr(ipage); |
| 256 | memcpy(dst_addr, src_addr, MAX_INLINE_DATA); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 257 | |
| 258 | set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
| 259 | set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); |
| 260 | |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 261 | update_inode(inode, ipage); |
| 262 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 263 | return true; |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | if (f2fs_has_inline_data(inode)) { |
| 267 | ipage = get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 268 | f2fs_bug_on(sbi, IS_ERR(ipage)); |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 269 | truncate_inline_inode(ipage, 0); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 270 | f2fs_clear_inline_inode(inode); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 271 | update_inode(inode, ipage); |
| 272 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 273 | } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 274 | truncate_blocks(inode, 0, false); |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 275 | goto process_inline; |
| 276 | } |
Jaegeuk Kim | 0342fd3 | 2014-08-07 16:57:17 -0700 | [diff] [blame] | 277 | return false; |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 278 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 279 | |
| 280 | struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir, |
| 281 | struct qstr *name, struct page **res_page) |
| 282 | { |
| 283 | struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 284 | struct f2fs_inline_dentry *inline_dentry; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 285 | struct f2fs_dir_entry *de; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 286 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 287 | struct page *ipage; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 288 | |
| 289 | ipage = get_node_page(sbi, dir->i_ino); |
| 290 | if (IS_ERR(ipage)) |
| 291 | return NULL; |
| 292 | |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 293 | inline_dentry = inline_data_addr(ipage); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 294 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 295 | make_dentry_ptr(&d, (void *)inline_dentry, 2); |
| 296 | de = find_target_dentry(name, NULL, &d); |
| 297 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 298 | unlock_page(ipage); |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 299 | if (de) |
| 300 | *res_page = ipage; |
| 301 | else |
| 302 | f2fs_put_page(ipage, 0); |
| 303 | |
| 304 | /* |
| 305 | * For the most part, it should be a bug when name_len is zero. |
| 306 | * We stop here for figuring out where the bugs has occurred. |
| 307 | */ |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 308 | f2fs_bug_on(sbi, d.max < 0); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 309 | return de; |
| 310 | } |
| 311 | |
| 312 | struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir, |
| 313 | struct page **p) |
| 314 | { |
| 315 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 316 | struct page *ipage; |
| 317 | struct f2fs_dir_entry *de; |
| 318 | struct f2fs_inline_dentry *dentry_blk; |
| 319 | |
| 320 | ipage = get_node_page(sbi, dir->i_ino); |
| 321 | if (IS_ERR(ipage)) |
| 322 | return NULL; |
| 323 | |
| 324 | dentry_blk = inline_data_addr(ipage); |
| 325 | de = &dentry_blk->dentry[1]; |
| 326 | *p = ipage; |
| 327 | unlock_page(ipage); |
| 328 | return de; |
| 329 | } |
| 330 | |
| 331 | int make_empty_inline_dir(struct inode *inode, struct inode *parent, |
| 332 | struct page *ipage) |
| 333 | { |
| 334 | struct f2fs_inline_dentry *dentry_blk; |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 335 | struct f2fs_dentry_ptr d; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 336 | |
| 337 | dentry_blk = inline_data_addr(ipage); |
| 338 | |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 339 | make_dentry_ptr(&d, (void *)dentry_blk, 2); |
| 340 | do_make_empty_dir(inode, parent, &d); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 341 | |
| 342 | set_page_dirty(ipage); |
| 343 | |
| 344 | /* update i_size to MAX_INLINE_DATA */ |
| 345 | if (i_size_read(inode) < MAX_INLINE_DATA) { |
| 346 | i_size_write(inode, MAX_INLINE_DATA); |
| 347 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
Jaegeuk Kim | d64948a | 2014-10-20 20:28:49 -0700 | [diff] [blame] | 352 | static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 353 | struct f2fs_inline_dentry *inline_dentry) |
| 354 | { |
| 355 | struct page *page; |
| 356 | struct dnode_of_data dn; |
| 357 | struct f2fs_dentry_block *dentry_blk; |
| 358 | int err; |
| 359 | |
| 360 | page = grab_cache_page(dir->i_mapping, 0); |
| 361 | if (!page) |
| 362 | return -ENOMEM; |
| 363 | |
| 364 | set_new_dnode(&dn, dir, ipage, NULL, 0); |
| 365 | err = f2fs_reserve_block(&dn, 0); |
| 366 | if (err) |
| 367 | goto out; |
| 368 | |
| 369 | f2fs_wait_on_page_writeback(page, DATA); |
| 370 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 371 | |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 372 | dentry_blk = kmap_atomic(page); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 373 | |
| 374 | /* copy data from inline dentry block to new dentry block */ |
| 375 | memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap, |
| 376 | INLINE_DENTRY_BITMAP_SIZE); |
| 377 | memcpy(dentry_blk->dentry, inline_dentry->dentry, |
| 378 | sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY); |
| 379 | memcpy(dentry_blk->filename, inline_dentry->filename, |
| 380 | NR_INLINE_DENTRY * F2FS_SLOT_LEN); |
| 381 | |
Jaegeuk Kim | f1e33a0 | 2014-10-18 23:41:38 -0700 | [diff] [blame] | 382 | kunmap_atomic(dentry_blk); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 383 | SetPageUptodate(page); |
| 384 | set_page_dirty(page); |
| 385 | |
| 386 | /* clear inline dir and flag after data writeback */ |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 387 | truncate_inline_inode(ipage, 0); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 388 | |
Jaegeuk Kim | 3289c06 | 2014-10-13 20:00:16 -0700 | [diff] [blame] | 389 | stat_dec_inline_dir(dir); |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 390 | clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 391 | |
| 392 | if (i_size_read(dir) < PAGE_CACHE_SIZE) { |
| 393 | i_size_write(dir, PAGE_CACHE_SIZE); |
| 394 | set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
| 395 | } |
| 396 | |
| 397 | sync_inode_page(&dn); |
| 398 | out: |
| 399 | f2fs_put_page(page, 1); |
| 400 | return err; |
| 401 | } |
| 402 | |
| 403 | int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name, |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 404 | struct inode *inode, nid_t ino, umode_t mode) |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 405 | { |
| 406 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 407 | struct page *ipage; |
| 408 | unsigned int bit_pos; |
| 409 | f2fs_hash_t name_hash; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 410 | size_t namelen = name->len; |
| 411 | struct f2fs_inline_dentry *dentry_blk = NULL; |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 412 | struct f2fs_dentry_ptr d; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 413 | int slots = GET_DENTRY_SLOTS(namelen); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 414 | struct page *page = NULL; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 415 | int err = 0; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 416 | |
| 417 | ipage = get_node_page(sbi, dir->i_ino); |
| 418 | if (IS_ERR(ipage)) |
| 419 | return PTR_ERR(ipage); |
| 420 | |
| 421 | dentry_blk = inline_data_addr(ipage); |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 422 | bit_pos = room_for_filename(&dentry_blk->dentry_bitmap, |
| 423 | slots, NR_INLINE_DENTRY); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 424 | if (bit_pos >= NR_INLINE_DENTRY) { |
| 425 | err = f2fs_convert_inline_dir(dir, ipage, dentry_blk); |
| 426 | if (!err) |
| 427 | err = -EAGAIN; |
| 428 | goto out; |
| 429 | } |
| 430 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 431 | if (inode) { |
| 432 | down_write(&F2FS_I(inode)->i_sem); |
| 433 | page = init_inode_metadata(inode, dir, name, ipage); |
| 434 | if (IS_ERR(page)) { |
| 435 | err = PTR_ERR(page); |
| 436 | goto fail; |
| 437 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 438 | } |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 439 | |
| 440 | f2fs_wait_on_page_writeback(ipage, NODE); |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 441 | |
| 442 | name_hash = f2fs_dentry_hash(name); |
| 443 | make_dentry_ptr(&d, (void *)dentry_blk, 2); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 444 | f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos); |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 445 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 446 | set_page_dirty(ipage); |
| 447 | |
| 448 | /* we don't need to mark_inode_dirty now */ |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 449 | if (inode) { |
| 450 | F2FS_I(inode)->i_pino = dir->i_ino; |
| 451 | update_inode(inode, page); |
| 452 | f2fs_put_page(page, 1); |
| 453 | } |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 454 | |
| 455 | update_parent_metadata(dir, inode, 0); |
| 456 | fail: |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 457 | if (inode) |
| 458 | up_write(&F2FS_I(inode)->i_sem); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 459 | |
| 460 | if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) { |
| 461 | update_inode(dir, ipage); |
| 462 | clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
| 463 | } |
| 464 | out: |
| 465 | f2fs_put_page(ipage, 1); |
| 466 | return err; |
| 467 | } |
| 468 | |
| 469 | void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, |
| 470 | struct inode *dir, struct inode *inode) |
| 471 | { |
| 472 | struct f2fs_inline_dentry *inline_dentry; |
| 473 | int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); |
| 474 | unsigned int bit_pos; |
| 475 | int i; |
| 476 | |
| 477 | lock_page(page); |
Jaegeuk Kim | 59a0615 | 2014-10-13 19:34:26 -0700 | [diff] [blame] | 478 | f2fs_wait_on_page_writeback(page, NODE); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 479 | |
| 480 | inline_dentry = inline_data_addr(page); |
| 481 | bit_pos = dentry - inline_dentry->dentry; |
| 482 | for (i = 0; i < slots; i++) |
| 483 | test_and_clear_bit_le(bit_pos + i, |
| 484 | &inline_dentry->dentry_bitmap); |
| 485 | |
| 486 | set_page_dirty(page); |
| 487 | |
| 488 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 489 | |
| 490 | if (inode) |
| 491 | f2fs_drop_nlink(dir, inode, page); |
| 492 | |
| 493 | f2fs_put_page(page, 1); |
| 494 | } |
| 495 | |
| 496 | bool f2fs_empty_inline_dir(struct inode *dir) |
| 497 | { |
| 498 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 499 | struct page *ipage; |
| 500 | unsigned int bit_pos = 2; |
| 501 | struct f2fs_inline_dentry *dentry_blk; |
| 502 | |
| 503 | ipage = get_node_page(sbi, dir->i_ino); |
| 504 | if (IS_ERR(ipage)) |
| 505 | return false; |
| 506 | |
| 507 | dentry_blk = inline_data_addr(ipage); |
| 508 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 509 | NR_INLINE_DENTRY, |
| 510 | bit_pos); |
| 511 | |
| 512 | f2fs_put_page(ipage, 1); |
| 513 | |
| 514 | if (bit_pos < NR_INLINE_DENTRY) |
| 515 | return false; |
| 516 | |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx) |
| 521 | { |
| 522 | struct inode *inode = file_inode(file); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 523 | struct f2fs_inline_dentry *inline_dentry = NULL; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 524 | struct page *ipage = NULL; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 525 | struct f2fs_dentry_ptr d; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 526 | |
| 527 | if (ctx->pos == NR_INLINE_DENTRY) |
| 528 | return 0; |
| 529 | |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 530 | ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 531 | if (IS_ERR(ipage)) |
| 532 | return PTR_ERR(ipage); |
| 533 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 534 | inline_dentry = inline_data_addr(ipage); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 535 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 536 | make_dentry_ptr(&d, (void *)inline_dentry, 2); |
| 537 | |
| 538 | if (!f2fs_fill_dentries(ctx, &d, 0)) |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 539 | ctx->pos = NR_INLINE_DENTRY; |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 540 | |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 541 | f2fs_put_page(ipage, 1); |
Chao Yu | 201a05b | 2014-09-24 18:17:53 +0800 | [diff] [blame] | 542 | return 0; |
| 543 | } |