blob: f2d3c581e7761b9ba7b82db95b755e6ac034fbac [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 Kim158c1942014-11-25 11:34:02 -080087 int dirty, 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 Kim09b8b3c2014-11-18 10:50:21 -0800109 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700110 kunmap_atomic(dst_addr);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900111 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700112no_update:
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800113 /* clear dirty state */
114 dirty = clear_page_dirty_for_io(page);
115
Huajun Lie18c65b2013-11-10 23:13:19 +0800116 /* write data page to try to make data consistent */
117 set_page_writeback(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700118
119 write_data_page(page, dn, &new_blk_addr, &fio);
120 update_extent_cache(new_blk_addr, dn);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000121 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800122 if (dirty)
123 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800124
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800125 /* this converted inline_data should be recovered. */
126 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
127
Huajun Lie18c65b2013-11-10 23:13:19 +0800128 /* clear inline data and flag after data writeback */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700129 truncate_inline_data(dn->inode_page, 0);
130clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700131 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800132 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700133 sync_inode_page(dn);
134 f2fs_put_dnode(dn);
135 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800136}
137
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700138int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800139{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700140 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
141 struct dnode_of_data dn;
142 struct page *ipage, *page;
143 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800144
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700145 page = grab_cache_page(inode->i_mapping, 0);
146 if (!page)
147 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900148
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700149 f2fs_lock_op(sbi);
150
151 ipage = get_node_page(sbi, inode->i_ino);
152 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800153 err = PTR_ERR(ipage);
154 goto out;
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700155 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800156
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700157 set_new_dnode(&dn, inode, ipage, ipage, 0);
158
159 if (f2fs_has_inline_data(inode))
160 err = f2fs_convert_inline_page(&dn, page);
161
162 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800163out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700164 f2fs_unlock_op(sbi);
165
166 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800167 return err;
168}
169
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700170int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800171{
172 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800173 struct dnode_of_data dn;
174 int err;
175
176 set_new_dnode(&dn, inode, NULL, NULL, 0);
177 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
178 if (err)
179 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800180
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700181 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700182 f2fs_put_dnode(&dn);
183 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700184 }
185
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700186 f2fs_bug_on(F2FS_I_SB(inode), page->index);
187
188 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700189 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700190 dst_addr = inline_data_addr(dn.inode_page);
191 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700192 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800193
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700194 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700195 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
196
Huajun Lie18c65b2013-11-10 23:13:19 +0800197 sync_inode_page(&dn);
198 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800199 return 0;
200}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900201
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700202void truncate_inline_data(struct page *ipage, u64 from)
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800203{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700204 void *addr;
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800205
206 if (from >= MAX_INLINE_DATA)
207 return;
208
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900209 f2fs_wait_on_page_writeback(ipage, NODE);
210
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700211 addr = inline_data_addr(ipage);
212 memset(addr + from, 0, MAX_INLINE_DATA - from);
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800213}
214
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700215bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900216{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700217 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900218 struct f2fs_inode *ri = NULL;
219 void *src_addr, *dst_addr;
220 struct page *ipage;
221
222 /*
223 * The inline_data recovery policy is as follows.
224 * [prev.] [next] of inline_data flag
225 * o o -> recover inline_data
226 * o x -> remove inline_data, and then recover data blocks
227 * x o -> remove inline_data, and then recover inline_data
228 * x x -> recover data blocks
229 */
230 if (IS_INODE(npage))
231 ri = F2FS_INODE(npage);
232
233 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700234 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900235process_inline:
236 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700237 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900238
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900239 f2fs_wait_on_page_writeback(ipage, NODE);
240
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900241 src_addr = inline_data_addr(npage);
242 dst_addr = inline_data_addr(ipage);
243 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700244
245 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
246 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
247
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900248 update_inode(inode, ipage);
249 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700250 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900251 }
252
253 if (f2fs_has_inline_data(inode)) {
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 Kimb3d208f2014-10-23 19:48:09 -0700256 truncate_inline_data(ipage, 0);
257 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900258 update_inode(inode, ipage);
259 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700260 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700261 truncate_blocks(inode, 0, false);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900262 goto process_inline;
263 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700264 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900265}
Chao Yu201a05b2014-09-24 18:17:53 +0800266
267struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
268 struct qstr *name, struct page **res_page)
269{
270 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700271 struct f2fs_inline_dentry *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800272 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700273 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700274 struct page *ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800275
276 ipage = get_node_page(sbi, dir->i_ino);
277 if (IS_ERR(ipage))
278 return NULL;
279
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700280 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800281
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700282 make_dentry_ptr(&d, (void *)inline_dentry, 2);
283 de = find_target_dentry(name, NULL, &d);
284
Chao Yu201a05b2014-09-24 18:17:53 +0800285 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700286 if (de)
287 *res_page = ipage;
288 else
289 f2fs_put_page(ipage, 0);
290
291 /*
292 * For the most part, it should be a bug when name_len is zero.
293 * We stop here for figuring out where the bugs has occurred.
294 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700295 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800296 return de;
297}
298
299struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
300 struct page **p)
301{
302 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
303 struct page *ipage;
304 struct f2fs_dir_entry *de;
305 struct f2fs_inline_dentry *dentry_blk;
306
307 ipage = get_node_page(sbi, dir->i_ino);
308 if (IS_ERR(ipage))
309 return NULL;
310
311 dentry_blk = inline_data_addr(ipage);
312 de = &dentry_blk->dentry[1];
313 *p = ipage;
314 unlock_page(ipage);
315 return de;
316}
317
318int make_empty_inline_dir(struct inode *inode, struct inode *parent,
319 struct page *ipage)
320{
321 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700322 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800323
324 dentry_blk = inline_data_addr(ipage);
325
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700326 make_dentry_ptr(&d, (void *)dentry_blk, 2);
327 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800328
329 set_page_dirty(ipage);
330
331 /* update i_size to MAX_INLINE_DATA */
332 if (i_size_read(inode) < MAX_INLINE_DATA) {
333 i_size_write(inode, MAX_INLINE_DATA);
334 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
335 }
336 return 0;
337}
338
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700339static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800340 struct f2fs_inline_dentry *inline_dentry)
341{
342 struct page *page;
343 struct dnode_of_data dn;
344 struct f2fs_dentry_block *dentry_blk;
345 int err;
346
347 page = grab_cache_page(dir->i_mapping, 0);
348 if (!page)
349 return -ENOMEM;
350
351 set_new_dnode(&dn, dir, ipage, NULL, 0);
352 err = f2fs_reserve_block(&dn, 0);
353 if (err)
354 goto out;
355
356 f2fs_wait_on_page_writeback(page, DATA);
357 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
358
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700359 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800360
361 /* copy data from inline dentry block to new dentry block */
362 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
363 INLINE_DENTRY_BITMAP_SIZE);
364 memcpy(dentry_blk->dentry, inline_dentry->dentry,
365 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
366 memcpy(dentry_blk->filename, inline_dentry->filename,
367 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
368
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700369 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800370 SetPageUptodate(page);
371 set_page_dirty(page);
372
373 /* clear inline dir and flag after data writeback */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700374 truncate_inline_data(ipage, 0);
375
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700376 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800377 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800378
379 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
380 i_size_write(dir, PAGE_CACHE_SIZE);
381 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
382 }
383
384 sync_inode_page(&dn);
385out:
386 f2fs_put_page(page, 1);
387 return err;
388}
389
390int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
391 struct inode *inode)
392{
393 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
394 struct page *ipage;
395 unsigned int bit_pos;
396 f2fs_hash_t name_hash;
397 struct f2fs_dir_entry *de;
398 size_t namelen = name->len;
399 struct f2fs_inline_dentry *dentry_blk = NULL;
400 int slots = GET_DENTRY_SLOTS(namelen);
401 struct page *page;
402 int err = 0;
403 int i;
404
405 name_hash = f2fs_dentry_hash(name);
406
407 ipage = get_node_page(sbi, dir->i_ino);
408 if (IS_ERR(ipage))
409 return PTR_ERR(ipage);
410
411 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700412 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
413 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800414 if (bit_pos >= NR_INLINE_DENTRY) {
415 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
416 if (!err)
417 err = -EAGAIN;
418 goto out;
419 }
420
Chao Yu201a05b2014-09-24 18:17:53 +0800421 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700422 page = init_inode_metadata(inode, dir, name, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800423 if (IS_ERR(page)) {
424 err = PTR_ERR(page);
425 goto fail;
426 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700427
428 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800429 de = &dentry_blk->dentry[bit_pos];
430 de->hash_code = name_hash;
431 de->name_len = cpu_to_le16(namelen);
432 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
433 de->ino = cpu_to_le32(inode->i_ino);
434 set_de_type(de, inode);
435 for (i = 0; i < slots; i++)
436 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
437 set_page_dirty(ipage);
438
439 /* we don't need to mark_inode_dirty now */
440 F2FS_I(inode)->i_pino = dir->i_ino;
441 update_inode(inode, page);
442 f2fs_put_page(page, 1);
443
444 update_parent_metadata(dir, inode, 0);
445fail:
446 up_write(&F2FS_I(inode)->i_sem);
447
448 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
449 update_inode(dir, ipage);
450 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
451 }
452out:
453 f2fs_put_page(ipage, 1);
454 return err;
455}
456
457void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
458 struct inode *dir, struct inode *inode)
459{
460 struct f2fs_inline_dentry *inline_dentry;
461 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
462 unsigned int bit_pos;
463 int i;
464
465 lock_page(page);
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700466 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800467
468 inline_dentry = inline_data_addr(page);
469 bit_pos = dentry - inline_dentry->dentry;
470 for (i = 0; i < slots; i++)
471 test_and_clear_bit_le(bit_pos + i,
472 &inline_dentry->dentry_bitmap);
473
474 set_page_dirty(page);
475
476 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
477
478 if (inode)
479 f2fs_drop_nlink(dir, inode, page);
480
481 f2fs_put_page(page, 1);
482}
483
484bool f2fs_empty_inline_dir(struct inode *dir)
485{
486 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
487 struct page *ipage;
488 unsigned int bit_pos = 2;
489 struct f2fs_inline_dentry *dentry_blk;
490
491 ipage = get_node_page(sbi, dir->i_ino);
492 if (IS_ERR(ipage))
493 return false;
494
495 dentry_blk = inline_data_addr(ipage);
496 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
497 NR_INLINE_DENTRY,
498 bit_pos);
499
500 f2fs_put_page(ipage, 1);
501
502 if (bit_pos < NR_INLINE_DENTRY)
503 return false;
504
505 return true;
506}
507
508int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx)
509{
510 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800511 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800512 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700513 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800514
515 if (ctx->pos == NR_INLINE_DENTRY)
516 return 0;
517
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700518 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800519 if (IS_ERR(ipage))
520 return PTR_ERR(ipage);
521
Chao Yu201a05b2014-09-24 18:17:53 +0800522 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800523
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700524 make_dentry_ptr(&d, (void *)inline_dentry, 2);
525
526 if (!f2fs_fill_dentries(ctx, &d, 0))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700527 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800528
Chao Yu201a05b2014-09-24 18:17:53 +0800529 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800530 return 0;
531}