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