Skip to content

Commit 66c0d85

Browse files
Brian Fosterbrauner
authored andcommitted
iomap: rework iomap_write_begin() to return folio offset and length
iomap_write_begin() returns a folio based on current pos and remaining length in the iter, and each caller then trims the pos/length to the given folio. Clean this up a bit and let iomap_write_begin() return the trimmed range along with the folio. Signed-off-by: Brian Foster <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent c0f8658 commit 66c0d85

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

fs/iomap/buffered-io.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,12 @@ static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
679679
return submit_bio_wait(&bio);
680680
}
681681

682-
static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
683-
size_t len, struct folio *folio)
682+
static int __iomap_write_begin(const struct iomap_iter *iter, size_t len,
683+
struct folio *folio)
684684
{
685685
const struct iomap *srcmap = iomap_iter_srcmap(iter);
686686
struct iomap_folio_state *ifs;
687+
loff_t pos = iter->pos;
687688
loff_t block_size = i_blocksize(iter->inode);
688689
loff_t block_start = round_down(pos, block_size);
689690
loff_t block_end = round_up(pos + len, block_size);
@@ -794,15 +795,22 @@ static int iomap_write_begin_inline(const struct iomap_iter *iter,
794795
return iomap_read_inline_data(iter, folio);
795796
}
796797

797-
static int iomap_write_begin(struct iomap_iter *iter, size_t len,
798-
struct folio **foliop)
798+
/*
799+
* Grab and prepare a folio for write based on iter state. Returns the folio,
800+
* offset, and length. Callers can optionally pass a max length *plen,
801+
* otherwise init to zero.
802+
*/
803+
static int iomap_write_begin(struct iomap_iter *iter, struct folio **foliop,
804+
size_t *poffset, u64 *plen)
799805
{
800806
const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
801807
const struct iomap *srcmap = iomap_iter_srcmap(iter);
802808
loff_t pos = iter->pos;
809+
u64 len = min_t(u64, SIZE_MAX, iomap_length(iter));
803810
struct folio *folio;
804811
int status = 0;
805812

813+
len = min_not_zero(len, *plen);
806814
BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length);
807815
if (srcmap != &iter->iomap)
808816
BUG_ON(pos + len > srcmap->offset + srcmap->length);
@@ -834,20 +842,20 @@ static int iomap_write_begin(struct iomap_iter *iter, size_t len,
834842
}
835843
}
836844

837-
if (pos + len > folio_pos(folio) + folio_size(folio))
838-
len = folio_pos(folio) + folio_size(folio) - pos;
845+
pos = iomap_trim_folio_range(iter, folio, poffset, &len);
839846

840847
if (srcmap->type == IOMAP_INLINE)
841848
status = iomap_write_begin_inline(iter, folio);
842849
else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
843850
status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
844851
else
845-
status = __iomap_write_begin(iter, pos, len, folio);
852+
status = __iomap_write_begin(iter, len, folio);
846853

847854
if (unlikely(status))
848855
goto out_unlock;
849856

850857
*foliop = folio;
858+
*plen = len;
851859
return 0;
852860

853861
out_unlock:
@@ -968,15 +976,15 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
968976
break;
969977
}
970978

971-
status = iomap_write_begin(iter, bytes, &folio);
979+
status = iomap_write_begin(iter, &folio, &offset, &bytes);
972980
if (unlikely(status)) {
973981
iomap_write_failed(iter->inode, iter->pos, bytes);
974982
break;
975983
}
976984
if (iter->iomap.flags & IOMAP_F_STALE)
977985
break;
978986

979-
pos = iomap_trim_folio_range(iter, folio, &offset, &bytes);
987+
pos = iter->pos;
980988

981989
if (mapping_writably_mapped(mapping))
982990
flush_dcache_folio(folio);
@@ -1296,14 +1304,12 @@ static int iomap_unshare_iter(struct iomap_iter *iter)
12961304
bool ret;
12971305

12981306
bytes = min_t(u64, SIZE_MAX, bytes);
1299-
status = iomap_write_begin(iter, bytes, &folio);
1307+
status = iomap_write_begin(iter, &folio, &offset, &bytes);
13001308
if (unlikely(status))
13011309
return status;
13021310
if (iomap->flags & IOMAP_F_STALE)
13031311
break;
13041312

1305-
iomap_trim_folio_range(iter, folio, &offset, &bytes);
1306-
13071313
ret = iomap_write_end(iter, bytes, bytes, folio);
13081314
__iomap_put_folio(iter, bytes, folio);
13091315
if (WARN_ON_ONCE(!ret))
@@ -1368,7 +1374,7 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
13681374
bool ret;
13691375

13701376
bytes = min_t(u64, SIZE_MAX, bytes);
1371-
status = iomap_write_begin(iter, bytes, &folio);
1377+
status = iomap_write_begin(iter, &folio, &offset, &bytes);
13721378
if (status)
13731379
return status;
13741380
if (iter->iomap.flags & IOMAP_F_STALE)
@@ -1377,7 +1383,6 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
13771383
/* warn about zeroing folios beyond eof that won't write back */
13781384
WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);
13791385

1380-
iomap_trim_folio_range(iter, folio, &offset, &bytes);
13811386
folio_zero_range(folio, offset, bytes);
13821387
folio_mark_accessed(folio);
13831388

0 commit comments

Comments
 (0)