blob: f26fb87da1633347c918d6dc5ed1ec16a95cf71e [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
Huajun Lie18c65b2013-11-10 23:13:19 +080016bool f2fs_may_inline(struct inode *inode)
17{
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
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070024 if (!S_ISREG(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
Huajun Lie18c65b2013-11-10 23:13:19 +080030 return true;
31}
32
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070033void read_inline_data(struct page *page, struct page *ipage)
Huajun Lie18c65b2013-11-10 23:13:19 +080034{
Huajun Lie18c65b2013-11-10 23:13:19 +080035 void *src_addr, *dst_addr;
36
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070037 if (PageUptodate(page))
38 return;
Chao Yu04a17fb2013-12-30 18:36:23 +080039
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070040 f2fs_bug_on(F2FS_P_SB(page), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080041
Chao Yu18309aa2013-12-30 09:29:06 +080042 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080043
44 /* Copy the whole inline data block */
45 src_addr = inline_data_addr(ipage);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070046 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080047 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim427a45c2014-10-26 22:59:27 -070048 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070049 kunmap_atomic(dst_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +080050 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070051}
Huajun Lie18c65b2013-11-10 23:13:19 +080052
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070053int f2fs_read_inline_data(struct inode *inode, struct page *page)
54{
55 struct page *ipage;
56
57 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
58 if (IS_ERR(ipage)) {
59 unlock_page(page);
60 return PTR_ERR(ipage);
61 }
62
63 if (!f2fs_has_inline_data(inode)) {
64 f2fs_put_page(ipage, 1);
65 return -EAGAIN;
66 }
67
68 if (page->index)
69 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
70 else
71 read_inline_data(page, ipage);
72
73 SetPageUptodate(page);
74 f2fs_put_page(ipage, 1);
75 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080076 return 0;
77}
78
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070079int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +080080{
Huajun Lie18c65b2013-11-10 23:13:19 +080081 void *src_addr, *dst_addr;
82 block_t new_blk_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +080083 struct f2fs_io_info fio = {
84 .type = DATA,
85 .rw = WRITE_SYNC | REQ_PRIO,
86 };
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070087 int err;
Huajun Lie18c65b2013-11-10 23:13:19 +080088
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070089 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080090
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070091 if (!f2fs_exist_data(dn->inode))
92 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -070093
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070094 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +090095 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070096 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +080097
Jaegeuk Kim9ac13492014-04-29 17:35:10 +090098 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070099
100 if (PageUptodate(page))
101 goto no_update;
102
Chao Yu18309aa2013-12-30 09:29:06 +0800103 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800104
105 /* Copy the whole inline data block */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700106 src_addr = inline_data_addr(dn->inode_page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700107 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800108 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700109 kunmap_atomic(dst_addr);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900110 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700111no_update:
Huajun Lie18c65b2013-11-10 23:13:19 +0800112 /* write data page to try to make data consistent */
113 set_page_writeback(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700114
115 write_data_page(page, dn, &new_blk_addr, &fio);
116 update_extent_cache(new_blk_addr, dn);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000117 f2fs_wait_on_page_writeback(page, DATA);
Huajun Lie18c65b2013-11-10 23:13:19 +0800118
119 /* clear inline data and flag after data writeback */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700120 truncate_inline_data(dn->inode_page, 0);
121clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700122 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800123 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700124 sync_inode_page(dn);
125 f2fs_put_dnode(dn);
126 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800127}
128
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700129int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800130{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700131 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
132 struct dnode_of_data dn;
133 struct page *ipage, *page;
134 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800135
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700136 page = grab_cache_page(inode->i_mapping, 0);
137 if (!page)
138 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900139
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700140 f2fs_lock_op(sbi);
141
142 ipage = get_node_page(sbi, inode->i_ino);
143 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800144 err = PTR_ERR(ipage);
145 goto out;
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700146 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800147
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700148 set_new_dnode(&dn, inode, ipage, ipage, 0);
149
150 if (f2fs_has_inline_data(inode))
151 err = f2fs_convert_inline_page(&dn, page);
152
153 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800154out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700155 f2fs_unlock_op(sbi);
156
157 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800158 return err;
159}
160
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700161int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800162{
163 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800164 struct dnode_of_data dn;
165 int err;
166
167 set_new_dnode(&dn, inode, NULL, NULL, 0);
168 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
169 if (err)
170 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800171
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700172 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700173 f2fs_put_dnode(&dn);
174 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700175 }
176
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700177 f2fs_bug_on(F2FS_I_SB(inode), page->index);
178
179 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700180 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700181 dst_addr = inline_data_addr(dn.inode_page);
182 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700183 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800184
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700185 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700186 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
187
Huajun Lie18c65b2013-11-10 23:13:19 +0800188 sync_inode_page(&dn);
189 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800190 return 0;
191}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900192
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700193void truncate_inline_data(struct page *ipage, u64 from)
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800194{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700195 void *addr;
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800196
197 if (from >= MAX_INLINE_DATA)
198 return;
199
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900200 f2fs_wait_on_page_writeback(ipage, NODE);
201
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700202 addr = inline_data_addr(ipage);
203 memset(addr + from, 0, MAX_INLINE_DATA - from);
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800204}
205
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700206bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900207{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700208 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900209 struct f2fs_inode *ri = NULL;
210 void *src_addr, *dst_addr;
211 struct page *ipage;
212
213 /*
214 * The inline_data recovery policy is as follows.
215 * [prev.] [next] of inline_data flag
216 * o o -> recover inline_data
217 * o x -> remove inline_data, and then recover data blocks
218 * x o -> remove inline_data, and then recover inline_data
219 * x x -> recover data blocks
220 */
221 if (IS_INODE(npage))
222 ri = F2FS_INODE(npage);
223
224 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700225 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900226process_inline:
227 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700228 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900229
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900230 f2fs_wait_on_page_writeback(ipage, NODE);
231
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900232 src_addr = inline_data_addr(npage);
233 dst_addr = inline_data_addr(ipage);
234 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700235
236 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
237 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
238
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900239 update_inode(inode, ipage);
240 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700241 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900242 }
243
244 if (f2fs_has_inline_data(inode)) {
245 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700246 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700247 truncate_inline_data(ipage, 0);
248 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900249 update_inode(inode, ipage);
250 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700251 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700252 truncate_blocks(inode, 0, false);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900253 goto process_inline;
254 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700255 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900256}
Chao Yu201a05b2014-09-24 18:17:53 +0800257
258struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
259 struct qstr *name, struct page **res_page)
260{
261 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700262 struct f2fs_inline_dentry *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800263 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700264 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700265 struct page *ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800266
267 ipage = get_node_page(sbi, dir->i_ino);
268 if (IS_ERR(ipage))
269 return NULL;
270
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700271 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800272
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700273 make_dentry_ptr(&d, (void *)inline_dentry, 2);
274 de = find_target_dentry(name, NULL, &d);
275
Chao Yu201a05b2014-09-24 18:17:53 +0800276 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700277 if (de)
278 *res_page = ipage;
279 else
280 f2fs_put_page(ipage, 0);
281
282 /*
283 * For the most part, it should be a bug when name_len is zero.
284 * We stop here for figuring out where the bugs has occurred.
285 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700286 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800287 return de;
288}
289
290struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
291 struct page **p)
292{
293 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
294 struct page *ipage;
295 struct f2fs_dir_entry *de;
296 struct f2fs_inline_dentry *dentry_blk;
297
298 ipage = get_node_page(sbi, dir->i_ino);
299 if (IS_ERR(ipage))
300 return NULL;
301
302 dentry_blk = inline_data_addr(ipage);
303 de = &dentry_blk->dentry[1];
304 *p = ipage;
305 unlock_page(ipage);
306 return de;
307}
308
309int make_empty_inline_dir(struct inode *inode, struct inode *parent,
310 struct page *ipage)
311{
312 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700313 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800314
315 dentry_blk = inline_data_addr(ipage);
316
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700317 make_dentry_ptr(&d, (void *)dentry_blk, 2);
318 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800319
320 set_page_dirty(ipage);
321
322 /* update i_size to MAX_INLINE_DATA */
323 if (i_size_read(inode) < MAX_INLINE_DATA) {
324 i_size_write(inode, MAX_INLINE_DATA);
325 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
326 }
327 return 0;
328}
329
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700330static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800331 struct f2fs_inline_dentry *inline_dentry)
332{
333 struct page *page;
334 struct dnode_of_data dn;
335 struct f2fs_dentry_block *dentry_blk;
336 int err;
337
338 page = grab_cache_page(dir->i_mapping, 0);
339 if (!page)
340 return -ENOMEM;
341
342 set_new_dnode(&dn, dir, ipage, NULL, 0);
343 err = f2fs_reserve_block(&dn, 0);
344 if (err)
345 goto out;
346
347 f2fs_wait_on_page_writeback(page, DATA);
348 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
349
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700350 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800351
352 /* copy data from inline dentry block to new dentry block */
353 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
354 INLINE_DENTRY_BITMAP_SIZE);
355 memcpy(dentry_blk->dentry, inline_dentry->dentry,
356 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
357 memcpy(dentry_blk->filename, inline_dentry->filename,
358 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
359
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700360 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800361 SetPageUptodate(page);
362 set_page_dirty(page);
363
364 /* clear inline dir and flag after data writeback */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700365 truncate_inline_data(ipage, 0);
366
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700367 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800368 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800369
370 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
371 i_size_write(dir, PAGE_CACHE_SIZE);
372 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
373 }
374
375 sync_inode_page(&dn);
376out:
377 f2fs_put_page(page, 1);
378 return err;
379}
380
381int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
382 struct inode *inode)
383{
384 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
385 struct page *ipage;
386 unsigned int bit_pos;
387 f2fs_hash_t name_hash;
388 struct f2fs_dir_entry *de;
389 size_t namelen = name->len;
390 struct f2fs_inline_dentry *dentry_blk = NULL;
391 int slots = GET_DENTRY_SLOTS(namelen);
392 struct page *page;
393 int err = 0;
394 int i;
395
396 name_hash = f2fs_dentry_hash(name);
397
398 ipage = get_node_page(sbi, dir->i_ino);
399 if (IS_ERR(ipage))
400 return PTR_ERR(ipage);
401
402 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700403 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
404 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800405 if (bit_pos >= NR_INLINE_DENTRY) {
406 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
407 if (!err)
408 err = -EAGAIN;
409 goto out;
410 }
411
Chao Yu201a05b2014-09-24 18:17:53 +0800412 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700413 page = init_inode_metadata(inode, dir, name, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800414 if (IS_ERR(page)) {
415 err = PTR_ERR(page);
416 goto fail;
417 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700418
419 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800420 de = &dentry_blk->dentry[bit_pos];
421 de->hash_code = name_hash;
422 de->name_len = cpu_to_le16(namelen);
423 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
424 de->ino = cpu_to_le32(inode->i_ino);
425 set_de_type(de, inode);
426 for (i = 0; i < slots; i++)
427 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
428 set_page_dirty(ipage);
429
430 /* we don't need to mark_inode_dirty now */
431 F2FS_I(inode)->i_pino = dir->i_ino;
432 update_inode(inode, page);
433 f2fs_put_page(page, 1);
434
435 update_parent_metadata(dir, inode, 0);
436fail:
437 up_write(&F2FS_I(inode)->i_sem);
438
439 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
440 update_inode(dir, ipage);
441 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
442 }
443out:
444 f2fs_put_page(ipage, 1);
445 return err;
446}
447
448void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
449 struct inode *dir, struct inode *inode)
450{
451 struct f2fs_inline_dentry *inline_dentry;
452 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
453 unsigned int bit_pos;
454 int i;
455
456 lock_page(page);
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700457 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800458
459 inline_dentry = inline_data_addr(page);
460 bit_pos = dentry - inline_dentry->dentry;
461 for (i = 0; i < slots; i++)
462 test_and_clear_bit_le(bit_pos + i,
463 &inline_dentry->dentry_bitmap);
464
465 set_page_dirty(page);
466
467 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
468
469 if (inode)
470 f2fs_drop_nlink(dir, inode, page);
471
472 f2fs_put_page(page, 1);
473}
474
475bool f2fs_empty_inline_dir(struct inode *dir)
476{
477 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
478 struct page *ipage;
479 unsigned int bit_pos = 2;
480 struct f2fs_inline_dentry *dentry_blk;
481
482 ipage = get_node_page(sbi, dir->i_ino);
483 if (IS_ERR(ipage))
484 return false;
485
486 dentry_blk = inline_data_addr(ipage);
487 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
488 NR_INLINE_DENTRY,
489 bit_pos);
490
491 f2fs_put_page(ipage, 1);
492
493 if (bit_pos < NR_INLINE_DENTRY)
494 return false;
495
496 return true;
497}
498
499int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx)
500{
501 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800502 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800503 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700504 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800505
506 if (ctx->pos == NR_INLINE_DENTRY)
507 return 0;
508
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700509 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800510 if (IS_ERR(ipage))
511 return PTR_ERR(ipage);
512
Chao Yu201a05b2014-09-24 18:17:53 +0800513 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800514
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700515 make_dentry_ptr(&d, (void *)inline_dentry, 2);
516
517 if (!f2fs_fill_dentries(ctx, &d, 0))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700518 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800519
Chao Yu201a05b2014-09-24 18:17:53 +0800520 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800521 return 0;
522}