14
14
#include <linux/pagevec.h>
15
15
#include <linux/blkdev.h>
16
16
#include <linux/bio.h>
17
+ #include <linux/blk-crypto.h>
17
18
#include <linux/swap.h>
18
19
#include <linux/prefetch.h>
19
20
#include <linux/uio.h>
@@ -459,6 +460,33 @@ static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
459
460
return bio ;
460
461
}
461
462
463
+ static void f2fs_set_bio_crypt_ctx (struct bio * bio , const struct inode * inode ,
464
+ pgoff_t first_idx ,
465
+ const struct f2fs_io_info * fio ,
466
+ gfp_t gfp_mask )
467
+ {
468
+ /*
469
+ * The f2fs garbage collector sets ->encrypted_page when it wants to
470
+ * read/write raw data without encryption.
471
+ */
472
+ if (!fio || !fio -> encrypted_page )
473
+ fscrypt_set_bio_crypt_ctx (bio , inode , first_idx , gfp_mask );
474
+ }
475
+
476
+ static bool f2fs_crypt_mergeable_bio (struct bio * bio , const struct inode * inode ,
477
+ pgoff_t next_idx ,
478
+ const struct f2fs_io_info * fio )
479
+ {
480
+ /*
481
+ * The f2fs garbage collector sets ->encrypted_page when it wants to
482
+ * read/write raw data without encryption.
483
+ */
484
+ if (fio && fio -> encrypted_page )
485
+ return !bio_has_crypt_ctx (bio );
486
+
487
+ return fscrypt_mergeable_bio (bio , inode , next_idx );
488
+ }
489
+
462
490
static inline void __submit_bio (struct f2fs_sb_info * sbi ,
463
491
struct bio * bio , enum page_type type )
464
492
{
@@ -684,6 +712,9 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
684
712
/* Allocate a new bio */
685
713
bio = __bio_alloc (fio , 1 );
686
714
715
+ f2fs_set_bio_crypt_ctx (bio , fio -> page -> mapping -> host ,
716
+ fio -> page -> index , fio , GFP_NOIO );
717
+
687
718
if (bio_add_page (bio , page , PAGE_SIZE , 0 ) < PAGE_SIZE ) {
688
719
bio_put (bio );
689
720
return - EFAULT ;
@@ -763,9 +794,10 @@ static void del_bio_entry(struct bio_entry *be)
763
794
kmem_cache_free (bio_entry_slab , be );
764
795
}
765
796
766
- static int add_ipu_page (struct f2fs_sb_info * sbi , struct bio * * bio ,
797
+ static int add_ipu_page (struct f2fs_io_info * fio , struct bio * * bio ,
767
798
struct page * page )
768
799
{
800
+ struct f2fs_sb_info * sbi = fio -> sbi ;
769
801
enum temp_type temp ;
770
802
bool found = false;
771
803
int ret = - EAGAIN ;
@@ -782,13 +814,19 @@ static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio,
782
814
783
815
found = true;
784
816
785
- if (bio_add_page (* bio , page , PAGE_SIZE , 0 ) ==
786
- PAGE_SIZE ) {
817
+ f2fs_bug_on (sbi , !page_is_mergeable (sbi , * bio ,
818
+ * fio -> last_block ,
819
+ fio -> new_blkaddr ));
820
+ if (f2fs_crypt_mergeable_bio (* bio ,
821
+ fio -> page -> mapping -> host ,
822
+ fio -> page -> index , fio ) &&
823
+ bio_add_page (* bio , page , PAGE_SIZE , 0 ) ==
824
+ PAGE_SIZE ) {
787
825
ret = 0 ;
788
826
break ;
789
827
}
790
828
791
- /* bio is full */
829
+ /* page can't be merged into bio; submit the bio */
792
830
del_bio_entry (be );
793
831
__submit_bio (sbi , * bio , DATA );
794
832
break ;
@@ -880,11 +918,13 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
880
918
if (!bio ) {
881
919
bio = __bio_alloc (fio , BIO_MAX_PAGES );
882
920
__attach_io_flag (fio );
921
+ f2fs_set_bio_crypt_ctx (bio , fio -> page -> mapping -> host ,
922
+ fio -> page -> index , fio , GFP_NOIO );
883
923
bio_set_op_attrs (bio , fio -> op , fio -> op_flags );
884
924
885
925
add_bio_entry (fio -> sbi , bio , page , fio -> temp );
886
926
} else {
887
- if (add_ipu_page (fio -> sbi , & bio , page ))
927
+ if (add_ipu_page (fio , & bio , page ))
888
928
goto alloc_new ;
889
929
}
890
930
@@ -936,8 +976,11 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
936
976
937
977
inc_page_count (sbi , WB_DATA_TYPE (bio_page ));
938
978
939
- if (io -> bio && !io_is_mergeable (sbi , io -> bio , io , fio ,
940
- io -> last_block_in_bio , fio -> new_blkaddr ))
979
+ if (io -> bio &&
980
+ (!io_is_mergeable (sbi , io -> bio , io , fio , io -> last_block_in_bio ,
981
+ fio -> new_blkaddr ) ||
982
+ !f2fs_crypt_mergeable_bio (io -> bio , fio -> page -> mapping -> host ,
983
+ bio_page -> index , fio )))
941
984
__submit_merged_bio (io );
942
985
alloc_new :
943
986
if (io -> bio == NULL ) {
@@ -949,6 +992,8 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
949
992
goto skip ;
950
993
}
951
994
io -> bio = __bio_alloc (fio , BIO_MAX_PAGES );
995
+ f2fs_set_bio_crypt_ctx (io -> bio , fio -> page -> mapping -> host ,
996
+ bio_page -> index , fio , GFP_NOIO );
952
997
io -> fio = * fio ;
953
998
}
954
999
@@ -993,11 +1038,14 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
993
1038
for_write );
994
1039
if (!bio )
995
1040
return ERR_PTR (- ENOMEM );
1041
+
1042
+ f2fs_set_bio_crypt_ctx (bio , inode , first_idx , NULL , GFP_NOFS );
1043
+
996
1044
f2fs_target_device (sbi , blkaddr , bio );
997
1045
bio -> bi_end_io = f2fs_read_end_io ;
998
1046
bio_set_op_attrs (bio , REQ_OP_READ , op_flag );
999
1047
1000
- if (f2fs_encrypted_file (inode ))
1048
+ if (fscrypt_inode_uses_fs_layer_crypto (inode ))
1001
1049
post_read_steps |= 1 << STEP_DECRYPT ;
1002
1050
if (f2fs_compressed_file (inode ))
1003
1051
post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ ;
@@ -2073,8 +2121,9 @@ static int f2fs_read_single_page(struct inode *inode, struct page *page,
2073
2121
* This page will go to BIO. Do we need to send this
2074
2122
* BIO off first?
2075
2123
*/
2076
- if (bio && !page_is_mergeable (F2FS_I_SB (inode ), bio ,
2077
- * last_block_in_bio , block_nr )) {
2124
+ if (bio && (!page_is_mergeable (F2FS_I_SB (inode ), bio ,
2125
+ * last_block_in_bio , block_nr ) ||
2126
+ !f2fs_crypt_mergeable_bio (bio , inode , page -> index , NULL ))) {
2078
2127
submit_and_realloc :
2079
2128
__submit_bio (F2FS_I_SB (inode ), bio , DATA );
2080
2129
bio = NULL ;
@@ -2204,8 +2253,9 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2204
2253
blkaddr = data_blkaddr (dn .inode , dn .node_page ,
2205
2254
dn .ofs_in_node + i + 1 );
2206
2255
2207
- if (bio && !page_is_mergeable (sbi , bio ,
2208
- * last_block_in_bio , blkaddr )) {
2256
+ if (bio && (!page_is_mergeable (sbi , bio ,
2257
+ * last_block_in_bio , blkaddr ) ||
2258
+ !f2fs_crypt_mergeable_bio (bio , inode , page -> index , NULL ))) {
2209
2259
submit_and_realloc :
2210
2260
__submit_bio (sbi , bio , DATA );
2211
2261
bio = NULL ;
@@ -2421,6 +2471,9 @@ int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2421
2471
/* wait for GCed page writeback via META_MAPPING */
2422
2472
f2fs_wait_on_block_writeback (inode , fio -> old_blkaddr );
2423
2473
2474
+ if (fscrypt_inode_uses_inline_crypto (inode ))
2475
+ return 0 ;
2476
+
2424
2477
retry_encrypt :
2425
2478
fio -> encrypted_page = fscrypt_encrypt_pagecache_blocks (page ,
2426
2479
PAGE_SIZE , 0 , gfp_flags );
@@ -2594,7 +2647,7 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2594
2647
f2fs_unlock_op (fio -> sbi );
2595
2648
err = f2fs_inplace_write_data (fio );
2596
2649
if (err ) {
2597
- if (f2fs_encrypted_file (inode ))
2650
+ if (fscrypt_inode_uses_fs_layer_crypto (inode ))
2598
2651
fscrypt_finalize_bounce_page (& fio -> encrypted_page );
2599
2652
if (PageWriteback (page ))
2600
2653
end_page_writeback (page );
0 commit comments