io_uring: poll: remove unnecessary req->ref set
[linux-block.git] / io_uring / sync.c
CommitLineData
0d584727
JA
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/fs.h>
5#include <linux/file.h>
6#include <linux/mm.h>
7#include <linux/slab.h>
8#include <linux/namei.h>
9#include <linux/io_uring.h>
10#include <linux/fsnotify.h>
11
12#include <uapi/linux/io_uring.h>
13
14#include "io_uring_types.h"
15#include "io_uring.h"
16#include "sync.h"
17
18struct io_sync {
19 struct file *file;
20 loff_t len;
21 loff_t off;
22 int flags;
23 int mode;
24};
25
26int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
27{
28 struct io_sync *sync = io_kiocb_to_cmd(req);
29
30 if (unlikely(sqe->addr || sqe->buf_index || sqe->splice_fd_in))
31 return -EINVAL;
32
33 sync->off = READ_ONCE(sqe->off);
34 sync->len = READ_ONCE(sqe->len);
35 sync->flags = READ_ONCE(sqe->sync_range_flags);
36 return 0;
37}
38
39int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
40{
41 struct io_sync *sync = io_kiocb_to_cmd(req);
42 int ret;
43
44 /* sync_file_range always requires a blocking context */
45 if (issue_flags & IO_URING_F_NONBLOCK)
46 return -EAGAIN;
47
48 ret = sync_file_range(req->file, sync->off, sync->len, sync->flags);
49 io_req_set_res(req, ret, 0);
50 return IOU_OK;
51}
52
53int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
54{
55 struct io_sync *sync = io_kiocb_to_cmd(req);
56
57 if (unlikely(sqe->addr || sqe->buf_index || sqe->splice_fd_in))
58 return -EINVAL;
59
60 sync->flags = READ_ONCE(sqe->fsync_flags);
61 if (unlikely(sync->flags & ~IORING_FSYNC_DATASYNC))
62 return -EINVAL;
63
64 sync->off = READ_ONCE(sqe->off);
65 sync->len = READ_ONCE(sqe->len);
66 return 0;
67}
68
69int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
70{
71 struct io_sync *sync = io_kiocb_to_cmd(req);
72 loff_t end = sync->off + sync->len;
73 int ret;
74
75 /* fsync always requires a blocking context */
76 if (issue_flags & IO_URING_F_NONBLOCK)
77 return -EAGAIN;
78
79 ret = vfs_fsync_range(req->file, sync->off, end > 0 ? end : LLONG_MAX,
80 sync->flags & IORING_FSYNC_DATASYNC);
81 io_req_set_res(req, ret, 0);
82 return IOU_OK;
83}
84
85int io_fallocate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
86{
87 struct io_sync *sync = io_kiocb_to_cmd(req);
88
89 if (sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
90 return -EINVAL;
91
92 sync->off = READ_ONCE(sqe->off);
93 sync->len = READ_ONCE(sqe->addr);
94 sync->mode = READ_ONCE(sqe->len);
95 return 0;
96}
97
98int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
99{
100 struct io_sync *sync = io_kiocb_to_cmd(req);
101 int ret;
102
103 /* fallocate always requiring blocking context */
104 if (issue_flags & IO_URING_F_NONBLOCK)
105 return -EAGAIN;
106 ret = vfs_fallocate(req->file, sync->mode, sync->off, sync->len);
107 if (ret >= 0)
108 fsnotify_modify(req->file);
109 io_req_set_res(req, ret, 0);
110 return IOU_OK;
111}