@@ -766,6 +766,22 @@ static void __iomap_put_folio(struct iomap_iter *iter, size_t ret,
766
766
}
767
767
}
768
768
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
+
769
785
static int iomap_write_begin_inline (const struct iomap_iter * iter ,
770
786
struct folio * folio )
771
787
{
@@ -920,7 +936,7 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
920
936
struct folio * folio ;
921
937
loff_t old_size ;
922
938
size_t offset ; /* Offset into folio */
923
- size_t bytes ; /* Bytes to write to folio */
939
+ u64 bytes ; /* Bytes to write to folio */
924
940
size_t copied ; /* Bytes copied from user */
925
941
u64 written ; /* Bytes have been written */
926
942
loff_t pos ;
@@ -959,11 +975,8 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
959
975
}
960
976
if (iter -> iomap .flags & IOMAP_F_STALE )
961
977
break ;
962
- pos = iter -> pos ;
963
978
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 );
967
980
968
981
if (mapping_writably_mapped (mapping ))
969
982
flush_dcache_folio (folio );
@@ -1280,7 +1293,6 @@ static int iomap_unshare_iter(struct iomap_iter *iter)
1280
1293
do {
1281
1294
struct folio * folio ;
1282
1295
size_t offset ;
1283
- loff_t pos ;
1284
1296
bool ret ;
1285
1297
1286
1298
bytes = min_t (u64 , SIZE_MAX , bytes );
@@ -1289,11 +1301,8 @@ static int iomap_unshare_iter(struct iomap_iter *iter)
1289
1301
return status ;
1290
1302
if (iomap -> flags & IOMAP_F_STALE )
1291
1303
break ;
1292
- pos = iter -> pos ;
1293
1304
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 );
1297
1306
1298
1307
ret = iomap_write_end (iter , bytes , bytes , folio );
1299
1308
__iomap_put_folio (iter , bytes , folio );
@@ -1356,7 +1365,6 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
1356
1365
do {
1357
1366
struct folio * folio ;
1358
1367
size_t offset ;
1359
- loff_t pos ;
1360
1368
bool ret ;
1361
1369
1362
1370
bytes = min_t (u64 , SIZE_MAX , bytes );
@@ -1365,14 +1373,11 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
1365
1373
return status ;
1366
1374
if (iter -> iomap .flags & IOMAP_F_STALE )
1367
1375
break ;
1368
- pos = iter -> pos ;
1369
1376
1370
1377
/* warn about zeroing folios beyond eof that won't write back */
1371
1378
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 ;
1375
1379
1380
+ iomap_trim_folio_range (iter , folio , & offset , & bytes );
1376
1381
folio_zero_range (folio , offset , bytes );
1377
1382
folio_mark_accessed (folio );
1378
1383
0 commit comments