@@ -679,11 +679,12 @@ static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
679
679
return submit_bio_wait (& bio );
680
680
}
681
681
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 )
684
684
{
685
685
const struct iomap * srcmap = iomap_iter_srcmap (iter );
686
686
struct iomap_folio_state * ifs ;
687
+ loff_t pos = iter -> pos ;
687
688
loff_t block_size = i_blocksize (iter -> inode );
688
689
loff_t block_start = round_down (pos , block_size );
689
690
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,
794
795
return iomap_read_inline_data (iter , folio );
795
796
}
796
797
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 )
799
805
{
800
806
const struct iomap_folio_ops * folio_ops = iter -> iomap .folio_ops ;
801
807
const struct iomap * srcmap = iomap_iter_srcmap (iter );
802
808
loff_t pos = iter -> pos ;
809
+ u64 len = min_t (u64 , SIZE_MAX , iomap_length (iter ));
803
810
struct folio * folio ;
804
811
int status = 0 ;
805
812
813
+ len = min_not_zero (len , * plen );
806
814
BUG_ON (pos + len > iter -> iomap .offset + iter -> iomap .length );
807
815
if (srcmap != & iter -> iomap )
808
816
BUG_ON (pos + len > srcmap -> offset + srcmap -> length );
@@ -834,20 +842,20 @@ static int iomap_write_begin(struct iomap_iter *iter, size_t len,
834
842
}
835
843
}
836
844
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 );
839
846
840
847
if (srcmap -> type == IOMAP_INLINE )
841
848
status = iomap_write_begin_inline (iter , folio );
842
849
else if (srcmap -> flags & IOMAP_F_BUFFER_HEAD )
843
850
status = __block_write_begin_int (folio , pos , len , NULL , srcmap );
844
851
else
845
- status = __iomap_write_begin (iter , pos , len , folio );
852
+ status = __iomap_write_begin (iter , len , folio );
846
853
847
854
if (unlikely (status ))
848
855
goto out_unlock ;
849
856
850
857
* foliop = folio ;
858
+ * plen = len ;
851
859
return 0 ;
852
860
853
861
out_unlock :
@@ -968,15 +976,15 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
968
976
break ;
969
977
}
970
978
971
- status = iomap_write_begin (iter , bytes , & folio );
979
+ status = iomap_write_begin (iter , & folio , & offset , & bytes );
972
980
if (unlikely (status )) {
973
981
iomap_write_failed (iter -> inode , iter -> pos , bytes );
974
982
break ;
975
983
}
976
984
if (iter -> iomap .flags & IOMAP_F_STALE )
977
985
break ;
978
986
979
- pos = iomap_trim_folio_range ( iter , folio , & offset , & bytes ) ;
987
+ pos = iter -> pos ;
980
988
981
989
if (mapping_writably_mapped (mapping ))
982
990
flush_dcache_folio (folio );
@@ -1296,14 +1304,12 @@ static int iomap_unshare_iter(struct iomap_iter *iter)
1296
1304
bool ret ;
1297
1305
1298
1306
bytes = min_t (u64 , SIZE_MAX , bytes );
1299
- status = iomap_write_begin (iter , bytes , & folio );
1307
+ status = iomap_write_begin (iter , & folio , & offset , & bytes );
1300
1308
if (unlikely (status ))
1301
1309
return status ;
1302
1310
if (iomap -> flags & IOMAP_F_STALE )
1303
1311
break ;
1304
1312
1305
- iomap_trim_folio_range (iter , folio , & offset , & bytes );
1306
-
1307
1313
ret = iomap_write_end (iter , bytes , bytes , folio );
1308
1314
__iomap_put_folio (iter , bytes , folio );
1309
1315
if (WARN_ON_ONCE (!ret ))
@@ -1368,7 +1374,7 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
1368
1374
bool ret ;
1369
1375
1370
1376
bytes = min_t (u64 , SIZE_MAX , bytes );
1371
- status = iomap_write_begin (iter , bytes , & folio );
1377
+ status = iomap_write_begin (iter , & folio , & offset , & bytes );
1372
1378
if (status )
1373
1379
return status ;
1374
1380
if (iter -> iomap .flags & IOMAP_F_STALE )
@@ -1377,7 +1383,6 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
1377
1383
/* warn about zeroing folios beyond eof that won't write back */
1378
1384
WARN_ON_ONCE (folio_pos (folio ) > iter -> inode -> i_size );
1379
1385
1380
- iomap_trim_folio_range (iter , folio , & offset , & bytes );
1381
1386
folio_zero_range (folio , offset , bytes );
1382
1387
folio_mark_accessed (folio );
1383
1388
0 commit comments