f2fs: make get_lock_data_page to handle encrypted inode

This patch refactors get_lock_data_page() to handle encryption case directly.
In order to do that, it introduces common f2fs_submit_page_read().

Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>

+51 -58
+51 -58
fs/f2fs/data.c
··· 456 return err; 457 } 458 459 static void __set_data_blkaddr(struct dnode_of_data *dn) 460 { 461 struct f2fs_node *rn = F2FS_NODE(dn->node_page); ··· 620 struct page *page; 621 struct extent_info ei = {0,0,0}; 622 int err; 623 - struct f2fs_io_info fio = { 624 - .sbi = F2FS_I_SB(inode), 625 - .type = DATA, 626 - .op = REQ_OP_READ, 627 - .op_flags = op_flags, 628 - .encrypted_page = NULL, 629 - }; 630 - 631 - if (f2fs_encrypted_file(inode)) 632 - return read_mapping_page(mapping, index, NULL); 633 634 page = f2fs_grab_cache_page(mapping, index, for_write); 635 if (!page) ··· 660 return page; 661 } 662 663 - fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr; 664 - fio.page = page; 665 - err = f2fs_submit_page_bio(&fio); 666 if (err) 667 goto put_err; 668 return page; ··· 1185 return ret; 1186 } 1187 1188 - static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr, 1189 - unsigned nr_pages) 1190 - { 1191 - struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1192 - struct fscrypt_ctx *ctx = NULL; 1193 - struct bio *bio; 1194 - 1195 - if (f2fs_encrypted_file(inode)) { 1196 - ctx = fscrypt_get_ctx(inode, GFP_NOFS); 1197 - if (IS_ERR(ctx)) 1198 - return ERR_CAST(ctx); 1199 - 1200 - /* wait the page to be moved by cleaning */ 1201 - f2fs_wait_on_block_writeback(sbi, blkaddr); 1202 - } 1203 - 1204 - bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES)); 1205 - if (!bio) { 1206 - if (ctx) 1207 - fscrypt_release_ctx(ctx); 1208 - return ERR_PTR(-ENOMEM); 1209 - } 1210 - f2fs_target_device(sbi, blkaddr, bio); 1211 - bio->bi_end_io = f2fs_read_end_io; 1212 - bio->bi_private = ctx; 1213 - 1214 - return bio; 1215 - } 1216 - 1217 /* 1218 * This function was originally taken from fs/mpage.c, and customized for f2fs. 1219 * Major change was from block_size == page_size in f2fs by default. ··· 1281 bio = NULL; 1282 } 1283 if (bio == NULL) { 1284 - bio = f2fs_grab_bio(inode, block_nr, nr_pages); 1285 if (IS_ERR(bio)) { 1286 bio = NULL; 1287 goto set_error_page; 1288 } 1289 - bio_set_op_attrs(bio, REQ_OP_READ, 0); 1290 } 1291 1292 if (bio_add_page(bio, page, blocksize, 0) < blocksize) ··· 1994 zero_user_segment(page, 0, PAGE_SIZE); 1995 SetPageUptodate(page); 1996 } else { 1997 - struct bio *bio; 1998 - 1999 - bio = f2fs_grab_bio(inode, blkaddr, 1); 2000 - if (IS_ERR(bio)) { 2001 - err = PTR_ERR(bio); 2002 goto fail; 2003 - } 2004 - bio->bi_opf = REQ_OP_READ; 2005 - if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { 2006 - bio_put(bio); 2007 - err = -EFAULT; 2008 - goto fail; 2009 - } 2010 - 2011 - __submit_bio(sbi, bio, DATA); 2012 2013 lock_page(page); 2014 if (unlikely(page->mapping != mapping)) {
··· 456 return err; 457 } 458 459 + static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr, 460 + unsigned nr_pages) 461 + { 462 + struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 463 + struct fscrypt_ctx *ctx = NULL; 464 + struct bio *bio; 465 + 466 + if (f2fs_encrypted_file(inode)) { 467 + ctx = fscrypt_get_ctx(inode, GFP_NOFS); 468 + if (IS_ERR(ctx)) 469 + return ERR_CAST(ctx); 470 + 471 + /* wait the page to be moved by cleaning */ 472 + f2fs_wait_on_block_writeback(sbi, blkaddr); 473 + } 474 + 475 + bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES)); 476 + if (!bio) { 477 + if (ctx) 478 + fscrypt_release_ctx(ctx); 479 + return ERR_PTR(-ENOMEM); 480 + } 481 + f2fs_target_device(sbi, blkaddr, bio); 482 + bio->bi_end_io = f2fs_read_end_io; 483 + bio->bi_private = ctx; 484 + bio_set_op_attrs(bio, REQ_OP_READ, 0); 485 + 486 + return bio; 487 + } 488 + 489 + /* This can handle encryption stuffs */ 490 + static int f2fs_submit_page_read(struct inode *inode, struct page *page, 491 + block_t blkaddr) 492 + { 493 + struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1); 494 + 495 + if (IS_ERR(bio)) 496 + return PTR_ERR(bio); 497 + 498 + if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { 499 + bio_put(bio); 500 + return -EFAULT; 501 + } 502 + __submit_bio(F2FS_I_SB(inode), bio, DATA); 503 + return 0; 504 + } 505 + 506 static void __set_data_blkaddr(struct dnode_of_data *dn) 507 { 508 struct f2fs_node *rn = F2FS_NODE(dn->node_page); ··· 573 struct page *page; 574 struct extent_info ei = {0,0,0}; 575 int err; 576 577 page = f2fs_grab_cache_page(mapping, index, for_write); 578 if (!page) ··· 623 return page; 624 } 625 626 + err = f2fs_submit_page_read(inode, page, dn.data_blkaddr); 627 if (err) 628 goto put_err; 629 return page; ··· 1150 return ret; 1151 } 1152 1153 /* 1154 * This function was originally taken from fs/mpage.c, and customized for f2fs. 1155 * Major change was from block_size == page_size in f2fs by default. ··· 1275 bio = NULL; 1276 } 1277 if (bio == NULL) { 1278 + bio = f2fs_grab_read_bio(inode, block_nr, nr_pages); 1279 if (IS_ERR(bio)) { 1280 bio = NULL; 1281 goto set_error_page; 1282 } 1283 } 1284 1285 if (bio_add_page(bio, page, blocksize, 0) < blocksize) ··· 1989 zero_user_segment(page, 0, PAGE_SIZE); 1990 SetPageUptodate(page); 1991 } else { 1992 + err = f2fs_submit_page_read(inode, page, blkaddr); 1993 + if (err) 1994 goto fail; 1995 1996 lock_page(page); 1997 if (unlikely(page->mapping != mapping)) {