blob: 5f5b34be5cd9eaa6e2976c3a7d032e583d4c9154 [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,
Huajun Lie18c65b2013-11-10 23:13:19 +0800116 };
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800117 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800118
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700119 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +0800120
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700121 if (!f2fs_exist_data(dn->inode))
122 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700123
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700124 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900125 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700126 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800127
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900128 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700129
130 if (PageUptodate(page))
131 goto no_update;
132
Chao Yu18309aa2013-12-30 09:29:06 +0800133 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800134
135 /* Copy the whole inline data block */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700136 src_addr = inline_data_addr(dn->inode_page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700137 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800138 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim09b8b3c2014-11-18 10:50:21 -0800139 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700140 kunmap_atomic(dst_addr);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900141 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700142no_update:
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800143 /* clear dirty state */
144 dirty = clear_page_dirty_for_io(page);
145
Huajun Lie18c65b2013-11-10 23:13:19 +0800146 /* write data page to try to make data consistent */
147 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800148 fio.blk_addr = dn->data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700149 write_data_page(dn, &fio);
Chao Yu216a6202015-03-19 19:23:32 +0800150 set_data_blkaddr(dn);
Chao Yu7e4dde72015-02-05 17:51:34 +0800151 f2fs_update_extent_cache(dn);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000152 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800153 if (dirty)
154 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800155
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800156 /* this converted inline_data should be recovered. */
157 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
158
Huajun Lie18c65b2013-11-10 23:13:19 +0800159 /* clear inline data and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800160 truncate_inline_inode(dn->inode_page, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700161clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700162 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800163 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700164 sync_inode_page(dn);
165 f2fs_put_dnode(dn);
166 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800167}
168
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700169int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800170{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700171 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
172 struct dnode_of_data dn;
173 struct page *ipage, *page;
174 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800175
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700176 page = grab_cache_page(inode->i_mapping, 0);
177 if (!page)
178 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900179
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700180 f2fs_lock_op(sbi);
181
182 ipage = get_node_page(sbi, inode->i_ino);
183 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800184 err = PTR_ERR(ipage);
185 goto out;
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700186 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800187
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700188 set_new_dnode(&dn, inode, ipage, ipage, 0);
189
190 if (f2fs_has_inline_data(inode))
191 err = f2fs_convert_inline_page(&dn, page);
192
193 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800194out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700195 f2fs_unlock_op(sbi);
196
197 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800198 return err;
199}
200
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700201int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800202{
203 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800204 struct dnode_of_data dn;
205 int err;
206
207 set_new_dnode(&dn, inode, NULL, NULL, 0);
208 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
209 if (err)
210 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800211
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700212 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700213 f2fs_put_dnode(&dn);
214 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700215 }
216
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700217 f2fs_bug_on(F2FS_I_SB(inode), page->index);
218
219 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700220 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700221 dst_addr = inline_data_addr(dn.inode_page);
222 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700223 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800224
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700225 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700226 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
227
Huajun Lie18c65b2013-11-10 23:13:19 +0800228 sync_inode_page(&dn);
229 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800230 return 0;
231}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900232
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700233bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900234{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700235 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900236 struct f2fs_inode *ri = NULL;
237 void *src_addr, *dst_addr;
238 struct page *ipage;
239
240 /*
241 * The inline_data recovery policy is as follows.
242 * [prev.] [next] of inline_data flag
243 * o o -> recover inline_data
244 * o x -> remove inline_data, and then recover data blocks
245 * x o -> remove inline_data, and then recover inline_data
246 * x x -> recover data blocks
247 */
248 if (IS_INODE(npage))
249 ri = F2FS_INODE(npage);
250
251 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700252 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900253process_inline:
254 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700255 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900256
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900257 f2fs_wait_on_page_writeback(ipage, NODE);
258
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900259 src_addr = inline_data_addr(npage);
260 dst_addr = inline_data_addr(ipage);
261 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700262
263 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
264 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
265
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900266 update_inode(inode, ipage);
267 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700268 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900269 }
270
271 if (f2fs_has_inline_data(inode)) {
272 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700273 f2fs_bug_on(sbi, IS_ERR(ipage));
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800274 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700275 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900276 update_inode(inode, ipage);
277 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700278 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700279 truncate_blocks(inode, 0, false);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900280 goto process_inline;
281 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700282 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900283}
Chao Yu201a05b2014-09-24 18:17:53 +0800284
285struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
286 struct qstr *name, struct page **res_page)
287{
288 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700289 struct f2fs_inline_dentry *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800290 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700291 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700292 struct page *ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800293
294 ipage = get_node_page(sbi, dir->i_ino);
295 if (IS_ERR(ipage))
296 return NULL;
297
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700298 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800299
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700300 make_dentry_ptr(&d, (void *)inline_dentry, 2);
301 de = find_target_dentry(name, NULL, &d);
302
Chao Yu201a05b2014-09-24 18:17:53 +0800303 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700304 if (de)
305 *res_page = ipage;
306 else
307 f2fs_put_page(ipage, 0);
308
309 /*
310 * For the most part, it should be a bug when name_len is zero.
311 * We stop here for figuring out where the bugs has occurred.
312 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700313 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800314 return de;
315}
316
317struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
318 struct page **p)
319{
320 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
321 struct page *ipage;
322 struct f2fs_dir_entry *de;
323 struct f2fs_inline_dentry *dentry_blk;
324
325 ipage = get_node_page(sbi, dir->i_ino);
326 if (IS_ERR(ipage))
327 return NULL;
328
329 dentry_blk = inline_data_addr(ipage);
330 de = &dentry_blk->dentry[1];
331 *p = ipage;
332 unlock_page(ipage);
333 return de;
334}
335
336int make_empty_inline_dir(struct inode *inode, struct inode *parent,
337 struct page *ipage)
338{
339 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700340 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800341
342 dentry_blk = inline_data_addr(ipage);
343
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700344 make_dentry_ptr(&d, (void *)dentry_blk, 2);
345 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800346
347 set_page_dirty(ipage);
348
349 /* update i_size to MAX_INLINE_DATA */
350 if (i_size_read(inode) < MAX_INLINE_DATA) {
351 i_size_write(inode, MAX_INLINE_DATA);
352 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
353 }
354 return 0;
355}
356
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700357static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800358 struct f2fs_inline_dentry *inline_dentry)
359{
360 struct page *page;
361 struct dnode_of_data dn;
362 struct f2fs_dentry_block *dentry_blk;
363 int err;
364
365 page = grab_cache_page(dir->i_mapping, 0);
366 if (!page)
367 return -ENOMEM;
368
369 set_new_dnode(&dn, dir, ipage, NULL, 0);
370 err = f2fs_reserve_block(&dn, 0);
371 if (err)
372 goto out;
373
374 f2fs_wait_on_page_writeback(page, DATA);
375 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
376
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700377 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800378
379 /* copy data from inline dentry block to new dentry block */
380 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
381 INLINE_DENTRY_BITMAP_SIZE);
382 memcpy(dentry_blk->dentry, inline_dentry->dentry,
383 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
384 memcpy(dentry_blk->filename, inline_dentry->filename,
385 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
386
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700387 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800388 SetPageUptodate(page);
389 set_page_dirty(page);
390
391 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800392 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700393
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700394 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800395 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800396
397 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
398 i_size_write(dir, PAGE_CACHE_SIZE);
399 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
400 }
401
402 sync_inode_page(&dn);
403out:
404 f2fs_put_page(page, 1);
405 return err;
406}
407
408int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700409 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800410{
411 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
412 struct page *ipage;
413 unsigned int bit_pos;
414 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800415 size_t namelen = name->len;
416 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800417 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800418 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700419 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800420 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800421
422 ipage = get_node_page(sbi, dir->i_ino);
423 if (IS_ERR(ipage))
424 return PTR_ERR(ipage);
425
426 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700427 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
428 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800429 if (bit_pos >= NR_INLINE_DENTRY) {
430 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
431 if (!err)
432 err = -EAGAIN;
433 goto out;
434 }
435
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700436 if (inode) {
437 down_write(&F2FS_I(inode)->i_sem);
438 page = init_inode_metadata(inode, dir, name, ipage);
439 if (IS_ERR(page)) {
440 err = PTR_ERR(page);
441 goto fail;
442 }
Chao Yu201a05b2014-09-24 18:17:53 +0800443 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700444
445 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu3b4d7322015-02-16 16:17:20 +0800446
447 name_hash = f2fs_dentry_hash(name);
448 make_dentry_ptr(&d, (void *)dentry_blk, 2);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700449 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800450
Chao Yu201a05b2014-09-24 18:17:53 +0800451 set_page_dirty(ipage);
452
453 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700454 if (inode) {
455 F2FS_I(inode)->i_pino = dir->i_ino;
456 update_inode(inode, page);
457 f2fs_put_page(page, 1);
458 }
Chao Yu201a05b2014-09-24 18:17:53 +0800459
460 update_parent_metadata(dir, inode, 0);
461fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700462 if (inode)
463 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800464
465 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
466 update_inode(dir, ipage);
467 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
468 }
469out:
470 f2fs_put_page(ipage, 1);
471 return err;
472}
473
474void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
475 struct inode *dir, struct inode *inode)
476{
477 struct f2fs_inline_dentry *inline_dentry;
478 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
479 unsigned int bit_pos;
480 int i;
481
482 lock_page(page);
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700483 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800484
485 inline_dentry = inline_data_addr(page);
486 bit_pos = dentry - inline_dentry->dentry;
487 for (i = 0; i < slots; i++)
488 test_and_clear_bit_le(bit_pos + i,
489 &inline_dentry->dentry_bitmap);
490
491 set_page_dirty(page);
492
493 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
494
495 if (inode)
496 f2fs_drop_nlink(dir, inode, page);
497
498 f2fs_put_page(page, 1);
499}
500
501bool f2fs_empty_inline_dir(struct inode *dir)
502{
503 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
504 struct page *ipage;
505 unsigned int bit_pos = 2;
506 struct f2fs_inline_dentry *dentry_blk;
507
508 ipage = get_node_page(sbi, dir->i_ino);
509 if (IS_ERR(ipage))
510 return false;
511
512 dentry_blk = inline_data_addr(ipage);
513 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
514 NR_INLINE_DENTRY,
515 bit_pos);
516
517 f2fs_put_page(ipage, 1);
518
519 if (bit_pos < NR_INLINE_DENTRY)
520 return false;
521
522 return true;
523}
524
525int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx)
526{
527 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800528 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800529 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700530 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800531
532 if (ctx->pos == NR_INLINE_DENTRY)
533 return 0;
534
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700535 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800536 if (IS_ERR(ipage))
537 return PTR_ERR(ipage);
538
Chao Yu201a05b2014-09-24 18:17:53 +0800539 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800540
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700541 make_dentry_ptr(&d, (void *)inline_dentry, 2);
542
543 if (!f2fs_fill_dentries(ctx, &d, 0))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700544 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800545
Chao Yu201a05b2014-09-24 18:17:53 +0800546 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800547 return 0;
548}