blob: d40261ca0f29fb5da5af9e34a8f7a6126c04f3dc [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
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 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);
Chao Yu216a6202015-03-19 19:23:32 +0800136 set_data_blkaddr(dn);
Chao Yu7e4dde72015-02-05 17:51:34 +0800137 f2fs_update_extent_cache(dn);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800138 f2fs_wait_on_page_writeback(page, DATA, true);
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800139 if (dirty)
140 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800141
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800142 /* this converted inline_data should be recovered. */
143 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
144
Huajun Lie18c65b2013-11-10 23:13:19 +0800145 /* clear inline data and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800146 truncate_inline_inode(dn->inode_page, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800147 clear_inline_node(dn->inode_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700148clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700149 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800150 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700151 sync_inode_page(dn);
152 f2fs_put_dnode(dn);
153 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800154}
155
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700156int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800157{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700158 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
159 struct dnode_of_data dn;
160 struct page *ipage, *page;
161 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800162
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800163 if (!f2fs_has_inline_data(inode))
164 return 0;
165
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700166 page = grab_cache_page(inode->i_mapping, 0);
167 if (!page)
168 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900169
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700170 f2fs_lock_op(sbi);
171
172 ipage = get_node_page(sbi, inode->i_ino);
173 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800174 err = PTR_ERR(ipage);
175 goto out;
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700176 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800177
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700178 set_new_dnode(&dn, inode, ipage, ipage, 0);
179
180 if (f2fs_has_inline_data(inode))
181 err = f2fs_convert_inline_page(&dn, page);
182
183 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800184out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700185 f2fs_unlock_op(sbi);
186
187 f2fs_put_page(page, 1);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800188
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800189 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800190
Huajun Lie18c65b2013-11-10 23:13:19 +0800191 return err;
192}
193
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700194int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800195{
196 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800197 struct dnode_of_data dn;
198 int err;
199
200 set_new_dnode(&dn, inode, NULL, NULL, 0);
201 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
202 if (err)
203 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800204
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700205 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700206 f2fs_put_dnode(&dn);
207 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700208 }
209
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700210 f2fs_bug_on(F2FS_I_SB(inode), page->index);
211
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800212 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700213 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700214 dst_addr = inline_data_addr(dn.inode_page);
215 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700216 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800217
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700218 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700219 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
220
Huajun Lie18c65b2013-11-10 23:13:19 +0800221 sync_inode_page(&dn);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800222 clear_inline_node(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800223 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800224 return 0;
225}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900226
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700227bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900228{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700229 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900230 struct f2fs_inode *ri = NULL;
231 void *src_addr, *dst_addr;
232 struct page *ipage;
233
234 /*
235 * The inline_data recovery policy is as follows.
236 * [prev.] [next] of inline_data flag
237 * o o -> recover inline_data
238 * o x -> remove inline_data, and then recover data blocks
239 * x o -> remove inline_data, and then recover inline_data
240 * x x -> recover data blocks
241 */
242 if (IS_INODE(npage))
243 ri = F2FS_INODE(npage);
244
245 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700246 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900247process_inline:
248 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700249 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900250
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800251 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900252
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900253 src_addr = inline_data_addr(npage);
254 dst_addr = inline_data_addr(ipage);
255 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700256
257 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
258 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
259
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900260 update_inode(inode, ipage);
261 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700262 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900263 }
264
265 if (f2fs_has_inline_data(inode)) {
266 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700267 f2fs_bug_on(sbi, IS_ERR(ipage));
Nicholas Krause545fe422015-09-21 18:55:49 -0400268 if (!truncate_inline_inode(ipage, 0))
269 return false;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700270 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900271 update_inode(inode, ipage);
272 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700273 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Nicholas Krause545fe422015-09-21 18:55:49 -0400274 if (truncate_blocks(inode, 0, false))
275 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900276 goto process_inline;
277 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700278 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900279}
Chao Yu201a05b2014-09-24 18:17:53 +0800280
281struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700282 struct f2fs_filename *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800283{
284 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700285 struct f2fs_inline_dentry *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700286 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
Chao Yu201a05b2014-09-24 18:17:53 +0800287 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700288 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700289 struct page *ipage;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700290 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800291
292 ipage = get_node_page(sbi, dir->i_ino);
293 if (IS_ERR(ipage))
294 return NULL;
295
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700296 namehash = f2fs_dentry_hash(&name);
297
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700298 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800299
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700300 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700301 de = find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800302 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700303 if (de)
304 *res_page = ipage;
305 else
306 f2fs_put_page(ipage, 0);
307
308 /*
309 * For the most part, it should be a bug when name_len is zero.
310 * We stop here for figuring out where the bugs has occurred.
311 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700312 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800313 return de;
314}
315
316struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
317 struct page **p)
318{
319 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
320 struct page *ipage;
321 struct f2fs_dir_entry *de;
322 struct f2fs_inline_dentry *dentry_blk;
323
324 ipage = get_node_page(sbi, dir->i_ino);
325 if (IS_ERR(ipage))
326 return NULL;
327
328 dentry_blk = inline_data_addr(ipage);
329 de = &dentry_blk->dentry[1];
330 *p = ipage;
331 unlock_page(ipage);
332 return de;
333}
334
335int make_empty_inline_dir(struct inode *inode, struct inode *parent,
336 struct page *ipage)
337{
338 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700339 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800340
341 dentry_blk = inline_data_addr(ipage);
342
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700343 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700344 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800345
346 set_page_dirty(ipage);
347
348 /* update i_size to MAX_INLINE_DATA */
349 if (i_size_read(inode) < MAX_INLINE_DATA) {
350 i_size_write(inode, MAX_INLINE_DATA);
351 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
352 }
353 return 0;
354}
355
Chao Yu470f00e2015-07-14 18:14:06 +0800356/*
357 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
358 * release ipage in this function.
359 */
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700360static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800361 struct f2fs_inline_dentry *inline_dentry)
362{
363 struct page *page;
364 struct dnode_of_data dn;
365 struct f2fs_dentry_block *dentry_blk;
366 int err;
367
368 page = grab_cache_page(dir->i_mapping, 0);
Chao Yu470f00e2015-07-14 18:14:06 +0800369 if (!page) {
370 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800371 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800372 }
Chao Yu201a05b2014-09-24 18:17:53 +0800373
374 set_new_dnode(&dn, dir, ipage, NULL, 0);
375 err = f2fs_reserve_block(&dn, 0);
376 if (err)
377 goto out;
378
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800379 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yu4ec17d62015-08-24 17:36:25 +0800380 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800381
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700382 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800383
384 /* copy data from inline dentry block to new dentry block */
385 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
386 INLINE_DENTRY_BITMAP_SIZE);
Chao Yu4ec17d62015-08-24 17:36:25 +0800387 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
388 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
389 /*
390 * we do not need to zero out remainder part of dentry and filename
391 * field, since we have used bitmap for marking the usage status of
392 * them, besides, we can also ignore copying/zeroing reserved space
393 * of dentry block, because them haven't been used so far.
394 */
Chao Yu201a05b2014-09-24 18:17:53 +0800395 memcpy(dentry_blk->dentry, inline_dentry->dentry,
396 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
397 memcpy(dentry_blk->filename, inline_dentry->filename,
398 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
399
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700400 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800401 SetPageUptodate(page);
402 set_page_dirty(page);
403
404 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800405 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700406
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700407 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800408 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800409
410 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
411 i_size_write(dir, PAGE_CACHE_SIZE);
412 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
413 }
414
415 sync_inode_page(&dn);
416out:
417 f2fs_put_page(page, 1);
418 return err;
419}
420
421int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700422 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800423{
424 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
425 struct page *ipage;
426 unsigned int bit_pos;
427 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800428 size_t namelen = name->len;
429 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800430 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800431 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700432 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800433 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800434
435 ipage = get_node_page(sbi, dir->i_ino);
436 if (IS_ERR(ipage))
437 return PTR_ERR(ipage);
438
439 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700440 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
441 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800442 if (bit_pos >= NR_INLINE_DENTRY) {
443 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
Chao Yu470f00e2015-07-14 18:14:06 +0800444 if (err)
445 return err;
446 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800447 goto out;
448 }
449
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700450 if (inode) {
451 down_write(&F2FS_I(inode)->i_sem);
452 page = init_inode_metadata(inode, dir, name, ipage);
453 if (IS_ERR(page)) {
454 err = PTR_ERR(page);
455 goto fail;
456 }
Chao Yu201a05b2014-09-24 18:17:53 +0800457 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700458
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800459 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu3b4d7322015-02-16 16:17:20 +0800460
461 name_hash = f2fs_dentry_hash(name);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700462 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700463 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800464
Chao Yu201a05b2014-09-24 18:17:53 +0800465 set_page_dirty(ipage);
466
467 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700468 if (inode) {
469 F2FS_I(inode)->i_pino = dir->i_ino;
470 update_inode(inode, page);
471 f2fs_put_page(page, 1);
472 }
Chao Yu201a05b2014-09-24 18:17:53 +0800473
474 update_parent_metadata(dir, inode, 0);
475fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700476 if (inode)
477 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800478
479 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
480 update_inode(dir, ipage);
481 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
482 }
483out:
484 f2fs_put_page(ipage, 1);
485 return err;
486}
487
488void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
489 struct inode *dir, struct inode *inode)
490{
491 struct f2fs_inline_dentry *inline_dentry;
492 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
493 unsigned int bit_pos;
494 int i;
495
496 lock_page(page);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800497 f2fs_wait_on_page_writeback(page, NODE, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800498
499 inline_dentry = inline_data_addr(page);
500 bit_pos = dentry - inline_dentry->dentry;
501 for (i = 0; i < slots; i++)
502 test_and_clear_bit_le(bit_pos + i,
503 &inline_dentry->dentry_bitmap);
504
505 set_page_dirty(page);
506
507 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
508
509 if (inode)
510 f2fs_drop_nlink(dir, inode, page);
511
512 f2fs_put_page(page, 1);
513}
514
515bool f2fs_empty_inline_dir(struct inode *dir)
516{
517 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
518 struct page *ipage;
519 unsigned int bit_pos = 2;
520 struct f2fs_inline_dentry *dentry_blk;
521
522 ipage = get_node_page(sbi, dir->i_ino);
523 if (IS_ERR(ipage))
524 return false;
525
526 dentry_blk = inline_data_addr(ipage);
527 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
528 NR_INLINE_DENTRY,
529 bit_pos);
530
531 f2fs_put_page(ipage, 1);
532
533 if (bit_pos < NR_INLINE_DENTRY)
534 return false;
535
536 return true;
537}
538
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700539int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
540 struct f2fs_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800541{
542 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800543 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800544 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700545 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800546
547 if (ctx->pos == NR_INLINE_DENTRY)
548 return 0;
549
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700550 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800551 if (IS_ERR(ipage))
552 return PTR_ERR(ipage);
553
Chao Yu201a05b2014-09-24 18:17:53 +0800554 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800555
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700556 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700557
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700558 if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700559 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800560
Chao Yu201a05b2014-09-24 18:17:53 +0800561 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800562 return 0;
563}
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700564
565int f2fs_inline_data_fiemap(struct inode *inode,
566 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
567{
568 __u64 byteaddr, ilen;
569 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
570 FIEMAP_EXTENT_LAST;
571 struct node_info ni;
572 struct page *ipage;
573 int err = 0;
574
575 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
576 if (IS_ERR(ipage))
577 return PTR_ERR(ipage);
578
579 if (!f2fs_has_inline_data(inode)) {
580 err = -EAGAIN;
581 goto out;
582 }
583
584 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode));
585 if (start >= ilen)
586 goto out;
587 if (start + len < ilen)
588 ilen = start + len;
589 ilen -= start;
590
591 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
592 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
593 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage);
594 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
595out:
596 f2fs_put_page(ipage, 1);
597 return err;
598}