linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] f2fs: get rid of buffer_head use
@ 2024-06-07 10:18 Chao Yu
  2024-06-07 13:33 ` Matthew Wilcox
  0 siblings, 1 reply; 4+ messages in thread
From: Chao Yu @ 2024-06-07 10:18 UTC (permalink / raw)
  To: jaegeuk; +Cc: linux-f2fs-devel, linux-kernel, Chao Yu, Matthew Wilcox

Convert to use folio and related functionality.

Cc: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Chao Yu <chao@kernel.org>
---
v2:
- convert to use folio rather than page suggested by Matthew.
 fs/f2fs/data.c  |  1 -
 fs/f2fs/f2fs.h  |  7 +++-
 fs/f2fs/file.c  |  1 -
 fs/f2fs/inode.c |  1 -
 fs/f2fs/super.c | 94 +++++++++++++++++++++++++++++--------------------
 5 files changed, 62 insertions(+), 42 deletions(-)

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index b9b0debc6b3d..ad495ea87b32 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -7,7 +7,6 @@
  */
 #include <linux/fs.h>
 #include <linux/f2fs_fs.h>
-#include <linux/buffer_head.h>
 #include <linux/sched/mm.h>
 #include <linux/mpage.h>
 #include <linux/writeback.h>
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 9688df332147..c82d2050890d 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -11,7 +11,6 @@
 #include <linux/uio.h>
 #include <linux/types.h>
 #include <linux/page-flags.h>
-#include <linux/buffer_head.h>
 #include <linux/slab.h>
 #include <linux/crc32.h>
 #include <linux/magic.h>
@@ -1990,6 +1989,12 @@ static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
 	return (struct f2fs_super_block *)(sbi->raw_super);
 }
 
+static inline struct f2fs_super_block *F2FS_SUPER_BLOCK(struct folio *folio)
+{
+	return (struct f2fs_super_block *)(page_address(folio_page(folio, 0)) +
+							F2FS_SUPER_OFFSET);
+}
+
 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
 {
 	return (struct f2fs_checkpoint *)(sbi->ckpt);
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index c50213da474d..efc676bc7800 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -8,7 +8,6 @@
 #include <linux/fs.h>
 #include <linux/f2fs_fs.h>
 #include <linux/stat.h>
-#include <linux/buffer_head.h>
 #include <linux/writeback.h>
 #include <linux/blkdev.h>
 #include <linux/falloc.h>
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index dbfebbddf675..87982e06bbe7 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -7,7 +7,6 @@
  */
 #include <linux/fs.h>
 #include <linux/f2fs_fs.h>
-#include <linux/buffer_head.h>
 #include <linux/writeback.h>
 #include <linux/sched/mm.h>
 #include <linux/lz4.h>
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 4a1bc8f40f9a..f14eba4cbbf7 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -11,7 +11,6 @@
 #include <linux/fs_context.h>
 #include <linux/sched/mm.h>
 #include <linux/statfs.h>
-#include <linux/buffer_head.h>
 #include <linux/kthread.h>
 #include <linux/parser.h>
 #include <linux/mount.h>
@@ -3333,24 +3332,42 @@ loff_t max_file_blocks(struct inode *inode)
 	return result;
 }
 
-static int __f2fs_commit_super(struct buffer_head *bh,
-			struct f2fs_super_block *super)
+static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio,
+								bool update)
 {
-	lock_buffer(bh);
-	if (super)
-		memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
-	set_buffer_dirty(bh);
-	unlock_buffer(bh);
-
+	struct bio *bio;
 	/* it's rare case, we can do fua all the time */
-	return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
+	blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA;
+	int ret;
+
+	folio_lock(folio);
+	folio_wait_writeback(folio);
+	if (update)
+		memcpy(F2FS_SUPER_BLOCK(folio), F2FS_RAW_SUPER(sbi),
+					sizeof(struct f2fs_super_block));
+	folio_mark_dirty(folio);
+	folio_clear_dirty_for_io(folio);
+	folio_start_writeback(folio);
+	folio_unlock(folio);
+
+	bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS);
+
+	/* it doesn't need to set crypto context for superblock update */
+	bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(folio_index(folio));
+
+	if (!bio_add_folio(bio, folio, PAGE_SIZE, 0))
+		f2fs_bug_on(sbi, 1);
+
+	ret = submit_bio_wait(bio);
+	folio_end_writeback(folio);
+
+	return ret;
 }
 
 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
-					struct buffer_head *bh)
+							struct folio *folio)
 {
-	struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
-					(bh->b_data + F2FS_SUPER_OFFSET);
+	struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio);
 	struct super_block *sb = sbi->sb;
 	u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
 	u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
@@ -3425,7 +3442,7 @@ static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
 			set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
 			res = "internally";
 		} else {
-			err = __f2fs_commit_super(bh, NULL);
+			err = __f2fs_commit_super(sbi, folio, false);
 			res = err ? "failed" : "done";
 		}
 		f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
@@ -3438,12 +3455,11 @@ static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
 }
 
 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
-				struct buffer_head *bh)
+						struct folio *folio)
 {
 	block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
 	block_t total_sections, blocks_per_seg;
-	struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
-					(bh->b_data + F2FS_SUPER_OFFSET);
+	struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio);
 	size_t crc_offset = 0;
 	__u32 crc = 0;
 
@@ -3601,7 +3617,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
 	}
 
 	/* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
-	if (sanity_check_area_boundary(sbi, bh))
+	if (sanity_check_area_boundary(sbi, folio))
 		return -EFSCORRUPTED;
 
 	return 0;
@@ -3948,7 +3964,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 {
 	struct super_block *sb = sbi->sb;
 	int block;
-	struct buffer_head *bh;
+	struct folio *folio;
 	struct f2fs_super_block *super;
 	int err = 0;
 
@@ -3957,32 +3973,32 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 		return -ENOMEM;
 
 	for (block = 0; block < 2; block++) {
-		bh = sb_bread(sb, block);
-		if (!bh) {
+		folio = read_mapping_folio(sb->s_bdev->bd_inode->i_mapping,
+								block, NULL);
+		if (IS_ERR(folio)) {
 			f2fs_err(sbi, "Unable to read %dth superblock",
 				 block + 1);
-			err = -EIO;
+			err = PTR_ERR(folio);
 			*recovery = 1;
 			continue;
 		}
 
 		/* sanity checking of raw super */
-		err = sanity_check_raw_super(sbi, bh);
+		err = sanity_check_raw_super(sbi, folio);
 		if (err) {
 			f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
 				 block + 1);
-			brelse(bh);
+			folio_put(folio);
 			*recovery = 1;
 			continue;
 		}
 
 		if (!*raw_super) {
-			memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
-							sizeof(*super));
+			memcpy(super, F2FS_SUPER_BLOCK(folio), sizeof(*super));
 			*valid_super_block = block;
 			*raw_super = super;
 		}
-		brelse(bh);
+		folio_put(folio);
 	}
 
 	/* No valid superblock */
@@ -3996,7 +4012,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
-	struct buffer_head *bh;
+	struct folio *folio;
 	__u32 crc = 0;
 	int err;
 
@@ -4014,22 +4030,24 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 	}
 
 	/* write back-up superblock first */
-	bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
-	if (!bh)
-		return -EIO;
-	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
-	brelse(bh);
+	folio = read_mapping_folio(sbi->sb->s_bdev->bd_inode->i_mapping,
+				sbi->valid_super_block ? 0 : 1, NULL);
+	if (IS_ERR(folio))
+		return PTR_ERR(folio);
+	err = __f2fs_commit_super(sbi, folio, true);
+	folio_put(folio);
 
 	/* if we are in recovery path, skip writing valid superblock */
 	if (recover || err)
 		return err;
 
 	/* write current valid superblock */
-	bh = sb_bread(sbi->sb, sbi->valid_super_block);
-	if (!bh)
-		return -EIO;
-	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
-	brelse(bh);
+	folio = read_mapping_folio(sbi->sb->s_bdev->bd_inode->i_mapping,
+					sbi->valid_super_block, NULL);
+	if (IS_ERR(folio))
+		return PTR_ERR(folio);
+	err = __f2fs_commit_super(sbi, folio, true);
+	folio_put(folio);
 	return err;
 }
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] f2fs: get rid of buffer_head use
  2024-06-07 10:18 [PATCH v2] f2fs: get rid of buffer_head use Chao Yu
@ 2024-06-07 13:33 ` Matthew Wilcox
  2024-06-07 14:10   ` Chao Yu
  0 siblings, 1 reply; 4+ messages in thread
From: Matthew Wilcox @ 2024-06-07 13:33 UTC (permalink / raw)
  To: Chao Yu; +Cc: jaegeuk, linux-f2fs-devel, linux-kernel

On Fri, Jun 07, 2024 at 06:18:29PM +0800, Chao Yu wrote:
> @@ -1990,6 +1989,12 @@ static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
>  	return (struct f2fs_super_block *)(sbi->raw_super);
>  }
>  
> +static inline struct f2fs_super_block *F2FS_SUPER_BLOCK(struct folio *folio)
> +{
> +	return (struct f2fs_super_block *)(page_address(folio_page(folio, 0)) +
> +							F2FS_SUPER_OFFSET);
> +}

This assumes that the superblock is in the first page of the folio.
That's not necessarily guaranteed; let's say you have a 64KiB folio
that covers the start of the bdev.

I don't quite know how to write this because f2fs defines its block size
in terms of PAGE_SIZE, which just seems like nonsense to me.  If you
format a filesystem on a 16KiB PAGE_SIZE machine and then try to mount
it on a machine with a 4KiB PAGE_SIZE, it's going to go horribly wrong.

You'd need to pass in something that indicates whether you're trying to
access the first or second superblock; there's no way to tell from the
folio which one it is.

> +static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio,
> +								bool update)
>  {
> -	lock_buffer(bh);
> -	if (super)
> -		memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
> -	set_buffer_dirty(bh);
> -	unlock_buffer(bh);
> -
> +	struct bio *bio;
>  	/* it's rare case, we can do fua all the time */
> -	return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
> +	blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA;
> +	int ret;
> +
> +	folio_lock(folio);
> +	folio_wait_writeback(folio);
> +	if (update)
> +		memcpy(F2FS_SUPER_BLOCK(folio), F2FS_RAW_SUPER(sbi),
> +					sizeof(struct f2fs_super_block));
> +	folio_mark_dirty(folio);
> +	folio_clear_dirty_for_io(folio);
> +	folio_start_writeback(folio);
> +	folio_unlock(folio);
> +
> +	bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS);
> +
> +	/* it doesn't need to set crypto context for superblock update */
> +	bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(folio_index(folio));
> +
> +	if (!bio_add_folio(bio, folio, PAGE_SIZE, 0))
> +		f2fs_bug_on(sbi, 1);

Better make that folio_size(folio) to support bs>PS.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] f2fs: get rid of buffer_head use
  2024-06-07 13:33 ` Matthew Wilcox
@ 2024-06-07 14:10   ` Chao Yu
  2024-06-07 16:02     ` Matthew Wilcox
  0 siblings, 1 reply; 4+ messages in thread
From: Chao Yu @ 2024-06-07 14:10 UTC (permalink / raw)
  To: Matthew Wilcox; +Cc: jaegeuk, linux-f2fs-devel, linux-kernel

On 2024/6/7 21:33, Matthew Wilcox wrote:
> On Fri, Jun 07, 2024 at 06:18:29PM +0800, Chao Yu wrote:
>> @@ -1990,6 +1989,12 @@ static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
>>   	return (struct f2fs_super_block *)(sbi->raw_super);
>>   }
>>   
>> +static inline struct f2fs_super_block *F2FS_SUPER_BLOCK(struct folio *folio)
>> +{
>> +	return (struct f2fs_super_block *)(page_address(folio_page(folio, 0)) +
>> +							F2FS_SUPER_OFFSET);
>> +}
> 
> This assumes that the superblock is in the first page of the folio.
> That's not necessarily guaranteed; let's say you have a 64KiB folio
> that covers the start of the bdev.

Oh, I missed to add large folio support in this version.

For the case: page size is 4KiB, and folio size is 64KiB,
read_mapping_folio(mapping, 0, NULL) and read_mapping_folio(mapping,
1, NULL) will return the same #0 folio, right?

> 
> I don't quite know how to write this because f2fs defines its block size
> in terms of PAGE_SIZE, which just seems like nonsense to me.  If you
> format a filesystem on a 16KiB PAGE_SIZE machine and then try to mount
> it on a machine with a 4KiB PAGE_SIZE, it's going to go horribly wrong.

f2fs will check on-disk block size w/ PAGE_SIZE in sanity_check_raw_super()
as below:

	/* only support block_size equals to PAGE_SIZE */
	if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
		f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
			  le32_to_cpu(raw_super->log_blocksize),
			  F2FS_BLKSIZE_BITS);
		return -EFSCORRUPTED;
	}

> 
> You'd need to pass in something that indicates whether you're trying to
> access the first or second superblock; there's no way to tell from the
> folio which one it is.
> 
>> +static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio,
>> +								bool update)
>>   {
>> -	lock_buffer(bh);
>> -	if (super)
>> -		memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
>> -	set_buffer_dirty(bh);
>> -	unlock_buffer(bh);
>> -
>> +	struct bio *bio;
>>   	/* it's rare case, we can do fua all the time */
>> -	return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
>> +	blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA;
>> +	int ret;
>> +
>> +	folio_lock(folio);
>> +	folio_wait_writeback(folio);
>> +	if (update)
>> +		memcpy(F2FS_SUPER_BLOCK(folio), F2FS_RAW_SUPER(sbi),
>> +					sizeof(struct f2fs_super_block));
>> +	folio_mark_dirty(folio);
>> +	folio_clear_dirty_for_io(folio);
>> +	folio_start_writeback(folio);
>> +	folio_unlock(folio);
>> +
>> +	bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS);
>> +
>> +	/* it doesn't need to set crypto context for superblock update */
>> +	bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(folio_index(folio));
>> +
>> +	if (!bio_add_folio(bio, folio, PAGE_SIZE, 0))
>> +		f2fs_bug_on(sbi, 1);
> 
> Better make that folio_size(folio) to support bs>PS.

Looks better, will update.

Thanks,

> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] f2fs: get rid of buffer_head use
  2024-06-07 14:10   ` Chao Yu
@ 2024-06-07 16:02     ` Matthew Wilcox
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Wilcox @ 2024-06-07 16:02 UTC (permalink / raw)
  To: Chao Yu; +Cc: jaegeuk, linux-f2fs-devel, linux-kernel

On Fri, Jun 07, 2024 at 10:10:52PM +0800, Chao Yu wrote:
> On 2024/6/7 21:33, Matthew Wilcox wrote:
> > On Fri, Jun 07, 2024 at 06:18:29PM +0800, Chao Yu wrote:
> > > @@ -1990,6 +1989,12 @@ static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
> > >   	return (struct f2fs_super_block *)(sbi->raw_super);
> > >   }
> > > +static inline struct f2fs_super_block *F2FS_SUPER_BLOCK(struct folio *folio)
> > > +{
> > > +	return (struct f2fs_super_block *)(page_address(folio_page(folio, 0)) +
> > > +							F2FS_SUPER_OFFSET);
> > > +}
> > 
> > This assumes that the superblock is in the first page of the folio.
> > That's not necessarily guaranteed; let's say you have a 64KiB folio
> > that covers the start of the bdev.
> 
> Oh, I missed to add large folio support in this version.
> 
> For the case: page size is 4KiB, and folio size is 64KiB,
> read_mapping_folio(mapping, 0, NULL) and read_mapping_folio(mapping,
> 1, NULL) will return the same #0 folio, right?

That's right.  If you want to pass a page into F2FS_SUPER_BLOCK, that
would be fine.  Assuming you're not trying to support fs blocksize !=
PAGE_SIZE.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-06-07 16:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-07 10:18 [PATCH v2] f2fs: get rid of buffer_head use Chao Yu
2024-06-07 13:33 ` Matthew Wilcox
2024-06-07 14:10   ` Chao Yu
2024-06-07 16:02     ` Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).