Skip to content

Commit c4f9a1b

Browse files
Brian Fosterbrauner
authored andcommitted
iomap: helper to trim pos/bytes to within folio
Several buffered write based iteration callbacks duplicate logic to trim the current pos and length to within the current folio. Factor this into a helper to make it easier to relocate closer to folio lookup. 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 3ceb65b commit c4f9a1b

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

fs/iomap/buffered-io.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,22 @@ static void __iomap_put_folio(struct iomap_iter *iter, size_t ret,
766766
}
767767
}
768768

769+
/* trim pos and bytes to within a given folio */
770+
static loff_t iomap_trim_folio_range(struct iomap_iter *iter,
771+
struct folio *folio, size_t *offset, u64 *bytes)
772+
{
773+
loff_t pos = iter->pos;
774+
size_t fsize = folio_size(folio);
775+
776+
WARN_ON_ONCE(pos < folio_pos(folio));
777+
WARN_ON_ONCE(pos >= folio_pos(folio) + fsize);
778+
779+
*offset = offset_in_folio(folio, pos);
780+
*bytes = min(*bytes, fsize - *offset);
781+
782+
return pos;
783+
}
784+
769785
static int iomap_write_begin_inline(const struct iomap_iter *iter,
770786
struct folio *folio)
771787
{
@@ -920,7 +936,7 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
920936
struct folio *folio;
921937
loff_t old_size;
922938
size_t offset; /* Offset into folio */
923-
size_t bytes; /* Bytes to write to folio */
939+
u64 bytes; /* Bytes to write to folio */
924940
size_t copied; /* Bytes copied from user */
925941
u64 written; /* Bytes have been written */
926942
loff_t pos;
@@ -959,11 +975,8 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
959975
}
960976
if (iter->iomap.flags & IOMAP_F_STALE)
961977
break;
962-
pos = iter->pos;
963978

964-
offset = offset_in_folio(folio, pos);
965-
if (bytes > folio_size(folio) - offset)
966-
bytes = folio_size(folio) - offset;
979+
pos = iomap_trim_folio_range(iter, folio, &offset, &bytes);
967980

968981
if (mapping_writably_mapped(mapping))
969982
flush_dcache_folio(folio);
@@ -1280,7 +1293,6 @@ static int iomap_unshare_iter(struct iomap_iter *iter)
12801293
do {
12811294
struct folio *folio;
12821295
size_t offset;
1283-
loff_t pos;
12841296
bool ret;
12851297

12861298
bytes = min_t(u64, SIZE_MAX, bytes);
@@ -1289,11 +1301,8 @@ static int iomap_unshare_iter(struct iomap_iter *iter)
12891301
return status;
12901302
if (iomap->flags & IOMAP_F_STALE)
12911303
break;
1292-
pos = iter->pos;
12931304

1294-
offset = offset_in_folio(folio, pos);
1295-
if (bytes > folio_size(folio) - offset)
1296-
bytes = folio_size(folio) - offset;
1305+
iomap_trim_folio_range(iter, folio, &offset, &bytes);
12971306

12981307
ret = iomap_write_end(iter, bytes, bytes, folio);
12991308
__iomap_put_folio(iter, bytes, folio);
@@ -1356,7 +1365,6 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
13561365
do {
13571366
struct folio *folio;
13581367
size_t offset;
1359-
loff_t pos;
13601368
bool ret;
13611369

13621370
bytes = min_t(u64, SIZE_MAX, bytes);
@@ -1365,14 +1373,11 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
13651373
return status;
13661374
if (iter->iomap.flags & IOMAP_F_STALE)
13671375
break;
1368-
pos = iter->pos;
13691376

13701377
/* warn about zeroing folios beyond eof that won't write back */
13711378
WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);
1372-
offset = offset_in_folio(folio, pos);
1373-
if (bytes > folio_size(folio) - offset)
1374-
bytes = folio_size(folio) - offset;
13751379

1380+
iomap_trim_folio_range(iter, folio, &offset, &bytes);
13761381
folio_zero_range(folio, offset, bytes);
13771382
folio_mark_accessed(folio);
13781383

0 commit comments

Comments
 (0)