Merge branch 'topic/zxdma' into for-linus
[linux/fpc-iii.git] / fs / f2fs / inline.c
blob38e75fb1e48812b38d477a13a53f0469fb95bbd3
1 /*
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 */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
14 #include "f2fs.h"
16 bool f2fs_may_inline_data(struct inode *inode)
18 if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
19 return false;
21 if (f2fs_is_atomic_file(inode))
22 return false;
24 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
25 return false;
27 if (i_size_read(inode) > MAX_INLINE_DATA)
28 return false;
30 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
31 return false;
33 return true;
36 bool f2fs_may_inline_dentry(struct inode *inode)
38 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
39 return false;
41 if (!S_ISDIR(inode->i_mode))
42 return false;
44 return true;
47 void read_inline_data(struct page *page, struct page *ipage)
49 void *src_addr, *dst_addr;
51 if (PageUptodate(page))
52 return;
54 f2fs_bug_on(F2FS_P_SB(page), page->index);
56 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
58 /* Copy the whole inline data block */
59 src_addr = inline_data_addr(ipage);
60 dst_addr = kmap_atomic(page);
61 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
62 flush_dcache_page(page);
63 kunmap_atomic(dst_addr);
64 SetPageUptodate(page);
67 bool truncate_inline_inode(struct page *ipage, u64 from)
69 void *addr;
71 if (from >= MAX_INLINE_DATA)
72 return false;
74 addr = inline_data_addr(ipage);
76 f2fs_wait_on_page_writeback(ipage, NODE);
77 memset(addr + from, 0, MAX_INLINE_DATA - from);
79 return true;
82 int f2fs_read_inline_data(struct inode *inode, struct page *page)
84 struct page *ipage;
86 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
87 if (IS_ERR(ipage)) {
88 unlock_page(page);
89 return PTR_ERR(ipage);
92 if (!f2fs_has_inline_data(inode)) {
93 f2fs_put_page(ipage, 1);
94 return -EAGAIN;
97 if (page->index)
98 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
99 else
100 read_inline_data(page, ipage);
102 SetPageUptodate(page);
103 f2fs_put_page(ipage, 1);
104 unlock_page(page);
105 return 0;
108 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
110 void *src_addr, *dst_addr;
111 struct f2fs_io_info fio = {
112 .sbi = F2FS_I_SB(dn->inode),
113 .type = DATA,
114 .rw = WRITE_SYNC | REQ_PRIO,
115 .page = page,
116 .encrypted_page = NULL,
118 int dirty, err;
120 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
122 if (!f2fs_exist_data(dn->inode))
123 goto clear_out;
125 err = f2fs_reserve_block(dn, 0);
126 if (err)
127 return err;
129 f2fs_wait_on_page_writeback(page, DATA);
131 if (PageUptodate(page))
132 goto no_update;
134 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
136 /* Copy the whole inline data block */
137 src_addr = inline_data_addr(dn->inode_page);
138 dst_addr = kmap_atomic(page);
139 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
140 flush_dcache_page(page);
141 kunmap_atomic(dst_addr);
142 SetPageUptodate(page);
143 no_update:
144 /* clear dirty state */
145 dirty = clear_page_dirty_for_io(page);
147 /* write data page to try to make data consistent */
148 set_page_writeback(page);
149 fio.blk_addr = dn->data_blkaddr;
150 write_data_page(dn, &fio);
151 set_data_blkaddr(dn);
152 f2fs_update_extent_cache(dn);
153 f2fs_wait_on_page_writeback(page, DATA);
154 if (dirty)
155 inode_dec_dirty_pages(dn->inode);
157 /* this converted inline_data should be recovered. */
158 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
160 /* clear inline data and flag after data writeback */
161 truncate_inline_inode(dn->inode_page, 0);
162 clear_out:
163 stat_dec_inline_inode(dn->inode);
164 f2fs_clear_inline_inode(dn->inode);
165 sync_inode_page(dn);
166 f2fs_put_dnode(dn);
167 return 0;
170 int f2fs_convert_inline_inode(struct inode *inode)
172 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
173 struct dnode_of_data dn;
174 struct page *ipage, *page;
175 int err = 0;
177 page = grab_cache_page(inode->i_mapping, 0);
178 if (!page)
179 return -ENOMEM;
181 f2fs_lock_op(sbi);
183 ipage = get_node_page(sbi, inode->i_ino);
184 if (IS_ERR(ipage)) {
185 err = PTR_ERR(ipage);
186 goto out;
189 set_new_dnode(&dn, inode, ipage, ipage, 0);
191 if (f2fs_has_inline_data(inode))
192 err = f2fs_convert_inline_page(&dn, page);
194 f2fs_put_dnode(&dn);
195 out:
196 f2fs_unlock_op(sbi);
198 f2fs_put_page(page, 1);
199 return err;
202 int f2fs_write_inline_data(struct inode *inode, struct page *page)
204 void *src_addr, *dst_addr;
205 struct dnode_of_data dn;
206 int err;
208 set_new_dnode(&dn, inode, NULL, NULL, 0);
209 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
210 if (err)
211 return err;
213 if (!f2fs_has_inline_data(inode)) {
214 f2fs_put_dnode(&dn);
215 return -EAGAIN;
218 f2fs_bug_on(F2FS_I_SB(inode), page->index);
220 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
221 src_addr = kmap_atomic(page);
222 dst_addr = inline_data_addr(dn.inode_page);
223 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
224 kunmap_atomic(src_addr);
226 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
227 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
229 sync_inode_page(&dn);
230 f2fs_put_dnode(&dn);
231 return 0;
234 bool recover_inline_data(struct inode *inode, struct page *npage)
236 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
237 struct f2fs_inode *ri = NULL;
238 void *src_addr, *dst_addr;
239 struct page *ipage;
242 * The inline_data recovery policy is as follows.
243 * [prev.] [next] of inline_data flag
244 * o o -> recover inline_data
245 * o x -> remove inline_data, and then recover data blocks
246 * x o -> remove inline_data, and then recover inline_data
247 * x x -> recover data blocks
249 if (IS_INODE(npage))
250 ri = F2FS_INODE(npage);
252 if (f2fs_has_inline_data(inode) &&
253 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
254 process_inline:
255 ipage = get_node_page(sbi, inode->i_ino);
256 f2fs_bug_on(sbi, IS_ERR(ipage));
258 f2fs_wait_on_page_writeback(ipage, NODE);
260 src_addr = inline_data_addr(npage);
261 dst_addr = inline_data_addr(ipage);
262 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
264 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
265 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
267 update_inode(inode, ipage);
268 f2fs_put_page(ipage, 1);
269 return true;
272 if (f2fs_has_inline_data(inode)) {
273 ipage = get_node_page(sbi, inode->i_ino);
274 f2fs_bug_on(sbi, IS_ERR(ipage));
275 truncate_inline_inode(ipage, 0);
276 f2fs_clear_inline_inode(inode);
277 update_inode(inode, ipage);
278 f2fs_put_page(ipage, 1);
279 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
280 truncate_blocks(inode, 0, false);
281 goto process_inline;
283 return false;
286 struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
287 struct f2fs_filename *fname, struct page **res_page)
289 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
290 struct f2fs_inline_dentry *inline_dentry;
291 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
292 struct f2fs_dir_entry *de;
293 struct f2fs_dentry_ptr d;
294 struct page *ipage;
295 f2fs_hash_t namehash;
297 ipage = get_node_page(sbi, dir->i_ino);
298 if (IS_ERR(ipage))
299 return NULL;
301 namehash = f2fs_dentry_hash(&name);
303 inline_dentry = inline_data_addr(ipage);
305 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
306 de = find_target_dentry(fname, namehash, NULL, &d);
307 unlock_page(ipage);
308 if (de)
309 *res_page = ipage;
310 else
311 f2fs_put_page(ipage, 0);
314 * For the most part, it should be a bug when name_len is zero.
315 * We stop here for figuring out where the bugs has occurred.
317 f2fs_bug_on(sbi, d.max < 0);
318 return de;
321 struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
322 struct page **p)
324 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
325 struct page *ipage;
326 struct f2fs_dir_entry *de;
327 struct f2fs_inline_dentry *dentry_blk;
329 ipage = get_node_page(sbi, dir->i_ino);
330 if (IS_ERR(ipage))
331 return NULL;
333 dentry_blk = inline_data_addr(ipage);
334 de = &dentry_blk->dentry[1];
335 *p = ipage;
336 unlock_page(ipage);
337 return de;
340 int make_empty_inline_dir(struct inode *inode, struct inode *parent,
341 struct page *ipage)
343 struct f2fs_inline_dentry *dentry_blk;
344 struct f2fs_dentry_ptr d;
346 dentry_blk = inline_data_addr(ipage);
348 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
349 do_make_empty_dir(inode, parent, &d);
351 set_page_dirty(ipage);
353 /* update i_size to MAX_INLINE_DATA */
354 if (i_size_read(inode) < MAX_INLINE_DATA) {
355 i_size_write(inode, MAX_INLINE_DATA);
356 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
358 return 0;
361 static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
362 struct f2fs_inline_dentry *inline_dentry)
364 struct page *page;
365 struct dnode_of_data dn;
366 struct f2fs_dentry_block *dentry_blk;
367 int err;
369 page = grab_cache_page(dir->i_mapping, 0);
370 if (!page)
371 return -ENOMEM;
373 set_new_dnode(&dn, dir, ipage, NULL, 0);
374 err = f2fs_reserve_block(&dn, 0);
375 if (err)
376 goto out;
378 f2fs_wait_on_page_writeback(page, DATA);
379 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
381 dentry_blk = kmap_atomic(page);
383 /* copy data from inline dentry block to new dentry block */
384 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
385 INLINE_DENTRY_BITMAP_SIZE);
386 memcpy(dentry_blk->dentry, inline_dentry->dentry,
387 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
388 memcpy(dentry_blk->filename, inline_dentry->filename,
389 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
391 kunmap_atomic(dentry_blk);
392 SetPageUptodate(page);
393 set_page_dirty(page);
395 /* clear inline dir and flag after data writeback */
396 truncate_inline_inode(ipage, 0);
398 stat_dec_inline_dir(dir);
399 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
401 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
402 i_size_write(dir, PAGE_CACHE_SIZE);
403 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
406 sync_inode_page(&dn);
407 out:
408 f2fs_put_page(page, 1);
409 return err;
412 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
413 struct inode *inode, nid_t ino, umode_t mode)
415 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
416 struct page *ipage;
417 unsigned int bit_pos;
418 f2fs_hash_t name_hash;
419 size_t namelen = name->len;
420 struct f2fs_inline_dentry *dentry_blk = NULL;
421 struct f2fs_dentry_ptr d;
422 int slots = GET_DENTRY_SLOTS(namelen);
423 struct page *page = NULL;
424 int err = 0;
426 ipage = get_node_page(sbi, dir->i_ino);
427 if (IS_ERR(ipage))
428 return PTR_ERR(ipage);
430 dentry_blk = inline_data_addr(ipage);
431 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
432 slots, NR_INLINE_DENTRY);
433 if (bit_pos >= NR_INLINE_DENTRY) {
434 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
435 if (!err)
436 err = -EAGAIN;
437 goto out;
440 if (inode) {
441 down_write(&F2FS_I(inode)->i_sem);
442 page = init_inode_metadata(inode, dir, name, ipage);
443 if (IS_ERR(page)) {
444 err = PTR_ERR(page);
445 goto fail;
449 f2fs_wait_on_page_writeback(ipage, NODE);
451 name_hash = f2fs_dentry_hash(name);
452 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
453 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
455 set_page_dirty(ipage);
457 /* we don't need to mark_inode_dirty now */
458 if (inode) {
459 F2FS_I(inode)->i_pino = dir->i_ino;
460 update_inode(inode, page);
461 f2fs_put_page(page, 1);
464 update_parent_metadata(dir, inode, 0);
465 fail:
466 if (inode)
467 up_write(&F2FS_I(inode)->i_sem);
469 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
470 update_inode(dir, ipage);
471 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
473 out:
474 f2fs_put_page(ipage, 1);
475 return err;
478 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
479 struct inode *dir, struct inode *inode)
481 struct f2fs_inline_dentry *inline_dentry;
482 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
483 unsigned int bit_pos;
484 int i;
486 lock_page(page);
487 f2fs_wait_on_page_writeback(page, NODE);
489 inline_dentry = inline_data_addr(page);
490 bit_pos = dentry - inline_dentry->dentry;
491 for (i = 0; i < slots; i++)
492 test_and_clear_bit_le(bit_pos + i,
493 &inline_dentry->dentry_bitmap);
495 set_page_dirty(page);
497 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
499 if (inode)
500 f2fs_drop_nlink(dir, inode, page);
502 f2fs_put_page(page, 1);
505 bool f2fs_empty_inline_dir(struct inode *dir)
507 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
508 struct page *ipage;
509 unsigned int bit_pos = 2;
510 struct f2fs_inline_dentry *dentry_blk;
512 ipage = get_node_page(sbi, dir->i_ino);
513 if (IS_ERR(ipage))
514 return false;
516 dentry_blk = inline_data_addr(ipage);
517 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
518 NR_INLINE_DENTRY,
519 bit_pos);
521 f2fs_put_page(ipage, 1);
523 if (bit_pos < NR_INLINE_DENTRY)
524 return false;
526 return true;
529 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
530 struct f2fs_str *fstr)
532 struct inode *inode = file_inode(file);
533 struct f2fs_inline_dentry *inline_dentry = NULL;
534 struct page *ipage = NULL;
535 struct f2fs_dentry_ptr d;
537 if (ctx->pos == NR_INLINE_DENTRY)
538 return 0;
540 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
541 if (IS_ERR(ipage))
542 return PTR_ERR(ipage);
544 inline_dentry = inline_data_addr(ipage);
546 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
548 if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
549 ctx->pos = NR_INLINE_DENTRY;
551 f2fs_put_page(ipage, 1);
552 return 0;