blob: 375d2c797f4c85dff8cf04017e04412695caeaa7 [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
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
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
Chao Yu0bfcfcc2015-03-10 13:16:25 +080053bool truncate_inline_inode(struct page *ipage, u64 from)
Chao Yufeeb0de2015-01-26 20:22:55 +080054{
Chao Yu0bfcfcc2015-03-10 13:16:25 +080055 void *addr;
56
Chao Yu0bfcfcc2015-03-10 13:16:25 +080057 if (from >= MAX_INLINE_DATA)
58 return false;
59
60 addr = inline_data_addr(ipage);
61
Chao Yufeeb0de2015-01-26 20:22:55 +080062 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080063 memset(addr + from, 0, MAX_INLINE_DATA - from);
64
65 return true;
Chao Yufeeb0de2015-01-26 20:22:55 +080066}
67
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070068int f2fs_read_inline_data(struct inode *inode, struct page *page)
69{
70 struct page *ipage;
71
72 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
73 if (IS_ERR(ipage)) {
74 unlock_page(page);
75 return PTR_ERR(ipage);
76 }
77
78 if (!f2fs_has_inline_data(inode)) {
79 f2fs_put_page(ipage, 1);
80 return -EAGAIN;
81 }
82
83 if (page->index)
84 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
85 else
86 read_inline_data(page, ipage);
87
88 SetPageUptodate(page);
89 f2fs_put_page(ipage, 1);
90 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080091 return 0;
92}
93
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070094int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +080095{
Huajun Lie18c65b2013-11-10 23:13:19 +080096 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +080097 struct f2fs_io_info fio = {
98 .type = DATA,
99 .rw = WRITE_SYNC | REQ_PRIO,
100 };
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800101 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800102
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700103 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +0800104
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700105 if (!f2fs_exist_data(dn->inode))
106 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700107
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700108 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900109 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700110 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800111
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900112 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700113
114 if (PageUptodate(page))
115 goto no_update;
116
Chao Yu18309aa2013-12-30 09:29:06 +0800117 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800118
119 /* Copy the whole inline data block */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700120 src_addr = inline_data_addr(dn->inode_page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700121 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800122 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim09b8b3c2014-11-18 10:50:21 -0800123 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700124 kunmap_atomic(dst_addr);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900125 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700126no_update:
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800127 /* clear dirty state */
128 dirty = clear_page_dirty_for_io(page);
129
Huajun Lie18c65b2013-11-10 23:13:19 +0800130 /* write data page to try to make data consistent */
131 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800132 fio.blk_addr = dn->data_blkaddr;
133 write_data_page(page, dn, &fio);
Chao Yu7e4dde72015-02-05 17:51:34 +0800134 f2fs_update_extent_cache(dn);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000135 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800136 if (dirty)
137 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800138
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800139 /* this converted inline_data should be recovered. */
140 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
141
Huajun Lie18c65b2013-11-10 23:13:19 +0800142 /* clear inline data and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800143 truncate_inline_inode(dn->inode_page, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700144clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700145 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800146 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700147 sync_inode_page(dn);
148 f2fs_put_dnode(dn);
149 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800150}
151
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700152int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800153{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700154 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
155 struct dnode_of_data dn;
156 struct page *ipage, *page;
157 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800158
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700159 page = grab_cache_page(inode->i_mapping, 0);
160 if (!page)
161 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900162
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700163 f2fs_lock_op(sbi);
164
165 ipage = get_node_page(sbi, inode->i_ino);
166 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800167 err = PTR_ERR(ipage);
168 goto out;
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700169 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800170
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700171 set_new_dnode(&dn, inode, ipage, ipage, 0);
172
173 if (f2fs_has_inline_data(inode))
174 err = f2fs_convert_inline_page(&dn, page);
175
176 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800177out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700178 f2fs_unlock_op(sbi);
179
180 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800181 return err;
182}
183
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700184int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800185{
186 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800187 struct dnode_of_data dn;
188 int err;
189
190 set_new_dnode(&dn, inode, NULL, NULL, 0);
191 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
192 if (err)
193 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800194
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700195 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700196 f2fs_put_dnode(&dn);
197 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700198 }
199
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700200 f2fs_bug_on(F2FS_I_SB(inode), page->index);
201
202 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700203 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700204 dst_addr = inline_data_addr(dn.inode_page);
205 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700206 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800207
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700208 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700209 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
210
Huajun Lie18c65b2013-11-10 23:13:19 +0800211 sync_inode_page(&dn);
212 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800213 return 0;
214}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900215
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700216bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900217{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700218 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900219 struct f2fs_inode *ri = NULL;
220 void *src_addr, *dst_addr;
221 struct page *ipage;
222
223 /*
224 * The inline_data recovery policy is as follows.
225 * [prev.] [next] of inline_data flag
226 * o o -> recover inline_data
227 * o x -> remove inline_data, and then recover data blocks
228 * x o -> remove inline_data, and then recover inline_data
229 * x x -> recover data blocks
230 */
231 if (IS_INODE(npage))
232 ri = F2FS_INODE(npage);
233
234 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700235 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900236process_inline:
237 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700238 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900239
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900240 f2fs_wait_on_page_writeback(ipage, NODE);
241
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900242 src_addr = inline_data_addr(npage);
243 dst_addr = inline_data_addr(ipage);
244 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700245
246 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
247 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
248
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 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900252 }
253
254 if (f2fs_has_inline_data(inode)) {
255 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700256 f2fs_bug_on(sbi, IS_ERR(ipage));
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800257 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700258 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900259 update_inode(inode, ipage);
260 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700261 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700262 truncate_blocks(inode, 0, false);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900263 goto process_inline;
264 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700265 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900266}
Chao Yu201a05b2014-09-24 18:17:53 +0800267
268struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
269 struct qstr *name, struct page **res_page)
270{
271 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700272 struct f2fs_inline_dentry *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800273 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700274 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700275 struct page *ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800276
277 ipage = get_node_page(sbi, dir->i_ino);
278 if (IS_ERR(ipage))
279 return NULL;
280
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700281 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800282
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700283 make_dentry_ptr(&d, (void *)inline_dentry, 2);
284 de = find_target_dentry(name, NULL, &d);
285
Chao Yu201a05b2014-09-24 18:17:53 +0800286 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700287 if (de)
288 *res_page = ipage;
289 else
290 f2fs_put_page(ipage, 0);
291
292 /*
293 * For the most part, it should be a bug when name_len is zero.
294 * We stop here for figuring out where the bugs has occurred.
295 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700296 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800297 return de;
298}
299
300struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
301 struct page **p)
302{
303 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
304 struct page *ipage;
305 struct f2fs_dir_entry *de;
306 struct f2fs_inline_dentry *dentry_blk;
307
308 ipage = get_node_page(sbi, dir->i_ino);
309 if (IS_ERR(ipage))
310 return NULL;
311
312 dentry_blk = inline_data_addr(ipage);
313 de = &dentry_blk->dentry[1];
314 *p = ipage;
315 unlock_page(ipage);
316 return de;
317}
318
319int make_empty_inline_dir(struct inode *inode, struct inode *parent,
320 struct page *ipage)
321{
322 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700323 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800324
325 dentry_blk = inline_data_addr(ipage);
326
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700327 make_dentry_ptr(&d, (void *)dentry_blk, 2);
328 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800329
330 set_page_dirty(ipage);
331
332 /* update i_size to MAX_INLINE_DATA */
333 if (i_size_read(inode) < MAX_INLINE_DATA) {
334 i_size_write(inode, MAX_INLINE_DATA);
335 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
336 }
337 return 0;
338}
339
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700340static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800341 struct f2fs_inline_dentry *inline_dentry)
342{
343 struct page *page;
344 struct dnode_of_data dn;
345 struct f2fs_dentry_block *dentry_blk;
346 int err;
347
348 page = grab_cache_page(dir->i_mapping, 0);
349 if (!page)
350 return -ENOMEM;
351
352 set_new_dnode(&dn, dir, ipage, NULL, 0);
353 err = f2fs_reserve_block(&dn, 0);
354 if (err)
355 goto out;
356
357 f2fs_wait_on_page_writeback(page, DATA);
358 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
359
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700360 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800361
362 /* copy data from inline dentry block to new dentry block */
363 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
364 INLINE_DENTRY_BITMAP_SIZE);
365 memcpy(dentry_blk->dentry, inline_dentry->dentry,
366 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
367 memcpy(dentry_blk->filename, inline_dentry->filename,
368 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
369
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700370 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800371 SetPageUptodate(page);
372 set_page_dirty(page);
373
374 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800375 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700376
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700377 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800378 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800379
380 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
381 i_size_write(dir, PAGE_CACHE_SIZE);
382 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
383 }
384
385 sync_inode_page(&dn);
386out:
387 f2fs_put_page(page, 1);
388 return err;
389}
390
391int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
392 struct inode *inode)
393{
394 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
395 struct page *ipage;
396 unsigned int bit_pos;
397 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800398 size_t namelen = name->len;
399 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800400 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800401 int slots = GET_DENTRY_SLOTS(namelen);
402 struct page *page;
403 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800404
405 ipage = get_node_page(sbi, dir->i_ino);
406 if (IS_ERR(ipage))
407 return PTR_ERR(ipage);
408
409 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700410 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
411 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800412 if (bit_pos >= NR_INLINE_DENTRY) {
413 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
414 if (!err)
415 err = -EAGAIN;
416 goto out;
417 }
418
Chao Yu201a05b2014-09-24 18:17:53 +0800419 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700420 page = init_inode_metadata(inode, dir, name, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800421 if (IS_ERR(page)) {
422 err = PTR_ERR(page);
423 goto fail;
424 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700425
426 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu3b4d7322015-02-16 16:17:20 +0800427
428 name_hash = f2fs_dentry_hash(name);
429 make_dentry_ptr(&d, (void *)dentry_blk, 2);
430 f2fs_update_dentry(inode, &d, name, name_hash, bit_pos);
431
Chao Yu201a05b2014-09-24 18:17:53 +0800432 set_page_dirty(ipage);
433
434 /* we don't need to mark_inode_dirty now */
435 F2FS_I(inode)->i_pino = dir->i_ino;
436 update_inode(inode, page);
437 f2fs_put_page(page, 1);
438
439 update_parent_metadata(dir, inode, 0);
440fail:
441 up_write(&F2FS_I(inode)->i_sem);
442
443 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
444 update_inode(dir, ipage);
445 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
446 }
447out:
448 f2fs_put_page(ipage, 1);
449 return err;
450}
451
452void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
453 struct inode *dir, struct inode *inode)
454{
455 struct f2fs_inline_dentry *inline_dentry;
456 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
457 unsigned int bit_pos;
458 int i;
459
460 lock_page(page);
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700461 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800462
463 inline_dentry = inline_data_addr(page);
464 bit_pos = dentry - inline_dentry->dentry;
465 for (i = 0; i < slots; i++)
466 test_and_clear_bit_le(bit_pos + i,
467 &inline_dentry->dentry_bitmap);
468
469 set_page_dirty(page);
470
471 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
472
473 if (inode)
474 f2fs_drop_nlink(dir, inode, page);
475
476 f2fs_put_page(page, 1);
477}
478
479bool f2fs_empty_inline_dir(struct inode *dir)
480{
481 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
482 struct page *ipage;
483 unsigned int bit_pos = 2;
484 struct f2fs_inline_dentry *dentry_blk;
485
486 ipage = get_node_page(sbi, dir->i_ino);
487 if (IS_ERR(ipage))
488 return false;
489
490 dentry_blk = inline_data_addr(ipage);
491 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
492 NR_INLINE_DENTRY,
493 bit_pos);
494
495 f2fs_put_page(ipage, 1);
496
497 if (bit_pos < NR_INLINE_DENTRY)
498 return false;
499
500 return true;
501}
502
503int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx)
504{
505 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800506 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800507 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700508 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800509
510 if (ctx->pos == NR_INLINE_DENTRY)
511 return 0;
512
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700513 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800514 if (IS_ERR(ipage))
515 return PTR_ERR(ipage);
516
Chao Yu201a05b2014-09-24 18:17:53 +0800517 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800518
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700519 make_dentry_ptr(&d, (void *)inline_dentry, 2);
520
521 if (!f2fs_fill_dentries(ctx, &d, 0))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700522 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800523
Chao Yu201a05b2014-09-24 18:17:53 +0800524 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800525 return 0;
526}