summaryrefslogtreecommitdiffstats
path: root/fs/bcachefs/fs-io.c
diff options
context:
space:
mode:
authorBrian Foster <bfoster@redhat.com>2023-04-03 14:17:26 +0200
committerKent Overstreet <kent.overstreet@linux.dev>2023-10-22 23:10:00 +0200
commitbf98ee10d45af8e97c0802e39cc77ee607072633 (patch)
treeff208e015921eecbaced1c6201a47cbfb7d2f901 /fs/bcachefs/fs-io.c
parentbcachefs: Fix a null ptr deref in fsck check_extents() (diff)
downloadlinux-bf98ee10d45af8e97c0802e39cc77ee607072633.tar.xz
linux-bf98ee10d45af8e97c0802e39cc77ee607072633.zip
bcachefs: folio pos to bch_folio_sector index helper
Create a small helper to translate from file offset to the associated bch_folio_sector index in the underlying bch_folio. The helper assumes the file offset is covered by the passed folio. Signed-off-by: Brian Foster <bfoster@redhat.com> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'fs/bcachefs/fs-io.c')
-rw-r--r--fs/bcachefs/fs-io.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/fs/bcachefs/fs-io.c b/fs/bcachefs/fs-io.c
index 7823141ea98b..ea5039254609 100644
--- a/fs/bcachefs/fs-io.c
+++ b/fs/bcachefs/fs-io.c
@@ -528,6 +528,14 @@ static inline void folio_sector_set(struct folio *folio,
s->s[i].state = n;
}
+/* file offset (to folio offset) to bch_folio_sector index */
+static inline int folio_pos_to_s(struct folio *folio, loff_t pos)
+{
+ u64 f_offset = pos - folio_pos(folio);
+ BUG_ON(pos < folio_pos(folio) || pos >= folio_end_pos(folio));
+ return f_offset >> SECTOR_SHIFT;
+}
+
static inline struct bch_folio *__bch2_folio(struct folio *folio)
{
return folio_has_private(folio)
@@ -2902,7 +2910,7 @@ static int __bch2_truncate_folio(struct bch_inode_info *inode,
end_pos = folio_end_pos(folio);
if (inode->v.i_size > folio_pos(folio))
end_pos = min_t(u64, inode->v.i_size, end_pos);
- ret = s->s[(end_pos - folio_pos(folio) - 1) >> 9].state >= SECTOR_dirty;
+ ret = s->s[folio_pos_to_s(folio, end_pos - 1)].state >= SECTOR_dirty;
folio_zero_segment(folio, start_offset, end_offset);
@@ -3653,15 +3661,15 @@ err:
/* fseek: */
-static int folio_data_offset(struct folio *folio, unsigned offset)
+static int folio_data_offset(struct folio *folio, loff_t pos)
{
struct bch_folio *s = bch2_folio(folio);
unsigned i, sectors = folio_sectors(folio);
if (s)
- for (i = offset >> 9; i < sectors; i++)
+ for (i = folio_pos_to_s(folio, pos); i < sectors; i++)
if (s->s[i].state >= SECTOR_dirty)
- return i << 9;
+ return i << SECTOR_SHIFT;
return -1;
}
@@ -3687,8 +3695,7 @@ static loff_t bch2_seek_pagecache_data(struct inode *vinode,
folio_lock(folio);
offset = folio_data_offset(folio,
- max(folio_pos(folio), start_offset) -
- folio_pos(folio));
+ max(folio_pos(folio), start_offset));
if (offset >= 0) {
ret = clamp(folio_pos(folio) + offset,
start_offset, end_offset);
@@ -3762,7 +3769,7 @@ static bool folio_hole_offset(struct address_space *mapping, loff_t *offset)
{
struct folio *folio;
struct bch_folio *s;
- unsigned i, sectors, f_offset;
+ unsigned i, sectors;
bool ret = true;
folio = filemap_lock_folio(mapping, *offset >> PAGE_SHIFT);
@@ -3774,11 +3781,10 @@ static bool folio_hole_offset(struct address_space *mapping, loff_t *offset)
goto unlock;
sectors = folio_sectors(folio);
- f_offset = *offset - folio_pos(folio);
-
- for (i = f_offset >> 9; i < sectors; i++)
+ for (i = folio_pos_to_s(folio, *offset); i < sectors; i++)
if (s->s[i].state < SECTOR_dirty) {
- *offset = max(*offset, folio_pos(folio) + (i << 9));
+ *offset = max(*offset,
+ folio_pos(folio) + (i << SECTOR_SHIFT));
goto unlock;
}