blob: 3d143be428959153872146e4c4bba4278fec2da5 [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"
15
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070016bool f2fs_may_inline_data(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +080017{
Jaegeuk Kim40813632014-09-02 15:31:18 -070018 if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
Huajun Lie18c65b2013-11-10 23:13:19 +080019 return false;
20
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070021 if (f2fs_is_atomic_file(inode))
22 return false;
23
Wanpeng Li368a0e42015-03-19 13:23:48 +080024 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
Huajun Lie18c65b2013-11-10 23:13:19 +080025 return false;
26
Jaegeuk Kim92dffd02014-11-11 14:10:01 -080027 if (i_size_read(inode) > MAX_INLINE_DATA)
28 return false;
29
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070030 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
31 return false;
32
Huajun Lie18c65b2013-11-10 23:13:19 +080033 return true;
34}
35
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070036bool f2fs_may_inline_dentry(struct inode *inode)
37{
38 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
39 return false;
40
41 if (!S_ISDIR(inode->i_mode))
42 return false;
43
44 return true;
45}
46
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070047void read_inline_data(struct page *page, struct page *ipage)
Huajun Lie18c65b2013-11-10 23:13:19 +080048{
Huajun Lie18c65b2013-11-10 23:13:19 +080049 void *src_addr, *dst_addr;
50
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070051 if (PageUptodate(page))
52 return;
Chao Yu04a17fb2013-12-30 18:36:23 +080053
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070054 f2fs_bug_on(F2FS_P_SB(page), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080055
Chao Yu18309aa2013-12-30 09:29:06 +080056 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080057
58 /* Copy the whole inline data block */
59 src_addr = inline_data_addr(ipage);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070060 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080061 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim427a45c2014-10-26 22:59:27 -070062 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070063 kunmap_atomic(dst_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +080064 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070065}
Huajun Lie18c65b2013-11-10 23:13:19 +080066
Chao Yu0bfcfcc2015-03-10 13:16:25 +080067bool truncate_inline_inode(struct page *ipage, u64 from)
Chao Yufeeb0de2015-01-26 20:22:55 +080068{
Chao Yu0bfcfcc2015-03-10 13:16:25 +080069 void *addr;
70
Chao Yu0bfcfcc2015-03-10 13:16:25 +080071 if (from >= MAX_INLINE_DATA)
72 return false;
73
74 addr = inline_data_addr(ipage);
75
Chao Yufeeb0de2015-01-26 20:22:55 +080076 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080077 memset(addr + from, 0, MAX_INLINE_DATA - from);
78
79 return true;
Chao Yufeeb0de2015-01-26 20:22:55 +080080}
81
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070082int f2fs_read_inline_data(struct inode *inode, struct page *page)
83{
84 struct page *ipage;
85
86 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
87 if (IS_ERR(ipage)) {
88 unlock_page(page);
89 return PTR_ERR(ipage);
90 }
91
92 if (!f2fs_has_inline_data(inode)) {
93 f2fs_put_page(ipage, 1);
94 return -EAGAIN;
95 }
96
97 if (page->index)
98 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
99 else
100 read_inline_data(page, ipage);
101
102 SetPageUptodate(page);
103 f2fs_put_page(ipage, 1);
104 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800105 return 0;
106}
107
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700108int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800109{
Huajun Lie18c65b2013-11-10 23:13:19 +0800110 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800111 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700112 .sbi = F2FS_I_SB(dn->inode),
Huajun Lie18c65b2013-11-10 23:13:19 +0800113 .type = DATA,
114 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700115 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700116 .encrypted_page = NULL,
Huajun Lie18c65b2013-11-10 23:13:19 +0800117 };
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800118 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800119
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700120 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +0800121
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700122 if (!f2fs_exist_data(dn->inode))
123 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700124
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700125 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900126 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700127 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800128
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900129 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700130
131 if (PageUptodate(page))
132 goto no_update;
133
Chao Yu18309aa2013-12-30 09:29:06 +0800134 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800135
136 /* Copy the whole inline data block */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700137 src_addr = inline_data_addr(dn->inode_page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700138 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800139 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim09b8b3c2014-11-18 10:50:21 -0800140 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700141 kunmap_atomic(dst_addr);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900142 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700143no_update:
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700144 set_page_dirty(page);
145
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800146 /* clear dirty state */
147 dirty = clear_page_dirty_for_io(page);
148
Huajun Lie18c65b2013-11-10 23:13:19 +0800149 /* write data page to try to make data consistent */
150 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800151 fio.blk_addr = dn->data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700152 write_data_page(dn, &fio);
Chao Yu216a6202015-03-19 19:23:32 +0800153 set_data_blkaddr(dn);
Chao Yu7e4dde72015-02-05 17:51:34 +0800154 f2fs_update_extent_cache(dn);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000155 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800156 if (dirty)
157 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800158
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800159 /* this converted inline_data should be recovered. */
160 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
161
Huajun Lie18c65b2013-11-10 23:13:19 +0800162 /* clear inline data and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800163 truncate_inline_inode(dn->inode_page, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700164clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700165 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800166 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700167 sync_inode_page(dn);
168 f2fs_put_dnode(dn);
169 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800170}
171
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700172int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800173{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700174 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
175 struct dnode_of_data dn;
176 struct page *ipage, *page;
177 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800178
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700179 page = grab_cache_page(inode->i_mapping, 0);
180 if (!page)
181 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900182
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700183 f2fs_lock_op(sbi);
184
185 ipage = get_node_page(sbi, inode->i_ino);
186 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800187 err = PTR_ERR(ipage);
188 goto out;
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700189 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800190
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700191 set_new_dnode(&dn, inode, ipage, ipage, 0);
192
193 if (f2fs_has_inline_data(inode))
194 err = f2fs_convert_inline_page(&dn, page);
195
196 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800197out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700198 f2fs_unlock_op(sbi);
199
200 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800201 return err;
202}
203
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700204int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800205{
206 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800207 struct dnode_of_data dn;
208 int err;
209
210 set_new_dnode(&dn, inode, NULL, NULL, 0);
211 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
212 if (err)
213 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800214
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700215 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700216 f2fs_put_dnode(&dn);
217 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700218 }
219
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700220 f2fs_bug_on(F2FS_I_SB(inode), page->index);
221
222 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700223 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700224 dst_addr = inline_data_addr(dn.inode_page);
225 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700226 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800227
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700228 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700229 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
230
Huajun Lie18c65b2013-11-10 23:13:19 +0800231 sync_inode_page(&dn);
232 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800233 return 0;
234}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900235
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700236bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900237{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700238 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900239 struct f2fs_inode *ri = NULL;
240 void *src_addr, *dst_addr;
241 struct page *ipage;
242
243 /*
244 * The inline_data recovery policy is as follows.
245 * [prev.] [next] of inline_data flag
246 * o o -> recover inline_data
247 * o x -> remove inline_data, and then recover data blocks
248 * x o -> remove inline_data, and then recover inline_data
249 * x x -> recover data blocks
250 */
251 if (IS_INODE(npage))
252 ri = F2FS_INODE(npage);
253
254 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700255 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900256process_inline:
257 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700258 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900259
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900260 f2fs_wait_on_page_writeback(ipage, NODE);
261
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900262 src_addr = inline_data_addr(npage);
263 dst_addr = inline_data_addr(ipage);
264 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700265
266 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
267 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
268
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 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900272 }
273
274 if (f2fs_has_inline_data(inode)) {
275 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700276 f2fs_bug_on(sbi, IS_ERR(ipage));
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800277 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700278 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900279 update_inode(inode, ipage);
280 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700281 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700282 truncate_blocks(inode, 0, false);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900283 goto process_inline;
284 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700285 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900286}
Chao Yu201a05b2014-09-24 18:17:53 +0800287
288struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700289 struct f2fs_filename *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800290{
291 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700292 struct f2fs_inline_dentry *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700293 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
Chao Yu201a05b2014-09-24 18:17:53 +0800294 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700295 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700296 struct page *ipage;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700297 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800298
299 ipage = get_node_page(sbi, dir->i_ino);
300 if (IS_ERR(ipage))
301 return NULL;
302
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700303 namehash = f2fs_dentry_hash(&name);
304
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700305 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800306
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700307 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700308 de = find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800309 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700310 if (de)
311 *res_page = ipage;
312 else
313 f2fs_put_page(ipage, 0);
314
315 /*
316 * For the most part, it should be a bug when name_len is zero.
317 * We stop here for figuring out where the bugs has occurred.
318 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700319 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800320 return de;
321}
322
323struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
324 struct page **p)
325{
326 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
327 struct page *ipage;
328 struct f2fs_dir_entry *de;
329 struct f2fs_inline_dentry *dentry_blk;
330
331 ipage = get_node_page(sbi, dir->i_ino);
332 if (IS_ERR(ipage))
333 return NULL;
334
335 dentry_blk = inline_data_addr(ipage);
336 de = &dentry_blk->dentry[1];
337 *p = ipage;
338 unlock_page(ipage);
339 return de;
340}
341
342int make_empty_inline_dir(struct inode *inode, struct inode *parent,
343 struct page *ipage)
344{
345 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700346 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800347
348 dentry_blk = inline_data_addr(ipage);
349
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700350 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700351 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800352
353 set_page_dirty(ipage);
354
355 /* update i_size to MAX_INLINE_DATA */
356 if (i_size_read(inode) < MAX_INLINE_DATA) {
357 i_size_write(inode, MAX_INLINE_DATA);
358 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
359 }
360 return 0;
361}
362
Chao Yu470f00e2015-07-14 18:14:06 +0800363/*
364 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
365 * release ipage in this function.
366 */
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700367static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800368 struct f2fs_inline_dentry *inline_dentry)
369{
370 struct page *page;
371 struct dnode_of_data dn;
372 struct f2fs_dentry_block *dentry_blk;
373 int err;
374
375 page = grab_cache_page(dir->i_mapping, 0);
Chao Yu470f00e2015-07-14 18:14:06 +0800376 if (!page) {
377 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800378 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800379 }
Chao Yu201a05b2014-09-24 18:17:53 +0800380
381 set_new_dnode(&dn, dir, ipage, NULL, 0);
382 err = f2fs_reserve_block(&dn, 0);
383 if (err)
384 goto out;
385
386 f2fs_wait_on_page_writeback(page, DATA);
Chao Yu4ec17d62015-08-24 17:36:25 +0800387 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800388
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700389 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800390
391 /* copy data from inline dentry block to new dentry block */
392 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
393 INLINE_DENTRY_BITMAP_SIZE);
Chao Yu4ec17d62015-08-24 17:36:25 +0800394 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
395 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
396 /*
397 * we do not need to zero out remainder part of dentry and filename
398 * field, since we have used bitmap for marking the usage status of
399 * them, besides, we can also ignore copying/zeroing reserved space
400 * of dentry block, because them haven't been used so far.
401 */
Chao Yu201a05b2014-09-24 18:17:53 +0800402 memcpy(dentry_blk->dentry, inline_dentry->dentry,
403 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
404 memcpy(dentry_blk->filename, inline_dentry->filename,
405 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
406
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700407 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800408 SetPageUptodate(page);
409 set_page_dirty(page);
410
411 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800412 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700413
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700414 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800415 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800416
417 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
418 i_size_write(dir, PAGE_CACHE_SIZE);
419 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
420 }
421
422 sync_inode_page(&dn);
423out:
424 f2fs_put_page(page, 1);
425 return err;
426}
427
428int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700429 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800430{
431 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
432 struct page *ipage;
433 unsigned int bit_pos;
434 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800435 size_t namelen = name->len;
436 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800437 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800438 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700439 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800440 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800441
442 ipage = get_node_page(sbi, dir->i_ino);
443 if (IS_ERR(ipage))
444 return PTR_ERR(ipage);
445
446 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700447 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
448 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800449 if (bit_pos >= NR_INLINE_DENTRY) {
450 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
Chao Yu470f00e2015-07-14 18:14:06 +0800451 if (err)
452 return err;
453 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800454 goto out;
455 }
456
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700457 if (inode) {
458 down_write(&F2FS_I(inode)->i_sem);
459 page = init_inode_metadata(inode, dir, name, ipage);
460 if (IS_ERR(page)) {
461 err = PTR_ERR(page);
462 goto fail;
463 }
Chao Yu201a05b2014-09-24 18:17:53 +0800464 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700465
466 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu3b4d7322015-02-16 16:17:20 +0800467
468 name_hash = f2fs_dentry_hash(name);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700469 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700470 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800471
Chao Yu201a05b2014-09-24 18:17:53 +0800472 set_page_dirty(ipage);
473
474 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700475 if (inode) {
476 F2FS_I(inode)->i_pino = dir->i_ino;
477 update_inode(inode, page);
478 f2fs_put_page(page, 1);
479 }
Chao Yu201a05b2014-09-24 18:17:53 +0800480
481 update_parent_metadata(dir, inode, 0);
482fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700483 if (inode)
484 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800485
486 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
487 update_inode(dir, ipage);
488 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
489 }
490out:
491 f2fs_put_page(ipage, 1);
492 return err;
493}
494
495void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
496 struct inode *dir, struct inode *inode)
497{
498 struct f2fs_inline_dentry *inline_dentry;
499 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
500 unsigned int bit_pos;
501 int i;
502
503 lock_page(page);
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700504 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800505
506 inline_dentry = inline_data_addr(page);
507 bit_pos = dentry - inline_dentry->dentry;
508 for (i = 0; i < slots; i++)
509 test_and_clear_bit_le(bit_pos + i,
510 &inline_dentry->dentry_bitmap);
511
512 set_page_dirty(page);
513
514 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
515
516 if (inode)
517 f2fs_drop_nlink(dir, inode, page);
518
519 f2fs_put_page(page, 1);
520}
521
522bool f2fs_empty_inline_dir(struct inode *dir)
523{
524 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
525 struct page *ipage;
526 unsigned int bit_pos = 2;
527 struct f2fs_inline_dentry *dentry_blk;
528
529 ipage = get_node_page(sbi, dir->i_ino);
530 if (IS_ERR(ipage))
531 return false;
532
533 dentry_blk = inline_data_addr(ipage);
534 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
535 NR_INLINE_DENTRY,
536 bit_pos);
537
538 f2fs_put_page(ipage, 1);
539
540 if (bit_pos < NR_INLINE_DENTRY)
541 return false;
542
543 return true;
544}
545
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700546int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
547 struct f2fs_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800548{
549 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800550 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800551 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700552 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800553
554 if (ctx->pos == NR_INLINE_DENTRY)
555 return 0;
556
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700557 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800558 if (IS_ERR(ipage))
559 return PTR_ERR(ipage);
560
Chao Yu201a05b2014-09-24 18:17:53 +0800561 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800562
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700563 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700564
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700565 if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700566 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800567
Chao Yu201a05b2014-09-24 18:17:53 +0800568 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800569 return 0;
570}