blob: 896db0416f0e6f0f3e172dedd20f61e916eb8e2d [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Huajun Lie18c65b2013-11-10 23:13:19 +08002/*
3 * fs/f2fs/inline.c
4 * Copyright (c) 2013, Intel Corporation
5 * Authors: Huajun Li <huajun.li@intel.com>
6 * Haicheng Li <haicheng.li@intel.com>
Huajun Lie18c65b2013-11-10 23:13:19 +08007 */
8
9#include <linux/fs.h>
10#include <linux/f2fs_fs.h>
11
12#include "f2fs.h"
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -070013#include "node.h"
Huajun Lie18c65b2013-11-10 23:13:19 +080014
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070015bool f2fs_may_inline_data(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +080016{
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070017 if (f2fs_is_atomic_file(inode))
18 return false;
19
Wanpeng Li368a0e42015-03-19 13:23:48 +080020 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
Huajun Lie18c65b2013-11-10 23:13:19 +080021 return false;
22
Chao Yuf2470372017-07-19 00:19:05 +080023 if (i_size_read(inode) > MAX_INLINE_DATA(inode))
Jaegeuk Kim92dffd02014-11-11 14:10:01 -080024 return false;
25
Eric Biggers6dbb1792018-04-18 11:09:48 -070026 if (f2fs_post_read_required(inode))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070027 return false;
28
Huajun Lie18c65b2013-11-10 23:13:19 +080029 return true;
30}
31
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070032bool f2fs_may_inline_dentry(struct inode *inode)
33{
34 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
35 return false;
36
37 if (!S_ISDIR(inode->i_mode))
38 return false;
39
40 return true;
41}
42
Chao Yu4d57b862018-05-30 00:20:41 +080043void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
Huajun Lie18c65b2013-11-10 23:13:19 +080044{
Chao Yuf2470372017-07-19 00:19:05 +080045 struct inode *inode = page->mapping->host;
Huajun Lie18c65b2013-11-10 23:13:19 +080046 void *src_addr, *dst_addr;
47
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070048 if (PageUptodate(page))
49 return;
Chao Yu04a17fb2013-12-30 18:36:23 +080050
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070051 f2fs_bug_on(F2FS_P_SB(page), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080052
Chao Yuf2470372017-07-19 00:19:05 +080053 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080054
55 /* Copy the whole inline data block */
Chao Yuf2470372017-07-19 00:19:05 +080056 src_addr = inline_data_addr(inode, ipage);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070057 dst_addr = kmap_atomic(page);
Chao Yuf2470372017-07-19 00:19:05 +080058 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
Jaegeuk Kim427a45c2014-10-26 22:59:27 -070059 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070060 kunmap_atomic(dst_addr);
Jaegeuk Kim237c0792016-06-30 18:49:15 -070061 if (!PageUptodate(page))
62 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070063}
Huajun Lie18c65b2013-11-10 23:13:19 +080064
Chao Yu4d57b862018-05-30 00:20:41 +080065void f2fs_truncate_inline_inode(struct inode *inode,
66 struct page *ipage, u64 from)
Chao Yufeeb0de2015-01-26 20:22:55 +080067{
Chao Yu0bfcfcc2015-03-10 13:16:25 +080068 void *addr;
69
Chao Yuf2470372017-07-19 00:19:05 +080070 if (from >= MAX_INLINE_DATA(inode))
Kinglong Meebd4667c2017-03-10 20:43:20 +080071 return;
Chao Yu0bfcfcc2015-03-10 13:16:25 +080072
Chao Yuf2470372017-07-19 00:19:05 +080073 addr = inline_data_addr(inode, ipage);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080074
Chao Yubae0ee72018-12-25 17:43:42 +080075 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Chao Yuf2470372017-07-19 00:19:05 +080076 memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -070077 set_page_dirty(ipage);
Kinglong Meebd4667c2017-03-10 20:43:20 +080078
79 if (from == 0)
80 clear_inode_flag(inode, FI_DATA_EXIST);
Chao Yufeeb0de2015-01-26 20:22:55 +080081}
82
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070083int f2fs_read_inline_data(struct inode *inode, struct page *page)
84{
85 struct page *ipage;
86
Chao Yu4d57b862018-05-30 00:20:41 +080087 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070088 if (IS_ERR(ipage)) {
89 unlock_page(page);
90 return PTR_ERR(ipage);
91 }
92
93 if (!f2fs_has_inline_data(inode)) {
94 f2fs_put_page(ipage, 1);
95 return -EAGAIN;
96 }
97
98 if (page->index)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030099 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700100 else
Chao Yu4d57b862018-05-30 00:20:41 +0800101 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700102
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700103 if (!PageUptodate(page))
104 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700105 f2fs_put_page(ipage, 1);
106 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800107 return 0;
108}
109
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700110int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800111{
Huajun Lie18c65b2013-11-10 23:13:19 +0800112 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700113 .sbi = F2FS_I_SB(dn->inode),
Chao Yu39d787b2017-09-29 13:59:38 +0800114 .ino = dn->inode->i_ino,
Huajun Lie18c65b2013-11-10 23:13:19 +0800115 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500116 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600117 .op_flags = REQ_SYNC | REQ_PRIO,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700118 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700119 .encrypted_page = NULL,
Chao Yub0af6d42017-08-02 23:21:48 +0800120 .io_type = FS_DATA_IO,
Huajun Lie18c65b2013-11-10 23:13:19 +0800121 };
Chao Yu77357302018-07-17 00:02:17 +0800122 struct node_info ni;
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800123 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800124
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700125 if (!f2fs_exist_data(dn->inode))
126 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700127
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700128 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900129 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700130 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800131
Chao Yu77357302018-07-17 00:02:17 +0800132 err = f2fs_get_node_info(fio.sbi, dn->nid, &ni);
133 if (err) {
Chao Yue8c82c12019-09-03 10:06:25 +0800134 f2fs_truncate_data_blocks_range(dn, 1);
Chao Yu77357302018-07-17 00:02:17 +0800135 f2fs_put_dnode(dn);
136 return err;
137 }
138
139 fio.version = ni.version;
140
Chao Yu4dbe38d2018-06-30 18:13:40 +0800141 if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
142 f2fs_put_dnode(dn);
143 set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800144 f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
145 __func__, dn->inode->i_ino, dn->data_blkaddr);
Chao Yu10f966b2019-06-20 11:36:14 +0800146 return -EFSCORRUPTED;
Chao Yu4dbe38d2018-06-30 18:13:40 +0800147 }
148
Yunlei He85ead812016-02-03 10:26:13 +0800149 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700150
Chao Yu4d57b862018-05-30 00:20:41 +0800151 f2fs_do_read_inline_data(page, dn->inode_page);
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700152 set_page_dirty(page);
153
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800154 /* clear dirty state */
155 dirty = clear_page_dirty_for_io(page);
156
Huajun Lie18c65b2013-11-10 23:13:19 +0800157 /* write data page to try to make data consistent */
158 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -0700159 ClearPageError(page);
Chao Yu7a9d7542016-02-22 18:36:38 +0800160 fio.old_blkaddr = dn->data_blkaddr;
Jaegeuk Kimef095d12017-03-24 20:05:13 -0400161 set_inode_flag(dn->inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +0800162 f2fs_outplace_write_data(dn, &fio);
Chao Yubae0ee72018-12-25 17:43:42 +0800163 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu933439c2016-10-11 22:57:01 +0800164 if (dirty) {
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800165 inode_dec_dirty_pages(dn->inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800166 f2fs_remove_dirty_inode(dn->inode);
Chao Yu933439c2016-10-11 22:57:01 +0800167 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800168
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800169 /* this converted inline_data should be recovered. */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700170 set_inode_flag(dn->inode, FI_APPEND_WRITE);
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800171
Huajun Lie18c65b2013-11-10 23:13:19 +0800172 /* clear inline data and flag after data writeback */
Chao Yu4d57b862018-05-30 00:20:41 +0800173 f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800174 clear_inline_node(dn->inode_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700175clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700176 stat_dec_inline_inode(dn->inode);
Kinglong Meebd4667c2017-03-10 20:43:20 +0800177 clear_inode_flag(dn->inode, FI_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700178 f2fs_put_dnode(dn);
179 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800180}
181
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700182int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800183{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700184 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
185 struct dnode_of_data dn;
186 struct page *ipage, *page;
187 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800188
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800189 if (!f2fs_has_inline_data(inode))
190 return 0;
191
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700192 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700193 if (!page)
194 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900195
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700196 f2fs_lock_op(sbi);
197
Chao Yu4d57b862018-05-30 00:20:41 +0800198 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700199 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800200 err = PTR_ERR(ipage);
201 goto out;
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700202 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800203
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700204 set_new_dnode(&dn, inode, ipage, ipage, 0);
205
206 if (f2fs_has_inline_data(inode))
207 err = f2fs_convert_inline_page(&dn, page);
208
209 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800210out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700211 f2fs_unlock_op(sbi);
212
213 f2fs_put_page(page, 1);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800214
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800215 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800216
Huajun Lie18c65b2013-11-10 23:13:19 +0800217 return err;
218}
219
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700220int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800221{
222 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800223 struct dnode_of_data dn;
224 int err;
225
226 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800227 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800228 if (err)
229 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800230
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700231 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700232 f2fs_put_dnode(&dn);
233 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700234 }
235
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700236 f2fs_bug_on(F2FS_I_SB(inode), page->index);
237
Chao Yubae0ee72018-12-25 17:43:42 +0800238 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700239 src_addr = kmap_atomic(page);
Chao Yuf2470372017-07-19 00:19:05 +0800240 dst_addr = inline_data_addr(inode, dn.inode_page);
241 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700242 kunmap_atomic(src_addr);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700243 set_page_dirty(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800244
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500245 f2fs_clear_page_cache_dirty_tag(page);
Daeho Jeong0abd8e72017-09-11 16:30:28 +0900246
Jaegeuk Kim91942322016-05-20 10:13:22 -0700247 set_inode_flag(inode, FI_APPEND_WRITE);
248 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700249
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800250 clear_inline_node(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800251 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800252 return 0;
253}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900254
Chao Yu4d57b862018-05-30 00:20:41 +0800255bool f2fs_recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900256{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700257 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900258 struct f2fs_inode *ri = NULL;
259 void *src_addr, *dst_addr;
260 struct page *ipage;
261
262 /*
263 * The inline_data recovery policy is as follows.
264 * [prev.] [next] of inline_data flag
265 * o o -> recover inline_data
266 * o x -> remove inline_data, and then recover data blocks
267 * x o -> remove inline_data, and then recover inline_data
268 * x x -> recover data blocks
269 */
270 if (IS_INODE(npage))
271 ri = F2FS_INODE(npage);
272
273 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700274 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900275process_inline:
Chao Yu4d57b862018-05-30 00:20:41 +0800276 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700277 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900278
Chao Yubae0ee72018-12-25 17:43:42 +0800279 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900280
Chao Yuf2470372017-07-19 00:19:05 +0800281 src_addr = inline_data_addr(inode, npage);
282 dst_addr = inline_data_addr(inode, ipage);
283 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700284
Jaegeuk Kim91942322016-05-20 10:13:22 -0700285 set_inode_flag(inode, FI_INLINE_DATA);
286 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700287
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700288 set_page_dirty(ipage);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900289 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700290 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900291 }
292
293 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800294 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700295 f2fs_bug_on(sbi, IS_ERR(ipage));
Chao Yu4d57b862018-05-30 00:20:41 +0800296 f2fs_truncate_inline_inode(inode, ipage, 0);
Kinglong Meebd4667c2017-03-10 20:43:20 +0800297 clear_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900298 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700299 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Chao Yuc42d28c2019-02-02 17:33:01 +0800300 if (f2fs_truncate_blocks(inode, 0, false))
Nicholas Krause545fe422015-09-21 18:55:49 -0400301 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900302 goto process_inline;
303 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700304 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900305}
Chao Yu201a05b2014-09-24 18:17:53 +0800306
Chao Yu4d57b862018-05-30 00:20:41 +0800307struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700308 struct fscrypt_name *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800309{
310 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700311 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
Chao Yu201a05b2014-09-24 18:17:53 +0800312 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700313 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700314 struct page *ipage;
Chao Yuf2470372017-07-19 00:19:05 +0800315 void *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700316 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800317
Chao Yu4d57b862018-05-30 00:20:41 +0800318 ipage = f2fs_get_node_page(sbi, dir->i_ino);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700319 if (IS_ERR(ipage)) {
320 *res_page = ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800321 return NULL;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700322 }
Chao Yu201a05b2014-09-24 18:17:53 +0800323
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700324 namehash = f2fs_dentry_hash(dir, &name, fname);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700325
Chao Yuf2470372017-07-19 00:19:05 +0800326 inline_dentry = inline_data_addr(dir, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800327
Chao Yuf2470372017-07-19 00:19:05 +0800328 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu4d57b862018-05-30 00:20:41 +0800329 de = f2fs_find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800330 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700331 if (de)
332 *res_page = ipage;
333 else
334 f2fs_put_page(ipage, 0);
335
Chao Yu201a05b2014-09-24 18:17:53 +0800336 return de;
337}
338
Chao Yu4d57b862018-05-30 00:20:41 +0800339int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
Chao Yu201a05b2014-09-24 18:17:53 +0800340 struct page *ipage)
341{
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700342 struct f2fs_dentry_ptr d;
Chao Yuf2470372017-07-19 00:19:05 +0800343 void *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800344
Chao Yuf2470372017-07-19 00:19:05 +0800345 inline_dentry = inline_data_addr(inode, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800346
Chao Yuf2470372017-07-19 00:19:05 +0800347 make_dentry_ptr_inline(inode, &d, inline_dentry);
Chao Yu4d57b862018-05-30 00:20:41 +0800348 f2fs_do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800349
350 set_page_dirty(ipage);
351
352 /* update i_size to MAX_INLINE_DATA */
Chao Yuf2470372017-07-19 00:19:05 +0800353 if (i_size_read(inode) < MAX_INLINE_DATA(inode))
354 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
Chao Yu201a05b2014-09-24 18:17:53 +0800355 return 0;
356}
357
Chao Yu470f00e2015-07-14 18:14:06 +0800358/*
359 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
360 * release ipage in this function.
361 */
Chao Yu675f10b2016-02-22 18:29:18 +0800362static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
Chao Yuf2470372017-07-19 00:19:05 +0800363 void *inline_dentry)
Chao Yu201a05b2014-09-24 18:17:53 +0800364{
365 struct page *page;
366 struct dnode_of_data dn;
367 struct f2fs_dentry_block *dentry_blk;
Chao Yu76a9dd82017-07-16 15:08:54 +0800368 struct f2fs_dentry_ptr src, dst;
Chao Yu201a05b2014-09-24 18:17:53 +0800369 int err;
370
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700371 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
Chao Yu470f00e2015-07-14 18:14:06 +0800372 if (!page) {
373 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800374 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800375 }
Chao Yu201a05b2014-09-24 18:17:53 +0800376
377 set_new_dnode(&dn, dir, ipage, NULL, 0);
378 err = f2fs_reserve_block(&dn, 0);
379 if (err)
380 goto out;
381
Chao Yu4dbe38d2018-06-30 18:13:40 +0800382 if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
383 f2fs_put_dnode(&dn);
384 set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800385 f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
386 __func__, dir->i_ino, dn.data_blkaddr);
Chao Yu10f966b2019-06-20 11:36:14 +0800387 err = -EFSCORRUPTED;
Chao Yu4dbe38d2018-06-30 18:13:40 +0800388 goto out;
389 }
390
Chao Yubae0ee72018-12-25 17:43:42 +0800391 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800392
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800393 dentry_blk = page_address(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800394
Chao Yuf2470372017-07-19 00:19:05 +0800395 make_dentry_ptr_inline(dir, &src, inline_dentry);
396 make_dentry_ptr_block(dir, &dst, dentry_blk);
Chao Yu76a9dd82017-07-16 15:08:54 +0800397
Chao Yu201a05b2014-09-24 18:17:53 +0800398 /* copy data from inline dentry block to new dentry block */
Chao Yu76a9dd82017-07-16 15:08:54 +0800399 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
400 memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap);
Chao Yu4ec17d62015-08-24 17:36:25 +0800401 /*
402 * we do not need to zero out remainder part of dentry and filename
403 * field, since we have used bitmap for marking the usage status of
404 * them, besides, we can also ignore copying/zeroing reserved space
405 * of dentry block, because them haven't been used so far.
406 */
Chao Yu76a9dd82017-07-16 15:08:54 +0800407 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
408 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
Chao Yu201a05b2014-09-24 18:17:53 +0800409
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700410 if (!PageUptodate(page))
411 SetPageUptodate(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800412 set_page_dirty(page);
413
414 /* clear inline dir and flag after data writeback */
Chao Yu4d57b862018-05-30 00:20:41 +0800415 f2fs_truncate_inline_inode(dir, ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700416
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700417 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700418 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800419
Chao Yu45a74682019-04-11 11:48:09 +0800420 /*
421 * should retrieve reserved space which was used to keep
422 * inline_dentry's structure for backward compatibility.
423 */
424 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
425 !f2fs_has_inline_xattr(dir))
426 F2FS_I(dir)->i_inline_xattr_size = 0;
427
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700428 f2fs_i_depth_write(dir, 1);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700429 if (i_size_read(dir) < PAGE_SIZE)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700430 f2fs_i_size_write(dir, PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800431out:
432 f2fs_put_page(page, 1);
433 return err;
434}
435
Chao Yuf2470372017-07-19 00:19:05 +0800436static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800437{
438 struct f2fs_dentry_ptr d;
439 unsigned long bit_pos = 0;
440 int err = 0;
441
Chao Yuf2470372017-07-19 00:19:05 +0800442 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu675f10b2016-02-22 18:29:18 +0800443
444 while (bit_pos < d.max) {
445 struct f2fs_dir_entry *de;
446 struct qstr new_name;
447 nid_t ino;
448 umode_t fake_mode;
449
450 if (!test_bit_le(bit_pos, d.bitmap)) {
451 bit_pos++;
452 continue;
453 }
454
455 de = &d.dentry[bit_pos];
Chao Yua4a13f52016-04-27 22:22:20 +0800456
457 if (unlikely(!de->name_len)) {
458 bit_pos++;
459 continue;
460 }
461
Chao Yu675f10b2016-02-22 18:29:18 +0800462 new_name.name = d.filename[bit_pos];
Eric Biggers0c0b4712016-10-11 10:36:12 -0700463 new_name.len = le16_to_cpu(de->name_len);
Chao Yu675f10b2016-02-22 18:29:18 +0800464
465 ino = le32_to_cpu(de->ino);
Chao Yu4d57b862018-05-30 00:20:41 +0800466 fake_mode = f2fs_get_de_type(de) << S_SHIFT;
Chao Yu675f10b2016-02-22 18:29:18 +0800467
Chao Yu9421d572016-08-28 18:57:55 +0800468 err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL,
Chao Yu675f10b2016-02-22 18:29:18 +0800469 ino, fake_mode);
470 if (err)
471 goto punch_dentry_pages;
472
Chao Yu675f10b2016-02-22 18:29:18 +0800473 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
474 }
475 return 0;
476punch_dentry_pages:
477 truncate_inode_pages(&dir->i_data, 0);
Chao Yuc42d28c2019-02-02 17:33:01 +0800478 f2fs_truncate_blocks(dir, 0, false);
Chao Yu4d57b862018-05-30 00:20:41 +0800479 f2fs_remove_dirty_inode(dir);
Chao Yu675f10b2016-02-22 18:29:18 +0800480 return err;
481}
482
483static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
Chao Yuf2470372017-07-19 00:19:05 +0800484 void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800485{
Chao Yuf2470372017-07-19 00:19:05 +0800486 void *backup_dentry;
Chao Yu675f10b2016-02-22 18:29:18 +0800487 int err;
488
Chao Yu1ecc0c52016-09-23 21:30:09 +0800489 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
Chao Yuf2470372017-07-19 00:19:05 +0800490 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
Chao Yu8975bdf2016-05-14 19:03:53 +0800491 if (!backup_dentry) {
492 f2fs_put_page(ipage, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800493 return -ENOMEM;
Chao Yu8975bdf2016-05-14 19:03:53 +0800494 }
Chao Yu675f10b2016-02-22 18:29:18 +0800495
Chao Yuf2470372017-07-19 00:19:05 +0800496 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
Chao Yu4d57b862018-05-30 00:20:41 +0800497 f2fs_truncate_inline_inode(dir, ipage, 0);
Chao Yu675f10b2016-02-22 18:29:18 +0800498
499 unlock_page(ipage);
500
501 err = f2fs_add_inline_entries(dir, backup_dentry);
502 if (err)
503 goto recover;
504
505 lock_page(ipage);
506
507 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700508 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu45a74682019-04-11 11:48:09 +0800509
510 /*
511 * should retrieve reserved space which was used to keep
512 * inline_dentry's structure for backward compatibility.
513 */
514 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
515 !f2fs_has_inline_xattr(dir))
516 F2FS_I(dir)->i_inline_xattr_size = 0;
517
Jaegeuk Kim52225952018-12-13 18:38:33 -0800518 kvfree(backup_dentry);
Chao Yu675f10b2016-02-22 18:29:18 +0800519 return 0;
520recover:
521 lock_page(ipage);
Chao Yubae0ee72018-12-25 17:43:42 +0800522 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Chao Yuf2470372017-07-19 00:19:05 +0800523 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700524 f2fs_i_depth_write(dir, 0);
Chao Yuf2470372017-07-19 00:19:05 +0800525 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700526 set_page_dirty(ipage);
Chao Yu675f10b2016-02-22 18:29:18 +0800527 f2fs_put_page(ipage, 1);
528
Jaegeuk Kim52225952018-12-13 18:38:33 -0800529 kvfree(backup_dentry);
Chao Yu675f10b2016-02-22 18:29:18 +0800530 return err;
531}
532
533static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yuf2470372017-07-19 00:19:05 +0800534 void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800535{
536 if (!F2FS_I(dir)->i_dir_level)
537 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
538 else
539 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
540}
541
Chao Yu9421d572016-08-28 18:57:55 +0800542int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
543 const struct qstr *orig_name,
544 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800545{
546 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
547 struct page *ipage;
548 unsigned int bit_pos;
549 f2fs_hash_t name_hash;
Chao Yuf2470372017-07-19 00:19:05 +0800550 void *inline_dentry = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800551 struct f2fs_dentry_ptr d;
Chao Yu9421d572016-08-28 18:57:55 +0800552 int slots = GET_DENTRY_SLOTS(new_name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700553 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800554 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800555
Chao Yu4d57b862018-05-30 00:20:41 +0800556 ipage = f2fs_get_node_page(sbi, dir->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800557 if (IS_ERR(ipage))
558 return PTR_ERR(ipage);
559
Chao Yuf2470372017-07-19 00:19:05 +0800560 inline_dentry = inline_data_addr(dir, ipage);
561 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu76a9dd82017-07-16 15:08:54 +0800562
Chao Yu4d57b862018-05-30 00:20:41 +0800563 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
Chao Yu76a9dd82017-07-16 15:08:54 +0800564 if (bit_pos >= d.max) {
Tiezhu Yanga0057742017-06-09 06:32:54 +0800565 err = f2fs_convert_inline_dir(dir, ipage, inline_dentry);
Chao Yu470f00e2015-07-14 18:14:06 +0800566 if (err)
567 return err;
568 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800569 goto out;
570 }
571
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700572 if (inode) {
573 down_write(&F2FS_I(inode)->i_sem);
Chao Yu4d57b862018-05-30 00:20:41 +0800574 page = f2fs_init_inode_metadata(inode, dir, new_name,
Chao Yu9421d572016-08-28 18:57:55 +0800575 orig_name, ipage);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700576 if (IS_ERR(page)) {
577 err = PTR_ERR(page);
578 goto fail;
579 }
Chao Yu201a05b2014-09-24 18:17:53 +0800580 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700581
Chao Yubae0ee72018-12-25 17:43:42 +0800582 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Chao Yu3b4d7322015-02-16 16:17:20 +0800583
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700584 name_hash = f2fs_dentry_hash(dir, new_name, NULL);
Chao Yu9421d572016-08-28 18:57:55 +0800585 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800586
Chao Yu201a05b2014-09-24 18:17:53 +0800587 set_page_dirty(ipage);
588
589 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700590 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700591 f2fs_i_pino_write(inode, dir->i_ino);
Chao Yu98194032019-09-10 09:14:16 +0800592
593 /* synchronize inode page's data from inode cache */
594 if (is_inode_flag_set(inode, FI_NEW_INODE))
595 f2fs_update_inode(inode, page);
596
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700597 f2fs_put_page(page, 1);
598 }
Chao Yu201a05b2014-09-24 18:17:53 +0800599
Chao Yu4d57b862018-05-30 00:20:41 +0800600 f2fs_update_parent_metadata(dir, inode, 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800601fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700602 if (inode)
603 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800604out:
605 f2fs_put_page(ipage, 1);
606 return err;
607}
608
609void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
610 struct inode *dir, struct inode *inode)
611{
Chao Yu76a9dd82017-07-16 15:08:54 +0800612 struct f2fs_dentry_ptr d;
Chao Yuf2470372017-07-19 00:19:05 +0800613 void *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800614 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
615 unsigned int bit_pos;
616 int i;
617
618 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800619 f2fs_wait_on_page_writeback(page, NODE, true, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800620
Chao Yuf2470372017-07-19 00:19:05 +0800621 inline_dentry = inline_data_addr(dir, page);
622 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu76a9dd82017-07-16 15:08:54 +0800623
624 bit_pos = dentry - d.dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800625 for (i = 0; i < slots; i++)
Chao Yu76a9dd82017-07-16 15:08:54 +0800626 __clear_bit_le(bit_pos + i, d.bitmap);
Chao Yu201a05b2014-09-24 18:17:53 +0800627
628 set_page_dirty(page);
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700629 f2fs_put_page(page, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800630
Deepa Dinamani078cd822016-09-14 07:48:04 -0700631 dir->i_ctime = dir->i_mtime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700632 f2fs_mark_inode_dirty_sync(dir, false);
Chao Yu201a05b2014-09-24 18:17:53 +0800633
634 if (inode)
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700635 f2fs_drop_nlink(dir, inode);
Chao Yu201a05b2014-09-24 18:17:53 +0800636}
637
638bool f2fs_empty_inline_dir(struct inode *dir)
639{
640 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
641 struct page *ipage;
642 unsigned int bit_pos = 2;
Chao Yuf2470372017-07-19 00:19:05 +0800643 void *inline_dentry;
Chao Yu76a9dd82017-07-16 15:08:54 +0800644 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800645
Chao Yu4d57b862018-05-30 00:20:41 +0800646 ipage = f2fs_get_node_page(sbi, dir->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800647 if (IS_ERR(ipage))
648 return false;
649
Chao Yuf2470372017-07-19 00:19:05 +0800650 inline_dentry = inline_data_addr(dir, ipage);
651 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu76a9dd82017-07-16 15:08:54 +0800652
653 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
Chao Yu201a05b2014-09-24 18:17:53 +0800654
655 f2fs_put_page(ipage, 1);
656
Chao Yu76a9dd82017-07-16 15:08:54 +0800657 if (bit_pos < d.max)
Chao Yu201a05b2014-09-24 18:17:53 +0800658 return false;
659
660 return true;
661}
662
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700663int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700664 struct fscrypt_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800665{
666 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800667 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700668 struct f2fs_dentry_ptr d;
Chao Yuf2470372017-07-19 00:19:05 +0800669 void *inline_dentry = NULL;
Chao Yued6bd4b2016-10-29 18:46:34 +0800670 int err;
Chao Yu201a05b2014-09-24 18:17:53 +0800671
Chao Yu76a9dd82017-07-16 15:08:54 +0800672 make_dentry_ptr_inline(inode, &d, inline_dentry);
673
674 if (ctx->pos == d.max)
Chao Yu201a05b2014-09-24 18:17:53 +0800675 return 0;
676
Chao Yu4d57b862018-05-30 00:20:41 +0800677 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800678 if (IS_ERR(ipage))
679 return PTR_ERR(ipage);
680
Chao Yuaadcef62019-03-12 15:44:27 +0800681 /*
682 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
683 * ipage without page's lock held.
684 */
685 unlock_page(ipage);
686
Chao Yuf2470372017-07-19 00:19:05 +0800687 inline_dentry = inline_data_addr(inode, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800688
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300689 make_dentry_ptr_inline(inode, &d, inline_dentry);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700690
Chao Yued6bd4b2016-10-29 18:46:34 +0800691 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
692 if (!err)
Chao Yu76a9dd82017-07-16 15:08:54 +0800693 ctx->pos = d.max;
Chao Yu201a05b2014-09-24 18:17:53 +0800694
Chao Yuaadcef62019-03-12 15:44:27 +0800695 f2fs_put_page(ipage, 0);
Chao Yued6bd4b2016-10-29 18:46:34 +0800696 return err < 0 ? err : 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800697}
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700698
699int f2fs_inline_data_fiemap(struct inode *inode,
700 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
701{
702 __u64 byteaddr, ilen;
703 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
704 FIEMAP_EXTENT_LAST;
705 struct node_info ni;
706 struct page *ipage;
707 int err = 0;
708
Chao Yu4d57b862018-05-30 00:20:41 +0800709 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700710 if (IS_ERR(ipage))
711 return PTR_ERR(ipage);
712
Chao Yu7975f342019-07-22 18:03:50 +0800713 if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
714 !f2fs_has_inline_data(inode)) {
715 err = -EAGAIN;
716 goto out;
717 }
718
719 if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) {
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700720 err = -EAGAIN;
721 goto out;
722 }
723
Chao Yuf2470372017-07-19 00:19:05 +0800724 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700725 if (start >= ilen)
726 goto out;
727 if (start + len < ilen)
728 ilen = start + len;
729 ilen -= start;
730
Chao Yu77357302018-07-17 00:02:17 +0800731 err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
732 if (err)
733 goto out;
734
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700735 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
Chao Yuf2470372017-07-19 00:19:05 +0800736 byteaddr += (char *)inline_data_addr(inode, ipage) -
737 (char *)F2FS_INODE(ipage);
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700738 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
739out:
740 f2fs_put_page(ipage, 1);
741 return err;
742}