aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-09-12 23:05:58 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2017-09-12 23:05:58 -0400
commit6d8ef53e8b2fed8b0f91df0c6da7cc92747d934a (patch)
treefabe39021985b631ea5d457126e9ed3d53e910f0
parentcdb897e3279ad1677138d6bdf1cfaf1393718a08 (diff)
parente6c6de18f010d9a7d592f4044d2c30213cb3a7bc (diff)
Merge tag 'f2fs-for-4.14' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs
Pull f2fs updates from Jaegeuk Kim: "In this round, we've mostly tuned f2fs to provide better user experience for Android. Especially, we've worked on atomic write feature again with SQLite community in order to support it officially. And we added or modified several facilities to analyze and enhance IO behaviors. Major changes include: - add app/fs io stat - add inode checksum feature - support project/journalled quota - enhance atomic write with new ioctl() which exposes feature set - enhance background gc/discard/fstrim flows with new gc_urgent mode - add F2FS_IOC_FS{GET,SET}XATTR - fix some quota flows" * tag 'f2fs-for-4.14' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs: (63 commits) f2fs: hurry up to issue discard after io interruption f2fs: fix to show correct discard_granularity in sysfs f2fs: detect dirty inode in evict_inode f2fs: clear radix tree dirty tag of pages whose dirty flag is cleared f2fs: speed up gc_urgent mode with SSR f2fs: better to wait for fstrim completion f2fs: avoid race in between read xattr & write xattr f2fs: make get_lock_data_page to handle encrypted inode f2fs: use generic terms used for encrypted block management f2fs: introduce f2fs_encrypted_file for clean-up Revert "f2fs: add a new function get_ssr_cost" f2fs: constify super_operations f2fs: fix to wake up all sleeping flusher f2fs: avoid race in between atomic_read & atomic_inc f2fs: remove unneeded parameter of change_curseg f2fs: update i_flags correctly f2fs: don't check inode's checksum if it was dirtied or writebacked f2fs: don't need to update inode checksum for recovery f2fs: trigger fdatasync for non-atomic_write file f2fs: fix to avoid race in between aio and gc ...
-rw-r--r--Documentation/ABI/testing/sysfs-fs-f2fs21
-rw-r--r--Documentation/filesystems/f2fs.txt19
-rw-r--r--fs/f2fs/acl.c5
-rw-r--r--fs/f2fs/checkpoint.c60
-rw-r--r--fs/f2fs/data.c177
-rw-r--r--fs/f2fs/dir.c7
-rw-r--r--fs/f2fs/f2fs.h285
-rw-r--r--fs/f2fs/file.c360
-rw-r--r--fs/f2fs/gc.c115
-rw-r--r--fs/f2fs/gc.h27
-rw-r--r--fs/f2fs/inline.c142
-rw-r--r--fs/f2fs/inode.c132
-rw-r--r--fs/f2fs/namei.c43
-rw-r--r--fs/f2fs/node.c79
-rw-r--r--fs/f2fs/recovery.c83
-rw-r--r--fs/f2fs/segment.c292
-rw-r--r--fs/f2fs/segment.h47
-rw-r--r--fs/f2fs/super.c433
-rw-r--r--fs/f2fs/sysfs.c251
-rw-r--r--fs/f2fs/xattr.c8
-rw-r--r--include/linux/f2fs_fs.h40
-rw-r--r--include/trace/events/f2fs.h113
22 files changed, 2228 insertions, 511 deletions
diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
index 84c606fb3ca4..11b7f4ebea7c 100644
--- a/Documentation/ABI/testing/sysfs-fs-f2fs
+++ b/Documentation/ABI/testing/sysfs-fs-f2fs
@@ -57,6 +57,15 @@ Contact: "Jaegeuk Kim" <jaegeuk.kim@samsung.com>
57Description: 57Description:
58 Controls the issue rate of small discard commands. 58 Controls the issue rate of small discard commands.
59 59
60What: /sys/fs/f2fs/<disk>/discard_granularity
61Date: July 2017
62Contact: "Chao Yu" <yuchao0@huawei.com>
63Description:
64 Controls discard granularity of inner discard thread, inner thread
65 will not issue discards with size that is smaller than granularity.
66 The unit size is one block, now only support configuring in range
67 of [1, 512].
68
60What: /sys/fs/f2fs/<disk>/max_victim_search 69What: /sys/fs/f2fs/<disk>/max_victim_search
61Date: January 2014 70Date: January 2014
62Contact: "Jaegeuk Kim" <jaegeuk.kim@samsung.com> 71Contact: "Jaegeuk Kim" <jaegeuk.kim@samsung.com>
@@ -130,3 +139,15 @@ Date: June 2017
130Contact: "Chao Yu" <yuchao0@huawei.com> 139Contact: "Chao Yu" <yuchao0@huawei.com>
131Description: 140Description:
132 Controls current reserved blocks in system. 141 Controls current reserved blocks in system.
142
143What: /sys/fs/f2fs/<disk>/gc_urgent
144Date: August 2017
145Contact: "Jaegeuk Kim" <jaegeuk@kernel.org>
146Description:
147 Do background GC agressively
148
149What: /sys/fs/f2fs/<disk>/gc_urgent_sleep_time
150Date: August 2017
151Contact: "Jaegeuk Kim" <jaegeuk@kernel.org>
152Description:
153 Controls sleep time of GC urgent mode
diff --git a/Documentation/filesystems/f2fs.txt b/Documentation/filesystems/f2fs.txt
index 273ccb26885e..13c2ff034348 100644
--- a/Documentation/filesystems/f2fs.txt
+++ b/Documentation/filesystems/f2fs.txt
@@ -164,6 +164,16 @@ io_bits=%u Set the bit size of write IO requests. It should be set
164 with "mode=lfs". 164 with "mode=lfs".
165usrquota Enable plain user disk quota accounting. 165usrquota Enable plain user disk quota accounting.
166grpquota Enable plain group disk quota accounting. 166grpquota Enable plain group disk quota accounting.
167prjquota Enable plain project quota accounting.
168usrjquota=<file> Appoint specified file and type during mount, so that quota
169grpjquota=<file> information can be properly updated during recovery flow,
170prjjquota=<file> <quota file>: must be in root directory;
171jqfmt=<quota type> <quota type>: [vfsold,vfsv0,vfsv1].
172offusrjquota Turn off user journelled quota.
173offgrpjquota Turn off group journelled quota.
174offprjjquota Turn off project journelled quota.
175quota Enable plain user disk quota accounting.
176noquota Disable all plain disk quota option.
167 177
168================================================================================ 178================================================================================
169DEBUGFS ENTRIES 179DEBUGFS ENTRIES
@@ -209,6 +219,15 @@ Files in /sys/fs/f2fs/<devname>
209 gc_idle = 1 will select the Cost Benefit approach 219 gc_idle = 1 will select the Cost Benefit approach
210 & setting gc_idle = 2 will select the greedy approach. 220 & setting gc_idle = 2 will select the greedy approach.
211 221
222 gc_urgent This parameter controls triggering background GCs
223 urgently or not. Setting gc_urgent = 0 [default]
224 makes back to default behavior, while if it is set
225 to 1, background thread starts to do GC by given
226 gc_urgent_sleep_time interval.
227
228 gc_urgent_sleep_time This parameter controls sleep time for gc_urgent.
229 500 ms is set by default. See above gc_urgent.
230
212 reclaim_segments This parameter controls the number of prefree 231 reclaim_segments This parameter controls the number of prefree
213 segments to be reclaimed. If the number of prefree 232 segments to be reclaimed. If the number of prefree
214 segments is larger than the number of segments 233 segments is larger than the number of segments
diff --git a/fs/f2fs/acl.c b/fs/f2fs/acl.c
index b4b8438c42ef..436b3a1464d9 100644
--- a/fs/f2fs/acl.c
+++ b/fs/f2fs/acl.c
@@ -207,15 +207,16 @@ static int __f2fs_set_acl(struct inode *inode, int type,
207 void *value = NULL; 207 void *value = NULL;
208 size_t size = 0; 208 size_t size = 0;
209 int error; 209 int error;
210 umode_t mode = inode->i_mode;
210 211
211 switch (type) { 212 switch (type) {
212 case ACL_TYPE_ACCESS: 213 case ACL_TYPE_ACCESS:
213 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS; 214 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
214 if (acl && !ipage) { 215 if (acl && !ipage) {
215 error = posix_acl_update_mode(inode, &inode->i_mode, &acl); 216 error = posix_acl_update_mode(inode, &mode, &acl);
216 if (error) 217 if (error)
217 return error; 218 return error;
218 set_acl_inode(inode, inode->i_mode); 219 set_acl_inode(inode, mode);
219 } 220 }
220 break; 221 break;
221 222
diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index 5b876f6d3f6b..04fe1df052b2 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -230,8 +230,9 @@ void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
230 ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true); 230 ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
231} 231}
232 232
233static int f2fs_write_meta_page(struct page *page, 233static int __f2fs_write_meta_page(struct page *page,
234 struct writeback_control *wbc) 234 struct writeback_control *wbc,
235 enum iostat_type io_type)
235{ 236{
236 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 237 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
237 238
@@ -244,7 +245,7 @@ static int f2fs_write_meta_page(struct page *page,
244 if (unlikely(f2fs_cp_error(sbi))) 245 if (unlikely(f2fs_cp_error(sbi)))
245 goto redirty_out; 246 goto redirty_out;
246 247
247 write_meta_page(sbi, page); 248 write_meta_page(sbi, page, io_type);
248 dec_page_count(sbi, F2FS_DIRTY_META); 249 dec_page_count(sbi, F2FS_DIRTY_META);
249 250
250 if (wbc->for_reclaim) 251 if (wbc->for_reclaim)
@@ -263,6 +264,12 @@ redirty_out:
263 return AOP_WRITEPAGE_ACTIVATE; 264 return AOP_WRITEPAGE_ACTIVATE;
264} 265}
265 266
267static int f2fs_write_meta_page(struct page *page,
268 struct writeback_control *wbc)
269{
270 return __f2fs_write_meta_page(page, wbc, FS_META_IO);
271}
272
266static int f2fs_write_meta_pages(struct address_space *mapping, 273static int f2fs_write_meta_pages(struct address_space *mapping,
267 struct writeback_control *wbc) 274 struct writeback_control *wbc)
268{ 275{
@@ -283,7 +290,7 @@ static int f2fs_write_meta_pages(struct address_space *mapping,
283 290
284 trace_f2fs_writepages(mapping->host, wbc, META); 291 trace_f2fs_writepages(mapping->host, wbc, META);
285 diff = nr_pages_to_write(sbi, META, wbc); 292 diff = nr_pages_to_write(sbi, META, wbc);
286 written = sync_meta_pages(sbi, META, wbc->nr_to_write); 293 written = sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
287 mutex_unlock(&sbi->cp_mutex); 294 mutex_unlock(&sbi->cp_mutex);
288 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 295 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
289 return 0; 296 return 0;
@@ -295,7 +302,7 @@ skip_write:
295} 302}
296 303
297long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 304long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
298 long nr_to_write) 305 long nr_to_write, enum iostat_type io_type)
299{ 306{
300 struct address_space *mapping = META_MAPPING(sbi); 307 struct address_space *mapping = META_MAPPING(sbi);
301 pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX; 308 pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
@@ -346,7 +353,7 @@ continue_unlock:
346 if (!clear_page_dirty_for_io(page)) 353 if (!clear_page_dirty_for_io(page))
347 goto continue_unlock; 354 goto continue_unlock;
348 355
349 if (mapping->a_ops->writepage(page, &wbc)) { 356 if (__f2fs_write_meta_page(page, &wbc, io_type)) {
350 unlock_page(page); 357 unlock_page(page);
351 break; 358 break;
352 } 359 }
@@ -581,11 +588,24 @@ static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
581int recover_orphan_inodes(struct f2fs_sb_info *sbi) 588int recover_orphan_inodes(struct f2fs_sb_info *sbi)
582{ 589{
583 block_t start_blk, orphan_blocks, i, j; 590 block_t start_blk, orphan_blocks, i, j;
584 int err; 591 unsigned int s_flags = sbi->sb->s_flags;
592 int err = 0;
585 593
586 if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 594 if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
587 return 0; 595 return 0;
588 596
597 if (s_flags & MS_RDONLY) {
598 f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
599 sbi->sb->s_flags &= ~MS_RDONLY;
600 }
601
602#ifdef CONFIG_QUOTA
603 /* Needed for iput() to work correctly and not trash data */
604 sbi->sb->s_flags |= MS_ACTIVE;
605 /* Turn on quotas so that they are updated correctly */
606 f2fs_enable_quota_files(sbi);
607#endif
608
589 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi); 609 start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
590 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi); 610 orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
591 611
@@ -601,14 +621,21 @@ int recover_orphan_inodes(struct f2fs_sb_info *sbi)
601 err = recover_orphan_inode(sbi, ino); 621 err = recover_orphan_inode(sbi, ino);
602 if (err) { 622 if (err) {
603 f2fs_put_page(page, 1); 623 f2fs_put_page(page, 1);
604 return err; 624 goto out;
605 } 625 }
606 } 626 }
607 f2fs_put_page(page, 1); 627 f2fs_put_page(page, 1);
608 } 628 }
609 /* clear Orphan Flag */ 629 /* clear Orphan Flag */
610 clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG); 630 clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
611 return 0; 631out:
632#ifdef CONFIG_QUOTA
633 /* Turn quotas off */
634 f2fs_quota_off_umount(sbi->sb);
635#endif
636 sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
637
638 return err;
612} 639}
613 640
614static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk) 641static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
@@ -904,7 +931,14 @@ retry:
904 if (inode) { 931 if (inode) {
905 unsigned long cur_ino = inode->i_ino; 932 unsigned long cur_ino = inode->i_ino;
906 933
934 if (is_dir)
935 F2FS_I(inode)->cp_task = current;
936
907 filemap_fdatawrite(inode->i_mapping); 937 filemap_fdatawrite(inode->i_mapping);
938
939 if (is_dir)
940 F2FS_I(inode)->cp_task = NULL;
941
908 iput(inode); 942 iput(inode);
909 /* We need to give cpu to another writers. */ 943 /* We need to give cpu to another writers. */
910 if (ino == cur_ino) { 944 if (ino == cur_ino) {
@@ -1017,7 +1051,7 @@ retry_flush_nodes:
1017 1051
1018 if (get_pages(sbi, F2FS_DIRTY_NODES)) { 1052 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1019 up_write(&sbi->node_write); 1053 up_write(&sbi->node_write);
1020 err = sync_node_pages(sbi, &wbc); 1054 err = sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1021 if (err) { 1055 if (err) {
1022 up_write(&sbi->node_change); 1056 up_write(&sbi->node_change);
1023 f2fs_unlock_all(sbi); 1057 f2fs_unlock_all(sbi);
@@ -1115,7 +1149,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1115 1149
1116 /* Flush all the NAT/SIT pages */ 1150 /* Flush all the NAT/SIT pages */
1117 while (get_pages(sbi, F2FS_DIRTY_META)) { 1151 while (get_pages(sbi, F2FS_DIRTY_META)) {
1118 sync_meta_pages(sbi, META, LONG_MAX); 1152 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1119 if (unlikely(f2fs_cp_error(sbi))) 1153 if (unlikely(f2fs_cp_error(sbi)))
1120 return -EIO; 1154 return -EIO;
1121 } 1155 }
@@ -1194,7 +1228,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1194 1228
1195 /* Flush all the NAT BITS pages */ 1229 /* Flush all the NAT BITS pages */
1196 while (get_pages(sbi, F2FS_DIRTY_META)) { 1230 while (get_pages(sbi, F2FS_DIRTY_META)) {
1197 sync_meta_pages(sbi, META, LONG_MAX); 1231 sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1198 if (unlikely(f2fs_cp_error(sbi))) 1232 if (unlikely(f2fs_cp_error(sbi)))
1199 return -EIO; 1233 return -EIO;
1200 } 1234 }
@@ -1249,7 +1283,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1249 percpu_counter_set(&sbi->alloc_valid_block_count, 0); 1283 percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1250 1284
1251 /* Here, we only have one bio having CP pack */ 1285 /* Here, we only have one bio having CP pack */
1252 sync_meta_pages(sbi, META_FLUSH, LONG_MAX); 1286 sync_meta_pages(sbi, META_FLUSH, LONG_MAX, FS_CP_META_IO);
1253 1287
1254 /* wait for previous submitted meta pages writeback */ 1288 /* wait for previous submitted meta pages writeback */
1255 wait_on_all_pages_writeback(sbi); 1289 wait_on_all_pages_writeback(sbi);
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index fb96bb71da00..36b535207c88 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -457,14 +457,65 @@ out_fail:
457 return err; 457 return err;
458} 458}
459 459
460static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
461 unsigned nr_pages)
462{
463 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
464 struct fscrypt_ctx *ctx = NULL;
465 struct bio *bio;
466
467 if (f2fs_encrypted_file(inode)) {
468 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
469 if (IS_ERR(ctx))
470 return ERR_CAST(ctx);
471
472 /* wait the page to be moved by cleaning */
473 f2fs_wait_on_block_writeback(sbi, blkaddr);
474 }
475
476 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
477 if (!bio) {
478 if (ctx)
479 fscrypt_release_ctx(ctx);
480 return ERR_PTR(-ENOMEM);
481 }
482 f2fs_target_device(sbi, blkaddr, bio);
483 bio->bi_end_io = f2fs_read_end_io;
484 bio->bi_private = ctx;
485 bio_set_op_attrs(bio, REQ_OP_READ, 0);
486
487 return bio;
488}
489
490/* This can handle encryption stuffs */
491static int f2fs_submit_page_read(struct inode *inode, struct page *page,
492 block_t blkaddr)
493{
494 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1);
495
496 if (IS_ERR(bio))
497 return PTR_ERR(bio);
498
499 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
500 bio_put(bio);
501 return -EFAULT;
502 }
503 __submit_bio(F2FS_I_SB(inode), bio, DATA);
504 return 0;
505}
506
460static void __set_data_blkaddr(struct dnode_of_data *dn) 507static void __set_data_blkaddr(struct dnode_of_data *dn)
461{ 508{
462 struct f2fs_node *rn = F2FS_NODE(dn->node_page); 509 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
463 __le32 *addr_array; 510 __le32 *addr_array;
511 int base = 0;
512
513 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
514 base = get_extra_isize(dn->inode);
464 515
465 /* Get physical address of data block */ 516 /* Get physical address of data block */
466 addr_array = blkaddr_in_node(rn); 517 addr_array = blkaddr_in_node(rn);
467 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); 518 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
468} 519}
469 520
470/* 521/*
@@ -508,8 +559,8 @@ int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
508 f2fs_wait_on_page_writeback(dn->node_page, NODE, true); 559 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
509 560
510 for (; count > 0; dn->ofs_in_node++) { 561 for (; count > 0; dn->ofs_in_node++) {
511 block_t blkaddr = 562 block_t blkaddr = datablock_addr(dn->inode,
512 datablock_addr(dn->node_page, dn->ofs_in_node); 563 dn->node_page, dn->ofs_in_node);
513 if (blkaddr == NULL_ADDR) { 564 if (blkaddr == NULL_ADDR) {
514 dn->data_blkaddr = NEW_ADDR; 565 dn->data_blkaddr = NEW_ADDR;
515 __set_data_blkaddr(dn); 566 __set_data_blkaddr(dn);
@@ -570,16 +621,6 @@ struct page *get_read_data_page(struct inode *inode, pgoff_t index,
570 struct page *page; 621 struct page *page;
571 struct extent_info ei = {0,0,0}; 622 struct extent_info ei = {0,0,0};
572 int err; 623 int err;
573 struct f2fs_io_info fio = {
574 .sbi = F2FS_I_SB(inode),
575 .type = DATA,
576 .op = REQ_OP_READ,
577 .op_flags = op_flags,
578 .encrypted_page = NULL,
579 };
580
581 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
582 return read_mapping_page(mapping, index, NULL);
583 624
584 page = f2fs_grab_cache_page(mapping, index, for_write); 625 page = f2fs_grab_cache_page(mapping, index, for_write);
585 if (!page) 626 if (!page)
@@ -620,9 +661,7 @@ got_it:
620 return page; 661 return page;
621 } 662 }
622 663
623 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr; 664 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
624 fio.page = page;
625 err = f2fs_submit_page_bio(&fio);
626 if (err) 665 if (err)
627 goto put_err; 666 goto put_err;
628 return page; 667 return page;
@@ -756,7 +795,8 @@ static int __allocate_data_block(struct dnode_of_data *dn)
756 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) 795 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
757 return -EPERM; 796 return -EPERM;
758 797
759 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node); 798 dn->data_blkaddr = datablock_addr(dn->inode,
799 dn->node_page, dn->ofs_in_node);
760 if (dn->data_blkaddr == NEW_ADDR) 800 if (dn->data_blkaddr == NEW_ADDR)
761 goto alloc; 801 goto alloc;
762 802
@@ -782,7 +822,7 @@ alloc:
782 822
783static inline bool __force_buffered_io(struct inode *inode, int rw) 823static inline bool __force_buffered_io(struct inode *inode, int rw)
784{ 824{
785 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) || 825 return (f2fs_encrypted_file(inode) ||
786 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) || 826 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
787 F2FS_I_SB(inode)->s_ndevs); 827 F2FS_I_SB(inode)->s_ndevs);
788} 828}
@@ -814,7 +854,7 @@ int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
814 F2FS_GET_BLOCK_PRE_AIO : 854 F2FS_GET_BLOCK_PRE_AIO :
815 F2FS_GET_BLOCK_PRE_DIO); 855 F2FS_GET_BLOCK_PRE_DIO);
816 } 856 }
817 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) { 857 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
818 err = f2fs_convert_inline_inode(inode); 858 err = f2fs_convert_inline_inode(inode);
819 if (err) 859 if (err)
820 return err; 860 return err;
@@ -903,7 +943,7 @@ next_dnode:
903 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 943 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
904 944
905next_block: 945next_block:
906 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); 946 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
907 947
908 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) { 948 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
909 if (create) { 949 if (create) {
@@ -1040,7 +1080,7 @@ static int get_data_block_dio(struct inode *inode, sector_t iblock,
1040 struct buffer_head *bh_result, int create) 1080 struct buffer_head *bh_result, int create)
1041{ 1081{
1042 return __get_data_block(inode, iblock, bh_result, create, 1082 return __get_data_block(inode, iblock, bh_result, create,
1043 F2FS_GET_BLOCK_DIO, NULL); 1083 F2FS_GET_BLOCK_DEFAULT, NULL);
1044} 1084}
1045 1085
1046static int get_data_block_bmap(struct inode *inode, sector_t iblock, 1086static int get_data_block_bmap(struct inode *inode, sector_t iblock,
@@ -1146,35 +1186,6 @@ out:
1146 return ret; 1186 return ret;
1147} 1187}
1148 1188
1149static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1150 unsigned nr_pages)
1151{
1152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1153 struct fscrypt_ctx *ctx = NULL;
1154 struct bio *bio;
1155
1156 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1157 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1158 if (IS_ERR(ctx))
1159 return ERR_CAST(ctx);
1160
1161 /* wait the page to be moved by cleaning */
1162 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1163 }
1164
1165 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1166 if (!bio) {
1167 if (ctx)
1168 fscrypt_release_ctx(ctx);
1169 return ERR_PTR(-ENOMEM);
1170 }
1171 f2fs_target_device(sbi, blkaddr, bio);
1172 bio->bi_end_io = f2fs_read_end_io;
1173 bio->bi_private = ctx;
1174
1175 return bio;
1176}
1177
1178/* 1189/*
1179 * This function was originally taken from fs/mpage.c, and customized for f2fs. 1190 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1180 * Major change was from block_size == page_size in f2fs by default. 1191 * Major change was from block_size == page_size in f2fs by default.
@@ -1240,7 +1251,7 @@ static int f2fs_mpage_readpages(struct address_space *mapping,
1240 map.m_len = last_block - block_in_file; 1251 map.m_len = last_block - block_in_file;
1241 1252
1242 if (f2fs_map_blocks(inode, &map, 0, 1253 if (f2fs_map_blocks(inode, &map, 0,
1243 F2FS_GET_BLOCK_READ)) 1254 F2FS_GET_BLOCK_DEFAULT))
1244 goto set_error_page; 1255 goto set_error_page;
1245 } 1256 }
1246got_it: 1257got_it:
@@ -1271,12 +1282,11 @@ submit_and_realloc:
1271 bio = NULL; 1282 bio = NULL;
1272 } 1283 }
1273 if (bio == NULL) { 1284 if (bio == NULL) {
1274 bio = f2fs_grab_bio(inode, block_nr, nr_pages); 1285 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages);
1275 if (IS_ERR(bio)) { 1286 if (IS_ERR(bio)) {
1276 bio = NULL; 1287 bio = NULL;
1277 goto set_error_page; 1288 goto set_error_page;
1278 } 1289 }
1279 bio_set_op_attrs(bio, REQ_OP_READ, 0);
1280 } 1290 }
1281 1291
1282 if (bio_add_page(bio, page, blocksize, 0) < blocksize) 1292 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
@@ -1341,11 +1351,11 @@ static int encrypt_one_page(struct f2fs_io_info *fio)
1341 struct inode *inode = fio->page->mapping->host; 1351 struct inode *inode = fio->page->mapping->host;
1342 gfp_t gfp_flags = GFP_NOFS; 1352 gfp_t gfp_flags = GFP_NOFS;
1343 1353
1344 if (!f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode)) 1354 if (!f2fs_encrypted_file(inode))
1345 return 0; 1355 return 0;
1346 1356
1347 /* wait for GCed encrypted page writeback */ 1357 /* wait for GCed encrypted page writeback */
1348 f2fs_wait_on_encrypted_page_writeback(fio->sbi, fio->old_blkaddr); 1358 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
1349 1359
1350retry_encrypt: 1360retry_encrypt:
1351 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page, 1361 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
@@ -1471,7 +1481,8 @@ out:
1471} 1481}
1472 1482
1473static int __write_data_page(struct page *page, bool *submitted, 1483static int __write_data_page(struct page *page, bool *submitted,
1474 struct writeback_control *wbc) 1484 struct writeback_control *wbc,
1485 enum iostat_type io_type)
1475{ 1486{
1476 struct inode *inode = page->mapping->host; 1487 struct inode *inode = page->mapping->host;
1477 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1488 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
@@ -1492,6 +1503,7 @@ static int __write_data_page(struct page *page, bool *submitted,
1492 .encrypted_page = NULL, 1503 .encrypted_page = NULL,
1493 .submitted = false, 1504 .submitted = false,
1494 .need_lock = LOCK_RETRY, 1505 .need_lock = LOCK_RETRY,
1506 .io_type = io_type,
1495 }; 1507 };
1496 1508
1497 trace_f2fs_writepage(page, DATA); 1509 trace_f2fs_writepage(page, DATA);
@@ -1598,7 +1610,7 @@ redirty_out:
1598static int f2fs_write_data_page(struct page *page, 1610static int f2fs_write_data_page(struct page *page,
1599 struct writeback_control *wbc) 1611 struct writeback_control *wbc)
1600{ 1612{
1601 return __write_data_page(page, NULL, wbc); 1613 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
1602} 1614}
1603 1615
1604/* 1616/*
@@ -1607,7 +1619,8 @@ static int f2fs_write_data_page(struct page *page,
1607 * warm/hot data page. 1619 * warm/hot data page.
1608 */ 1620 */
1609static int f2fs_write_cache_pages(struct address_space *mapping, 1621static int f2fs_write_cache_pages(struct address_space *mapping,
1610 struct writeback_control *wbc) 1622 struct writeback_control *wbc,
1623 enum iostat_type io_type)
1611{ 1624{
1612 int ret = 0; 1625 int ret = 0;
1613 int done = 0; 1626 int done = 0;
@@ -1697,7 +1710,7 @@ continue_unlock:
1697 if (!clear_page_dirty_for_io(page)) 1710 if (!clear_page_dirty_for_io(page))
1698 goto continue_unlock; 1711 goto continue_unlock;
1699 1712
1700 ret = __write_data_page(page, &submitted, wbc); 1713 ret = __write_data_page(page, &submitted, wbc, io_type);
1701 if (unlikely(ret)) { 1714 if (unlikely(ret)) {
1702 /* 1715 /*
1703 * keep nr_to_write, since vfs uses this to 1716 * keep nr_to_write, since vfs uses this to
@@ -1752,8 +1765,9 @@ continue_unlock:
1752 return ret; 1765 return ret;
1753} 1766}
1754 1767
1755static int f2fs_write_data_pages(struct address_space *mapping, 1768int __f2fs_write_data_pages(struct address_space *mapping,
1756 struct writeback_control *wbc) 1769 struct writeback_control *wbc,
1770 enum iostat_type io_type)
1757{ 1771{
1758 struct inode *inode = mapping->host; 1772 struct inode *inode = mapping->host;
1759 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1773 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
@@ -1790,7 +1804,7 @@ static int f2fs_write_data_pages(struct address_space *mapping,
1790 goto skip_write; 1804 goto skip_write;
1791 1805
1792 blk_start_plug(&plug); 1806 blk_start_plug(&plug);
1793 ret = f2fs_write_cache_pages(mapping, wbc); 1807 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
1794 blk_finish_plug(&plug); 1808 blk_finish_plug(&plug);
1795 1809
1796 if (wbc->sync_mode == WB_SYNC_ALL) 1810 if (wbc->sync_mode == WB_SYNC_ALL)
@@ -1809,6 +1823,16 @@ skip_write:
1809 return 0; 1823 return 0;
1810} 1824}
1811 1825
1826static int f2fs_write_data_pages(struct address_space *mapping,
1827 struct writeback_control *wbc)
1828{
1829 struct inode *inode = mapping->host;
1830
1831 return __f2fs_write_data_pages(mapping, wbc,
1832 F2FS_I(inode)->cp_task == current ?
1833 FS_CP_DATA_IO : FS_DATA_IO);
1834}
1835
1812static void f2fs_write_failed(struct address_space *mapping, loff_t to) 1836static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1813{ 1837{
1814 struct inode *inode = mapping->host; 1838 struct inode *inode = mapping->host;
@@ -1858,7 +1882,7 @@ restart:
1858 set_new_dnode(&dn, inode, ipage, ipage, 0); 1882 set_new_dnode(&dn, inode, ipage, ipage, 0);
1859 1883
1860 if (f2fs_has_inline_data(inode)) { 1884 if (f2fs_has_inline_data(inode)) {
1861 if (pos + len <= MAX_INLINE_DATA) { 1885 if (pos + len <= MAX_INLINE_DATA(inode)) {
1862 read_inline_data(page, ipage); 1886 read_inline_data(page, ipage);
1863 set_inode_flag(inode, FI_DATA_EXIST); 1887 set_inode_flag(inode, FI_DATA_EXIST);
1864 if (inode->i_nlink) 1888 if (inode->i_nlink)
@@ -1956,8 +1980,8 @@ repeat:
1956 f2fs_wait_on_page_writeback(page, DATA, false); 1980 f2fs_wait_on_page_writeback(page, DATA, false);
1957 1981
1958 /* wait for GCed encrypted page writeback */ 1982 /* wait for GCed encrypted page writeback */
1959 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) 1983 if (f2fs_encrypted_file(inode))
1960 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr); 1984 f2fs_wait_on_block_writeback(sbi, blkaddr);
1961 1985
1962 if (len == PAGE_SIZE || PageUptodate(page)) 1986 if (len == PAGE_SIZE || PageUptodate(page))
1963 return 0; 1987 return 0;
@@ -1971,21 +1995,9 @@ repeat:
1971 zero_user_segment(page, 0, PAGE_SIZE); 1995 zero_user_segment(page, 0, PAGE_SIZE);
1972 SetPageUptodate(page); 1996 SetPageUptodate(page);
1973 } else { 1997 } else {
1974 struct bio *bio; 1998 err = f2fs_submit_page_read(inode, page, blkaddr);
1975 1999 if (err)
1976 bio = f2fs_grab_bio(inode, blkaddr, 1);
1977 if (IS_ERR(bio)) {
1978 err = PTR_ERR(bio);
1979 goto fail;
1980 }
1981 bio->bi_opf = REQ_OP_READ;
1982 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1983 bio_put(bio);
1984 err = -EFAULT;
1985 goto fail; 2000 goto fail;
1986 }
1987
1988 __submit_bio(sbi, bio, DATA);
1989 2001
1990 lock_page(page); 2002 lock_page(page);
1991 if (unlikely(page->mapping != mapping)) { 2003 if (unlikely(page->mapping != mapping)) {
@@ -2075,10 +2087,13 @@ static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2075 up_read(&F2FS_I(inode)->dio_rwsem[rw]); 2087 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2076 2088
2077 if (rw == WRITE) { 2089 if (rw == WRITE) {
2078 if (err > 0) 2090 if (err > 0) {
2091 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2092 err);
2079 set_inode_flag(inode, FI_UPDATE_WRITE); 2093 set_inode_flag(inode, FI_UPDATE_WRITE);
2080 else if (err < 0) 2094 } else if (err < 0) {
2081 f2fs_write_failed(mapping, offset + count); 2095 f2fs_write_failed(mapping, offset + count);
2096 }
2082 } 2097 }
2083 2098
2084 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); 2099 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index 37f9c7f55605..c0c933ad43c8 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -705,6 +705,8 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
705 struct f2fs_dentry_block *dentry_blk; 705 struct f2fs_dentry_block *dentry_blk;
706 unsigned int bit_pos; 706 unsigned int bit_pos;
707 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 707 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
708 struct address_space *mapping = page_mapping(page);
709 unsigned long flags;
708 int i; 710 int i;
709 711
710 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME); 712 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
@@ -735,6 +737,11 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
735 737
736 if (bit_pos == NR_DENTRY_IN_BLOCK && 738 if (bit_pos == NR_DENTRY_IN_BLOCK &&
737 !truncate_hole(dir, page->index, page->index + 1)) { 739 !truncate_hole(dir, page->index, page->index + 1)) {
740 spin_lock_irqsave(&mapping->tree_lock, flags);
741 radix_tree_tag_clear(&mapping->page_tree, page_index(page),
742 PAGECACHE_TAG_DIRTY);
743 spin_unlock_irqrestore(&mapping->tree_lock, flags);
744
738 clear_page_dirty_for_io(page); 745 clear_page_dirty_for_io(page);
739 ClearPagePrivate(page); 746 ClearPagePrivate(page);
740 ClearPageUptodate(page); 747 ClearPageUptodate(page);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 94a88b233e98..9a7c90386947 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -91,6 +91,8 @@ extern char *fault_name[FAULT_MAX];
91#define F2FS_MOUNT_LFS 0x00040000 91#define F2FS_MOUNT_LFS 0x00040000
92#define F2FS_MOUNT_USRQUOTA 0x00080000 92#define F2FS_MOUNT_USRQUOTA 0x00080000
93#define F2FS_MOUNT_GRPQUOTA 0x00100000 93#define F2FS_MOUNT_GRPQUOTA 0x00100000
94#define F2FS_MOUNT_PRJQUOTA 0x00200000
95#define F2FS_MOUNT_QUOTA 0x00400000
94 96
95#define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option) 97#define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
96#define set_opt(sbi, option) ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option) 98#define set_opt(sbi, option) ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
@@ -110,8 +112,12 @@ struct f2fs_mount_info {
110 unsigned int opt; 112 unsigned int opt;
111}; 113};
112 114
113#define F2FS_FEATURE_ENCRYPT 0x0001 115#define F2FS_FEATURE_ENCRYPT 0x0001
114#define F2FS_FEATURE_BLKZONED 0x0002 116#define F2FS_FEATURE_BLKZONED 0x0002
117#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
118#define F2FS_FEATURE_EXTRA_ATTR 0x0008
119#define F2FS_FEATURE_PRJQUOTA 0x0010
120#define F2FS_FEATURE_INODE_CHKSUM 0x0020
115 121
116#define F2FS_HAS_FEATURE(sb, mask) \ 122#define F2FS_HAS_FEATURE(sb, mask) \
117 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0) 123 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -142,6 +148,8 @@ enum {
142 (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg) 148 (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
143#define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi) 149#define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
144#define DISCARD_ISSUE_RATE 8 150#define DISCARD_ISSUE_RATE 8
151#define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */
152#define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
145#define DEF_CP_INTERVAL 60 /* 60 secs */ 153#define DEF_CP_INTERVAL 60 /* 60 secs */
146#define DEF_IDLE_INTERVAL 5 /* 5 secs */ 154#define DEF_IDLE_INTERVAL 5 /* 5 secs */
147 155
@@ -190,11 +198,18 @@ struct discard_entry {
190 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */ 198 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
191}; 199};
192 200
201/* default discard granularity of inner discard thread, unit: block count */
202#define DEFAULT_DISCARD_GRANULARITY 16
203
193/* max discard pend list number */ 204/* max discard pend list number */
194#define MAX_PLIST_NUM 512 205#define MAX_PLIST_NUM 512
195#define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \ 206#define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
196 (MAX_PLIST_NUM - 1) : (blk_num - 1)) 207 (MAX_PLIST_NUM - 1) : (blk_num - 1))
197 208
209#define P_ACTIVE 0x01
210#define P_TRIM 0x02
211#define plist_issue(tag) (((tag) & P_ACTIVE) || ((tag) & P_TRIM))
212
198enum { 213enum {
199 D_PREP, 214 D_PREP,
200 D_SUBMIT, 215 D_SUBMIT,
@@ -230,11 +245,14 @@ struct discard_cmd_control {
230 struct task_struct *f2fs_issue_discard; /* discard thread */ 245 struct task_struct *f2fs_issue_discard; /* discard thread */
231 struct list_head entry_list; /* 4KB discard entry list */ 246 struct list_head entry_list; /* 4KB discard entry list */
232 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */ 247 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
248 unsigned char pend_list_tag[MAX_PLIST_NUM];/* tag for pending entries */
233 struct list_head wait_list; /* store on-flushing entries */ 249 struct list_head wait_list; /* store on-flushing entries */
234 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */ 250 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
251 unsigned int discard_wake; /* to wake up discard thread */
235 struct mutex cmd_lock; 252 struct mutex cmd_lock;
236 unsigned int nr_discards; /* # of discards in the list */ 253 unsigned int nr_discards; /* # of discards in the list */
237 unsigned int max_discards; /* max. discards to be issued */ 254 unsigned int max_discards; /* max. discards to be issued */
255 unsigned int discard_granularity; /* discard granularity */
238 unsigned int undiscard_blks; /* # of undiscard blocks */ 256 unsigned int undiscard_blks; /* # of undiscard blocks */
239 atomic_t issued_discard; /* # of issued discard */ 257 atomic_t issued_discard; /* # of issued discard */
240 atomic_t issing_discard; /* # of issing discard */ 258 atomic_t issing_discard; /* # of issing discard */
@@ -308,6 +326,7 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal,
308 struct f2fs_flush_device) 326 struct f2fs_flush_device)
309#define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \ 327#define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
310 struct f2fs_gc_range) 328 struct f2fs_gc_range)
329#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
311 330
312#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY 331#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
313#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY 332#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
@@ -332,6 +351,9 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal,
332#define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION 351#define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
333#endif 352#endif
334 353
354#define F2FS_IOC_FSGETXATTR FS_IOC_FSGETXATTR
355#define F2FS_IOC_FSSETXATTR FS_IOC_FSSETXATTR
356
335struct f2fs_gc_range { 357struct f2fs_gc_range {
336 u32 sync; 358 u32 sync;
337 u64 start; 359 u64 start;
@@ -355,16 +377,36 @@ struct f2fs_flush_device {
355 u32 segments; /* # of segments to flush */ 377 u32 segments; /* # of segments to flush */
356}; 378};
357 379
380/* for inline stuff */
381#define DEF_INLINE_RESERVED_SIZE 1
382static inline int get_extra_isize(struct inode *inode);
383#define MAX_INLINE_DATA(inode) (sizeof(__le32) * \
384 (CUR_ADDRS_PER_INODE(inode) - \
385 DEF_INLINE_RESERVED_SIZE - \
386 F2FS_INLINE_XATTR_ADDRS))
387
388/* for inline dir */
389#define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
390 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
391 BITS_PER_BYTE + 1))
392#define INLINE_DENTRY_BITMAP_SIZE(inode) ((NR_INLINE_DENTRY(inode) + \
393 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
394#define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
395 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
396 NR_INLINE_DENTRY(inode) + \
397 INLINE_DENTRY_BITMAP_SIZE(inode)))
398
358/* 399/*
359 * For INODE and NODE manager 400 * For INODE and NODE manager
360 */ 401 */
361/* for directory operations */ 402/* for directory operations */
362struct f2fs_dentry_ptr { 403struct f2fs_dentry_ptr {
363 struct inode *inode; 404 struct inode *inode;
364 const void *bitmap; 405 void *bitmap;
365 struct f2fs_dir_entry *dentry; 406 struct f2fs_dir_entry *dentry;
366 __u8 (*filename)[F2FS_SLOT_LEN]; 407 __u8 (*filename)[F2FS_SLOT_LEN];
367 int max; 408 int max;
409 int nr_bitmap;
368}; 410};
369 411
370static inline void make_dentry_ptr_block(struct inode *inode, 412static inline void make_dentry_ptr_block(struct inode *inode,
@@ -372,19 +414,26 @@ static inline void make_dentry_ptr_block(struct inode *inode,
372{ 414{
373 d->inode = inode; 415 d->inode = inode;
374 d->max = NR_DENTRY_IN_BLOCK; 416 d->max = NR_DENTRY_IN_BLOCK;
417 d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
375 d->bitmap = &t->dentry_bitmap; 418 d->bitmap = &t->dentry_bitmap;
376 d->dentry = t->dentry; 419 d->dentry = t->dentry;
377 d->filename = t->filename; 420 d->filename = t->filename;
378} 421}
379 422
380static inline void make_dentry_ptr_inline(struct inode *inode, 423static inline void make_dentry_ptr_inline(struct inode *inode,
381 struct f2fs_dentry_ptr *d, struct f2fs_inline_dentry *t) 424 struct f2fs_dentry_ptr *d, void *t)
382{ 425{
426 int entry_cnt = NR_INLINE_DENTRY(inode);
427 int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
428 int reserved_size = INLINE_RESERVED_SIZE(inode);
429
383 d->inode = inode; 430 d->inode = inode;
384 d->max = NR_INLINE_DENTRY; 431 d->max = entry_cnt;
385 d->bitmap = &t->dentry_bitmap; 432 d->nr_bitmap = bitmap_size;
386 d->dentry = t->dentry; 433 d->bitmap = t;
387 d->filename = t->filename; 434 d->dentry = t + bitmap_size + reserved_size;
435 d->filename = t + bitmap_size + reserved_size +
436 SIZE_OF_DIR_ENTRY * entry_cnt;
388} 437}
389 438
390/* 439/*
@@ -473,12 +522,13 @@ struct f2fs_map_blocks {
473}; 522};
474 523
475/* for flag in get_data_block */ 524/* for flag in get_data_block */
476#define F2FS_GET_BLOCK_READ 0 525enum {
477#define F2FS_GET_BLOCK_DIO 1 526 F2FS_GET_BLOCK_DEFAULT,
478#define F2FS_GET_BLOCK_FIEMAP 2 527 F2FS_GET_BLOCK_FIEMAP,
479#define F2FS_GET_BLOCK_BMAP 3 528 F2FS_GET_BLOCK_BMAP,
480#define F2FS_GET_BLOCK_PRE_DIO 4 529 F2FS_GET_BLOCK_PRE_DIO,
481#define F2FS_GET_BLOCK_PRE_AIO 5 530 F2FS_GET_BLOCK_PRE_AIO,
531};
482 532
483/* 533/*
484 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later. 534 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
@@ -521,6 +571,7 @@ struct f2fs_inode_info {
521 f2fs_hash_t chash; /* hash value of given file name */ 571 f2fs_hash_t chash; /* hash value of given file name */
522 unsigned int clevel; /* maximum level of given file name */ 572 unsigned int clevel; /* maximum level of given file name */
523 struct task_struct *task; /* lookup and create consistency */ 573 struct task_struct *task; /* lookup and create consistency */
574 struct task_struct *cp_task; /* separate cp/wb IO stats*/
524 nid_t i_xattr_nid; /* node id that contains xattrs */ 575 nid_t i_xattr_nid; /* node id that contains xattrs */
525 loff_t last_disk_size; /* lastly written file size */ 576 loff_t last_disk_size; /* lastly written file size */
526 577
@@ -533,10 +584,15 @@ struct f2fs_inode_info {
533 struct list_head dirty_list; /* dirty list for dirs and files */ 584 struct list_head dirty_list; /* dirty list for dirs and files */
534 struct list_head gdirty_list; /* linked in global dirty list */ 585 struct list_head gdirty_list; /* linked in global dirty list */
535 struct list_head inmem_pages; /* inmemory pages managed by f2fs */ 586 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
587 struct task_struct *inmem_task; /* store inmemory task */
536 struct mutex inmem_lock; /* lock for inmemory pages */ 588 struct mutex inmem_lock; /* lock for inmemory pages */
537 struct extent_tree *extent_tree; /* cached extent_tree entry */ 589 struct extent_tree *extent_tree; /* cached extent_tree entry */
538 struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */ 590 struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
539 struct rw_semaphore i_mmap_sem; 591 struct rw_semaphore i_mmap_sem;
592 struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
593
594 int i_extra_isize; /* size of extra space located in i_addr */
595 kprojid_t i_projid; /* id for project quota */
540}; 596};
541 597
542static inline void get_extent_info(struct extent_info *ext, 598static inline void get_extent_info(struct extent_info *ext,
@@ -823,6 +879,23 @@ enum need_lock_type {
823 LOCK_RETRY, 879 LOCK_RETRY,
824}; 880};
825 881
882enum iostat_type {
883 APP_DIRECT_IO, /* app direct IOs */
884 APP_BUFFERED_IO, /* app buffered IOs */
885 APP_WRITE_IO, /* app write IOs */
886 APP_MAPPED_IO, /* app mapped IOs */
887 FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */
888 FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */
889 FS_META_IO, /* meta IOs from kworker/reclaimer */
890 FS_GC_DATA_IO, /* data IOs from forground gc */
891 FS_GC_NODE_IO, /* node IOs from forground gc */
892 FS_CP_DATA_IO, /* data IOs from checkpoint */
893 FS_CP_NODE_IO, /* node IOs from checkpoint */
894 FS_CP_META_IO, /* meta IOs from checkpoint */
895 FS_DISCARD, /* discard */
896 NR_IO_TYPE,
897};
898
826struct f2fs_io_info { 899struct f2fs_io_info {
827 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */ 900 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
828 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */ 901 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
@@ -837,6 +910,7 @@ struct f2fs_io_info {
837 bool submitted; /* indicate IO submission */ 910 bool submitted; /* indicate IO submission */
838 int need_lock; /* indicate we need to lock cp_rwsem */ 911 int need_lock; /* indicate we need to lock cp_rwsem */
839 bool in_list; /* indicate fio is in io_list */ 912 bool in_list; /* indicate fio is in io_list */
913 enum iostat_type io_type; /* io type */
840}; 914};
841 915
842#define is_read_io(rw) ((rw) == READ) 916#define is_read_io(rw) ((rw) == READ)
@@ -1028,6 +1102,11 @@ struct f2fs_sb_info {
1028#endif 1102#endif
1029 spinlock_t stat_lock; /* lock for stat operations */ 1103 spinlock_t stat_lock; /* lock for stat operations */
1030 1104
1105 /* For app/fs IO statistics */
1106 spinlock_t iostat_lock;
1107 unsigned long long write_iostat[NR_IO_TYPE];
1108 bool iostat_enable;
1109
1031 /* For sysfs suppport */ 1110 /* For sysfs suppport */
1032 struct kobject s_kobj; 1111 struct kobject s_kobj;
1033 struct completion s_kobj_unregister; 1112 struct completion s_kobj_unregister;
@@ -1046,10 +1125,19 @@ struct f2fs_sb_info {
1046 /* Reference to checksum algorithm driver via cryptoapi */ 1125 /* Reference to checksum algorithm driver via cryptoapi */
1047 struct crypto_shash *s_chksum_driver; 1126 struct crypto_shash *s_chksum_driver;
1048 1127
1128 /* Precomputed FS UUID checksum for seeding other checksums */
1129 __u32 s_chksum_seed;
1130
1049 /* For fault injection */ 1131 /* For fault injection */
1050#ifdef CONFIG_F2FS_FAULT_INJECTION 1132#ifdef CONFIG_F2FS_FAULT_INJECTION
1051 struct f2fs_fault_info fault_info; 1133 struct f2fs_fault_info fault_info;
1052#endif 1134#endif
1135
1136#ifdef CONFIG_QUOTA
1137 /* Names of quota files with journalled quota */
1138 char *s_qf_names[MAXQUOTAS];
1139 int s_jquota_fmt; /* Format of quota to use */
1140#endif
1053}; 1141};
1054 1142
1055#ifdef CONFIG_F2FS_FAULT_INJECTION 1143#ifdef CONFIG_F2FS_FAULT_INJECTION
@@ -1137,6 +1225,27 @@ static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1137 return f2fs_crc32(sbi, buf, buf_size) == blk_crc; 1225 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1138} 1226}
1139 1227
1228static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1229 const void *address, unsigned int length)
1230{
1231 struct {
1232 struct shash_desc shash;
1233 char ctx[4];
1234 } desc;
1235 int err;
1236
1237 BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1238
1239 desc.shash.tfm = sbi->s_chksum_driver;
1240 desc.shash.flags = 0;
1241 *(u32 *)desc.ctx = crc;
1242
1243 err = crypto_shash_update(&desc.shash, address, length);
1244 BUG_ON(err);
1245
1246 return *(u32 *)desc.ctx;
1247}
1248
1140static inline struct f2fs_inode_info *F2FS_I(struct inode *inode) 1249static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1141{ 1250{
1142 return container_of(inode, struct f2fs_inode_info, vfs_inode); 1251 return container_of(inode, struct f2fs_inode_info, vfs_inode);
@@ -1760,20 +1869,38 @@ static inline bool IS_INODE(struct page *page)
1760 return RAW_IS_INODE(p); 1869 return RAW_IS_INODE(p);
1761} 1870}
1762 1871
1872static inline int offset_in_addr(struct f2fs_inode *i)
1873{
1874 return (i->i_inline & F2FS_EXTRA_ATTR) ?
1875 (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
1876}
1877
1763static inline __le32 *blkaddr_in_node(struct f2fs_node *node) 1878static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
1764{ 1879{
1765 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr; 1880 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
1766} 1881}
1767 1882
1768static inline block_t datablock_addr(struct page *node_page, 1883static inline int f2fs_has_extra_attr(struct inode *inode);
1769 unsigned int offset) 1884static inline block_t datablock_addr(struct inode *inode,
1885 struct page *node_page, unsigned int offset)
1770{ 1886{
1771 struct f2fs_node *raw_node; 1887 struct f2fs_node *raw_node;
1772 __le32 *addr_array; 1888 __le32 *addr_array;
1889 int base = 0;
1890 bool is_inode = IS_INODE(node_page);
1773 1891
1774 raw_node = F2FS_NODE(node_page); 1892 raw_node = F2FS_NODE(node_page);
1893
1894 /* from GC path only */
1895 if (!inode) {
1896 if (is_inode)
1897 base = offset_in_addr(&raw_node->i);
1898 } else if (f2fs_has_extra_attr(inode) && is_inode) {
1899 base = get_extra_isize(inode);
1900 }
1901
1775 addr_array = blkaddr_in_node(raw_node); 1902 addr_array = blkaddr_in_node(raw_node);
1776 return le32_to_cpu(addr_array[offset]); 1903 return le32_to_cpu(addr_array[base + offset]);
1777} 1904}
1778 1905
1779static inline int f2fs_test_bit(unsigned int nr, char *addr) 1906static inline int f2fs_test_bit(unsigned int nr, char *addr)
@@ -1836,6 +1963,20 @@ static inline void f2fs_change_bit(unsigned int nr, char *addr)
1836 *addr ^= mask; 1963 *addr ^= mask;
1837} 1964}
1838 1965
1966#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1967#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1968#define F2FS_FL_INHERITED (FS_PROJINHERIT_FL)
1969
1970static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1971{
1972 if (S_ISDIR(mode))
1973 return flags;
1974 else if (S_ISREG(mode))
1975 return flags & F2FS_REG_FLMASK;
1976 else
1977 return flags & F2FS_OTHER_FLMASK;
1978}
1979
1839/* used for f2fs_inode_info->flags */ 1980/* used for f2fs_inode_info->flags */
1840enum { 1981enum {
1841 FI_NEW_INODE, /* indicate newly allocated inode */ 1982 FI_NEW_INODE, /* indicate newly allocated inode */
@@ -1864,6 +2005,8 @@ enum {
1864 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */ 2005 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
1865 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */ 2006 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
1866 FI_HOT_DATA, /* indicate file is hot */ 2007 FI_HOT_DATA, /* indicate file is hot */
2008 FI_EXTRA_ATTR, /* indicate file has extra attribute */
2009 FI_PROJ_INHERIT, /* indicate file inherits projectid */
1867}; 2010};
1868 2011
1869static inline void __mark_inode_dirty_flag(struct inode *inode, 2012static inline void __mark_inode_dirty_flag(struct inode *inode,
@@ -1983,6 +2126,8 @@ static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
1983 set_bit(FI_DATA_EXIST, &fi->flags); 2126 set_bit(FI_DATA_EXIST, &fi->flags);
1984 if (ri->i_inline & F2FS_INLINE_DOTS) 2127 if (ri->i_inline & F2FS_INLINE_DOTS)
1985 set_bit(FI_INLINE_DOTS, &fi->flags); 2128 set_bit(FI_INLINE_DOTS, &fi->flags);
2129 if (ri->i_inline & F2FS_EXTRA_ATTR)
2130 set_bit(FI_EXTRA_ATTR, &fi->flags);
1986} 2131}
1987 2132
1988static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri) 2133static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
@@ -1999,6 +2144,13 @@ static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
1999 ri->i_inline |= F2FS_DATA_EXIST; 2144 ri->i_inline |= F2FS_DATA_EXIST;
2000 if (is_inode_flag_set(inode, FI_INLINE_DOTS)) 2145 if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2001 ri->i_inline |= F2FS_INLINE_DOTS; 2146 ri->i_inline |= F2FS_INLINE_DOTS;
2147 if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
2148 ri->i_inline |= F2FS_EXTRA_ATTR;
2149}
2150
2151static inline int f2fs_has_extra_attr(struct inode *inode)
2152{
2153 return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2002} 2154}
2003 2155
2004static inline int f2fs_has_inline_xattr(struct inode *inode) 2156static inline int f2fs_has_inline_xattr(struct inode *inode)
@@ -2009,8 +2161,8 @@ static inline int f2fs_has_inline_xattr(struct inode *inode)
2009static inline unsigned int addrs_per_inode(struct inode *inode) 2161static inline unsigned int addrs_per_inode(struct inode *inode)
2010{ 2162{
2011 if (f2fs_has_inline_xattr(inode)) 2163 if (f2fs_has_inline_xattr(inode))
2012 return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS; 2164 return CUR_ADDRS_PER_INODE(inode) - F2FS_INLINE_XATTR_ADDRS;
2013 return DEF_ADDRS_PER_INODE; 2165 return CUR_ADDRS_PER_INODE(inode);
2014} 2166}
2015 2167
2016static inline void *inline_xattr_addr(struct page *page) 2168static inline void *inline_xattr_addr(struct page *page)
@@ -2069,11 +2221,12 @@ static inline bool f2fs_is_drop_cache(struct inode *inode)
2069 return is_inode_flag_set(inode, FI_DROP_CACHE); 2221 return is_inode_flag_set(inode, FI_DROP_CACHE);
2070} 2222}
2071 2223
2072static inline void *inline_data_addr(struct page *page) 2224static inline void *inline_data_addr(struct inode *inode, struct page *page)
2073{ 2225{
2074 struct f2fs_inode *ri = F2FS_INODE(page); 2226 struct f2fs_inode *ri = F2FS_INODE(page);
2227 int extra_size = get_extra_isize(inode);
2075 2228
2076 return (void *)&(ri->i_addr[1]); 2229 return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
2077} 2230}
2078 2231
2079static inline int f2fs_has_inline_dentry(struct inode *inode) 2232static inline int f2fs_has_inline_dentry(struct inode *inode)
@@ -2164,10 +2317,50 @@ static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2164 return kmalloc(size, flags); 2317 return kmalloc(size, flags);
2165} 2318}
2166 2319
2320static inline int get_extra_isize(struct inode *inode)
2321{
2322 return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2323}
2324
2167#define get_inode_mode(i) \ 2325#define get_inode_mode(i) \
2168 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \ 2326 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2169 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode)) 2327 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2170 2328
2329#define F2FS_TOTAL_EXTRA_ATTR_SIZE \
2330 (offsetof(struct f2fs_inode, i_extra_end) - \
2331 offsetof(struct f2fs_inode, i_extra_isize)) \
2332
2333#define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
2334#define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \
2335 ((offsetof(typeof(*f2fs_inode), field) + \
2336 sizeof((f2fs_inode)->field)) \
2337 <= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize)) \
2338
2339static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2340{
2341 int i;
2342
2343 spin_lock(&sbi->iostat_lock);
2344 for (i = 0; i < NR_IO_TYPE; i++)
2345 sbi->write_iostat[i] = 0;
2346 spin_unlock(&sbi->iostat_lock);
2347}
2348
2349static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2350 enum iostat_type type, unsigned long long io_bytes)
2351{
2352 if (!sbi->iostat_enable)
2353 return;
2354 spin_lock(&sbi->iostat_lock);
2355 sbi->write_iostat[type] += io_bytes;
2356
2357 if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2358 sbi->write_iostat[APP_BUFFERED_IO] =
2359 sbi->write_iostat[APP_WRITE_IO] -
2360 sbi->write_iostat[APP_DIRECT_IO];
2361 spin_unlock(&sbi->iostat_lock);
2362}
2363
2171/* 2364/*
2172 * file.c 2365 * file.c
2173 */ 2366 */
@@ -2187,6 +2380,8 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
2187 * inode.c 2380 * inode.c
2188 */ 2381 */
2189void f2fs_set_inode_flags(struct inode *inode); 2382void f2fs_set_inode_flags(struct inode *inode);
2383bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2384void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2190struct inode *f2fs_iget(struct super_block *sb, unsigned long ino); 2385struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2191struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino); 2386struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2192int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink); 2387int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
@@ -2255,6 +2450,8 @@ static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2255 */ 2450 */
2256int f2fs_inode_dirtied(struct inode *inode, bool sync); 2451int f2fs_inode_dirtied(struct inode *inode, bool sync);
2257void f2fs_inode_synced(struct inode *inode); 2452void f2fs_inode_synced(struct inode *inode);
2453void f2fs_enable_quota_files(struct f2fs_sb_info *sbi);
2454void f2fs_quota_off_umount(struct super_block *sb);
2258int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover); 2455int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2259int f2fs_sync_fs(struct super_block *sb, int sync); 2456int f2fs_sync_fs(struct super_block *sb, int sync);
2260extern __printf(3, 4) 2457extern __printf(3, 4)
@@ -2285,15 +2482,15 @@ int truncate_xattr_node(struct inode *inode, struct page *page);
2285int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino); 2482int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2286int remove_inode_page(struct inode *inode); 2483int remove_inode_page(struct inode *inode);
2287struct page *new_inode_page(struct inode *inode); 2484struct page *new_inode_page(struct inode *inode);
2288struct page *new_node_page(struct dnode_of_data *dn, 2485struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2289 unsigned int ofs, struct page *ipage);
2290void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid); 2486void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2291struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid); 2487struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2292struct page *get_node_page_ra(struct page *parent, int start); 2488struct page *get_node_page_ra(struct page *parent, int start);
2293void move_node_page(struct page *node_page, int gc_type); 2489void move_node_page(struct page *node_page, int gc_type);
2294int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode, 2490int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2295 struct writeback_control *wbc, bool atomic); 2491 struct writeback_control *wbc, bool atomic);
2296int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc); 2492int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2493 bool do_balance, enum iostat_type io_type);
2297void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount); 2494void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2298bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid); 2495bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2299void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid); 2496void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
@@ -2314,6 +2511,7 @@ void destroy_node_manager_caches(void);
2314/* 2511/*
2315 * segment.c 2512 * segment.c
2316 */ 2513 */
2514bool need_SSR(struct f2fs_sb_info *sbi);
2317void register_inmem_page(struct inode *inode, struct page *page); 2515void register_inmem_page(struct inode *inode, struct page *page);
2318void drop_inmem_pages(struct inode *inode); 2516void drop_inmem_pages(struct inode *inode);
2319void drop_inmem_page(struct inode *inode, struct page *page); 2517void drop_inmem_page(struct inode *inode, struct page *page);
@@ -2336,7 +2534,8 @@ int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2336bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc); 2534bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2337struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno); 2535struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2338void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr); 2536void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2339void write_meta_page(struct f2fs_sb_info *sbi, struct page *page); 2537void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2538 enum iostat_type io_type);
2340void write_node_page(unsigned int nid, struct f2fs_io_info *fio); 2539void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2341void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio); 2540void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2342int rewrite_data_page(struct f2fs_io_info *fio); 2541int rewrite_data_page(struct f2fs_io_info *fio);
@@ -2353,8 +2552,7 @@ void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2353 struct f2fs_io_info *fio, bool add_list); 2552 struct f2fs_io_info *fio, bool add_list);
2354void f2fs_wait_on_page_writeback(struct page *page, 2553void f2fs_wait_on_page_writeback(struct page *page,
2355 enum page_type type, bool ordered); 2554 enum page_type type, bool ordered);
2356void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi, 2555void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2357 block_t blkaddr);
2358void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk); 2556void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2359void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk); 2557void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2360int lookup_journal_in_cursum(struct f2fs_journal *journal, int type, 2558int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
@@ -2377,7 +2575,7 @@ int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2377 int type, bool sync); 2575 int type, bool sync);
2378void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index); 2576void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2379long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 2577long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2380 long nr_to_write); 2578 long nr_to_write, enum iostat_type io_type);
2381void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type); 2579void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2382void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type); 2580void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2383void release_ino_entry(struct f2fs_sb_info *sbi, bool all); 2581void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
@@ -2430,6 +2628,9 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2430int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 2628int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2431 u64 start, u64 len); 2629 u64 start, u64 len);
2432void f2fs_set_page_dirty_nobuffers(struct page *page); 2630void f2fs_set_page_dirty_nobuffers(struct page *page);
2631int __f2fs_write_data_pages(struct address_space *mapping,
2632 struct writeback_control *wbc,
2633 enum iostat_type io_type);
2433void f2fs_invalidate_page(struct page *page, unsigned int offset, 2634void f2fs_invalidate_page(struct page *page, unsigned int offset,
2434 unsigned int length); 2635 unsigned int length);
2435int f2fs_release_page(struct page *page, gfp_t wait); 2636int f2fs_release_page(struct page *page, gfp_t wait);
@@ -2726,10 +2927,10 @@ void destroy_extent_cache(void);
2726/* 2927/*
2727 * sysfs.c 2928 * sysfs.c
2728 */ 2929 */
2729int __init f2fs_register_sysfs(void); 2930int __init f2fs_init_sysfs(void);
2730void f2fs_unregister_sysfs(void); 2931void f2fs_exit_sysfs(void);
2731int f2fs_init_sysfs(struct f2fs_sb_info *sbi); 2932int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
2732void f2fs_exit_sysfs(struct f2fs_sb_info *sbi); 2933void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
2733 2934
2734/* 2935/*
2735 * crypto support 2936 * crypto support
@@ -2739,6 +2940,11 @@ static inline bool f2fs_encrypted_inode(struct inode *inode)
2739 return file_is_encrypt(inode); 2940 return file_is_encrypt(inode);
2740} 2941}
2741 2942
2943static inline bool f2fs_encrypted_file(struct inode *inode)
2944{
2945 return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
2946}
2947
2742static inline void f2fs_set_encrypted_inode(struct inode *inode) 2948static inline void f2fs_set_encrypted_inode(struct inode *inode)
2743{ 2949{
2744#ifdef CONFIG_F2FS_FS_ENCRYPTION 2950#ifdef CONFIG_F2FS_FS_ENCRYPTION
@@ -2761,6 +2967,21 @@ static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
2761 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED); 2967 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
2762} 2968}
2763 2969
2970static inline int f2fs_sb_has_extra_attr(struct super_block *sb)
2971{
2972 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_EXTRA_ATTR);
2973}
2974
2975static inline int f2fs_sb_has_project_quota(struct super_block *sb)
2976{
2977 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_PRJQUOTA);
2978}
2979
2980static inline int f2fs_sb_has_inode_chksum(struct super_block *sb)
2981{
2982 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_INODE_CHKSUM);
2983}
2984
2764#ifdef CONFIG_BLK_DEV_ZONED 2985#ifdef CONFIG_BLK_DEV_ZONED
2765static inline int get_blkz_type(struct f2fs_sb_info *sbi, 2986static inline int get_blkz_type(struct f2fs_sb_info *sbi,
2766 struct block_device *bdev, block_t blkaddr) 2987 struct block_device *bdev, block_t blkaddr)
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 843a0d99f7ea..517e112c8a9a 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -98,14 +98,16 @@ static int f2fs_vm_page_mkwrite(struct vm_fault *vmf)
98 if (!PageUptodate(page)) 98 if (!PageUptodate(page))
99 SetPageUptodate(page); 99 SetPageUptodate(page);
100 100
101 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
102
101 trace_f2fs_vm_page_mkwrite(page, DATA); 103 trace_f2fs_vm_page_mkwrite(page, DATA);
102mapped: 104mapped:
103 /* fill the page */ 105 /* fill the page */
104 f2fs_wait_on_page_writeback(page, DATA, false); 106 f2fs_wait_on_page_writeback(page, DATA, false);
105 107
106 /* wait for GCed encrypted page writeback */ 108 /* wait for GCed encrypted page writeback */
107 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) 109 if (f2fs_encrypted_file(inode))
108 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr); 110 f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
109 111
110out_sem: 112out_sem:
111 up_read(&F2FS_I(inode)->i_mmap_sem); 113 up_read(&F2FS_I(inode)->i_mmap_sem);
@@ -274,9 +276,19 @@ sync_nodes:
274 goto sync_nodes; 276 goto sync_nodes;
275 } 277 }
276 278
277 ret = wait_on_node_pages_writeback(sbi, ino); 279 /*
278 if (ret) 280 * If it's atomic_write, it's just fine to keep write ordering. So
279 goto out; 281 * here we don't need to wait for node write completion, since we use
282 * node chain which serializes node blocks. If one of node writes are
283 * reordered, we can see simply broken chain, resulting in stopping
284 * roll-forward recovery. It means we'll recover all or none node blocks
285 * given fsync mark.
286 */
287 if (!atomic) {
288 ret = wait_on_node_pages_writeback(sbi, ino);
289 if (ret)
290 goto out;
291 }
280 292
281 /* once recovery info is written, don't need to tack this */ 293 /* once recovery info is written, don't need to tack this */
282 remove_ino_entry(sbi, ino, APPEND_INO); 294 remove_ino_entry(sbi, ino, APPEND_INO);
@@ -382,7 +394,8 @@ static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
382 dn.ofs_in_node++, pgofs++, 394 dn.ofs_in_node++, pgofs++,
383 data_ofs = (loff_t)pgofs << PAGE_SHIFT) { 395 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
384 block_t blkaddr; 396 block_t blkaddr;
385 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); 397 blkaddr = datablock_addr(dn.inode,
398 dn.node_page, dn.ofs_in_node);
386 399
387 if (__found_offset(blkaddr, dirty, pgofs, whence)) { 400 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
388 f2fs_put_dnode(&dn); 401 f2fs_put_dnode(&dn);
@@ -467,9 +480,13 @@ int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
467 struct f2fs_node *raw_node; 480 struct f2fs_node *raw_node;
468 int nr_free = 0, ofs = dn->ofs_in_node, len = count; 481 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
469 __le32 *addr; 482 __le32 *addr;
483 int base = 0;
484
485 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
486 base = get_extra_isize(dn->inode);
470 487
471 raw_node = F2FS_NODE(dn->node_page); 488 raw_node = F2FS_NODE(dn->node_page);
472 addr = blkaddr_in_node(raw_node) + ofs; 489 addr = blkaddr_in_node(raw_node) + base + ofs;
473 490
474 for (; count > 0; count--, addr++, dn->ofs_in_node++) { 491 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
475 block_t blkaddr = le32_to_cpu(*addr); 492 block_t blkaddr = le32_to_cpu(*addr);
@@ -647,7 +664,7 @@ int f2fs_getattr(const struct path *path, struct kstat *stat,
647 struct f2fs_inode_info *fi = F2FS_I(inode); 664 struct f2fs_inode_info *fi = F2FS_I(inode);
648 unsigned int flags; 665 unsigned int flags;
649 666
650 flags = fi->i_flags & FS_FL_USER_VISIBLE; 667 flags = fi->i_flags & (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
651 if (flags & FS_APPEND_FL) 668 if (flags & FS_APPEND_FL)
652 stat->attributes |= STATX_ATTR_APPEND; 669 stat->attributes |= STATX_ATTR_APPEND;
653 if (flags & FS_COMPR_FL) 670 if (flags & FS_COMPR_FL)
@@ -927,7 +944,8 @@ next_dnode:
927 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) - 944 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
928 dn.ofs_in_node, len); 945 dn.ofs_in_node, len);
929 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) { 946 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
930 *blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); 947 *blkaddr = datablock_addr(dn.inode,
948 dn.node_page, dn.ofs_in_node);
931 if (!is_checkpointed_data(sbi, *blkaddr)) { 949 if (!is_checkpointed_data(sbi, *blkaddr)) {
932 950
933 if (test_opt(sbi, LFS)) { 951 if (test_opt(sbi, LFS)) {
@@ -1003,8 +1021,8 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1003 ADDRS_PER_PAGE(dn.node_page, dst_inode) - 1021 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1004 dn.ofs_in_node, len - i); 1022 dn.ofs_in_node, len - i);
1005 do { 1023 do {
1006 dn.data_blkaddr = datablock_addr(dn.node_page, 1024 dn.data_blkaddr = datablock_addr(dn.inode,
1007 dn.ofs_in_node); 1025 dn.node_page, dn.ofs_in_node);
1008 truncate_data_blocks_range(&dn, 1); 1026 truncate_data_blocks_range(&dn, 1);
1009 1027
1010 if (do_replace[i]) { 1028 if (do_replace[i]) {
@@ -1173,7 +1191,8 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1173 int ret; 1191 int ret;
1174 1192
1175 for (; index < end; index++, dn->ofs_in_node++) { 1193 for (; index < end; index++, dn->ofs_in_node++) {
1176 if (datablock_addr(dn->node_page, dn->ofs_in_node) == NULL_ADDR) 1194 if (datablock_addr(dn->inode, dn->node_page,
1195 dn->ofs_in_node) == NULL_ADDR)
1177 count++; 1196 count++;
1178 } 1197 }
1179 1198
@@ -1184,8 +1203,8 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1184 1203
1185 dn->ofs_in_node = ofs_in_node; 1204 dn->ofs_in_node = ofs_in_node;
1186 for (index = start; index < end; index++, dn->ofs_in_node++) { 1205 for (index = start; index < end; index++, dn->ofs_in_node++) {
1187 dn->data_blkaddr = 1206 dn->data_blkaddr = datablock_addr(dn->inode,
1188 datablock_addr(dn->node_page, dn->ofs_in_node); 1207 dn->node_page, dn->ofs_in_node);
1189 /* 1208 /*
1190 * reserve_new_blocks will not guarantee entire block 1209 * reserve_new_blocks will not guarantee entire block
1191 * allocation. 1210 * allocation.
@@ -1495,33 +1514,67 @@ static int f2fs_release_file(struct inode *inode, struct file *filp)
1495 return 0; 1514 return 0;
1496} 1515}
1497 1516
1498#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) 1517static int f2fs_file_flush(struct file *file, fl_owner_t id)
1499#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1500
1501static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1502{ 1518{
1503 if (S_ISDIR(mode)) 1519 struct inode *inode = file_inode(file);
1504 return flags; 1520
1505 else if (S_ISREG(mode)) 1521 /*
1506 return flags & F2FS_REG_FLMASK; 1522 * If the process doing a transaction is crashed, we should do
1507 else 1523 * roll-back. Otherwise, other reader/write can see corrupted database
1508 return flags & F2FS_OTHER_FLMASK; 1524 * until all the writers close its file. Since this should be done
1525 * before dropping file lock, it needs to do in ->flush.
1526 */
1527 if (f2fs_is_atomic_file(inode) &&
1528 F2FS_I(inode)->inmem_task == current)
1529 drop_inmem_pages(inode);
1530 return 0;
1509} 1531}
1510 1532
1511static int f2fs_ioc_getflags(struct file *filp, unsigned long arg) 1533static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1512{ 1534{
1513 struct inode *inode = file_inode(filp); 1535 struct inode *inode = file_inode(filp);
1514 struct f2fs_inode_info *fi = F2FS_I(inode); 1536 struct f2fs_inode_info *fi = F2FS_I(inode);
1515 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE; 1537 unsigned int flags = fi->i_flags &
1538 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
1516 return put_user(flags, (int __user *)arg); 1539 return put_user(flags, (int __user *)arg);
1517} 1540}
1518 1541
1542static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1543{
1544 struct f2fs_inode_info *fi = F2FS_I(inode);
1545 unsigned int oldflags;
1546
1547 /* Is it quota file? Do not allow user to mess with it */
1548 if (IS_NOQUOTA(inode))
1549 return -EPERM;
1550
1551 flags = f2fs_mask_flags(inode->i_mode, flags);
1552
1553 oldflags = fi->i_flags;
1554
1555 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL))
1556 if (!capable(CAP_LINUX_IMMUTABLE))
1557 return -EPERM;
1558
1559 flags = flags & (FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1560 flags |= oldflags & ~(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1561 fi->i_flags = flags;
1562
1563 if (fi->i_flags & FS_PROJINHERIT_FL)
1564 set_inode_flag(inode, FI_PROJ_INHERIT);
1565 else
1566 clear_inode_flag(inode, FI_PROJ_INHERIT);
1567
1568 inode->i_ctime = current_time(inode);
1569 f2fs_set_inode_flags(inode);
1570 f2fs_mark_inode_dirty_sync(inode, false);
1571 return 0;
1572}
1573
1519static int f2fs_ioc_setflags(struct file *filp, unsigned long arg) 1574static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1520{ 1575{
1521 struct inode *inode = file_inode(filp); 1576 struct inode *inode = file_inode(filp);
1522 struct f2fs_inode_info *fi = F2FS_I(inode);
1523 unsigned int flags; 1577 unsigned int flags;
1524 unsigned int oldflags;
1525 int ret; 1578 int ret;
1526 1579
1527 if (!inode_owner_or_capable(inode)) 1580 if (!inode_owner_or_capable(inode))
@@ -1536,31 +1589,8 @@ static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1536 1589
1537 inode_lock(inode); 1590 inode_lock(inode);
1538 1591
1539 /* Is it quota file? Do not allow user to mess with it */ 1592 ret = __f2fs_ioc_setflags(inode, flags);
1540 if (IS_NOQUOTA(inode)) {
1541 ret = -EPERM;
1542 goto unlock_out;
1543 }
1544
1545 flags = f2fs_mask_flags(inode->i_mode, flags);
1546
1547 oldflags = fi->i_flags;
1548
1549 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1550 if (!capable(CAP_LINUX_IMMUTABLE)) {
1551 ret = -EPERM;
1552 goto unlock_out;
1553 }
1554 }
1555
1556 flags = flags & FS_FL_USER_MODIFIABLE;
1557 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1558 fi->i_flags = flags;
1559 1593
1560 inode->i_ctime = current_time(inode);
1561 f2fs_set_inode_flags(inode);
1562 f2fs_mark_inode_dirty_sync(inode, false);
1563unlock_out:
1564 inode_unlock(inode); 1594 inode_unlock(inode);
1565 mnt_drop_write_file(filp); 1595 mnt_drop_write_file(filp);
1566 return ret; 1596 return ret;
@@ -1610,10 +1640,12 @@ static int f2fs_ioc_start_atomic_write(struct file *filp)
1610 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); 1640 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
1611 if (ret) { 1641 if (ret) {
1612 clear_inode_flag(inode, FI_ATOMIC_FILE); 1642 clear_inode_flag(inode, FI_ATOMIC_FILE);
1643 clear_inode_flag(inode, FI_HOT_DATA);
1613 goto out; 1644 goto out;
1614 } 1645 }
1615 1646
1616inc_stat: 1647inc_stat:
1648 F2FS_I(inode)->inmem_task = current;
1617 stat_inc_atomic_write(inode); 1649 stat_inc_atomic_write(inode);
1618 stat_update_max_atomic_write(inode); 1650 stat_update_max_atomic_write(inode);
1619out: 1651out:
@@ -1647,10 +1679,11 @@ static int f2fs_ioc_commit_atomic_write(struct file *filp)
1647 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); 1679 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1648 if (!ret) { 1680 if (!ret) {
1649 clear_inode_flag(inode, FI_ATOMIC_FILE); 1681 clear_inode_flag(inode, FI_ATOMIC_FILE);
1682 clear_inode_flag(inode, FI_HOT_DATA);
1650 stat_dec_atomic_write(inode); 1683 stat_dec_atomic_write(inode);
1651 } 1684 }
1652 } else { 1685 } else {
1653 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); 1686 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
1654 } 1687 }
1655err_out: 1688err_out:
1656 inode_unlock(inode); 1689 inode_unlock(inode);
@@ -1786,7 +1819,7 @@ static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1786 f2fs_stop_checkpoint(sbi, false); 1819 f2fs_stop_checkpoint(sbi, false);
1787 break; 1820 break;
1788 case F2FS_GOING_DOWN_METAFLUSH: 1821 case F2FS_GOING_DOWN_METAFLUSH:
1789 sync_meta_pages(sbi, META, LONG_MAX); 1822 sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
1790 f2fs_stop_checkpoint(sbi, false); 1823 f2fs_stop_checkpoint(sbi, false);
1791 break; 1824 break;
1792 default: 1825 default:
@@ -2043,7 +2076,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2043 */ 2076 */
2044 while (map.m_lblk < pg_end) { 2077 while (map.m_lblk < pg_end) {
2045 map.m_len = pg_end - map.m_lblk; 2078 map.m_len = pg_end - map.m_lblk;
2046 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ); 2079 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2047 if (err) 2080 if (err)
2048 goto out; 2081 goto out;
2049 2082
@@ -2085,7 +2118,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2085 2118
2086do_map: 2119do_map:
2087 map.m_len = pg_end - map.m_lblk; 2120 map.m_len = pg_end - map.m_lblk;
2088 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ); 2121 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2089 if (err) 2122 if (err)
2090 goto clear_out; 2123 goto clear_out;
2091 2124
@@ -2384,6 +2417,210 @@ out:
2384 return ret; 2417 return ret;
2385} 2418}
2386 2419
2420static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2421{
2422 struct inode *inode = file_inode(filp);
2423 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2424
2425 /* Must validate to set it with SQLite behavior in Android. */
2426 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2427
2428 return put_user(sb_feature, (u32 __user *)arg);
2429}
2430
2431#ifdef CONFIG_QUOTA
2432static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2433{
2434 struct inode *inode = file_inode(filp);
2435 struct f2fs_inode_info *fi = F2FS_I(inode);
2436 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2437 struct super_block *sb = sbi->sb;
2438 struct dquot *transfer_to[MAXQUOTAS] = {};
2439 struct page *ipage;
2440 kprojid_t kprojid;
2441 int err;
2442
2443 if (!f2fs_sb_has_project_quota(sb)) {
2444 if (projid != F2FS_DEF_PROJID)
2445 return -EOPNOTSUPP;
2446 else
2447 return 0;
2448 }
2449
2450 if (!f2fs_has_extra_attr(inode))
2451 return -EOPNOTSUPP;
2452
2453 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2454
2455 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2456 return 0;
2457
2458 err = mnt_want_write_file(filp);
2459 if (err)
2460 return err;
2461
2462 err = -EPERM;
2463 inode_lock(inode);
2464
2465 /* Is it quota file? Do not allow user to mess with it */
2466 if (IS_NOQUOTA(inode))
2467 goto out_unlock;
2468
2469 ipage = get_node_page(sbi, inode->i_ino);
2470 if (IS_ERR(ipage)) {
2471 err = PTR_ERR(ipage);
2472 goto out_unlock;
2473 }
2474
2475 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2476 i_projid)) {
2477 err = -EOVERFLOW;
2478 f2fs_put_page(ipage, 1);
2479 goto out_unlock;
2480 }
2481 f2fs_put_page(ipage, 1);
2482
2483 dquot_initialize(inode);
2484
2485 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2486 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2487 err = __dquot_transfer(inode, transfer_to);
2488 dqput(transfer_to[PRJQUOTA]);
2489 if (err)
2490 goto out_dirty;
2491 }
2492
2493 F2FS_I(inode)->i_projid = kprojid;
2494 inode->i_ctime = current_time(inode);
2495out_dirty:
2496 f2fs_mark_inode_dirty_sync(inode, true);
2497out_unlock:
2498 inode_unlock(inode);
2499 mnt_drop_write_file(filp);
2500 return err;
2501}
2502#else
2503static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2504{
2505 if (projid != F2FS_DEF_PROJID)
2506 return -EOPNOTSUPP;
2507 return 0;
2508}
2509#endif
2510
2511/* Transfer internal flags to xflags */
2512static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2513{
2514 __u32 xflags = 0;
2515
2516 if (iflags & FS_SYNC_FL)
2517 xflags |= FS_XFLAG_SYNC;
2518 if (iflags & FS_IMMUTABLE_FL)
2519 xflags |= FS_XFLAG_IMMUTABLE;
2520 if (iflags & FS_APPEND_FL)
2521 xflags |= FS_XFLAG_APPEND;
2522 if (iflags & FS_NODUMP_FL)
2523 xflags |= FS_XFLAG_NODUMP;
2524 if (iflags & FS_NOATIME_FL)
2525 xflags |= FS_XFLAG_NOATIME;
2526 if (iflags & FS_PROJINHERIT_FL)
2527 xflags |= FS_XFLAG_PROJINHERIT;
2528 return xflags;
2529}
2530
2531#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2532 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2533 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2534
2535/* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */
2536#define F2FS_FL_XFLAG_VISIBLE (FS_SYNC_FL | \
2537 FS_IMMUTABLE_FL | \
2538 FS_APPEND_FL | \
2539 FS_NODUMP_FL | \
2540 FS_NOATIME_FL | \
2541 FS_PROJINHERIT_FL)
2542
2543/* Transfer xflags flags to internal */
2544static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2545{
2546 unsigned long iflags = 0;
2547
2548 if (xflags & FS_XFLAG_SYNC)
2549 iflags |= FS_SYNC_FL;
2550 if (xflags & FS_XFLAG_IMMUTABLE)
2551 iflags |= FS_IMMUTABLE_FL;
2552 if (xflags & FS_XFLAG_APPEND)
2553 iflags |= FS_APPEND_FL;
2554 if (xflags & FS_XFLAG_NODUMP)
2555 iflags |= FS_NODUMP_FL;
2556 if (xflags & FS_XFLAG_NOATIME)
2557 iflags |= FS_NOATIME_FL;
2558 if (xflags & FS_XFLAG_PROJINHERIT)
2559 iflags |= FS_PROJINHERIT_FL;
2560
2561 return iflags;
2562}
2563
2564static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2565{
2566 struct inode *inode = file_inode(filp);
2567 struct f2fs_inode_info *fi = F2FS_I(inode);
2568 struct fsxattr fa;
2569
2570 memset(&fa, 0, sizeof(struct fsxattr));
2571 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
2572 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL));
2573
2574 if (f2fs_sb_has_project_quota(inode->i_sb))
2575 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2576 fi->i_projid);
2577
2578 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2579 return -EFAULT;
2580 return 0;
2581}
2582
2583static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2584{
2585 struct inode *inode = file_inode(filp);
2586 struct f2fs_inode_info *fi = F2FS_I(inode);
2587 struct fsxattr fa;
2588 unsigned int flags;
2589 int err;
2590
2591 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2592 return -EFAULT;
2593
2594 /* Make sure caller has proper permission */
2595 if (!inode_owner_or_capable(inode))
2596 return -EACCES;
2597
2598 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2599 return -EOPNOTSUPP;
2600
2601 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2602 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2603 return -EOPNOTSUPP;
2604
2605 err = mnt_want_write_file(filp);
2606 if (err)
2607 return err;
2608
2609 inode_lock(inode);
2610 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2611 (flags & F2FS_FL_XFLAG_VISIBLE);
2612 err = __f2fs_ioc_setflags(inode, flags);
2613 inode_unlock(inode);
2614 mnt_drop_write_file(filp);
2615 if (err)
2616 return err;
2617
2618 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2619 if (err)
2620 return err;
2621
2622 return 0;
2623}
2387 2624
2388long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 2625long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2389{ 2626{
@@ -2426,6 +2663,12 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2426 return f2fs_ioc_move_range(filp, arg); 2663 return f2fs_ioc_move_range(filp, arg);
2427 case F2FS_IOC_FLUSH_DEVICE: 2664 case F2FS_IOC_FLUSH_DEVICE:
2428 return f2fs_ioc_flush_device(filp, arg); 2665 return f2fs_ioc_flush_device(filp, arg);
2666 case F2FS_IOC_GET_FEATURES:
2667 return f2fs_ioc_get_features(filp, arg);
2668 case F2FS_IOC_FSGETXATTR:
2669 return f2fs_ioc_fsgetxattr(filp, arg);
2670 case F2FS_IOC_FSSETXATTR:
2671 return f2fs_ioc_fssetxattr(filp, arg);
2429 default: 2672 default:
2430 return -ENOTTY; 2673 return -ENOTTY;
2431 } 2674 }
@@ -2455,6 +2698,9 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2455 ret = __generic_file_write_iter(iocb, from); 2698 ret = __generic_file_write_iter(iocb, from);
2456 blk_finish_plug(&plug); 2699 blk_finish_plug(&plug);
2457 clear_inode_flag(inode, FI_NO_PREALLOC); 2700 clear_inode_flag(inode, FI_NO_PREALLOC);
2701
2702 if (ret > 0)
2703 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
2458 } 2704 }
2459 inode_unlock(inode); 2705 inode_unlock(inode);
2460 2706
@@ -2491,6 +2737,9 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2491 case F2FS_IOC_DEFRAGMENT: 2737 case F2FS_IOC_DEFRAGMENT:
2492 case F2FS_IOC_MOVE_RANGE: 2738 case F2FS_IOC_MOVE_RANGE:
2493 case F2FS_IOC_FLUSH_DEVICE: 2739 case F2FS_IOC_FLUSH_DEVICE:
2740 case F2FS_IOC_GET_FEATURES:
2741 case F2FS_IOC_FSGETXATTR:
2742 case F2FS_IOC_FSSETXATTR:
2494 break; 2743 break;
2495 default: 2744 default:
2496 return -ENOIOCTLCMD; 2745 return -ENOIOCTLCMD;
@@ -2506,6 +2755,7 @@ const struct file_operations f2fs_file_operations = {
2506 .open = f2fs_file_open, 2755 .open = f2fs_file_open,
2507 .release = f2fs_release_file, 2756 .release = f2fs_release_file,
2508 .mmap = f2fs_file_mmap, 2757 .mmap = f2fs_file_mmap,
2758 .flush = f2fs_file_flush,
2509 .fsync = f2fs_sync_file, 2759 .fsync = f2fs_sync_file,
2510 .fallocate = f2fs_fallocate, 2760 .fallocate = f2fs_fallocate,
2511 .unlocked_ioctl = f2fs_ioctl, 2761 .unlocked_ioctl = f2fs_ioctl,
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index fa3d2e2df8e7..bfe6a8ccc3a0 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -28,16 +28,21 @@ static int gc_thread_func(void *data)
28 struct f2fs_sb_info *sbi = data; 28 struct f2fs_sb_info *sbi = data;
29 struct f2fs_gc_kthread *gc_th = sbi->gc_thread; 29 struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
30 wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head; 30 wait_queue_head_t *wq = &sbi->gc_thread->gc_wait_queue_head;
31 long wait_ms; 31 unsigned int wait_ms;
32 32
33 wait_ms = gc_th->min_sleep_time; 33 wait_ms = gc_th->min_sleep_time;
34 34
35 set_freezable(); 35 set_freezable();
36 do { 36 do {
37 wait_event_interruptible_timeout(*wq, 37 wait_event_interruptible_timeout(*wq,
38 kthread_should_stop() || freezing(current), 38 kthread_should_stop() || freezing(current) ||
39 gc_th->gc_wake,
39 msecs_to_jiffies(wait_ms)); 40 msecs_to_jiffies(wait_ms));
40 41
42 /* give it a try one time */
43 if (gc_th->gc_wake)
44 gc_th->gc_wake = 0;
45
41 if (try_to_freeze()) 46 if (try_to_freeze())
42 continue; 47 continue;
43 if (kthread_should_stop()) 48 if (kthread_should_stop())
@@ -55,6 +60,9 @@ static int gc_thread_func(void *data)
55 } 60 }
56#endif 61#endif
57 62
63 if (!sb_start_write_trylock(sbi->sb))
64 continue;
65
58 /* 66 /*
59 * [GC triggering condition] 67 * [GC triggering condition]
60 * 0. GC is not conducted currently. 68 * 0. GC is not conducted currently.
@@ -69,19 +77,24 @@ static int gc_thread_func(void *data)
69 * So, I'd like to wait some time to collect dirty segments. 77 * So, I'd like to wait some time to collect dirty segments.
70 */ 78 */
71 if (!mutex_trylock(&sbi->gc_mutex)) 79 if (!mutex_trylock(&sbi->gc_mutex))
72 continue; 80 goto next;
81
82 if (gc_th->gc_urgent) {
83 wait_ms = gc_th->urgent_sleep_time;
84 goto do_gc;
85 }
73 86
74 if (!is_idle(sbi)) { 87 if (!is_idle(sbi)) {
75 increase_sleep_time(gc_th, &wait_ms); 88 increase_sleep_time(gc_th, &wait_ms);
76 mutex_unlock(&sbi->gc_mutex); 89 mutex_unlock(&sbi->gc_mutex);
77 continue; 90 goto next;
78 } 91 }
79 92
80 if (has_enough_invalid_blocks(sbi)) 93 if (has_enough_invalid_blocks(sbi))
81 decrease_sleep_time(gc_th, &wait_ms); 94 decrease_sleep_time(gc_th, &wait_ms);
82 else 95 else
83 increase_sleep_time(gc_th, &wait_ms); 96 increase_sleep_time(gc_th, &wait_ms);
84 97do_gc:
85 stat_inc_bggc_count(sbi); 98 stat_inc_bggc_count(sbi);
86 99
87 /* if return value is not zero, no victim was selected */ 100 /* if return value is not zero, no victim was selected */
@@ -93,6 +106,8 @@ static int gc_thread_func(void *data)
93 106
94 /* balancing f2fs's metadata periodically */ 107 /* balancing f2fs's metadata periodically */
95 f2fs_balance_fs_bg(sbi); 108 f2fs_balance_fs_bg(sbi);
109next:
110 sb_end_write(sbi->sb);
96 111
97 } while (!kthread_should_stop()); 112 } while (!kthread_should_stop());
98 return 0; 113 return 0;
@@ -110,11 +125,14 @@ int start_gc_thread(struct f2fs_sb_info *sbi)
110 goto out; 125 goto out;
111 } 126 }
112 127
128 gc_th->urgent_sleep_time = DEF_GC_THREAD_URGENT_SLEEP_TIME;
113 gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME; 129 gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME;
114 gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME; 130 gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME;
115 gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME; 131 gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME;
116 132
117 gc_th->gc_idle = 0; 133 gc_th->gc_idle = 0;
134 gc_th->gc_urgent = 0;
135 gc_th->gc_wake= 0;
118 136
119 sbi->gc_thread = gc_th; 137 sbi->gc_thread = gc_th;
120 init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head); 138 init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
@@ -259,20 +277,11 @@ static unsigned int get_greedy_cost(struct f2fs_sb_info *sbi,
259 valid_blocks * 2 : valid_blocks; 277 valid_blocks * 2 : valid_blocks;
260} 278}
261 279
262static unsigned int get_ssr_cost(struct f2fs_sb_info *sbi,
263 unsigned int segno)
264{
265 struct seg_entry *se = get_seg_entry(sbi, segno);
266
267 return se->ckpt_valid_blocks > se->valid_blocks ?
268 se->ckpt_valid_blocks : se->valid_blocks;
269}
270
271static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi, 280static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi,
272 unsigned int segno, struct victim_sel_policy *p) 281 unsigned int segno, struct victim_sel_policy *p)
273{ 282{
274 if (p->alloc_mode == SSR) 283 if (p->alloc_mode == SSR)
275 return get_ssr_cost(sbi, segno); 284 return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
276 285
277 /* alloc_mode == LFS */ 286 /* alloc_mode == LFS */
278 if (p->gc_mode == GC_GREEDY) 287 if (p->gc_mode == GC_GREEDY)
@@ -582,7 +591,7 @@ static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
582 } 591 }
583 592
584 *nofs = ofs_of_node(node_page); 593 *nofs = ofs_of_node(node_page);
585 source_blkaddr = datablock_addr(node_page, ofs_in_node); 594 source_blkaddr = datablock_addr(NULL, node_page, ofs_in_node);
586 f2fs_put_page(node_page, 1); 595 f2fs_put_page(node_page, 1);
587 596
588 if (source_blkaddr != blkaddr) 597 if (source_blkaddr != blkaddr)
@@ -590,8 +599,12 @@ static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
590 return true; 599 return true;
591} 600}
592 601
593static void move_encrypted_block(struct inode *inode, block_t bidx, 602/*
594 unsigned int segno, int off) 603 * Move data block via META_MAPPING while keeping locked data page.
604 * This can be used to move blocks, aka LBAs, directly on disk.
605 */
606static void move_data_block(struct inode *inode, block_t bidx,
607 unsigned int segno, int off)
595{ 608{
596 struct f2fs_io_info fio = { 609 struct f2fs_io_info fio = {
597 .sbi = F2FS_I_SB(inode), 610 .sbi = F2FS_I_SB(inode),
@@ -684,6 +697,8 @@ static void move_encrypted_block(struct inode *inode, block_t bidx,
684 fio.new_blkaddr = newaddr; 697 fio.new_blkaddr = newaddr;
685 f2fs_submit_page_write(&fio); 698 f2fs_submit_page_write(&fio);
686 699
700 f2fs_update_iostat(fio.sbi, FS_GC_DATA_IO, F2FS_BLKSIZE);
701
687 f2fs_update_data_blkaddr(&dn, newaddr); 702 f2fs_update_data_blkaddr(&dn, newaddr);
688 set_inode_flag(inode, FI_APPEND_WRITE); 703 set_inode_flag(inode, FI_APPEND_WRITE);
689 if (page->index == 0) 704 if (page->index == 0)
@@ -731,6 +746,7 @@ static void move_data_page(struct inode *inode, block_t bidx, int gc_type,
731 .page = page, 746 .page = page,
732 .encrypted_page = NULL, 747 .encrypted_page = NULL,
733 .need_lock = LOCK_REQ, 748 .need_lock = LOCK_REQ,
749 .io_type = FS_GC_DATA_IO,
734 }; 750 };
735 bool is_dirty = PageDirty(page); 751 bool is_dirty = PageDirty(page);
736 int err; 752 int err;
@@ -819,8 +835,7 @@ next_step:
819 continue; 835 continue;
820 836
821 /* if encrypted inode, let's go phase 3 */ 837 /* if encrypted inode, let's go phase 3 */
822 if (f2fs_encrypted_inode(inode) && 838 if (f2fs_encrypted_file(inode)) {
823 S_ISREG(inode->i_mode)) {
824 add_gc_inode(gc_list, inode); 839 add_gc_inode(gc_list, inode);
825 continue; 840 continue;
826 } 841 }
@@ -854,14 +869,18 @@ next_step:
854 continue; 869 continue;
855 } 870 }
856 locked = true; 871 locked = true;
872
873 /* wait for all inflight aio data */
874 inode_dio_wait(inode);
857 } 875 }
858 876
859 start_bidx = start_bidx_of_node(nofs, inode) 877 start_bidx = start_bidx_of_node(nofs, inode)
860 + ofs_in_node; 878 + ofs_in_node;
861 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) 879 if (f2fs_encrypted_file(inode))
862 move_encrypted_block(inode, start_bidx, segno, off); 880 move_data_block(inode, start_bidx, segno, off);
863 else 881 else
864 move_data_page(inode, start_bidx, gc_type, segno, off); 882 move_data_page(inode, start_bidx, gc_type,
883 segno, off);
865 884
866 if (locked) { 885 if (locked) {
867 up_write(&fi->dio_rwsem[WRITE]); 886 up_write(&fi->dio_rwsem[WRITE]);
@@ -898,7 +917,7 @@ static int do_garbage_collect(struct f2fs_sb_info *sbi,
898 struct blk_plug plug; 917 struct blk_plug plug;
899 unsigned int segno = start_segno; 918 unsigned int segno = start_segno;
900 unsigned int end_segno = start_segno + sbi->segs_per_sec; 919 unsigned int end_segno = start_segno + sbi->segs_per_sec;
901 int sec_freed = 0; 920 int seg_freed = 0;
902 unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ? 921 unsigned char type = IS_DATASEG(get_seg_entry(sbi, segno)->type) ?
903 SUM_TYPE_DATA : SUM_TYPE_NODE; 922 SUM_TYPE_DATA : SUM_TYPE_NODE;
904 923
@@ -944,6 +963,10 @@ static int do_garbage_collect(struct f2fs_sb_info *sbi,
944 gc_type); 963 gc_type);
945 964
946 stat_inc_seg_count(sbi, type, gc_type); 965 stat_inc_seg_count(sbi, type, gc_type);
966
967 if (gc_type == FG_GC &&
968 get_valid_blocks(sbi, segno, false) == 0)
969 seg_freed++;
947next: 970next:
948 f2fs_put_page(sum_page, 0); 971 f2fs_put_page(sum_page, 0);
949 } 972 }
@@ -954,21 +977,17 @@ next:
954 977
955 blk_finish_plug(&plug); 978 blk_finish_plug(&plug);
956 979
957 if (gc_type == FG_GC &&
958 get_valid_blocks(sbi, start_segno, true) == 0)
959 sec_freed = 1;
960
961 stat_inc_call_count(sbi->stat_info); 980 stat_inc_call_count(sbi->stat_info);
962 981
963 return sec_freed; 982 return seg_freed;
964} 983}
965 984
966int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, 985int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
967 bool background, unsigned int segno) 986 bool background, unsigned int segno)
968{ 987{
969 int gc_type = sync ? FG_GC : BG_GC; 988 int gc_type = sync ? FG_GC : BG_GC;
970 int sec_freed = 0; 989 int sec_freed = 0, seg_freed = 0, total_freed = 0;
971 int ret; 990 int ret = 0;
972 struct cp_control cpc; 991 struct cp_control cpc;
973 unsigned int init_segno = segno; 992 unsigned int init_segno = segno;
974 struct gc_inode_list gc_list = { 993 struct gc_inode_list gc_list = {
@@ -976,6 +995,15 @@ int f2fs_gc(struct f2fs_sb_info *sbi, bool sync,
976 .iroot = RADIX_TREE_INIT(GFP_NOFS), 995 .iroot = RADIX_TREE_INIT(GFP_NOFS),
977 }; 996 };
978 997
998 trace_f2fs_gc_begin(sbi->sb, sync, background,
999 get_pages(sbi, F2FS_DIRTY_NODES),
1000 get_pages(sbi, F2FS_DIRTY_DENTS),
1001 get_pages(sbi, F2FS_DIRTY_IMETA),
1002 free_sections(sbi),
1003 free_segments(sbi),
1004 reserved_segments(sbi),
1005 prefree_segments(sbi));
1006
979 cpc.reason = __get_cp_reason(sbi); 1007 cpc.reason = __get_cp_reason(sbi);
980gc_more: 1008gc_more:
981 if (unlikely(!(sbi->sb->s_flags & MS_ACTIVE))) { 1009 if (unlikely(!(sbi->sb->s_flags & MS_ACTIVE))) {
@@ -1002,17 +1030,20 @@ gc_more:
1002 gc_type = FG_GC; 1030 gc_type = FG_GC;
1003 } 1031 }
1004 1032
1005 ret = -EINVAL;
1006 /* f2fs_balance_fs doesn't need to do BG_GC in critical path. */ 1033 /* f2fs_balance_fs doesn't need to do BG_GC in critical path. */
1007 if (gc_type == BG_GC && !background) 1034 if (gc_type == BG_GC && !background) {
1035 ret = -EINVAL;
1008 goto stop; 1036 goto stop;
1009 if (!__get_victim(sbi, &segno, gc_type)) 1037 }
1038 if (!__get_victim(sbi, &segno, gc_type)) {
1039 ret = -ENODATA;
1010 goto stop; 1040 goto stop;
1011 ret = 0; 1041 }
1012 1042
1013 if (do_garbage_collect(sbi, segno, &gc_list, gc_type) && 1043 seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type);
1014 gc_type == FG_GC) 1044 if (gc_type == FG_GC && seg_freed == sbi->segs_per_sec)
1015 sec_freed++; 1045 sec_freed++;
1046 total_freed += seg_freed;
1016 1047
1017 if (gc_type == FG_GC) 1048 if (gc_type == FG_GC)
1018 sbi->cur_victim_sec = NULL_SEGNO; 1049 sbi->cur_victim_sec = NULL_SEGNO;
@@ -1029,6 +1060,16 @@ gc_more:
1029stop: 1060stop:
1030 SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0; 1061 SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
1031 SIT_I(sbi)->last_victim[FLUSH_DEVICE] = init_segno; 1062 SIT_I(sbi)->last_victim[FLUSH_DEVICE] = init_segno;
1063
1064 trace_f2fs_gc_end(sbi->sb, ret, total_freed, sec_freed,
1065 get_pages(sbi, F2FS_DIRTY_NODES),
1066 get_pages(sbi, F2FS_DIRTY_DENTS),
1067 get_pages(sbi, F2FS_DIRTY_IMETA),
1068 free_sections(sbi),
1069 free_segments(sbi),
1070 reserved_segments(sbi),
1071 prefree_segments(sbi));
1072
1032 mutex_unlock(&sbi->gc_mutex); 1073 mutex_unlock(&sbi->gc_mutex);
1033 1074
1034 put_gc_inode(&gc_list); 1075 put_gc_inode(&gc_list);
diff --git a/fs/f2fs/gc.h b/fs/f2fs/gc.h
index a993967dcdb9..9325191fab2d 100644
--- a/fs/f2fs/gc.h
+++ b/fs/f2fs/gc.h
@@ -13,6 +13,7 @@
13 * whether IO subsystem is idle 13 * whether IO subsystem is idle
14 * or not 14 * or not
15 */ 15 */
16#define DEF_GC_THREAD_URGENT_SLEEP_TIME 500 /* 500 ms */
16#define DEF_GC_THREAD_MIN_SLEEP_TIME 30000 /* milliseconds */ 17#define DEF_GC_THREAD_MIN_SLEEP_TIME 30000 /* milliseconds */
17#define DEF_GC_THREAD_MAX_SLEEP_TIME 60000 18#define DEF_GC_THREAD_MAX_SLEEP_TIME 60000
18#define DEF_GC_THREAD_NOGC_SLEEP_TIME 300000 /* wait 5 min */ 19#define DEF_GC_THREAD_NOGC_SLEEP_TIME 300000 /* wait 5 min */
@@ -27,12 +28,15 @@ struct f2fs_gc_kthread {
27 wait_queue_head_t gc_wait_queue_head; 28 wait_queue_head_t gc_wait_queue_head;
28 29
29 /* for gc sleep time */ 30 /* for gc sleep time */
31 unsigned int urgent_sleep_time;
30 unsigned int min_sleep_time; 32 unsigned int min_sleep_time;
31 unsigned int max_sleep_time; 33 unsigned int max_sleep_time;
32 unsigned int no_gc_sleep_time; 34 unsigned int no_gc_sleep_time;
33 35
34 /* for changing gc mode */ 36 /* for changing gc mode */
35 unsigned int gc_idle; 37 unsigned int gc_idle;
38 unsigned int gc_urgent;
39 unsigned int gc_wake;
36}; 40};
37 41
38struct gc_inode_list { 42struct gc_inode_list {
@@ -65,25 +69,32 @@ static inline block_t limit_free_user_blocks(struct f2fs_sb_info *sbi)
65} 69}
66 70
67static inline void increase_sleep_time(struct f2fs_gc_kthread *gc_th, 71static inline void increase_sleep_time(struct f2fs_gc_kthread *gc_th,
68 long *wait) 72 unsigned int *wait)
69{ 73{
74 unsigned int min_time = gc_th->min_sleep_time;
75 unsigned int max_time = gc_th->max_sleep_time;
76
70 if (*wait == gc_th->no_gc_sleep_time) 77 if (*wait == gc_th->no_gc_sleep_time)
71 return; 78 return;
72 79
73 *wait += gc_th->min_sleep_time; 80 if ((long long)*wait + (long long)min_time > (long long)max_time)
74 if (*wait > gc_th->max_sleep_time) 81 *wait = max_time;
75 *wait = gc_th->max_sleep_time; 82 else
83 *wait += min_time;
76} 84}
77 85
78static inline void decrease_sleep_time(struct f2fs_gc_kthread *gc_th, 86static inline void decrease_sleep_time(struct f2fs_gc_kthread *gc_th,
79 long *wait) 87 unsigned int *wait)
80{ 88{
89 unsigned int min_time = gc_th->min_sleep_time;
90
81 if (*wait == gc_th->no_gc_sleep_time) 91 if (*wait == gc_th->no_gc_sleep_time)
82 *wait = gc_th->max_sleep_time; 92 *wait = gc_th->max_sleep_time;
83 93
84 *wait -= gc_th->min_sleep_time; 94 if ((long long)*wait - (long long)min_time < (long long)min_time)
85 if (*wait <= gc_th->min_sleep_time) 95 *wait = min_time;
86 *wait = gc_th->min_sleep_time; 96 else
97 *wait -= min_time;
87} 98}
88 99
89static inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi) 100static inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi)
diff --git a/fs/f2fs/inline.c b/fs/f2fs/inline.c
index e0fd4376e6fb..8322e4e7bb3f 100644
--- a/fs/f2fs/inline.c
+++ b/fs/f2fs/inline.c
@@ -22,10 +22,10 @@ bool f2fs_may_inline_data(struct inode *inode)
22 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode)) 22 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
23 return false; 23 return false;
24 24
25 if (i_size_read(inode) > MAX_INLINE_DATA) 25 if (i_size_read(inode) > MAX_INLINE_DATA(inode))
26 return false; 26 return false;
27 27
28 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) 28 if (f2fs_encrypted_file(inode))
29 return false; 29 return false;
30 30
31 return true; 31 return true;
@@ -44,6 +44,7 @@ bool f2fs_may_inline_dentry(struct inode *inode)
44 44
45void read_inline_data(struct page *page, struct page *ipage) 45void read_inline_data(struct page *page, struct page *ipage)
46{ 46{
47 struct inode *inode = page->mapping->host;
47 void *src_addr, *dst_addr; 48 void *src_addr, *dst_addr;
48 49
49 if (PageUptodate(page)) 50 if (PageUptodate(page))
@@ -51,12 +52,12 @@ void read_inline_data(struct page *page, struct page *ipage)
51 52
52 f2fs_bug_on(F2FS_P_SB(page), page->index); 53 f2fs_bug_on(F2FS_P_SB(page), page->index);
53 54
54 zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE); 55 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
55 56
56 /* Copy the whole inline data block */ 57 /* Copy the whole inline data block */
57 src_addr = inline_data_addr(ipage); 58 src_addr = inline_data_addr(inode, ipage);
58 dst_addr = kmap_atomic(page); 59 dst_addr = kmap_atomic(page);
59 memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 60 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
60 flush_dcache_page(page); 61 flush_dcache_page(page);
61 kunmap_atomic(dst_addr); 62 kunmap_atomic(dst_addr);
62 if (!PageUptodate(page)) 63 if (!PageUptodate(page))
@@ -67,13 +68,13 @@ void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from)
67{ 68{
68 void *addr; 69 void *addr;
69 70
70 if (from >= MAX_INLINE_DATA) 71 if (from >= MAX_INLINE_DATA(inode))
71 return; 72 return;
72 73
73 addr = inline_data_addr(ipage); 74 addr = inline_data_addr(inode, ipage);
74 75
75 f2fs_wait_on_page_writeback(ipage, NODE, true); 76 f2fs_wait_on_page_writeback(ipage, NODE, true);
76 memset(addr + from, 0, MAX_INLINE_DATA - from); 77 memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
77 set_page_dirty(ipage); 78 set_page_dirty(ipage);
78 79
79 if (from == 0) 80 if (from == 0)
@@ -116,6 +117,7 @@ int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
116 .op_flags = REQ_SYNC | REQ_PRIO, 117 .op_flags = REQ_SYNC | REQ_PRIO,
117 .page = page, 118 .page = page,
118 .encrypted_page = NULL, 119 .encrypted_page = NULL,
120 .io_type = FS_DATA_IO,
119 }; 121 };
120 int dirty, err; 122 int dirty, err;
121 123
@@ -200,6 +202,8 @@ int f2fs_write_inline_data(struct inode *inode, struct page *page)
200{ 202{
201 void *src_addr, *dst_addr; 203 void *src_addr, *dst_addr;
202 struct dnode_of_data dn; 204 struct dnode_of_data dn;
205 struct address_space *mapping = page_mapping(page);
206 unsigned long flags;
203 int err; 207 int err;
204 208
205 set_new_dnode(&dn, inode, NULL, NULL, 0); 209 set_new_dnode(&dn, inode, NULL, NULL, 0);
@@ -216,11 +220,16 @@ int f2fs_write_inline_data(struct inode *inode, struct page *page)
216 220
217 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true); 221 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true);
218 src_addr = kmap_atomic(page); 222 src_addr = kmap_atomic(page);
219 dst_addr = inline_data_addr(dn.inode_page); 223 dst_addr = inline_data_addr(inode, dn.inode_page);
220 memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 224 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
221 kunmap_atomic(src_addr); 225 kunmap_atomic(src_addr);
222 set_page_dirty(dn.inode_page); 226 set_page_dirty(dn.inode_page);
223 227
228 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
224 set_inode_flag(inode, FI_APPEND_WRITE); 233 set_inode_flag(inode, FI_APPEND_WRITE);
225 set_inode_flag(inode, FI_DATA_EXIST); 234 set_inode_flag(inode, FI_DATA_EXIST);
226 235
@@ -255,9 +264,9 @@ process_inline:
255 264
256 f2fs_wait_on_page_writeback(ipage, NODE, true); 265 f2fs_wait_on_page_writeback(ipage, NODE, true);
257 266
258 src_addr = inline_data_addr(npage); 267 src_addr = inline_data_addr(inode, npage);
259 dst_addr = inline_data_addr(ipage); 268 dst_addr = inline_data_addr(inode, ipage);
260 memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 269 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
261 270
262 set_inode_flag(inode, FI_INLINE_DATA); 271 set_inode_flag(inode, FI_INLINE_DATA);
263 set_inode_flag(inode, FI_DATA_EXIST); 272 set_inode_flag(inode, FI_DATA_EXIST);
@@ -285,11 +294,11 @@ struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
285 struct fscrypt_name *fname, struct page **res_page) 294 struct fscrypt_name *fname, struct page **res_page)
286{ 295{
287 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); 296 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
288 struct f2fs_inline_dentry *inline_dentry;
289 struct qstr name = FSTR_TO_QSTR(&fname->disk_name); 297 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
290 struct f2fs_dir_entry *de; 298 struct f2fs_dir_entry *de;
291 struct f2fs_dentry_ptr d; 299 struct f2fs_dentry_ptr d;
292 struct page *ipage; 300 struct page *ipage;
301 void *inline_dentry;
293 f2fs_hash_t namehash; 302 f2fs_hash_t namehash;
294 303
295 ipage = get_node_page(sbi, dir->i_ino); 304 ipage = get_node_page(sbi, dir->i_ino);
@@ -300,9 +309,9 @@ struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
300 309
301 namehash = f2fs_dentry_hash(&name, fname); 310 namehash = f2fs_dentry_hash(&name, fname);
302 311
303 inline_dentry = inline_data_addr(ipage); 312 inline_dentry = inline_data_addr(dir, ipage);
304 313
305 make_dentry_ptr_inline(NULL, &d, inline_dentry); 314 make_dentry_ptr_inline(dir, &d, inline_dentry);
306 de = find_target_dentry(fname, namehash, NULL, &d); 315 de = find_target_dentry(fname, namehash, NULL, &d);
307 unlock_page(ipage); 316 unlock_page(ipage);
308 if (de) 317 if (de)
@@ -316,19 +325,19 @@ struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
316int make_empty_inline_dir(struct inode *inode, struct inode *parent, 325int make_empty_inline_dir(struct inode *inode, struct inode *parent,
317 struct page *ipage) 326 struct page *ipage)
318{ 327{
319 struct f2fs_inline_dentry *inline_dentry;
320 struct f2fs_dentry_ptr d; 328 struct f2fs_dentry_ptr d;
329 void *inline_dentry;
321 330
322 inline_dentry = inline_data_addr(ipage); 331 inline_dentry = inline_data_addr(inode, ipage);
323 332
324 make_dentry_ptr_inline(NULL, &d, inline_dentry); 333 make_dentry_ptr_inline(inode, &d, inline_dentry);
325 do_make_empty_dir(inode, parent, &d); 334 do_make_empty_dir(inode, parent, &d);
326 335
327 set_page_dirty(ipage); 336 set_page_dirty(ipage);
328 337
329 /* update i_size to MAX_INLINE_DATA */ 338 /* update i_size to MAX_INLINE_DATA */
330 if (i_size_read(inode) < MAX_INLINE_DATA) 339 if (i_size_read(inode) < MAX_INLINE_DATA(inode))
331 f2fs_i_size_write(inode, MAX_INLINE_DATA); 340 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
332 return 0; 341 return 0;
333} 342}
334 343
@@ -337,11 +346,12 @@ int make_empty_inline_dir(struct inode *inode, struct inode *parent,
337 * release ipage in this function. 346 * release ipage in this function.
338 */ 347 */
339static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage, 348static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
340 struct f2fs_inline_dentry *inline_dentry) 349 void *inline_dentry)
341{ 350{
342 struct page *page; 351 struct page *page;
343 struct dnode_of_data dn; 352 struct dnode_of_data dn;
344 struct f2fs_dentry_block *dentry_blk; 353 struct f2fs_dentry_block *dentry_blk;
354 struct f2fs_dentry_ptr src, dst;
345 int err; 355 int err;
346 356
347 page = f2fs_grab_cache_page(dir->i_mapping, 0, false); 357 page = f2fs_grab_cache_page(dir->i_mapping, 0, false);
@@ -356,25 +366,24 @@ static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
356 goto out; 366 goto out;
357 367
358 f2fs_wait_on_page_writeback(page, DATA, true); 368 f2fs_wait_on_page_writeback(page, DATA, true);
359 zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE); 369 zero_user_segment(page, MAX_INLINE_DATA(dir), PAGE_SIZE);
360 370
361 dentry_blk = kmap_atomic(page); 371 dentry_blk = kmap_atomic(page);
362 372
373 make_dentry_ptr_inline(dir, &src, inline_dentry);
374 make_dentry_ptr_block(dir, &dst, dentry_blk);
375
363 /* copy data from inline dentry block to new dentry block */ 376 /* copy data from inline dentry block to new dentry block */
364 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap, 377 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
365 INLINE_DENTRY_BITMAP_SIZE); 378 memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap);
366 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
367 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
368 /* 379 /*
369 * we do not need to zero out remainder part of dentry and filename 380 * we do not need to zero out remainder part of dentry and filename
370 * field, since we have used bitmap for marking the usage status of 381 * field, since we have used bitmap for marking the usage status of
371 * them, besides, we can also ignore copying/zeroing reserved space 382 * them, besides, we can also ignore copying/zeroing reserved space
372 * of dentry block, because them haven't been used so far. 383 * of dentry block, because them haven't been used so far.
373 */ 384 */
374 memcpy(dentry_blk->dentry, inline_dentry->dentry, 385 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
375 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY); 386 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
376 memcpy(dentry_blk->filename, inline_dentry->filename,
377 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
378 387
379 kunmap_atomic(dentry_blk); 388 kunmap_atomic(dentry_blk);
380 if (!PageUptodate(page)) 389 if (!PageUptodate(page))
@@ -395,14 +404,13 @@ out:
395 return err; 404 return err;
396} 405}
397 406
398static int f2fs_add_inline_entries(struct inode *dir, 407static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
399 struct f2fs_inline_dentry *inline_dentry)
400{ 408{
401 struct f2fs_dentry_ptr d; 409 struct f2fs_dentry_ptr d;
402 unsigned long bit_pos = 0; 410 unsigned long bit_pos = 0;
403 int err = 0; 411 int err = 0;
404 412
405 make_dentry_ptr_inline(NULL, &d, inline_dentry); 413 make_dentry_ptr_inline(dir, &d, inline_dentry);
406 414
407 while (bit_pos < d.max) { 415 while (bit_pos < d.max) {
408 struct f2fs_dir_entry *de; 416 struct f2fs_dir_entry *de;
@@ -444,19 +452,19 @@ punch_dentry_pages:
444} 452}
445 453
446static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage, 454static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
447 struct f2fs_inline_dentry *inline_dentry) 455 void *inline_dentry)
448{ 456{
449 struct f2fs_inline_dentry *backup_dentry; 457 void *backup_dentry;
450 int err; 458 int err;
451 459
452 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir), 460 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
453 sizeof(struct f2fs_inline_dentry), GFP_F2FS_ZERO); 461 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
454 if (!backup_dentry) { 462 if (!backup_dentry) {
455 f2fs_put_page(ipage, 1); 463 f2fs_put_page(ipage, 1);
456 return -ENOMEM; 464 return -ENOMEM;
457 } 465 }
458 466
459 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA); 467 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
460 truncate_inline_inode(dir, ipage, 0); 468 truncate_inline_inode(dir, ipage, 0);
461 469
462 unlock_page(ipage); 470 unlock_page(ipage);
@@ -473,9 +481,9 @@ static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
473 return 0; 481 return 0;
474recover: 482recover:
475 lock_page(ipage); 483 lock_page(ipage);
476 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA); 484 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
477 f2fs_i_depth_write(dir, 0); 485 f2fs_i_depth_write(dir, 0);
478 f2fs_i_size_write(dir, MAX_INLINE_DATA); 486 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
479 set_page_dirty(ipage); 487 set_page_dirty(ipage);
480 f2fs_put_page(ipage, 1); 488 f2fs_put_page(ipage, 1);
481 489
@@ -484,7 +492,7 @@ recover:
484} 492}
485 493
486static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, 494static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
487 struct f2fs_inline_dentry *inline_dentry) 495 void *inline_dentry)
488{ 496{
489 if (!F2FS_I(dir)->i_dir_level) 497 if (!F2FS_I(dir)->i_dir_level)
490 return f2fs_move_inline_dirents(dir, ipage, inline_dentry); 498 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
@@ -500,7 +508,7 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
500 struct page *ipage; 508 struct page *ipage;
501 unsigned int bit_pos; 509 unsigned int bit_pos;
502 f2fs_hash_t name_hash; 510 f2fs_hash_t name_hash;
503 struct f2fs_inline_dentry *inline_dentry = NULL; 511 void *inline_dentry = NULL;
504 struct f2fs_dentry_ptr d; 512 struct f2fs_dentry_ptr d;
505 int slots = GET_DENTRY_SLOTS(new_name->len); 513 int slots = GET_DENTRY_SLOTS(new_name->len);
506 struct page *page = NULL; 514 struct page *page = NULL;
@@ -510,10 +518,11 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
510 if (IS_ERR(ipage)) 518 if (IS_ERR(ipage))
511 return PTR_ERR(ipage); 519 return PTR_ERR(ipage);
512 520
513 inline_dentry = inline_data_addr(ipage); 521 inline_dentry = inline_data_addr(dir, ipage);
514 bit_pos = room_for_filename(&inline_dentry->dentry_bitmap, 522 make_dentry_ptr_inline(dir, &d, inline_dentry);
515 slots, NR_INLINE_DENTRY); 523
516 if (bit_pos >= NR_INLINE_DENTRY) { 524 bit_pos = room_for_filename(d.bitmap, slots, d.max);
525 if (bit_pos >= d.max) {
517 err = f2fs_convert_inline_dir(dir, ipage, inline_dentry); 526 err = f2fs_convert_inline_dir(dir, ipage, inline_dentry);
518 if (err) 527 if (err)
519 return err; 528 return err;
@@ -534,7 +543,6 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
534 f2fs_wait_on_page_writeback(ipage, NODE, true); 543 f2fs_wait_on_page_writeback(ipage, NODE, true);
535 544
536 name_hash = f2fs_dentry_hash(new_name, NULL); 545 name_hash = f2fs_dentry_hash(new_name, NULL);
537 make_dentry_ptr_inline(NULL, &d, inline_dentry);
538 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos); 546 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
539 547
540 set_page_dirty(ipage); 548 set_page_dirty(ipage);
@@ -557,7 +565,8 @@ out:
557void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, 565void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
558 struct inode *dir, struct inode *inode) 566 struct inode *dir, struct inode *inode)
559{ 567{
560 struct f2fs_inline_dentry *inline_dentry; 568 struct f2fs_dentry_ptr d;
569 void *inline_dentry;
561 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 570 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
562 unsigned int bit_pos; 571 unsigned int bit_pos;
563 int i; 572 int i;
@@ -565,11 +574,12 @@ void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
565 lock_page(page); 574 lock_page(page);
566 f2fs_wait_on_page_writeback(page, NODE, true); 575 f2fs_wait_on_page_writeback(page, NODE, true);
567 576
568 inline_dentry = inline_data_addr(page); 577 inline_dentry = inline_data_addr(dir, page);
569 bit_pos = dentry - inline_dentry->dentry; 578 make_dentry_ptr_inline(dir, &d, inline_dentry);
579
580 bit_pos = dentry - d.dentry;
570 for (i = 0; i < slots; i++) 581 for (i = 0; i < slots; i++)
571 __clear_bit_le(bit_pos + i, 582 __clear_bit_le(bit_pos + i, d.bitmap);
572 &inline_dentry->dentry_bitmap);
573 583
574 set_page_dirty(page); 584 set_page_dirty(page);
575 f2fs_put_page(page, 1); 585 f2fs_put_page(page, 1);
@@ -586,20 +596,21 @@ bool f2fs_empty_inline_dir(struct inode *dir)
586 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 596 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
587 struct page *ipage; 597 struct page *ipage;
588 unsigned int bit_pos = 2; 598 unsigned int bit_pos = 2;
589 struct f2fs_inline_dentry *inline_dentry; 599 void *inline_dentry;
600 struct f2fs_dentry_ptr d;
590 601
591 ipage = get_node_page(sbi, dir->i_ino); 602 ipage = get_node_page(sbi, dir->i_ino);
592 if (IS_ERR(ipage)) 603 if (IS_ERR(ipage))
593 return false; 604 return false;
594 605
595 inline_dentry = inline_data_addr(ipage); 606 inline_dentry = inline_data_addr(dir, ipage);
596 bit_pos = find_next_bit_le(&inline_dentry->dentry_bitmap, 607 make_dentry_ptr_inline(dir, &d, inline_dentry);
597 NR_INLINE_DENTRY, 608
598 bit_pos); 609 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
599 610
600 f2fs_put_page(ipage, 1); 611 f2fs_put_page(ipage, 1);
601 612
602 if (bit_pos < NR_INLINE_DENTRY) 613 if (bit_pos < d.max)
603 return false; 614 return false;
604 615
605 return true; 616 return true;
@@ -609,25 +620,27 @@ int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
609 struct fscrypt_str *fstr) 620 struct fscrypt_str *fstr)
610{ 621{
611 struct inode *inode = file_inode(file); 622 struct inode *inode = file_inode(file);
612 struct f2fs_inline_dentry *inline_dentry = NULL;
613 struct page *ipage = NULL; 623 struct page *ipage = NULL;
614 struct f2fs_dentry_ptr d; 624 struct f2fs_dentry_ptr d;
625 void *inline_dentry = NULL;
615 int err; 626 int err;
616 627
617 if (ctx->pos == NR_INLINE_DENTRY) 628 make_dentry_ptr_inline(inode, &d, inline_dentry);
629
630 if (ctx->pos == d.max)
618 return 0; 631 return 0;
619 632
620 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 633 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
621 if (IS_ERR(ipage)) 634 if (IS_ERR(ipage))
622 return PTR_ERR(ipage); 635 return PTR_ERR(ipage);
623 636
624 inline_dentry = inline_data_addr(ipage); 637 inline_dentry = inline_data_addr(inode, ipage);
625 638
626 make_dentry_ptr_inline(inode, &d, inline_dentry); 639 make_dentry_ptr_inline(inode, &d, inline_dentry);
627 640
628 err = f2fs_fill_dentries(ctx, &d, 0, fstr); 641 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
629 if (!err) 642 if (!err)
630 ctx->pos = NR_INLINE_DENTRY; 643 ctx->pos = d.max;
631 644
632 f2fs_put_page(ipage, 1); 645 f2fs_put_page(ipage, 1);
633 return err < 0 ? err : 0; 646 return err < 0 ? err : 0;
@@ -652,7 +665,7 @@ int f2fs_inline_data_fiemap(struct inode *inode,
652 goto out; 665 goto out;
653 } 666 }
654 667
655 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode)); 668 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
656 if (start >= ilen) 669 if (start >= ilen)
657 goto out; 670 goto out;
658 if (start + len < ilen) 671 if (start + len < ilen)
@@ -661,7 +674,8 @@ int f2fs_inline_data_fiemap(struct inode *inode,
661 674
662 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni); 675 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
663 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits; 676 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
664 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage); 677 byteaddr += (char *)inline_data_addr(inode, ipage) -
678 (char *)F2FS_INODE(ipage);
665 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags); 679 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
666out: 680out:
667 f2fs_put_page(ipage, 1); 681 f2fs_put_page(ipage, 1);
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index 6cd312a17c69..50c88e37ed66 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -49,20 +49,22 @@ void f2fs_set_inode_flags(struct inode *inode)
49 49
50static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri) 50static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
51{ 51{
52 int extra_size = get_extra_isize(inode);
53
52 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 54 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
53 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 55 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
54 if (ri->i_addr[0]) 56 if (ri->i_addr[extra_size])
55 inode->i_rdev = 57 inode->i_rdev = old_decode_dev(
56 old_decode_dev(le32_to_cpu(ri->i_addr[0])); 58 le32_to_cpu(ri->i_addr[extra_size]));
57 else 59 else
58 inode->i_rdev = 60 inode->i_rdev = new_decode_dev(
59 new_decode_dev(le32_to_cpu(ri->i_addr[1])); 61 le32_to_cpu(ri->i_addr[extra_size + 1]));
60 } 62 }
61} 63}
62 64
63static bool __written_first_block(struct f2fs_inode *ri) 65static bool __written_first_block(struct f2fs_inode *ri)
64{ 66{
65 block_t addr = le32_to_cpu(ri->i_addr[0]); 67 block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
66 68
67 if (addr != NEW_ADDR && addr != NULL_ADDR) 69 if (addr != NEW_ADDR && addr != NULL_ADDR)
68 return true; 70 return true;
@@ -71,25 +73,27 @@ static bool __written_first_block(struct f2fs_inode *ri)
71 73
72static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri) 74static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
73{ 75{
76 int extra_size = get_extra_isize(inode);
77
74 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 78 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
75 if (old_valid_dev(inode->i_rdev)) { 79 if (old_valid_dev(inode->i_rdev)) {
76 ri->i_addr[0] = 80 ri->i_addr[extra_size] =
77 cpu_to_le32(old_encode_dev(inode->i_rdev)); 81 cpu_to_le32(old_encode_dev(inode->i_rdev));
78 ri->i_addr[1] = 0; 82 ri->i_addr[extra_size + 1] = 0;
79 } else { 83 } else {
80 ri->i_addr[0] = 0; 84 ri->i_addr[extra_size] = 0;
81 ri->i_addr[1] = 85 ri->i_addr[extra_size + 1] =
82 cpu_to_le32(new_encode_dev(inode->i_rdev)); 86 cpu_to_le32(new_encode_dev(inode->i_rdev));
83 ri->i_addr[2] = 0; 87 ri->i_addr[extra_size + 2] = 0;
84 } 88 }
85 } 89 }
86} 90}
87 91
88static void __recover_inline_status(struct inode *inode, struct page *ipage) 92static void __recover_inline_status(struct inode *inode, struct page *ipage)
89{ 93{
90 void *inline_data = inline_data_addr(ipage); 94 void *inline_data = inline_data_addr(inode, ipage);
91 __le32 *start = inline_data; 95 __le32 *start = inline_data;
92 __le32 *end = start + MAX_INLINE_DATA / sizeof(__le32); 96 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
93 97
94 while (start < end) { 98 while (start < end) {
95 if (*start++) { 99 if (*start++) {
@@ -104,12 +108,84 @@ static void __recover_inline_status(struct inode *inode, struct page *ipage)
104 return; 108 return;
105} 109}
106 110
111static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
112{
113 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
114 int extra_isize = le32_to_cpu(ri->i_extra_isize);
115
116 if (!f2fs_sb_has_inode_chksum(sbi->sb))
117 return false;
118
119 if (!RAW_IS_INODE(F2FS_NODE(page)) || !(ri->i_inline & F2FS_EXTRA_ATTR))
120 return false;
121
122 if (!F2FS_FITS_IN_INODE(ri, extra_isize, i_inode_checksum))
123 return false;
124
125 return true;
126}
127
128static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
129{
130 struct f2fs_node *node = F2FS_NODE(page);
131 struct f2fs_inode *ri = &node->i;
132 __le32 ino = node->footer.ino;
133 __le32 gen = ri->i_generation;
134 __u32 chksum, chksum_seed;
135 __u32 dummy_cs = 0;
136 unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
137 unsigned int cs_size = sizeof(dummy_cs);
138
139 chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
140 sizeof(ino));
141 chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
142
143 chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
144 chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
145 offset += cs_size;
146 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
147 F2FS_BLKSIZE - offset);
148 return chksum;
149}
150
151bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
152{
153 struct f2fs_inode *ri;
154 __u32 provided, calculated;
155
156 if (!f2fs_enable_inode_chksum(sbi, page) ||
157 PageDirty(page) || PageWriteback(page))
158 return true;
159
160 ri = &F2FS_NODE(page)->i;
161 provided = le32_to_cpu(ri->i_inode_checksum);
162 calculated = f2fs_inode_chksum(sbi, page);
163
164 if (provided != calculated)
165 f2fs_msg(sbi->sb, KERN_WARNING,
166 "checksum invalid, ino = %x, %x vs. %x",
167 ino_of_node(page), provided, calculated);
168
169 return provided == calculated;
170}
171
172void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
173{
174 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
175
176 if (!f2fs_enable_inode_chksum(sbi, page))
177 return;
178
179 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
180}
181
107static int do_read_inode(struct inode *inode) 182static int do_read_inode(struct inode *inode)
108{ 183{
109 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 184 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
110 struct f2fs_inode_info *fi = F2FS_I(inode); 185 struct f2fs_inode_info *fi = F2FS_I(inode);
111 struct page *node_page; 186 struct page *node_page;
112 struct f2fs_inode *ri; 187 struct f2fs_inode *ri;
188 projid_t i_projid;
113 189
114 /* Check if ino is within scope */ 190 /* Check if ino is within scope */
115 if (check_nid_range(sbi, inode->i_ino)) { 191 if (check_nid_range(sbi, inode->i_ino)) {
@@ -153,6 +229,9 @@ static int do_read_inode(struct inode *inode)
153 229
154 get_inline_info(inode, ri); 230 get_inline_info(inode, ri);
155 231
232 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
233 le16_to_cpu(ri->i_extra_isize) : 0;
234
156 /* check data exist */ 235 /* check data exist */
157 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode)) 236 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
158 __recover_inline_status(inode, node_page); 237 __recover_inline_status(inode, node_page);
@@ -166,6 +245,16 @@ static int do_read_inode(struct inode *inode)
166 if (!need_inode_block_update(sbi, inode->i_ino)) 245 if (!need_inode_block_update(sbi, inode->i_ino))
167 fi->last_disk_size = inode->i_size; 246 fi->last_disk_size = inode->i_size;
168 247
248 if (fi->i_flags & FS_PROJINHERIT_FL)
249 set_inode_flag(inode, FI_PROJ_INHERIT);
250
251 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi->sb) &&
252 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
253 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
254 else
255 i_projid = F2FS_DEF_PROJID;
256 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
257
169 f2fs_put_page(node_page, 1); 258 f2fs_put_page(node_page, 1);
170 259
171 stat_inc_inline_xattr(inode); 260 stat_inc_inline_xattr(inode);
@@ -292,6 +381,20 @@ int update_inode(struct inode *inode, struct page *node_page)
292 ri->i_generation = cpu_to_le32(inode->i_generation); 381 ri->i_generation = cpu_to_le32(inode->i_generation);
293 ri->i_dir_level = F2FS_I(inode)->i_dir_level; 382 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
294 383
384 if (f2fs_has_extra_attr(inode)) {
385 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
386
387 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)->sb) &&
388 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
389 i_projid)) {
390 projid_t i_projid;
391
392 i_projid = from_kprojid(&init_user_ns,
393 F2FS_I(inode)->i_projid);
394 ri->i_projid = cpu_to_le32(i_projid);
395 }
396 }
397
295 __set_inode_rdev(inode, ri); 398 __set_inode_rdev(inode, ri);
296 set_cold_node(inode, node_page); 399 set_cold_node(inode, node_page);
297 400
@@ -416,6 +519,9 @@ no_delete:
416 stat_dec_inline_dir(inode); 519 stat_dec_inline_dir(inode);
417 stat_dec_inline_inode(inode); 520 stat_dec_inline_inode(inode);
418 521
522 if (!is_set_ckpt_flags(sbi, CP_ERROR_FLAG))
523 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
524
419 /* ino == 0, if f2fs_new_inode() was failed t*/ 525 /* ino == 0, if f2fs_new_inode() was failed t*/
420 if (inode->i_ino) 526 if (inode->i_ino)
421 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino, 527 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index 760d85223c81..a4dab98c4b7b 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -58,6 +58,13 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
58 goto fail; 58 goto fail;
59 } 59 }
60 60
61 if (f2fs_sb_has_project_quota(sbi->sb) &&
62 (F2FS_I(dir)->i_flags & FS_PROJINHERIT_FL))
63 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
64 else
65 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
66 F2FS_DEF_PROJID);
67
61 err = dquot_initialize(inode); 68 err = dquot_initialize(inode);
62 if (err) 69 if (err)
63 goto fail_drop; 70 goto fail_drop;
@@ -72,6 +79,11 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
72 79
73 set_inode_flag(inode, FI_NEW_INODE); 80 set_inode_flag(inode, FI_NEW_INODE);
74 81
82 if (f2fs_sb_has_extra_attr(sbi->sb)) {
83 set_inode_flag(inode, FI_EXTRA_ATTR);
84 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
85 }
86
75 if (test_opt(sbi, INLINE_XATTR)) 87 if (test_opt(sbi, INLINE_XATTR))
76 set_inode_flag(inode, FI_INLINE_XATTR); 88 set_inode_flag(inode, FI_INLINE_XATTR);
77 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode)) 89 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
@@ -85,6 +97,15 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
85 stat_inc_inline_inode(inode); 97 stat_inc_inline_inode(inode);
86 stat_inc_inline_dir(inode); 98 stat_inc_inline_dir(inode);
87 99
100 F2FS_I(inode)->i_flags =
101 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
102
103 if (S_ISDIR(inode->i_mode))
104 F2FS_I(inode)->i_flags |= FS_INDEX_FL;
105
106 if (F2FS_I(inode)->i_flags & FS_PROJINHERIT_FL)
107 set_inode_flag(inode, FI_PROJ_INHERIT);
108
88 trace_f2fs_new_inode(inode, 0); 109 trace_f2fs_new_inode(inode, 0);
89 return inode; 110 return inode;
90 111
@@ -204,6 +225,11 @@ static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
204 !fscrypt_has_permitted_context(dir, inode)) 225 !fscrypt_has_permitted_context(dir, inode))
205 return -EPERM; 226 return -EPERM;
206 227
228 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
229 (!projid_eq(F2FS_I(dir)->i_projid,
230 F2FS_I(old_dentry->d_inode)->i_projid)))
231 return -EXDEV;
232
207 err = dquot_initialize(dir); 233 err = dquot_initialize(dir);
208 if (err) 234 if (err)
209 return err; 235 return err;
@@ -261,6 +287,10 @@ static int __recover_dot_dentries(struct inode *dir, nid_t pino)
261 return 0; 287 return 0;
262 } 288 }
263 289
290 err = dquot_initialize(dir);
291 if (err)
292 return err;
293
264 f2fs_balance_fs(sbi, true); 294 f2fs_balance_fs(sbi, true);
265 295
266 f2fs_lock_op(sbi); 296 f2fs_lock_op(sbi);
@@ -724,6 +754,11 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
724 goto out; 754 goto out;
725 } 755 }
726 756
757 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
758 (!projid_eq(F2FS_I(new_dir)->i_projid,
759 F2FS_I(old_dentry->d_inode)->i_projid)))
760 return -EXDEV;
761
727 err = dquot_initialize(old_dir); 762 err = dquot_initialize(old_dir);
728 if (err) 763 if (err)
729 goto out; 764 goto out;
@@ -912,6 +947,14 @@ static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
912 !fscrypt_has_permitted_context(old_dir, new_inode))) 947 !fscrypt_has_permitted_context(old_dir, new_inode)))
913 return -EPERM; 948 return -EPERM;
914 949
950 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
951 !projid_eq(F2FS_I(new_dir)->i_projid,
952 F2FS_I(old_dentry->d_inode)->i_projid)) ||
953 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
954 !projid_eq(F2FS_I(old_dir)->i_projid,
955 F2FS_I(new_dentry->d_inode)->i_projid)))
956 return -EXDEV;
957
915 err = dquot_initialize(old_dir); 958 err = dquot_initialize(old_dir);
916 if (err) 959 if (err)
917 goto out; 960 goto out;
diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
index d53fe620939e..fca87835a1da 100644
--- a/fs/f2fs/node.c
+++ b/fs/f2fs/node.c
@@ -19,6 +19,7 @@
19#include "f2fs.h" 19#include "f2fs.h"
20#include "node.h" 20#include "node.h"
21#include "segment.h" 21#include "segment.h"
22#include "xattr.h"
22#include "trace.h" 23#include "trace.h"
23#include <trace/events/f2fs.h> 24#include <trace/events/f2fs.h>
24 25
@@ -554,7 +555,7 @@ static int get_node_path(struct inode *inode, long block,
554 level = 3; 555 level = 3;
555 goto got; 556 goto got;
556 } else { 557 } else {
557 BUG(); 558 return -E2BIG;
558 } 559 }
559got: 560got:
560 return level; 561 return level;
@@ -578,6 +579,8 @@ int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
578 int err = 0; 579 int err = 0;
579 580
580 level = get_node_path(dn->inode, index, offset, noffset); 581 level = get_node_path(dn->inode, index, offset, noffset);
582 if (level < 0)
583 return level;
581 584
582 nids[0] = dn->inode->i_ino; 585 nids[0] = dn->inode->i_ino;
583 npage[0] = dn->inode_page; 586 npage[0] = dn->inode_page;
@@ -613,7 +616,7 @@ int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
613 } 616 }
614 617
615 dn->nid = nids[i]; 618 dn->nid = nids[i];
616 npage[i] = new_node_page(dn, noffset[i], NULL); 619 npage[i] = new_node_page(dn, noffset[i]);
617 if (IS_ERR(npage[i])) { 620 if (IS_ERR(npage[i])) {
618 alloc_nid_failed(sbi, nids[i]); 621 alloc_nid_failed(sbi, nids[i]);
619 err = PTR_ERR(npage[i]); 622 err = PTR_ERR(npage[i]);
@@ -654,7 +657,8 @@ int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
654 dn->nid = nids[level]; 657 dn->nid = nids[level];
655 dn->ofs_in_node = offset[level]; 658 dn->ofs_in_node = offset[level];
656 dn->node_page = npage[level]; 659 dn->node_page = npage[level];
657 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node); 660 dn->data_blkaddr = datablock_addr(dn->inode,
661 dn->node_page, dn->ofs_in_node);
658 return 0; 662 return 0;
659 663
660release_pages: 664release_pages:
@@ -876,6 +880,8 @@ int truncate_inode_blocks(struct inode *inode, pgoff_t from)
876 trace_f2fs_truncate_inode_blocks_enter(inode, from); 880 trace_f2fs_truncate_inode_blocks_enter(inode, from);
877 881
878 level = get_node_path(inode, from, offset, noffset); 882 level = get_node_path(inode, from, offset, noffset);
883 if (level < 0)
884 return level;
879 885
880 page = get_node_page(sbi, inode->i_ino); 886 page = get_node_page(sbi, inode->i_ino);
881 if (IS_ERR(page)) { 887 if (IS_ERR(page)) {
@@ -1022,11 +1028,10 @@ struct page *new_inode_page(struct inode *inode)
1022 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); 1028 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1023 1029
1024 /* caller should f2fs_put_page(page, 1); */ 1030 /* caller should f2fs_put_page(page, 1); */
1025 return new_node_page(&dn, 0, NULL); 1031 return new_node_page(&dn, 0);
1026} 1032}
1027 1033
1028struct page *new_node_page(struct dnode_of_data *dn, 1034struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs)
1029 unsigned int ofs, struct page *ipage)
1030{ 1035{
1031 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1036 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1032 struct node_info new_ni; 1037 struct node_info new_ni;
@@ -1170,6 +1175,11 @@ repeat:
1170 err = -EIO; 1175 err = -EIO;
1171 goto out_err; 1176 goto out_err;
1172 } 1177 }
1178
1179 if (!f2fs_inode_chksum_verify(sbi, page)) {
1180 err = -EBADMSG;
1181 goto out_err;
1182 }
1173page_hit: 1183page_hit:
1174 if(unlikely(nid != nid_of_node(page))) { 1184 if(unlikely(nid != nid_of_node(page))) {
1175 f2fs_msg(sbi->sb, KERN_WARNING, "inconsistent node block, " 1185 f2fs_msg(sbi->sb, KERN_WARNING, "inconsistent node block, "
@@ -1177,9 +1187,9 @@ page_hit:
1177 nid, nid_of_node(page), ino_of_node(page), 1187 nid, nid_of_node(page), ino_of_node(page),
1178 ofs_of_node(page), cpver_of_node(page), 1188 ofs_of_node(page), cpver_of_node(page),
1179 next_blkaddr_of_node(page)); 1189 next_blkaddr_of_node(page));
1180 ClearPageUptodate(page);
1181 err = -EINVAL; 1190 err = -EINVAL;
1182out_err: 1191out_err:
1192 ClearPageUptodate(page);
1183 f2fs_put_page(page, 1); 1193 f2fs_put_page(page, 1);
1184 return ERR_PTR(err); 1194 return ERR_PTR(err);
1185 } 1195 }
@@ -1326,7 +1336,8 @@ continue_unlock:
1326} 1336}
1327 1337
1328static int __write_node_page(struct page *page, bool atomic, bool *submitted, 1338static int __write_node_page(struct page *page, bool atomic, bool *submitted,
1329 struct writeback_control *wbc) 1339 struct writeback_control *wbc, bool do_balance,
1340 enum iostat_type io_type)
1330{ 1341{
1331 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 1342 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1332 nid_t nid; 1343 nid_t nid;
@@ -1339,6 +1350,7 @@ static int __write_node_page(struct page *page, bool atomic, bool *submitted,
1339 .page = page, 1350 .page = page,
1340 .encrypted_page = NULL, 1351 .encrypted_page = NULL,
1341 .submitted = false, 1352 .submitted = false,
1353 .io_type = io_type,
1342 }; 1354 };
1343 1355
1344 trace_f2fs_writepage(page, NODE); 1356 trace_f2fs_writepage(page, NODE);
@@ -1395,6 +1407,8 @@ static int __write_node_page(struct page *page, bool atomic, bool *submitted,
1395 if (submitted) 1407 if (submitted)
1396 *submitted = fio.submitted; 1408 *submitted = fio.submitted;
1397 1409
1410 if (do_balance)
1411 f2fs_balance_fs(sbi, false);
1398 return 0; 1412 return 0;
1399 1413
1400redirty_out: 1414redirty_out:
@@ -1405,7 +1419,7 @@ redirty_out:
1405static int f2fs_write_node_page(struct page *page, 1419static int f2fs_write_node_page(struct page *page,
1406 struct writeback_control *wbc) 1420 struct writeback_control *wbc)
1407{ 1421{
1408 return __write_node_page(page, false, NULL, wbc); 1422 return __write_node_page(page, false, NULL, wbc, false, FS_NODE_IO);
1409} 1423}
1410 1424
1411int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode, 1425int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
@@ -1493,7 +1507,8 @@ continue_unlock:
1493 1507
1494 ret = __write_node_page(page, atomic && 1508 ret = __write_node_page(page, atomic &&
1495 page == last_page, 1509 page == last_page,
1496 &submitted, wbc); 1510 &submitted, wbc, true,
1511 FS_NODE_IO);
1497 if (ret) { 1512 if (ret) {
1498 unlock_page(page); 1513 unlock_page(page);
1499 f2fs_put_page(last_page, 0); 1514 f2fs_put_page(last_page, 0);
@@ -1530,7 +1545,8 @@ out:
1530 return ret ? -EIO: 0; 1545 return ret ? -EIO: 0;
1531} 1546}
1532 1547
1533int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc) 1548int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
1549 bool do_balance, enum iostat_type io_type)
1534{ 1550{
1535 pgoff_t index, end; 1551 pgoff_t index, end;
1536 struct pagevec pvec; 1552 struct pagevec pvec;
@@ -1608,7 +1624,8 @@ continue_unlock:
1608 set_fsync_mark(page, 0); 1624 set_fsync_mark(page, 0);
1609 set_dentry_mark(page, 0); 1625 set_dentry_mark(page, 0);
1610 1626
1611 ret = __write_node_page(page, false, &submitted, wbc); 1627 ret = __write_node_page(page, false, &submitted,
1628 wbc, do_balance, io_type);
1612 if (ret) 1629 if (ret)
1613 unlock_page(page); 1630 unlock_page(page);
1614 else if (submitted) 1631 else if (submitted)
@@ -1697,7 +1714,7 @@ static int f2fs_write_node_pages(struct address_space *mapping,
1697 diff = nr_pages_to_write(sbi, NODE, wbc); 1714 diff = nr_pages_to_write(sbi, NODE, wbc);
1698 wbc->sync_mode = WB_SYNC_NONE; 1715 wbc->sync_mode = WB_SYNC_NONE;
1699 blk_start_plug(&plug); 1716 blk_start_plug(&plug);
1700 sync_node_pages(sbi, wbc); 1717 sync_node_pages(sbi, wbc, true, FS_NODE_IO);
1701 blk_finish_plug(&plug); 1718 blk_finish_plug(&plug);
1702 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); 1719 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
1703 return 0; 1720 return 0;
@@ -2191,7 +2208,8 @@ int recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
2191{ 2208{
2192 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2209 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2193 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid; 2210 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
2194 nid_t new_xnid = nid_of_node(page); 2211 nid_t new_xnid;
2212 struct dnode_of_data dn;
2195 struct node_info ni; 2213 struct node_info ni;
2196 struct page *xpage; 2214 struct page *xpage;
2197 2215
@@ -2207,22 +2225,22 @@ int recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
2207 2225
2208recover_xnid: 2226recover_xnid:
2209 /* 2: update xattr nid in inode */ 2227 /* 2: update xattr nid in inode */
2210 remove_free_nid(sbi, new_xnid); 2228 if (!alloc_nid(sbi, &new_xnid))
2211 f2fs_i_xnid_write(inode, new_xnid); 2229 return -ENOSPC;
2212 if (unlikely(inc_valid_node_count(sbi, inode, false))) 2230
2213 f2fs_bug_on(sbi, 1); 2231 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
2232 xpage = new_node_page(&dn, XATTR_NODE_OFFSET);
2233 if (IS_ERR(xpage)) {
2234 alloc_nid_failed(sbi, new_xnid);
2235 return PTR_ERR(xpage);
2236 }
2237
2238 alloc_nid_done(sbi, new_xnid);
2214 update_inode_page(inode); 2239 update_inode_page(inode);
2215 2240
2216 /* 3: update and set xattr node page dirty */ 2241 /* 3: update and set xattr node page dirty */
2217 xpage = grab_cache_page(NODE_MAPPING(sbi), new_xnid); 2242 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE);
2218 if (!xpage)
2219 return -ENOMEM;
2220
2221 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), PAGE_SIZE);
2222 2243
2223 get_node_info(sbi, new_xnid, &ni);
2224 ni.ino = inode->i_ino;
2225 set_node_addr(sbi, &ni, NEW_ADDR, false);
2226 set_page_dirty(xpage); 2244 set_page_dirty(xpage);
2227 f2fs_put_page(xpage, 1); 2245 f2fs_put_page(xpage, 1);
2228 2246
@@ -2262,7 +2280,14 @@ retry:
2262 dst->i_blocks = cpu_to_le64(1); 2280 dst->i_blocks = cpu_to_le64(1);
2263 dst->i_links = cpu_to_le32(1); 2281 dst->i_links = cpu_to_le32(1);
2264 dst->i_xattr_nid = 0; 2282 dst->i_xattr_nid = 0;
2265 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR; 2283 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
2284 if (dst->i_inline & F2FS_EXTRA_ATTR) {
2285 dst->i_extra_isize = src->i_extra_isize;
2286 if (f2fs_sb_has_project_quota(sbi->sb) &&
2287 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2288 i_projid))
2289 dst->i_projid = src->i_projid;
2290 }
2266 2291
2267 new_ni = old_ni; 2292 new_ni = old_ni;
2268 new_ni.ino = ino; 2293 new_ni.ino = ino;
diff --git a/fs/f2fs/recovery.c b/fs/f2fs/recovery.c
index 907d6b7dde6a..9626758bc762 100644
--- a/fs/f2fs/recovery.c
+++ b/fs/f2fs/recovery.c
@@ -69,20 +69,34 @@ static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
69} 69}
70 70
71static struct fsync_inode_entry *add_fsync_inode(struct f2fs_sb_info *sbi, 71static struct fsync_inode_entry *add_fsync_inode(struct f2fs_sb_info *sbi,
72 struct list_head *head, nid_t ino) 72 struct list_head *head, nid_t ino, bool quota_inode)
73{ 73{
74 struct inode *inode; 74 struct inode *inode;
75 struct fsync_inode_entry *entry; 75 struct fsync_inode_entry *entry;
76 int err;
76 77
77 inode = f2fs_iget_retry(sbi->sb, ino); 78 inode = f2fs_iget_retry(sbi->sb, ino);
78 if (IS_ERR(inode)) 79 if (IS_ERR(inode))
79 return ERR_CAST(inode); 80 return ERR_CAST(inode);
80 81
82 err = dquot_initialize(inode);
83 if (err)
84 goto err_out;
85
86 if (quota_inode) {
87 err = dquot_alloc_inode(inode);
88 if (err)
89 goto err_out;
90 }
91
81 entry = f2fs_kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO); 92 entry = f2fs_kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO);
82 entry->inode = inode; 93 entry->inode = inode;
83 list_add_tail(&entry->list, head); 94 list_add_tail(&entry->list, head);
84 95
85 return entry; 96 return entry;
97err_out:
98 iput(inode);
99 return ERR_PTR(err);
86} 100}
87 101
88static void del_fsync_inode(struct fsync_inode_entry *entry) 102static void del_fsync_inode(struct fsync_inode_entry *entry)
@@ -107,7 +121,8 @@ static int recover_dentry(struct inode *inode, struct page *ipage,
107 121
108 entry = get_fsync_inode(dir_list, pino); 122 entry = get_fsync_inode(dir_list, pino);
109 if (!entry) { 123 if (!entry) {
110 entry = add_fsync_inode(F2FS_I_SB(inode), dir_list, pino); 124 entry = add_fsync_inode(F2FS_I_SB(inode), dir_list,
125 pino, false);
111 if (IS_ERR(entry)) { 126 if (IS_ERR(entry)) {
112 dir = ERR_CAST(entry); 127 dir = ERR_CAST(entry);
113 err = PTR_ERR(entry); 128 err = PTR_ERR(entry);
@@ -140,6 +155,13 @@ retry:
140 err = -EEXIST; 155 err = -EEXIST;
141 goto out_unmap_put; 156 goto out_unmap_put;
142 } 157 }
158
159 err = dquot_initialize(einode);
160 if (err) {
161 iput(einode);
162 goto out_unmap_put;
163 }
164
143 err = acquire_orphan_inode(F2FS_I_SB(inode)); 165 err = acquire_orphan_inode(F2FS_I_SB(inode));
144 if (err) { 166 if (err) {
145 iput(einode); 167 iput(einode);
@@ -226,18 +248,22 @@ static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
226 248
227 entry = get_fsync_inode(head, ino_of_node(page)); 249 entry = get_fsync_inode(head, ino_of_node(page));
228 if (!entry) { 250 if (!entry) {
251 bool quota_inode = false;
252
229 if (!check_only && 253 if (!check_only &&
230 IS_INODE(page) && is_dent_dnode(page)) { 254 IS_INODE(page) && is_dent_dnode(page)) {
231 err = recover_inode_page(sbi, page); 255 err = recover_inode_page(sbi, page);
232 if (err) 256 if (err)
233 break; 257 break;
258 quota_inode = true;
234 } 259 }
235 260
236 /* 261 /*
237 * CP | dnode(F) | inode(DF) 262 * CP | dnode(F) | inode(DF)
238 * For this case, we should not give up now. 263 * For this case, we should not give up now.
239 */ 264 */
240 entry = add_fsync_inode(sbi, head, ino_of_node(page)); 265 entry = add_fsync_inode(sbi, head, ino_of_node(page),
266 quota_inode);
241 if (IS_ERR(entry)) { 267 if (IS_ERR(entry)) {
242 err = PTR_ERR(entry); 268 err = PTR_ERR(entry);
243 if (err == -ENOENT) { 269 if (err == -ENOENT) {
@@ -291,7 +317,7 @@ static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
291 return 0; 317 return 0;
292 318
293 /* Get the previous summary */ 319 /* Get the previous summary */
294 for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) { 320 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
295 struct curseg_info *curseg = CURSEG_I(sbi, i); 321 struct curseg_info *curseg = CURSEG_I(sbi, i);
296 if (curseg->segno == segno) { 322 if (curseg->segno == segno) {
297 sum = curseg->sum_blk->entries[blkoff]; 323 sum = curseg->sum_blk->entries[blkoff];
@@ -328,10 +354,18 @@ got_it:
328 f2fs_put_page(node_page, 1); 354 f2fs_put_page(node_page, 1);
329 355
330 if (ino != dn->inode->i_ino) { 356 if (ino != dn->inode->i_ino) {
357 int ret;
358
331 /* Deallocate previous index in the node page */ 359 /* Deallocate previous index in the node page */
332 inode = f2fs_iget_retry(sbi->sb, ino); 360 inode = f2fs_iget_retry(sbi->sb, ino);
333 if (IS_ERR(inode)) 361 if (IS_ERR(inode))
334 return PTR_ERR(inode); 362 return PTR_ERR(inode);
363
364 ret = dquot_initialize(inode);
365 if (ret) {
366 iput(inode);
367 return ret;
368 }
335 } else { 369 } else {
336 inode = dn->inode; 370 inode = dn->inode;
337 } 371 }
@@ -361,7 +395,8 @@ out:
361 return 0; 395 return 0;
362 396
363truncate_out: 397truncate_out:
364 if (datablock_addr(tdn.node_page, tdn.ofs_in_node) == blkaddr) 398 if (datablock_addr(tdn.inode, tdn.node_page,
399 tdn.ofs_in_node) == blkaddr)
365 truncate_data_blocks_range(&tdn, 1); 400 truncate_data_blocks_range(&tdn, 1);
366 if (dn->inode->i_ino == nid && !dn->inode_page_locked) 401 if (dn->inode->i_ino == nid && !dn->inode_page_locked)
367 unlock_page(dn->inode_page); 402 unlock_page(dn->inode_page);
@@ -414,8 +449,8 @@ retry_dn:
414 for (; start < end; start++, dn.ofs_in_node++) { 449 for (; start < end; start++, dn.ofs_in_node++) {
415 block_t src, dest; 450 block_t src, dest;
416 451
417 src = datablock_addr(dn.node_page, dn.ofs_in_node); 452 src = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
418 dest = datablock_addr(page, dn.ofs_in_node); 453 dest = datablock_addr(dn.inode, page, dn.ofs_in_node);
419 454
420 /* skip recovering if dest is the same as src */ 455 /* skip recovering if dest is the same as src */
421 if (src == dest) 456 if (src == dest)
@@ -557,12 +592,27 @@ int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
557 struct list_head dir_list; 592 struct list_head dir_list;
558 int err; 593 int err;
559 int ret = 0; 594 int ret = 0;
595 unsigned long s_flags = sbi->sb->s_flags;
560 bool need_writecp = false; 596 bool need_writecp = false;
561 597
598 if (s_flags & MS_RDONLY) {
599 f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
600 sbi->sb->s_flags &= ~MS_RDONLY;
601 }
602
603#ifdef CONFIG_QUOTA
604 /* Needed for iput() to work correctly and not trash data */
605 sbi->sb->s_flags |= MS_ACTIVE;
606 /* Turn on quotas so that they are updated correctly */
607 f2fs_enable_quota_files(sbi);
608#endif
609
562 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry", 610 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
563 sizeof(struct fsync_inode_entry)); 611 sizeof(struct fsync_inode_entry));
564 if (!fsync_entry_slab) 612 if (!fsync_entry_slab) {
565 return -ENOMEM; 613 err = -ENOMEM;
614 goto out;
615 }
566 616
567 INIT_LIST_HEAD(&inode_list); 617 INIT_LIST_HEAD(&inode_list);
568 INIT_LIST_HEAD(&dir_list); 618 INIT_LIST_HEAD(&dir_list);
@@ -573,11 +623,11 @@ int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
573 /* step #1: find fsynced inode numbers */ 623 /* step #1: find fsynced inode numbers */
574 err = find_fsync_dnodes(sbi, &inode_list, check_only); 624 err = find_fsync_dnodes(sbi, &inode_list, check_only);
575 if (err || list_empty(&inode_list)) 625 if (err || list_empty(&inode_list))
576 goto out; 626 goto skip;
577 627
578 if (check_only) { 628 if (check_only) {
579 ret = 1; 629 ret = 1;
580 goto out; 630 goto skip;
581 } 631 }
582 632
583 need_writecp = true; 633 need_writecp = true;
@@ -586,7 +636,7 @@ int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
586 err = recover_data(sbi, &inode_list, &dir_list); 636 err = recover_data(sbi, &inode_list, &dir_list);
587 if (!err) 637 if (!err)
588 f2fs_bug_on(sbi, !list_empty(&inode_list)); 638 f2fs_bug_on(sbi, !list_empty(&inode_list));
589out: 639skip:
590 destroy_fsync_dnodes(&inode_list); 640 destroy_fsync_dnodes(&inode_list);
591 641
592 /* truncate meta pages to be used by the recovery */ 642 /* truncate meta pages to be used by the recovery */
@@ -599,8 +649,6 @@ out:
599 } 649 }
600 650
601 clear_sbi_flag(sbi, SBI_POR_DOING); 651 clear_sbi_flag(sbi, SBI_POR_DOING);
602 if (err)
603 set_ckpt_flags(sbi, CP_ERROR_FLAG);
604 mutex_unlock(&sbi->cp_mutex); 652 mutex_unlock(&sbi->cp_mutex);
605 653
606 /* let's drop all the directory inodes for clean checkpoint */ 654 /* let's drop all the directory inodes for clean checkpoint */
@@ -614,5 +662,12 @@ out:
614 } 662 }
615 663
616 kmem_cache_destroy(fsync_entry_slab); 664 kmem_cache_destroy(fsync_entry_slab);
665out:
666#ifdef CONFIG_QUOTA
667 /* Turn quotas off */
668 f2fs_quota_off_umount(sbi->sb);
669#endif
670 sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
671
617 return ret ? ret: err; 672 return ret ? ret: err;
618} 673}
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 6f8fc4a6e701..621b9b3d320b 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -17,10 +17,12 @@
17#include <linux/swap.h> 17#include <linux/swap.h>
18#include <linux/timer.h> 18#include <linux/timer.h>
19#include <linux/freezer.h> 19#include <linux/freezer.h>
20#include <linux/sched/signal.h>
20 21
21#include "f2fs.h" 22#include "f2fs.h"
22#include "segment.h" 23#include "segment.h"
23#include "node.h" 24#include "node.h"
25#include "gc.h"
24#include "trace.h" 26#include "trace.h"
25#include <trace/events/f2fs.h> 27#include <trace/events/f2fs.h>
26 28
@@ -167,6 +169,21 @@ found:
167 return result - size + __reverse_ffz(tmp); 169 return result - size + __reverse_ffz(tmp);
168} 170}
169 171
172bool need_SSR(struct f2fs_sb_info *sbi)
173{
174 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
175 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
176 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
177
178 if (test_opt(sbi, LFS))
179 return false;
180 if (sbi->gc_thread && sbi->gc_thread->gc_urgent)
181 return true;
182
183 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
184 2 * reserved_sections(sbi));
185}
186
170void register_inmem_page(struct inode *inode, struct page *page) 187void register_inmem_page(struct inode *inode, struct page *page)
171{ 188{
172 struct f2fs_inode_info *fi = F2FS_I(inode); 189 struct f2fs_inode_info *fi = F2FS_I(inode);
@@ -213,9 +230,15 @@ static int __revoke_inmem_pages(struct inode *inode,
213 struct node_info ni; 230 struct node_info ni;
214 231
215 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE); 232 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
216 233retry:
217 set_new_dnode(&dn, inode, NULL, NULL, 0); 234 set_new_dnode(&dn, inode, NULL, NULL, 0);
218 if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) { 235 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
236 if (err) {
237 if (err == -ENOMEM) {
238 congestion_wait(BLK_RW_ASYNC, HZ/50);
239 cond_resched();
240 goto retry;
241 }
219 err = -EAGAIN; 242 err = -EAGAIN;
220 goto next; 243 goto next;
221 } 244 }
@@ -248,6 +271,7 @@ void drop_inmem_pages(struct inode *inode)
248 mutex_unlock(&fi->inmem_lock); 271 mutex_unlock(&fi->inmem_lock);
249 272
250 clear_inode_flag(inode, FI_ATOMIC_FILE); 273 clear_inode_flag(inode, FI_ATOMIC_FILE);
274 clear_inode_flag(inode, FI_HOT_DATA);
251 stat_dec_atomic_write(inode); 275 stat_dec_atomic_write(inode);
252} 276}
253 277
@@ -292,6 +316,7 @@ static int __commit_inmem_pages(struct inode *inode,
292 .type = DATA, 316 .type = DATA,
293 .op = REQ_OP_WRITE, 317 .op = REQ_OP_WRITE,
294 .op_flags = REQ_SYNC | REQ_PRIO, 318 .op_flags = REQ_SYNC | REQ_PRIO,
319 .io_type = FS_DATA_IO,
295 }; 320 };
296 pgoff_t last_idx = ULONG_MAX; 321 pgoff_t last_idx = ULONG_MAX;
297 int err = 0; 322 int err = 0;
@@ -309,17 +334,21 @@ static int __commit_inmem_pages(struct inode *inode,
309 inode_dec_dirty_pages(inode); 334 inode_dec_dirty_pages(inode);
310 remove_dirty_inode(inode); 335 remove_dirty_inode(inode);
311 } 336 }
312 337retry:
313 fio.page = page; 338 fio.page = page;
314 fio.old_blkaddr = NULL_ADDR; 339 fio.old_blkaddr = NULL_ADDR;
315 fio.encrypted_page = NULL; 340 fio.encrypted_page = NULL;
316 fio.need_lock = LOCK_DONE; 341 fio.need_lock = LOCK_DONE;
317 err = do_write_data_page(&fio); 342 err = do_write_data_page(&fio);
318 if (err) { 343 if (err) {
344 if (err == -ENOMEM) {
345 congestion_wait(BLK_RW_ASYNC, HZ/50);
346 cond_resched();
347 goto retry;
348 }
319 unlock_page(page); 349 unlock_page(page);
320 break; 350 break;
321 } 351 }
322
323 /* record old blkaddr for revoking */ 352 /* record old blkaddr for revoking */
324 cur->old_addr = fio.old_blkaddr; 353 cur->old_addr = fio.old_blkaddr;
325 last_idx = page->index; 354 last_idx = page->index;
@@ -481,6 +510,8 @@ repeat:
481 if (kthread_should_stop()) 510 if (kthread_should_stop())
482 return 0; 511 return 0;
483 512
513 sb_start_intwrite(sbi->sb);
514
484 if (!llist_empty(&fcc->issue_list)) { 515 if (!llist_empty(&fcc->issue_list)) {
485 struct flush_cmd *cmd, *next; 516 struct flush_cmd *cmd, *next;
486 int ret; 517 int ret;
@@ -499,6 +530,8 @@ repeat:
499 fcc->dispatch_list = NULL; 530 fcc->dispatch_list = NULL;
500 } 531 }
501 532
533 sb_end_intwrite(sbi->sb);
534
502 wait_event_interruptible(*q, 535 wait_event_interruptible(*q,
503 kthread_should_stop() || !llist_empty(&fcc->issue_list)); 536 kthread_should_stop() || !llist_empty(&fcc->issue_list));
504 goto repeat; 537 goto repeat;
@@ -519,8 +552,7 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi)
519 return ret; 552 return ret;
520 } 553 }
521 554
522 if (!atomic_read(&fcc->issing_flush)) { 555 if (atomic_inc_return(&fcc->issing_flush) == 1) {
523 atomic_inc(&fcc->issing_flush);
524 ret = submit_flush_wait(sbi); 556 ret = submit_flush_wait(sbi);
525 atomic_dec(&fcc->issing_flush); 557 atomic_dec(&fcc->issing_flush);
526 558
@@ -530,18 +562,39 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi)
530 562
531 init_completion(&cmd.wait); 563 init_completion(&cmd.wait);
532 564
533 atomic_inc(&fcc->issing_flush);
534 llist_add(&cmd.llnode, &fcc->issue_list); 565 llist_add(&cmd.llnode, &fcc->issue_list);
535 566
536 if (!fcc->dispatch_list) 567 /* update issue_list before we wake up issue_flush thread */
568 smp_mb();
569
570 if (waitqueue_active(&fcc->flush_wait_queue))
537 wake_up(&fcc->flush_wait_queue); 571 wake_up(&fcc->flush_wait_queue);
538 572
539 if (fcc->f2fs_issue_flush) { 573 if (fcc->f2fs_issue_flush) {
540 wait_for_completion(&cmd.wait); 574 wait_for_completion(&cmd.wait);
541 atomic_dec(&fcc->issing_flush); 575 atomic_dec(&fcc->issing_flush);
542 } else { 576 } else {
543 llist_del_all(&fcc->issue_list); 577 struct llist_node *list;
544 atomic_set(&fcc->issing_flush, 0); 578
579 list = llist_del_all(&fcc->issue_list);
580 if (!list) {
581 wait_for_completion(&cmd.wait);
582 atomic_dec(&fcc->issing_flush);
583 } else {
584 struct flush_cmd *tmp, *next;
585
586 ret = submit_flush_wait(sbi);
587
588 llist_for_each_entry_safe(tmp, next, list, llnode) {
589 if (tmp == &cmd) {
590 cmd.ret = ret;
591 atomic_dec(&fcc->issing_flush);
592 continue;
593 }
594 tmp->ret = ret;
595 complete(&tmp->wait);
596 }
597 }
545 } 598 }
546 599
547 return cmd.ret; 600 return cmd.ret;
@@ -778,11 +831,14 @@ void __check_sit_bitmap(struct f2fs_sb_info *sbi,
778 sentry = get_seg_entry(sbi, segno); 831 sentry = get_seg_entry(sbi, segno);
779 offset = GET_BLKOFF_FROM_SEG0(sbi, blk); 832 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
780 833
781 size = min((unsigned long)(end - blk), max_blocks); 834 if (end < START_BLOCK(sbi, segno + 1))
835 size = GET_BLKOFF_FROM_SEG0(sbi, end);
836 else
837 size = max_blocks;
782 map = (unsigned long *)(sentry->cur_valid_map); 838 map = (unsigned long *)(sentry->cur_valid_map);
783 offset = __find_rev_next_bit(map, size, offset); 839 offset = __find_rev_next_bit(map, size, offset);
784 f2fs_bug_on(sbi, offset != size); 840 f2fs_bug_on(sbi, offset != size);
785 blk += size; 841 blk = START_BLOCK(sbi, segno + 1);
786 } 842 }
787#endif 843#endif
788} 844}
@@ -815,6 +871,8 @@ static void __submit_discard_cmd(struct f2fs_sb_info *sbi,
815 submit_bio(bio); 871 submit_bio(bio);
816 list_move_tail(&dc->list, &dcc->wait_list); 872 list_move_tail(&dc->list, &dcc->wait_list);
817 __check_sit_bitmap(sbi, dc->start, dc->start + dc->len); 873 __check_sit_bitmap(sbi, dc->start, dc->start + dc->len);
874
875 f2fs_update_iostat(sbi, FS_DISCARD, 1);
818 } 876 }
819 } else { 877 } else {
820 __remove_discard_cmd(sbi, dc); 878 __remove_discard_cmd(sbi, dc);
@@ -996,32 +1054,81 @@ static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
996 return 0; 1054 return 0;
997} 1055}
998 1056
999static void __issue_discard_cmd(struct f2fs_sb_info *sbi, bool issue_cond) 1057static int __issue_discard_cmd(struct f2fs_sb_info *sbi, bool issue_cond)
1000{ 1058{
1001 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1059 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1002 struct list_head *pend_list; 1060 struct list_head *pend_list;
1003 struct discard_cmd *dc, *tmp; 1061 struct discard_cmd *dc, *tmp;
1004 struct blk_plug plug; 1062 struct blk_plug plug;
1005 int i, iter = 0; 1063 int iter = 0, issued = 0;
1064 int i;
1065 bool io_interrupted = false;
1006 1066
1007 mutex_lock(&dcc->cmd_lock); 1067 mutex_lock(&dcc->cmd_lock);
1008 f2fs_bug_on(sbi, 1068 f2fs_bug_on(sbi,
1009 !__check_rb_tree_consistence(sbi, &dcc->root)); 1069 !__check_rb_tree_consistence(sbi, &dcc->root));
1010 blk_start_plug(&plug); 1070 blk_start_plug(&plug);
1011 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) { 1071 for (i = MAX_PLIST_NUM - 1;
1072 i >= 0 && plist_issue(dcc->pend_list_tag[i]); i--) {
1012 pend_list = &dcc->pend_list[i]; 1073 pend_list = &dcc->pend_list[i];
1013 list_for_each_entry_safe(dc, tmp, pend_list, list) { 1074 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1014 f2fs_bug_on(sbi, dc->state != D_PREP); 1075 f2fs_bug_on(sbi, dc->state != D_PREP);
1015 1076
1016 if (!issue_cond || is_idle(sbi)) 1077 /* Hurry up to finish fstrim */
1078 if (dcc->pend_list_tag[i] & P_TRIM) {
1079 __submit_discard_cmd(sbi, dc);
1080 issued++;
1081
1082 if (fatal_signal_pending(current))
1083 break;
1084 continue;
1085 }
1086
1087 if (!issue_cond) {
1017 __submit_discard_cmd(sbi, dc); 1088 __submit_discard_cmd(sbi, dc);
1018 if (issue_cond && iter++ > DISCARD_ISSUE_RATE) 1089 issued++;
1090 continue;
1091 }
1092
1093 if (is_idle(sbi)) {
1094 __submit_discard_cmd(sbi, dc);
1095 issued++;
1096 } else {
1097 io_interrupted = true;
1098 }
1099
1100 if (++iter >= DISCARD_ISSUE_RATE)
1019 goto out; 1101 goto out;
1020 } 1102 }
1103 if (list_empty(pend_list) && dcc->pend_list_tag[i] & P_TRIM)
1104 dcc->pend_list_tag[i] &= (~P_TRIM);
1021 } 1105 }
1022out: 1106out:
1023 blk_finish_plug(&plug); 1107 blk_finish_plug(&plug);
1024 mutex_unlock(&dcc->cmd_lock); 1108 mutex_unlock(&dcc->cmd_lock);
1109
1110 if (!issued && io_interrupted)
1111 issued = -1;
1112
1113 return issued;
1114}
1115
1116static void __drop_discard_cmd(struct f2fs_sb_info *sbi)
1117{
1118 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1119 struct list_head *pend_list;
1120 struct discard_cmd *dc, *tmp;
1121 int i;
1122
1123 mutex_lock(&dcc->cmd_lock);
1124 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1125 pend_list = &dcc->pend_list[i];
1126 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1127 f2fs_bug_on(sbi, dc->state != D_PREP);
1128 __remove_discard_cmd(sbi, dc);
1129 }
1130 }
1131 mutex_unlock(&dcc->cmd_lock);
1025} 1132}
1026 1133
1027static void __wait_one_discard_bio(struct f2fs_sb_info *sbi, 1134static void __wait_one_discard_bio(struct f2fs_sb_info *sbi,
@@ -1102,34 +1209,63 @@ void stop_discard_thread(struct f2fs_sb_info *sbi)
1102 } 1209 }
1103} 1210}
1104 1211
1105/* This comes from f2fs_put_super */ 1212/* This comes from f2fs_put_super and f2fs_trim_fs */
1106void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi) 1213void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi)
1107{ 1214{
1108 __issue_discard_cmd(sbi, false); 1215 __issue_discard_cmd(sbi, false);
1216 __drop_discard_cmd(sbi);
1109 __wait_discard_cmd(sbi, false); 1217 __wait_discard_cmd(sbi, false);
1110} 1218}
1111 1219
1220static void mark_discard_range_all(struct f2fs_sb_info *sbi)
1221{
1222 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1223 int i;
1224
1225 mutex_lock(&dcc->cmd_lock);
1226 for (i = 0; i < MAX_PLIST_NUM; i++)
1227 dcc->pend_list_tag[i] |= P_TRIM;
1228 mutex_unlock(&dcc->cmd_lock);
1229}
1230
1112static int issue_discard_thread(void *data) 1231static int issue_discard_thread(void *data)
1113{ 1232{
1114 struct f2fs_sb_info *sbi = data; 1233 struct f2fs_sb_info *sbi = data;
1115 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1234 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1116 wait_queue_head_t *q = &dcc->discard_wait_queue; 1235 wait_queue_head_t *q = &dcc->discard_wait_queue;
1236 unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1237 int issued;
1117 1238
1118 set_freezable(); 1239 set_freezable();
1119 1240
1120 do { 1241 do {
1121 wait_event_interruptible(*q, kthread_should_stop() || 1242 wait_event_interruptible_timeout(*q,
1122 freezing(current) || 1243 kthread_should_stop() || freezing(current) ||
1123 atomic_read(&dcc->discard_cmd_cnt)); 1244 dcc->discard_wake,
1245 msecs_to_jiffies(wait_ms));
1124 if (try_to_freeze()) 1246 if (try_to_freeze())
1125 continue; 1247 continue;
1126 if (kthread_should_stop()) 1248 if (kthread_should_stop())
1127 return 0; 1249 return 0;
1128 1250
1129 __issue_discard_cmd(sbi, true); 1251 if (dcc->discard_wake) {
1130 __wait_discard_cmd(sbi, true); 1252 dcc->discard_wake = 0;
1253 if (sbi->gc_thread && sbi->gc_thread->gc_urgent)
1254 mark_discard_range_all(sbi);
1255 }
1256
1257 sb_start_intwrite(sbi->sb);
1258
1259 issued = __issue_discard_cmd(sbi, true);
1260 if (issued) {
1261 __wait_discard_cmd(sbi, true);
1262 wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1263 } else {
1264 wait_ms = DEF_MAX_DISCARD_ISSUE_TIME;
1265 }
1266
1267 sb_end_intwrite(sbi->sb);
1131 1268
1132 congestion_wait(BLK_RW_SYNC, HZ/50);
1133 } while (!kthread_should_stop()); 1269 } while (!kthread_should_stop());
1134 return 0; 1270 return 0;
1135} 1271}
@@ -1320,7 +1456,8 @@ static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1320 1456
1321void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1457void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1322{ 1458{
1323 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list); 1459 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1460 struct list_head *head = &dcc->entry_list;
1324 struct discard_entry *entry, *this; 1461 struct discard_entry *entry, *this;
1325 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 1462 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1326 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; 1463 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
@@ -1402,11 +1539,11 @@ skip:
1402 goto find_next; 1539 goto find_next;
1403 1540
1404 list_del(&entry->list); 1541 list_del(&entry->list);
1405 SM_I(sbi)->dcc_info->nr_discards -= total_len; 1542 dcc->nr_discards -= total_len;
1406 kmem_cache_free(discard_entry_slab, entry); 1543 kmem_cache_free(discard_entry_slab, entry);
1407 } 1544 }
1408 1545
1409 wake_up(&SM_I(sbi)->dcc_info->discard_wait_queue); 1546 wake_up_discard_thread(sbi, false);
1410} 1547}
1411 1548
1412static int create_discard_cmd_control(struct f2fs_sb_info *sbi) 1549static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
@@ -1424,9 +1561,13 @@ static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
1424 if (!dcc) 1561 if (!dcc)
1425 return -ENOMEM; 1562 return -ENOMEM;
1426 1563
1564 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
1427 INIT_LIST_HEAD(&dcc->entry_list); 1565 INIT_LIST_HEAD(&dcc->entry_list);
1428 for (i = 0; i < MAX_PLIST_NUM; i++) 1566 for (i = 0; i < MAX_PLIST_NUM; i++) {
1429 INIT_LIST_HEAD(&dcc->pend_list[i]); 1567 INIT_LIST_HEAD(&dcc->pend_list[i]);
1568 if (i >= dcc->discard_granularity - 1)
1569 dcc->pend_list_tag[i] |= P_ACTIVE;
1570 }
1430 INIT_LIST_HEAD(&dcc->wait_list); 1571 INIT_LIST_HEAD(&dcc->wait_list);
1431 mutex_init(&dcc->cmd_lock); 1572 mutex_init(&dcc->cmd_lock);
1432 atomic_set(&dcc->issued_discard, 0); 1573 atomic_set(&dcc->issued_discard, 0);
@@ -1491,6 +1632,10 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1491 struct seg_entry *se; 1632 struct seg_entry *se;
1492 unsigned int segno, offset; 1633 unsigned int segno, offset;
1493 long int new_vblocks; 1634 long int new_vblocks;
1635 bool exist;
1636#ifdef CONFIG_F2FS_CHECK_FS
1637 bool mir_exist;
1638#endif
1494 1639
1495 segno = GET_SEGNO(sbi, blkaddr); 1640 segno = GET_SEGNO(sbi, blkaddr);
1496 1641
@@ -1507,17 +1652,25 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1507 1652
1508 /* Update valid block bitmap */ 1653 /* Update valid block bitmap */
1509 if (del > 0) { 1654 if (del > 0) {
1510 if (f2fs_test_and_set_bit(offset, se->cur_valid_map)) { 1655 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
1511#ifdef CONFIG_F2FS_CHECK_FS 1656#ifdef CONFIG_F2FS_CHECK_FS
1512 if (f2fs_test_and_set_bit(offset, 1657 mir_exist = f2fs_test_and_set_bit(offset,
1513 se->cur_valid_map_mir)) 1658 se->cur_valid_map_mir);
1514 f2fs_bug_on(sbi, 1); 1659 if (unlikely(exist != mir_exist)) {
1515 else 1660 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1516 WARN_ON(1); 1661 "when setting bitmap, blk:%u, old bit:%d",
1517#else 1662 blkaddr, exist);
1518 f2fs_bug_on(sbi, 1); 1663 f2fs_bug_on(sbi, 1);
1664 }
1519#endif 1665#endif
1666 if (unlikely(exist)) {
1667 f2fs_msg(sbi->sb, KERN_ERR,
1668 "Bitmap was wrongly set, blk:%u", blkaddr);
1669 f2fs_bug_on(sbi, 1);
1670 se->valid_blocks--;
1671 del = 0;
1520 } 1672 }
1673
1521 if (f2fs_discard_en(sbi) && 1674 if (f2fs_discard_en(sbi) &&
1522 !f2fs_test_and_set_bit(offset, se->discard_map)) 1675 !f2fs_test_and_set_bit(offset, se->discard_map))
1523 sbi->discard_blks--; 1676 sbi->discard_blks--;
@@ -1528,17 +1681,25 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1528 se->ckpt_valid_blocks++; 1681 se->ckpt_valid_blocks++;
1529 } 1682 }
1530 } else { 1683 } else {
1531 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) { 1684 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
1532#ifdef CONFIG_F2FS_CHECK_FS 1685#ifdef CONFIG_F2FS_CHECK_FS
1533 if (!f2fs_test_and_clear_bit(offset, 1686 mir_exist = f2fs_test_and_clear_bit(offset,
1534 se->cur_valid_map_mir)) 1687 se->cur_valid_map_mir);
1535 f2fs_bug_on(sbi, 1); 1688 if (unlikely(exist != mir_exist)) {
1536 else 1689 f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
1537 WARN_ON(1); 1690 "when clearing bitmap, blk:%u, old bit:%d",
1538#else 1691 blkaddr, exist);
1539 f2fs_bug_on(sbi, 1); 1692 f2fs_bug_on(sbi, 1);
1693 }
1540#endif 1694#endif
1695 if (unlikely(!exist)) {
1696 f2fs_msg(sbi->sb, KERN_ERR,
1697 "Bitmap was wrongly cleared, blk:%u", blkaddr);
1698 f2fs_bug_on(sbi, 1);
1699 se->valid_blocks++;
1700 del = 0;
1541 } 1701 }
1702
1542 if (f2fs_discard_en(sbi) && 1703 if (f2fs_discard_en(sbi) &&
1543 f2fs_test_and_clear_bit(offset, se->discard_map)) 1704 f2fs_test_and_clear_bit(offset, se->discard_map))
1544 sbi->discard_blks++; 1705 sbi->discard_blks++;
@@ -1900,7 +2061,7 @@ static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1900 * This function always allocates a used segment(from dirty seglist) by SSR 2061 * This function always allocates a used segment(from dirty seglist) by SSR
1901 * manner, so it should recover the existing segment information of valid blocks 2062 * manner, so it should recover the existing segment information of valid blocks
1902 */ 2063 */
1903static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse) 2064static void change_curseg(struct f2fs_sb_info *sbi, int type)
1904{ 2065{
1905 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2066 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1906 struct curseg_info *curseg = CURSEG_I(sbi, type); 2067 struct curseg_info *curseg = CURSEG_I(sbi, type);
@@ -1921,12 +2082,10 @@ static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1921 curseg->alloc_type = SSR; 2082 curseg->alloc_type = SSR;
1922 __next_free_blkoff(sbi, curseg, 0); 2083 __next_free_blkoff(sbi, curseg, 0);
1923 2084
1924 if (reuse) { 2085 sum_page = get_sum_page(sbi, new_segno);
1925 sum_page = get_sum_page(sbi, new_segno); 2086 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1926 sum_node = (struct f2fs_summary_block *)page_address(sum_page); 2087 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1927 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); 2088 f2fs_put_page(sum_page, 1);
1928 f2fs_put_page(sum_page, 1);
1929 }
1930} 2089}
1931 2090
1932static int get_ssr_segment(struct f2fs_sb_info *sbi, int type) 2091static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
@@ -1990,7 +2149,7 @@ static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1990 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type)) 2149 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1991 new_curseg(sbi, type, false); 2150 new_curseg(sbi, type, false);
1992 else if (need_SSR(sbi) && get_ssr_segment(sbi, type)) 2151 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1993 change_curseg(sbi, type, true); 2152 change_curseg(sbi, type);
1994 else 2153 else
1995 new_curseg(sbi, type, false); 2154 new_curseg(sbi, type, false);
1996 2155
@@ -2083,6 +2242,9 @@ int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
2083 2242
2084 schedule(); 2243 schedule();
2085 } 2244 }
2245 /* It's time to issue all the filed discards */
2246 mark_discard_range_all(sbi);
2247 f2fs_wait_discard_bios(sbi);
2086out: 2248out:
2087 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed); 2249 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
2088 return err; 2250 return err;
@@ -2202,9 +2364,12 @@ void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2202 2364
2203 mutex_unlock(&sit_i->sentry_lock); 2365 mutex_unlock(&sit_i->sentry_lock);
2204 2366
2205 if (page && IS_NODESEG(type)) 2367 if (page && IS_NODESEG(type)) {
2206 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); 2368 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
2207 2369
2370 f2fs_inode_chksum_set(sbi, page);
2371 }
2372
2208 if (add_list) { 2373 if (add_list) {
2209 struct f2fs_bio_info *io; 2374 struct f2fs_bio_info *io;
2210 2375
@@ -2236,7 +2401,8 @@ reallocate:
2236 } 2401 }
2237} 2402}
2238 2403
2239void write_meta_page(struct f2fs_sb_info *sbi, struct page *page) 2404void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2405 enum iostat_type io_type)
2240{ 2406{
2241 struct f2fs_io_info fio = { 2407 struct f2fs_io_info fio = {
2242 .sbi = sbi, 2408 .sbi = sbi,
@@ -2255,6 +2421,8 @@ void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
2255 2421
2256 set_page_writeback(page); 2422 set_page_writeback(page);
2257 f2fs_submit_page_write(&fio); 2423 f2fs_submit_page_write(&fio);
2424
2425 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
2258} 2426}
2259 2427
2260void write_node_page(unsigned int nid, struct f2fs_io_info *fio) 2428void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
@@ -2263,6 +2431,8 @@ void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
2263 2431
2264 set_summary(&sum, nid, 0, 0); 2432 set_summary(&sum, nid, 0, 0);
2265 do_write_page(&sum, fio); 2433 do_write_page(&sum, fio);
2434
2435 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
2266} 2436}
2267 2437
2268void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio) 2438void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
@@ -2276,13 +2446,22 @@ void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
2276 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); 2446 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
2277 do_write_page(&sum, fio); 2447 do_write_page(&sum, fio);
2278 f2fs_update_data_blkaddr(dn, fio->new_blkaddr); 2448 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
2449
2450 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
2279} 2451}
2280 2452
2281int rewrite_data_page(struct f2fs_io_info *fio) 2453int rewrite_data_page(struct f2fs_io_info *fio)
2282{ 2454{
2455 int err;
2456
2283 fio->new_blkaddr = fio->old_blkaddr; 2457 fio->new_blkaddr = fio->old_blkaddr;
2284 stat_inc_inplace_blocks(fio->sbi); 2458 stat_inc_inplace_blocks(fio->sbi);
2285 return f2fs_submit_page_bio(fio); 2459
2460 err = f2fs_submit_page_bio(fio);
2461
2462 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
2463
2464 return err;
2286} 2465}
2287 2466
2288void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 2467void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
@@ -2324,7 +2503,7 @@ void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2324 /* change the current segment */ 2503 /* change the current segment */
2325 if (segno != curseg->segno) { 2504 if (segno != curseg->segno) {
2326 curseg->next_segno = segno; 2505 curseg->next_segno = segno;
2327 change_curseg(sbi, type, true); 2506 change_curseg(sbi, type);
2328 } 2507 }
2329 2508
2330 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); 2509 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
@@ -2343,7 +2522,7 @@ void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2343 if (recover_curseg) { 2522 if (recover_curseg) {
2344 if (old_cursegno != curseg->segno) { 2523 if (old_cursegno != curseg->segno) {
2345 curseg->next_segno = old_cursegno; 2524 curseg->next_segno = old_cursegno;
2346 change_curseg(sbi, type, true); 2525 change_curseg(sbi, type);
2347 } 2526 }
2348 curseg->next_blkoff = old_blkoff; 2527 curseg->next_blkoff = old_blkoff;
2349 } 2528 }
@@ -2382,8 +2561,7 @@ void f2fs_wait_on_page_writeback(struct page *page,
2382 } 2561 }
2383} 2562}
2384 2563
2385void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi, 2564void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr)
2386 block_t blkaddr)
2387{ 2565{
2388 struct page *cpage; 2566 struct page *cpage;
2389 2567
diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h
index 6b871b492fd5..e0a6cc23ace3 100644
--- a/fs/f2fs/segment.h
+++ b/fs/f2fs/segment.h
@@ -492,29 +492,11 @@ static inline int overprovision_segments(struct f2fs_sb_info *sbi)
492 return SM_I(sbi)->ovp_segments; 492 return SM_I(sbi)->ovp_segments;
493} 493}
494 494
495static inline int overprovision_sections(struct f2fs_sb_info *sbi)
496{
497 return GET_SEC_FROM_SEG(sbi, (unsigned int)overprovision_segments(sbi));
498}
499
500static inline int reserved_sections(struct f2fs_sb_info *sbi) 495static inline int reserved_sections(struct f2fs_sb_info *sbi)
501{ 496{
502 return GET_SEC_FROM_SEG(sbi, (unsigned int)reserved_segments(sbi)); 497 return GET_SEC_FROM_SEG(sbi, (unsigned int)reserved_segments(sbi));
503} 498}
504 499
505static inline bool need_SSR(struct f2fs_sb_info *sbi)
506{
507 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
508 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
509 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
510
511 if (test_opt(sbi, LFS))
512 return false;
513
514 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
515 2 * reserved_sections(sbi));
516}
517
518static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi, 500static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
519 int freed, int needed) 501 int freed, int needed)
520{ 502{
@@ -577,6 +559,10 @@ static inline bool need_inplace_update_policy(struct inode *inode,
577 if (test_opt(sbi, LFS)) 559 if (test_opt(sbi, LFS))
578 return false; 560 return false;
579 561
562 /* if this is cold file, we should overwrite to avoid fragmentation */
563 if (file_is_cold(inode))
564 return true;
565
580 if (policy & (0x1 << F2FS_IPU_FORCE)) 566 if (policy & (0x1 << F2FS_IPU_FORCE))
581 return true; 567 return true;
582 if (policy & (0x1 << F2FS_IPU_SSR) && need_SSR(sbi)) 568 if (policy & (0x1 << F2FS_IPU_SSR) && need_SSR(sbi))
@@ -799,3 +785,28 @@ static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
799 wbc->nr_to_write = desired; 785 wbc->nr_to_write = desired;
800 return desired - nr_to_write; 786 return desired - nr_to_write;
801} 787}
788
789static inline void wake_up_discard_thread(struct f2fs_sb_info *sbi, bool force)
790{
791 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
792 bool wakeup = false;
793 int i;
794
795 if (force)
796 goto wake_up;
797
798 mutex_lock(&dcc->cmd_lock);
799 for (i = MAX_PLIST_NUM - 1;
800 i >= 0 && plist_issue(dcc->pend_list_tag[i]); i--) {
801 if (!list_empty(&dcc->pend_list[i])) {
802 wakeup = true;
803 break;
804 }
805 }
806 mutex_unlock(&dcc->cmd_lock);
807 if (!wakeup)
808 return;
809wake_up:
810 dcc->discard_wake = 1;
811 wake_up_interruptible_all(&dcc->discard_wait_queue);
812}
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 32e4c025e97e..89f61eb3d167 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -25,6 +25,7 @@
25#include <linux/quotaops.h> 25#include <linux/quotaops.h>
26#include <linux/f2fs_fs.h> 26#include <linux/f2fs_fs.h>
27#include <linux/sysfs.h> 27#include <linux/sysfs.h>
28#include <linux/quota.h>
28 29
29#include "f2fs.h" 30#include "f2fs.h"
30#include "node.h" 31#include "node.h"
@@ -107,8 +108,20 @@ enum {
107 Opt_fault_injection, 108 Opt_fault_injection,
108 Opt_lazytime, 109 Opt_lazytime,
109 Opt_nolazytime, 110 Opt_nolazytime,
111 Opt_quota,
112 Opt_noquota,
110 Opt_usrquota, 113 Opt_usrquota,
111 Opt_grpquota, 114 Opt_grpquota,
115 Opt_prjquota,
116 Opt_usrjquota,
117 Opt_grpjquota,
118 Opt_prjjquota,
119 Opt_offusrjquota,
120 Opt_offgrpjquota,
121 Opt_offprjjquota,
122 Opt_jqfmt_vfsold,
123 Opt_jqfmt_vfsv0,
124 Opt_jqfmt_vfsv1,
112 Opt_err, 125 Opt_err,
113}; 126};
114 127
@@ -144,8 +157,20 @@ static match_table_t f2fs_tokens = {
144 {Opt_fault_injection, "fault_injection=%u"}, 157 {Opt_fault_injection, "fault_injection=%u"},
145 {Opt_lazytime, "lazytime"}, 158 {Opt_lazytime, "lazytime"},
146 {Opt_nolazytime, "nolazytime"}, 159 {Opt_nolazytime, "nolazytime"},
160 {Opt_quota, "quota"},
161 {Opt_noquota, "noquota"},
147 {Opt_usrquota, "usrquota"}, 162 {Opt_usrquota, "usrquota"},
148 {Opt_grpquota, "grpquota"}, 163 {Opt_grpquota, "grpquota"},
164 {Opt_prjquota, "prjquota"},
165 {Opt_usrjquota, "usrjquota=%s"},
166 {Opt_grpjquota, "grpjquota=%s"},
167 {Opt_prjjquota, "prjjquota=%s"},
168 {Opt_offusrjquota, "usrjquota="},
169 {Opt_offgrpjquota, "grpjquota="},
170 {Opt_offprjjquota, "prjjquota="},
171 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
172 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
173 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
149 {Opt_err, NULL}, 174 {Opt_err, NULL},
150}; 175};
151 176
@@ -157,7 +182,7 @@ void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
157 va_start(args, fmt); 182 va_start(args, fmt);
158 vaf.fmt = fmt; 183 vaf.fmt = fmt;
159 vaf.va = &args; 184 vaf.va = &args;
160 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf); 185 printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
161 va_end(args); 186 va_end(args);
162} 187}
163 188
@@ -168,6 +193,104 @@ static void init_once(void *foo)
168 inode_init_once(&fi->vfs_inode); 193 inode_init_once(&fi->vfs_inode);
169} 194}
170 195
196#ifdef CONFIG_QUOTA
197static const char * const quotatypes[] = INITQFNAMES;
198#define QTYPE2NAME(t) (quotatypes[t])
199static int f2fs_set_qf_name(struct super_block *sb, int qtype,
200 substring_t *args)
201{
202 struct f2fs_sb_info *sbi = F2FS_SB(sb);
203 char *qname;
204 int ret = -EINVAL;
205
206 if (sb_any_quota_loaded(sb) && !sbi->s_qf_names[qtype]) {
207 f2fs_msg(sb, KERN_ERR,
208 "Cannot change journaled "
209 "quota options when quota turned on");
210 return -EINVAL;
211 }
212 qname = match_strdup(args);
213 if (!qname) {
214 f2fs_msg(sb, KERN_ERR,
215 "Not enough memory for storing quotafile name");
216 return -EINVAL;
217 }
218 if (sbi->s_qf_names[qtype]) {
219 if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
220 ret = 0;
221 else
222 f2fs_msg(sb, KERN_ERR,
223 "%s quota file already specified",
224 QTYPE2NAME(qtype));
225 goto errout;
226 }
227 if (strchr(qname, '/')) {
228 f2fs_msg(sb, KERN_ERR,
229 "quotafile must be on filesystem root");
230 goto errout;
231 }
232 sbi->s_qf_names[qtype] = qname;
233 set_opt(sbi, QUOTA);
234 return 0;
235errout:
236 kfree(qname);
237 return ret;
238}
239
240static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
241{
242 struct f2fs_sb_info *sbi = F2FS_SB(sb);
243
244 if (sb_any_quota_loaded(sb) && sbi->s_qf_names[qtype]) {
245 f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
246 " when quota turned on");
247 return -EINVAL;
248 }
249 kfree(sbi->s_qf_names[qtype]);
250 sbi->s_qf_names[qtype] = NULL;
251 return 0;
252}
253
254static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
255{
256 /*
257 * We do the test below only for project quotas. 'usrquota' and
258 * 'grpquota' mount options are allowed even without quota feature
259 * to support legacy quotas in quota files.
260 */
261 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
262 f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
263 "Cannot enable project quota enforcement.");
264 return -1;
265 }
266 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA] ||
267 sbi->s_qf_names[PRJQUOTA]) {
268 if (test_opt(sbi, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
269 clear_opt(sbi, USRQUOTA);
270
271 if (test_opt(sbi, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
272 clear_opt(sbi, GRPQUOTA);
273
274 if (test_opt(sbi, PRJQUOTA) && sbi->s_qf_names[PRJQUOTA])
275 clear_opt(sbi, PRJQUOTA);
276
277 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
278 test_opt(sbi, PRJQUOTA)) {
279 f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
280 "format mixing");
281 return -1;
282 }
283
284 if (!sbi->s_jquota_fmt) {
285 f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
286 "not specified");
287 return -1;
288 }
289 }
290 return 0;
291}
292#endif
293
171static int parse_options(struct super_block *sb, char *options) 294static int parse_options(struct super_block *sb, char *options)
172{ 295{
173 struct f2fs_sb_info *sbi = F2FS_SB(sb); 296 struct f2fs_sb_info *sbi = F2FS_SB(sb);
@@ -175,6 +298,9 @@ static int parse_options(struct super_block *sb, char *options)
175 substring_t args[MAX_OPT_ARGS]; 298 substring_t args[MAX_OPT_ARGS];
176 char *p, *name; 299 char *p, *name;
177 int arg = 0; 300 int arg = 0;
301#ifdef CONFIG_QUOTA
302 int ret;
303#endif
178 304
179 if (!options) 305 if (!options)
180 return 0; 306 return 0;
@@ -386,15 +512,76 @@ static int parse_options(struct super_block *sb, char *options)
386 sb->s_flags &= ~MS_LAZYTIME; 512 sb->s_flags &= ~MS_LAZYTIME;
387 break; 513 break;
388#ifdef CONFIG_QUOTA 514#ifdef CONFIG_QUOTA
515 case Opt_quota:
389 case Opt_usrquota: 516 case Opt_usrquota:
390 set_opt(sbi, USRQUOTA); 517 set_opt(sbi, USRQUOTA);
391 break; 518 break;
392 case Opt_grpquota: 519 case Opt_grpquota:
393 set_opt(sbi, GRPQUOTA); 520 set_opt(sbi, GRPQUOTA);
394 break; 521 break;
522 case Opt_prjquota:
523 set_opt(sbi, PRJQUOTA);
524 break;
525 case Opt_usrjquota:
526 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
527 if (ret)
528 return ret;
529 break;
530 case Opt_grpjquota:
531 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
532 if (ret)
533 return ret;
534 break;
535 case Opt_prjjquota:
536 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
537 if (ret)
538 return ret;
539 break;
540 case Opt_offusrjquota:
541 ret = f2fs_clear_qf_name(sb, USRQUOTA);
542 if (ret)
543 return ret;
544 break;
545 case Opt_offgrpjquota:
546 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
547 if (ret)
548 return ret;
549 break;
550 case Opt_offprjjquota:
551 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
552 if (ret)
553 return ret;
554 break;
555 case Opt_jqfmt_vfsold:
556 sbi->s_jquota_fmt = QFMT_VFS_OLD;
557 break;
558 case Opt_jqfmt_vfsv0:
559 sbi->s_jquota_fmt = QFMT_VFS_V0;
560 break;
561 case Opt_jqfmt_vfsv1:
562 sbi->s_jquota_fmt = QFMT_VFS_V1;
563 break;
564 case Opt_noquota:
565 clear_opt(sbi, QUOTA);
566 clear_opt(sbi, USRQUOTA);
567 clear_opt(sbi, GRPQUOTA);
568 clear_opt(sbi, PRJQUOTA);
569 break;
395#else 570#else
571 case Opt_quota:
396 case Opt_usrquota: 572 case Opt_usrquota:
397 case Opt_grpquota: 573 case Opt_grpquota:
574 case Opt_prjquota:
575 case Opt_usrjquota:
576 case Opt_grpjquota:
577 case Opt_prjjquota:
578 case Opt_offusrjquota:
579 case Opt_offgrpjquota:
580 case Opt_offprjjquota:
581 case Opt_jqfmt_vfsold:
582 case Opt_jqfmt_vfsv0:
583 case Opt_jqfmt_vfsv1:
584 case Opt_noquota:
398 f2fs_msg(sb, KERN_INFO, 585 f2fs_msg(sb, KERN_INFO,
399 "quota operations not supported"); 586 "quota operations not supported");
400 break; 587 break;
@@ -406,6 +593,10 @@ static int parse_options(struct super_block *sb, char *options)
406 return -EINVAL; 593 return -EINVAL;
407 } 594 }
408 } 595 }
596#ifdef CONFIG_QUOTA
597 if (f2fs_check_quota_options(sbi))
598 return -EINVAL;
599#endif
409 600
410 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) { 601 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
411 f2fs_msg(sb, KERN_ERR, 602 f2fs_msg(sb, KERN_ERR,
@@ -439,6 +630,7 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb)
439 init_rwsem(&fi->dio_rwsem[READ]); 630 init_rwsem(&fi->dio_rwsem[READ]);
440 init_rwsem(&fi->dio_rwsem[WRITE]); 631 init_rwsem(&fi->dio_rwsem[WRITE]);
441 init_rwsem(&fi->i_mmap_sem); 632 init_rwsem(&fi->i_mmap_sem);
633 init_rwsem(&fi->i_xattr_sem);
442 634
443#ifdef CONFIG_QUOTA 635#ifdef CONFIG_QUOTA
444 memset(&fi->i_dquot, 0, sizeof(fi->i_dquot)); 636 memset(&fi->i_dquot, 0, sizeof(fi->i_dquot));
@@ -446,6 +638,7 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb)
446#endif 638#endif
447 /* Will be used by directory only */ 639 /* Will be used by directory only */
448 fi->i_dir_level = F2FS_SB(sb)->dir_level; 640 fi->i_dir_level = F2FS_SB(sb)->dir_level;
641
449 return &fi->vfs_inode; 642 return &fi->vfs_inode;
450} 643}
451 644
@@ -584,7 +777,6 @@ static void destroy_device_list(struct f2fs_sb_info *sbi)
584 kfree(sbi->devs); 777 kfree(sbi->devs);
585} 778}
586 779
587static void f2fs_quota_off_umount(struct super_block *sb);
588static void f2fs_put_super(struct super_block *sb) 780static void f2fs_put_super(struct super_block *sb)
589{ 781{
590 struct f2fs_sb_info *sbi = F2FS_SB(sb); 782 struct f2fs_sb_info *sbi = F2FS_SB(sb);
@@ -642,7 +834,7 @@ static void f2fs_put_super(struct super_block *sb)
642 834
643 kfree(sbi->ckpt); 835 kfree(sbi->ckpt);
644 836
645 f2fs_exit_sysfs(sbi); 837 f2fs_unregister_sysfs(sbi);
646 838
647 sb->s_fs_info = NULL; 839 sb->s_fs_info = NULL;
648 if (sbi->s_chksum_driver) 840 if (sbi->s_chksum_driver)
@@ -651,6 +843,10 @@ static void f2fs_put_super(struct super_block *sb)
651 843
652 destroy_device_list(sbi); 844 destroy_device_list(sbi);
653 mempool_destroy(sbi->write_io_dummy); 845 mempool_destroy(sbi->write_io_dummy);
846#ifdef CONFIG_QUOTA
847 for (i = 0; i < MAXQUOTAS; i++)
848 kfree(sbi->s_qf_names[i]);
849#endif
654 destroy_percpu_info(sbi); 850 destroy_percpu_info(sbi);
655 for (i = 0; i < NR_PAGE_TYPE; i++) 851 for (i = 0; i < NR_PAGE_TYPE; i++)
656 kfree(sbi->write_io[i]); 852 kfree(sbi->write_io[i]);
@@ -664,6 +860,9 @@ int f2fs_sync_fs(struct super_block *sb, int sync)
664 860
665 trace_f2fs_sync_fs(sb, sync); 861 trace_f2fs_sync_fs(sb, sync);
666 862
863 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
864 return -EAGAIN;
865
667 if (sync) { 866 if (sync) {
668 struct cp_control cpc; 867 struct cp_control cpc;
669 868
@@ -698,6 +897,48 @@ static int f2fs_unfreeze(struct super_block *sb)
698 return 0; 897 return 0;
699} 898}
700 899
900#ifdef CONFIG_QUOTA
901static int f2fs_statfs_project(struct super_block *sb,
902 kprojid_t projid, struct kstatfs *buf)
903{
904 struct kqid qid;
905 struct dquot *dquot;
906 u64 limit;
907 u64 curblock;
908
909 qid = make_kqid_projid(projid);
910 dquot = dqget(sb, qid);
911 if (IS_ERR(dquot))
912 return PTR_ERR(dquot);
913 spin_lock(&dq_data_lock);
914
915 limit = (dquot->dq_dqb.dqb_bsoftlimit ?
916 dquot->dq_dqb.dqb_bsoftlimit :
917 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
918 if (limit && buf->f_blocks > limit) {
919 curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
920 buf->f_blocks = limit;
921 buf->f_bfree = buf->f_bavail =
922 (buf->f_blocks > curblock) ?
923 (buf->f_blocks - curblock) : 0;
924 }
925
926 limit = dquot->dq_dqb.dqb_isoftlimit ?
927 dquot->dq_dqb.dqb_isoftlimit :
928 dquot->dq_dqb.dqb_ihardlimit;
929 if (limit && buf->f_files > limit) {
930 buf->f_files = limit;
931 buf->f_ffree =
932 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
933 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
934 }
935
936 spin_unlock(&dq_data_lock);
937 dqput(dquot);
938 return 0;
939}
940#endif
941
701static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) 942static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
702{ 943{
703 struct super_block *sb = dentry->d_sb; 944 struct super_block *sb = dentry->d_sb;
@@ -733,9 +974,49 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
733 buf->f_fsid.val[0] = (u32)id; 974 buf->f_fsid.val[0] = (u32)id;
734 buf->f_fsid.val[1] = (u32)(id >> 32); 975 buf->f_fsid.val[1] = (u32)(id >> 32);
735 976
977#ifdef CONFIG_QUOTA
978 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
979 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
980 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
981 }
982#endif
736 return 0; 983 return 0;
737} 984}
738 985
986static inline void f2fs_show_quota_options(struct seq_file *seq,
987 struct super_block *sb)
988{
989#ifdef CONFIG_QUOTA
990 struct f2fs_sb_info *sbi = F2FS_SB(sb);
991
992 if (sbi->s_jquota_fmt) {
993 char *fmtname = "";
994
995 switch (sbi->s_jquota_fmt) {
996 case QFMT_VFS_OLD:
997 fmtname = "vfsold";
998 break;
999 case QFMT_VFS_V0:
1000 fmtname = "vfsv0";
1001 break;
1002 case QFMT_VFS_V1:
1003 fmtname = "vfsv1";
1004 break;
1005 }
1006 seq_printf(seq, ",jqfmt=%s", fmtname);
1007 }
1008
1009 if (sbi->s_qf_names[USRQUOTA])
1010 seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
1011
1012 if (sbi->s_qf_names[GRPQUOTA])
1013 seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
1014
1015 if (sbi->s_qf_names[PRJQUOTA])
1016 seq_show_option(seq, "prjjquota", sbi->s_qf_names[PRJQUOTA]);
1017#endif
1018}
1019
739static int f2fs_show_options(struct seq_file *seq, struct dentry *root) 1020static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
740{ 1021{
741 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb); 1022 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
@@ -809,11 +1090,16 @@ static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
809 sbi->fault_info.inject_rate); 1090 sbi->fault_info.inject_rate);
810#endif 1091#endif
811#ifdef CONFIG_QUOTA 1092#ifdef CONFIG_QUOTA
1093 if (test_opt(sbi, QUOTA))
1094 seq_puts(seq, ",quota");
812 if (test_opt(sbi, USRQUOTA)) 1095 if (test_opt(sbi, USRQUOTA))
813 seq_puts(seq, ",usrquota"); 1096 seq_puts(seq, ",usrquota");
814 if (test_opt(sbi, GRPQUOTA)) 1097 if (test_opt(sbi, GRPQUOTA))
815 seq_puts(seq, ",grpquota"); 1098 seq_puts(seq, ",grpquota");
1099 if (test_opt(sbi, PRJQUOTA))
1100 seq_puts(seq, ",prjquota");
816#endif 1101#endif
1102 f2fs_show_quota_options(seq, sbi->sb);
817 1103
818 return 0; 1104 return 0;
819} 1105}
@@ -862,6 +1148,11 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
862#ifdef CONFIG_F2FS_FAULT_INJECTION 1148#ifdef CONFIG_F2FS_FAULT_INJECTION
863 struct f2fs_fault_info ffi = sbi->fault_info; 1149 struct f2fs_fault_info ffi = sbi->fault_info;
864#endif 1150#endif
1151#ifdef CONFIG_QUOTA
1152 int s_jquota_fmt;
1153 char *s_qf_names[MAXQUOTAS];
1154 int i, j;
1155#endif
865 1156
866 /* 1157 /*
867 * Save the old mount options in case we 1158 * Save the old mount options in case we
@@ -871,6 +1162,23 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
871 old_sb_flags = sb->s_flags; 1162 old_sb_flags = sb->s_flags;
872 active_logs = sbi->active_logs; 1163 active_logs = sbi->active_logs;
873 1164
1165#ifdef CONFIG_QUOTA
1166 s_jquota_fmt = sbi->s_jquota_fmt;
1167 for (i = 0; i < MAXQUOTAS; i++) {
1168 if (sbi->s_qf_names[i]) {
1169 s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
1170 GFP_KERNEL);
1171 if (!s_qf_names[i]) {
1172 for (j = 0; j < i; j++)
1173 kfree(s_qf_names[j]);
1174 return -ENOMEM;
1175 }
1176 } else {
1177 s_qf_names[i] = NULL;
1178 }
1179 }
1180#endif
1181
874 /* recover superblocks we couldn't write due to previous RO mount */ 1182 /* recover superblocks we couldn't write due to previous RO mount */
875 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) { 1183 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
876 err = f2fs_commit_super(sbi, false); 1184 err = f2fs_commit_super(sbi, false);
@@ -952,6 +1260,11 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
952 goto restore_gc; 1260 goto restore_gc;
953 } 1261 }
954skip: 1262skip:
1263#ifdef CONFIG_QUOTA
1264 /* Release old quota file names */
1265 for (i = 0; i < MAXQUOTAS; i++)
1266 kfree(s_qf_names[i]);
1267#endif
955 /* Update the POSIXACL Flag */ 1268 /* Update the POSIXACL Flag */
956 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) | 1269 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
957 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0); 1270 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
@@ -966,6 +1279,13 @@ restore_gc:
966 stop_gc_thread(sbi); 1279 stop_gc_thread(sbi);
967 } 1280 }
968restore_opts: 1281restore_opts:
1282#ifdef CONFIG_QUOTA
1283 sbi->s_jquota_fmt = s_jquota_fmt;
1284 for (i = 0; i < MAXQUOTAS; i++) {
1285 kfree(sbi->s_qf_names[i]);
1286 sbi->s_qf_names[i] = s_qf_names[i];
1287 }
1288#endif
969 sbi->mount_opt = org_mount_opt; 1289 sbi->mount_opt = org_mount_opt;
970 sbi->active_logs = active_logs; 1290 sbi->active_logs = active_logs;
971 sb->s_flags = old_sb_flags; 1291 sb->s_flags = old_sb_flags;
@@ -1065,7 +1385,7 @@ static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1065 } 1385 }
1066 1386
1067 if (len == towrite) 1387 if (len == towrite)
1068 return err; 1388 return 0;
1069 inode->i_version++; 1389 inode->i_version++;
1070 inode->i_mtime = inode->i_ctime = current_time(inode); 1390 inode->i_mtime = inode->i_ctime = current_time(inode);
1071 f2fs_mark_inode_dirty_sync(inode, false); 1391 f2fs_mark_inode_dirty_sync(inode, false);
@@ -1082,6 +1402,27 @@ static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1082 return &F2FS_I(inode)->i_reserved_quota; 1402 return &F2FS_I(inode)->i_reserved_quota;
1083} 1403}
1084 1404
1405static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1406{
1407 return dquot_quota_on_mount(sbi->sb, sbi->s_qf_names[type],
1408 sbi->s_jquota_fmt, type);
1409}
1410
1411void f2fs_enable_quota_files(struct f2fs_sb_info *sbi)
1412{
1413 int i, ret;
1414
1415 for (i = 0; i < MAXQUOTAS; i++) {
1416 if (sbi->s_qf_names[i]) {
1417 ret = f2fs_quota_on_mount(sbi, i);
1418 if (ret < 0)
1419 f2fs_msg(sbi->sb, KERN_ERR,
1420 "Cannot turn on journaled "
1421 "quota: error %d", ret);
1422 }
1423 }
1424}
1425
1085static int f2fs_quota_sync(struct super_block *sb, int type) 1426static int f2fs_quota_sync(struct super_block *sb, int type)
1086{ 1427{
1087 struct quota_info *dqopt = sb_dqopt(sb); 1428 struct quota_info *dqopt = sb_dqopt(sb);
@@ -1119,7 +1460,7 @@ static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1119 struct inode *inode; 1460 struct inode *inode;
1120 int err; 1461 int err;
1121 1462
1122 err = f2fs_quota_sync(sb, -1); 1463 err = f2fs_quota_sync(sb, type);
1123 if (err) 1464 if (err)
1124 return err; 1465 return err;
1125 1466
@@ -1147,7 +1488,7 @@ static int f2fs_quota_off(struct super_block *sb, int type)
1147 if (!inode || !igrab(inode)) 1488 if (!inode || !igrab(inode))
1148 return dquot_quota_off(sb, type); 1489 return dquot_quota_off(sb, type);
1149 1490
1150 f2fs_quota_sync(sb, -1); 1491 f2fs_quota_sync(sb, type);
1151 1492
1152 err = dquot_quota_off(sb, type); 1493 err = dquot_quota_off(sb, type);
1153 if (err) 1494 if (err)
@@ -1163,7 +1504,7 @@ out_put:
1163 return err; 1504 return err;
1164} 1505}
1165 1506
1166static void f2fs_quota_off_umount(struct super_block *sb) 1507void f2fs_quota_off_umount(struct super_block *sb)
1167{ 1508{
1168 int type; 1509 int type;
1169 1510
@@ -1171,6 +1512,12 @@ static void f2fs_quota_off_umount(struct super_block *sb)
1171 f2fs_quota_off(sb, type); 1512 f2fs_quota_off(sb, type);
1172} 1513}
1173 1514
1515int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
1516{
1517 *projid = F2FS_I(inode)->i_projid;
1518 return 0;
1519}
1520
1174static const struct dquot_operations f2fs_quota_operations = { 1521static const struct dquot_operations f2fs_quota_operations = {
1175 .get_reserved_space = f2fs_get_reserved_space, 1522 .get_reserved_space = f2fs_get_reserved_space,
1176 .write_dquot = dquot_commit, 1523 .write_dquot = dquot_commit,
@@ -1180,6 +1527,7 @@ static const struct dquot_operations f2fs_quota_operations = {
1180 .write_info = dquot_commit_info, 1527 .write_info = dquot_commit_info,
1181 .alloc_dquot = dquot_alloc, 1528 .alloc_dquot = dquot_alloc,
1182 .destroy_dquot = dquot_destroy, 1529 .destroy_dquot = dquot_destroy,
1530 .get_projid = f2fs_get_projid,
1183 .get_next_id = dquot_get_next_id, 1531 .get_next_id = dquot_get_next_id,
1184}; 1532};
1185 1533
@@ -1194,12 +1542,12 @@ static const struct quotactl_ops f2fs_quotactl_ops = {
1194 .get_nextdqblk = dquot_get_next_dqblk, 1542 .get_nextdqblk = dquot_get_next_dqblk,
1195}; 1543};
1196#else 1544#else
1197static inline void f2fs_quota_off_umount(struct super_block *sb) 1545void f2fs_quota_off_umount(struct super_block *sb)
1198{ 1546{
1199} 1547}
1200#endif 1548#endif
1201 1549
1202static struct super_operations f2fs_sops = { 1550static const struct super_operations f2fs_sops = {
1203 .alloc_inode = f2fs_alloc_inode, 1551 .alloc_inode = f2fs_alloc_inode,
1204 .drop_inode = f2fs_drop_inode, 1552 .drop_inode = f2fs_drop_inode,
1205 .destroy_inode = f2fs_destroy_inode, 1553 .destroy_inode = f2fs_destroy_inode,
@@ -1303,9 +1651,16 @@ static const struct export_operations f2fs_export_ops = {
1303 1651
1304static loff_t max_file_blocks(void) 1652static loff_t max_file_blocks(void)
1305{ 1653{
1306 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS); 1654 loff_t result = 0;
1307 loff_t leaf_count = ADDRS_PER_BLOCK; 1655 loff_t leaf_count = ADDRS_PER_BLOCK;
1308 1656
1657 /*
1658 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
1659 * F2FS_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
1660 * space in inode.i_addr, it will be more safe to reassign
1661 * result as zero.
1662 */
1663
1309 /* two direct node blocks */ 1664 /* two direct node blocks */
1310 result += (leaf_count * 2); 1665 result += (leaf_count * 2);
1311 1666
@@ -1922,6 +2277,11 @@ try_onemore:
1922 sb->s_fs_info = sbi; 2277 sb->s_fs_info = sbi;
1923 sbi->raw_super = raw_super; 2278 sbi->raw_super = raw_super;
1924 2279
2280 /* precompute checksum seed for metadata */
2281 if (f2fs_sb_has_inode_chksum(sb))
2282 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
2283 sizeof(raw_super->uuid));
2284
1925 /* 2285 /*
1926 * The BLKZONED feature indicates that the drive was formatted with 2286 * The BLKZONED feature indicates that the drive was formatted with
1927 * zone alignment optimization. This is optional for host-aware 2287 * zone alignment optimization. This is optional for host-aware
@@ -1956,7 +2316,7 @@ try_onemore:
1956#ifdef CONFIG_QUOTA 2316#ifdef CONFIG_QUOTA
1957 sb->dq_op = &f2fs_quota_operations; 2317 sb->dq_op = &f2fs_quota_operations;
1958 sb->s_qcop = &f2fs_quotactl_ops; 2318 sb->s_qcop = &f2fs_quotactl_ops;
1959 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; 2319 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1960#endif 2320#endif
1961 2321
1962 sb->s_op = &f2fs_sops; 2322 sb->s_op = &f2fs_sops;
@@ -1980,6 +2340,10 @@ try_onemore:
1980 set_sbi_flag(sbi, SBI_POR_DOING); 2340 set_sbi_flag(sbi, SBI_POR_DOING);
1981 spin_lock_init(&sbi->stat_lock); 2341 spin_lock_init(&sbi->stat_lock);
1982 2342
2343 /* init iostat info */
2344 spin_lock_init(&sbi->iostat_lock);
2345 sbi->iostat_enable = false;
2346
1983 for (i = 0; i < NR_PAGE_TYPE; i++) { 2347 for (i = 0; i < NR_PAGE_TYPE; i++) {
1984 int n = (i == META) ? 1: NR_TEMP_TYPE; 2348 int n = (i == META) ? 1: NR_TEMP_TYPE;
1985 int j; 2349 int j;
@@ -2098,11 +2462,6 @@ try_onemore:
2098 if (err) 2462 if (err)
2099 goto free_nm; 2463 goto free_nm;
2100 2464
2101 /* if there are nt orphan nodes free them */
2102 err = recover_orphan_inodes(sbi);
2103 if (err)
2104 goto free_node_inode;
2105
2106 /* read root inode and dentry */ 2465 /* read root inode and dentry */
2107 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi)); 2466 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
2108 if (IS_ERR(root)) { 2467 if (IS_ERR(root)) {
@@ -2122,10 +2481,15 @@ try_onemore:
2122 goto free_root_inode; 2481 goto free_root_inode;
2123 } 2482 }
2124 2483
2125 err = f2fs_init_sysfs(sbi); 2484 err = f2fs_register_sysfs(sbi);
2126 if (err) 2485 if (err)
2127 goto free_root_inode; 2486 goto free_root_inode;
2128 2487
2488 /* if there are nt orphan nodes free them */
2489 err = recover_orphan_inodes(sbi);
2490 if (err)
2491 goto free_sysfs;
2492
2129 /* recover fsynced data */ 2493 /* recover fsynced data */
2130 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) { 2494 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
2131 /* 2495 /*
@@ -2135,7 +2499,7 @@ try_onemore:
2135 if (bdev_read_only(sb->s_bdev) && 2499 if (bdev_read_only(sb->s_bdev) &&
2136 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { 2500 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
2137 err = -EROFS; 2501 err = -EROFS;
2138 goto free_sysfs; 2502 goto free_meta;
2139 } 2503 }
2140 2504
2141 if (need_fsck) 2505 if (need_fsck)
@@ -2149,7 +2513,7 @@ try_onemore:
2149 need_fsck = true; 2513 need_fsck = true;
2150 f2fs_msg(sb, KERN_ERR, 2514 f2fs_msg(sb, KERN_ERR,
2151 "Cannot recover all fsync data errno=%d", err); 2515 "Cannot recover all fsync data errno=%d", err);
2152 goto free_sysfs; 2516 goto free_meta;
2153 } 2517 }
2154 } else { 2518 } else {
2155 err = recover_fsync_data(sbi, true); 2519 err = recover_fsync_data(sbi, true);
@@ -2173,7 +2537,7 @@ skip_recovery:
2173 /* After POR, we can run background GC thread.*/ 2537 /* After POR, we can run background GC thread.*/
2174 err = start_gc_thread(sbi); 2538 err = start_gc_thread(sbi);
2175 if (err) 2539 if (err)
2176 goto free_sysfs; 2540 goto free_meta;
2177 } 2541 }
2178 kfree(options); 2542 kfree(options);
2179 2543
@@ -2191,9 +2555,17 @@ skip_recovery:
2191 f2fs_update_time(sbi, REQ_TIME); 2555 f2fs_update_time(sbi, REQ_TIME);
2192 return 0; 2556 return 0;
2193 2557
2194free_sysfs: 2558free_meta:
2195 f2fs_sync_inode_meta(sbi); 2559 f2fs_sync_inode_meta(sbi);
2196 f2fs_exit_sysfs(sbi); 2560 /*
2561 * Some dirty meta pages can be produced by recover_orphan_inodes()
2562 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
2563 * followed by write_checkpoint() through f2fs_write_node_pages(), which
2564 * falls into an infinite loop in sync_meta_pages().
2565 */
2566 truncate_inode_pages_final(META_MAPPING(sbi));
2567free_sysfs:
2568 f2fs_unregister_sysfs(sbi);
2197free_root_inode: 2569free_root_inode:
2198 dput(sb->s_root); 2570 dput(sb->s_root);
2199 sb->s_root = NULL; 2571 sb->s_root = NULL;
@@ -2202,13 +2574,6 @@ free_node_inode:
2202 mutex_lock(&sbi->umount_mutex); 2574 mutex_lock(&sbi->umount_mutex);
2203 release_ino_entry(sbi, true); 2575 release_ino_entry(sbi, true);
2204 f2fs_leave_shrinker(sbi); 2576 f2fs_leave_shrinker(sbi);
2205 /*
2206 * Some dirty meta pages can be produced by recover_orphan_inodes()
2207 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
2208 * followed by write_checkpoint() through f2fs_write_node_pages(), which
2209 * falls into an infinite loop in sync_meta_pages().
2210 */
2211 truncate_inode_pages_final(META_MAPPING(sbi));
2212 iput(sbi->node_inode); 2577 iput(sbi->node_inode);
2213 mutex_unlock(&sbi->umount_mutex); 2578 mutex_unlock(&sbi->umount_mutex);
2214 f2fs_destroy_stats(sbi); 2579 f2fs_destroy_stats(sbi);
@@ -2228,6 +2593,10 @@ free_options:
2228 for (i = 0; i < NR_PAGE_TYPE; i++) 2593 for (i = 0; i < NR_PAGE_TYPE; i++)
2229 kfree(sbi->write_io[i]); 2594 kfree(sbi->write_io[i]);
2230 destroy_percpu_info(sbi); 2595 destroy_percpu_info(sbi);
2596#ifdef CONFIG_QUOTA
2597 for (i = 0; i < MAXQUOTAS; i++)
2598 kfree(sbi->s_qf_names[i]);
2599#endif
2231 kfree(options); 2600 kfree(options);
2232free_sb_buf: 2601free_sb_buf:
2233 kfree(raw_super); 2602 kfree(raw_super);
@@ -2311,7 +2680,7 @@ static int __init init_f2fs_fs(void)
2311 err = create_extent_cache(); 2680 err = create_extent_cache();
2312 if (err) 2681 if (err)
2313 goto free_checkpoint_caches; 2682 goto free_checkpoint_caches;
2314 err = f2fs_register_sysfs(); 2683 err = f2fs_init_sysfs();
2315 if (err) 2684 if (err)
2316 goto free_extent_cache; 2685 goto free_extent_cache;
2317 err = register_shrinker(&f2fs_shrinker_info); 2686 err = register_shrinker(&f2fs_shrinker_info);
@@ -2330,7 +2699,7 @@ free_filesystem:
2330free_shrinker: 2699free_shrinker:
2331 unregister_shrinker(&f2fs_shrinker_info); 2700 unregister_shrinker(&f2fs_shrinker_info);
2332free_sysfs: 2701free_sysfs:
2333 f2fs_unregister_sysfs(); 2702 f2fs_exit_sysfs();
2334free_extent_cache: 2703free_extent_cache:
2335 destroy_extent_cache(); 2704 destroy_extent_cache();
2336free_checkpoint_caches: 2705free_checkpoint_caches:
@@ -2350,7 +2719,7 @@ static void __exit exit_f2fs_fs(void)
2350 f2fs_destroy_root_stats(); 2719 f2fs_destroy_root_stats();
2351 unregister_filesystem(&f2fs_fs_type); 2720 unregister_filesystem(&f2fs_fs_type);
2352 unregister_shrinker(&f2fs_shrinker_info); 2721 unregister_shrinker(&f2fs_shrinker_info);
2353 f2fs_unregister_sysfs(); 2722 f2fs_exit_sysfs();
2354 destroy_extent_cache(); 2723 destroy_extent_cache();
2355 destroy_checkpoint_caches(); 2724 destroy_checkpoint_caches();
2356 destroy_segment_manager_caches(); 2725 destroy_segment_manager_caches();
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index 71191d89917d..e2c258f717cd 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -18,7 +18,6 @@
18#include "gc.h" 18#include "gc.h"
19 19
20static struct proc_dir_entry *f2fs_proc_root; 20static struct proc_dir_entry *f2fs_proc_root;
21static struct kset *f2fs_kset;
22 21
23/* Sysfs support for f2fs */ 22/* Sysfs support for f2fs */
24enum { 23enum {
@@ -41,6 +40,7 @@ struct f2fs_attr {
41 const char *, size_t); 40 const char *, size_t);
42 int struct_type; 41 int struct_type;
43 int offset; 42 int offset;
43 int id;
44}; 44};
45 45
46static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type) 46static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
@@ -76,6 +76,34 @@ static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
76 BD_PART_WRITTEN(sbi))); 76 BD_PART_WRITTEN(sbi)));
77} 77}
78 78
79static ssize_t features_show(struct f2fs_attr *a,
80 struct f2fs_sb_info *sbi, char *buf)
81{
82 struct super_block *sb = sbi->sb;
83 int len = 0;
84
85 if (!sb->s_bdev->bd_part)
86 return snprintf(buf, PAGE_SIZE, "0\n");
87
88 if (f2fs_sb_has_crypto(sb))
89 len += snprintf(buf, PAGE_SIZE - len, "%s",
90 "encryption");
91 if (f2fs_sb_mounted_blkzoned(sb))
92 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
93 len ? ", " : "", "blkzoned");
94 if (f2fs_sb_has_extra_attr(sb))
95 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
96 len ? ", " : "", "extra_attr");
97 if (f2fs_sb_has_project_quota(sb))
98 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
99 len ? ", " : "", "projquota");
100 if (f2fs_sb_has_inode_chksum(sb))
101 len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
102 len ? ", " : "", "inode_checksum");
103 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
104 return len;
105}
106
79static ssize_t f2fs_sbi_show(struct f2fs_attr *a, 107static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
80 struct f2fs_sb_info *sbi, char *buf) 108 struct f2fs_sb_info *sbi, char *buf)
81{ 109{
@@ -124,7 +152,39 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
124 spin_unlock(&sbi->stat_lock); 152 spin_unlock(&sbi->stat_lock);
125 return count; 153 return count;
126 } 154 }
155
156 if (!strcmp(a->attr.name, "discard_granularity")) {
157 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
158 int i;
159
160 if (t == 0 || t > MAX_PLIST_NUM)
161 return -EINVAL;
162 if (t == *ui)
163 return count;
164
165 mutex_lock(&dcc->cmd_lock);
166 for (i = 0; i < MAX_PLIST_NUM; i++) {
167 if (i >= t - 1)
168 dcc->pend_list_tag[i] |= P_ACTIVE;
169 else
170 dcc->pend_list_tag[i] &= (~P_ACTIVE);
171 }
172 mutex_unlock(&dcc->cmd_lock);
173
174 *ui = t;
175 return count;
176 }
177
127 *ui = t; 178 *ui = t;
179
180 if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
181 f2fs_reset_iostat(sbi);
182 if (!strcmp(a->attr.name, "gc_urgent") && t == 1 && sbi->gc_thread) {
183 sbi->gc_thread->gc_wake = 1;
184 wake_up_interruptible_all(&sbi->gc_thread->gc_wait_queue_head);
185 wake_up_discard_thread(sbi, true);
186 }
187
128 return count; 188 return count;
129} 189}
130 190
@@ -155,6 +215,30 @@ static void f2fs_sb_release(struct kobject *kobj)
155 complete(&sbi->s_kobj_unregister); 215 complete(&sbi->s_kobj_unregister);
156} 216}
157 217
218enum feat_id {
219 FEAT_CRYPTO = 0,
220 FEAT_BLKZONED,
221 FEAT_ATOMIC_WRITE,
222 FEAT_EXTRA_ATTR,
223 FEAT_PROJECT_QUOTA,
224 FEAT_INODE_CHECKSUM,
225};
226
227static ssize_t f2fs_feature_show(struct f2fs_attr *a,
228 struct f2fs_sb_info *sbi, char *buf)
229{
230 switch (a->id) {
231 case FEAT_CRYPTO:
232 case FEAT_BLKZONED:
233 case FEAT_ATOMIC_WRITE:
234 case FEAT_EXTRA_ATTR:
235 case FEAT_PROJECT_QUOTA:
236 case FEAT_INODE_CHECKSUM:
237 return snprintf(buf, PAGE_SIZE, "supported\n");
238 }
239 return 0;
240}
241
158#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \ 242#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
159static struct f2fs_attr f2fs_attr_##_name = { \ 243static struct f2fs_attr f2fs_attr_##_name = { \
160 .attr = {.name = __stringify(_name), .mode = _mode }, \ 244 .attr = {.name = __stringify(_name), .mode = _mode }, \
@@ -172,12 +256,23 @@ static struct f2fs_attr f2fs_attr_##_name = { \
172#define F2FS_GENERAL_RO_ATTR(name) \ 256#define F2FS_GENERAL_RO_ATTR(name) \
173static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL) 257static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
174 258
259#define F2FS_FEATURE_RO_ATTR(_name, _id) \
260static struct f2fs_attr f2fs_attr_##_name = { \
261 .attr = {.name = __stringify(_name), .mode = 0444 }, \
262 .show = f2fs_feature_show, \
263 .id = _id, \
264}
265
266F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
267 urgent_sleep_time);
175F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time); 268F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
176F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time); 269F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
177F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time); 270F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
178F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle); 271F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
272F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent, gc_urgent);
179F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments); 273F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
180F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards); 274F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
275F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
181F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks); 276F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
182F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections); 277F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
183F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy); 278F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
@@ -191,20 +286,36 @@ F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
191F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level); 286F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
192F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]); 287F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
193F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]); 288F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
289F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
194#ifdef CONFIG_F2FS_FAULT_INJECTION 290#ifdef CONFIG_F2FS_FAULT_INJECTION
195F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate); 291F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
196F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type); 292F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
197#endif 293#endif
198F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes); 294F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
295F2FS_GENERAL_RO_ATTR(features);
296
297#ifdef CONFIG_F2FS_FS_ENCRYPTION
298F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
299#endif
300#ifdef CONFIG_BLK_DEV_ZONED
301F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
302#endif
303F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
304F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
305F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
306F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
199 307
200#define ATTR_LIST(name) (&f2fs_attr_##name.attr) 308#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
201static struct attribute *f2fs_attrs[] = { 309static struct attribute *f2fs_attrs[] = {
310 ATTR_LIST(gc_urgent_sleep_time),
202 ATTR_LIST(gc_min_sleep_time), 311 ATTR_LIST(gc_min_sleep_time),
203 ATTR_LIST(gc_max_sleep_time), 312 ATTR_LIST(gc_max_sleep_time),
204 ATTR_LIST(gc_no_gc_sleep_time), 313 ATTR_LIST(gc_no_gc_sleep_time),
205 ATTR_LIST(gc_idle), 314 ATTR_LIST(gc_idle),
315 ATTR_LIST(gc_urgent),
206 ATTR_LIST(reclaim_segments), 316 ATTR_LIST(reclaim_segments),
207 ATTR_LIST(max_small_discards), 317 ATTR_LIST(max_small_discards),
318 ATTR_LIST(discard_granularity),
208 ATTR_LIST(batched_trim_sections), 319 ATTR_LIST(batched_trim_sections),
209 ATTR_LIST(ipu_policy), 320 ATTR_LIST(ipu_policy),
210 ATTR_LIST(min_ipu_util), 321 ATTR_LIST(min_ipu_util),
@@ -217,26 +328,59 @@ static struct attribute *f2fs_attrs[] = {
217 ATTR_LIST(dirty_nats_ratio), 328 ATTR_LIST(dirty_nats_ratio),
218 ATTR_LIST(cp_interval), 329 ATTR_LIST(cp_interval),
219 ATTR_LIST(idle_interval), 330 ATTR_LIST(idle_interval),
331 ATTR_LIST(iostat_enable),
220#ifdef CONFIG_F2FS_FAULT_INJECTION 332#ifdef CONFIG_F2FS_FAULT_INJECTION
221 ATTR_LIST(inject_rate), 333 ATTR_LIST(inject_rate),
222 ATTR_LIST(inject_type), 334 ATTR_LIST(inject_type),
223#endif 335#endif
224 ATTR_LIST(lifetime_write_kbytes), 336 ATTR_LIST(lifetime_write_kbytes),
337 ATTR_LIST(features),
225 ATTR_LIST(reserved_blocks), 338 ATTR_LIST(reserved_blocks),
226 NULL, 339 NULL,
227}; 340};
228 341
342static struct attribute *f2fs_feat_attrs[] = {
343#ifdef CONFIG_F2FS_FS_ENCRYPTION
344 ATTR_LIST(encryption),
345#endif
346#ifdef CONFIG_BLK_DEV_ZONED
347 ATTR_LIST(block_zoned),
348#endif
349 ATTR_LIST(atomic_write),
350 ATTR_LIST(extra_attr),
351 ATTR_LIST(project_quota),
352 ATTR_LIST(inode_checksum),
353 NULL,
354};
355
229static const struct sysfs_ops f2fs_attr_ops = { 356static const struct sysfs_ops f2fs_attr_ops = {
230 .show = f2fs_attr_show, 357 .show = f2fs_attr_show,
231 .store = f2fs_attr_store, 358 .store = f2fs_attr_store,
232}; 359};
233 360
234static struct kobj_type f2fs_ktype = { 361static struct kobj_type f2fs_sb_ktype = {
235 .default_attrs = f2fs_attrs, 362 .default_attrs = f2fs_attrs,
236 .sysfs_ops = &f2fs_attr_ops, 363 .sysfs_ops = &f2fs_attr_ops,
237 .release = f2fs_sb_release, 364 .release = f2fs_sb_release,
238}; 365};
239 366
367static struct kobj_type f2fs_ktype = {
368 .sysfs_ops = &f2fs_attr_ops,
369};
370
371static struct kset f2fs_kset = {
372 .kobj = {.ktype = &f2fs_ktype},
373};
374
375static struct kobj_type f2fs_feat_ktype = {
376 .default_attrs = f2fs_feat_attrs,
377 .sysfs_ops = &f2fs_attr_ops,
378};
379
380static struct kobject f2fs_feat = {
381 .kset = &f2fs_kset,
382};
383
240static int segment_info_seq_show(struct seq_file *seq, void *offset) 384static int segment_info_seq_show(struct seq_file *seq, void *offset)
241{ 385{
242 struct super_block *sb = seq->private; 386 struct super_block *sb = seq->private;
@@ -288,6 +432,48 @@ static int segment_bits_seq_show(struct seq_file *seq, void *offset)
288 return 0; 432 return 0;
289} 433}
290 434
435static int iostat_info_seq_show(struct seq_file *seq, void *offset)
436{
437 struct super_block *sb = seq->private;
438 struct f2fs_sb_info *sbi = F2FS_SB(sb);
439 time64_t now = ktime_get_real_seconds();
440
441 if (!sbi->iostat_enable)
442 return 0;
443
444 seq_printf(seq, "time: %-16llu\n", now);
445
446 /* print app IOs */
447 seq_printf(seq, "app buffered: %-16llu\n",
448 sbi->write_iostat[APP_BUFFERED_IO]);
449 seq_printf(seq, "app direct: %-16llu\n",
450 sbi->write_iostat[APP_DIRECT_IO]);
451 seq_printf(seq, "app mapped: %-16llu\n",
452 sbi->write_iostat[APP_MAPPED_IO]);
453
454 /* print fs IOs */
455 seq_printf(seq, "fs data: %-16llu\n",
456 sbi->write_iostat[FS_DATA_IO]);
457 seq_printf(seq, "fs node: %-16llu\n",
458 sbi->write_iostat[FS_NODE_IO]);
459 seq_printf(seq, "fs meta: %-16llu\n",
460 sbi->write_iostat[FS_META_IO]);
461 seq_printf(seq, "fs gc data: %-16llu\n",
462 sbi->write_iostat[FS_GC_DATA_IO]);
463 seq_printf(seq, "fs gc node: %-16llu\n",
464 sbi->write_iostat[FS_GC_NODE_IO]);
465 seq_printf(seq, "fs cp data: %-16llu\n",
466 sbi->write_iostat[FS_CP_DATA_IO]);
467 seq_printf(seq, "fs cp node: %-16llu\n",
468 sbi->write_iostat[FS_CP_NODE_IO]);
469 seq_printf(seq, "fs cp meta: %-16llu\n",
470 sbi->write_iostat[FS_CP_META_IO]);
471 seq_printf(seq, "fs discard: %-16llu\n",
472 sbi->write_iostat[FS_DISCARD]);
473
474 return 0;
475}
476
291#define F2FS_PROC_FILE_DEF(_name) \ 477#define F2FS_PROC_FILE_DEF(_name) \
292static int _name##_open_fs(struct inode *inode, struct file *file) \ 478static int _name##_open_fs(struct inode *inode, struct file *file) \
293{ \ 479{ \
@@ -303,28 +489,47 @@ static const struct file_operations f2fs_seq_##_name##_fops = { \
303 489
304F2FS_PROC_FILE_DEF(segment_info); 490F2FS_PROC_FILE_DEF(segment_info);
305F2FS_PROC_FILE_DEF(segment_bits); 491F2FS_PROC_FILE_DEF(segment_bits);
492F2FS_PROC_FILE_DEF(iostat_info);
306 493
307int __init f2fs_register_sysfs(void) 494int __init f2fs_init_sysfs(void)
308{ 495{
309 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL); 496 int ret;
310 497
311 f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj); 498 kobject_set_name(&f2fs_kset.kobj, "f2fs");
312 if (!f2fs_kset) 499 f2fs_kset.kobj.parent = fs_kobj;
313 return -ENOMEM; 500 ret = kset_register(&f2fs_kset);
314 return 0; 501 if (ret)
502 return ret;
503
504 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
505 NULL, "features");
506 if (ret)
507 kset_unregister(&f2fs_kset);
508 else
509 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
510 return ret;
315} 511}
316 512
317void f2fs_unregister_sysfs(void) 513void f2fs_exit_sysfs(void)
318{ 514{
319 kset_unregister(f2fs_kset); 515 kobject_put(&f2fs_feat);
516 kset_unregister(&f2fs_kset);
320 remove_proc_entry("fs/f2fs", NULL); 517 remove_proc_entry("fs/f2fs", NULL);
518 f2fs_proc_root = NULL;
321} 519}
322 520
323int f2fs_init_sysfs(struct f2fs_sb_info *sbi) 521int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
324{ 522{
325 struct super_block *sb = sbi->sb; 523 struct super_block *sb = sbi->sb;
326 int err; 524 int err;
327 525
526 sbi->s_kobj.kset = &f2fs_kset;
527 init_completion(&sbi->s_kobj_unregister);
528 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
529 "%s", sb->s_id);
530 if (err)
531 return err;
532
328 if (f2fs_proc_root) 533 if (f2fs_proc_root)
329 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root); 534 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
330 535
@@ -333,33 +538,19 @@ int f2fs_init_sysfs(struct f2fs_sb_info *sbi)
333 &f2fs_seq_segment_info_fops, sb); 538 &f2fs_seq_segment_info_fops, sb);
334 proc_create_data("segment_bits", S_IRUGO, sbi->s_proc, 539 proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
335 &f2fs_seq_segment_bits_fops, sb); 540 &f2fs_seq_segment_bits_fops, sb);
541 proc_create_data("iostat_info", S_IRUGO, sbi->s_proc,
542 &f2fs_seq_iostat_info_fops, sb);
336 } 543 }
337
338 sbi->s_kobj.kset = f2fs_kset;
339 init_completion(&sbi->s_kobj_unregister);
340 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
341 "%s", sb->s_id);
342 if (err)
343 goto err_out;
344 return 0; 544 return 0;
345err_out:
346 if (sbi->s_proc) {
347 remove_proc_entry("segment_info", sbi->s_proc);
348 remove_proc_entry("segment_bits", sbi->s_proc);
349 remove_proc_entry(sb->s_id, f2fs_proc_root);
350 }
351 return err;
352} 545}
353 546
354void f2fs_exit_sysfs(struct f2fs_sb_info *sbi) 547void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
355{ 548{
356 kobject_del(&sbi->s_kobj);
357 kobject_put(&sbi->s_kobj);
358 wait_for_completion(&sbi->s_kobj_unregister);
359
360 if (sbi->s_proc) { 549 if (sbi->s_proc) {
550 remove_proc_entry("iostat_info", sbi->s_proc);
361 remove_proc_entry("segment_info", sbi->s_proc); 551 remove_proc_entry("segment_info", sbi->s_proc);
362 remove_proc_entry("segment_bits", sbi->s_proc); 552 remove_proc_entry("segment_bits", sbi->s_proc);
363 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root); 553 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
364 } 554 }
555 kobject_del(&sbi->s_kobj);
365} 556}
diff --git a/fs/f2fs/xattr.c b/fs/f2fs/xattr.c
index 832c5110abab..7c65540148f8 100644
--- a/fs/f2fs/xattr.c
+++ b/fs/f2fs/xattr.c
@@ -442,7 +442,7 @@ static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
442 } else { 442 } else {
443 struct dnode_of_data dn; 443 struct dnode_of_data dn;
444 set_new_dnode(&dn, inode, NULL, NULL, new_nid); 444 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
445 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage); 445 xpage = new_node_page(&dn, XATTR_NODE_OFFSET);
446 if (IS_ERR(xpage)) { 446 if (IS_ERR(xpage)) {
447 alloc_nid_failed(sbi, new_nid); 447 alloc_nid_failed(sbi, new_nid);
448 return PTR_ERR(xpage); 448 return PTR_ERR(xpage);
@@ -473,8 +473,10 @@ int f2fs_getxattr(struct inode *inode, int index, const char *name,
473 if (len > F2FS_NAME_LEN) 473 if (len > F2FS_NAME_LEN)
474 return -ERANGE; 474 return -ERANGE;
475 475
476 down_read(&F2FS_I(inode)->i_xattr_sem);
476 error = lookup_all_xattrs(inode, ipage, index, len, name, 477 error = lookup_all_xattrs(inode, ipage, index, len, name,
477 &entry, &base_addr); 478 &entry, &base_addr);
479 up_read(&F2FS_I(inode)->i_xattr_sem);
478 if (error) 480 if (error)
479 return error; 481 return error;
480 482
@@ -503,7 +505,9 @@ ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
503 int error = 0; 505 int error = 0;
504 size_t rest = buffer_size; 506 size_t rest = buffer_size;
505 507
508 down_read(&F2FS_I(inode)->i_xattr_sem);
506 error = read_all_xattrs(inode, NULL, &base_addr); 509 error = read_all_xattrs(inode, NULL, &base_addr);
510 up_read(&F2FS_I(inode)->i_xattr_sem);
507 if (error) 511 if (error)
508 return error; 512 return error;
509 513
@@ -686,7 +690,9 @@ int f2fs_setxattr(struct inode *inode, int index, const char *name,
686 f2fs_lock_op(sbi); 690 f2fs_lock_op(sbi);
687 /* protect xattr_ver */ 691 /* protect xattr_ver */
688 down_write(&F2FS_I(inode)->i_sem); 692 down_write(&F2FS_I(inode)->i_sem);
693 down_write(&F2FS_I(inode)->i_xattr_sem);
689 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags); 694 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
695 up_write(&F2FS_I(inode)->i_xattr_sem);
690 up_write(&F2FS_I(inode)->i_sem); 696 up_write(&F2FS_I(inode)->i_sem);
691 f2fs_unlock_op(sbi); 697 f2fs_unlock_op(sbi);
692 698
diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
index b6feed6547ce..2a0c453d7235 100644
--- a/include/linux/f2fs_fs.h
+++ b/include/linux/f2fs_fs.h
@@ -186,6 +186,8 @@ struct f2fs_extent {
186#define F2FS_NAME_LEN 255 186#define F2FS_NAME_LEN 255
187#define F2FS_INLINE_XATTR_ADDRS 50 /* 200 bytes for inline xattrs */ 187#define F2FS_INLINE_XATTR_ADDRS 50 /* 200 bytes for inline xattrs */
188#define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */ 188#define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */
189#define CUR_ADDRS_PER_INODE(inode) (DEF_ADDRS_PER_INODE - \
190 get_extra_isize(inode))
189#define DEF_NIDS_PER_INODE 5 /* Node IDs in an Inode */ 191#define DEF_NIDS_PER_INODE 5 /* Node IDs in an Inode */
190#define ADDRS_PER_INODE(inode) addrs_per_inode(inode) 192#define ADDRS_PER_INODE(inode) addrs_per_inode(inode)
191#define ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */ 193#define ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */
@@ -205,9 +207,7 @@ struct f2fs_extent {
205#define F2FS_INLINE_DENTRY 0x04 /* file inline dentry flag */ 207#define F2FS_INLINE_DENTRY 0x04 /* file inline dentry flag */
206#define F2FS_DATA_EXIST 0x08 /* file inline data exist flag */ 208#define F2FS_DATA_EXIST 0x08 /* file inline data exist flag */
207#define F2FS_INLINE_DOTS 0x10 /* file having implicit dot dentries */ 209#define F2FS_INLINE_DOTS 0x10 /* file having implicit dot dentries */
208 210#define F2FS_EXTRA_ATTR 0x20 /* file having extra attribute */
209#define MAX_INLINE_DATA (sizeof(__le32) * (DEF_ADDRS_PER_INODE - \
210 F2FS_INLINE_XATTR_ADDRS - 1))
211 211
212struct f2fs_inode { 212struct f2fs_inode {
213 __le16 i_mode; /* file mode */ 213 __le16 i_mode; /* file mode */
@@ -235,8 +235,16 @@ struct f2fs_inode {
235 235
236 struct f2fs_extent i_ext; /* caching a largest extent */ 236 struct f2fs_extent i_ext; /* caching a largest extent */
237 237
238 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */ 238 union {
239 239 struct {
240 __le16 i_extra_isize; /* extra inode attribute size */
241 __le16 i_padding; /* padding */
242 __le32 i_projid; /* project id */
243 __le32 i_inode_checksum;/* inode meta checksum */
244 __le32 i_extra_end[0]; /* for attribute size calculation */
245 };
246 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */
247 };
240 __le32 i_nid[DEF_NIDS_PER_INODE]; /* direct(2), indirect(2), 248 __le32 i_nid[DEF_NIDS_PER_INODE]; /* direct(2), indirect(2),
241 double_indirect(1) node id */ 249 double_indirect(1) node id */
242} __packed; 250} __packed;
@@ -465,7 +473,7 @@ typedef __le32 f2fs_hash_t;
465#define MAX_DIR_BUCKETS (1 << ((MAX_DIR_HASH_DEPTH / 2) - 1)) 473#define MAX_DIR_BUCKETS (1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
466 474
467/* 475/*
468 * space utilization of regular dentry and inline dentry 476 * space utilization of regular dentry and inline dentry (w/o extra reservation)
469 * regular dentry inline dentry 477 * regular dentry inline dentry
470 * bitmap 1 * 27 = 27 1 * 23 = 23 478 * bitmap 1 * 27 = 27 1 * 23 = 23
471 * reserved 1 * 3 = 3 1 * 7 = 7 479 * reserved 1 * 3 = 3 1 * 7 = 7
@@ -501,24 +509,6 @@ struct f2fs_dentry_block {
501 __u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN]; 509 __u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
502} __packed; 510} __packed;
503 511
504/* for inline dir */
505#define NR_INLINE_DENTRY (MAX_INLINE_DATA * BITS_PER_BYTE / \
506 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
507 BITS_PER_BYTE + 1))
508#define INLINE_DENTRY_BITMAP_SIZE ((NR_INLINE_DENTRY + \
509 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
510#define INLINE_RESERVED_SIZE (MAX_INLINE_DATA - \
511 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
512 NR_INLINE_DENTRY + INLINE_DENTRY_BITMAP_SIZE))
513
514/* inline directory entry structure */
515struct f2fs_inline_dentry {
516 __u8 dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
517 __u8 reserved[INLINE_RESERVED_SIZE];
518 struct f2fs_dir_entry dentry[NR_INLINE_DENTRY];
519 __u8 filename[NR_INLINE_DENTRY][F2FS_SLOT_LEN];
520} __packed;
521
522/* file types used in inode_info->flags */ 512/* file types used in inode_info->flags */
523enum { 513enum {
524 F2FS_FT_UNKNOWN, 514 F2FS_FT_UNKNOWN,
@@ -534,4 +524,6 @@ enum {
534 524
535#define S_SHIFT 12 525#define S_SHIFT 12
536 526
527#define F2FS_DEF_PROJID 0 /* default project ID */
528
537#endif /* _LINUX_F2FS_FS_H */ 529#endif /* _LINUX_F2FS_FS_H */
diff --git a/include/trace/events/f2fs.h b/include/trace/events/f2fs.h
index bc4dd7837e4c..5d216f7fb05a 100644
--- a/include/trace/events/f2fs.h
+++ b/include/trace/events/f2fs.h
@@ -543,14 +543,14 @@ TRACE_EVENT(f2fs_map_blocks,
543 543
544TRACE_EVENT(f2fs_background_gc, 544TRACE_EVENT(f2fs_background_gc,
545 545
546 TP_PROTO(struct super_block *sb, long wait_ms, 546 TP_PROTO(struct super_block *sb, unsigned int wait_ms,
547 unsigned int prefree, unsigned int free), 547 unsigned int prefree, unsigned int free),
548 548
549 TP_ARGS(sb, wait_ms, prefree, free), 549 TP_ARGS(sb, wait_ms, prefree, free),
550 550
551 TP_STRUCT__entry( 551 TP_STRUCT__entry(
552 __field(dev_t, dev) 552 __field(dev_t, dev)
553 __field(long, wait_ms) 553 __field(unsigned int, wait_ms)
554 __field(unsigned int, prefree) 554 __field(unsigned int, prefree)
555 __field(unsigned int, free) 555 __field(unsigned int, free)
556 ), 556 ),
@@ -562,13 +562,120 @@ TRACE_EVENT(f2fs_background_gc,
562 __entry->free = free; 562 __entry->free = free;
563 ), 563 ),
564 564
565 TP_printk("dev = (%d,%d), wait_ms = %ld, prefree = %u, free = %u", 565 TP_printk("dev = (%d,%d), wait_ms = %u, prefree = %u, free = %u",
566 show_dev(__entry->dev), 566 show_dev(__entry->dev),
567 __entry->wait_ms, 567 __entry->wait_ms,
568 __entry->prefree, 568 __entry->prefree,
569 __entry->free) 569 __entry->free)
570); 570);
571 571
572TRACE_EVENT(f2fs_gc_begin,
573
574 TP_PROTO(struct super_block *sb, bool sync, bool background,
575 long long dirty_nodes, long long dirty_dents,
576 long long dirty_imeta, unsigned int free_sec,
577 unsigned int free_seg, int reserved_seg,
578 unsigned int prefree_seg),
579
580 TP_ARGS(sb, sync, background, dirty_nodes, dirty_dents, dirty_imeta,
581 free_sec, free_seg, reserved_seg, prefree_seg),
582
583 TP_STRUCT__entry(
584 __field(dev_t, dev)
585 __field(bool, sync)
586 __field(bool, background)
587 __field(long long, dirty_nodes)
588 __field(long long, dirty_dents)
589 __field(long long, dirty_imeta)
590 __field(unsigned int, free_sec)
591 __field(unsigned int, free_seg)
592 __field(int, reserved_seg)
593 __field(unsigned int, prefree_seg)
594 ),
595
596 TP_fast_assign(
597 __entry->dev = sb->s_dev;
598 __entry->sync = sync;
599 __entry->background = background;
600 __entry->dirty_nodes = dirty_nodes;
601 __entry->dirty_dents = dirty_dents;
602 __entry->dirty_imeta = dirty_imeta;
603 __entry->free_sec = free_sec;
604 __entry->free_seg = free_seg;
605 __entry->reserved_seg = reserved_seg;
606 __entry->prefree_seg = prefree_seg;
607 ),
608
609 TP_printk("dev = (%d,%d), sync = %d, background = %d, nodes = %lld, "
610 "dents = %lld, imeta = %lld, free_sec:%u, free_seg:%u, "
611 "rsv_seg:%d, prefree_seg:%u",
612 show_dev(__entry->dev),
613 __entry->sync,
614 __entry->background,
615 __entry->dirty_nodes,
616 __entry->dirty_dents,
617 __entry->dirty_imeta,
618 __entry->free_sec,
619 __entry->free_seg,
620 __entry->reserved_seg,
621 __entry->prefree_seg)
622);
623
624TRACE_EVENT(f2fs_gc_end,
625
626 TP_PROTO(struct super_block *sb, int ret, int seg_freed,
627 int sec_freed, long long dirty_nodes,
628 long long dirty_dents, long long dirty_imeta,
629 unsigned int free_sec, unsigned int free_seg,
630 int reserved_seg, unsigned int prefree_seg),
631
632 TP_ARGS(sb, ret, seg_freed, sec_freed, dirty_nodes, dirty_dents,
633 dirty_imeta, free_sec, free_seg, reserved_seg, prefree_seg),
634
635 TP_STRUCT__entry(
636 __field(dev_t, dev)
637 __field(int, ret)
638 __field(int, seg_freed)
639 __field(int, sec_freed)
640 __field(long long, dirty_nodes)
641 __field(long long, dirty_dents)
642 __field(long long, dirty_imeta)
643 __field(unsigned int, free_sec)
644 __field(unsigned int, free_seg)
645 __field(int, reserved_seg)
646 __field(unsigned int, prefree_seg)
647 ),
648
649 TP_fast_assign(
650 __entry->dev = sb->s_dev;
651 __entry->ret = ret;
652 __entry->seg_freed = seg_freed;
653 __entry->sec_freed = sec_freed;
654 __entry->dirty_nodes = dirty_nodes;
655 __entry->dirty_dents = dirty_dents;
656 __entry->dirty_imeta = dirty_imeta;
657 __entry->free_sec = free_sec;
658 __entry->free_seg = free_seg;
659 __entry->reserved_seg = reserved_seg;
660 __entry->prefree_seg = prefree_seg;
661 ),
662
663 TP_printk("dev = (%d,%d), ret = %d, seg_freed = %d, sec_freed = %d, "
664 "nodes = %lld, dents = %lld, imeta = %lld, free_sec:%u, "
665 "free_seg:%u, rsv_seg:%d, prefree_seg:%u",
666 show_dev(__entry->dev),
667 __entry->ret,
668 __entry->seg_freed,
669 __entry->sec_freed,
670 __entry->dirty_nodes,
671 __entry->dirty_dents,
672 __entry->dirty_imeta,
673 __entry->free_sec,
674 __entry->free_seg,
675 __entry->reserved_seg,
676 __entry->prefree_seg)
677);
678
572TRACE_EVENT(f2fs_get_victim, 679TRACE_EVENT(f2fs_get_victim,
573 680
574 TP_PROTO(struct super_block *sb, int type, int gc_type, 681 TP_PROTO(struct super_block *sb, int type, int gc_type,