blob: 78d6ebe165cd03f3481876f5c3cd00dd5e565720 [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) {
134 f2fs_put_dnode(dn);
135 return err;
136 }
137
138 fio.version = ni.version;
139
Chao Yu4dbe38d2018-06-30 18:13:40 +0800140 if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
141 f2fs_put_dnode(dn);
142 set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800143 f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
144 __func__, dn->inode->i_ino, dn->data_blkaddr);
Chao Yu10f966b2019-06-20 11:36:14 +0800145 return -EFSCORRUPTED;
Chao Yu4dbe38d2018-06-30 18:13:40 +0800146 }
147
Yunlei He85ead812016-02-03 10:26:13 +0800148 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700149
Chao Yu4d57b862018-05-30 00:20:41 +0800150 f2fs_do_read_inline_data(page, dn->inode_page);
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700151 set_page_dirty(page);
152
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800153 /* clear dirty state */
154 dirty = clear_page_dirty_for_io(page);
155
Huajun Lie18c65b2013-11-10 23:13:19 +0800156 /* write data page to try to make data consistent */
157 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -0700158 ClearPageError(page);
Chao Yu7a9d7542016-02-22 18:36:38 +0800159 fio.old_blkaddr = dn->data_blkaddr;
Jaegeuk Kimef095d12017-03-24 20:05:13 -0400160 set_inode_flag(dn->inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +0800161 f2fs_outplace_write_data(dn, &fio);
Chao Yubae0ee72018-12-25 17:43:42 +0800162 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu933439c2016-10-11 22:57:01 +0800163 if (dirty) {
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800164 inode_dec_dirty_pages(dn->inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800165 f2fs_remove_dirty_inode(dn->inode);
Chao Yu933439c2016-10-11 22:57:01 +0800166 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800167
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800168 /* this converted inline_data should be recovered. */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700169 set_inode_flag(dn->inode, FI_APPEND_WRITE);
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800170
Huajun Lie18c65b2013-11-10 23:13:19 +0800171 /* clear inline data and flag after data writeback */
Chao Yu4d57b862018-05-30 00:20:41 +0800172 f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800173 clear_inline_node(dn->inode_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700174clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700175 stat_dec_inline_inode(dn->inode);
Kinglong Meebd4667c2017-03-10 20:43:20 +0800176 clear_inode_flag(dn->inode, FI_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700177 f2fs_put_dnode(dn);
178 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800179}
180
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700181int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800182{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700183 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
184 struct dnode_of_data dn;
185 struct page *ipage, *page;
186 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800187
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800188 if (!f2fs_has_inline_data(inode))
189 return 0;
190
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700191 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700192 if (!page)
193 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900194
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700195 f2fs_lock_op(sbi);
196
Chao Yu4d57b862018-05-30 00:20:41 +0800197 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700198 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800199 err = PTR_ERR(ipage);
200 goto out;
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700201 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800202
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700203 set_new_dnode(&dn, inode, ipage, ipage, 0);
204
205 if (f2fs_has_inline_data(inode))
206 err = f2fs_convert_inline_page(&dn, page);
207
208 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800209out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700210 f2fs_unlock_op(sbi);
211
212 f2fs_put_page(page, 1);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800213
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800214 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800215
Huajun Lie18c65b2013-11-10 23:13:19 +0800216 return err;
217}
218
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700219int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800220{
221 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800222 struct dnode_of_data dn;
223 int err;
224
225 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800226 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800227 if (err)
228 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800229
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700230 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700231 f2fs_put_dnode(&dn);
232 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700233 }
234
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700235 f2fs_bug_on(F2FS_I_SB(inode), page->index);
236
Chao Yubae0ee72018-12-25 17:43:42 +0800237 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700238 src_addr = kmap_atomic(page);
Chao Yuf2470372017-07-19 00:19:05 +0800239 dst_addr = inline_data_addr(inode, dn.inode_page);
240 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700241 kunmap_atomic(src_addr);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700242 set_page_dirty(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800243
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500244 f2fs_clear_page_cache_dirty_tag(page);
Daeho Jeong0abd8e72017-09-11 16:30:28 +0900245
Jaegeuk Kim91942322016-05-20 10:13:22 -0700246 set_inode_flag(inode, FI_APPEND_WRITE);
247 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700248
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800249 clear_inline_node(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800250 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800251 return 0;
252}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900253
Chao Yu4d57b862018-05-30 00:20:41 +0800254bool f2fs_recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900255{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900257 struct f2fs_inode *ri = NULL;
258 void *src_addr, *dst_addr;
259 struct page *ipage;
260
261 /*
262 * The inline_data recovery policy is as follows.
263 * [prev.] [next] of inline_data flag
264 * o o -> recover inline_data
265 * o x -> remove inline_data, and then recover data blocks
266 * x o -> remove inline_data, and then recover inline_data
267 * x x -> recover data blocks
268 */
269 if (IS_INODE(npage))
270 ri = F2FS_INODE(npage);
271
272 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700273 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900274process_inline:
Chao Yu4d57b862018-05-30 00:20:41 +0800275 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700276 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900277
Chao Yubae0ee72018-12-25 17:43:42 +0800278 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900279
Chao Yuf2470372017-07-19 00:19:05 +0800280 src_addr = inline_data_addr(inode, npage);
281 dst_addr = inline_data_addr(inode, ipage);
282 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700283
Jaegeuk Kim91942322016-05-20 10:13:22 -0700284 set_inode_flag(inode, FI_INLINE_DATA);
285 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700286
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700287 set_page_dirty(ipage);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900288 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700289 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900290 }
291
292 if (f2fs_has_inline_data(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800293 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700294 f2fs_bug_on(sbi, IS_ERR(ipage));
Chao Yu4d57b862018-05-30 00:20:41 +0800295 f2fs_truncate_inline_inode(inode, ipage, 0);
Kinglong Meebd4667c2017-03-10 20:43:20 +0800296 clear_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900297 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700298 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Chao Yuc42d28c2019-02-02 17:33:01 +0800299 if (f2fs_truncate_blocks(inode, 0, false))
Nicholas Krause545fe422015-09-21 18:55:49 -0400300 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900301 goto process_inline;
302 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700303 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900304}
Chao Yu201a05b2014-09-24 18:17:53 +0800305
Chao Yu4d57b862018-05-30 00:20:41 +0800306struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700307 struct fscrypt_name *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800308{
309 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700310 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
Chao Yu201a05b2014-09-24 18:17:53 +0800311 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700312 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700313 struct page *ipage;
Chao Yuf2470372017-07-19 00:19:05 +0800314 void *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700315 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800316
Chao Yu4d57b862018-05-30 00:20:41 +0800317 ipage = f2fs_get_node_page(sbi, dir->i_ino);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700318 if (IS_ERR(ipage)) {
319 *res_page = ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800320 return NULL;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700321 }
Chao Yu201a05b2014-09-24 18:17:53 +0800322
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700323 namehash = f2fs_dentry_hash(dir, &name, fname);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700324
Chao Yuf2470372017-07-19 00:19:05 +0800325 inline_dentry = inline_data_addr(dir, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800326
Chao Yuf2470372017-07-19 00:19:05 +0800327 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu4d57b862018-05-30 00:20:41 +0800328 de = f2fs_find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800329 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700330 if (de)
331 *res_page = ipage;
332 else
333 f2fs_put_page(ipage, 0);
334
Chao Yu201a05b2014-09-24 18:17:53 +0800335 return de;
336}
337
Chao Yu4d57b862018-05-30 00:20:41 +0800338int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
Chao Yu201a05b2014-09-24 18:17:53 +0800339 struct page *ipage)
340{
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700341 struct f2fs_dentry_ptr d;
Chao Yuf2470372017-07-19 00:19:05 +0800342 void *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800343
Chao Yuf2470372017-07-19 00:19:05 +0800344 inline_dentry = inline_data_addr(inode, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800345
Chao Yuf2470372017-07-19 00:19:05 +0800346 make_dentry_ptr_inline(inode, &d, inline_dentry);
Chao Yu4d57b862018-05-30 00:20:41 +0800347 f2fs_do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800348
349 set_page_dirty(ipage);
350
351 /* update i_size to MAX_INLINE_DATA */
Chao Yuf2470372017-07-19 00:19:05 +0800352 if (i_size_read(inode) < MAX_INLINE_DATA(inode))
353 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
Chao Yu201a05b2014-09-24 18:17:53 +0800354 return 0;
355}
356
Chao Yu470f00e2015-07-14 18:14:06 +0800357/*
358 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
359 * release ipage in this function.
360 */
Chao Yu675f10b2016-02-22 18:29:18 +0800361static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
Chao Yuf2470372017-07-19 00:19:05 +0800362 void *inline_dentry)
Chao Yu201a05b2014-09-24 18:17:53 +0800363{
364 struct page *page;
365 struct dnode_of_data dn;
366 struct f2fs_dentry_block *dentry_blk;
Chao Yu76a9dd82017-07-16 15:08:54 +0800367 struct f2fs_dentry_ptr src, dst;
Chao Yu201a05b2014-09-24 18:17:53 +0800368 int err;
369
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700370 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
Chao Yu470f00e2015-07-14 18:14:06 +0800371 if (!page) {
372 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800373 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800374 }
Chao Yu201a05b2014-09-24 18:17:53 +0800375
376 set_new_dnode(&dn, dir, ipage, NULL, 0);
377 err = f2fs_reserve_block(&dn, 0);
378 if (err)
379 goto out;
380
Chao Yu4dbe38d2018-06-30 18:13:40 +0800381 if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
382 f2fs_put_dnode(&dn);
383 set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800384 f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
385 __func__, dir->i_ino, dn.data_blkaddr);
Chao Yu10f966b2019-06-20 11:36:14 +0800386 err = -EFSCORRUPTED;
Chao Yu4dbe38d2018-06-30 18:13:40 +0800387 goto out;
388 }
389
Chao Yubae0ee72018-12-25 17:43:42 +0800390 f2fs_wait_on_page_writeback(page, DATA, true, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800391
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800392 dentry_blk = page_address(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800393
Chao Yuf2470372017-07-19 00:19:05 +0800394 make_dentry_ptr_inline(dir, &src, inline_dentry);
395 make_dentry_ptr_block(dir, &dst, dentry_blk);
Chao Yu76a9dd82017-07-16 15:08:54 +0800396
Chao Yu201a05b2014-09-24 18:17:53 +0800397 /* copy data from inline dentry block to new dentry block */
Chao Yu76a9dd82017-07-16 15:08:54 +0800398 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
399 memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap);
Chao Yu4ec17d62015-08-24 17:36:25 +0800400 /*
401 * we do not need to zero out remainder part of dentry and filename
402 * field, since we have used bitmap for marking the usage status of
403 * them, besides, we can also ignore copying/zeroing reserved space
404 * of dentry block, because them haven't been used so far.
405 */
Chao Yu76a9dd82017-07-16 15:08:54 +0800406 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
407 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
Chao Yu201a05b2014-09-24 18:17:53 +0800408
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700409 if (!PageUptodate(page))
410 SetPageUptodate(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800411 set_page_dirty(page);
412
413 /* clear inline dir and flag after data writeback */
Chao Yu4d57b862018-05-30 00:20:41 +0800414 f2fs_truncate_inline_inode(dir, ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700415
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700416 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700417 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800418
Chao Yu45a74682019-04-11 11:48:09 +0800419 /*
420 * should retrieve reserved space which was used to keep
421 * inline_dentry's structure for backward compatibility.
422 */
423 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
424 !f2fs_has_inline_xattr(dir))
425 F2FS_I(dir)->i_inline_xattr_size = 0;
426
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700427 f2fs_i_depth_write(dir, 1);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700428 if (i_size_read(dir) < PAGE_SIZE)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700429 f2fs_i_size_write(dir, PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800430out:
431 f2fs_put_page(page, 1);
432 return err;
433}
434
Chao Yuf2470372017-07-19 00:19:05 +0800435static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800436{
437 struct f2fs_dentry_ptr d;
438 unsigned long bit_pos = 0;
439 int err = 0;
440
Chao Yuf2470372017-07-19 00:19:05 +0800441 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu675f10b2016-02-22 18:29:18 +0800442
443 while (bit_pos < d.max) {
444 struct f2fs_dir_entry *de;
445 struct qstr new_name;
446 nid_t ino;
447 umode_t fake_mode;
448
449 if (!test_bit_le(bit_pos, d.bitmap)) {
450 bit_pos++;
451 continue;
452 }
453
454 de = &d.dentry[bit_pos];
Chao Yua4a13f52016-04-27 22:22:20 +0800455
456 if (unlikely(!de->name_len)) {
457 bit_pos++;
458 continue;
459 }
460
Chao Yu675f10b2016-02-22 18:29:18 +0800461 new_name.name = d.filename[bit_pos];
Eric Biggers0c0b4712016-10-11 10:36:12 -0700462 new_name.len = le16_to_cpu(de->name_len);
Chao Yu675f10b2016-02-22 18:29:18 +0800463
464 ino = le32_to_cpu(de->ino);
Chao Yu4d57b862018-05-30 00:20:41 +0800465 fake_mode = f2fs_get_de_type(de) << S_SHIFT;
Chao Yu675f10b2016-02-22 18:29:18 +0800466
Chao Yu9421d572016-08-28 18:57:55 +0800467 err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL,
Chao Yu675f10b2016-02-22 18:29:18 +0800468 ino, fake_mode);
469 if (err)
470 goto punch_dentry_pages;
471
Chao Yu675f10b2016-02-22 18:29:18 +0800472 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
473 }
474 return 0;
475punch_dentry_pages:
476 truncate_inode_pages(&dir->i_data, 0);
Chao Yuc42d28c2019-02-02 17:33:01 +0800477 f2fs_truncate_blocks(dir, 0, false);
Chao Yu4d57b862018-05-30 00:20:41 +0800478 f2fs_remove_dirty_inode(dir);
Chao Yu675f10b2016-02-22 18:29:18 +0800479 return err;
480}
481
482static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
Chao Yuf2470372017-07-19 00:19:05 +0800483 void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800484{
Chao Yuf2470372017-07-19 00:19:05 +0800485 void *backup_dentry;
Chao Yu675f10b2016-02-22 18:29:18 +0800486 int err;
487
Chao Yu1ecc0c52016-09-23 21:30:09 +0800488 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
Chao Yuf2470372017-07-19 00:19:05 +0800489 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
Chao Yu8975bdf2016-05-14 19:03:53 +0800490 if (!backup_dentry) {
491 f2fs_put_page(ipage, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800492 return -ENOMEM;
Chao Yu8975bdf2016-05-14 19:03:53 +0800493 }
Chao Yu675f10b2016-02-22 18:29:18 +0800494
Chao Yuf2470372017-07-19 00:19:05 +0800495 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
Chao Yu4d57b862018-05-30 00:20:41 +0800496 f2fs_truncate_inline_inode(dir, ipage, 0);
Chao Yu675f10b2016-02-22 18:29:18 +0800497
498 unlock_page(ipage);
499
500 err = f2fs_add_inline_entries(dir, backup_dentry);
501 if (err)
502 goto recover;
503
504 lock_page(ipage);
505
506 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700507 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu45a74682019-04-11 11:48:09 +0800508
509 /*
510 * should retrieve reserved space which was used to keep
511 * inline_dentry's structure for backward compatibility.
512 */
513 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
514 !f2fs_has_inline_xattr(dir))
515 F2FS_I(dir)->i_inline_xattr_size = 0;
516
Jaegeuk Kim52225952018-12-13 18:38:33 -0800517 kvfree(backup_dentry);
Chao Yu675f10b2016-02-22 18:29:18 +0800518 return 0;
519recover:
520 lock_page(ipage);
Chao Yubae0ee72018-12-25 17:43:42 +0800521 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Chao Yuf2470372017-07-19 00:19:05 +0800522 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700523 f2fs_i_depth_write(dir, 0);
Chao Yuf2470372017-07-19 00:19:05 +0800524 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700525 set_page_dirty(ipage);
Chao Yu675f10b2016-02-22 18:29:18 +0800526 f2fs_put_page(ipage, 1);
527
Jaegeuk Kim52225952018-12-13 18:38:33 -0800528 kvfree(backup_dentry);
Chao Yu675f10b2016-02-22 18:29:18 +0800529 return err;
530}
531
532static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yuf2470372017-07-19 00:19:05 +0800533 void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800534{
535 if (!F2FS_I(dir)->i_dir_level)
536 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
537 else
538 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
539}
540
Chao Yu9421d572016-08-28 18:57:55 +0800541int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
542 const struct qstr *orig_name,
543 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800544{
545 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
546 struct page *ipage;
547 unsigned int bit_pos;
548 f2fs_hash_t name_hash;
Chao Yuf2470372017-07-19 00:19:05 +0800549 void *inline_dentry = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800550 struct f2fs_dentry_ptr d;
Chao Yu9421d572016-08-28 18:57:55 +0800551 int slots = GET_DENTRY_SLOTS(new_name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700552 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800553 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800554
Chao Yu4d57b862018-05-30 00:20:41 +0800555 ipage = f2fs_get_node_page(sbi, dir->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800556 if (IS_ERR(ipage))
557 return PTR_ERR(ipage);
558
Chao Yuf2470372017-07-19 00:19:05 +0800559 inline_dentry = inline_data_addr(dir, ipage);
560 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu76a9dd82017-07-16 15:08:54 +0800561
Chao Yu4d57b862018-05-30 00:20:41 +0800562 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
Chao Yu76a9dd82017-07-16 15:08:54 +0800563 if (bit_pos >= d.max) {
Tiezhu Yanga0057742017-06-09 06:32:54 +0800564 err = f2fs_convert_inline_dir(dir, ipage, inline_dentry);
Chao Yu470f00e2015-07-14 18:14:06 +0800565 if (err)
566 return err;
567 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800568 goto out;
569 }
570
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700571 if (inode) {
572 down_write(&F2FS_I(inode)->i_sem);
Chao Yu4d57b862018-05-30 00:20:41 +0800573 page = f2fs_init_inode_metadata(inode, dir, new_name,
Chao Yu9421d572016-08-28 18:57:55 +0800574 orig_name, ipage);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700575 if (IS_ERR(page)) {
576 err = PTR_ERR(page);
577 goto fail;
578 }
Chao Yu201a05b2014-09-24 18:17:53 +0800579 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700580
Chao Yubae0ee72018-12-25 17:43:42 +0800581 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Chao Yu3b4d7322015-02-16 16:17:20 +0800582
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700583 name_hash = f2fs_dentry_hash(dir, new_name, NULL);
Chao Yu9421d572016-08-28 18:57:55 +0800584 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800585
Chao Yu201a05b2014-09-24 18:17:53 +0800586 set_page_dirty(ipage);
587
588 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700589 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700590 f2fs_i_pino_write(inode, dir->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700591 f2fs_put_page(page, 1);
592 }
Chao Yu201a05b2014-09-24 18:17:53 +0800593
Chao Yu4d57b862018-05-30 00:20:41 +0800594 f2fs_update_parent_metadata(dir, inode, 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800595fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700596 if (inode)
597 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800598out:
599 f2fs_put_page(ipage, 1);
600 return err;
601}
602
603void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
604 struct inode *dir, struct inode *inode)
605{
Chao Yu76a9dd82017-07-16 15:08:54 +0800606 struct f2fs_dentry_ptr d;
Chao Yuf2470372017-07-19 00:19:05 +0800607 void *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800608 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
609 unsigned int bit_pos;
610 int i;
611
612 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800613 f2fs_wait_on_page_writeback(page, NODE, true, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800614
Chao Yuf2470372017-07-19 00:19:05 +0800615 inline_dentry = inline_data_addr(dir, page);
616 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu76a9dd82017-07-16 15:08:54 +0800617
618 bit_pos = dentry - d.dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800619 for (i = 0; i < slots; i++)
Chao Yu76a9dd82017-07-16 15:08:54 +0800620 __clear_bit_le(bit_pos + i, d.bitmap);
Chao Yu201a05b2014-09-24 18:17:53 +0800621
622 set_page_dirty(page);
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700623 f2fs_put_page(page, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800624
Deepa Dinamani078cd822016-09-14 07:48:04 -0700625 dir->i_ctime = dir->i_mtime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700626 f2fs_mark_inode_dirty_sync(dir, false);
Chao Yu201a05b2014-09-24 18:17:53 +0800627
628 if (inode)
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700629 f2fs_drop_nlink(dir, inode);
Chao Yu201a05b2014-09-24 18:17:53 +0800630}
631
632bool f2fs_empty_inline_dir(struct inode *dir)
633{
634 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
635 struct page *ipage;
636 unsigned int bit_pos = 2;
Chao Yuf2470372017-07-19 00:19:05 +0800637 void *inline_dentry;
Chao Yu76a9dd82017-07-16 15:08:54 +0800638 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800639
Chao Yu4d57b862018-05-30 00:20:41 +0800640 ipage = f2fs_get_node_page(sbi, dir->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800641 if (IS_ERR(ipage))
642 return false;
643
Chao Yuf2470372017-07-19 00:19:05 +0800644 inline_dentry = inline_data_addr(dir, ipage);
645 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu76a9dd82017-07-16 15:08:54 +0800646
647 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
Chao Yu201a05b2014-09-24 18:17:53 +0800648
649 f2fs_put_page(ipage, 1);
650
Chao Yu76a9dd82017-07-16 15:08:54 +0800651 if (bit_pos < d.max)
Chao Yu201a05b2014-09-24 18:17:53 +0800652 return false;
653
654 return true;
655}
656
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700657int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700658 struct fscrypt_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800659{
660 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800661 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700662 struct f2fs_dentry_ptr d;
Chao Yuf2470372017-07-19 00:19:05 +0800663 void *inline_dentry = NULL;
Chao Yued6bd4b2016-10-29 18:46:34 +0800664 int err;
Chao Yu201a05b2014-09-24 18:17:53 +0800665
Chao Yu76a9dd82017-07-16 15:08:54 +0800666 make_dentry_ptr_inline(inode, &d, inline_dentry);
667
668 if (ctx->pos == d.max)
Chao Yu201a05b2014-09-24 18:17:53 +0800669 return 0;
670
Chao Yu4d57b862018-05-30 00:20:41 +0800671 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800672 if (IS_ERR(ipage))
673 return PTR_ERR(ipage);
674
Chao Yuaadcef62019-03-12 15:44:27 +0800675 /*
676 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
677 * ipage without page's lock held.
678 */
679 unlock_page(ipage);
680
Chao Yuf2470372017-07-19 00:19:05 +0800681 inline_dentry = inline_data_addr(inode, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800682
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300683 make_dentry_ptr_inline(inode, &d, inline_dentry);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700684
Chao Yued6bd4b2016-10-29 18:46:34 +0800685 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
686 if (!err)
Chao Yu76a9dd82017-07-16 15:08:54 +0800687 ctx->pos = d.max;
Chao Yu201a05b2014-09-24 18:17:53 +0800688
Chao Yuaadcef62019-03-12 15:44:27 +0800689 f2fs_put_page(ipage, 0);
Chao Yued6bd4b2016-10-29 18:46:34 +0800690 return err < 0 ? err : 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800691}
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700692
693int f2fs_inline_data_fiemap(struct inode *inode,
694 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
695{
696 __u64 byteaddr, ilen;
697 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
698 FIEMAP_EXTENT_LAST;
699 struct node_info ni;
700 struct page *ipage;
701 int err = 0;
702
Chao Yu4d57b862018-05-30 00:20:41 +0800703 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700704 if (IS_ERR(ipage))
705 return PTR_ERR(ipage);
706
Chao Yu7975f342019-07-22 18:03:50 +0800707 if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
708 !f2fs_has_inline_data(inode)) {
709 err = -EAGAIN;
710 goto out;
711 }
712
713 if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) {
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700714 err = -EAGAIN;
715 goto out;
716 }
717
Chao Yuf2470372017-07-19 00:19:05 +0800718 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700719 if (start >= ilen)
720 goto out;
721 if (start + len < ilen)
722 ilen = start + len;
723 ilen -= start;
724
Chao Yu77357302018-07-17 00:02:17 +0800725 err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
726 if (err)
727 goto out;
728
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700729 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
Chao Yuf2470372017-07-19 00:19:05 +0800730 byteaddr += (char *)inline_data_addr(inode, ipage) -
731 (char *)F2FS_INODE(ipage);
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700732 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
733out:
734 f2fs_put_page(ipage, 1);
735 return err;
736}