blob: 358214e9f70765887b1e1353efef1ec77555eb90 [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 Kim67f8cf3c2015-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
Chao Yu18309aa2013-12-30 09:29:06 +080054 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_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)
96 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
97 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 Kim158c1942014-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 Kim158c1942014-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 Kim158c1942014-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. */
141 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
142
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 Kimb3d208f2014-10-23 19:48:09 -0700164 page = grab_cache_page(inode->i_mapping, 0);
165 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 Kimb067ba12014-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 Kimfff04f92014-07-25 07:40:59 -0700216 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700217 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
218
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
255 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
256 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
257
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
306 /*
307 * For the most part, it should be a bug when name_len is zero.
308 * We stop here for figuring out where the bugs has occurred.
309 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700310 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800311 return de;
312}
313
314struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
315 struct page **p)
316{
317 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
318 struct page *ipage;
319 struct f2fs_dir_entry *de;
320 struct f2fs_inline_dentry *dentry_blk;
321
322 ipage = get_node_page(sbi, dir->i_ino);
323 if (IS_ERR(ipage))
324 return NULL;
325
326 dentry_blk = inline_data_addr(ipage);
327 de = &dentry_blk->dentry[1];
328 *p = ipage;
329 unlock_page(ipage);
330 return de;
331}
332
333int make_empty_inline_dir(struct inode *inode, struct inode *parent,
334 struct page *ipage)
335{
336 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700337 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800338
339 dentry_blk = inline_data_addr(ipage);
340
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700341 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700342 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800343
344 set_page_dirty(ipage);
345
346 /* update i_size to MAX_INLINE_DATA */
347 if (i_size_read(inode) < MAX_INLINE_DATA) {
348 i_size_write(inode, MAX_INLINE_DATA);
349 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
350 }
351 return 0;
352}
353
Chao Yu470f00e2015-07-14 18:14:06 +0800354/*
355 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
356 * release ipage in this function.
357 */
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700358static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800359 struct f2fs_inline_dentry *inline_dentry)
360{
361 struct page *page;
362 struct dnode_of_data dn;
363 struct f2fs_dentry_block *dentry_blk;
364 int err;
365
366 page = grab_cache_page(dir->i_mapping, 0);
Chao Yu470f00e2015-07-14 18:14:06 +0800367 if (!page) {
368 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800369 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800370 }
Chao Yu201a05b2014-09-24 18:17:53 +0800371
372 set_new_dnode(&dn, dir, ipage, NULL, 0);
373 err = f2fs_reserve_block(&dn, 0);
374 if (err)
375 goto out;
376
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800377 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu4ec17d62015-08-24 17:36:25 +0800378 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800379
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700380 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800381
382 /* copy data from inline dentry block to new dentry block */
383 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
384 INLINE_DENTRY_BITMAP_SIZE);
Chao Yu4ec17d62015-08-24 17:36:25 +0800385 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
386 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
387 /*
388 * we do not need to zero out remainder part of dentry and filename
389 * field, since we have used bitmap for marking the usage status of
390 * them, besides, we can also ignore copying/zeroing reserved space
391 * of dentry block, because them haven't been used so far.
392 */
Chao Yu201a05b2014-09-24 18:17:53 +0800393 memcpy(dentry_blk->dentry, inline_dentry->dentry,
394 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
395 memcpy(dentry_blk->filename, inline_dentry->filename,
396 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
397
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700398 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800399 SetPageUptodate(page);
400 set_page_dirty(page);
401
402 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800403 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700404
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700405 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800406 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800407
408 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
409 i_size_write(dir, PAGE_CACHE_SIZE);
410 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
411 }
412
413 sync_inode_page(&dn);
414out:
415 f2fs_put_page(page, 1);
416 return err;
417}
418
419int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700420 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800421{
422 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
423 struct page *ipage;
424 unsigned int bit_pos;
425 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800426 size_t namelen = name->len;
427 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800428 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800429 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700430 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800431 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800432
433 ipage = get_node_page(sbi, dir->i_ino);
434 if (IS_ERR(ipage))
435 return PTR_ERR(ipage);
436
437 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700438 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
439 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800440 if (bit_pos >= NR_INLINE_DENTRY) {
441 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
Chao Yu470f00e2015-07-14 18:14:06 +0800442 if (err)
443 return err;
444 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800445 goto out;
446 }
447
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700448 if (inode) {
449 down_write(&F2FS_I(inode)->i_sem);
450 page = init_inode_metadata(inode, dir, name, ipage);
451 if (IS_ERR(page)) {
452 err = PTR_ERR(page);
453 goto fail;
454 }
Chao Yu201a05b2014-09-24 18:17:53 +0800455 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700456
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800457 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu3b4d7322015-02-16 16:17:20 +0800458
459 name_hash = f2fs_dentry_hash(name);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700460 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700461 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800462
Chao Yu201a05b2014-09-24 18:17:53 +0800463 set_page_dirty(ipage);
464
465 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700466 if (inode) {
467 F2FS_I(inode)->i_pino = dir->i_ino;
468 update_inode(inode, page);
469 f2fs_put_page(page, 1);
470 }
Chao Yu201a05b2014-09-24 18:17:53 +0800471
472 update_parent_metadata(dir, inode, 0);
473fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700474 if (inode)
475 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800476
477 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
478 update_inode(dir, ipage);
479 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
480 }
481out:
482 f2fs_put_page(ipage, 1);
483 return err;
484}
485
486void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
487 struct inode *dir, struct inode *inode)
488{
489 struct f2fs_inline_dentry *inline_dentry;
490 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
491 unsigned int bit_pos;
492 int i;
493
494 lock_page(page);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800495 f2fs_wait_on_page_writeback(page, NODE, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800496
497 inline_dentry = inline_data_addr(page);
498 bit_pos = dentry - inline_dentry->dentry;
499 for (i = 0; i < slots; i++)
500 test_and_clear_bit_le(bit_pos + i,
501 &inline_dentry->dentry_bitmap);
502
503 set_page_dirty(page);
504
505 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
506
507 if (inode)
508 f2fs_drop_nlink(dir, inode, page);
509
510 f2fs_put_page(page, 1);
511}
512
513bool f2fs_empty_inline_dir(struct inode *dir)
514{
515 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
516 struct page *ipage;
517 unsigned int bit_pos = 2;
518 struct f2fs_inline_dentry *dentry_blk;
519
520 ipage = get_node_page(sbi, dir->i_ino);
521 if (IS_ERR(ipage))
522 return false;
523
524 dentry_blk = inline_data_addr(ipage);
525 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
526 NR_INLINE_DENTRY,
527 bit_pos);
528
529 f2fs_put_page(ipage, 1);
530
531 if (bit_pos < NR_INLINE_DENTRY)
532 return false;
533
534 return true;
535}
536
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700537int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700538 struct fscrypt_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800539{
540 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800541 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800542 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700543 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800544
545 if (ctx->pos == NR_INLINE_DENTRY)
546 return 0;
547
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700548 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800549 if (IS_ERR(ipage))
550 return PTR_ERR(ipage);
551
Chao Yu201a05b2014-09-24 18:17:53 +0800552 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800553
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700554 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700555
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700556 if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700557 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800558
Chao Yu201a05b2014-09-24 18:17:53 +0800559 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800560 return 0;
561}
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700562
563int f2fs_inline_data_fiemap(struct inode *inode,
564 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
565{
566 __u64 byteaddr, ilen;
567 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
568 FIEMAP_EXTENT_LAST;
569 struct node_info ni;
570 struct page *ipage;
571 int err = 0;
572
573 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
574 if (IS_ERR(ipage))
575 return PTR_ERR(ipage);
576
577 if (!f2fs_has_inline_data(inode)) {
578 err = -EAGAIN;
579 goto out;
580 }
581
582 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode));
583 if (start >= ilen)
584 goto out;
585 if (start + len < ilen)
586 ilen = start + len;
587 ilen -= start;
588
589 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
590 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
591 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage);
592 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
593out:
594 f2fs_put_page(ipage, 1);
595 return err;
596}