Factor out a common function to remove duplicate code in ovl_read_iter/ovl_write_iter. Signed-off-by: yangerkun <yangerkun@xxxxxxxxxx> --- fs/overlayfs/file.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index d081faa55e83..4ac3cd698c7d 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -282,6 +282,23 @@ static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2) orig_iocb->ki_complete(orig_iocb, res, res2); } +static struct ovl_aio_req *ovl_get_aio_req(struct kiocb *iocb, struct fd *real, int ifl) +{ + struct ovl_aio_req *req; + + req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); + if (!req) + return NULL; + + req->fd = *real; + real->flags = 0; + req->orig_iocb = iocb; + kiocb_clone(&req->iocb, iocb, real->file); + req->iocb.ki_flags = ifl; + req->iocb.ki_complete = ovl_aio_rw_complete; + return req; +} + static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; @@ -304,15 +321,10 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) struct ovl_aio_req *aio_req; ret = -ENOMEM; - aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); + aio_req = ovl_get_aio_req(iocb, &real, iocb->ki_flags); if (!aio_req) goto out; - aio_req->fd = real; - real.flags = 0; - aio_req->orig_iocb = iocb; - kiocb_clone(&aio_req->iocb, iocb, real.file); - aio_req->iocb.ki_complete = ovl_aio_rw_complete; ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter); if (ret != -EIOCBQUEUED) ovl_aio_cleanup_handler(aio_req); @@ -364,7 +376,7 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) struct ovl_aio_req *aio_req; ret = -ENOMEM; - aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); + aio_req = ovl_get_aio_req(iocb, &real, ifl); if (!aio_req) goto out; @@ -372,12 +384,6 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) /* Pacify lockdep, same trick as done in aio_write() */ __sb_writers_release(file_inode(real.file)->i_sb, SB_FREEZE_WRITE); - aio_req->fd = real; - real.flags = 0; - aio_req->orig_iocb = iocb; - kiocb_clone(&aio_req->iocb, iocb, real.file); - aio_req->iocb.ki_flags = ifl; - aio_req->iocb.ki_complete = ovl_aio_rw_complete; ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter); if (ret != -EIOCBQUEUED) ovl_aio_cleanup_handler(aio_req); -- 2.31.1