blob: 99d514815af7535674e74918e222c13bc6b9fcac [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
Huajun Lie18c65b2013-11-10 23:13:19 +080030 return true;
31}
32
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070033bool f2fs_may_inline_dentry(struct inode *inode)
34{
35 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
36 return false;
37
38 if (!S_ISDIR(inode->i_mode))
39 return false;
40
41 return true;
42}
43
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070044void read_inline_data(struct page *page, struct page *ipage)
Huajun Lie18c65b2013-11-10 23:13:19 +080045{
Huajun Lie18c65b2013-11-10 23:13:19 +080046 void *src_addr, *dst_addr;
47
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070048 if (PageUptodate(page))
49 return;
Chao Yu04a17fb2013-12-30 18:36:23 +080050
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070051 f2fs_bug_on(F2FS_P_SB(page), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080052
Chao Yu18309aa2013-12-30 09:29:06 +080053 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080054
55 /* Copy the whole inline data block */
56 src_addr = inline_data_addr(ipage);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070057 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080058 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim427a45c2014-10-26 22:59:27 -070059 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070060 kunmap_atomic(dst_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +080061 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070062}
Huajun Lie18c65b2013-11-10 23:13:19 +080063
Chao Yu0bfcfcc2015-03-10 13:16:25 +080064bool truncate_inline_inode(struct page *ipage, u64 from)
Chao Yufeeb0de2015-01-26 20:22:55 +080065{
Chao Yu0bfcfcc2015-03-10 13:16:25 +080066 void *addr;
67
Chao Yu0bfcfcc2015-03-10 13:16:25 +080068 if (from >= MAX_INLINE_DATA)
69 return false;
70
71 addr = inline_data_addr(ipage);
72
Chao Yufeeb0de2015-01-26 20:22:55 +080073 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080074 memset(addr + from, 0, MAX_INLINE_DATA - from);
75
76 return true;
Chao Yufeeb0de2015-01-26 20:22:55 +080077}
78
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070079int f2fs_read_inline_data(struct inode *inode, struct page *page)
80{
81 struct page *ipage;
82
83 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
84 if (IS_ERR(ipage)) {
85 unlock_page(page);
86 return PTR_ERR(ipage);
87 }
88
89 if (!f2fs_has_inline_data(inode)) {
90 f2fs_put_page(ipage, 1);
91 return -EAGAIN;
92 }
93
94 if (page->index)
95 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
96 else
97 read_inline_data(page, ipage);
98
99 SetPageUptodate(page);
100 f2fs_put_page(ipage, 1);
101 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800102 return 0;
103}
104
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700105int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800106{
Huajun Lie18c65b2013-11-10 23:13:19 +0800107 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800108 struct f2fs_io_info fio = {
109 .type = DATA,
110 .rw = WRITE_SYNC | REQ_PRIO,
111 };
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800112 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800113
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700114 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +0800115
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700116 if (!f2fs_exist_data(dn->inode))
117 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700118
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700119 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900120 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700121 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800122
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900123 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700124
125 if (PageUptodate(page))
126 goto no_update;
127
Chao Yu18309aa2013-12-30 09:29:06 +0800128 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800129
130 /* Copy the whole inline data block */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700131 src_addr = inline_data_addr(dn->inode_page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700132 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800133 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim09b8b3c2014-11-18 10:50:21 -0800134 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700135 kunmap_atomic(dst_addr);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900136 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700137no_update:
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800138 /* clear dirty state */
139 dirty = clear_page_dirty_for_io(page);
140
Huajun Lie18c65b2013-11-10 23:13:19 +0800141 /* write data page to try to make data consistent */
142 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800143 fio.blk_addr = dn->data_blkaddr;
144 write_data_page(page, dn, &fio);
Chao Yu216a6202015-03-19 19:23:32 +0800145 set_data_blkaddr(dn);
Chao Yu7e4dde72015-02-05 17:51:34 +0800146 f2fs_update_extent_cache(dn);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000147 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800148 if (dirty)
149 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800150
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800151 /* this converted inline_data should be recovered. */
152 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
153
Huajun Lie18c65b2013-11-10 23:13:19 +0800154 /* clear inline data and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800155 truncate_inline_inode(dn->inode_page, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700156clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700157 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800158 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700159 sync_inode_page(dn);
160 f2fs_put_dnode(dn);
161 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800162}
163
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700164int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800165{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700166 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
167 struct dnode_of_data dn;
168 struct page *ipage, *page;
169 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800170
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700171 page = grab_cache_page(inode->i_mapping, 0);
172 if (!page)
173 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900174
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700175 f2fs_lock_op(sbi);
176
177 ipage = get_node_page(sbi, inode->i_ino);
178 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800179 err = PTR_ERR(ipage);
180 goto out;
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700181 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800182
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700183 set_new_dnode(&dn, inode, ipage, ipage, 0);
184
185 if (f2fs_has_inline_data(inode))
186 err = f2fs_convert_inline_page(&dn, page);
187
188 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800189out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700190 f2fs_unlock_op(sbi);
191
192 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800193 return err;
194}
195
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700196int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800197{
198 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800199 struct dnode_of_data dn;
200 int err;
201
202 set_new_dnode(&dn, inode, NULL, NULL, 0);
203 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
204 if (err)
205 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800206
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700207 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700208 f2fs_put_dnode(&dn);
209 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700210 }
211
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700212 f2fs_bug_on(F2FS_I_SB(inode), page->index);
213
214 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700215 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700216 dst_addr = inline_data_addr(dn.inode_page);
217 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700218 kunmap_atomic(src_addr);
Huajun Lie18c65b2013-11-10 23:13:19 +0800219
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700220 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700221 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
222
Huajun Lie18c65b2013-11-10 23:13:19 +0800223 sync_inode_page(&dn);
224 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800225 return 0;
226}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900227
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700228bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900229{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700230 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900231 struct f2fs_inode *ri = NULL;
232 void *src_addr, *dst_addr;
233 struct page *ipage;
234
235 /*
236 * The inline_data recovery policy is as follows.
237 * [prev.] [next] of inline_data flag
238 * o o -> recover inline_data
239 * o x -> remove inline_data, and then recover data blocks
240 * x o -> remove inline_data, and then recover inline_data
241 * x x -> recover data blocks
242 */
243 if (IS_INODE(npage))
244 ri = F2FS_INODE(npage);
245
246 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700247 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900248process_inline:
249 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700250 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900251
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900252 f2fs_wait_on_page_writeback(ipage, NODE);
253
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900254 src_addr = inline_data_addr(npage);
255 dst_addr = inline_data_addr(ipage);
256 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700257
258 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
259 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
260
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900261 update_inode(inode, ipage);
262 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700263 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900264 }
265
266 if (f2fs_has_inline_data(inode)) {
267 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700268 f2fs_bug_on(sbi, IS_ERR(ipage));
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800269 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700270 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900271 update_inode(inode, ipage);
272 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700273 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700274 truncate_blocks(inode, 0, false);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900275 goto process_inline;
276 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700277 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900278}
Chao Yu201a05b2014-09-24 18:17:53 +0800279
280struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
281 struct qstr *name, struct page **res_page)
282{
283 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700284 struct f2fs_inline_dentry *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800285 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700286 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700287 struct page *ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800288
289 ipage = get_node_page(sbi, dir->i_ino);
290 if (IS_ERR(ipage))
291 return NULL;
292
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700293 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800294
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700295 make_dentry_ptr(&d, (void *)inline_dentry, 2);
296 de = find_target_dentry(name, NULL, &d);
297
Chao Yu201a05b2014-09-24 18:17:53 +0800298 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700299 if (de)
300 *res_page = ipage;
301 else
302 f2fs_put_page(ipage, 0);
303
304 /*
305 * For the most part, it should be a bug when name_len is zero.
306 * We stop here for figuring out where the bugs has occurred.
307 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700308 f2fs_bug_on(sbi, d.max < 0);
Chao Yu201a05b2014-09-24 18:17:53 +0800309 return de;
310}
311
312struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
313 struct page **p)
314{
315 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
316 struct page *ipage;
317 struct f2fs_dir_entry *de;
318 struct f2fs_inline_dentry *dentry_blk;
319
320 ipage = get_node_page(sbi, dir->i_ino);
321 if (IS_ERR(ipage))
322 return NULL;
323
324 dentry_blk = inline_data_addr(ipage);
325 de = &dentry_blk->dentry[1];
326 *p = ipage;
327 unlock_page(ipage);
328 return de;
329}
330
331int make_empty_inline_dir(struct inode *inode, struct inode *parent,
332 struct page *ipage)
333{
334 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700335 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800336
337 dentry_blk = inline_data_addr(ipage);
338
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700339 make_dentry_ptr(&d, (void *)dentry_blk, 2);
340 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800341
342 set_page_dirty(ipage);
343
344 /* update i_size to MAX_INLINE_DATA */
345 if (i_size_read(inode) < MAX_INLINE_DATA) {
346 i_size_write(inode, MAX_INLINE_DATA);
347 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
348 }
349 return 0;
350}
351
Jaegeuk Kimd64948a2014-10-20 20:28:49 -0700352static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800353 struct f2fs_inline_dentry *inline_dentry)
354{
355 struct page *page;
356 struct dnode_of_data dn;
357 struct f2fs_dentry_block *dentry_blk;
358 int err;
359
360 page = grab_cache_page(dir->i_mapping, 0);
361 if (!page)
362 return -ENOMEM;
363
364 set_new_dnode(&dn, dir, ipage, NULL, 0);
365 err = f2fs_reserve_block(&dn, 0);
366 if (err)
367 goto out;
368
369 f2fs_wait_on_page_writeback(page, DATA);
370 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
371
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700372 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800373
374 /* copy data from inline dentry block to new dentry block */
375 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
376 INLINE_DENTRY_BITMAP_SIZE);
377 memcpy(dentry_blk->dentry, inline_dentry->dentry,
378 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
379 memcpy(dentry_blk->filename, inline_dentry->filename,
380 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
381
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700382 kunmap_atomic(dentry_blk);
Chao Yu201a05b2014-09-24 18:17:53 +0800383 SetPageUptodate(page);
384 set_page_dirty(page);
385
386 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800387 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700388
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700389 stat_dec_inline_dir(dir);
Chao Yu622f28a2014-09-24 18:19:10 +0800390 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800391
392 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
393 i_size_write(dir, PAGE_CACHE_SIZE);
394 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
395 }
396
397 sync_inode_page(&dn);
398out:
399 f2fs_put_page(page, 1);
400 return err;
401}
402
403int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700404 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800405{
406 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
407 struct page *ipage;
408 unsigned int bit_pos;
409 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800410 size_t namelen = name->len;
411 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800412 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800413 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700414 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800415 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800416
417 ipage = get_node_page(sbi, dir->i_ino);
418 if (IS_ERR(ipage))
419 return PTR_ERR(ipage);
420
421 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700422 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
423 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800424 if (bit_pos >= NR_INLINE_DENTRY) {
425 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
426 if (!err)
427 err = -EAGAIN;
428 goto out;
429 }
430
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700431 if (inode) {
432 down_write(&F2FS_I(inode)->i_sem);
433 page = init_inode_metadata(inode, dir, name, ipage);
434 if (IS_ERR(page)) {
435 err = PTR_ERR(page);
436 goto fail;
437 }
Chao Yu201a05b2014-09-24 18:17:53 +0800438 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700439
440 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu3b4d7322015-02-16 16:17:20 +0800441
442 name_hash = f2fs_dentry_hash(name);
443 make_dentry_ptr(&d, (void *)dentry_blk, 2);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700444 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800445
Chao Yu201a05b2014-09-24 18:17:53 +0800446 set_page_dirty(ipage);
447
448 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700449 if (inode) {
450 F2FS_I(inode)->i_pino = dir->i_ino;
451 update_inode(inode, page);
452 f2fs_put_page(page, 1);
453 }
Chao Yu201a05b2014-09-24 18:17:53 +0800454
455 update_parent_metadata(dir, inode, 0);
456fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700457 if (inode)
458 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800459
460 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
461 update_inode(dir, ipage);
462 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
463 }
464out:
465 f2fs_put_page(ipage, 1);
466 return err;
467}
468
469void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
470 struct inode *dir, struct inode *inode)
471{
472 struct f2fs_inline_dentry *inline_dentry;
473 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
474 unsigned int bit_pos;
475 int i;
476
477 lock_page(page);
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700478 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu201a05b2014-09-24 18:17:53 +0800479
480 inline_dentry = inline_data_addr(page);
481 bit_pos = dentry - inline_dentry->dentry;
482 for (i = 0; i < slots; i++)
483 test_and_clear_bit_le(bit_pos + i,
484 &inline_dentry->dentry_bitmap);
485
486 set_page_dirty(page);
487
488 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
489
490 if (inode)
491 f2fs_drop_nlink(dir, inode, page);
492
493 f2fs_put_page(page, 1);
494}
495
496bool f2fs_empty_inline_dir(struct inode *dir)
497{
498 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
499 struct page *ipage;
500 unsigned int bit_pos = 2;
501 struct f2fs_inline_dentry *dentry_blk;
502
503 ipage = get_node_page(sbi, dir->i_ino);
504 if (IS_ERR(ipage))
505 return false;
506
507 dentry_blk = inline_data_addr(ipage);
508 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
509 NR_INLINE_DENTRY,
510 bit_pos);
511
512 f2fs_put_page(ipage, 1);
513
514 if (bit_pos < NR_INLINE_DENTRY)
515 return false;
516
517 return true;
518}
519
520int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx)
521{
522 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800523 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800524 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700525 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800526
527 if (ctx->pos == NR_INLINE_DENTRY)
528 return 0;
529
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700530 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800531 if (IS_ERR(ipage))
532 return PTR_ERR(ipage);
533
Chao Yu201a05b2014-09-24 18:17:53 +0800534 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800535
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700536 make_dentry_ptr(&d, (void *)inline_dentry, 2);
537
538 if (!f2fs_fill_dentries(ctx, &d, 0))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700539 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800540
Chao Yu201a05b2014-09-24 18:17:53 +0800541 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800542 return 0;
543}