blob: c50dee937c1b1cf99ebaff78d6c754d003887c42 [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);
Huajun Lie18c65b2013-11-10 23:13:19 +080062 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070063}
Huajun Lie18c65b2013-11-10 23:13:19 +080064
Chao Yu0bfcfcc2015-03-10 13:16:25 +080065bool truncate_inline_inode(struct page *ipage, u64 from)
Chao Yufeeb0de2015-01-26 20:22:55 +080066{
Chao Yu0bfcfcc2015-03-10 13:16:25 +080067 void *addr;
68
Chao Yu0bfcfcc2015-03-10 13:16:25 +080069 if (from >= MAX_INLINE_DATA)
70 return false;
71
72 addr = inline_data_addr(ipage);
73
Jaegeuk Kimfec1d652016-01-20 23:43:51 +080074 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080075 memset(addr + from, 0, MAX_INLINE_DATA - from);
76
77 return true;
Chao Yufeeb0de2015-01-26 20:22:55 +080078}
79
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070080int f2fs_read_inline_data(struct inode *inode, struct page *page)
81{
82 struct page *ipage;
83
84 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
85 if (IS_ERR(ipage)) {
86 unlock_page(page);
87 return PTR_ERR(ipage);
88 }
89
90 if (!f2fs_has_inline_data(inode)) {
91 f2fs_put_page(ipage, 1);
92 return -EAGAIN;
93 }
94
95 if (page->index)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030096 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070097 else
98 read_inline_data(page, ipage);
99
100 SetPageUptodate(page);
101 f2fs_put_page(ipage, 1);
102 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800103 return 0;
104}
105
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700106int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800107{
Huajun Lie18c65b2013-11-10 23:13:19 +0800108 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700109 .sbi = F2FS_I_SB(dn->inode),
Huajun Lie18c65b2013-11-10 23:13:19 +0800110 .type = DATA,
111 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700112 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700113 .encrypted_page = NULL,
Huajun Lie18c65b2013-11-10 23:13:19 +0800114 };
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800115 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800116
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700117 if (!f2fs_exist_data(dn->inode))
118 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700119
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700120 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900121 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700122 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800123
Yunlei He85ead812016-02-03 10:26:13 +0800124 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700125
Shawn Lin80606562016-02-19 16:02:51 +0800126 read_inline_data(page, dn->inode_page);
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700127 set_page_dirty(page);
128
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800129 /* clear dirty state */
130 dirty = clear_page_dirty_for_io(page);
131
Huajun Lie18c65b2013-11-10 23:13:19 +0800132 /* write data page to try to make data consistent */
133 set_page_writeback(page);
Chao Yu7a9d7542016-02-22 18:36:38 +0800134 fio.old_blkaddr = dn->data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700135 write_data_page(dn, &fio);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800136 f2fs_wait_on_page_writeback(page, DATA, true);
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800137 if (dirty)
138 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800139
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800140 /* this converted inline_data should be recovered. */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700141 set_inode_flag(dn->inode, FI_APPEND_WRITE);
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800142
Huajun Lie18c65b2013-11-10 23:13:19 +0800143 /* clear inline data and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800144 truncate_inline_inode(dn->inode_page, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800145 clear_inline_node(dn->inode_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700146clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700147 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800148 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700149 sync_inode_page(dn);
150 f2fs_put_dnode(dn);
151 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800152}
153
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700154int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800155{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700156 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
157 struct dnode_of_data dn;
158 struct page *ipage, *page;
159 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800160
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800161 if (!f2fs_has_inline_data(inode))
162 return 0;
163
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700164 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700165 if (!page)
166 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900167
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700168 f2fs_lock_op(sbi);
169
170 ipage = get_node_page(sbi, inode->i_ino);
171 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800172 err = PTR_ERR(ipage);
173 goto out;
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700174 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800175
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700176 set_new_dnode(&dn, inode, ipage, ipage, 0);
177
178 if (f2fs_has_inline_data(inode))
179 err = f2fs_convert_inline_page(&dn, page);
180
181 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800182out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700183 f2fs_unlock_op(sbi);
184
185 f2fs_put_page(page, 1);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800186
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800187 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800188
Huajun Lie18c65b2013-11-10 23:13:19 +0800189 return err;
190}
191
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700192int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800193{
194 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800195 struct dnode_of_data dn;
196 int err;
197
198 set_new_dnode(&dn, inode, NULL, NULL, 0);
199 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
200 if (err)
201 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800202
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700203 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700204 f2fs_put_dnode(&dn);
205 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700206 }
207
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700208 f2fs_bug_on(F2FS_I_SB(inode), page->index);
209
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800210 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700211 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700212 dst_addr = inline_data_addr(dn.inode_page);
213 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700214 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800215
Jaegeuk Kim91942322016-05-20 10:13:22 -0700216 set_inode_flag(inode, FI_APPEND_WRITE);
217 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700218
Huajun Lie18c65b2013-11-10 23:13:19 +0800219 sync_inode_page(&dn);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800220 clear_inline_node(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800221 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800222 return 0;
223}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900224
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700225bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900226{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700227 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900228 struct f2fs_inode *ri = NULL;
229 void *src_addr, *dst_addr;
230 struct page *ipage;
231
232 /*
233 * The inline_data recovery policy is as follows.
234 * [prev.] [next] of inline_data flag
235 * o o -> recover inline_data
236 * o x -> remove inline_data, and then recover data blocks
237 * x o -> remove inline_data, and then recover inline_data
238 * x x -> recover data blocks
239 */
240 if (IS_INODE(npage))
241 ri = F2FS_INODE(npage);
242
243 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700244 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900245process_inline:
246 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700247 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900248
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800249 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900250
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900251 src_addr = inline_data_addr(npage);
252 dst_addr = inline_data_addr(ipage);
253 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700254
Jaegeuk Kim91942322016-05-20 10:13:22 -0700255 set_inode_flag(inode, FI_INLINE_DATA);
256 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700257
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900258 update_inode(inode, ipage);
259 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700260 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900261 }
262
263 if (f2fs_has_inline_data(inode)) {
264 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700265 f2fs_bug_on(sbi, IS_ERR(ipage));
Nicholas Krause545fe422015-09-21 18:55:49 -0400266 if (!truncate_inline_inode(ipage, 0))
267 return false;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700268 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900269 update_inode(inode, ipage);
270 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700271 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Nicholas Krause545fe422015-09-21 18:55:49 -0400272 if (truncate_blocks(inode, 0, false))
273 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900274 goto process_inline;
275 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700276 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900277}
Chao Yu201a05b2014-09-24 18:17:53 +0800278
279struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700280 struct fscrypt_name *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800281{
282 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700283 struct f2fs_inline_dentry *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700284 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
Chao Yu201a05b2014-09-24 18:17:53 +0800285 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700286 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700287 struct page *ipage;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700288 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800289
290 ipage = get_node_page(sbi, dir->i_ino);
291 if (IS_ERR(ipage))
292 return NULL;
293
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700294 namehash = f2fs_dentry_hash(&name);
295
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700296 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800297
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700298 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700299 de = find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800300 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700301 if (de)
302 *res_page = ipage;
303 else
304 f2fs_put_page(ipage, 0);
305
Chao Yu201a05b2014-09-24 18:17:53 +0800306 return de;
307}
308
309struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
310 struct page **p)
311{
312 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
313 struct page *ipage;
314 struct f2fs_dir_entry *de;
315 struct f2fs_inline_dentry *dentry_blk;
316
317 ipage = get_node_page(sbi, dir->i_ino);
318 if (IS_ERR(ipage))
319 return NULL;
320
321 dentry_blk = inline_data_addr(ipage);
322 de = &dentry_blk->dentry[1];
323 *p = ipage;
324 unlock_page(ipage);
325 return de;
326}
327
328int make_empty_inline_dir(struct inode *inode, struct inode *parent,
329 struct page *ipage)
330{
331 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700332 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800333
334 dentry_blk = inline_data_addr(ipage);
335
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700336 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700337 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800338
339 set_page_dirty(ipage);
340
341 /* update i_size to MAX_INLINE_DATA */
342 if (i_size_read(inode) < MAX_INLINE_DATA) {
343 i_size_write(inode, MAX_INLINE_DATA);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700344 set_inode_flag(inode, FI_UPDATE_DIR);
Chao Yu201a05b2014-09-24 18:17:53 +0800345 }
346 return 0;
347}
348
Chao Yu470f00e2015-07-14 18:14:06 +0800349/*
350 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
351 * release ipage in this function.
352 */
Chao Yu675f10b2016-02-22 18:29:18 +0800353static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800354 struct f2fs_inline_dentry *inline_dentry)
355{
356 struct page *page;
357 struct dnode_of_data dn;
358 struct f2fs_dentry_block *dentry_blk;
359 int err;
360
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700361 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
Chao Yu470f00e2015-07-14 18:14:06 +0800362 if (!page) {
363 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800364 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800365 }
Chao Yu201a05b2014-09-24 18:17:53 +0800366
367 set_new_dnode(&dn, dir, ipage, NULL, 0);
368 err = f2fs_reserve_block(&dn, 0);
369 if (err)
370 goto out;
371
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800372 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300373 zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800374
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700375 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800376
377 /* copy data from inline dentry block to new dentry block */
378 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
379 INLINE_DENTRY_BITMAP_SIZE);
Chao Yu4ec17d62015-08-24 17:36:25 +0800380 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
381 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
382 /*
383 * we do not need to zero out remainder part of dentry and filename
384 * field, since we have used bitmap for marking the usage status of
385 * them, besides, we can also ignore copying/zeroing reserved space
386 * of dentry block, because them haven't been used so far.
387 */
Chao Yu201a05b2014-09-24 18:17:53 +0800388 memcpy(dentry_blk->dentry, inline_dentry->dentry,
389 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
390 memcpy(dentry_blk->filename, inline_dentry->filename,
391 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
392
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700393 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800394 SetPageUptodate(page);
395 set_page_dirty(page);
396
397 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800398 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700399
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700400 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700401 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800402
Chao Yue4103842016-05-14 19:03:52 +0800403 F2FS_I(dir)->i_current_depth = 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300404 if (i_size_read(dir) < PAGE_SIZE) {
405 i_size_write(dir, PAGE_SIZE);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700406 set_inode_flag(dir, FI_UPDATE_DIR);
Chao Yu201a05b2014-09-24 18:17:53 +0800407 }
408
409 sync_inode_page(&dn);
410out:
411 f2fs_put_page(page, 1);
412 return err;
413}
414
Chao Yu675f10b2016-02-22 18:29:18 +0800415static int f2fs_add_inline_entries(struct inode *dir,
416 struct f2fs_inline_dentry *inline_dentry)
417{
418 struct f2fs_dentry_ptr d;
419 unsigned long bit_pos = 0;
420 int err = 0;
421
422 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
423
424 while (bit_pos < d.max) {
425 struct f2fs_dir_entry *de;
426 struct qstr new_name;
427 nid_t ino;
428 umode_t fake_mode;
429
430 if (!test_bit_le(bit_pos, d.bitmap)) {
431 bit_pos++;
432 continue;
433 }
434
435 de = &d.dentry[bit_pos];
Chao Yua4a13f52016-04-27 22:22:20 +0800436
437 if (unlikely(!de->name_len)) {
438 bit_pos++;
439 continue;
440 }
441
Chao Yu675f10b2016-02-22 18:29:18 +0800442 new_name.name = d.filename[bit_pos];
443 new_name.len = de->name_len;
444
445 ino = le32_to_cpu(de->ino);
446 fake_mode = get_de_type(de) << S_SHIFT;
447
448 err = f2fs_add_regular_entry(dir, &new_name, NULL,
449 ino, fake_mode);
450 if (err)
451 goto punch_dentry_pages;
452
Chao Yu675f10b2016-02-22 18:29:18 +0800453 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
454 }
455 return 0;
456punch_dentry_pages:
457 truncate_inode_pages(&dir->i_data, 0);
458 truncate_blocks(dir, 0, false);
459 remove_dirty_inode(dir);
460 return err;
461}
462
463static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
464 struct f2fs_inline_dentry *inline_dentry)
465{
466 struct f2fs_inline_dentry *backup_dentry;
467 int err;
468
Jaegeuk Kim0414b002016-04-29 15:16:42 -0700469 backup_dentry = f2fs_kmalloc(sizeof(struct f2fs_inline_dentry),
Chao Yu675f10b2016-02-22 18:29:18 +0800470 GFP_F2FS_ZERO);
Chao Yu8975bdf2016-05-14 19:03:53 +0800471 if (!backup_dentry) {
472 f2fs_put_page(ipage, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800473 return -ENOMEM;
Chao Yu8975bdf2016-05-14 19:03:53 +0800474 }
Chao Yu675f10b2016-02-22 18:29:18 +0800475
476 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA);
477 truncate_inline_inode(ipage, 0);
478
479 unlock_page(ipage);
480
481 err = f2fs_add_inline_entries(dir, backup_dentry);
482 if (err)
483 goto recover;
484
485 lock_page(ipage);
486
487 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700488 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu675f10b2016-02-22 18:29:18 +0800489 update_inode(dir, ipage);
490 kfree(backup_dentry);
491 return 0;
492recover:
493 lock_page(ipage);
494 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700495 F2FS_I(dir)->i_current_depth = 0;
Chao Yu675f10b2016-02-22 18:29:18 +0800496 i_size_write(dir, MAX_INLINE_DATA);
497 update_inode(dir, ipage);
498 f2fs_put_page(ipage, 1);
499
500 kfree(backup_dentry);
501 return err;
502}
503
504static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
505 struct f2fs_inline_dentry *inline_dentry)
506{
507 if (!F2FS_I(dir)->i_dir_level)
508 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
509 else
510 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
511}
512
Chao Yu201a05b2014-09-24 18:17:53 +0800513int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700514 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800515{
516 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
517 struct page *ipage;
518 unsigned int bit_pos;
519 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800520 size_t namelen = name->len;
521 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800522 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800523 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700524 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800525 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800526
527 ipage = get_node_page(sbi, dir->i_ino);
528 if (IS_ERR(ipage))
529 return PTR_ERR(ipage);
530
531 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700532 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
533 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800534 if (bit_pos >= NR_INLINE_DENTRY) {
535 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
Chao Yu470f00e2015-07-14 18:14:06 +0800536 if (err)
537 return err;
538 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800539 goto out;
540 }
541
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700542 if (inode) {
543 down_write(&F2FS_I(inode)->i_sem);
544 page = init_inode_metadata(inode, dir, name, ipage);
545 if (IS_ERR(page)) {
546 err = PTR_ERR(page);
547 goto fail;
548 }
Chao Yu201a05b2014-09-24 18:17:53 +0800549 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700550
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800551 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu3b4d7322015-02-16 16:17:20 +0800552
553 name_hash = f2fs_dentry_hash(name);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700554 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700555 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800556
Chao Yu201a05b2014-09-24 18:17:53 +0800557 set_page_dirty(ipage);
558
559 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700560 if (inode) {
561 F2FS_I(inode)->i_pino = dir->i_ino;
562 update_inode(inode, page);
563 f2fs_put_page(page, 1);
564 }
Chao Yu201a05b2014-09-24 18:17:53 +0800565
566 update_parent_metadata(dir, inode, 0);
567fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700568 if (inode)
569 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800570
Jaegeuk Kim91942322016-05-20 10:13:22 -0700571 if (is_inode_flag_set(dir, FI_UPDATE_DIR)) {
Chao Yu201a05b2014-09-24 18:17:53 +0800572 update_inode(dir, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700573 clear_inode_flag(dir, FI_UPDATE_DIR);
Chao Yu201a05b2014-09-24 18:17:53 +0800574 }
575out:
576 f2fs_put_page(ipage, 1);
577 return err;
578}
579
580void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
581 struct inode *dir, struct inode *inode)
582{
583 struct f2fs_inline_dentry *inline_dentry;
584 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
585 unsigned int bit_pos;
586 int i;
587
588 lock_page(page);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800589 f2fs_wait_on_page_writeback(page, NODE, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800590
591 inline_dentry = inline_data_addr(page);
592 bit_pos = dentry - inline_dentry->dentry;
593 for (i = 0; i < slots; i++)
594 test_and_clear_bit_le(bit_pos + i,
595 &inline_dentry->dentry_bitmap);
596
597 set_page_dirty(page);
598
599 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
600
601 if (inode)
602 f2fs_drop_nlink(dir, inode, page);
603
604 f2fs_put_page(page, 1);
605}
606
607bool f2fs_empty_inline_dir(struct inode *dir)
608{
609 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
610 struct page *ipage;
611 unsigned int bit_pos = 2;
612 struct f2fs_inline_dentry *dentry_blk;
613
614 ipage = get_node_page(sbi, dir->i_ino);
615 if (IS_ERR(ipage))
616 return false;
617
618 dentry_blk = inline_data_addr(ipage);
619 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
620 NR_INLINE_DENTRY,
621 bit_pos);
622
623 f2fs_put_page(ipage, 1);
624
625 if (bit_pos < NR_INLINE_DENTRY)
626 return false;
627
628 return true;
629}
630
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700631int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700632 struct fscrypt_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800633{
634 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800635 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800636 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700637 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800638
639 if (ctx->pos == NR_INLINE_DENTRY)
640 return 0;
641
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700642 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800643 if (IS_ERR(ipage))
644 return PTR_ERR(ipage);
645
Chao Yu201a05b2014-09-24 18:17:53 +0800646 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800647
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700648 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700649
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700650 if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700651 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800652
Chao Yu201a05b2014-09-24 18:17:53 +0800653 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800654 return 0;
655}
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700656
657int f2fs_inline_data_fiemap(struct inode *inode,
658 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
659{
660 __u64 byteaddr, ilen;
661 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
662 FIEMAP_EXTENT_LAST;
663 struct node_info ni;
664 struct page *ipage;
665 int err = 0;
666
667 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
668 if (IS_ERR(ipage))
669 return PTR_ERR(ipage);
670
671 if (!f2fs_has_inline_data(inode)) {
672 err = -EAGAIN;
673 goto out;
674 }
675
676 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode));
677 if (start >= ilen)
678 goto out;
679 if (start + len < ilen)
680 ilen = start + len;
681 ilen -= start;
682
683 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
684 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
685 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage);
686 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
687out:
688 f2fs_put_page(ipage, 1);
689 return err;
690}