blob: bda7126466c09f9b16f4d275cac5b287a06c9142 [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 Kim40813632014-09-02 15:31:18 -070019 if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
Huajun Lie18c65b2013-11-10 23:13:19 +080020 return false;
21
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070022 if (f2fs_is_atomic_file(inode))
23 return false;
24
Wanpeng Li368a0e42015-03-19 13:23:48 +080025 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
Huajun Lie18c65b2013-11-10 23:13:19 +080026 return false;
27
Jaegeuk Kim92dffd02014-11-11 14:10:01 -080028 if (i_size_read(inode) > MAX_INLINE_DATA)
29 return false;
30
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070031 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
32 return false;
33
Huajun Lie18c65b2013-11-10 23:13:19 +080034 return true;
35}
36
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070037bool f2fs_may_inline_dentry(struct inode *inode)
38{
39 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
40 return false;
41
42 if (!S_ISDIR(inode->i_mode))
43 return false;
44
45 return true;
46}
47
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070048void read_inline_data(struct page *page, struct page *ipage)
Huajun Lie18c65b2013-11-10 23:13:19 +080049{
Huajun Lie18c65b2013-11-10 23:13:19 +080050 void *src_addr, *dst_addr;
51
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070052 if (PageUptodate(page))
53 return;
Chao Yu04a17fb2013-12-30 18:36:23 +080054
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070055 f2fs_bug_on(F2FS_P_SB(page), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080056
Chao Yu18309aa2013-12-30 09:29:06 +080057 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080058
59 /* Copy the whole inline data block */
60 src_addr = inline_data_addr(ipage);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070061 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080062 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim427a45c2014-10-26 22:59:27 -070063 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070064 kunmap_atomic(dst_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +080065 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070066}
Huajun Lie18c65b2013-11-10 23:13:19 +080067
Chao Yu0bfcfcc2015-03-10 13:16:25 +080068bool truncate_inline_inode(struct page *ipage, u64 from)
Chao Yufeeb0de2015-01-26 20:22:55 +080069{
Chao Yu0bfcfcc2015-03-10 13:16:25 +080070 void *addr;
71
Chao Yu0bfcfcc2015-03-10 13:16:25 +080072 if (from >= MAX_INLINE_DATA)
73 return false;
74
75 addr = inline_data_addr(ipage);
76
Chao Yufeeb0de2015-01-26 20:22:55 +080077 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080078 memset(addr + from, 0, MAX_INLINE_DATA - from);
79
80 return true;
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)
99 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
100 else
101 read_inline_data(page, ipage);
102
103 SetPageUptodate(page);
104 f2fs_put_page(ipage, 1);
105 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800106 return 0;
107}
108
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700109int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800110{
Huajun Lie18c65b2013-11-10 23:13:19 +0800111 void *src_addr, *dst_addr;
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,
115 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700116 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700117 .encrypted_page = NULL,
Huajun Lie18c65b2013-11-10 23:13:19 +0800118 };
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800119 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800120
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700121 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +0800122
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700123 if (!f2fs_exist_data(dn->inode))
124 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700125
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700126 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900127 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700128 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800129
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900130 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700131
132 if (PageUptodate(page))
133 goto no_update;
134
Chao Yu18309aa2013-12-30 09:29:06 +0800135 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800136
137 /* Copy the whole inline data block */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700138 src_addr = inline_data_addr(dn->inode_page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700139 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800140 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim09b8b3c2014-11-18 10:50:21 -0800141 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700142 kunmap_atomic(dst_addr);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900143 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700144no_update:
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700145 set_page_dirty(page);
146
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800147 /* clear dirty state */
148 dirty = clear_page_dirty_for_io(page);
149
Huajun Lie18c65b2013-11-10 23:13:19 +0800150 /* write data page to try to make data consistent */
151 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800152 fio.blk_addr = dn->data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700153 write_data_page(dn, &fio);
Chao Yu216a6202015-03-19 19:23:32 +0800154 set_data_blkaddr(dn);
Chao Yu7e4dde72015-02-05 17:51:34 +0800155 f2fs_update_extent_cache(dn);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000156 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800157 if (dirty)
158 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800159
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800160 /* this converted inline_data should be recovered. */
161 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
162
Huajun Lie18c65b2013-11-10 23:13:19 +0800163 /* clear inline data and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800164 truncate_inline_inode(dn->inode_page, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700165clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700166 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800167 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700168 sync_inode_page(dn);
169 f2fs_put_dnode(dn);
170 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800171}
172
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700173int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800174{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700175 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
176 struct dnode_of_data dn;
177 struct page *ipage, *page;
178 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800179
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700180 page = grab_cache_page(inode->i_mapping, 0);
181 if (!page)
182 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900183
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700184 f2fs_lock_op(sbi);
185
186 ipage = get_node_page(sbi, inode->i_ino);
187 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800188 err = PTR_ERR(ipage);
189 goto out;
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700190 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800191
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700192 set_new_dnode(&dn, inode, ipage, ipage, 0);
193
194 if (f2fs_has_inline_data(inode))
195 err = f2fs_convert_inline_page(&dn, page);
196
197 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800198out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700199 f2fs_unlock_op(sbi);
200
201 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800202 return err;
203}
204
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700205int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800206{
207 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800208 struct dnode_of_data dn;
209 int err;
210
211 set_new_dnode(&dn, inode, NULL, NULL, 0);
212 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
213 if (err)
214 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800215
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700216 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700217 f2fs_put_dnode(&dn);
218 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700219 }
220
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700221 f2fs_bug_on(F2FS_I_SB(inode), page->index);
222
223 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700224 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700225 dst_addr = inline_data_addr(dn.inode_page);
226 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700227 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800228
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700229 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700230 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
231
Huajun Lie18c65b2013-11-10 23:13:19 +0800232 sync_inode_page(&dn);
233 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800234 return 0;
235}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900236
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700237bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900238{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700239 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900240 struct f2fs_inode *ri = NULL;
241 void *src_addr, *dst_addr;
242 struct page *ipage;
243
244 /*
245 * The inline_data recovery policy is as follows.
246 * [prev.] [next] of inline_data flag
247 * o o -> recover inline_data
248 * o x -> remove inline_data, and then recover data blocks
249 * x o -> remove inline_data, and then recover inline_data
250 * x x -> recover data blocks
251 */
252 if (IS_INODE(npage))
253 ri = F2FS_INODE(npage);
254
255 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700256 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900257process_inline:
258 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700259 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900260
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900261 f2fs_wait_on_page_writeback(ipage, NODE);
262
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900263 src_addr = inline_data_addr(npage);
264 dst_addr = inline_data_addr(ipage);
265 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700266
267 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
268 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
269
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900270 update_inode(inode, ipage);
271 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700272 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900273 }
274
275 if (f2fs_has_inline_data(inode)) {
276 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700277 f2fs_bug_on(sbi, IS_ERR(ipage));
Nicholas Krause545fe422015-09-21 18:55:49 -0400278 if (!truncate_inline_inode(ipage, 0))
279 return false;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700280 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900281 update_inode(inode, ipage);
282 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700283 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Nicholas Krause545fe422015-09-21 18:55:49 -0400284 if (truncate_blocks(inode, 0, false))
285 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900286 goto process_inline;
287 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700288 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900289}
Chao Yu201a05b2014-09-24 18:17:53 +0800290
291struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700292 struct f2fs_filename *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800293{
294 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700295 struct f2fs_inline_dentry *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700296 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
Chao Yu201a05b2014-09-24 18:17:53 +0800297 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700298 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700299 struct page *ipage;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700300 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800301
302 ipage = get_node_page(sbi, dir->i_ino);
303 if (IS_ERR(ipage))
304 return NULL;
305
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700306 namehash = f2fs_dentry_hash(&name);
307
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700308 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800309
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700310 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700311 de = find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800312 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700313 if (de)
314 *res_page = ipage;
315 else
316 f2fs_put_page(ipage, 0);
317
318 /*
319 * For the most part, it should be a bug when name_len is zero.
320 * We stop here for figuring out where the bugs has occurred.
321 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700322 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800323 return de;
324}
325
326struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
327 struct page **p)
328{
329 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
330 struct page *ipage;
331 struct f2fs_dir_entry *de;
332 struct f2fs_inline_dentry *dentry_blk;
333
334 ipage = get_node_page(sbi, dir->i_ino);
335 if (IS_ERR(ipage))
336 return NULL;
337
338 dentry_blk = inline_data_addr(ipage);
339 de = &dentry_blk->dentry[1];
340 *p = ipage;
341 unlock_page(ipage);
342 return de;
343}
344
345int make_empty_inline_dir(struct inode *inode, struct inode *parent,
346 struct page *ipage)
347{
348 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700349 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800350
351 dentry_blk = inline_data_addr(ipage);
352
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700353 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700354 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800355
356 set_page_dirty(ipage);
357
358 /* update i_size to MAX_INLINE_DATA */
359 if (i_size_read(inode) < MAX_INLINE_DATA) {
360 i_size_write(inode, MAX_INLINE_DATA);
361 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
362 }
363 return 0;
364}
365
Chao Yu470f00e2015-07-14 18:14:06 +0800366/*
367 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
368 * release ipage in this function.
369 */
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700370static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800371 struct f2fs_inline_dentry *inline_dentry)
372{
373 struct page *page;
374 struct dnode_of_data dn;
375 struct f2fs_dentry_block *dentry_blk;
376 int err;
377
378 page = grab_cache_page(dir->i_mapping, 0);
Chao Yu470f00e2015-07-14 18:14:06 +0800379 if (!page) {
380 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800381 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800382 }
Chao Yu201a05b2014-09-24 18:17:53 +0800383
384 set_new_dnode(&dn, dir, ipage, NULL, 0);
385 err = f2fs_reserve_block(&dn, 0);
386 if (err)
387 goto out;
388
389 f2fs_wait_on_page_writeback(page, DATA);
Chao Yu4ec17d62015-08-24 17:36:25 +0800390 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800391
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700392 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800393
394 /* copy data from inline dentry block to new dentry block */
395 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
396 INLINE_DENTRY_BITMAP_SIZE);
Chao Yu4ec17d62015-08-24 17:36:25 +0800397 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
398 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
399 /*
400 * we do not need to zero out remainder part of dentry and filename
401 * field, since we have used bitmap for marking the usage status of
402 * them, besides, we can also ignore copying/zeroing reserved space
403 * of dentry block, because them haven't been used so far.
404 */
Chao Yu201a05b2014-09-24 18:17:53 +0800405 memcpy(dentry_blk->dentry, inline_dentry->dentry,
406 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
407 memcpy(dentry_blk->filename, inline_dentry->filename,
408 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
409
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700410 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800411 SetPageUptodate(page);
412 set_page_dirty(page);
413
414 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800415 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700416
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700417 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800418 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800419
420 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
421 i_size_write(dir, PAGE_CACHE_SIZE);
422 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
423 }
424
425 sync_inode_page(&dn);
426out:
427 f2fs_put_page(page, 1);
428 return err;
429}
430
431int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700432 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800433{
434 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
435 struct page *ipage;
436 unsigned int bit_pos;
437 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800438 size_t namelen = name->len;
439 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800440 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800441 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700442 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800443 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800444
445 ipage = get_node_page(sbi, dir->i_ino);
446 if (IS_ERR(ipage))
447 return PTR_ERR(ipage);
448
449 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700450 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
451 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800452 if (bit_pos >= NR_INLINE_DENTRY) {
453 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
Chao Yu470f00e2015-07-14 18:14:06 +0800454 if (err)
455 return err;
456 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800457 goto out;
458 }
459
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700460 if (inode) {
461 down_write(&F2FS_I(inode)->i_sem);
462 page = init_inode_metadata(inode, dir, name, ipage);
463 if (IS_ERR(page)) {
464 err = PTR_ERR(page);
465 goto fail;
466 }
Chao Yu201a05b2014-09-24 18:17:53 +0800467 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700468
469 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu3b4d7322015-02-16 16:17:20 +0800470
471 name_hash = f2fs_dentry_hash(name);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700472 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700473 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800474
Chao Yu201a05b2014-09-24 18:17:53 +0800475 set_page_dirty(ipage);
476
477 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700478 if (inode) {
479 F2FS_I(inode)->i_pino = dir->i_ino;
480 update_inode(inode, page);
481 f2fs_put_page(page, 1);
482 }
Chao Yu201a05b2014-09-24 18:17:53 +0800483
484 update_parent_metadata(dir, inode, 0);
485fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700486 if (inode)
487 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800488
489 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
490 update_inode(dir, ipage);
491 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
492 }
493out:
494 f2fs_put_page(ipage, 1);
495 return err;
496}
497
498void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
499 struct inode *dir, struct inode *inode)
500{
501 struct f2fs_inline_dentry *inline_dentry;
502 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
503 unsigned int bit_pos;
504 int i;
505
506 lock_page(page);
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700507 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800508
509 inline_dentry = inline_data_addr(page);
510 bit_pos = dentry - inline_dentry->dentry;
511 for (i = 0; i < slots; i++)
512 test_and_clear_bit_le(bit_pos + i,
513 &inline_dentry->dentry_bitmap);
514
515 set_page_dirty(page);
516
517 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
518
519 if (inode)
520 f2fs_drop_nlink(dir, inode, page);
521
522 f2fs_put_page(page, 1);
523}
524
525bool f2fs_empty_inline_dir(struct inode *dir)
526{
527 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
528 struct page *ipage;
529 unsigned int bit_pos = 2;
530 struct f2fs_inline_dentry *dentry_blk;
531
532 ipage = get_node_page(sbi, dir->i_ino);
533 if (IS_ERR(ipage))
534 return false;
535
536 dentry_blk = inline_data_addr(ipage);
537 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
538 NR_INLINE_DENTRY,
539 bit_pos);
540
541 f2fs_put_page(ipage, 1);
542
543 if (bit_pos < NR_INLINE_DENTRY)
544 return false;
545
546 return true;
547}
548
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700549int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
550 struct f2fs_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800551{
552 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800553 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800554 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700555 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800556
557 if (ctx->pos == NR_INLINE_DENTRY)
558 return 0;
559
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700560 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800561 if (IS_ERR(ipage))
562 return PTR_ERR(ipage);
563
Chao Yu201a05b2014-09-24 18:17:53 +0800564 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800565
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700566 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700567
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700568 if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700569 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800570
Chao Yu201a05b2014-09-24 18:17:53 +0800571 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800572 return 0;
573}
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700574
575int f2fs_inline_data_fiemap(struct inode *inode,
576 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
577{
578 __u64 byteaddr, ilen;
579 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
580 FIEMAP_EXTENT_LAST;
581 struct node_info ni;
582 struct page *ipage;
583 int err = 0;
584
585 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
586 if (IS_ERR(ipage))
587 return PTR_ERR(ipage);
588
589 if (!f2fs_has_inline_data(inode)) {
590 err = -EAGAIN;
591 goto out;
592 }
593
594 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode));
595 if (start >= ilen)
596 goto out;
597 if (start + len < ilen)
598 ilen = start + len;
599 ilen -= start;
600
601 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
602 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
603 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage);
604 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
605out:
606 f2fs_put_page(ipage, 1);
607 return err;
608}