drm/qxl: Replace ttm_bo_unref with ttm_bo_put
[linux/fpc-iii.git] / fs / f2fs / inline.c
blob115dc219344b15be987905121b38961b8fdcd630
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"
15 #include "node.h"
17 bool f2fs_may_inline_data(struct inode *inode)
19 if (f2fs_is_atomic_file(inode))
20 return false;
22 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
23 return false;
25 if (i_size_read(inode) > MAX_INLINE_DATA(inode))
26 return false;
28 if (f2fs_post_read_required(inode))
29 return false;
31 return true;
34 bool f2fs_may_inline_dentry(struct inode *inode)
36 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
37 return false;
39 if (!S_ISDIR(inode->i_mode))
40 return false;
42 return true;
45 void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
47 struct inode *inode = page->mapping->host;
48 void *src_addr, *dst_addr;
50 if (PageUptodate(page))
51 return;
53 f2fs_bug_on(F2FS_P_SB(page), page->index);
55 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
57 /* Copy the whole inline data block */
58 src_addr = inline_data_addr(inode, ipage);
59 dst_addr = kmap_atomic(page);
60 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
61 flush_dcache_page(page);
62 kunmap_atomic(dst_addr);
63 if (!PageUptodate(page))
64 SetPageUptodate(page);
67 void f2fs_truncate_inline_inode(struct inode *inode,
68 struct page *ipage, u64 from)
70 void *addr;
72 if (from >= MAX_INLINE_DATA(inode))
73 return;
75 addr = inline_data_addr(inode, ipage);
77 f2fs_wait_on_page_writeback(ipage, NODE, true);
78 memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
79 set_page_dirty(ipage);
81 if (from == 0)
82 clear_inode_flag(inode, FI_DATA_EXIST);
85 int f2fs_read_inline_data(struct inode *inode, struct page *page)
87 struct page *ipage;
89 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
90 if (IS_ERR(ipage)) {
91 unlock_page(page);
92 return PTR_ERR(ipage);
95 if (!f2fs_has_inline_data(inode)) {
96 f2fs_put_page(ipage, 1);
97 return -EAGAIN;
100 if (page->index)
101 zero_user_segment(page, 0, PAGE_SIZE);
102 else
103 f2fs_do_read_inline_data(page, ipage);
105 if (!PageUptodate(page))
106 SetPageUptodate(page);
107 f2fs_put_page(ipage, 1);
108 unlock_page(page);
109 return 0;
112 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
114 struct f2fs_io_info fio = {
115 .sbi = F2FS_I_SB(dn->inode),
116 .ino = dn->inode->i_ino,
117 .type = DATA,
118 .op = REQ_OP_WRITE,
119 .op_flags = REQ_SYNC | REQ_PRIO,
120 .page = page,
121 .encrypted_page = NULL,
122 .io_type = FS_DATA_IO,
124 struct node_info ni;
125 int dirty, err;
127 if (!f2fs_exist_data(dn->inode))
128 goto clear_out;
130 err = f2fs_reserve_block(dn, 0);
131 if (err)
132 return err;
134 err = f2fs_get_node_info(fio.sbi, dn->nid, &ni);
135 if (err) {
136 f2fs_put_dnode(dn);
137 return err;
140 fio.version = ni.version;
142 if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
143 f2fs_put_dnode(dn);
144 set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
145 f2fs_msg(fio.sbi->sb, KERN_WARNING,
146 "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, "
147 "run fsck to fix.",
148 __func__, dn->inode->i_ino, dn->data_blkaddr);
149 return -EINVAL;
152 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
154 f2fs_do_read_inline_data(page, dn->inode_page);
155 set_page_dirty(page);
157 /* clear dirty state */
158 dirty = clear_page_dirty_for_io(page);
160 /* write data page to try to make data consistent */
161 set_page_writeback(page);
162 ClearPageError(page);
163 fio.old_blkaddr = dn->data_blkaddr;
164 set_inode_flag(dn->inode, FI_HOT_DATA);
165 f2fs_outplace_write_data(dn, &fio);
166 f2fs_wait_on_page_writeback(page, DATA, true);
167 if (dirty) {
168 inode_dec_dirty_pages(dn->inode);
169 f2fs_remove_dirty_inode(dn->inode);
172 /* this converted inline_data should be recovered. */
173 set_inode_flag(dn->inode, FI_APPEND_WRITE);
175 /* clear inline data and flag after data writeback */
176 f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
177 clear_inline_node(dn->inode_page);
178 clear_out:
179 stat_dec_inline_inode(dn->inode);
180 clear_inode_flag(dn->inode, FI_INLINE_DATA);
181 f2fs_put_dnode(dn);
182 return 0;
185 int f2fs_convert_inline_inode(struct inode *inode)
187 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
188 struct dnode_of_data dn;
189 struct page *ipage, *page;
190 int err = 0;
192 if (!f2fs_has_inline_data(inode))
193 return 0;
195 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
196 if (!page)
197 return -ENOMEM;
199 f2fs_lock_op(sbi);
201 ipage = f2fs_get_node_page(sbi, inode->i_ino);
202 if (IS_ERR(ipage)) {
203 err = PTR_ERR(ipage);
204 goto out;
207 set_new_dnode(&dn, inode, ipage, ipage, 0);
209 if (f2fs_has_inline_data(inode))
210 err = f2fs_convert_inline_page(&dn, page);
212 f2fs_put_dnode(&dn);
213 out:
214 f2fs_unlock_op(sbi);
216 f2fs_put_page(page, 1);
218 f2fs_balance_fs(sbi, dn.node_changed);
220 return err;
223 int f2fs_write_inline_data(struct inode *inode, struct page *page)
225 void *src_addr, *dst_addr;
226 struct dnode_of_data dn;
227 int err;
229 set_new_dnode(&dn, inode, NULL, NULL, 0);
230 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
231 if (err)
232 return err;
234 if (!f2fs_has_inline_data(inode)) {
235 f2fs_put_dnode(&dn);
236 return -EAGAIN;
239 f2fs_bug_on(F2FS_I_SB(inode), page->index);
241 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true);
242 src_addr = kmap_atomic(page);
243 dst_addr = inline_data_addr(inode, dn.inode_page);
244 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
245 kunmap_atomic(src_addr);
246 set_page_dirty(dn.inode_page);
248 f2fs_clear_radix_tree_dirty_tag(page);
250 set_inode_flag(inode, FI_APPEND_WRITE);
251 set_inode_flag(inode, FI_DATA_EXIST);
253 clear_inline_node(dn.inode_page);
254 f2fs_put_dnode(&dn);
255 return 0;
258 bool f2fs_recover_inline_data(struct inode *inode, struct page *npage)
260 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
261 struct f2fs_inode *ri = NULL;
262 void *src_addr, *dst_addr;
263 struct page *ipage;
266 * The inline_data recovery policy is as follows.
267 * [prev.] [next] of inline_data flag
268 * o o -> recover inline_data
269 * o x -> remove inline_data, and then recover data blocks
270 * x o -> remove inline_data, and then recover inline_data
271 * x x -> recover data blocks
273 if (IS_INODE(npage))
274 ri = F2FS_INODE(npage);
276 if (f2fs_has_inline_data(inode) &&
277 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
278 process_inline:
279 ipage = f2fs_get_node_page(sbi, inode->i_ino);
280 f2fs_bug_on(sbi, IS_ERR(ipage));
282 f2fs_wait_on_page_writeback(ipage, NODE, true);
284 src_addr = inline_data_addr(inode, npage);
285 dst_addr = inline_data_addr(inode, ipage);
286 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
288 set_inode_flag(inode, FI_INLINE_DATA);
289 set_inode_flag(inode, FI_DATA_EXIST);
291 set_page_dirty(ipage);
292 f2fs_put_page(ipage, 1);
293 return true;
296 if (f2fs_has_inline_data(inode)) {
297 ipage = f2fs_get_node_page(sbi, inode->i_ino);
298 f2fs_bug_on(sbi, IS_ERR(ipage));
299 f2fs_truncate_inline_inode(inode, ipage, 0);
300 clear_inode_flag(inode, FI_INLINE_DATA);
301 f2fs_put_page(ipage, 1);
302 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
303 if (f2fs_truncate_blocks(inode, 0, false))
304 return false;
305 goto process_inline;
307 return false;
310 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
311 struct fscrypt_name *fname, struct page **res_page)
313 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
314 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
315 struct f2fs_dir_entry *de;
316 struct f2fs_dentry_ptr d;
317 struct page *ipage;
318 void *inline_dentry;
319 f2fs_hash_t namehash;
321 ipage = f2fs_get_node_page(sbi, dir->i_ino);
322 if (IS_ERR(ipage)) {
323 *res_page = ipage;
324 return NULL;
327 namehash = f2fs_dentry_hash(&name, fname);
329 inline_dentry = inline_data_addr(dir, ipage);
331 make_dentry_ptr_inline(dir, &d, inline_dentry);
332 de = f2fs_find_target_dentry(fname, namehash, NULL, &d);
333 unlock_page(ipage);
334 if (de)
335 *res_page = ipage;
336 else
337 f2fs_put_page(ipage, 0);
339 return de;
342 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
343 struct page *ipage)
345 struct f2fs_dentry_ptr d;
346 void *inline_dentry;
348 inline_dentry = inline_data_addr(inode, ipage);
350 make_dentry_ptr_inline(inode, &d, inline_dentry);
351 f2fs_do_make_empty_dir(inode, parent, &d);
353 set_page_dirty(ipage);
355 /* update i_size to MAX_INLINE_DATA */
356 if (i_size_read(inode) < MAX_INLINE_DATA(inode))
357 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
358 return 0;
362 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
363 * release ipage in this function.
365 static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
366 void *inline_dentry)
368 struct page *page;
369 struct dnode_of_data dn;
370 struct f2fs_dentry_block *dentry_blk;
371 struct f2fs_dentry_ptr src, dst;
372 int err;
374 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
375 if (!page) {
376 f2fs_put_page(ipage, 1);
377 return -ENOMEM;
380 set_new_dnode(&dn, dir, ipage, NULL, 0);
381 err = f2fs_reserve_block(&dn, 0);
382 if (err)
383 goto out;
385 if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
386 f2fs_put_dnode(&dn);
387 set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
388 f2fs_msg(F2FS_P_SB(page)->sb, KERN_WARNING,
389 "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, "
390 "run fsck to fix.",
391 __func__, dir->i_ino, dn.data_blkaddr);
392 err = -EINVAL;
393 goto out;
396 f2fs_wait_on_page_writeback(page, DATA, true);
398 dentry_blk = page_address(page);
400 make_dentry_ptr_inline(dir, &src, inline_dentry);
401 make_dentry_ptr_block(dir, &dst, dentry_blk);
403 /* copy data from inline dentry block to new dentry block */
404 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
405 memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap);
407 * we do not need to zero out remainder part of dentry and filename
408 * field, since we have used bitmap for marking the usage status of
409 * them, besides, we can also ignore copying/zeroing reserved space
410 * of dentry block, because them haven't been used so far.
412 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
413 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
415 if (!PageUptodate(page))
416 SetPageUptodate(page);
417 set_page_dirty(page);
419 /* clear inline dir and flag after data writeback */
420 f2fs_truncate_inline_inode(dir, ipage, 0);
422 stat_dec_inline_dir(dir);
423 clear_inode_flag(dir, FI_INLINE_DENTRY);
425 f2fs_i_depth_write(dir, 1);
426 if (i_size_read(dir) < PAGE_SIZE)
427 f2fs_i_size_write(dir, PAGE_SIZE);
428 out:
429 f2fs_put_page(page, 1);
430 return err;
433 static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
435 struct f2fs_dentry_ptr d;
436 unsigned long bit_pos = 0;
437 int err = 0;
439 make_dentry_ptr_inline(dir, &d, inline_dentry);
441 while (bit_pos < d.max) {
442 struct f2fs_dir_entry *de;
443 struct qstr new_name;
444 nid_t ino;
445 umode_t fake_mode;
447 if (!test_bit_le(bit_pos, d.bitmap)) {
448 bit_pos++;
449 continue;
452 de = &d.dentry[bit_pos];
454 if (unlikely(!de->name_len)) {
455 bit_pos++;
456 continue;
459 new_name.name = d.filename[bit_pos];
460 new_name.len = le16_to_cpu(de->name_len);
462 ino = le32_to_cpu(de->ino);
463 fake_mode = f2fs_get_de_type(de) << S_SHIFT;
465 err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL,
466 ino, fake_mode);
467 if (err)
468 goto punch_dentry_pages;
470 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
472 return 0;
473 punch_dentry_pages:
474 truncate_inode_pages(&dir->i_data, 0);
475 f2fs_truncate_blocks(dir, 0, false);
476 f2fs_remove_dirty_inode(dir);
477 return err;
480 static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
481 void *inline_dentry)
483 void *backup_dentry;
484 int err;
486 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
487 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
488 if (!backup_dentry) {
489 f2fs_put_page(ipage, 1);
490 return -ENOMEM;
493 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
494 f2fs_truncate_inline_inode(dir, ipage, 0);
496 unlock_page(ipage);
498 err = f2fs_add_inline_entries(dir, backup_dentry);
499 if (err)
500 goto recover;
502 lock_page(ipage);
504 stat_dec_inline_dir(dir);
505 clear_inode_flag(dir, FI_INLINE_DENTRY);
506 kfree(backup_dentry);
507 return 0;
508 recover:
509 lock_page(ipage);
510 f2fs_wait_on_page_writeback(ipage, NODE, true);
511 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
512 f2fs_i_depth_write(dir, 0);
513 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
514 set_page_dirty(ipage);
515 f2fs_put_page(ipage, 1);
517 kfree(backup_dentry);
518 return err;
521 static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
522 void *inline_dentry)
524 if (!F2FS_I(dir)->i_dir_level)
525 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
526 else
527 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
530 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
531 const struct qstr *orig_name,
532 struct inode *inode, nid_t ino, umode_t mode)
534 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
535 struct page *ipage;
536 unsigned int bit_pos;
537 f2fs_hash_t name_hash;
538 void *inline_dentry = NULL;
539 struct f2fs_dentry_ptr d;
540 int slots = GET_DENTRY_SLOTS(new_name->len);
541 struct page *page = NULL;
542 int err = 0;
544 ipage = f2fs_get_node_page(sbi, dir->i_ino);
545 if (IS_ERR(ipage))
546 return PTR_ERR(ipage);
548 inline_dentry = inline_data_addr(dir, ipage);
549 make_dentry_ptr_inline(dir, &d, inline_dentry);
551 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
552 if (bit_pos >= d.max) {
553 err = f2fs_convert_inline_dir(dir, ipage, inline_dentry);
554 if (err)
555 return err;
556 err = -EAGAIN;
557 goto out;
560 if (inode) {
561 down_write(&F2FS_I(inode)->i_sem);
562 page = f2fs_init_inode_metadata(inode, dir, new_name,
563 orig_name, ipage);
564 if (IS_ERR(page)) {
565 err = PTR_ERR(page);
566 goto fail;
570 f2fs_wait_on_page_writeback(ipage, NODE, true);
572 name_hash = f2fs_dentry_hash(new_name, NULL);
573 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
575 set_page_dirty(ipage);
577 /* we don't need to mark_inode_dirty now */
578 if (inode) {
579 f2fs_i_pino_write(inode, dir->i_ino);
580 f2fs_put_page(page, 1);
583 f2fs_update_parent_metadata(dir, inode, 0);
584 fail:
585 if (inode)
586 up_write(&F2FS_I(inode)->i_sem);
587 out:
588 f2fs_put_page(ipage, 1);
589 return err;
592 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
593 struct inode *dir, struct inode *inode)
595 struct f2fs_dentry_ptr d;
596 void *inline_dentry;
597 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
598 unsigned int bit_pos;
599 int i;
601 lock_page(page);
602 f2fs_wait_on_page_writeback(page, NODE, true);
604 inline_dentry = inline_data_addr(dir, page);
605 make_dentry_ptr_inline(dir, &d, inline_dentry);
607 bit_pos = dentry - d.dentry;
608 for (i = 0; i < slots; i++)
609 __clear_bit_le(bit_pos + i, d.bitmap);
611 set_page_dirty(page);
612 f2fs_put_page(page, 1);
614 dir->i_ctime = dir->i_mtime = current_time(dir);
615 f2fs_mark_inode_dirty_sync(dir, false);
617 if (inode)
618 f2fs_drop_nlink(dir, inode);
621 bool f2fs_empty_inline_dir(struct inode *dir)
623 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
624 struct page *ipage;
625 unsigned int bit_pos = 2;
626 void *inline_dentry;
627 struct f2fs_dentry_ptr d;
629 ipage = f2fs_get_node_page(sbi, dir->i_ino);
630 if (IS_ERR(ipage))
631 return false;
633 inline_dentry = inline_data_addr(dir, ipage);
634 make_dentry_ptr_inline(dir, &d, inline_dentry);
636 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
638 f2fs_put_page(ipage, 1);
640 if (bit_pos < d.max)
641 return false;
643 return true;
646 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
647 struct fscrypt_str *fstr)
649 struct inode *inode = file_inode(file);
650 struct page *ipage = NULL;
651 struct f2fs_dentry_ptr d;
652 void *inline_dentry = NULL;
653 int err;
655 make_dentry_ptr_inline(inode, &d, inline_dentry);
657 if (ctx->pos == d.max)
658 return 0;
660 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
661 if (IS_ERR(ipage))
662 return PTR_ERR(ipage);
664 inline_dentry = inline_data_addr(inode, ipage);
666 make_dentry_ptr_inline(inode, &d, inline_dentry);
668 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
669 if (!err)
670 ctx->pos = d.max;
672 f2fs_put_page(ipage, 1);
673 return err < 0 ? err : 0;
676 int f2fs_inline_data_fiemap(struct inode *inode,
677 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
679 __u64 byteaddr, ilen;
680 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
681 FIEMAP_EXTENT_LAST;
682 struct node_info ni;
683 struct page *ipage;
684 int err = 0;
686 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
687 if (IS_ERR(ipage))
688 return PTR_ERR(ipage);
690 if (!f2fs_has_inline_data(inode)) {
691 err = -EAGAIN;
692 goto out;
695 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
696 if (start >= ilen)
697 goto out;
698 if (start + len < ilen)
699 ilen = start + len;
700 ilen -= start;
702 err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
703 if (err)
704 goto out;
706 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
707 byteaddr += (char *)inline_data_addr(inode, ipage) -
708 (char *)F2FS_INODE(ipage);
709 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
710 out:
711 f2fs_put_page(ipage, 1);
712 return err;