blob: d411ab6c74832ec232b87618edc985c83779559b [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 Kim67f8cf32015-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
Jaegeuk Kim92dffd02014-11-11 14:10:01 -080025 if (i_size_read(inode) > MAX_INLINE_DATA)
26 return false;
27
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -070028 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
29 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{
Huajun Lie18c65b2013-11-10 23:13:19 +080047 void *src_addr, *dst_addr;
48
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070049 if (PageUptodate(page))
50 return;
Chao Yu04a17fb2013-12-30 18:36:23 +080051
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070052 f2fs_bug_on(F2FS_P_SB(page), page->index);
Huajun Lie18c65b2013-11-10 23:13:19 +080053
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030054 zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080055
56 /* Copy the whole inline data block */
57 src_addr = inline_data_addr(ipage);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070058 dst_addr = kmap_atomic(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080059 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kim427a45c2014-10-26 22:59:27 -070060 flush_dcache_page(page);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -070061 kunmap_atomic(dst_addr);
Jaegeuk Kim237c0792016-06-30 18:49:15 -070062 if (!PageUptodate(page))
63 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070064}
Huajun Lie18c65b2013-11-10 23:13:19 +080065
Chao Yu0bfcfcc2015-03-10 13:16:25 +080066bool truncate_inline_inode(struct page *ipage, u64 from)
Chao Yufeeb0de2015-01-26 20:22:55 +080067{
Chao Yu0bfcfcc2015-03-10 13:16:25 +080068 void *addr;
69
Chao Yu0bfcfcc2015-03-10 13:16:25 +080070 if (from >= MAX_INLINE_DATA)
71 return false;
72
73 addr = inline_data_addr(ipage);
74
Jaegeuk Kimfec1d652016-01-20 23:43:51 +080075 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080076 memset(addr + from, 0, MAX_INLINE_DATA - from);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -070077 set_page_dirty(ipage);
Chao Yu0bfcfcc2015-03-10 13:16:25 +080078 return true;
Chao Yufeeb0de2015-01-26 20:22:55 +080079}
80
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070081int f2fs_read_inline_data(struct inode *inode, struct page *page)
82{
83 struct page *ipage;
84
85 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
86 if (IS_ERR(ipage)) {
87 unlock_page(page);
88 return PTR_ERR(ipage);
89 }
90
91 if (!f2fs_has_inline_data(inode)) {
92 f2fs_put_page(ipage, 1);
93 return -EAGAIN;
94 }
95
96 if (page->index)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030097 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070098 else
99 read_inline_data(page, ipage);
100
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700101 if (!PageUptodate(page))
102 SetPageUptodate(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700103 f2fs_put_page(ipage, 1);
104 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800105 return 0;
106}
107
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700108int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800109{
Huajun Lie18c65b2013-11-10 23:13:19 +0800110 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700111 .sbi = F2FS_I_SB(dn->inode),
Huajun Lie18c65b2013-11-10 23:13:19 +0800112 .type = DATA,
113 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700114 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700115 .encrypted_page = NULL,
Huajun Lie18c65b2013-11-10 23:13:19 +0800116 };
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800117 int dirty, err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800118
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700119 if (!f2fs_exist_data(dn->inode))
120 goto clear_out;
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -0700121
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700122 err = f2fs_reserve_block(dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900123 if (err)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700124 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800125
Yunlei He85ead812016-02-03 10:26:13 +0800126 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700127
Shawn Lin80606562016-02-19 16:02:51 +0800128 read_inline_data(page, dn->inode_page);
Jaegeuk Kim6282adb2015-07-25 00:29:17 -0700129 set_page_dirty(page);
130
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800131 /* clear dirty state */
132 dirty = clear_page_dirty_for_io(page);
133
Huajun Lie18c65b2013-11-10 23:13:19 +0800134 /* write data page to try to make data consistent */
135 set_page_writeback(page);
Chao Yu7a9d7542016-02-22 18:36:38 +0800136 fio.old_blkaddr = dn->data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700137 write_data_page(dn, &fio);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800138 f2fs_wait_on_page_writeback(page, DATA, true);
Jaegeuk Kim158c194c2014-11-25 11:34:02 -0800139 if (dirty)
140 inode_dec_dirty_pages(dn->inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800141
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800142 /* this converted inline_data should be recovered. */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700143 set_inode_flag(dn->inode, FI_APPEND_WRITE);
Jaegeuk Kim95f5b0f2014-11-25 17:27:38 -0800144
Huajun Lie18c65b2013-11-10 23:13:19 +0800145 /* clear inline data and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800146 truncate_inline_inode(dn->inode_page, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800147 clear_inline_node(dn->inode_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700148clear_out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700149 stat_dec_inline_inode(dn->inode);
Jaegeuk Kim57e2a2c2014-11-10 16:29:14 -0800150 f2fs_clear_inline_inode(dn->inode);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700151 f2fs_put_dnode(dn);
152 return 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800153}
154
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700155int f2fs_convert_inline_inode(struct inode *inode)
Huajun Lie18c65b2013-11-10 23:13:19 +0800156{
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700157 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
158 struct dnode_of_data dn;
159 struct page *ipage, *page;
160 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +0800161
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -0800162 if (!f2fs_has_inline_data(inode))
163 return 0;
164
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700165 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700166 if (!page)
167 return -ENOMEM;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900168
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700169 f2fs_lock_op(sbi);
170
171 ipage = get_node_page(sbi, inode->i_ino);
172 if (IS_ERR(ipage)) {
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800173 err = PTR_ERR(ipage);
174 goto out;
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700175 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800176
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700177 set_new_dnode(&dn, inode, ipage, ipage, 0);
178
179 if (f2fs_has_inline_data(inode))
180 err = f2fs_convert_inline_page(&dn, page);
181
182 f2fs_put_dnode(&dn);
Jaegeuk Kim6d20aff2014-11-17 16:06:55 -0800183out:
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700184 f2fs_unlock_op(sbi);
185
186 f2fs_put_page(page, 1);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800187
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800188 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800189
Huajun Lie18c65b2013-11-10 23:13:19 +0800190 return err;
191}
192
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700193int f2fs_write_inline_data(struct inode *inode, struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800194{
195 void *src_addr, *dst_addr;
Huajun Lie18c65b2013-11-10 23:13:19 +0800196 struct dnode_of_data dn;
197 int err;
198
199 set_new_dnode(&dn, inode, NULL, NULL, 0);
200 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
201 if (err)
202 return err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800203
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700204 if (!f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700205 f2fs_put_dnode(&dn);
206 return -EAGAIN;
Jaegeuk Kimc08a6902014-10-15 10:16:54 -0700207 }
208
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700209 f2fs_bug_on(F2FS_I_SB(inode), page->index);
210
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800211 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700212 src_addr = kmap_atomic(page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700213 dst_addr = inline_data_addr(dn.inode_page);
214 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700215 kunmap_atomic(src_addr);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700216 set_page_dirty(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800217
Jaegeuk Kim91942322016-05-20 10:13:22 -0700218 set_inode_flag(inode, FI_APPEND_WRITE);
219 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700220
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800221 clear_inline_node(dn.inode_page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800222 f2fs_put_dnode(&dn);
Huajun Lie18c65b2013-11-10 23:13:19 +0800223 return 0;
224}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900225
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700226bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900227{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700228 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900229 struct f2fs_inode *ri = NULL;
230 void *src_addr, *dst_addr;
231 struct page *ipage;
232
233 /*
234 * The inline_data recovery policy is as follows.
235 * [prev.] [next] of inline_data flag
236 * o o -> recover inline_data
237 * o x -> remove inline_data, and then recover data blocks
238 * x o -> remove inline_data, and then recover inline_data
239 * x x -> recover data blocks
240 */
241 if (IS_INODE(npage))
242 ri = F2FS_INODE(npage);
243
244 if (f2fs_has_inline_data(inode) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700245 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900246process_inline:
247 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700248 f2fs_bug_on(sbi, IS_ERR(ipage));
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900249
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800250 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900251
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900252 src_addr = inline_data_addr(npage);
253 dst_addr = inline_data_addr(ipage);
254 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700255
Jaegeuk Kim91942322016-05-20 10:13:22 -0700256 set_inode_flag(inode, FI_INLINE_DATA);
257 set_inode_flag(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700258
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700259 set_page_dirty(ipage);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900260 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700261 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900262 }
263
264 if (f2fs_has_inline_data(inode)) {
265 ipage = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700266 f2fs_bug_on(sbi, IS_ERR(ipage));
Nicholas Krause545fe422015-09-21 18:55:49 -0400267 if (!truncate_inline_inode(ipage, 0))
268 return false;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700269 f2fs_clear_inline_inode(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900270 f2fs_put_page(ipage, 1);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700271 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Nicholas Krause545fe422015-09-21 18:55:49 -0400272 if (truncate_blocks(inode, 0, false))
273 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900274 goto process_inline;
275 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700276 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900277}
Chao Yu201a05b2014-09-24 18:17:53 +0800278
279struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700280 struct fscrypt_name *fname, struct page **res_page)
Chao Yu201a05b2014-09-24 18:17:53 +0800281{
282 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700283 struct f2fs_inline_dentry *inline_dentry;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700284 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
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;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700288 f2fs_hash_t namehash;
Chao Yu201a05b2014-09-24 18:17:53 +0800289
290 ipage = get_node_page(sbi, dir->i_ino);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700291 if (IS_ERR(ipage)) {
292 *res_page = ipage;
Chao Yu201a05b2014-09-24 18:17:53 +0800293 return NULL;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700294 }
Chao Yu201a05b2014-09-24 18:17:53 +0800295
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700296 namehash = f2fs_dentry_hash(&name);
297
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700298 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800299
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700300 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700301 de = find_target_dentry(fname, namehash, NULL, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800302 unlock_page(ipage);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700303 if (de)
304 *res_page = ipage;
305 else
306 f2fs_put_page(ipage, 0);
307
Chao Yu201a05b2014-09-24 18:17:53 +0800308 return de;
309}
310
Chao Yu201a05b2014-09-24 18:17:53 +0800311int make_empty_inline_dir(struct inode *inode, struct inode *parent,
312 struct page *ipage)
313{
314 struct f2fs_inline_dentry *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700315 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800316
317 dentry_blk = inline_data_addr(ipage);
318
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700319 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700320 do_make_empty_dir(inode, parent, &d);
Chao Yu201a05b2014-09-24 18:17:53 +0800321
322 set_page_dirty(ipage);
323
324 /* update i_size to MAX_INLINE_DATA */
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700325 if (i_size_read(inode) < MAX_INLINE_DATA)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700326 f2fs_i_size_write(inode, MAX_INLINE_DATA);
Chao Yu201a05b2014-09-24 18:17:53 +0800327 return 0;
328}
329
Chao Yu470f00e2015-07-14 18:14:06 +0800330/*
331 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
332 * release ipage in this function.
333 */
Chao Yu675f10b2016-02-22 18:29:18 +0800334static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
Chao Yu201a05b2014-09-24 18:17:53 +0800335 struct f2fs_inline_dentry *inline_dentry)
336{
337 struct page *page;
338 struct dnode_of_data dn;
339 struct f2fs_dentry_block *dentry_blk;
340 int err;
341
Jaegeuk Kim300e1292016-04-29 16:11:53 -0700342 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
Chao Yu470f00e2015-07-14 18:14:06 +0800343 if (!page) {
344 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800345 return -ENOMEM;
Chao Yu470f00e2015-07-14 18:14:06 +0800346 }
Chao Yu201a05b2014-09-24 18:17:53 +0800347
348 set_new_dnode(&dn, dir, ipage, NULL, 0);
349 err = f2fs_reserve_block(&dn, 0);
350 if (err)
351 goto out;
352
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800353 f2fs_wait_on_page_writeback(page, DATA, true);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300354 zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800355
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700356 dentry_blk = kmap_atomic(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800357
358 /* copy data from inline dentry block to new dentry block */
359 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
360 INLINE_DENTRY_BITMAP_SIZE);
Chao Yu4ec17d62015-08-24 17:36:25 +0800361 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
362 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
363 /*
364 * we do not need to zero out remainder part of dentry and filename
365 * field, since we have used bitmap for marking the usage status of
366 * them, besides, we can also ignore copying/zeroing reserved space
367 * of dentry block, because them haven't been used so far.
368 */
Chao Yu201a05b2014-09-24 18:17:53 +0800369 memcpy(dentry_blk->dentry, inline_dentry->dentry,
370 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
371 memcpy(dentry_blk->filename, inline_dentry->filename,
372 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
373
Jaegeuk Kimf1e33a02014-10-18 23:41:38 -0700374 kunmap_atomic(dentry_blk);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700375 if (!PageUptodate(page))
376 SetPageUptodate(page);
Chao Yu201a05b2014-09-24 18:17:53 +0800377 set_page_dirty(page);
378
379 /* clear inline dir and flag after data writeback */
Chao Yu0bfcfcc2015-03-10 13:16:25 +0800380 truncate_inline_inode(ipage, 0);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700381
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700382 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700383 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800384
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700385 f2fs_i_depth_write(dir, 1);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700386 if (i_size_read(dir) < PAGE_SIZE)
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700387 f2fs_i_size_write(dir, PAGE_SIZE);
Chao Yu201a05b2014-09-24 18:17:53 +0800388out:
389 f2fs_put_page(page, 1);
390 return err;
391}
392
Chao Yu675f10b2016-02-22 18:29:18 +0800393static int f2fs_add_inline_entries(struct inode *dir,
394 struct f2fs_inline_dentry *inline_dentry)
395{
396 struct f2fs_dentry_ptr d;
397 unsigned long bit_pos = 0;
398 int err = 0;
399
400 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
401
402 while (bit_pos < d.max) {
403 struct f2fs_dir_entry *de;
404 struct qstr new_name;
405 nid_t ino;
406 umode_t fake_mode;
407
408 if (!test_bit_le(bit_pos, d.bitmap)) {
409 bit_pos++;
410 continue;
411 }
412
413 de = &d.dentry[bit_pos];
Chao Yua4a13f52016-04-27 22:22:20 +0800414
415 if (unlikely(!de->name_len)) {
416 bit_pos++;
417 continue;
418 }
419
Chao Yu675f10b2016-02-22 18:29:18 +0800420 new_name.name = d.filename[bit_pos];
421 new_name.len = de->name_len;
422
423 ino = le32_to_cpu(de->ino);
424 fake_mode = get_de_type(de) << S_SHIFT;
425
426 err = f2fs_add_regular_entry(dir, &new_name, NULL,
427 ino, fake_mode);
428 if (err)
429 goto punch_dentry_pages;
430
Chao Yu675f10b2016-02-22 18:29:18 +0800431 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
432 }
433 return 0;
434punch_dentry_pages:
435 truncate_inode_pages(&dir->i_data, 0);
436 truncate_blocks(dir, 0, false);
437 remove_dirty_inode(dir);
438 return err;
439}
440
441static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
442 struct f2fs_inline_dentry *inline_dentry)
443{
444 struct f2fs_inline_dentry *backup_dentry;
445 int err;
446
Jaegeuk Kim0414b002016-04-29 15:16:42 -0700447 backup_dentry = f2fs_kmalloc(sizeof(struct f2fs_inline_dentry),
Chao Yu675f10b2016-02-22 18:29:18 +0800448 GFP_F2FS_ZERO);
Chao Yu8975bdf2016-05-14 19:03:53 +0800449 if (!backup_dentry) {
450 f2fs_put_page(ipage, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800451 return -ENOMEM;
Chao Yu8975bdf2016-05-14 19:03:53 +0800452 }
Chao Yu675f10b2016-02-22 18:29:18 +0800453
454 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA);
455 truncate_inline_inode(ipage, 0);
456
457 unlock_page(ipage);
458
459 err = f2fs_add_inline_entries(dir, backup_dentry);
460 if (err)
461 goto recover;
462
463 lock_page(ipage);
464
465 stat_dec_inline_dir(dir);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700466 clear_inode_flag(dir, FI_INLINE_DENTRY);
Chao Yu675f10b2016-02-22 18:29:18 +0800467 kfree(backup_dentry);
468 return 0;
469recover:
470 lock_page(ipage);
471 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700472 f2fs_i_depth_write(dir, 0);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700473 f2fs_i_size_write(dir, MAX_INLINE_DATA);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700474 set_page_dirty(ipage);
Chao Yu675f10b2016-02-22 18:29:18 +0800475 f2fs_put_page(ipage, 1);
476
477 kfree(backup_dentry);
478 return err;
479}
480
481static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
482 struct f2fs_inline_dentry *inline_dentry)
483{
484 if (!F2FS_I(dir)->i_dir_level)
485 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
486 else
487 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
488}
489
Chao Yu201a05b2014-09-24 18:17:53 +0800490int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700491 struct inode *inode, nid_t ino, umode_t mode)
Chao Yu201a05b2014-09-24 18:17:53 +0800492{
493 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
494 struct page *ipage;
495 unsigned int bit_pos;
496 f2fs_hash_t name_hash;
Chao Yu201a05b2014-09-24 18:17:53 +0800497 size_t namelen = name->len;
498 struct f2fs_inline_dentry *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800499 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800500 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700501 struct page *page = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800502 int err = 0;
Chao Yu201a05b2014-09-24 18:17:53 +0800503
504 ipage = get_node_page(sbi, dir->i_ino);
505 if (IS_ERR(ipage))
506 return PTR_ERR(ipage);
507
508 dentry_blk = inline_data_addr(ipage);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700509 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
510 slots, NR_INLINE_DENTRY);
Chao Yu201a05b2014-09-24 18:17:53 +0800511 if (bit_pos >= NR_INLINE_DENTRY) {
512 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
Chao Yu470f00e2015-07-14 18:14:06 +0800513 if (err)
514 return err;
515 err = -EAGAIN;
Chao Yu201a05b2014-09-24 18:17:53 +0800516 goto out;
517 }
518
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700519 if (inode) {
520 down_write(&F2FS_I(inode)->i_sem);
521 page = init_inode_metadata(inode, dir, name, ipage);
522 if (IS_ERR(page)) {
523 err = PTR_ERR(page);
524 goto fail;
525 }
Chao Yu201a05b2014-09-24 18:17:53 +0800526 }
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700527
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800528 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu3b4d7322015-02-16 16:17:20 +0800529
530 name_hash = f2fs_dentry_hash(name);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700531 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700532 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800533
Chao Yu201a05b2014-09-24 18:17:53 +0800534 set_page_dirty(ipage);
535
536 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700537 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700538 f2fs_i_pino_write(inode, dir->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700539 f2fs_put_page(page, 1);
540 }
Chao Yu201a05b2014-09-24 18:17:53 +0800541
542 update_parent_metadata(dir, inode, 0);
543fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700544 if (inode)
545 up_write(&F2FS_I(inode)->i_sem);
Chao Yu201a05b2014-09-24 18:17:53 +0800546out:
547 f2fs_put_page(ipage, 1);
548 return err;
549}
550
551void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
552 struct inode *dir, struct inode *inode)
553{
554 struct f2fs_inline_dentry *inline_dentry;
555 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
556 unsigned int bit_pos;
557 int i;
558
559 lock_page(page);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800560 f2fs_wait_on_page_writeback(page, NODE, true);
Chao Yu201a05b2014-09-24 18:17:53 +0800561
562 inline_dentry = inline_data_addr(page);
563 bit_pos = dentry - inline_dentry->dentry;
564 for (i = 0; i < slots; i++)
565 test_and_clear_bit_le(bit_pos + i,
566 &inline_dentry->dentry_bitmap);
567
568 set_page_dirty(page);
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700569 f2fs_put_page(page, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800570
571 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700572 f2fs_mark_inode_dirty_sync(dir);
Chao Yu201a05b2014-09-24 18:17:53 +0800573
574 if (inode)
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700575 f2fs_drop_nlink(dir, inode);
Chao Yu201a05b2014-09-24 18:17:53 +0800576}
577
578bool f2fs_empty_inline_dir(struct inode *dir)
579{
580 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
581 struct page *ipage;
582 unsigned int bit_pos = 2;
583 struct f2fs_inline_dentry *dentry_blk;
584
585 ipage = get_node_page(sbi, dir->i_ino);
586 if (IS_ERR(ipage))
587 return false;
588
589 dentry_blk = inline_data_addr(ipage);
590 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
591 NR_INLINE_DENTRY,
592 bit_pos);
593
594 f2fs_put_page(ipage, 1);
595
596 if (bit_pos < NR_INLINE_DENTRY)
597 return false;
598
599 return true;
600}
601
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700602int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700603 struct fscrypt_str *fstr)
Chao Yu201a05b2014-09-24 18:17:53 +0800604{
605 struct inode *inode = file_inode(file);
Chao Yu201a05b2014-09-24 18:17:53 +0800606 struct f2fs_inline_dentry *inline_dentry = NULL;
Chao Yu201a05b2014-09-24 18:17:53 +0800607 struct page *ipage = NULL;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700608 struct f2fs_dentry_ptr d;
Chao Yu201a05b2014-09-24 18:17:53 +0800609
610 if (ctx->pos == NR_INLINE_DENTRY)
611 return 0;
612
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700613 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu201a05b2014-09-24 18:17:53 +0800614 if (IS_ERR(ipage))
615 return PTR_ERR(ipage);
616
Chao Yu201a05b2014-09-24 18:17:53 +0800617 inline_dentry = inline_data_addr(ipage);
Chao Yu201a05b2014-09-24 18:17:53 +0800618
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700619 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700620
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700621 if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700622 ctx->pos = NR_INLINE_DENTRY;
Chao Yu201a05b2014-09-24 18:17:53 +0800623
Chao Yu201a05b2014-09-24 18:17:53 +0800624 f2fs_put_page(ipage, 1);
Chao Yu201a05b2014-09-24 18:17:53 +0800625 return 0;
626}
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700627
628int f2fs_inline_data_fiemap(struct inode *inode,
629 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
630{
631 __u64 byteaddr, ilen;
632 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
633 FIEMAP_EXTENT_LAST;
634 struct node_info ni;
635 struct page *ipage;
636 int err = 0;
637
638 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
639 if (IS_ERR(ipage))
640 return PTR_ERR(ipage);
641
642 if (!f2fs_has_inline_data(inode)) {
643 err = -EAGAIN;
644 goto out;
645 }
646
647 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode));
648 if (start >= ilen)
649 goto out;
650 if (start + len < ilen)
651 ilen = start + len;
652 ilen -= start;
653
654 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
655 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
656 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage);
657 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
658out:
659 f2fs_put_page(ipage, 1);
660 return err;
661}