staging: rtl8723bs: os_dep: change return type of rtw_suspend_ap_wow
[linux/fpc-iii.git] / fs / f2fs / inline.c
blob3613efca8c00ca3d43a157aebea86152c1de3706
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/inline.c
4 * Copyright (c) 2013, Intel Corporation
5 * Authors: Huajun Li <huajun.li@intel.com>
6 * Haicheng Li <haicheng.li@intel.com>
7 */
9 #include <linux/fs.h>
10 #include <linux/f2fs_fs.h>
12 #include "f2fs.h"
13 #include "node.h"
15 bool f2fs_may_inline_data(struct inode *inode)
17 if (f2fs_is_atomic_file(inode))
18 return false;
20 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
21 return false;
23 if (i_size_read(inode) > MAX_INLINE_DATA(inode))
24 return false;
26 if (f2fs_post_read_required(inode))
27 return false;
29 return true;
32 bool f2fs_may_inline_dentry(struct inode *inode)
34 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
35 return false;
37 if (!S_ISDIR(inode->i_mode))
38 return false;
40 return true;
43 void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
45 struct inode *inode = page->mapping->host;
46 void *src_addr, *dst_addr;
48 if (PageUptodate(page))
49 return;
51 f2fs_bug_on(F2FS_P_SB(page), page->index);
53 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
55 /* Copy the whole inline data block */
56 src_addr = inline_data_addr(inode, ipage);
57 dst_addr = kmap_atomic(page);
58 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
59 flush_dcache_page(page);
60 kunmap_atomic(dst_addr);
61 if (!PageUptodate(page))
62 SetPageUptodate(page);
65 void f2fs_truncate_inline_inode(struct inode *inode,
66 struct page *ipage, u64 from)
68 void *addr;
70 if (from >= MAX_INLINE_DATA(inode))
71 return;
73 addr = inline_data_addr(inode, ipage);
75 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
76 memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
77 set_page_dirty(ipage);
79 if (from == 0)
80 clear_inode_flag(inode, FI_DATA_EXIST);
83 int f2fs_read_inline_data(struct inode *inode, struct page *page)
85 struct page *ipage;
87 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
88 if (IS_ERR(ipage)) {
89 unlock_page(page);
90 return PTR_ERR(ipage);
93 if (!f2fs_has_inline_data(inode)) {
94 f2fs_put_page(ipage, 1);
95 return -EAGAIN;
98 if (page->index)
99 zero_user_segment(page, 0, PAGE_SIZE);
100 else
101 f2fs_do_read_inline_data(page, ipage);
103 if (!PageUptodate(page))
104 SetPageUptodate(page);
105 f2fs_put_page(ipage, 1);
106 unlock_page(page);
107 return 0;
110 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
112 struct f2fs_io_info fio = {
113 .sbi = F2FS_I_SB(dn->inode),
114 .ino = dn->inode->i_ino,
115 .type = DATA,
116 .op = REQ_OP_WRITE,
117 .op_flags = REQ_SYNC | REQ_PRIO,
118 .page = page,
119 .encrypted_page = NULL,
120 .io_type = FS_DATA_IO,
122 struct node_info ni;
123 int dirty, err;
125 if (!f2fs_exist_data(dn->inode))
126 goto clear_out;
128 err = f2fs_reserve_block(dn, 0);
129 if (err)
130 return err;
132 err = f2fs_get_node_info(fio.sbi, dn->nid, &ni);
133 if (err) {
134 f2fs_put_dnode(dn);
135 return err;
138 fio.version = ni.version;
140 if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
141 f2fs_put_dnode(dn);
142 set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
143 f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
144 __func__, dn->inode->i_ino, dn->data_blkaddr);
145 return -EFSCORRUPTED;
148 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
150 f2fs_do_read_inline_data(page, dn->inode_page);
151 set_page_dirty(page);
153 /* clear dirty state */
154 dirty = clear_page_dirty_for_io(page);
156 /* write data page to try to make data consistent */
157 set_page_writeback(page);
158 ClearPageError(page);
159 fio.old_blkaddr = dn->data_blkaddr;
160 set_inode_flag(dn->inode, FI_HOT_DATA);
161 f2fs_outplace_write_data(dn, &fio);
162 f2fs_wait_on_page_writeback(page, DATA, true, true);
163 if (dirty) {
164 inode_dec_dirty_pages(dn->inode);
165 f2fs_remove_dirty_inode(dn->inode);
168 /* this converted inline_data should be recovered. */
169 set_inode_flag(dn->inode, FI_APPEND_WRITE);
171 /* clear inline data and flag after data writeback */
172 f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
173 clear_inline_node(dn->inode_page);
174 clear_out:
175 stat_dec_inline_inode(dn->inode);
176 clear_inode_flag(dn->inode, FI_INLINE_DATA);
177 f2fs_put_dnode(dn);
178 return 0;
181 int f2fs_convert_inline_inode(struct inode *inode)
183 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
184 struct dnode_of_data dn;
185 struct page *ipage, *page;
186 int err = 0;
188 if (!f2fs_has_inline_data(inode))
189 return 0;
191 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
192 if (!page)
193 return -ENOMEM;
195 f2fs_lock_op(sbi);
197 ipage = f2fs_get_node_page(sbi, inode->i_ino);
198 if (IS_ERR(ipage)) {
199 err = PTR_ERR(ipage);
200 goto out;
203 set_new_dnode(&dn, inode, ipage, ipage, 0);
205 if (f2fs_has_inline_data(inode))
206 err = f2fs_convert_inline_page(&dn, page);
208 f2fs_put_dnode(&dn);
209 out:
210 f2fs_unlock_op(sbi);
212 f2fs_put_page(page, 1);
214 f2fs_balance_fs(sbi, dn.node_changed);
216 return err;
219 int f2fs_write_inline_data(struct inode *inode, struct page *page)
221 void *src_addr, *dst_addr;
222 struct dnode_of_data dn;
223 int err;
225 set_new_dnode(&dn, inode, NULL, NULL, 0);
226 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
227 if (err)
228 return err;
230 if (!f2fs_has_inline_data(inode)) {
231 f2fs_put_dnode(&dn);
232 return -EAGAIN;
235 f2fs_bug_on(F2FS_I_SB(inode), page->index);
237 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true);
238 src_addr = kmap_atomic(page);
239 dst_addr = inline_data_addr(inode, dn.inode_page);
240 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
241 kunmap_atomic(src_addr);
242 set_page_dirty(dn.inode_page);
244 f2fs_clear_page_cache_dirty_tag(page);
246 set_inode_flag(inode, FI_APPEND_WRITE);
247 set_inode_flag(inode, FI_DATA_EXIST);
249 clear_inline_node(dn.inode_page);
250 f2fs_put_dnode(&dn);
251 return 0;
254 bool f2fs_recover_inline_data(struct inode *inode, struct page *npage)
256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
257 struct f2fs_inode *ri = NULL;
258 void *src_addr, *dst_addr;
259 struct page *ipage;
262 * The inline_data recovery policy is as follows.
263 * [prev.] [next] of inline_data flag
264 * o o -> recover inline_data
265 * o x -> remove inline_data, and then recover data blocks
266 * x o -> remove inline_data, and then recover inline_data
267 * x x -> recover data blocks
269 if (IS_INODE(npage))
270 ri = F2FS_INODE(npage);
272 if (f2fs_has_inline_data(inode) &&
273 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
274 process_inline:
275 ipage = f2fs_get_node_page(sbi, inode->i_ino);
276 f2fs_bug_on(sbi, IS_ERR(ipage));
278 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
280 src_addr = inline_data_addr(inode, npage);
281 dst_addr = inline_data_addr(inode, ipage);
282 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
284 set_inode_flag(inode, FI_INLINE_DATA);
285 set_inode_flag(inode, FI_DATA_EXIST);
287 set_page_dirty(ipage);
288 f2fs_put_page(ipage, 1);
289 return true;
292 if (f2fs_has_inline_data(inode)) {
293 ipage = f2fs_get_node_page(sbi, inode->i_ino);
294 f2fs_bug_on(sbi, IS_ERR(ipage));
295 f2fs_truncate_inline_inode(inode, ipage, 0);
296 clear_inode_flag(inode, FI_INLINE_DATA);
297 f2fs_put_page(ipage, 1);
298 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
299 if (f2fs_truncate_blocks(inode, 0, false))
300 return false;
301 goto process_inline;
303 return false;
306 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
307 struct fscrypt_name *fname, struct page **res_page)
309 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
310 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
311 struct f2fs_dir_entry *de;
312 struct f2fs_dentry_ptr d;
313 struct page *ipage;
314 void *inline_dentry;
315 f2fs_hash_t namehash;
317 ipage = f2fs_get_node_page(sbi, dir->i_ino);
318 if (IS_ERR(ipage)) {
319 *res_page = ipage;
320 return NULL;
323 namehash = f2fs_dentry_hash(&name, fname);
325 inline_dentry = inline_data_addr(dir, ipage);
327 make_dentry_ptr_inline(dir, &d, inline_dentry);
328 de = f2fs_find_target_dentry(fname, namehash, NULL, &d);
329 unlock_page(ipage);
330 if (de)
331 *res_page = ipage;
332 else
333 f2fs_put_page(ipage, 0);
335 return de;
338 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
339 struct page *ipage)
341 struct f2fs_dentry_ptr d;
342 void *inline_dentry;
344 inline_dentry = inline_data_addr(inode, ipage);
346 make_dentry_ptr_inline(inode, &d, inline_dentry);
347 f2fs_do_make_empty_dir(inode, parent, &d);
349 set_page_dirty(ipage);
351 /* update i_size to MAX_INLINE_DATA */
352 if (i_size_read(inode) < MAX_INLINE_DATA(inode))
353 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
354 return 0;
358 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
359 * release ipage in this function.
361 static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
362 void *inline_dentry)
364 struct page *page;
365 struct dnode_of_data dn;
366 struct f2fs_dentry_block *dentry_blk;
367 struct f2fs_dentry_ptr src, dst;
368 int err;
370 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
371 if (!page) {
372 f2fs_put_page(ipage, 1);
373 return -ENOMEM;
376 set_new_dnode(&dn, dir, ipage, NULL, 0);
377 err = f2fs_reserve_block(&dn, 0);
378 if (err)
379 goto out;
381 if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
382 f2fs_put_dnode(&dn);
383 set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
384 f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
385 __func__, dir->i_ino, dn.data_blkaddr);
386 err = -EFSCORRUPTED;
387 goto out;
390 f2fs_wait_on_page_writeback(page, DATA, true, true);
392 dentry_blk = page_address(page);
394 make_dentry_ptr_inline(dir, &src, inline_dentry);
395 make_dentry_ptr_block(dir, &dst, dentry_blk);
397 /* copy data from inline dentry block to new dentry block */
398 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
399 memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap);
401 * we do not need to zero out remainder part of dentry and filename
402 * field, since we have used bitmap for marking the usage status of
403 * them, besides, we can also ignore copying/zeroing reserved space
404 * of dentry block, because them haven't been used so far.
406 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
407 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
409 if (!PageUptodate(page))
410 SetPageUptodate(page);
411 set_page_dirty(page);
413 /* clear inline dir and flag after data writeback */
414 f2fs_truncate_inline_inode(dir, ipage, 0);
416 stat_dec_inline_dir(dir);
417 clear_inode_flag(dir, FI_INLINE_DENTRY);
420 * should retrieve reserved space which was used to keep
421 * inline_dentry's structure for backward compatibility.
423 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
424 !f2fs_has_inline_xattr(dir))
425 F2FS_I(dir)->i_inline_xattr_size = 0;
427 f2fs_i_depth_write(dir, 1);
428 if (i_size_read(dir) < PAGE_SIZE)
429 f2fs_i_size_write(dir, PAGE_SIZE);
430 out:
431 f2fs_put_page(page, 1);
432 return err;
435 static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
437 struct f2fs_dentry_ptr d;
438 unsigned long bit_pos = 0;
439 int err = 0;
441 make_dentry_ptr_inline(dir, &d, inline_dentry);
443 while (bit_pos < d.max) {
444 struct f2fs_dir_entry *de;
445 struct qstr new_name;
446 nid_t ino;
447 umode_t fake_mode;
449 if (!test_bit_le(bit_pos, d.bitmap)) {
450 bit_pos++;
451 continue;
454 de = &d.dentry[bit_pos];
456 if (unlikely(!de->name_len)) {
457 bit_pos++;
458 continue;
461 new_name.name = d.filename[bit_pos];
462 new_name.len = le16_to_cpu(de->name_len);
464 ino = le32_to_cpu(de->ino);
465 fake_mode = f2fs_get_de_type(de) << S_SHIFT;
467 err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL,
468 ino, fake_mode);
469 if (err)
470 goto punch_dentry_pages;
472 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
474 return 0;
475 punch_dentry_pages:
476 truncate_inode_pages(&dir->i_data, 0);
477 f2fs_truncate_blocks(dir, 0, false);
478 f2fs_remove_dirty_inode(dir);
479 return err;
482 static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
483 void *inline_dentry)
485 void *backup_dentry;
486 int err;
488 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
489 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
490 if (!backup_dentry) {
491 f2fs_put_page(ipage, 1);
492 return -ENOMEM;
495 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
496 f2fs_truncate_inline_inode(dir, ipage, 0);
498 unlock_page(ipage);
500 err = f2fs_add_inline_entries(dir, backup_dentry);
501 if (err)
502 goto recover;
504 lock_page(ipage);
506 stat_dec_inline_dir(dir);
507 clear_inode_flag(dir, FI_INLINE_DENTRY);
510 * should retrieve reserved space which was used to keep
511 * inline_dentry's structure for backward compatibility.
513 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
514 !f2fs_has_inline_xattr(dir))
515 F2FS_I(dir)->i_inline_xattr_size = 0;
517 kvfree(backup_dentry);
518 return 0;
519 recover:
520 lock_page(ipage);
521 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
522 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
523 f2fs_i_depth_write(dir, 0);
524 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
525 set_page_dirty(ipage);
526 f2fs_put_page(ipage, 1);
528 kvfree(backup_dentry);
529 return err;
532 static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
533 void *inline_dentry)
535 if (!F2FS_I(dir)->i_dir_level)
536 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
537 else
538 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
541 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
542 const struct qstr *orig_name,
543 struct inode *inode, nid_t ino, umode_t mode)
545 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
546 struct page *ipage;
547 unsigned int bit_pos;
548 f2fs_hash_t name_hash;
549 void *inline_dentry = NULL;
550 struct f2fs_dentry_ptr d;
551 int slots = GET_DENTRY_SLOTS(new_name->len);
552 struct page *page = NULL;
553 int err = 0;
555 ipage = f2fs_get_node_page(sbi, dir->i_ino);
556 if (IS_ERR(ipage))
557 return PTR_ERR(ipage);
559 inline_dentry = inline_data_addr(dir, ipage);
560 make_dentry_ptr_inline(dir, &d, inline_dentry);
562 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
563 if (bit_pos >= d.max) {
564 err = f2fs_convert_inline_dir(dir, ipage, inline_dentry);
565 if (err)
566 return err;
567 err = -EAGAIN;
568 goto out;
571 if (inode) {
572 down_write(&F2FS_I(inode)->i_sem);
573 page = f2fs_init_inode_metadata(inode, dir, new_name,
574 orig_name, ipage);
575 if (IS_ERR(page)) {
576 err = PTR_ERR(page);
577 goto fail;
581 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
583 name_hash = f2fs_dentry_hash(new_name, NULL);
584 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
586 set_page_dirty(ipage);
588 /* we don't need to mark_inode_dirty now */
589 if (inode) {
590 f2fs_i_pino_write(inode, dir->i_ino);
591 f2fs_put_page(page, 1);
594 f2fs_update_parent_metadata(dir, inode, 0);
595 fail:
596 if (inode)
597 up_write(&F2FS_I(inode)->i_sem);
598 out:
599 f2fs_put_page(ipage, 1);
600 return err;
603 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
604 struct inode *dir, struct inode *inode)
606 struct f2fs_dentry_ptr d;
607 void *inline_dentry;
608 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
609 unsigned int bit_pos;
610 int i;
612 lock_page(page);
613 f2fs_wait_on_page_writeback(page, NODE, true, true);
615 inline_dentry = inline_data_addr(dir, page);
616 make_dentry_ptr_inline(dir, &d, inline_dentry);
618 bit_pos = dentry - d.dentry;
619 for (i = 0; i < slots; i++)
620 __clear_bit_le(bit_pos + i, d.bitmap);
622 set_page_dirty(page);
623 f2fs_put_page(page, 1);
625 dir->i_ctime = dir->i_mtime = current_time(dir);
626 f2fs_mark_inode_dirty_sync(dir, false);
628 if (inode)
629 f2fs_drop_nlink(dir, inode);
632 bool f2fs_empty_inline_dir(struct inode *dir)
634 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
635 struct page *ipage;
636 unsigned int bit_pos = 2;
637 void *inline_dentry;
638 struct f2fs_dentry_ptr d;
640 ipage = f2fs_get_node_page(sbi, dir->i_ino);
641 if (IS_ERR(ipage))
642 return false;
644 inline_dentry = inline_data_addr(dir, ipage);
645 make_dentry_ptr_inline(dir, &d, inline_dentry);
647 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
649 f2fs_put_page(ipage, 1);
651 if (bit_pos < d.max)
652 return false;
654 return true;
657 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
658 struct fscrypt_str *fstr)
660 struct inode *inode = file_inode(file);
661 struct page *ipage = NULL;
662 struct f2fs_dentry_ptr d;
663 void *inline_dentry = NULL;
664 int err;
666 make_dentry_ptr_inline(inode, &d, inline_dentry);
668 if (ctx->pos == d.max)
669 return 0;
671 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
672 if (IS_ERR(ipage))
673 return PTR_ERR(ipage);
676 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
677 * ipage without page's lock held.
679 unlock_page(ipage);
681 inline_dentry = inline_data_addr(inode, ipage);
683 make_dentry_ptr_inline(inode, &d, inline_dentry);
685 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
686 if (!err)
687 ctx->pos = d.max;
689 f2fs_put_page(ipage, 0);
690 return err < 0 ? err : 0;
693 int f2fs_inline_data_fiemap(struct inode *inode,
694 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
696 __u64 byteaddr, ilen;
697 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
698 FIEMAP_EXTENT_LAST;
699 struct node_info ni;
700 struct page *ipage;
701 int err = 0;
703 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
704 if (IS_ERR(ipage))
705 return PTR_ERR(ipage);
707 if (!f2fs_has_inline_data(inode)) {
708 err = -EAGAIN;
709 goto out;
712 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
713 if (start >= ilen)
714 goto out;
715 if (start + len < ilen)
716 ilen = start + len;
717 ilen -= start;
719 err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
720 if (err)
721 goto out;
723 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
724 byteaddr += (char *)inline_data_addr(inode, ipage) -
725 (char *)F2FS_INODE(ipage);
726 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
727 out:
728 f2fs_put_page(ipage, 1);
729 return err;