blob: 93c15337bcd9d7ae1361410d310f9280ef93c8f7 [file] [log] [blame]
Huajun Lie18c65b2013-11-10 23:13:19 +08001/*
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"
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -070015#include "node.h"
Huajun Lie18c65b2013-11-10 23:13:19 +080016
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070017bool f2fs_may_inline_data(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +080018{
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070019 if (f2fs_is_atomic_file(inode))
20 return false;
21
Wanpeng Li368a0e42015-03-19 13:23:48 +080022 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
Huajun Lie18c65b2013-11-10 23:13:19 +080023 return false;
24
Jaegeuk Kim92dffd02014-11-11 14:10:01 -080025 if (i_size_read(inode) > MAX_INLINE_DATA)
26 return false;
27
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070028 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
29 return false;
30
Huajun Lie18c65b2013-11-10 23:13:19 +080031 return true;
32}
33
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070034bool f2fs_may_inline_dentry(struct inode *inode)
35{
36 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
37 return false;
38
39 if (!S_ISDIR(inode->i_mode))
40 return false;
41
42 return true;
43}
44
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070045void read_inline_data(struct page *page, struct page *ipage)
Huajun Lie18c65b2013-11-10 23:13:19 +080046{
Huajun Lie18c65b2013-11-10 23:13:19 +080047 void *src_addr, *dst_addr;
48
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070049 if (PageUptodate(page))
50 return;
Chao Yu04a17fb2013-12-30 18:36:23 +080051
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070052 f2fs_bug_on(F2FS_P_SB(page), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080053
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030054 zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080055
56 /* Copy the whole inline data block */
57 src_addr = inline_data_addr(ipage);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070058 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080059 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim427a45c2014-10-26 22:59:27 -070060 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070061 kunmap_atomic(dst_addr);
Jaegeuk Kim237c0792016-06-30 18:49:15 -070062 if (!PageUptodate(page))
63 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070064}
Huajun Lie18c65b2013-11-10 23:13:19 +080065
Kinglong Meebd4667c2017-03-10 20:43:20 +080066void truncate_inline_inode(struct inode *inode, 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 Yu0bfcfcc2015-03-10 13:16:25 +080070 if (from >= MAX_INLINE_DATA)
Kinglong Meebd4667c2017-03-10 20:43:20 +080071 return;
Chao Yu0bfcfcc2015-03-10 13:16:25 +080072
73 addr = inline_data_addr(ipage);
74
Jaegeuk Kimfec1d652016-01-20 23:43:51 +080075 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080076 memset(addr + from, 0, MAX_INLINE_DATA - 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
87 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
88 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
101 read_inline_data(page, ipage);
102
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),
Huajun Lie18c65b2013-11-10 23:13:19 +0800114 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500115 .op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600116 .op_flags = REQ_SYNC | REQ_PRIO,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700117 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700118 .encrypted_page = NULL,
Huajun Lie18c65b2013-11-10 23:13:19 +0800119 };
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800120 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800121
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700122 if (!f2fs_exist_data(dn->inode))
123 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700124
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700125 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900126 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700127 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800128
Yunlei He85ead812016-02-03 10:26:13 +0800129 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700130
Shawn Lin80606562016-02-19 16:02:51 +0800131 read_inline_data(page, dn->inode_page);
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700132 set_page_dirty(page);
133
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800134 /* clear dirty state */
135 dirty = clear_page_dirty_for_io(page);
136
Huajun Lie18c65b2013-11-10 23:13:19 +0800137 /* write data page to try to make data consistent */
138 set_page_writeback(page);
Chao Yu7a9d7542016-02-22 18:36:38 +0800139 fio.old_blkaddr = dn->data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700140 write_data_page(dn, &fio);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800141 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu933439c2016-10-11 22:57:01 +0800142 if (dirty) {
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800143 inode_dec_dirty_pages(dn->inode);
Chao Yu933439c2016-10-11 22:57:01 +0800144 remove_dirty_inode(dn->inode);
145 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800146
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800147 /* this converted inline_data should be recovered. */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700148 set_inode_flag(dn->inode, FI_APPEND_WRITE);
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800149
Huajun Lie18c65b2013-11-10 23:13:19 +0800150 /* clear inline data and flag after data writeback */
Kinglong Meebd4667c2017-03-10 20:43:20 +0800151 truncate_inline_inode(dn->inode, dn->inode_page, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800152 clear_inline_node(dn->inode_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700153clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700154 stat_dec_inline_inode(dn->inode);
Kinglong Meebd4667c2017-03-10 20:43:20 +0800155 clear_inode_flag(dn->inode, FI_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700156 f2fs_put_dnode(dn);
157 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800158}
159
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700160int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800161{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700162 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
163 struct dnode_of_data dn;
164 struct page *ipage, *page;
165 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800166
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800167 if (!f2fs_has_inline_data(inode))
168 return 0;
169
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700170 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700171 if (!page)
172 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900173
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700174 f2fs_lock_op(sbi);
175
176 ipage = get_node_page(sbi, inode->i_ino);
177 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800178 err = PTR_ERR(ipage);
179 goto out;
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700180 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800181
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700182 set_new_dnode(&dn, inode, ipage, ipage, 0);
183
184 if (f2fs_has_inline_data(inode))
185 err = f2fs_convert_inline_page(&dn, page);
186
187 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800188out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700189 f2fs_unlock_op(sbi);
190
191 f2fs_put_page(page, 1);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800192
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800193 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800194
Huajun Lie18c65b2013-11-10 23:13:19 +0800195 return err;
196}
197
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700198int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800199{
200 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800201 struct dnode_of_data dn;
202 int err;
203
204 set_new_dnode(&dn, inode, NULL, NULL, 0);
205 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
206 if (err)
207 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800208
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700209 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700210 f2fs_put_dnode(&dn);
211 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700212 }
213
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700214 f2fs_bug_on(F2FS_I_SB(inode), page->index);
215
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800216 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700217 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700218 dst_addr = inline_data_addr(dn.inode_page);
219 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700220 kunmap_atomic(src_addr);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700221 set_page_dirty(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800222
Jaegeuk Kim91942322016-05-20 10:13:22 -0700223 set_inode_flag(inode, FI_APPEND_WRITE);
224 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700225
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800226 clear_inline_node(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800227 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800228 return 0;
229}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900230
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700231bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900232{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700233 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900234 struct f2fs_inode *ri = NULL;
235 void *src_addr, *dst_addr;
236 struct page *ipage;
237
238 /*
239 * The inline_data recovery policy is as follows.
240 * [prev.] [next] of inline_data flag
241 * o o -> recover inline_data
242 * o x -> remove inline_data, and then recover data blocks
243 * x o -> remove inline_data, and then recover inline_data
244 * x x -> recover data blocks
245 */
246 if (IS_INODE(npage))
247 ri = F2FS_INODE(npage);
248
249 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700250 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900251process_inline:
252 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700253 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900254
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800255 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900256
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900257 src_addr = inline_data_addr(npage);
258 dst_addr = inline_data_addr(ipage);
259 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700260
Jaegeuk Kim91942322016-05-20 10:13:22 -0700261 set_inode_flag(inode, FI_INLINE_DATA);
262 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700263
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700264 set_page_dirty(ipage);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900265 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700266 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900267 }
268
269 if (f2fs_has_inline_data(inode)) {
270 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700271 f2fs_bug_on(sbi, IS_ERR(ipage));
Kinglong Meebd4667c2017-03-10 20:43:20 +0800272 truncate_inline_inode(inode, ipage, 0);
273 clear_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900274 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700275 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Nicholas Krause545fe422015-09-21 18:55:49 -0400276 if (truncate_blocks(inode, 0, false))
277 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900278 goto process_inline;
279 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700280 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900281}
Chao Yu201a05b2014-09-24 18:17:53 +0800282
283struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700284 struct fscrypt_name *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800285{
286 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700287 struct f2fs_inline_dentry *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700288 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
Chao Yu201a05b2014-09-24 18:17:53 +0800289 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700290 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700291 struct page *ipage;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700292 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800293
294 ipage = get_node_page(sbi, dir->i_ino);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700295 if (IS_ERR(ipage)) {
296 *res_page = ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800297 return NULL;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700298 }
Chao Yu201a05b2014-09-24 18:17:53 +0800299
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700300 namehash = f2fs_dentry_hash(&name);
301
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700302 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800303
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700304 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700305 de = find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800306 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700307 if (de)
308 *res_page = ipage;
309 else
310 f2fs_put_page(ipage, 0);
311
Chao Yu201a05b2014-09-24 18:17:53 +0800312 return de;
313}
314
Chao Yu201a05b2014-09-24 18:17:53 +0800315int make_empty_inline_dir(struct inode *inode, struct inode *parent,
316 struct page *ipage)
317{
318 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700319 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800320
321 dentry_blk = inline_data_addr(ipage);
322
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700323 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700324 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800325
326 set_page_dirty(ipage);
327
328 /* update i_size to MAX_INLINE_DATA */
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700329 if (i_size_read(inode) < MAX_INLINE_DATA)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700330 f2fs_i_size_write(inode, MAX_INLINE_DATA);
Chao Yu201a05b2014-09-24 18:17:53 +0800331 return 0;
332}
333
Chao Yu470f00e2015-07-14 18:14:06 +0800334/*
335 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
336 * release ipage in this function.
337 */
Chao Yu675f10b2016-02-22 18:29:18 +0800338static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800339 struct f2fs_inline_dentry *inline_dentry)
340{
341 struct page *page;
342 struct dnode_of_data dn;
343 struct f2fs_dentry_block *dentry_blk;
344 int err;
345
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700346 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
Chao Yu470f00e2015-07-14 18:14:06 +0800347 if (!page) {
348 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800349 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800350 }
Chao Yu201a05b2014-09-24 18:17:53 +0800351
352 set_new_dnode(&dn, dir, ipage, NULL, 0);
353 err = f2fs_reserve_block(&dn, 0);
354 if (err)
355 goto out;
356
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800357 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300358 zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800359
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700360 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800361
362 /* copy data from inline dentry block to new dentry block */
363 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
364 INLINE_DENTRY_BITMAP_SIZE);
Chao Yu4ec17d62015-08-24 17:36:25 +0800365 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
366 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
367 /*
368 * we do not need to zero out remainder part of dentry and filename
369 * field, since we have used bitmap for marking the usage status of
370 * them, besides, we can also ignore copying/zeroing reserved space
371 * of dentry block, because them haven't been used so far.
372 */
Chao Yu201a05b2014-09-24 18:17:53 +0800373 memcpy(dentry_blk->dentry, inline_dentry->dentry,
374 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
375 memcpy(dentry_blk->filename, inline_dentry->filename,
376 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
377
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700378 kunmap_atomic(dentry_blk);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700379 if (!PageUptodate(page))
380 SetPageUptodate(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800381 set_page_dirty(page);
382
383 /* clear inline dir and flag after data writeback */
Kinglong Meebd4667c2017-03-10 20:43:20 +0800384 truncate_inline_inode(dir, ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700385
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700386 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700387 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800388
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700389 f2fs_i_depth_write(dir, 1);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700390 if (i_size_read(dir) < PAGE_SIZE)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700391 f2fs_i_size_write(dir, PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800392out:
393 f2fs_put_page(page, 1);
394 return err;
395}
396
Chao Yu675f10b2016-02-22 18:29:18 +0800397static int f2fs_add_inline_entries(struct inode *dir,
398 struct f2fs_inline_dentry *inline_dentry)
399{
400 struct f2fs_dentry_ptr d;
401 unsigned long bit_pos = 0;
402 int err = 0;
403
404 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
405
406 while (bit_pos < d.max) {
407 struct f2fs_dir_entry *de;
408 struct qstr new_name;
409 nid_t ino;
410 umode_t fake_mode;
411
412 if (!test_bit_le(bit_pos, d.bitmap)) {
413 bit_pos++;
414 continue;
415 }
416
417 de = &d.dentry[bit_pos];
Chao Yua4a13f52016-04-27 22:22:20 +0800418
419 if (unlikely(!de->name_len)) {
420 bit_pos++;
421 continue;
422 }
423
Chao Yu675f10b2016-02-22 18:29:18 +0800424 new_name.name = d.filename[bit_pos];
Eric Biggers0c0b4712016-10-11 10:36:12 -0700425 new_name.len = le16_to_cpu(de->name_len);
Chao Yu675f10b2016-02-22 18:29:18 +0800426
427 ino = le32_to_cpu(de->ino);
428 fake_mode = get_de_type(de) << S_SHIFT;
429
Chao Yu9421d572016-08-28 18:57:55 +0800430 err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL,
Chao Yu675f10b2016-02-22 18:29:18 +0800431 ino, fake_mode);
432 if (err)
433 goto punch_dentry_pages;
434
Chao Yu675f10b2016-02-22 18:29:18 +0800435 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
436 }
437 return 0;
438punch_dentry_pages:
439 truncate_inode_pages(&dir->i_data, 0);
440 truncate_blocks(dir, 0, false);
441 remove_dirty_inode(dir);
442 return err;
443}
444
445static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
446 struct f2fs_inline_dentry *inline_dentry)
447{
448 struct f2fs_inline_dentry *backup_dentry;
449 int err;
450
Chao Yu1ecc0c52016-09-23 21:30:09 +0800451 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
452 sizeof(struct f2fs_inline_dentry), GFP_F2FS_ZERO);
Chao Yu8975bdf2016-05-14 19:03:53 +0800453 if (!backup_dentry) {
454 f2fs_put_page(ipage, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800455 return -ENOMEM;
Chao Yu8975bdf2016-05-14 19:03:53 +0800456 }
Chao Yu675f10b2016-02-22 18:29:18 +0800457
458 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA);
Kinglong Meebd4667c2017-03-10 20:43:20 +0800459 truncate_inline_inode(dir, ipage, 0);
Chao Yu675f10b2016-02-22 18:29:18 +0800460
461 unlock_page(ipage);
462
463 err = f2fs_add_inline_entries(dir, backup_dentry);
464 if (err)
465 goto recover;
466
467 lock_page(ipage);
468
469 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700470 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu675f10b2016-02-22 18:29:18 +0800471 kfree(backup_dentry);
472 return 0;
473recover:
474 lock_page(ipage);
475 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700476 f2fs_i_depth_write(dir, 0);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700477 f2fs_i_size_write(dir, MAX_INLINE_DATA);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700478 set_page_dirty(ipage);
Chao Yu675f10b2016-02-22 18:29:18 +0800479 f2fs_put_page(ipage, 1);
480
481 kfree(backup_dentry);
482 return err;
483}
484
485static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
486 struct f2fs_inline_dentry *inline_dentry)
487{
488 if (!F2FS_I(dir)->i_dir_level)
489 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
490 else
491 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
492}
493
Chao Yu9421d572016-08-28 18:57:55 +0800494int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
495 const struct qstr *orig_name,
496 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800497{
498 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
499 struct page *ipage;
500 unsigned int bit_pos;
501 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800502 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800503 struct f2fs_dentry_ptr d;
Chao Yu9421d572016-08-28 18:57:55 +0800504 int slots = GET_DENTRY_SLOTS(new_name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700505 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800506 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800507
508 ipage = get_node_page(sbi, dir->i_ino);
509 if (IS_ERR(ipage))
510 return PTR_ERR(ipage);
511
512 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700513 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
514 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800515 if (bit_pos >= NR_INLINE_DENTRY) {
516 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
Chao Yu470f00e2015-07-14 18:14:06 +0800517 if (err)
518 return err;
519 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800520 goto out;
521 }
522
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700523 if (inode) {
524 down_write(&F2FS_I(inode)->i_sem);
Chao Yu9421d572016-08-28 18:57:55 +0800525 page = init_inode_metadata(inode, dir, new_name,
526 orig_name, ipage);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700527 if (IS_ERR(page)) {
528 err = PTR_ERR(page);
529 goto fail;
530 }
Shuoran Liu167451e2016-08-29 11:27:55 +0800531 if (f2fs_encrypted_inode(dir))
532 file_set_enc_name(inode);
Chao Yu201a05b2014-09-24 18:17:53 +0800533 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700534
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800535 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu3b4d7322015-02-16 16:17:20 +0800536
Chao Yu9421d572016-08-28 18:57:55 +0800537 name_hash = f2fs_dentry_hash(new_name);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700538 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Chao Yu9421d572016-08-28 18:57:55 +0800539 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800540
Chao Yu201a05b2014-09-24 18:17:53 +0800541 set_page_dirty(ipage);
542
543 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700544 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700545 f2fs_i_pino_write(inode, dir->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700546 f2fs_put_page(page, 1);
547 }
Chao Yu201a05b2014-09-24 18:17:53 +0800548
549 update_parent_metadata(dir, inode, 0);
550fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700551 if (inode)
552 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800553out:
554 f2fs_put_page(ipage, 1);
555 return err;
556}
557
558void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
559 struct inode *dir, struct inode *inode)
560{
561 struct f2fs_inline_dentry *inline_dentry;
562 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
563 unsigned int bit_pos;
564 int i;
565
566 lock_page(page);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800567 f2fs_wait_on_page_writeback(page, NODE, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800568
569 inline_dentry = inline_data_addr(page);
570 bit_pos = dentry - inline_dentry->dentry;
571 for (i = 0; i < slots; i++)
Jaegeuk Kim6bf6b262016-08-31 16:20:37 -0700572 __clear_bit_le(bit_pos + i,
Chao Yu201a05b2014-09-24 18:17:53 +0800573 &inline_dentry->dentry_bitmap);
574
575 set_page_dirty(page);
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700576 f2fs_put_page(page, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800577
Deepa Dinamani078cd822016-09-14 07:48:04 -0700578 dir->i_ctime = dir->i_mtime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700579 f2fs_mark_inode_dirty_sync(dir, false);
Chao Yu201a05b2014-09-24 18:17:53 +0800580
581 if (inode)
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700582 f2fs_drop_nlink(dir, inode);
Chao Yu201a05b2014-09-24 18:17:53 +0800583}
584
585bool f2fs_empty_inline_dir(struct inode *dir)
586{
587 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
588 struct page *ipage;
589 unsigned int bit_pos = 2;
590 struct f2fs_inline_dentry *dentry_blk;
591
592 ipage = get_node_page(sbi, dir->i_ino);
593 if (IS_ERR(ipage))
594 return false;
595
596 dentry_blk = inline_data_addr(ipage);
597 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
598 NR_INLINE_DENTRY,
599 bit_pos);
600
601 f2fs_put_page(ipage, 1);
602
603 if (bit_pos < NR_INLINE_DENTRY)
604 return false;
605
606 return true;
607}
608
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700609int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700610 struct fscrypt_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800611{
612 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800613 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800614 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700615 struct f2fs_dentry_ptr d;
Chao Yued6bd4b2016-10-29 18:46:34 +0800616 int err;
Chao Yu201a05b2014-09-24 18:17:53 +0800617
618 if (ctx->pos == NR_INLINE_DENTRY)
619 return 0;
620
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700621 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800622 if (IS_ERR(ipage))
623 return PTR_ERR(ipage);
624
Chao Yu201a05b2014-09-24 18:17:53 +0800625 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800626
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700627 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700628
Chao Yued6bd4b2016-10-29 18:46:34 +0800629 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
630 if (!err)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700631 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800632
Chao Yu201a05b2014-09-24 18:17:53 +0800633 f2fs_put_page(ipage, 1);
Chao Yued6bd4b2016-10-29 18:46:34 +0800634 return err < 0 ? err : 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800635}
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700636
637int f2fs_inline_data_fiemap(struct inode *inode,
638 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
639{
640 __u64 byteaddr, ilen;
641 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
642 FIEMAP_EXTENT_LAST;
643 struct node_info ni;
644 struct page *ipage;
645 int err = 0;
646
647 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
648 if (IS_ERR(ipage))
649 return PTR_ERR(ipage);
650
651 if (!f2fs_has_inline_data(inode)) {
652 err = -EAGAIN;
653 goto out;
654 }
655
656 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode));
657 if (start >= ilen)
658 goto out;
659 if (start + len < ilen)
660 ilen = start + len;
661 ilen -= start;
662
663 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
664 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
665 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage);
666 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
667out:
668 f2fs_put_page(ipage, 1);
669 return err;
670}