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();
108 rw->kiocb.dio_complete = NULL;
110 rw->addr = READ_ONCE(sqe->addr);
111 rw->len = READ_ONCE(sqe->len);
112 rw->flags = READ_ONCE(sqe->rw_flags);
114 /* Have to do this validation here, as this is in io_read() rw->len might
115 * have chanaged due to buffer selection
117 if (req->opcode == IORING_OP_READV && req->flags & REQ_F_BUFFER_SELECT) {
118 ret = io_iov_buffer_select_prep(req);
127 * Multishot read is prepared just like a normal read/write request, only
128 * difference is that we set the MULTISHOT flag.
130 int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
134 ret = io_prep_rw(req, sqe);
138 req->flags |= REQ_F_APOLL_MULTISHOT;
142 void io_readv_writev_cleanup(struct io_kiocb *req)
144 struct io_async_rw *io = req->async_data;
146 kfree(io->free_iovec);
149 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
155 case -ERESTARTNOINTR:
156 case -ERESTARTNOHAND:
157 case -ERESTART_RESTARTBLOCK:
159 * We can't just restart the syscall, since previously
160 * submitted sqes may already be in progress. Just fail this
166 kiocb->ki_complete(kiocb, ret);
170 static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req)
172 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
174 if (rw->kiocb.ki_pos != -1)
175 return &rw->kiocb.ki_pos;
177 if (!(req->file->f_mode & FMODE_STREAM)) {
178 req->flags |= REQ_F_CUR_POS;
179 rw->kiocb.ki_pos = req->file->f_pos;
180 return &rw->kiocb.ki_pos;
183 rw->kiocb.ki_pos = 0;
187 static void io_req_task_queue_reissue(struct io_kiocb *req)
189 req->io_task_work.func = io_queue_iowq;
190 io_req_task_work_add(req);
194 static bool io_resubmit_prep(struct io_kiocb *req)
196 struct io_async_rw *io = req->async_data;
198 if (!req_has_async_data(req))
199 return !io_req_prep_async(req);
200 iov_iter_restore(&io->s.iter, &io->s.iter_state);
204 static bool io_rw_should_reissue(struct io_kiocb *req)
206 umode_t mode = file_inode(req->file)->i_mode;
207 struct io_ring_ctx *ctx = req->ctx;
209 if (!S_ISBLK(mode) && !S_ISREG(mode))
211 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
212 !(ctx->flags & IORING_SETUP_IOPOLL)))
215 * If ref is dying, we might be running poll reap from the exit work.
216 * Don't attempt to reissue from that path, just let it fail with
219 if (percpu_ref_is_dying(&ctx->refs))
222 * Play it safe and assume not safe to re-import and reissue if we're
223 * not in the original thread group (or in task context).
225 if (!same_thread_group(req->task, current) || !in_task())
230 static bool io_resubmit_prep(struct io_kiocb *req)
234 static bool io_rw_should_reissue(struct io_kiocb *req)
240 static void io_req_end_write(struct io_kiocb *req)
242 if (req->flags & REQ_F_ISREG) {
243 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
245 kiocb_end_write(&rw->kiocb);
250 * Trigger the notifications after having done some IO, and finish the write
251 * accounting, if any.
253 static void io_req_io_end(struct io_kiocb *req)
255 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
257 if (rw->kiocb.ki_flags & IOCB_WRITE) {
258 io_req_end_write(req);
259 fsnotify_modify(req->file);
261 fsnotify_access(req->file);
265 static bool __io_complete_rw_common(struct io_kiocb *req, long res)
267 if (unlikely(res != req->cqe.res)) {
268 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
269 io_rw_should_reissue(req)) {
271 * Reissue will start accounting again, finish the
275 req->flags |= REQ_F_REISSUE | REQ_F_PARTIAL_IO;
284 static inline int io_fixup_rw_res(struct io_kiocb *req, long res)
286 struct io_async_rw *io = req->async_data;
288 /* add previously done IO, if any */
289 if (req_has_async_data(req) && io->bytes_done > 0) {
291 res = io->bytes_done;
293 res += io->bytes_done;
298 void io_req_rw_complete(struct io_kiocb *req, struct io_tw_state *ts)
300 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
301 struct kiocb *kiocb = &rw->kiocb;
303 if ((kiocb->ki_flags & IOCB_DIO_CALLER_COMP) && kiocb->dio_complete) {
304 long res = kiocb->dio_complete(rw->kiocb.private);
306 io_req_set_res(req, io_fixup_rw_res(req, res), 0);
311 if (req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)) {
312 unsigned issue_flags = ts->locked ? 0 : IO_URING_F_UNLOCKED;
314 req->cqe.flags |= io_put_kbuf(req, issue_flags);
316 io_req_task_complete(req, ts);
319 static void io_complete_rw(struct kiocb *kiocb, long res)
321 struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
322 struct io_kiocb *req = cmd_to_io_kiocb(rw);
324 if (!kiocb->dio_complete || !(kiocb->ki_flags & IOCB_DIO_CALLER_COMP)) {
325 if (__io_complete_rw_common(req, res))
327 io_req_set_res(req, io_fixup_rw_res(req, res), 0);
329 req->io_task_work.func = io_req_rw_complete;
330 __io_req_task_work_add(req, IOU_F_TWQ_LAZY_WAKE);
333 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
335 struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
336 struct io_kiocb *req = cmd_to_io_kiocb(rw);
338 if (kiocb->ki_flags & IOCB_WRITE)
339 io_req_end_write(req);
340 if (unlikely(res != req->cqe.res)) {
341 if (res == -EAGAIN && io_rw_should_reissue(req)) {
342 req->flags |= REQ_F_REISSUE | REQ_F_PARTIAL_IO;
348 /* order with io_iopoll_complete() checking ->iopoll_completed */
349 smp_store_release(&req->iopoll_completed, 1);
352 static int kiocb_done(struct io_kiocb *req, ssize_t ret,
353 unsigned int issue_flags)
355 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
356 unsigned final_ret = io_fixup_rw_res(req, ret);
358 if (ret >= 0 && req->flags & REQ_F_CUR_POS)
359 req->file->f_pos = rw->kiocb.ki_pos;
360 if (ret >= 0 && (rw->kiocb.ki_complete == io_complete_rw)) {
361 if (!__io_complete_rw_common(req, ret)) {
363 * Safe to call io_end from here as we're inline
364 * from the submission path.
367 io_req_set_res(req, final_ret,
368 io_put_kbuf(req, issue_flags));
372 io_rw_done(&rw->kiocb, ret);
375 if (req->flags & REQ_F_REISSUE) {
376 req->flags &= ~REQ_F_REISSUE;
377 if (io_resubmit_prep(req))
378 io_req_task_queue_reissue(req);
380 io_req_task_queue_fail(req, final_ret);
382 return IOU_ISSUE_SKIP_COMPLETE;
385 static struct iovec *__io_import_iovec(int ddir, struct io_kiocb *req,
386 struct io_rw_state *s,
387 unsigned int issue_flags)
389 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
390 struct iov_iter *iter = &s->iter;
391 u8 opcode = req->opcode;
397 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
398 ret = io_import_fixed(ddir, iter, req->imu, rw->addr, rw->len);
404 buf = u64_to_user_ptr(rw->addr);
407 if (!io_issue_defs[opcode].vectored || req->flags & REQ_F_BUFFER_SELECT) {
408 if (io_do_buffer_select(req)) {
409 buf = io_buffer_select(req, &sqe_len, issue_flags);
411 return ERR_PTR(-ENOBUFS);
412 rw->addr = (unsigned long) buf;
416 ret = import_ubuf(ddir, buf, sqe_len, iter);
423 ret = __import_iovec(ddir, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
425 if (unlikely(ret < 0))
430 static inline int io_import_iovec(int rw, struct io_kiocb *req,
431 struct iovec **iovec, struct io_rw_state *s,
432 unsigned int issue_flags)
434 *iovec = __io_import_iovec(rw, req, s, issue_flags);
436 return PTR_ERR(*iovec);
438 iov_iter_save_state(&s->iter, &s->iter_state);
442 static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
444 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
448 * For files that don't have ->read_iter() and ->write_iter(), handle them
449 * by looping over ->read() or ->write() manually.
451 static ssize_t loop_rw_iter(int ddir, struct io_rw *rw, struct iov_iter *iter)
453 struct kiocb *kiocb = &rw->kiocb;
454 struct file *file = kiocb->ki_filp;
459 * Don't support polled IO through this interface, and we can't
460 * support non-blocking either. For the latter, this just causes
461 * the kiocb to be handled from an async context.
463 if (kiocb->ki_flags & IOCB_HIPRI)
465 if ((kiocb->ki_flags & IOCB_NOWAIT) &&
466 !(kiocb->ki_filp->f_flags & O_NONBLOCK))
469 ppos = io_kiocb_ppos(kiocb);
471 while (iov_iter_count(iter)) {
476 if (iter_is_ubuf(iter)) {
477 addr = iter->ubuf + iter->iov_offset;
478 len = iov_iter_count(iter);
479 } else if (!iov_iter_is_bvec(iter)) {
480 addr = iter_iov_addr(iter);
481 len = iter_iov_len(iter);
483 addr = u64_to_user_ptr(rw->addr);
488 nr = file->f_op->read(file, addr, len, ppos);
490 nr = file->f_op->write(file, addr, len, ppos);
498 if (!iov_iter_is_bvec(iter)) {
499 iov_iter_advance(iter, nr);
513 static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
514 const struct iovec *fast_iov, struct iov_iter *iter)
516 struct io_async_rw *io = req->async_data;
518 memcpy(&io->s.iter, iter, sizeof(*iter));
519 io->free_iovec = iovec;
521 /* can only be fixed buffers, no need to do anything */
522 if (iov_iter_is_bvec(iter) || iter_is_ubuf(iter))
525 unsigned iov_off = 0;
527 io->s.iter.__iov = io->s.fast_iov;
528 if (iter->__iov != fast_iov) {
529 iov_off = iter_iov(iter) - fast_iov;
530 io->s.iter.__iov += iov_off;
532 if (io->s.fast_iov != fast_iov)
533 memcpy(io->s.fast_iov + iov_off, fast_iov + iov_off,
534 sizeof(struct iovec) * iter->nr_segs);
536 req->flags |= REQ_F_NEED_CLEANUP;
540 static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
541 struct io_rw_state *s, bool force)
543 if (!force && !io_cold_defs[req->opcode].prep_async)
545 if (!req_has_async_data(req)) {
546 struct io_async_rw *iorw;
548 if (io_alloc_async_data(req)) {
553 io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
554 iorw = req->async_data;
555 /* we've copied and mapped the iter, ensure state is saved */
556 iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
561 static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
563 struct io_async_rw *iorw = req->async_data;
567 /* submission path, ->uring_lock should already be taken */
568 ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
569 if (unlikely(ret < 0))
572 iorw->bytes_done = 0;
573 iorw->free_iovec = iov;
575 req->flags |= REQ_F_NEED_CLEANUP;
579 int io_readv_prep_async(struct io_kiocb *req)
581 return io_rw_prep_async(req, ITER_DEST);
584 int io_writev_prep_async(struct io_kiocb *req)
586 return io_rw_prep_async(req, ITER_SOURCE);
590 * This is our waitqueue callback handler, registered through __folio_lock_async()
591 * when we initially tried to do the IO with the iocb armed our waitqueue.
592 * This gets called when the page is unlocked, and we generally expect that to
593 * happen when the page IO is completed and the page is now uptodate. This will
594 * queue a task_work based retry of the operation, attempting to copy the data
595 * again. If the latter fails because the page was NOT uptodate, then we will
596 * do a thread based blocking retry of the operation. That's the unexpected
599 static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
602 struct wait_page_queue *wpq;
603 struct io_kiocb *req = wait->private;
604 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
605 struct wait_page_key *key = arg;
607 wpq = container_of(wait, struct wait_page_queue, wait);
609 if (!wake_page_match(wpq, key))
612 rw->kiocb.ki_flags &= ~IOCB_WAITQ;
613 list_del_init(&wait->entry);
614 io_req_task_queue(req);
619 * This controls whether a given IO request should be armed for async page
620 * based retry. If we return false here, the request is handed to the async
621 * worker threads for retry. If we're doing buffered reads on a regular file,
622 * we prepare a private wait_page_queue entry and retry the operation. This
623 * will either succeed because the page is now uptodate and unlocked, or it
624 * will register a callback when the page is unlocked at IO completion. Through
625 * that callback, io_uring uses task_work to setup a retry of the operation.
626 * That retry will attempt the buffered read again. The retry will generally
627 * succeed, or in rare cases where it fails, we then fall back to using the
628 * async worker threads for a blocking retry.
630 static bool io_rw_should_retry(struct io_kiocb *req)
632 struct io_async_rw *io = req->async_data;
633 struct wait_page_queue *wait = &io->wpq;
634 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
635 struct kiocb *kiocb = &rw->kiocb;
637 /* never retry for NOWAIT, we just complete with -EAGAIN */
638 if (req->flags & REQ_F_NOWAIT)
641 /* Only for buffered IO */
642 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
646 * just use poll if we can, and don't attempt if the fs doesn't
647 * support callback based unlocks
649 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
652 wait->wait.func = io_async_buf_func;
653 wait->wait.private = req;
654 wait->wait.flags = 0;
655 INIT_LIST_HEAD(&wait->wait.entry);
656 kiocb->ki_flags |= IOCB_WAITQ;
657 kiocb->ki_flags &= ~IOCB_NOWAIT;
658 kiocb->ki_waitq = wait;
662 static inline int io_iter_do_read(struct io_rw *rw, struct iov_iter *iter)
664 struct file *file = rw->kiocb.ki_filp;
666 if (likely(file->f_op->read_iter))
667 return call_read_iter(file, &rw->kiocb, iter);
668 else if (file->f_op->read)
669 return loop_rw_iter(READ, rw, iter);
674 static bool need_complete_io(struct io_kiocb *req)
676 return req->flags & REQ_F_ISREG ||
677 S_ISBLK(file_inode(req->file)->i_mode);
680 static int io_rw_init_file(struct io_kiocb *req, fmode_t mode)
682 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
683 struct kiocb *kiocb = &rw->kiocb;
684 struct io_ring_ctx *ctx = req->ctx;
685 struct file *file = req->file;
688 if (unlikely(!file || !(file->f_mode & mode)))
691 if (!(req->flags & REQ_F_FIXED_FILE))
692 req->flags |= io_file_get_flags(file);
694 kiocb->ki_flags = file->f_iocb_flags;
695 ret = kiocb_set_rw_flags(kiocb, rw->flags);
698 kiocb->ki_flags |= IOCB_ALLOC_CACHE;
701 * If the file is marked O_NONBLOCK, still allow retry for it if it
702 * supports async. Otherwise it's impossible to use O_NONBLOCK files
703 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
705 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
706 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
707 req->flags |= REQ_F_NOWAIT;
709 if (ctx->flags & IORING_SETUP_IOPOLL) {
710 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
713 kiocb->private = NULL;
714 kiocb->ki_flags |= IOCB_HIPRI;
715 kiocb->ki_complete = io_complete_rw_iopoll;
716 req->iopoll_completed = 0;
718 if (kiocb->ki_flags & IOCB_HIPRI)
720 kiocb->ki_complete = io_complete_rw;
726 static int __io_read(struct io_kiocb *req, unsigned int issue_flags)
728 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
729 struct io_rw_state __s, *s = &__s;
731 struct kiocb *kiocb = &rw->kiocb;
732 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
733 struct io_async_rw *io;
737 if (!req_has_async_data(req)) {
738 ret = io_import_iovec(ITER_DEST, req, &iovec, s, issue_flags);
739 if (unlikely(ret < 0))
742 io = req->async_data;
746 * Safe and required to re-import if we're using provided
747 * buffers, as we dropped the selected one before retry.
749 if (io_do_buffer_select(req)) {
750 ret = io_import_iovec(ITER_DEST, req, &iovec, s, issue_flags);
751 if (unlikely(ret < 0))
756 * We come here from an earlier attempt, restore our state to
757 * match in case it doesn't. It's cheap enough that we don't
758 * need to make this conditional.
760 iov_iter_restore(&s->iter, &s->iter_state);
763 ret = io_rw_init_file(req, FMODE_READ);
768 req->cqe.res = iov_iter_count(&s->iter);
770 if (force_nonblock) {
771 /* If the file doesn't support async, just async punt */
772 if (unlikely(!io_file_supports_nowait(req))) {
773 ret = io_setup_async_rw(req, iovec, s, true);
774 return ret ?: -EAGAIN;
776 kiocb->ki_flags |= IOCB_NOWAIT;
778 /* Ensure we clear previously set non-block flag */
779 kiocb->ki_flags &= ~IOCB_NOWAIT;
782 ppos = io_kiocb_update_pos(req);
784 ret = rw_verify_area(READ, req->file, ppos, req->cqe.res);
790 ret = io_iter_do_read(rw, &s->iter);
792 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
793 req->flags &= ~REQ_F_REISSUE;
795 * If we can poll, just do that. For a vectored read, we'll
796 * need to copy state first.
798 if (file_can_poll(req->file) && !io_issue_defs[req->opcode].vectored)
800 /* IOPOLL retry should happen for io-wq threads */
801 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
803 /* no retry on NONBLOCK nor RWF_NOWAIT */
804 if (req->flags & REQ_F_NOWAIT)
807 } else if (ret == -EIOCBQUEUED) {
810 return IOU_ISSUE_SKIP_COMPLETE;
811 } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock ||
812 (req->flags & REQ_F_NOWAIT) || !need_complete_io(req)) {
813 /* read all, failed, already did sync or don't want to retry */
818 * Don't depend on the iter state matching what was consumed, or being
819 * untouched in case of error. Restore it and we'll advance it
820 * manually if we need to.
822 iov_iter_restore(&s->iter, &s->iter_state);
824 ret2 = io_setup_async_rw(req, iovec, s, true);
827 ret = ret > 0 ? ret : ret2;
831 io = req->async_data;
834 * Now use our persistent iterator and state, if we aren't already.
835 * We've restored and mapped the iter to match.
840 * We end up here because of a partial read, either from
841 * above or inside this loop. Advance the iter by the bytes
842 * that were consumed.
844 iov_iter_advance(&s->iter, ret);
845 if (!iov_iter_count(&s->iter))
847 io->bytes_done += ret;
848 iov_iter_save_state(&s->iter, &s->iter_state);
850 /* if we can retry, do so with the callbacks armed */
851 if (!io_rw_should_retry(req)) {
852 kiocb->ki_flags &= ~IOCB_WAITQ;
856 req->cqe.res = iov_iter_count(&s->iter);
858 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
859 * we get -EIOCBQUEUED, then we'll get a notification when the
860 * desired page gets unlocked. We can also get a partial read
861 * here, and if we do, then just retry at the new offset.
863 ret = io_iter_do_read(rw, &s->iter);
864 if (ret == -EIOCBQUEUED)
865 return IOU_ISSUE_SKIP_COMPLETE;
866 /* we got some bytes, but not all. retry. */
867 kiocb->ki_flags &= ~IOCB_WAITQ;
868 iov_iter_restore(&s->iter, &s->iter_state);
871 /* it's faster to check here then delegate to kfree */
877 int io_read(struct io_kiocb *req, unsigned int issue_flags)
881 ret = __io_read(req, issue_flags);
883 return kiocb_done(req, ret, issue_flags);
888 int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)
890 unsigned int cflags = 0;
894 * Multishot MUST be used on a pollable file
896 if (!file_can_poll(req->file))
899 ret = __io_read(req, issue_flags);
902 * If we get -EAGAIN, recycle our buffer and just let normal poll
905 if (ret == -EAGAIN) {
906 io_kbuf_recycle(req, issue_flags);
911 * Any successful return value will keep the multishot read armed.
915 * Put our buffer and post a CQE. If we fail to post a CQE, then
916 * jump to the termination path. This request is then done.
918 cflags = io_put_kbuf(req, issue_flags);
920 if (io_fill_cqe_req_aux(req,
921 issue_flags & IO_URING_F_COMPLETE_DEFER,
922 ret, cflags | IORING_CQE_F_MORE)) {
923 if (issue_flags & IO_URING_F_MULTISHOT)
924 return IOU_ISSUE_SKIP_COMPLETE;
930 * Either an error, or we've hit overflow posting the CQE. For any
931 * multishot request, hitting overflow will terminate it.
933 io_req_set_res(req, ret, cflags);
934 if (issue_flags & IO_URING_F_MULTISHOT)
935 return IOU_STOP_MULTISHOT;
939 int io_write(struct io_kiocb *req, unsigned int issue_flags)
941 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
942 struct io_rw_state __s, *s = &__s;
944 struct kiocb *kiocb = &rw->kiocb;
945 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
949 if (!req_has_async_data(req)) {
950 ret = io_import_iovec(ITER_SOURCE, req, &iovec, s, issue_flags);
951 if (unlikely(ret < 0))
954 struct io_async_rw *io = req->async_data;
957 iov_iter_restore(&s->iter, &s->iter_state);
960 ret = io_rw_init_file(req, FMODE_WRITE);
965 req->cqe.res = iov_iter_count(&s->iter);
967 if (force_nonblock) {
968 /* If the file doesn't support async, just async punt */
969 if (unlikely(!io_file_supports_nowait(req)))
972 /* File path supports NOWAIT for non-direct_IO only for block devices. */
973 if (!(kiocb->ki_flags & IOCB_DIRECT) &&
974 !(kiocb->ki_filp->f_mode & FMODE_BUF_WASYNC) &&
975 (req->flags & REQ_F_ISREG))
978 kiocb->ki_flags |= IOCB_NOWAIT;
980 /* Ensure we clear previously set non-block flag */
981 kiocb->ki_flags &= ~IOCB_NOWAIT;
984 ppos = io_kiocb_update_pos(req);
986 ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res);
992 if (req->flags & REQ_F_ISREG)
993 kiocb_start_write(kiocb);
994 kiocb->ki_flags |= IOCB_WRITE;
996 if (likely(req->file->f_op->write_iter))
997 ret2 = call_write_iter(req->file, kiocb, &s->iter);
998 else if (req->file->f_op->write)
999 ret2 = loop_rw_iter(WRITE, rw, &s->iter);
1003 if (req->flags & REQ_F_REISSUE) {
1004 req->flags &= ~REQ_F_REISSUE;
1009 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
1010 * retry them without IOCB_NOWAIT.
1012 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
1014 /* no retry on NONBLOCK nor RWF_NOWAIT */
1015 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
1017 if (!force_nonblock || ret2 != -EAGAIN) {
1018 /* IOPOLL retry should happen for io-wq threads */
1019 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
1022 if (ret2 != req->cqe.res && ret2 >= 0 && need_complete_io(req)) {
1023 struct io_async_rw *io;
1025 trace_io_uring_short_write(req->ctx, kiocb->ki_pos - ret2,
1026 req->cqe.res, ret2);
1028 /* This is a partial write. The file pos has already been
1029 * updated, setup the async struct to complete the request
1030 * in the worker. Also update bytes_done to account for
1031 * the bytes already written.
1033 iov_iter_save_state(&s->iter, &s->iter_state);
1034 ret = io_setup_async_rw(req, iovec, s, true);
1036 io = req->async_data;
1038 io->bytes_done += ret2;
1040 if (kiocb->ki_flags & IOCB_WRITE)
1041 io_req_end_write(req);
1042 return ret ? ret : -EAGAIN;
1045 ret = kiocb_done(req, ret2, issue_flags);
1048 iov_iter_restore(&s->iter, &s->iter_state);
1049 ret = io_setup_async_rw(req, iovec, s, false);
1051 if (kiocb->ki_flags & IOCB_WRITE)
1052 io_req_end_write(req);
1057 /* it's reportedly faster than delegating the null check to kfree() */
1063 void io_rw_fail(struct io_kiocb *req)
1067 res = io_fixup_rw_res(req, req->cqe.res);
1068 io_req_set_res(req, res, req->cqe.flags);
1071 int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
1073 struct io_wq_work_node *pos, *start, *prev;
1074 unsigned int poll_flags = 0;
1075 DEFINE_IO_COMP_BATCH(iob);
1079 * Only spin for completions if we don't have multiple devices hanging
1080 * off our complete list.
1082 if (ctx->poll_multi_queue || force_nonspin)
1083 poll_flags |= BLK_POLL_ONESHOT;
1085 wq_list_for_each(pos, start, &ctx->iopoll_list) {
1086 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
1087 struct file *file = req->file;
1091 * Move completed and retryable entries to our local lists.
1092 * If we find a request that requires polling, break out
1093 * and complete those lists first, if we have entries there.
1095 if (READ_ONCE(req->iopoll_completed))
1098 if (req->opcode == IORING_OP_URING_CMD) {
1099 struct io_uring_cmd *ioucmd;
1101 ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
1102 ret = file->f_op->uring_cmd_iopoll(ioucmd, &iob,
1105 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
1107 ret = file->f_op->iopoll(&rw->kiocb, &iob, poll_flags);
1109 if (unlikely(ret < 0))
1112 poll_flags |= BLK_POLL_ONESHOT;
1114 /* iopoll may have completed current req */
1115 if (!rq_list_empty(iob.req_list) ||
1116 READ_ONCE(req->iopoll_completed))
1120 if (!rq_list_empty(iob.req_list))
1126 wq_list_for_each_resume(pos, prev) {
1127 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
1129 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
1130 if (!smp_load_acquire(&req->iopoll_completed))
1133 req->cqe.flags = io_put_kbuf(req, 0);
1135 if (unlikely(!nr_events))
1138 pos = start ? start->next : ctx->iopoll_list.first;
1139 wq_list_cut(&ctx->iopoll_list, prev, start);
1141 if (WARN_ON_ONCE(!wq_list_empty(&ctx->submit_state.compl_reqs)))
1143 ctx->submit_state.compl_reqs.first = pos;
1144 __io_submit_flush_completions(ctx);