1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
5 #include <linux/file.h>
6 #include <linux/blk-mq.h>
8 #include <linux/slab.h>
9 #include <linux/fsnotify.h>
10 #include <linux/poll.h>
11 #include <linux/nospec.h>
12 #include <linux/compat.h>
13 #include <linux/io_uring.h>
15 #include <uapi/linux/io_uring.h>
24 /* NOTE: kiocb has the file as the first member, so don't do it here */
31 static inline bool io_file_supports_nowait(struct io_kiocb *req)
33 return req->flags & REQ_F_SUPPORT_NOWAIT;
37 static int io_iov_compat_buffer_select_prep(struct io_rw *rw)
39 struct compat_iovec __user *uiov;
42 uiov = u64_to_user_ptr(rw->addr);
43 if (!access_ok(uiov, sizeof(*uiov)))
45 if (__get_user(clen, &uiov->iov_len))
55 static int io_iov_buffer_select_prep(struct io_kiocb *req)
57 struct iovec __user *uiov;
59 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
66 return io_iov_compat_buffer_select_prep(rw);
69 uiov = u64_to_user_ptr(rw->addr);
70 if (copy_from_user(&iov, uiov, sizeof(*uiov)))
72 rw->len = iov.iov_len;
76 int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
78 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
82 rw->kiocb.ki_pos = READ_ONCE(sqe->off);
83 /* used for fixed read/write too - just read unconditionally */
84 req->buf_index = READ_ONCE(sqe->buf_index);
86 if (req->opcode == IORING_OP_READ_FIXED ||
87 req->opcode == IORING_OP_WRITE_FIXED) {
88 struct io_ring_ctx *ctx = req->ctx;
91 if (unlikely(req->buf_index >= ctx->nr_user_bufs))
93 index = array_index_nospec(req->buf_index, ctx->nr_user_bufs);
94 req->imu = ctx->user_bufs[index];
95 io_req_set_rsrc_node(req, ctx, 0);
98 ioprio = READ_ONCE(sqe->ioprio);
100 ret = ioprio_check_cap(ioprio);
104 rw->kiocb.ki_ioprio = ioprio;
106 rw->kiocb.ki_ioprio = get_current_ioprio();
109 rw->addr = READ_ONCE(sqe->addr);
110 rw->len = READ_ONCE(sqe->len);
111 rw->flags = READ_ONCE(sqe->rw_flags);
113 /* Have to do this validation here, as this is in io_read() rw->len might
114 * have chanaged due to buffer selection
116 if (req->opcode == IORING_OP_READV && req->flags & REQ_F_BUFFER_SELECT) {
117 ret = io_iov_buffer_select_prep(req);
125 void io_readv_writev_cleanup(struct io_kiocb *req)
127 struct io_async_rw *io = req->async_data;
129 kfree(io->free_iovec);
132 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
138 case -ERESTARTNOINTR:
139 case -ERESTARTNOHAND:
140 case -ERESTART_RESTARTBLOCK:
142 * We can't just restart the syscall, since previously
143 * submitted sqes may already be in progress. Just fail this
149 kiocb->ki_complete(kiocb, ret);
153 static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req)
155 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
157 if (rw->kiocb.ki_pos != -1)
158 return &rw->kiocb.ki_pos;
160 if (!(req->file->f_mode & FMODE_STREAM)) {
161 req->flags |= REQ_F_CUR_POS;
162 rw->kiocb.ki_pos = req->file->f_pos;
163 return &rw->kiocb.ki_pos;
166 rw->kiocb.ki_pos = 0;
170 static void io_req_task_queue_reissue(struct io_kiocb *req)
172 req->io_task_work.func = io_queue_iowq;
173 io_req_task_work_add(req);
177 static bool io_resubmit_prep(struct io_kiocb *req)
179 struct io_async_rw *io = req->async_data;
181 if (!req_has_async_data(req))
182 return !io_req_prep_async(req);
183 iov_iter_restore(&io->s.iter, &io->s.iter_state);
187 static bool io_rw_should_reissue(struct io_kiocb *req)
189 umode_t mode = file_inode(req->file)->i_mode;
190 struct io_ring_ctx *ctx = req->ctx;
192 if (!S_ISBLK(mode) && !S_ISREG(mode))
194 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
195 !(ctx->flags & IORING_SETUP_IOPOLL)))
198 * If ref is dying, we might be running poll reap from the exit work.
199 * Don't attempt to reissue from that path, just let it fail with
202 if (percpu_ref_is_dying(&ctx->refs))
205 * Play it safe and assume not safe to re-import and reissue if we're
206 * not in the original thread group (or in task context).
208 if (!same_thread_group(req->task, current) || !in_task())
213 static bool io_resubmit_prep(struct io_kiocb *req)
217 static bool io_rw_should_reissue(struct io_kiocb *req)
223 static void kiocb_end_write(struct io_kiocb *req)
226 * Tell lockdep we inherited freeze protection from submission
229 if (req->flags & REQ_F_ISREG) {
230 struct super_block *sb = file_inode(req->file)->i_sb;
232 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
238 * Trigger the notifications after having done some IO, and finish the write
239 * accounting, if any.
241 static void io_req_io_end(struct io_kiocb *req)
243 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
247 if (rw->kiocb.ki_flags & IOCB_WRITE) {
248 kiocb_end_write(req);
249 fsnotify_modify(req->file);
251 fsnotify_access(req->file);
255 static bool __io_complete_rw_common(struct io_kiocb *req, long res)
257 if (unlikely(res != req->cqe.res)) {
258 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
259 io_rw_should_reissue(req)) {
261 * Reissue will start accounting again, finish the
265 req->flags |= REQ_F_REISSUE | REQ_F_PARTIAL_IO;
274 static inline int io_fixup_rw_res(struct io_kiocb *req, long res)
276 struct io_async_rw *io = req->async_data;
278 /* add previously done IO, if any */
279 if (req_has_async_data(req) && io->bytes_done > 0) {
281 res = io->bytes_done;
283 res += io->bytes_done;
288 static void io_req_rw_complete(struct io_kiocb *req, bool *locked)
291 io_req_task_complete(req, locked);
294 static void io_complete_rw(struct kiocb *kiocb, long res)
296 struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
297 struct io_kiocb *req = cmd_to_io_kiocb(rw);
299 if (__io_complete_rw_common(req, res))
301 io_req_set_res(req, io_fixup_rw_res(req, res), 0);
302 req->io_task_work.func = io_req_rw_complete;
303 io_req_task_work_add(req);
306 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
308 struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
309 struct io_kiocb *req = cmd_to_io_kiocb(rw);
311 if (kiocb->ki_flags & IOCB_WRITE)
312 kiocb_end_write(req);
313 if (unlikely(res != req->cqe.res)) {
314 if (res == -EAGAIN && io_rw_should_reissue(req)) {
315 req->flags |= REQ_F_REISSUE | REQ_F_PARTIAL_IO;
321 /* order with io_iopoll_complete() checking ->iopoll_completed */
322 smp_store_release(&req->iopoll_completed, 1);
325 static int kiocb_done(struct io_kiocb *req, ssize_t ret,
326 unsigned int issue_flags)
328 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
329 unsigned final_ret = io_fixup_rw_res(req, ret);
331 if (req->flags & REQ_F_CUR_POS)
332 req->file->f_pos = rw->kiocb.ki_pos;
333 if (ret >= 0 && (rw->kiocb.ki_complete == io_complete_rw)) {
334 if (!__io_complete_rw_common(req, ret)) {
336 * Safe to call io_end from here as we're inline
337 * from the submission path.
340 io_req_set_res(req, final_ret,
341 io_put_kbuf(req, issue_flags));
345 io_rw_done(&rw->kiocb, ret);
348 if (req->flags & REQ_F_REISSUE) {
349 req->flags &= ~REQ_F_REISSUE;
350 if (io_resubmit_prep(req))
351 io_req_task_queue_reissue(req);
353 io_req_task_queue_fail(req, final_ret);
355 return IOU_ISSUE_SKIP_COMPLETE;
358 static struct iovec *__io_import_iovec(int ddir, struct io_kiocb *req,
359 struct io_rw_state *s,
360 unsigned int issue_flags)
362 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
363 struct iov_iter *iter = &s->iter;
364 u8 opcode = req->opcode;
370 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
371 ret = io_import_fixed(ddir, iter, req->imu, rw->addr, rw->len);
377 buf = u64_to_user_ptr(rw->addr);
380 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE ||
381 (req->flags & REQ_F_BUFFER_SELECT)) {
382 if (io_do_buffer_select(req)) {
383 buf = io_buffer_select(req, &sqe_len, issue_flags);
385 return ERR_PTR(-ENOBUFS);
386 rw->addr = (unsigned long) buf;
390 ret = import_single_range(ddir, buf, sqe_len, s->fast_iov, iter);
397 ret = __import_iovec(ddir, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
399 if (unlikely(ret < 0))
404 static inline int io_import_iovec(int rw, struct io_kiocb *req,
405 struct iovec **iovec, struct io_rw_state *s,
406 unsigned int issue_flags)
408 *iovec = __io_import_iovec(rw, req, s, issue_flags);
409 if (unlikely(IS_ERR(*iovec)))
410 return PTR_ERR(*iovec);
412 iov_iter_save_state(&s->iter, &s->iter_state);
416 static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
418 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
422 * For files that don't have ->read_iter() and ->write_iter(), handle them
423 * by looping over ->read() or ->write() manually.
425 static ssize_t loop_rw_iter(int ddir, struct io_rw *rw, struct iov_iter *iter)
427 struct kiocb *kiocb = &rw->kiocb;
428 struct file *file = kiocb->ki_filp;
433 * Don't support polled IO through this interface, and we can't
434 * support non-blocking either. For the latter, this just causes
435 * the kiocb to be handled from an async context.
437 if (kiocb->ki_flags & IOCB_HIPRI)
439 if ((kiocb->ki_flags & IOCB_NOWAIT) &&
440 !(kiocb->ki_filp->f_flags & O_NONBLOCK))
443 ppos = io_kiocb_ppos(kiocb);
445 while (iov_iter_count(iter)) {
449 if (!iov_iter_is_bvec(iter)) {
450 iovec = iov_iter_iovec(iter);
452 iovec.iov_base = u64_to_user_ptr(rw->addr);
453 iovec.iov_len = rw->len;
457 nr = file->f_op->read(file, iovec.iov_base,
458 iovec.iov_len, ppos);
460 nr = file->f_op->write(file, iovec.iov_base,
461 iovec.iov_len, ppos);
470 if (!iov_iter_is_bvec(iter)) {
471 iov_iter_advance(iter, nr);
478 if (nr != iovec.iov_len)
485 static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
486 const struct iovec *fast_iov, struct iov_iter *iter)
488 struct io_async_rw *io = req->async_data;
490 memcpy(&io->s.iter, iter, sizeof(*iter));
491 io->free_iovec = iovec;
493 /* can only be fixed buffers, no need to do anything */
494 if (iov_iter_is_bvec(iter))
497 unsigned iov_off = 0;
499 io->s.iter.iov = io->s.fast_iov;
500 if (iter->iov != fast_iov) {
501 iov_off = iter->iov - fast_iov;
502 io->s.iter.iov += iov_off;
504 if (io->s.fast_iov != fast_iov)
505 memcpy(io->s.fast_iov + iov_off, fast_iov + iov_off,
506 sizeof(struct iovec) * iter->nr_segs);
508 req->flags |= REQ_F_NEED_CLEANUP;
512 static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
513 struct io_rw_state *s, bool force)
515 if (!force && !io_op_defs[req->opcode].prep_async)
517 if (!req_has_async_data(req)) {
518 struct io_async_rw *iorw;
520 if (io_alloc_async_data(req)) {
525 io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
526 iorw = req->async_data;
527 /* we've copied and mapped the iter, ensure state is saved */
528 iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
533 static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
535 struct io_async_rw *iorw = req->async_data;
539 /* submission path, ->uring_lock should already be taken */
540 ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
541 if (unlikely(ret < 0))
544 iorw->bytes_done = 0;
545 iorw->free_iovec = iov;
547 req->flags |= REQ_F_NEED_CLEANUP;
551 int io_readv_prep_async(struct io_kiocb *req)
553 return io_rw_prep_async(req, READ);
556 int io_writev_prep_async(struct io_kiocb *req)
558 return io_rw_prep_async(req, WRITE);
562 * This is our waitqueue callback handler, registered through __folio_lock_async()
563 * when we initially tried to do the IO with the iocb armed our waitqueue.
564 * This gets called when the page is unlocked, and we generally expect that to
565 * happen when the page IO is completed and the page is now uptodate. This will
566 * queue a task_work based retry of the operation, attempting to copy the data
567 * again. If the latter fails because the page was NOT uptodate, then we will
568 * do a thread based blocking retry of the operation. That's the unexpected
571 static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
574 struct wait_page_queue *wpq;
575 struct io_kiocb *req = wait->private;
576 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
577 struct wait_page_key *key = arg;
579 wpq = container_of(wait, struct wait_page_queue, wait);
581 if (!wake_page_match(wpq, key))
584 rw->kiocb.ki_flags &= ~IOCB_WAITQ;
585 list_del_init(&wait->entry);
586 io_req_task_queue(req);
591 * This controls whether a given IO request should be armed for async page
592 * based retry. If we return false here, the request is handed to the async
593 * worker threads for retry. If we're doing buffered reads on a regular file,
594 * we prepare a private wait_page_queue entry and retry the operation. This
595 * will either succeed because the page is now uptodate and unlocked, or it
596 * will register a callback when the page is unlocked at IO completion. Through
597 * that callback, io_uring uses task_work to setup a retry of the operation.
598 * That retry will attempt the buffered read again. The retry will generally
599 * succeed, or in rare cases where it fails, we then fall back to using the
600 * async worker threads for a blocking retry.
602 static bool io_rw_should_retry(struct io_kiocb *req)
604 struct io_async_rw *io = req->async_data;
605 struct wait_page_queue *wait = &io->wpq;
606 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
607 struct kiocb *kiocb = &rw->kiocb;
609 /* never retry for NOWAIT, we just complete with -EAGAIN */
610 if (req->flags & REQ_F_NOWAIT)
613 /* Only for buffered IO */
614 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
618 * just use poll if we can, and don't attempt if the fs doesn't
619 * support callback based unlocks
621 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
624 wait->wait.func = io_async_buf_func;
625 wait->wait.private = req;
626 wait->wait.flags = 0;
627 INIT_LIST_HEAD(&wait->wait.entry);
628 kiocb->ki_flags |= IOCB_WAITQ;
629 kiocb->ki_flags &= ~IOCB_NOWAIT;
630 kiocb->ki_waitq = wait;
634 static inline int io_iter_do_read(struct io_rw *rw, struct iov_iter *iter)
636 struct file *file = rw->kiocb.ki_filp;
638 if (likely(file->f_op->read_iter))
639 return call_read_iter(file, &rw->kiocb, iter);
640 else if (file->f_op->read)
641 return loop_rw_iter(READ, rw, iter);
646 static bool need_complete_io(struct io_kiocb *req)
648 return req->flags & REQ_F_ISREG ||
649 S_ISBLK(file_inode(req->file)->i_mode);
652 static int io_rw_init_file(struct io_kiocb *req, fmode_t mode)
654 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
655 struct kiocb *kiocb = &rw->kiocb;
656 struct io_ring_ctx *ctx = req->ctx;
657 struct file *file = req->file;
660 if (unlikely(!file || !(file->f_mode & mode)))
663 if (!io_req_ffs_set(req))
664 req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT;
666 kiocb->ki_flags = file->f_iocb_flags;
667 ret = kiocb_set_rw_flags(kiocb, rw->flags);
672 * If the file is marked O_NONBLOCK, still allow retry for it if it
673 * supports async. Otherwise it's impossible to use O_NONBLOCK files
674 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
676 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
677 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
678 req->flags |= REQ_F_NOWAIT;
680 if (ctx->flags & IORING_SETUP_IOPOLL) {
681 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
684 kiocb->private = NULL;
685 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
686 kiocb->ki_complete = io_complete_rw_iopoll;
687 req->iopoll_completed = 0;
689 if (kiocb->ki_flags & IOCB_HIPRI)
691 kiocb->ki_complete = io_complete_rw;
697 int io_read(struct io_kiocb *req, unsigned int issue_flags)
699 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
700 struct io_rw_state __s, *s = &__s;
702 struct kiocb *kiocb = &rw->kiocb;
703 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
704 struct io_async_rw *io;
708 if (!req_has_async_data(req)) {
709 ret = io_import_iovec(READ, req, &iovec, s, issue_flags);
710 if (unlikely(ret < 0))
713 io = req->async_data;
717 * Safe and required to re-import if we're using provided
718 * buffers, as we dropped the selected one before retry.
720 if (io_do_buffer_select(req)) {
721 ret = io_import_iovec(READ, req, &iovec, s, issue_flags);
722 if (unlikely(ret < 0))
727 * We come here from an earlier attempt, restore our state to
728 * match in case it doesn't. It's cheap enough that we don't
729 * need to make this conditional.
731 iov_iter_restore(&s->iter, &s->iter_state);
734 ret = io_rw_init_file(req, FMODE_READ);
739 req->cqe.res = iov_iter_count(&s->iter);
741 if (force_nonblock) {
742 /* If the file doesn't support async, just async punt */
743 if (unlikely(!io_file_supports_nowait(req))) {
744 ret = io_setup_async_rw(req, iovec, s, true);
745 return ret ?: -EAGAIN;
747 kiocb->ki_flags |= IOCB_NOWAIT;
749 /* Ensure we clear previously set non-block flag */
750 kiocb->ki_flags &= ~IOCB_NOWAIT;
753 ppos = io_kiocb_update_pos(req);
755 ret = rw_verify_area(READ, req->file, ppos, req->cqe.res);
761 ret = io_iter_do_read(rw, &s->iter);
763 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
764 req->flags &= ~REQ_F_REISSUE;
765 /* if we can poll, just do that */
766 if (req->opcode == IORING_OP_READ && file_can_poll(req->file))
768 /* IOPOLL retry should happen for io-wq threads */
769 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
771 /* no retry on NONBLOCK nor RWF_NOWAIT */
772 if (req->flags & REQ_F_NOWAIT)
775 } else if (ret == -EIOCBQUEUED) {
778 return IOU_ISSUE_SKIP_COMPLETE;
779 } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock ||
780 (req->flags & REQ_F_NOWAIT) || !need_complete_io(req)) {
781 /* read all, failed, already did sync or don't want to retry */
786 * Don't depend on the iter state matching what was consumed, or being
787 * untouched in case of error. Restore it and we'll advance it
788 * manually if we need to.
790 iov_iter_restore(&s->iter, &s->iter_state);
792 ret2 = io_setup_async_rw(req, iovec, s, true);
795 ret = ret > 0 ? ret : ret2;
799 io = req->async_data;
802 * Now use our persistent iterator and state, if we aren't already.
803 * We've restored and mapped the iter to match.
808 * We end up here because of a partial read, either from
809 * above or inside this loop. Advance the iter by the bytes
810 * that were consumed.
812 iov_iter_advance(&s->iter, ret);
813 if (!iov_iter_count(&s->iter))
815 io->bytes_done += ret;
816 iov_iter_save_state(&s->iter, &s->iter_state);
818 /* if we can retry, do so with the callbacks armed */
819 if (!io_rw_should_retry(req)) {
820 kiocb->ki_flags &= ~IOCB_WAITQ;
824 req->cqe.res = iov_iter_count(&s->iter);
826 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
827 * we get -EIOCBQUEUED, then we'll get a notification when the
828 * desired page gets unlocked. We can also get a partial read
829 * here, and if we do, then just retry at the new offset.
831 ret = io_iter_do_read(rw, &s->iter);
832 if (ret == -EIOCBQUEUED)
833 return IOU_ISSUE_SKIP_COMPLETE;
834 /* we got some bytes, but not all. retry. */
835 kiocb->ki_flags &= ~IOCB_WAITQ;
836 iov_iter_restore(&s->iter, &s->iter_state);
839 /* it's faster to check here then delegate to kfree */
842 return kiocb_done(req, ret, issue_flags);
845 int io_write(struct io_kiocb *req, unsigned int issue_flags)
847 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
848 struct io_rw_state __s, *s = &__s;
850 struct kiocb *kiocb = &rw->kiocb;
851 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
855 if (!req_has_async_data(req)) {
856 ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags);
857 if (unlikely(ret < 0))
860 struct io_async_rw *io = req->async_data;
863 iov_iter_restore(&s->iter, &s->iter_state);
866 ret = io_rw_init_file(req, FMODE_WRITE);
871 req->cqe.res = iov_iter_count(&s->iter);
873 if (force_nonblock) {
874 /* If the file doesn't support async, just async punt */
875 if (unlikely(!io_file_supports_nowait(req)))
878 /* File path supports NOWAIT for non-direct_IO only for block devices. */
879 if (!(kiocb->ki_flags & IOCB_DIRECT) &&
880 !(kiocb->ki_filp->f_mode & FMODE_BUF_WASYNC) &&
881 (req->flags & REQ_F_ISREG))
884 kiocb->ki_flags |= IOCB_NOWAIT;
886 /* Ensure we clear previously set non-block flag */
887 kiocb->ki_flags &= ~IOCB_NOWAIT;
890 ppos = io_kiocb_update_pos(req);
892 ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res);
899 * Open-code file_start_write here to grab freeze protection,
900 * which will be released by another thread in
901 * io_complete_rw(). Fool lockdep by telling it the lock got
902 * released so that it doesn't complain about the held lock when
903 * we return to userspace.
905 if (req->flags & REQ_F_ISREG) {
906 sb_start_write(file_inode(req->file)->i_sb);
907 __sb_writers_release(file_inode(req->file)->i_sb,
910 kiocb->ki_flags |= IOCB_WRITE;
912 if (likely(req->file->f_op->write_iter))
913 ret2 = call_write_iter(req->file, kiocb, &s->iter);
914 else if (req->file->f_op->write)
915 ret2 = loop_rw_iter(WRITE, rw, &s->iter);
919 if (req->flags & REQ_F_REISSUE) {
920 req->flags &= ~REQ_F_REISSUE;
925 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
926 * retry them without IOCB_NOWAIT.
928 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
930 /* no retry on NONBLOCK nor RWF_NOWAIT */
931 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
933 if (!force_nonblock || ret2 != -EAGAIN) {
934 /* IOPOLL retry should happen for io-wq threads */
935 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
938 if (ret2 != req->cqe.res && ret2 >= 0 && need_complete_io(req)) {
939 struct io_async_rw *io;
941 trace_io_uring_short_write(req->ctx, kiocb->ki_pos - ret2,
944 /* This is a partial write. The file pos has already been
945 * updated, setup the async struct to complete the request
946 * in the worker. Also update bytes_done to account for
947 * the bytes already written.
949 iov_iter_save_state(&s->iter, &s->iter_state);
950 ret = io_setup_async_rw(req, iovec, s, true);
952 io = req->async_data;
954 io->bytes_done += ret2;
956 if (kiocb->ki_flags & IOCB_WRITE)
957 kiocb_end_write(req);
958 return ret ? ret : -EAGAIN;
961 ret = kiocb_done(req, ret2, issue_flags);
964 iov_iter_restore(&s->iter, &s->iter_state);
965 ret = io_setup_async_rw(req, iovec, s, false);
967 if (kiocb->ki_flags & IOCB_WRITE)
968 kiocb_end_write(req);
973 /* it's reportedly faster than delegating the null check to kfree() */
979 static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
981 io_commit_cqring_flush(ctx);
982 if (ctx->flags & IORING_SETUP_SQPOLL)
986 void io_rw_fail(struct io_kiocb *req)
990 res = io_fixup_rw_res(req, req->cqe.res);
991 io_req_set_res(req, res, req->cqe.flags);
994 int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
996 struct io_wq_work_node *pos, *start, *prev;
997 unsigned int poll_flags = BLK_POLL_NOSLEEP;
998 DEFINE_IO_COMP_BATCH(iob);
1002 * Only spin for completions if we don't have multiple devices hanging
1003 * off our complete list.
1005 if (ctx->poll_multi_queue || force_nonspin)
1006 poll_flags |= BLK_POLL_ONESHOT;
1008 wq_list_for_each(pos, start, &ctx->iopoll_list) {
1009 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
1010 struct file *file = req->file;
1014 * Move completed and retryable entries to our local lists.
1015 * If we find a request that requires polling, break out
1016 * and complete those lists first, if we have entries there.
1018 if (READ_ONCE(req->iopoll_completed))
1021 if (req->opcode == IORING_OP_URING_CMD) {
1022 struct io_uring_cmd *ioucmd;
1024 ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
1025 ret = file->f_op->uring_cmd_iopoll(ioucmd, &iob,
1028 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
1030 ret = file->f_op->iopoll(&rw->kiocb, &iob, poll_flags);
1032 if (unlikely(ret < 0))
1035 poll_flags |= BLK_POLL_ONESHOT;
1037 /* iopoll may have completed current req */
1038 if (!rq_list_empty(iob.req_list) ||
1039 READ_ONCE(req->iopoll_completed))
1043 if (!rq_list_empty(iob.req_list))
1049 wq_list_for_each_resume(pos, prev) {
1050 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
1052 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
1053 if (!smp_load_acquire(&req->iopoll_completed))
1056 if (unlikely(req->flags & REQ_F_CQE_SKIP))
1059 req->cqe.flags = io_put_kbuf(req, 0);
1060 __io_fill_cqe_req(req->ctx, req);
1063 if (unlikely(!nr_events))
1066 io_commit_cqring(ctx);
1067 io_cqring_ev_posted_iopoll(ctx);
1068 pos = start ? start->next : ctx->iopoll_list.first;
1069 wq_list_cut(&ctx->iopoll_list, prev, start);
1070 io_free_batch_list(ctx, pos);