blob: 8322e4e7bb3fc432aabc8067b7ab459274c6d326 [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"
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -070015#include "node.h"
Huajun Lie18c65b2013-11-10 23:13:19 +080016
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070017bool f2fs_may_inline_data(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +080018{
Jaegeuk Kim88b88a62014-10-06 17:39:50 -070019 if (f2fs_is_atomic_file(inode))
20 return false;
21
Wanpeng Li368a0e42015-03-19 13:23:48 +080022 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
Huajun Lie18c65b2013-11-10 23:13:19 +080023 return false;
24
Chao Yue84f88e2017-07-19 00:19:05 +080025 if (i_size_read(inode) > MAX_INLINE_DATA(inode))
Jaegeuk Kim92dffd02014-11-11 14:10:01 -080026 return false;
27
Jaegeuk Kima4fb1892017-09-05 16:54:24 -070028 if (f2fs_encrypted_file(inode))
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070029 return false;
30
Huajun Lie18c65b2013-11-10 23:13:19 +080031 return true;
32}
33
Jaegeuk Kim01b960e2015-04-23 10:27:21 -070034bool f2fs_may_inline_dentry(struct inode *inode)
35{
36 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
37 return false;
38
39 if (!S_ISDIR(inode->i_mode))
40 return false;
41
42 return true;
43}
44
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070045void read_inline_data(struct page *page, struct page *ipage)
Huajun Lie18c65b2013-11-10 23:13:19 +080046{
Chao Yue84f88e2017-07-19 00:19:05 +080047 struct inode *inode = page->mapping->host;
Huajun Lie18c65b2013-11-10 23:13:19 +080048 void *src_addr, *dst_addr;
49
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070050 if (PageUptodate(page))
51 return;
Chao Yu04a17fb2013-12-30 18:36:23 +080052
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070053 f2fs_bug_on(F2FS_P_SB(page), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080054
Chao Yue84f88e2017-07-19 00:19:05 +080055 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080056
57 /* Copy the whole inline data block */
Chao Yue84f88e2017-07-19 00:19:05 +080058 src_addr = inline_data_addr(inode, ipage);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070059 dst_addr = kmap_atomic(page);
Chao Yue84f88e2017-07-19 00:19:05 +080060 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
Jaegeuk Kim427a45c2014-10-26 22:59:27 -070061 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070062 kunmap_atomic(dst_addr);
Jaegeuk Kim237c0792016-06-30 18:49:15 -070063 if (!PageUptodate(page))
64 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070065}
Huajun Lie18c65b2013-11-10 23:13:19 +080066
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070067void truncate_inline_inode(struct inode *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 Yue84f88e2017-07-19 00:19:05 +080071 if (from >= MAX_INLINE_DATA(inode))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070072 return;
Chao Yu0bfcfcc2015-03-10 13:16:25 +080073
Chao Yue84f88e2017-07-19 00:19:05 +080074 addr = inline_data_addr(inode, ipage);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080075
Jaegeuk Kimfec1d652016-01-20 23:43:51 +080076 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yue84f88e2017-07-19 00:19:05 +080077 memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -070078 set_page_dirty(ipage);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070079
80 if (from == 0)
81 clear_inode_flag(inode, FI_DATA_EXIST);
Chao Yufeeb0de2015-01-26 20:22:55 +080082}
83
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070084int f2fs_read_inline_data(struct inode *inode, struct page *page)
85{
86 struct page *ipage;
87
88 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
89 if (IS_ERR(ipage)) {
90 unlock_page(page);
91 return PTR_ERR(ipage);
92 }
93
94 if (!f2fs_has_inline_data(inode)) {
95 f2fs_put_page(ipage, 1);
96 return -EAGAIN;
97 }
98
99 if (page->index)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300100 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700101 else
102 read_inline_data(page, ipage);
103
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700104 if (!PageUptodate(page))
105 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700106 f2fs_put_page(ipage, 1);
107 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800108 return 0;
109}
110
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700111int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800112{
Huajun Lie18c65b2013-11-10 23:13:19 +0800113 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700114 .sbi = F2FS_I_SB(dn->inode),
Huajun Lie18c65b2013-11-10 23:13:19 +0800115 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500116 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700117 .op_flags = REQ_SYNC | REQ_PRIO,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700118 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700119 .encrypted_page = NULL,
Chao Yuc0fe4882017-08-02 23:21:48 +0800120 .io_type = FS_DATA_IO,
Huajun Lie18c65b2013-11-10 23:13:19 +0800121 };
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800122 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800123
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700124 if (!f2fs_exist_data(dn->inode))
125 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700126
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700127 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900128 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700129 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800130
Yunlei He85ead812016-02-03 10:26:13 +0800131 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700132
Shawn Lin80606562016-02-19 16:02:51 +0800133 read_inline_data(page, dn->inode_page);
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700134 set_page_dirty(page);
135
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800136 /* clear dirty state */
137 dirty = clear_page_dirty_for_io(page);
138
Huajun Lie18c65b2013-11-10 23:13:19 +0800139 /* write data page to try to make data consistent */
140 set_page_writeback(page);
Chao Yu7a9d7542016-02-22 18:36:38 +0800141 fio.old_blkaddr = dn->data_blkaddr;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700142 set_inode_flag(dn->inode, FI_HOT_DATA);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700143 write_data_page(dn, &fio);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800144 f2fs_wait_on_page_writeback(page, DATA, true);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700145 if (dirty) {
Jaegeuk Kim158c1942014-11-25 11:34:02 -0800146 inode_dec_dirty_pages(dn->inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700147 remove_dirty_inode(dn->inode);
148 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800149
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800150 /* this converted inline_data should be recovered. */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700151 set_inode_flag(dn->inode, FI_APPEND_WRITE);
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800152
Huajun Lie18c65b2013-11-10 23:13:19 +0800153 /* clear inline data and flag after data writeback */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700154 truncate_inline_inode(dn->inode, dn->inode_page, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800155 clear_inline_node(dn->inode_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700156clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700157 stat_dec_inline_inode(dn->inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700158 clear_inode_flag(dn->inode, FI_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700159 f2fs_put_dnode(dn);
160 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800161}
162
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700163int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800164{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700165 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
166 struct dnode_of_data dn;
167 struct page *ipage, *page;
168 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800169
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800170 if (!f2fs_has_inline_data(inode))
171 return 0;
172
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700173 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700174 if (!page)
175 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900176
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700177 f2fs_lock_op(sbi);
178
179 ipage = get_node_page(sbi, inode->i_ino);
180 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800181 err = PTR_ERR(ipage);
182 goto out;
Jaegeuk Kimb067ba12014-08-07 16:32:25 -0700183 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800184
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700185 set_new_dnode(&dn, inode, ipage, ipage, 0);
186
187 if (f2fs_has_inline_data(inode))
188 err = f2fs_convert_inline_page(&dn, page);
189
190 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800191out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700192 f2fs_unlock_op(sbi);
193
194 f2fs_put_page(page, 1);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800195
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800196 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800197
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;
Daeho Jeongbb17c212017-09-11 16:30:28 +0900205 struct address_space *mapping = page_mapping(page);
206 unsigned long flags;
Huajun Lie18c65b2013-11-10 23:13:19 +0800207 int err;
208
209 set_new_dnode(&dn, inode, NULL, NULL, 0);
210 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
211 if (err)
212 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800213
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700214 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700215 f2fs_put_dnode(&dn);
216 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700217 }
218
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700219 f2fs_bug_on(F2FS_I_SB(inode), page->index);
220
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800221 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700222 src_addr = kmap_atomic(page);
Chao Yue84f88e2017-07-19 00:19:05 +0800223 dst_addr = inline_data_addr(inode, dn.inode_page);
224 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700225 kunmap_atomic(src_addr);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700226 set_page_dirty(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800227
Daeho Jeongbb17c212017-09-11 16:30:28 +0900228 spin_lock_irqsave(&mapping->tree_lock, flags);
229 radix_tree_tag_clear(&mapping->page_tree, page_index(page),
230 PAGECACHE_TAG_DIRTY);
231 spin_unlock_irqrestore(&mapping->tree_lock, flags);
232
Jaegeuk Kim91942322016-05-20 10:13:22 -0700233 set_inode_flag(inode, FI_APPEND_WRITE);
234 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700235
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800236 clear_inline_node(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800237 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800238 return 0;
239}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900240
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700241bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900242{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700243 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900244 struct f2fs_inode *ri = NULL;
245 void *src_addr, *dst_addr;
246 struct page *ipage;
247
248 /*
249 * The inline_data recovery policy is as follows.
250 * [prev.] [next] of inline_data flag
251 * o o -> recover inline_data
252 * o x -> remove inline_data, and then recover data blocks
253 * x o -> remove inline_data, and then recover inline_data
254 * x x -> recover data blocks
255 */
256 if (IS_INODE(npage))
257 ri = F2FS_INODE(npage);
258
259 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700260 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900261process_inline:
262 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700263 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900264
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800265 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900266
Chao Yue84f88e2017-07-19 00:19:05 +0800267 src_addr = inline_data_addr(inode, npage);
268 dst_addr = inline_data_addr(inode, ipage);
269 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700270
Jaegeuk Kim91942322016-05-20 10:13:22 -0700271 set_inode_flag(inode, FI_INLINE_DATA);
272 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700273
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700274 set_page_dirty(ipage);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900275 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700276 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900277 }
278
279 if (f2fs_has_inline_data(inode)) {
280 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700281 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700282 truncate_inline_inode(inode, ipage, 0);
283 clear_inode_flag(inode, FI_INLINE_DATA);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900284 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700285 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Nicholas Krause545fe422015-09-21 18:55:49 -0400286 if (truncate_blocks(inode, 0, false))
287 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900288 goto process_inline;
289 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700290 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900291}
Chao Yu201a05b2014-09-24 18:17:53 +0800292
293struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700294 struct fscrypt_name *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800295{
296 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700297 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
Chao Yu201a05b2014-09-24 18:17:53 +0800298 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700299 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700300 struct page *ipage;
Chao Yue84f88e2017-07-19 00:19:05 +0800301 void *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700302 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800303
304 ipage = get_node_page(sbi, dir->i_ino);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700305 if (IS_ERR(ipage)) {
306 *res_page = ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800307 return NULL;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700308 }
Chao Yu201a05b2014-09-24 18:17:53 +0800309
Jaegeuk Kim8daed212017-04-24 10:00:08 -0700310 namehash = f2fs_dentry_hash(&name, fname);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700311
Chao Yue84f88e2017-07-19 00:19:05 +0800312 inline_dentry = inline_data_addr(dir, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800313
Chao Yue84f88e2017-07-19 00:19:05 +0800314 make_dentry_ptr_inline(dir, &d, inline_dentry);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700315 de = find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800316 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700317 if (de)
318 *res_page = ipage;
319 else
320 f2fs_put_page(ipage, 0);
321
Chao Yu201a05b2014-09-24 18:17:53 +0800322 return de;
323}
324
Chao Yu201a05b2014-09-24 18:17:53 +0800325int make_empty_inline_dir(struct inode *inode, struct inode *parent,
326 struct page *ipage)
327{
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700328 struct f2fs_dentry_ptr d;
Chao Yue84f88e2017-07-19 00:19:05 +0800329 void *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800330
Chao Yue84f88e2017-07-19 00:19:05 +0800331 inline_dentry = inline_data_addr(inode, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800332
Chao Yue84f88e2017-07-19 00:19:05 +0800333 make_dentry_ptr_inline(inode, &d, inline_dentry);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700334 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800335
336 set_page_dirty(ipage);
337
338 /* update i_size to MAX_INLINE_DATA */
Chao Yue84f88e2017-07-19 00:19:05 +0800339 if (i_size_read(inode) < MAX_INLINE_DATA(inode))
340 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
Chao Yu201a05b2014-09-24 18:17:53 +0800341 return 0;
342}
343
Chao Yu470f00e2015-07-14 18:14:06 +0800344/*
345 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
346 * release ipage in this function.
347 */
Chao Yu675f10b2016-02-22 18:29:18 +0800348static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
Chao Yue84f88e2017-07-19 00:19:05 +0800349 void *inline_dentry)
Chao Yu201a05b2014-09-24 18:17:53 +0800350{
351 struct page *page;
352 struct dnode_of_data dn;
353 struct f2fs_dentry_block *dentry_blk;
Chao Yub75c5f62017-07-16 15:08:54 +0800354 struct f2fs_dentry_ptr src, dst;
Chao Yu201a05b2014-09-24 18:17:53 +0800355 int err;
356
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700357 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
Chao Yu470f00e2015-07-14 18:14:06 +0800358 if (!page) {
359 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800360 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800361 }
Chao Yu201a05b2014-09-24 18:17:53 +0800362
363 set_new_dnode(&dn, dir, ipage, NULL, 0);
364 err = f2fs_reserve_block(&dn, 0);
365 if (err)
366 goto out;
367
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800368 f2fs_wait_on_page_writeback(page, DATA, true);
Chao Yue84f88e2017-07-19 00:19:05 +0800369 zero_user_segment(page, MAX_INLINE_DATA(dir), PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800370
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700371 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800372
Chao Yue84f88e2017-07-19 00:19:05 +0800373 make_dentry_ptr_inline(dir, &src, inline_dentry);
374 make_dentry_ptr_block(dir, &dst, dentry_blk);
Chao Yub75c5f62017-07-16 15:08:54 +0800375
Chao Yu201a05b2014-09-24 18:17:53 +0800376 /* copy data from inline dentry block to new dentry block */
Chao Yub75c5f62017-07-16 15:08:54 +0800377 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
378 memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap);
Chao Yu4ec17d62015-08-24 17:36:25 +0800379 /*
380 * we do not need to zero out remainder part of dentry and filename
381 * field, since we have used bitmap for marking the usage status of
382 * them, besides, we can also ignore copying/zeroing reserved space
383 * of dentry block, because them haven't been used so far.
384 */
Chao Yub75c5f62017-07-16 15:08:54 +0800385 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
386 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
Chao Yu201a05b2014-09-24 18:17:53 +0800387
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700388 kunmap_atomic(dentry_blk);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700389 if (!PageUptodate(page))
390 SetPageUptodate(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800391 set_page_dirty(page);
392
393 /* clear inline dir and flag after data writeback */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700394 truncate_inline_inode(dir, ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700395
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700396 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700397 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800398
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700399 f2fs_i_depth_write(dir, 1);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700400 if (i_size_read(dir) < PAGE_SIZE)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700401 f2fs_i_size_write(dir, PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800402out:
403 f2fs_put_page(page, 1);
404 return err;
405}
406
Chao Yue84f88e2017-07-19 00:19:05 +0800407static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800408{
409 struct f2fs_dentry_ptr d;
410 unsigned long bit_pos = 0;
411 int err = 0;
412
Chao Yue84f88e2017-07-19 00:19:05 +0800413 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yu675f10b2016-02-22 18:29:18 +0800414
415 while (bit_pos < d.max) {
416 struct f2fs_dir_entry *de;
417 struct qstr new_name;
418 nid_t ino;
419 umode_t fake_mode;
420
421 if (!test_bit_le(bit_pos, d.bitmap)) {
422 bit_pos++;
423 continue;
424 }
425
426 de = &d.dentry[bit_pos];
Chao Yua4a13f52016-04-27 22:22:20 +0800427
428 if (unlikely(!de->name_len)) {
429 bit_pos++;
430 continue;
431 }
432
Chao Yu675f10b2016-02-22 18:29:18 +0800433 new_name.name = d.filename[bit_pos];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700434 new_name.len = le16_to_cpu(de->name_len);
Chao Yu675f10b2016-02-22 18:29:18 +0800435
436 ino = le32_to_cpu(de->ino);
437 fake_mode = get_de_type(de) << S_SHIFT;
438
Chao Yu9421d572016-08-28 18:57:55 +0800439 err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL,
Chao Yu675f10b2016-02-22 18:29:18 +0800440 ino, fake_mode);
441 if (err)
442 goto punch_dentry_pages;
443
Chao Yu675f10b2016-02-22 18:29:18 +0800444 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
445 }
446 return 0;
447punch_dentry_pages:
448 truncate_inode_pages(&dir->i_data, 0);
449 truncate_blocks(dir, 0, false);
450 remove_dirty_inode(dir);
451 return err;
452}
453
454static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
Chao Yue84f88e2017-07-19 00:19:05 +0800455 void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800456{
Chao Yue84f88e2017-07-19 00:19:05 +0800457 void *backup_dentry;
Chao Yu675f10b2016-02-22 18:29:18 +0800458 int err;
459
Chao Yu1ecc0c52016-09-23 21:30:09 +0800460 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
Chao Yue84f88e2017-07-19 00:19:05 +0800461 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
Chao Yu8975bdf2016-05-14 19:03:53 +0800462 if (!backup_dentry) {
463 f2fs_put_page(ipage, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800464 return -ENOMEM;
Chao Yu8975bdf2016-05-14 19:03:53 +0800465 }
Chao Yu675f10b2016-02-22 18:29:18 +0800466
Chao Yue84f88e2017-07-19 00:19:05 +0800467 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700468 truncate_inline_inode(dir, ipage, 0);
Chao Yu675f10b2016-02-22 18:29:18 +0800469
470 unlock_page(ipage);
471
472 err = f2fs_add_inline_entries(dir, backup_dentry);
473 if (err)
474 goto recover;
475
476 lock_page(ipage);
477
478 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700479 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu675f10b2016-02-22 18:29:18 +0800480 kfree(backup_dentry);
481 return 0;
482recover:
483 lock_page(ipage);
Chao Yue84f88e2017-07-19 00:19:05 +0800484 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700485 f2fs_i_depth_write(dir, 0);
Chao Yue84f88e2017-07-19 00:19:05 +0800486 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700487 set_page_dirty(ipage);
Chao Yu675f10b2016-02-22 18:29:18 +0800488 f2fs_put_page(ipage, 1);
489
490 kfree(backup_dentry);
491 return err;
492}
493
494static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
Chao Yue84f88e2017-07-19 00:19:05 +0800495 void *inline_dentry)
Chao Yu675f10b2016-02-22 18:29:18 +0800496{
497 if (!F2FS_I(dir)->i_dir_level)
498 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
499 else
500 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
501}
502
Chao Yu9421d572016-08-28 18:57:55 +0800503int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
504 const struct qstr *orig_name,
505 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800506{
507 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
508 struct page *ipage;
509 unsigned int bit_pos;
510 f2fs_hash_t name_hash;
Chao Yue84f88e2017-07-19 00:19:05 +0800511 void *inline_dentry = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800512 struct f2fs_dentry_ptr d;
Chao Yu9421d572016-08-28 18:57:55 +0800513 int slots = GET_DENTRY_SLOTS(new_name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700514 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800515 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800516
517 ipage = get_node_page(sbi, dir->i_ino);
518 if (IS_ERR(ipage))
519 return PTR_ERR(ipage);
520
Chao Yue84f88e2017-07-19 00:19:05 +0800521 inline_dentry = inline_data_addr(dir, ipage);
522 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yub75c5f62017-07-16 15:08:54 +0800523
524 bit_pos = room_for_filename(d.bitmap, slots, d.max);
525 if (bit_pos >= d.max) {
Tiezhu Yangab3d0b22017-06-09 06:32:54 +0800526 err = f2fs_convert_inline_dir(dir, ipage, inline_dentry);
Chao Yu470f00e2015-07-14 18:14:06 +0800527 if (err)
528 return err;
529 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800530 goto out;
531 }
532
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700533 if (inode) {
534 down_write(&F2FS_I(inode)->i_sem);
Chao Yu9421d572016-08-28 18:57:55 +0800535 page = init_inode_metadata(inode, dir, new_name,
536 orig_name, ipage);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700537 if (IS_ERR(page)) {
538 err = PTR_ERR(page);
539 goto fail;
540 }
Chao Yu201a05b2014-09-24 18:17:53 +0800541 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700542
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800543 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu3b4d7322015-02-16 16:17:20 +0800544
Jaegeuk Kim8daed212017-04-24 10:00:08 -0700545 name_hash = f2fs_dentry_hash(new_name, NULL);
Chao Yu9421d572016-08-28 18:57:55 +0800546 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800547
Chao Yu201a05b2014-09-24 18:17:53 +0800548 set_page_dirty(ipage);
549
550 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700551 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700552 f2fs_i_pino_write(inode, dir->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700553 f2fs_put_page(page, 1);
554 }
Chao Yu201a05b2014-09-24 18:17:53 +0800555
556 update_parent_metadata(dir, inode, 0);
557fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700558 if (inode)
559 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800560out:
561 f2fs_put_page(ipage, 1);
562 return err;
563}
564
565void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
566 struct inode *dir, struct inode *inode)
567{
Chao Yub75c5f62017-07-16 15:08:54 +0800568 struct f2fs_dentry_ptr d;
Chao Yue84f88e2017-07-19 00:19:05 +0800569 void *inline_dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800570 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
571 unsigned int bit_pos;
572 int i;
573
574 lock_page(page);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800575 f2fs_wait_on_page_writeback(page, NODE, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800576
Chao Yue84f88e2017-07-19 00:19:05 +0800577 inline_dentry = inline_data_addr(dir, page);
578 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yub75c5f62017-07-16 15:08:54 +0800579
580 bit_pos = dentry - d.dentry;
Chao Yu201a05b2014-09-24 18:17:53 +0800581 for (i = 0; i < slots; i++)
Chao Yub75c5f62017-07-16 15:08:54 +0800582 __clear_bit_le(bit_pos + i, d.bitmap);
Chao Yu201a05b2014-09-24 18:17:53 +0800583
584 set_page_dirty(page);
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700585 f2fs_put_page(page, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800586
Deepa Dinamani078cd822016-09-14 07:48:04 -0700587 dir->i_ctime = dir->i_mtime = current_time(dir);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700588 f2fs_mark_inode_dirty_sync(dir, false);
Chao Yu201a05b2014-09-24 18:17:53 +0800589
590 if (inode)
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700591 f2fs_drop_nlink(dir, inode);
Chao Yu201a05b2014-09-24 18:17:53 +0800592}
593
594bool f2fs_empty_inline_dir(struct inode *dir)
595{
596 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
597 struct page *ipage;
598 unsigned int bit_pos = 2;
Chao Yue84f88e2017-07-19 00:19:05 +0800599 void *inline_dentry;
Chao Yub75c5f62017-07-16 15:08:54 +0800600 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800601
602 ipage = get_node_page(sbi, dir->i_ino);
603 if (IS_ERR(ipage))
604 return false;
605
Chao Yue84f88e2017-07-19 00:19:05 +0800606 inline_dentry = inline_data_addr(dir, ipage);
607 make_dentry_ptr_inline(dir, &d, inline_dentry);
Chao Yub75c5f62017-07-16 15:08:54 +0800608
609 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
Chao Yu201a05b2014-09-24 18:17:53 +0800610
611 f2fs_put_page(ipage, 1);
612
Chao Yub75c5f62017-07-16 15:08:54 +0800613 if (bit_pos < d.max)
Chao Yu201a05b2014-09-24 18:17:53 +0800614 return false;
615
616 return true;
617}
618
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700619int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700620 struct fscrypt_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800621{
622 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800623 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700624 struct f2fs_dentry_ptr d;
Chao Yue84f88e2017-07-19 00:19:05 +0800625 void *inline_dentry = NULL;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700626 int err;
Chao Yu201a05b2014-09-24 18:17:53 +0800627
Chao Yub75c5f62017-07-16 15:08:54 +0800628 make_dentry_ptr_inline(inode, &d, inline_dentry);
629
630 if (ctx->pos == d.max)
Chao Yu201a05b2014-09-24 18:17:53 +0800631 return 0;
632
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700633 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800634 if (IS_ERR(ipage))
635 return PTR_ERR(ipage);
636
Chao Yue84f88e2017-07-19 00:19:05 +0800637 inline_dentry = inline_data_addr(inode, ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800638
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700639 make_dentry_ptr_inline(inode, &d, inline_dentry);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700640
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700641 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
642 if (!err)
Chao Yub75c5f62017-07-16 15:08:54 +0800643 ctx->pos = d.max;
Chao Yu201a05b2014-09-24 18:17:53 +0800644
Chao Yu201a05b2014-09-24 18:17:53 +0800645 f2fs_put_page(ipage, 1);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700646 return err < 0 ? err : 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800647}
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700648
649int f2fs_inline_data_fiemap(struct inode *inode,
650 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
651{
652 __u64 byteaddr, ilen;
653 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
654 FIEMAP_EXTENT_LAST;
655 struct node_info ni;
656 struct page *ipage;
657 int err = 0;
658
659 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
660 if (IS_ERR(ipage))
661 return PTR_ERR(ipage);
662
663 if (!f2fs_has_inline_data(inode)) {
664 err = -EAGAIN;
665 goto out;
666 }
667
Chao Yue84f88e2017-07-19 00:19:05 +0800668 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700669 if (start >= ilen)
670 goto out;
671 if (start + len < ilen)
672 ilen = start + len;
673 ilen -= start;
674
675 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
676 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
Chao Yue84f88e2017-07-19 00:19:05 +0800677 byteaddr += (char *)inline_data_addr(inode, ipage) -
678 (char *)F2FS_INODE(ipage);
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700679 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
680out:
681 f2fs_put_page(ipage, 1);
682 return err;
683}