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