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