18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * fs/direct-io.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2002, Linus Torvalds.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * O_DIRECT
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * 04Jul2002	Andrew Morton
108c2ecf20Sopenharmony_ci *		Initial version
118c2ecf20Sopenharmony_ci * 11Sep2002	janetinc@us.ibm.com
128c2ecf20Sopenharmony_ci * 		added readv/writev support.
138c2ecf20Sopenharmony_ci * 29Oct2002	Andrew Morton
148c2ecf20Sopenharmony_ci *		rewrote bio_add_page() support.
158c2ecf20Sopenharmony_ci * 30Oct2002	pbadari@us.ibm.com
168c2ecf20Sopenharmony_ci *		added support for non-aligned IO.
178c2ecf20Sopenharmony_ci * 06Nov2002	pbadari@us.ibm.com
188c2ecf20Sopenharmony_ci *		added asynchronous IO support.
198c2ecf20Sopenharmony_ci * 21Jul2003	nathans@sgi.com
208c2ecf20Sopenharmony_ci *		added IO completion notifier.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/kernel.h>
248c2ecf20Sopenharmony_ci#include <linux/module.h>
258c2ecf20Sopenharmony_ci#include <linux/types.h>
268c2ecf20Sopenharmony_ci#include <linux/fs.h>
278c2ecf20Sopenharmony_ci#include <linux/mm.h>
288c2ecf20Sopenharmony_ci#include <linux/slab.h>
298c2ecf20Sopenharmony_ci#include <linux/highmem.h>
308c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
318c2ecf20Sopenharmony_ci#include <linux/task_io_accounting_ops.h>
328c2ecf20Sopenharmony_ci#include <linux/bio.h>
338c2ecf20Sopenharmony_ci#include <linux/wait.h>
348c2ecf20Sopenharmony_ci#include <linux/err.h>
358c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
368c2ecf20Sopenharmony_ci#include <linux/buffer_head.h>
378c2ecf20Sopenharmony_ci#include <linux/rwsem.h>
388c2ecf20Sopenharmony_ci#include <linux/uio.h>
398c2ecf20Sopenharmony_ci#include <linux/atomic.h>
408c2ecf20Sopenharmony_ci#include <linux/prefetch.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#include "internal.h"
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/*
458c2ecf20Sopenharmony_ci * How many user pages to map in one call to get_user_pages().  This determines
468c2ecf20Sopenharmony_ci * the size of a structure in the slab cache
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci#define DIO_PAGES	64
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/*
518c2ecf20Sopenharmony_ci * Flags for dio_complete()
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ci#define DIO_COMPLETE_ASYNC		0x01	/* This is async IO */
548c2ecf20Sopenharmony_ci#define DIO_COMPLETE_INVALIDATE		0x02	/* Can invalidate pages */
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/*
578c2ecf20Sopenharmony_ci * This code generally works in units of "dio_blocks".  A dio_block is
588c2ecf20Sopenharmony_ci * somewhere between the hard sector size and the filesystem block size.  it
598c2ecf20Sopenharmony_ci * is determined on a per-invocation basis.   When talking to the filesystem
608c2ecf20Sopenharmony_ci * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
618c2ecf20Sopenharmony_ci * down by dio->blkfactor.  Similarly, fs-blocksize quantities are converted
628c2ecf20Sopenharmony_ci * to bio_block quantities by shifting left by blkfactor.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * If blkfactor is zero then the user's request was aligned to the filesystem's
658c2ecf20Sopenharmony_ci * blocksize.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* dio_state only used in the submission path */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct dio_submit {
718c2ecf20Sopenharmony_ci	struct bio *bio;		/* bio under assembly */
728c2ecf20Sopenharmony_ci	unsigned blkbits;		/* doesn't change */
738c2ecf20Sopenharmony_ci	unsigned blkfactor;		/* When we're using an alignment which
748c2ecf20Sopenharmony_ci					   is finer than the filesystem's soft
758c2ecf20Sopenharmony_ci					   blocksize, this specifies how much
768c2ecf20Sopenharmony_ci					   finer.  blkfactor=2 means 1/4-block
778c2ecf20Sopenharmony_ci					   alignment.  Does not change */
788c2ecf20Sopenharmony_ci	unsigned start_zero_done;	/* flag: sub-blocksize zeroing has
798c2ecf20Sopenharmony_ci					   been performed at the start of a
808c2ecf20Sopenharmony_ci					   write */
818c2ecf20Sopenharmony_ci	int pages_in_io;		/* approximate total IO pages */
828c2ecf20Sopenharmony_ci	sector_t block_in_file;		/* Current offset into the underlying
838c2ecf20Sopenharmony_ci					   file in dio_block units. */
848c2ecf20Sopenharmony_ci	unsigned blocks_available;	/* At block_in_file.  changes */
858c2ecf20Sopenharmony_ci	int reap_counter;		/* rate limit reaping */
868c2ecf20Sopenharmony_ci	sector_t final_block_in_request;/* doesn't change */
878c2ecf20Sopenharmony_ci	int boundary;			/* prev block is at a boundary */
888c2ecf20Sopenharmony_ci	get_block_t *get_block;		/* block mapping function */
898c2ecf20Sopenharmony_ci	dio_submit_t *submit_io;	/* IO submition function */
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	loff_t logical_offset_in_bio;	/* current first logical block in bio */
928c2ecf20Sopenharmony_ci	sector_t final_block_in_bio;	/* current final block in bio + 1 */
938c2ecf20Sopenharmony_ci	sector_t next_block_for_io;	/* next block to be put under IO,
948c2ecf20Sopenharmony_ci					   in dio_blocks units */
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * Deferred addition of a page to the dio.  These variables are
988c2ecf20Sopenharmony_ci	 * private to dio_send_cur_page(), submit_page_section() and
998c2ecf20Sopenharmony_ci	 * dio_bio_add_page().
1008c2ecf20Sopenharmony_ci	 */
1018c2ecf20Sopenharmony_ci	struct page *cur_page;		/* The page */
1028c2ecf20Sopenharmony_ci	unsigned cur_page_offset;	/* Offset into it, in bytes */
1038c2ecf20Sopenharmony_ci	unsigned cur_page_len;		/* Nr of bytes at cur_page_offset */
1048c2ecf20Sopenharmony_ci	sector_t cur_page_block;	/* Where it starts */
1058c2ecf20Sopenharmony_ci	loff_t cur_page_fs_offset;	/* Offset in file */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	struct iov_iter *iter;
1088c2ecf20Sopenharmony_ci	/*
1098c2ecf20Sopenharmony_ci	 * Page queue.  These variables belong to dio_refill_pages() and
1108c2ecf20Sopenharmony_ci	 * dio_get_page().
1118c2ecf20Sopenharmony_ci	 */
1128c2ecf20Sopenharmony_ci	unsigned head;			/* next page to process */
1138c2ecf20Sopenharmony_ci	unsigned tail;			/* last valid page + 1 */
1148c2ecf20Sopenharmony_ci	size_t from, to;
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* dio_state communicated between submission path and end_io */
1188c2ecf20Sopenharmony_cistruct dio {
1198c2ecf20Sopenharmony_ci	int flags;			/* doesn't change */
1208c2ecf20Sopenharmony_ci	int op;
1218c2ecf20Sopenharmony_ci	int op_flags;
1228c2ecf20Sopenharmony_ci	blk_qc_t bio_cookie;
1238c2ecf20Sopenharmony_ci	struct gendisk *bio_disk;
1248c2ecf20Sopenharmony_ci	struct inode *inode;
1258c2ecf20Sopenharmony_ci	loff_t i_size;			/* i_size when submitted */
1268c2ecf20Sopenharmony_ci	dio_iodone_t *end_io;		/* IO completion function */
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	void *private;			/* copy from map_bh.b_private */
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/* BIO completion state */
1318c2ecf20Sopenharmony_ci	spinlock_t bio_lock;		/* protects BIO fields below */
1328c2ecf20Sopenharmony_ci	int page_errors;		/* errno from get_user_pages() */
1338c2ecf20Sopenharmony_ci	int is_async;			/* is IO async ? */
1348c2ecf20Sopenharmony_ci	bool defer_completion;		/* defer AIO completion to workqueue? */
1358c2ecf20Sopenharmony_ci	bool should_dirty;		/* if pages should be dirtied */
1368c2ecf20Sopenharmony_ci	int io_error;			/* IO error in completion path */
1378c2ecf20Sopenharmony_ci	unsigned long refcount;		/* direct_io_worker() and bios */
1388c2ecf20Sopenharmony_ci	struct bio *bio_list;		/* singly linked via bi_private */
1398c2ecf20Sopenharmony_ci	struct task_struct *waiter;	/* waiting task (NULL if none) */
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/* AIO related stuff */
1428c2ecf20Sopenharmony_ci	struct kiocb *iocb;		/* kiocb */
1438c2ecf20Sopenharmony_ci	ssize_t result;                 /* IO result */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/*
1468c2ecf20Sopenharmony_ci	 * pages[] (and any fields placed after it) are not zeroed out at
1478c2ecf20Sopenharmony_ci	 * allocation time.  Don't add new fields after pages[] unless you
1488c2ecf20Sopenharmony_ci	 * wish that they not be zeroed.
1498c2ecf20Sopenharmony_ci	 */
1508c2ecf20Sopenharmony_ci	union {
1518c2ecf20Sopenharmony_ci		struct page *pages[DIO_PAGES];	/* page buffer */
1528c2ecf20Sopenharmony_ci		struct work_struct complete_work;/* deferred AIO completion */
1538c2ecf20Sopenharmony_ci	};
1548c2ecf20Sopenharmony_ci} ____cacheline_aligned_in_smp;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic struct kmem_cache *dio_cache __read_mostly;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/*
1598c2ecf20Sopenharmony_ci * How many pages are in the queue?
1608c2ecf20Sopenharmony_ci */
1618c2ecf20Sopenharmony_cistatic inline unsigned dio_pages_present(struct dio_submit *sdio)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	return sdio->tail - sdio->head;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/*
1678c2ecf20Sopenharmony_ci * Go grab and pin some userspace pages.   Typically we'll get 64 at a time.
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_cistatic inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	ssize_t ret;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	ret = iov_iter_get_pages(sdio->iter, dio->pages, LONG_MAX, DIO_PAGES,
1748c2ecf20Sopenharmony_ci				&sdio->from);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (ret < 0 && sdio->blocks_available && (dio->op == REQ_OP_WRITE)) {
1778c2ecf20Sopenharmony_ci		struct page *page = ZERO_PAGE(0);
1788c2ecf20Sopenharmony_ci		/*
1798c2ecf20Sopenharmony_ci		 * A memory fault, but the filesystem has some outstanding
1808c2ecf20Sopenharmony_ci		 * mapped blocks.  We need to use those blocks up to avoid
1818c2ecf20Sopenharmony_ci		 * leaking stale data in the file.
1828c2ecf20Sopenharmony_ci		 */
1838c2ecf20Sopenharmony_ci		if (dio->page_errors == 0)
1848c2ecf20Sopenharmony_ci			dio->page_errors = ret;
1858c2ecf20Sopenharmony_ci		get_page(page);
1868c2ecf20Sopenharmony_ci		dio->pages[0] = page;
1878c2ecf20Sopenharmony_ci		sdio->head = 0;
1888c2ecf20Sopenharmony_ci		sdio->tail = 1;
1898c2ecf20Sopenharmony_ci		sdio->from = 0;
1908c2ecf20Sopenharmony_ci		sdio->to = PAGE_SIZE;
1918c2ecf20Sopenharmony_ci		return 0;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	if (ret >= 0) {
1958c2ecf20Sopenharmony_ci		iov_iter_advance(sdio->iter, ret);
1968c2ecf20Sopenharmony_ci		ret += sdio->from;
1978c2ecf20Sopenharmony_ci		sdio->head = 0;
1988c2ecf20Sopenharmony_ci		sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1998c2ecf20Sopenharmony_ci		sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1;
2008c2ecf20Sopenharmony_ci		return 0;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci	return ret;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/*
2068c2ecf20Sopenharmony_ci * Get another userspace page.  Returns an ERR_PTR on error.  Pages are
2078c2ecf20Sopenharmony_ci * buffered inside the dio so that we can call get_user_pages() against a
2088c2ecf20Sopenharmony_ci * decent number of pages, less frequently.  To provide nicer use of the
2098c2ecf20Sopenharmony_ci * L1 cache.
2108c2ecf20Sopenharmony_ci */
2118c2ecf20Sopenharmony_cistatic inline struct page *dio_get_page(struct dio *dio,
2128c2ecf20Sopenharmony_ci					struct dio_submit *sdio)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	if (dio_pages_present(sdio) == 0) {
2158c2ecf20Sopenharmony_ci		int ret;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci		ret = dio_refill_pages(dio, sdio);
2188c2ecf20Sopenharmony_ci		if (ret)
2198c2ecf20Sopenharmony_ci			return ERR_PTR(ret);
2208c2ecf20Sopenharmony_ci		BUG_ON(dio_pages_present(sdio) == 0);
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci	return dio->pages[sdio->head];
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci/*
2268c2ecf20Sopenharmony_ci * dio_complete() - called when all DIO BIO I/O has been completed
2278c2ecf20Sopenharmony_ci *
2288c2ecf20Sopenharmony_ci * This drops i_dio_count, lets interested parties know that a DIO operation
2298c2ecf20Sopenharmony_ci * has completed, and calculates the resulting return code for the operation.
2308c2ecf20Sopenharmony_ci *
2318c2ecf20Sopenharmony_ci * It lets the filesystem know if it registered an interest earlier via
2328c2ecf20Sopenharmony_ci * get_block.  Pass the private field of the map buffer_head so that
2338c2ecf20Sopenharmony_ci * filesystems can use it to hold additional state between get_block calls and
2348c2ecf20Sopenharmony_ci * dio_complete.
2358c2ecf20Sopenharmony_ci */
2368c2ecf20Sopenharmony_cistatic ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	loff_t offset = dio->iocb->ki_pos;
2398c2ecf20Sopenharmony_ci	ssize_t transferred = 0;
2408c2ecf20Sopenharmony_ci	int err;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/*
2438c2ecf20Sopenharmony_ci	 * AIO submission can race with bio completion to get here while
2448c2ecf20Sopenharmony_ci	 * expecting to have the last io completed by bio completion.
2458c2ecf20Sopenharmony_ci	 * In that case -EIOCBQUEUED is in fact not an error we want
2468c2ecf20Sopenharmony_ci	 * to preserve through this call.
2478c2ecf20Sopenharmony_ci	 */
2488c2ecf20Sopenharmony_ci	if (ret == -EIOCBQUEUED)
2498c2ecf20Sopenharmony_ci		ret = 0;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	if (dio->result) {
2528c2ecf20Sopenharmony_ci		transferred = dio->result;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		/* Check for short read case */
2558c2ecf20Sopenharmony_ci		if ((dio->op == REQ_OP_READ) &&
2568c2ecf20Sopenharmony_ci		    ((offset + transferred) > dio->i_size))
2578c2ecf20Sopenharmony_ci			transferred = dio->i_size - offset;
2588c2ecf20Sopenharmony_ci		/* ignore EFAULT if some IO has been done */
2598c2ecf20Sopenharmony_ci		if (unlikely(ret == -EFAULT) && transferred)
2608c2ecf20Sopenharmony_ci			ret = 0;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	if (ret == 0)
2648c2ecf20Sopenharmony_ci		ret = dio->page_errors;
2658c2ecf20Sopenharmony_ci	if (ret == 0)
2668c2ecf20Sopenharmony_ci		ret = dio->io_error;
2678c2ecf20Sopenharmony_ci	if (ret == 0)
2688c2ecf20Sopenharmony_ci		ret = transferred;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	if (dio->end_io) {
2718c2ecf20Sopenharmony_ci		// XXX: ki_pos??
2728c2ecf20Sopenharmony_ci		err = dio->end_io(dio->iocb, offset, ret, dio->private);
2738c2ecf20Sopenharmony_ci		if (err)
2748c2ecf20Sopenharmony_ci			ret = err;
2758c2ecf20Sopenharmony_ci	}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/*
2788c2ecf20Sopenharmony_ci	 * Try again to invalidate clean pages which might have been cached by
2798c2ecf20Sopenharmony_ci	 * non-direct readahead, or faulted in by get_user_pages() if the source
2808c2ecf20Sopenharmony_ci	 * of the write was an mmap'ed region of the file we're writing.  Either
2818c2ecf20Sopenharmony_ci	 * one is a pretty crazy thing to do, so we don't support it 100%.  If
2828c2ecf20Sopenharmony_ci	 * this invalidation fails, tough, the write still worked...
2838c2ecf20Sopenharmony_ci	 *
2848c2ecf20Sopenharmony_ci	 * And this page cache invalidation has to be after dio->end_io(), as
2858c2ecf20Sopenharmony_ci	 * some filesystems convert unwritten extents to real allocations in
2868c2ecf20Sopenharmony_ci	 * end_io() when necessary, otherwise a racing buffer read would cache
2878c2ecf20Sopenharmony_ci	 * zeros from unwritten extents.
2888c2ecf20Sopenharmony_ci	 */
2898c2ecf20Sopenharmony_ci	if (flags & DIO_COMPLETE_INVALIDATE &&
2908c2ecf20Sopenharmony_ci	    ret > 0 && dio->op == REQ_OP_WRITE &&
2918c2ecf20Sopenharmony_ci	    dio->inode->i_mapping->nrpages) {
2928c2ecf20Sopenharmony_ci		err = invalidate_inode_pages2_range(dio->inode->i_mapping,
2938c2ecf20Sopenharmony_ci					offset >> PAGE_SHIFT,
2948c2ecf20Sopenharmony_ci					(offset + ret - 1) >> PAGE_SHIFT);
2958c2ecf20Sopenharmony_ci		if (err)
2968c2ecf20Sopenharmony_ci			dio_warn_stale_pagecache(dio->iocb->ki_filp);
2978c2ecf20Sopenharmony_ci	}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	inode_dio_end(dio->inode);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	if (flags & DIO_COMPLETE_ASYNC) {
3028c2ecf20Sopenharmony_ci		/*
3038c2ecf20Sopenharmony_ci		 * generic_write_sync expects ki_pos to have been updated
3048c2ecf20Sopenharmony_ci		 * already, but the submission path only does this for
3058c2ecf20Sopenharmony_ci		 * synchronous I/O.
3068c2ecf20Sopenharmony_ci		 */
3078c2ecf20Sopenharmony_ci		dio->iocb->ki_pos += transferred;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci		if (ret > 0 && dio->op == REQ_OP_WRITE)
3108c2ecf20Sopenharmony_ci			ret = generic_write_sync(dio->iocb, ret);
3118c2ecf20Sopenharmony_ci		dio->iocb->ki_complete(dio->iocb, ret, 0);
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	kmem_cache_free(dio_cache, dio);
3158c2ecf20Sopenharmony_ci	return ret;
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic void dio_aio_complete_work(struct work_struct *work)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct dio *dio = container_of(work, struct dio, complete_work);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	dio_complete(dio, 0, DIO_COMPLETE_ASYNC | DIO_COMPLETE_INVALIDATE);
3238c2ecf20Sopenharmony_ci}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci/*
3288c2ecf20Sopenharmony_ci * Asynchronous IO callback.
3298c2ecf20Sopenharmony_ci */
3308c2ecf20Sopenharmony_cistatic void dio_bio_end_aio(struct bio *bio)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	struct dio *dio = bio->bi_private;
3338c2ecf20Sopenharmony_ci	unsigned long remaining;
3348c2ecf20Sopenharmony_ci	unsigned long flags;
3358c2ecf20Sopenharmony_ci	bool defer_completion = false;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* cleanup the bio */
3388c2ecf20Sopenharmony_ci	dio_bio_complete(dio, bio);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dio->bio_lock, flags);
3418c2ecf20Sopenharmony_ci	remaining = --dio->refcount;
3428c2ecf20Sopenharmony_ci	if (remaining == 1 && dio->waiter)
3438c2ecf20Sopenharmony_ci		wake_up_process(dio->waiter);
3448c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dio->bio_lock, flags);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (remaining == 0) {
3478c2ecf20Sopenharmony_ci		/*
3488c2ecf20Sopenharmony_ci		 * Defer completion when defer_completion is set or
3498c2ecf20Sopenharmony_ci		 * when the inode has pages mapped and this is AIO write.
3508c2ecf20Sopenharmony_ci		 * We need to invalidate those pages because there is a
3518c2ecf20Sopenharmony_ci		 * chance they contain stale data in the case buffered IO
3528c2ecf20Sopenharmony_ci		 * went in between AIO submission and completion into the
3538c2ecf20Sopenharmony_ci		 * same region.
3548c2ecf20Sopenharmony_ci		 */
3558c2ecf20Sopenharmony_ci		if (dio->result)
3568c2ecf20Sopenharmony_ci			defer_completion = dio->defer_completion ||
3578c2ecf20Sopenharmony_ci					   (dio->op == REQ_OP_WRITE &&
3588c2ecf20Sopenharmony_ci					    dio->inode->i_mapping->nrpages);
3598c2ecf20Sopenharmony_ci		if (defer_completion) {
3608c2ecf20Sopenharmony_ci			INIT_WORK(&dio->complete_work, dio_aio_complete_work);
3618c2ecf20Sopenharmony_ci			queue_work(dio->inode->i_sb->s_dio_done_wq,
3628c2ecf20Sopenharmony_ci				   &dio->complete_work);
3638c2ecf20Sopenharmony_ci		} else {
3648c2ecf20Sopenharmony_ci			dio_complete(dio, 0, DIO_COMPLETE_ASYNC);
3658c2ecf20Sopenharmony_ci		}
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/*
3708c2ecf20Sopenharmony_ci * The BIO completion handler simply queues the BIO up for the process-context
3718c2ecf20Sopenharmony_ci * handler.
3728c2ecf20Sopenharmony_ci *
3738c2ecf20Sopenharmony_ci * During I/O bi_private points at the dio.  After I/O, bi_private is used to
3748c2ecf20Sopenharmony_ci * implement a singly-linked list of completed BIOs, at dio->bio_list.
3758c2ecf20Sopenharmony_ci */
3768c2ecf20Sopenharmony_cistatic void dio_bio_end_io(struct bio *bio)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	struct dio *dio = bio->bi_private;
3798c2ecf20Sopenharmony_ci	unsigned long flags;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dio->bio_lock, flags);
3828c2ecf20Sopenharmony_ci	bio->bi_private = dio->bio_list;
3838c2ecf20Sopenharmony_ci	dio->bio_list = bio;
3848c2ecf20Sopenharmony_ci	if (--dio->refcount == 1 && dio->waiter)
3858c2ecf20Sopenharmony_ci		wake_up_process(dio->waiter);
3868c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dio->bio_lock, flags);
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic inline void
3908c2ecf20Sopenharmony_cidio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
3918c2ecf20Sopenharmony_ci	      struct block_device *bdev,
3928c2ecf20Sopenharmony_ci	      sector_t first_sector, int nr_vecs)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	struct bio *bio;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/*
3978c2ecf20Sopenharmony_ci	 * bio_alloc() is guaranteed to return a bio when allowed to sleep and
3988c2ecf20Sopenharmony_ci	 * we request a valid number of vectors.
3998c2ecf20Sopenharmony_ci	 */
4008c2ecf20Sopenharmony_ci	bio = bio_alloc(GFP_KERNEL, nr_vecs);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	bio_set_dev(bio, bdev);
4038c2ecf20Sopenharmony_ci	bio->bi_iter.bi_sector = first_sector;
4048c2ecf20Sopenharmony_ci	bio_set_op_attrs(bio, dio->op, dio->op_flags);
4058c2ecf20Sopenharmony_ci	if (dio->is_async)
4068c2ecf20Sopenharmony_ci		bio->bi_end_io = dio_bio_end_aio;
4078c2ecf20Sopenharmony_ci	else
4088c2ecf20Sopenharmony_ci		bio->bi_end_io = dio_bio_end_io;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	bio->bi_write_hint = dio->iocb->ki_hint;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	sdio->bio = bio;
4138c2ecf20Sopenharmony_ci	sdio->logical_offset_in_bio = sdio->cur_page_fs_offset;
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci/*
4178c2ecf20Sopenharmony_ci * In the AIO read case we speculatively dirty the pages before starting IO.
4188c2ecf20Sopenharmony_ci * During IO completion, any of these pages which happen to have been written
4198c2ecf20Sopenharmony_ci * back will be redirtied by bio_check_pages_dirty().
4208c2ecf20Sopenharmony_ci *
4218c2ecf20Sopenharmony_ci * bios hold a dio reference between submit_bio and ->end_io.
4228c2ecf20Sopenharmony_ci */
4238c2ecf20Sopenharmony_cistatic inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	struct bio *bio = sdio->bio;
4268c2ecf20Sopenharmony_ci	unsigned long flags;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	bio->bi_private = dio;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dio->bio_lock, flags);
4318c2ecf20Sopenharmony_ci	dio->refcount++;
4328c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dio->bio_lock, flags);
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty)
4358c2ecf20Sopenharmony_ci		bio_set_pages_dirty(bio);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	dio->bio_disk = bio->bi_disk;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	if (sdio->submit_io) {
4408c2ecf20Sopenharmony_ci		sdio->submit_io(bio, dio->inode, sdio->logical_offset_in_bio);
4418c2ecf20Sopenharmony_ci		dio->bio_cookie = BLK_QC_T_NONE;
4428c2ecf20Sopenharmony_ci	} else
4438c2ecf20Sopenharmony_ci		dio->bio_cookie = submit_bio(bio);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	sdio->bio = NULL;
4468c2ecf20Sopenharmony_ci	sdio->boundary = 0;
4478c2ecf20Sopenharmony_ci	sdio->logical_offset_in_bio = 0;
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci/*
4518c2ecf20Sopenharmony_ci * Release any resources in case of a failure
4528c2ecf20Sopenharmony_ci */
4538c2ecf20Sopenharmony_cistatic inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	while (sdio->head < sdio->tail)
4568c2ecf20Sopenharmony_ci		put_page(dio->pages[sdio->head++]);
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci/*
4608c2ecf20Sopenharmony_ci * Wait for the next BIO to complete.  Remove it and return it.  NULL is
4618c2ecf20Sopenharmony_ci * returned once all BIOs have been completed.  This must only be called once
4628c2ecf20Sopenharmony_ci * all bios have been issued so that dio->refcount can only decrease.  This
4638c2ecf20Sopenharmony_ci * requires that that the caller hold a reference on the dio.
4648c2ecf20Sopenharmony_ci */
4658c2ecf20Sopenharmony_cistatic struct bio *dio_await_one(struct dio *dio)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	unsigned long flags;
4688c2ecf20Sopenharmony_ci	struct bio *bio = NULL;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dio->bio_lock, flags);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/*
4738c2ecf20Sopenharmony_ci	 * Wait as long as the list is empty and there are bios in flight.  bio
4748c2ecf20Sopenharmony_ci	 * completion drops the count, maybe adds to the list, and wakes while
4758c2ecf20Sopenharmony_ci	 * holding the bio_lock so we don't need set_current_state()'s barrier
4768c2ecf20Sopenharmony_ci	 * and can call it after testing our condition.
4778c2ecf20Sopenharmony_ci	 */
4788c2ecf20Sopenharmony_ci	while (dio->refcount > 1 && dio->bio_list == NULL) {
4798c2ecf20Sopenharmony_ci		__set_current_state(TASK_UNINTERRUPTIBLE);
4808c2ecf20Sopenharmony_ci		dio->waiter = current;
4818c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&dio->bio_lock, flags);
4828c2ecf20Sopenharmony_ci		if (!(dio->iocb->ki_flags & IOCB_HIPRI) ||
4838c2ecf20Sopenharmony_ci		    !blk_poll(dio->bio_disk->queue, dio->bio_cookie, true))
4848c2ecf20Sopenharmony_ci			blk_io_schedule();
4858c2ecf20Sopenharmony_ci		/* wake up sets us TASK_RUNNING */
4868c2ecf20Sopenharmony_ci		spin_lock_irqsave(&dio->bio_lock, flags);
4878c2ecf20Sopenharmony_ci		dio->waiter = NULL;
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci	if (dio->bio_list) {
4908c2ecf20Sopenharmony_ci		bio = dio->bio_list;
4918c2ecf20Sopenharmony_ci		dio->bio_list = bio->bi_private;
4928c2ecf20Sopenharmony_ci	}
4938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dio->bio_lock, flags);
4948c2ecf20Sopenharmony_ci	return bio;
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci/*
4988c2ecf20Sopenharmony_ci * Process one completed BIO.  No locks are held.
4998c2ecf20Sopenharmony_ci */
5008c2ecf20Sopenharmony_cistatic blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	blk_status_t err = bio->bi_status;
5038c2ecf20Sopenharmony_ci	bool should_dirty = dio->op == REQ_OP_READ && dio->should_dirty;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	if (err) {
5068c2ecf20Sopenharmony_ci		if (err == BLK_STS_AGAIN && (bio->bi_opf & REQ_NOWAIT))
5078c2ecf20Sopenharmony_ci			dio->io_error = -EAGAIN;
5088c2ecf20Sopenharmony_ci		else
5098c2ecf20Sopenharmony_ci			dio->io_error = -EIO;
5108c2ecf20Sopenharmony_ci	}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	if (dio->is_async && should_dirty) {
5138c2ecf20Sopenharmony_ci		bio_check_pages_dirty(bio);	/* transfers ownership */
5148c2ecf20Sopenharmony_ci	} else {
5158c2ecf20Sopenharmony_ci		bio_release_pages(bio, should_dirty);
5168c2ecf20Sopenharmony_ci		bio_put(bio);
5178c2ecf20Sopenharmony_ci	}
5188c2ecf20Sopenharmony_ci	return err;
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci/*
5228c2ecf20Sopenharmony_ci * Wait on and process all in-flight BIOs.  This must only be called once
5238c2ecf20Sopenharmony_ci * all bios have been issued so that the refcount can only decrease.
5248c2ecf20Sopenharmony_ci * This just waits for all bios to make it through dio_bio_complete.  IO
5258c2ecf20Sopenharmony_ci * errors are propagated through dio->io_error and should be propagated via
5268c2ecf20Sopenharmony_ci * dio_complete().
5278c2ecf20Sopenharmony_ci */
5288c2ecf20Sopenharmony_cistatic void dio_await_completion(struct dio *dio)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	struct bio *bio;
5318c2ecf20Sopenharmony_ci	do {
5328c2ecf20Sopenharmony_ci		bio = dio_await_one(dio);
5338c2ecf20Sopenharmony_ci		if (bio)
5348c2ecf20Sopenharmony_ci			dio_bio_complete(dio, bio);
5358c2ecf20Sopenharmony_ci	} while (bio);
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci/*
5398c2ecf20Sopenharmony_ci * A really large O_DIRECT read or write can generate a lot of BIOs.  So
5408c2ecf20Sopenharmony_ci * to keep the memory consumption sane we periodically reap any completed BIOs
5418c2ecf20Sopenharmony_ci * during the BIO generation phase.
5428c2ecf20Sopenharmony_ci *
5438c2ecf20Sopenharmony_ci * This also helps to limit the peak amount of pinned userspace memory.
5448c2ecf20Sopenharmony_ci */
5458c2ecf20Sopenharmony_cistatic inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	int ret = 0;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	if (sdio->reap_counter++ >= 64) {
5508c2ecf20Sopenharmony_ci		while (dio->bio_list) {
5518c2ecf20Sopenharmony_ci			unsigned long flags;
5528c2ecf20Sopenharmony_ci			struct bio *bio;
5538c2ecf20Sopenharmony_ci			int ret2;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci			spin_lock_irqsave(&dio->bio_lock, flags);
5568c2ecf20Sopenharmony_ci			bio = dio->bio_list;
5578c2ecf20Sopenharmony_ci			dio->bio_list = bio->bi_private;
5588c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&dio->bio_lock, flags);
5598c2ecf20Sopenharmony_ci			ret2 = blk_status_to_errno(dio_bio_complete(dio, bio));
5608c2ecf20Sopenharmony_ci			if (ret == 0)
5618c2ecf20Sopenharmony_ci				ret = ret2;
5628c2ecf20Sopenharmony_ci		}
5638c2ecf20Sopenharmony_ci		sdio->reap_counter = 0;
5648c2ecf20Sopenharmony_ci	}
5658c2ecf20Sopenharmony_ci	return ret;
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci/*
5698c2ecf20Sopenharmony_ci * Create workqueue for deferred direct IO completions. We allocate the
5708c2ecf20Sopenharmony_ci * workqueue when it's first needed. This avoids creating workqueue for
5718c2ecf20Sopenharmony_ci * filesystems that don't need it and also allows us to create the workqueue
5728c2ecf20Sopenharmony_ci * late enough so the we can include s_id in the name of the workqueue.
5738c2ecf20Sopenharmony_ci */
5748c2ecf20Sopenharmony_ciint sb_init_dio_done_wq(struct super_block *sb)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	struct workqueue_struct *old;
5778c2ecf20Sopenharmony_ci	struct workqueue_struct *wq = alloc_workqueue("dio/%s",
5788c2ecf20Sopenharmony_ci						      WQ_MEM_RECLAIM, 0,
5798c2ecf20Sopenharmony_ci						      sb->s_id);
5808c2ecf20Sopenharmony_ci	if (!wq)
5818c2ecf20Sopenharmony_ci		return -ENOMEM;
5828c2ecf20Sopenharmony_ci	/*
5838c2ecf20Sopenharmony_ci	 * This has to be atomic as more DIOs can race to create the workqueue
5848c2ecf20Sopenharmony_ci	 */
5858c2ecf20Sopenharmony_ci	old = cmpxchg(&sb->s_dio_done_wq, NULL, wq);
5868c2ecf20Sopenharmony_ci	/* Someone created workqueue before us? Free ours... */
5878c2ecf20Sopenharmony_ci	if (old)
5888c2ecf20Sopenharmony_ci		destroy_workqueue(wq);
5898c2ecf20Sopenharmony_ci	return 0;
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic int dio_set_defer_completion(struct dio *dio)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	struct super_block *sb = dio->inode->i_sb;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (dio->defer_completion)
5978c2ecf20Sopenharmony_ci		return 0;
5988c2ecf20Sopenharmony_ci	dio->defer_completion = true;
5998c2ecf20Sopenharmony_ci	if (!sb->s_dio_done_wq)
6008c2ecf20Sopenharmony_ci		return sb_init_dio_done_wq(sb);
6018c2ecf20Sopenharmony_ci	return 0;
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci/*
6058c2ecf20Sopenharmony_ci * Call into the fs to map some more disk blocks.  We record the current number
6068c2ecf20Sopenharmony_ci * of available blocks at sdio->blocks_available.  These are in units of the
6078c2ecf20Sopenharmony_ci * fs blocksize, i_blocksize(inode).
6088c2ecf20Sopenharmony_ci *
6098c2ecf20Sopenharmony_ci * The fs is allowed to map lots of blocks at once.  If it wants to do that,
6108c2ecf20Sopenharmony_ci * it uses the passed inode-relative block number as the file offset, as usual.
6118c2ecf20Sopenharmony_ci *
6128c2ecf20Sopenharmony_ci * get_block() is passed the number of i_blkbits-sized blocks which direct_io
6138c2ecf20Sopenharmony_ci * has remaining to do.  The fs should not map more than this number of blocks.
6148c2ecf20Sopenharmony_ci *
6158c2ecf20Sopenharmony_ci * If the fs has mapped a lot of blocks, it should populate bh->b_size to
6168c2ecf20Sopenharmony_ci * indicate how much contiguous disk space has been made available at
6178c2ecf20Sopenharmony_ci * bh->b_blocknr.
6188c2ecf20Sopenharmony_ci *
6198c2ecf20Sopenharmony_ci * If *any* of the mapped blocks are new, then the fs must set buffer_new().
6208c2ecf20Sopenharmony_ci * This isn't very efficient...
6218c2ecf20Sopenharmony_ci *
6228c2ecf20Sopenharmony_ci * In the case of filesystem holes: the fs may return an arbitrarily-large
6238c2ecf20Sopenharmony_ci * hole by returning an appropriate value in b_size and by clearing
6248c2ecf20Sopenharmony_ci * buffer_mapped().  However the direct-io code will only process holes one
6258c2ecf20Sopenharmony_ci * block at a time - it will repeatedly call get_block() as it walks the hole.
6268c2ecf20Sopenharmony_ci */
6278c2ecf20Sopenharmony_cistatic int get_more_blocks(struct dio *dio, struct dio_submit *sdio,
6288c2ecf20Sopenharmony_ci			   struct buffer_head *map_bh)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	int ret;
6318c2ecf20Sopenharmony_ci	sector_t fs_startblk;	/* Into file, in filesystem-sized blocks */
6328c2ecf20Sopenharmony_ci	sector_t fs_endblk;	/* Into file, in filesystem-sized blocks */
6338c2ecf20Sopenharmony_ci	unsigned long fs_count;	/* Number of filesystem-sized blocks */
6348c2ecf20Sopenharmony_ci	int create;
6358c2ecf20Sopenharmony_ci	unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor;
6368c2ecf20Sopenharmony_ci	loff_t i_size;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	/*
6398c2ecf20Sopenharmony_ci	 * If there was a memory error and we've overwritten all the
6408c2ecf20Sopenharmony_ci	 * mapped blocks then we can now return that memory error
6418c2ecf20Sopenharmony_ci	 */
6428c2ecf20Sopenharmony_ci	ret = dio->page_errors;
6438c2ecf20Sopenharmony_ci	if (ret == 0) {
6448c2ecf20Sopenharmony_ci		BUG_ON(sdio->block_in_file >= sdio->final_block_in_request);
6458c2ecf20Sopenharmony_ci		fs_startblk = sdio->block_in_file >> sdio->blkfactor;
6468c2ecf20Sopenharmony_ci		fs_endblk = (sdio->final_block_in_request - 1) >>
6478c2ecf20Sopenharmony_ci					sdio->blkfactor;
6488c2ecf20Sopenharmony_ci		fs_count = fs_endblk - fs_startblk + 1;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci		map_bh->b_state = 0;
6518c2ecf20Sopenharmony_ci		map_bh->b_size = fs_count << i_blkbits;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci		/*
6548c2ecf20Sopenharmony_ci		 * For writes that could fill holes inside i_size on a
6558c2ecf20Sopenharmony_ci		 * DIO_SKIP_HOLES filesystem we forbid block creations: only
6568c2ecf20Sopenharmony_ci		 * overwrites are permitted. We will return early to the caller
6578c2ecf20Sopenharmony_ci		 * once we see an unmapped buffer head returned, and the caller
6588c2ecf20Sopenharmony_ci		 * will fall back to buffered I/O.
6598c2ecf20Sopenharmony_ci		 *
6608c2ecf20Sopenharmony_ci		 * Otherwise the decision is left to the get_blocks method,
6618c2ecf20Sopenharmony_ci		 * which may decide to handle it or also return an unmapped
6628c2ecf20Sopenharmony_ci		 * buffer head.
6638c2ecf20Sopenharmony_ci		 */
6648c2ecf20Sopenharmony_ci		create = dio->op == REQ_OP_WRITE;
6658c2ecf20Sopenharmony_ci		if (dio->flags & DIO_SKIP_HOLES) {
6668c2ecf20Sopenharmony_ci			i_size = i_size_read(dio->inode);
6678c2ecf20Sopenharmony_ci			if (i_size && fs_startblk <= (i_size - 1) >> i_blkbits)
6688c2ecf20Sopenharmony_ci				create = 0;
6698c2ecf20Sopenharmony_ci		}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci		ret = (*sdio->get_block)(dio->inode, fs_startblk,
6728c2ecf20Sopenharmony_ci						map_bh, create);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci		/* Store for completion */
6758c2ecf20Sopenharmony_ci		dio->private = map_bh->b_private;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci		if (ret == 0 && buffer_defer_completion(map_bh))
6788c2ecf20Sopenharmony_ci			ret = dio_set_defer_completion(dio);
6798c2ecf20Sopenharmony_ci	}
6808c2ecf20Sopenharmony_ci	return ret;
6818c2ecf20Sopenharmony_ci}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci/*
6848c2ecf20Sopenharmony_ci * There is no bio.  Make one now.
6858c2ecf20Sopenharmony_ci */
6868c2ecf20Sopenharmony_cistatic inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
6878c2ecf20Sopenharmony_ci		sector_t start_sector, struct buffer_head *map_bh)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	sector_t sector;
6908c2ecf20Sopenharmony_ci	int ret, nr_pages;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	ret = dio_bio_reap(dio, sdio);
6938c2ecf20Sopenharmony_ci	if (ret)
6948c2ecf20Sopenharmony_ci		goto out;
6958c2ecf20Sopenharmony_ci	sector = start_sector << (sdio->blkbits - 9);
6968c2ecf20Sopenharmony_ci	nr_pages = min(sdio->pages_in_io, BIO_MAX_PAGES);
6978c2ecf20Sopenharmony_ci	BUG_ON(nr_pages <= 0);
6988c2ecf20Sopenharmony_ci	dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
6998c2ecf20Sopenharmony_ci	sdio->boundary = 0;
7008c2ecf20Sopenharmony_ciout:
7018c2ecf20Sopenharmony_ci	return ret;
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci/*
7058c2ecf20Sopenharmony_ci * Attempt to put the current chunk of 'cur_page' into the current BIO.  If
7068c2ecf20Sopenharmony_ci * that was successful then update final_block_in_bio and take a ref against
7078c2ecf20Sopenharmony_ci * the just-added page.
7088c2ecf20Sopenharmony_ci *
7098c2ecf20Sopenharmony_ci * Return zero on success.  Non-zero means the caller needs to start a new BIO.
7108c2ecf20Sopenharmony_ci */
7118c2ecf20Sopenharmony_cistatic inline int dio_bio_add_page(struct dio_submit *sdio)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	int ret;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	ret = bio_add_page(sdio->bio, sdio->cur_page,
7168c2ecf20Sopenharmony_ci			sdio->cur_page_len, sdio->cur_page_offset);
7178c2ecf20Sopenharmony_ci	if (ret == sdio->cur_page_len) {
7188c2ecf20Sopenharmony_ci		/*
7198c2ecf20Sopenharmony_ci		 * Decrement count only, if we are done with this page
7208c2ecf20Sopenharmony_ci		 */
7218c2ecf20Sopenharmony_ci		if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE)
7228c2ecf20Sopenharmony_ci			sdio->pages_in_io--;
7238c2ecf20Sopenharmony_ci		get_page(sdio->cur_page);
7248c2ecf20Sopenharmony_ci		sdio->final_block_in_bio = sdio->cur_page_block +
7258c2ecf20Sopenharmony_ci			(sdio->cur_page_len >> sdio->blkbits);
7268c2ecf20Sopenharmony_ci		ret = 0;
7278c2ecf20Sopenharmony_ci	} else {
7288c2ecf20Sopenharmony_ci		ret = 1;
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci	return ret;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci/*
7348c2ecf20Sopenharmony_ci * Put cur_page under IO.  The section of cur_page which is described by
7358c2ecf20Sopenharmony_ci * cur_page_offset,cur_page_len is put into a BIO.  The section of cur_page
7368c2ecf20Sopenharmony_ci * starts on-disk at cur_page_block.
7378c2ecf20Sopenharmony_ci *
7388c2ecf20Sopenharmony_ci * We take a ref against the page here (on behalf of its presence in the bio).
7398c2ecf20Sopenharmony_ci *
7408c2ecf20Sopenharmony_ci * The caller of this function is responsible for removing cur_page from the
7418c2ecf20Sopenharmony_ci * dio, and for dropping the refcount which came from that presence.
7428c2ecf20Sopenharmony_ci */
7438c2ecf20Sopenharmony_cistatic inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio,
7448c2ecf20Sopenharmony_ci		struct buffer_head *map_bh)
7458c2ecf20Sopenharmony_ci{
7468c2ecf20Sopenharmony_ci	int ret = 0;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	if (sdio->bio) {
7498c2ecf20Sopenharmony_ci		loff_t cur_offset = sdio->cur_page_fs_offset;
7508c2ecf20Sopenharmony_ci		loff_t bio_next_offset = sdio->logical_offset_in_bio +
7518c2ecf20Sopenharmony_ci			sdio->bio->bi_iter.bi_size;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci		/*
7548c2ecf20Sopenharmony_ci		 * See whether this new request is contiguous with the old.
7558c2ecf20Sopenharmony_ci		 *
7568c2ecf20Sopenharmony_ci		 * Btrfs cannot handle having logically non-contiguous requests
7578c2ecf20Sopenharmony_ci		 * submitted.  For example if you have
7588c2ecf20Sopenharmony_ci		 *
7598c2ecf20Sopenharmony_ci		 * Logical:  [0-4095][HOLE][8192-12287]
7608c2ecf20Sopenharmony_ci		 * Physical: [0-4095]      [4096-8191]
7618c2ecf20Sopenharmony_ci		 *
7628c2ecf20Sopenharmony_ci		 * We cannot submit those pages together as one BIO.  So if our
7638c2ecf20Sopenharmony_ci		 * current logical offset in the file does not equal what would
7648c2ecf20Sopenharmony_ci		 * be the next logical offset in the bio, submit the bio we
7658c2ecf20Sopenharmony_ci		 * have.
7668c2ecf20Sopenharmony_ci		 */
7678c2ecf20Sopenharmony_ci		if (sdio->final_block_in_bio != sdio->cur_page_block ||
7688c2ecf20Sopenharmony_ci		    cur_offset != bio_next_offset)
7698c2ecf20Sopenharmony_ci			dio_bio_submit(dio, sdio);
7708c2ecf20Sopenharmony_ci	}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	if (sdio->bio == NULL) {
7738c2ecf20Sopenharmony_ci		ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
7748c2ecf20Sopenharmony_ci		if (ret)
7758c2ecf20Sopenharmony_ci			goto out;
7768c2ecf20Sopenharmony_ci	}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	if (dio_bio_add_page(sdio) != 0) {
7798c2ecf20Sopenharmony_ci		dio_bio_submit(dio, sdio);
7808c2ecf20Sopenharmony_ci		ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
7818c2ecf20Sopenharmony_ci		if (ret == 0) {
7828c2ecf20Sopenharmony_ci			ret = dio_bio_add_page(sdio);
7838c2ecf20Sopenharmony_ci			BUG_ON(ret != 0);
7848c2ecf20Sopenharmony_ci		}
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ciout:
7878c2ecf20Sopenharmony_ci	return ret;
7888c2ecf20Sopenharmony_ci}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci/*
7918c2ecf20Sopenharmony_ci * An autonomous function to put a chunk of a page under deferred IO.
7928c2ecf20Sopenharmony_ci *
7938c2ecf20Sopenharmony_ci * The caller doesn't actually know (or care) whether this piece of page is in
7948c2ecf20Sopenharmony_ci * a BIO, or is under IO or whatever.  We just take care of all possible
7958c2ecf20Sopenharmony_ci * situations here.  The separation between the logic of do_direct_IO() and
7968c2ecf20Sopenharmony_ci * that of submit_page_section() is important for clarity.  Please don't break.
7978c2ecf20Sopenharmony_ci *
7988c2ecf20Sopenharmony_ci * The chunk of page starts on-disk at blocknr.
7998c2ecf20Sopenharmony_ci *
8008c2ecf20Sopenharmony_ci * We perform deferred IO, by recording the last-submitted page inside our
8018c2ecf20Sopenharmony_ci * private part of the dio structure.  If possible, we just expand the IO
8028c2ecf20Sopenharmony_ci * across that page here.
8038c2ecf20Sopenharmony_ci *
8048c2ecf20Sopenharmony_ci * If that doesn't work out then we put the old page into the bio and add this
8058c2ecf20Sopenharmony_ci * page to the dio instead.
8068c2ecf20Sopenharmony_ci */
8078c2ecf20Sopenharmony_cistatic inline int
8088c2ecf20Sopenharmony_cisubmit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page,
8098c2ecf20Sopenharmony_ci		    unsigned offset, unsigned len, sector_t blocknr,
8108c2ecf20Sopenharmony_ci		    struct buffer_head *map_bh)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	int ret = 0;
8138c2ecf20Sopenharmony_ci	int boundary = sdio->boundary;	/* dio_send_cur_page may clear it */
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	if (dio->op == REQ_OP_WRITE) {
8168c2ecf20Sopenharmony_ci		/*
8178c2ecf20Sopenharmony_ci		 * Read accounting is performed in submit_bio()
8188c2ecf20Sopenharmony_ci		 */
8198c2ecf20Sopenharmony_ci		task_io_account_write(len);
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	/*
8238c2ecf20Sopenharmony_ci	 * Can we just grow the current page's presence in the dio?
8248c2ecf20Sopenharmony_ci	 */
8258c2ecf20Sopenharmony_ci	if (sdio->cur_page == page &&
8268c2ecf20Sopenharmony_ci	    sdio->cur_page_offset + sdio->cur_page_len == offset &&
8278c2ecf20Sopenharmony_ci	    sdio->cur_page_block +
8288c2ecf20Sopenharmony_ci	    (sdio->cur_page_len >> sdio->blkbits) == blocknr) {
8298c2ecf20Sopenharmony_ci		sdio->cur_page_len += len;
8308c2ecf20Sopenharmony_ci		goto out;
8318c2ecf20Sopenharmony_ci	}
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	/*
8348c2ecf20Sopenharmony_ci	 * If there's a deferred page already there then send it.
8358c2ecf20Sopenharmony_ci	 */
8368c2ecf20Sopenharmony_ci	if (sdio->cur_page) {
8378c2ecf20Sopenharmony_ci		ret = dio_send_cur_page(dio, sdio, map_bh);
8388c2ecf20Sopenharmony_ci		put_page(sdio->cur_page);
8398c2ecf20Sopenharmony_ci		sdio->cur_page = NULL;
8408c2ecf20Sopenharmony_ci		if (ret)
8418c2ecf20Sopenharmony_ci			return ret;
8428c2ecf20Sopenharmony_ci	}
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	get_page(page);		/* It is in dio */
8458c2ecf20Sopenharmony_ci	sdio->cur_page = page;
8468c2ecf20Sopenharmony_ci	sdio->cur_page_offset = offset;
8478c2ecf20Sopenharmony_ci	sdio->cur_page_len = len;
8488c2ecf20Sopenharmony_ci	sdio->cur_page_block = blocknr;
8498c2ecf20Sopenharmony_ci	sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits;
8508c2ecf20Sopenharmony_ciout:
8518c2ecf20Sopenharmony_ci	/*
8528c2ecf20Sopenharmony_ci	 * If boundary then we want to schedule the IO now to
8538c2ecf20Sopenharmony_ci	 * avoid metadata seeks.
8548c2ecf20Sopenharmony_ci	 */
8558c2ecf20Sopenharmony_ci	if (boundary) {
8568c2ecf20Sopenharmony_ci		ret = dio_send_cur_page(dio, sdio, map_bh);
8578c2ecf20Sopenharmony_ci		if (sdio->bio)
8588c2ecf20Sopenharmony_ci			dio_bio_submit(dio, sdio);
8598c2ecf20Sopenharmony_ci		put_page(sdio->cur_page);
8608c2ecf20Sopenharmony_ci		sdio->cur_page = NULL;
8618c2ecf20Sopenharmony_ci	}
8628c2ecf20Sopenharmony_ci	return ret;
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci/*
8668c2ecf20Sopenharmony_ci * If we are not writing the entire block and get_block() allocated
8678c2ecf20Sopenharmony_ci * the block for us, we need to fill-in the unused portion of the
8688c2ecf20Sopenharmony_ci * block with zeros. This happens only if user-buffer, fileoffset or
8698c2ecf20Sopenharmony_ci * io length is not filesystem block-size multiple.
8708c2ecf20Sopenharmony_ci *
8718c2ecf20Sopenharmony_ci * `end' is zero if we're doing the start of the IO, 1 at the end of the
8728c2ecf20Sopenharmony_ci * IO.
8738c2ecf20Sopenharmony_ci */
8748c2ecf20Sopenharmony_cistatic inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio,
8758c2ecf20Sopenharmony_ci		int end, struct buffer_head *map_bh)
8768c2ecf20Sopenharmony_ci{
8778c2ecf20Sopenharmony_ci	unsigned dio_blocks_per_fs_block;
8788c2ecf20Sopenharmony_ci	unsigned this_chunk_blocks;	/* In dio_blocks */
8798c2ecf20Sopenharmony_ci	unsigned this_chunk_bytes;
8808c2ecf20Sopenharmony_ci	struct page *page;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	sdio->start_zero_done = 1;
8838c2ecf20Sopenharmony_ci	if (!sdio->blkfactor || !buffer_new(map_bh))
8848c2ecf20Sopenharmony_ci		return;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	dio_blocks_per_fs_block = 1 << sdio->blkfactor;
8878c2ecf20Sopenharmony_ci	this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1);
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	if (!this_chunk_blocks)
8908c2ecf20Sopenharmony_ci		return;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	/*
8938c2ecf20Sopenharmony_ci	 * We need to zero out part of an fs block.  It is either at the
8948c2ecf20Sopenharmony_ci	 * beginning or the end of the fs block.
8958c2ecf20Sopenharmony_ci	 */
8968c2ecf20Sopenharmony_ci	if (end)
8978c2ecf20Sopenharmony_ci		this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	this_chunk_bytes = this_chunk_blocks << sdio->blkbits;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	page = ZERO_PAGE(0);
9028c2ecf20Sopenharmony_ci	if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes,
9038c2ecf20Sopenharmony_ci				sdio->next_block_for_io, map_bh))
9048c2ecf20Sopenharmony_ci		return;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	sdio->next_block_for_io += this_chunk_blocks;
9078c2ecf20Sopenharmony_ci}
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci/*
9108c2ecf20Sopenharmony_ci * Walk the user pages, and the file, mapping blocks to disk and generating
9118c2ecf20Sopenharmony_ci * a sequence of (page,offset,len,block) mappings.  These mappings are injected
9128c2ecf20Sopenharmony_ci * into submit_page_section(), which takes care of the next stage of submission
9138c2ecf20Sopenharmony_ci *
9148c2ecf20Sopenharmony_ci * Direct IO against a blockdev is different from a file.  Because we can
9158c2ecf20Sopenharmony_ci * happily perform page-sized but 512-byte aligned IOs.  It is important that
9168c2ecf20Sopenharmony_ci * blockdev IO be able to have fine alignment and large sizes.
9178c2ecf20Sopenharmony_ci *
9188c2ecf20Sopenharmony_ci * So what we do is to permit the ->get_block function to populate bh.b_size
9198c2ecf20Sopenharmony_ci * with the size of IO which is permitted at this offset and this i_blkbits.
9208c2ecf20Sopenharmony_ci *
9218c2ecf20Sopenharmony_ci * For best results, the blockdev should be set up with 512-byte i_blkbits and
9228c2ecf20Sopenharmony_ci * it should set b_size to PAGE_SIZE or more inside get_block().  This gives
9238c2ecf20Sopenharmony_ci * fine alignment but still allows this function to work in PAGE_SIZE units.
9248c2ecf20Sopenharmony_ci */
9258c2ecf20Sopenharmony_cistatic int do_direct_IO(struct dio *dio, struct dio_submit *sdio,
9268c2ecf20Sopenharmony_ci			struct buffer_head *map_bh)
9278c2ecf20Sopenharmony_ci{
9288c2ecf20Sopenharmony_ci	const unsigned blkbits = sdio->blkbits;
9298c2ecf20Sopenharmony_ci	const unsigned i_blkbits = blkbits + sdio->blkfactor;
9308c2ecf20Sopenharmony_ci	int ret = 0;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	while (sdio->block_in_file < sdio->final_block_in_request) {
9338c2ecf20Sopenharmony_ci		struct page *page;
9348c2ecf20Sopenharmony_ci		size_t from, to;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci		page = dio_get_page(dio, sdio);
9378c2ecf20Sopenharmony_ci		if (IS_ERR(page)) {
9388c2ecf20Sopenharmony_ci			ret = PTR_ERR(page);
9398c2ecf20Sopenharmony_ci			goto out;
9408c2ecf20Sopenharmony_ci		}
9418c2ecf20Sopenharmony_ci		from = sdio->head ? 0 : sdio->from;
9428c2ecf20Sopenharmony_ci		to = (sdio->head == sdio->tail - 1) ? sdio->to : PAGE_SIZE;
9438c2ecf20Sopenharmony_ci		sdio->head++;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci		while (from < to) {
9468c2ecf20Sopenharmony_ci			unsigned this_chunk_bytes;	/* # of bytes mapped */
9478c2ecf20Sopenharmony_ci			unsigned this_chunk_blocks;	/* # of blocks */
9488c2ecf20Sopenharmony_ci			unsigned u;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci			if (sdio->blocks_available == 0) {
9518c2ecf20Sopenharmony_ci				/*
9528c2ecf20Sopenharmony_ci				 * Need to go and map some more disk
9538c2ecf20Sopenharmony_ci				 */
9548c2ecf20Sopenharmony_ci				unsigned long blkmask;
9558c2ecf20Sopenharmony_ci				unsigned long dio_remainder;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci				ret = get_more_blocks(dio, sdio, map_bh);
9588c2ecf20Sopenharmony_ci				if (ret) {
9598c2ecf20Sopenharmony_ci					put_page(page);
9608c2ecf20Sopenharmony_ci					goto out;
9618c2ecf20Sopenharmony_ci				}
9628c2ecf20Sopenharmony_ci				if (!buffer_mapped(map_bh))
9638c2ecf20Sopenharmony_ci					goto do_holes;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci				sdio->blocks_available =
9668c2ecf20Sopenharmony_ci						map_bh->b_size >> blkbits;
9678c2ecf20Sopenharmony_ci				sdio->next_block_for_io =
9688c2ecf20Sopenharmony_ci					map_bh->b_blocknr << sdio->blkfactor;
9698c2ecf20Sopenharmony_ci				if (buffer_new(map_bh)) {
9708c2ecf20Sopenharmony_ci					clean_bdev_aliases(
9718c2ecf20Sopenharmony_ci						map_bh->b_bdev,
9728c2ecf20Sopenharmony_ci						map_bh->b_blocknr,
9738c2ecf20Sopenharmony_ci						map_bh->b_size >> i_blkbits);
9748c2ecf20Sopenharmony_ci				}
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci				if (!sdio->blkfactor)
9778c2ecf20Sopenharmony_ci					goto do_holes;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci				blkmask = (1 << sdio->blkfactor) - 1;
9808c2ecf20Sopenharmony_ci				dio_remainder = (sdio->block_in_file & blkmask);
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci				/*
9838c2ecf20Sopenharmony_ci				 * If we are at the start of IO and that IO
9848c2ecf20Sopenharmony_ci				 * starts partway into a fs-block,
9858c2ecf20Sopenharmony_ci				 * dio_remainder will be non-zero.  If the IO
9868c2ecf20Sopenharmony_ci				 * is a read then we can simply advance the IO
9878c2ecf20Sopenharmony_ci				 * cursor to the first block which is to be
9888c2ecf20Sopenharmony_ci				 * read.  But if the IO is a write and the
9898c2ecf20Sopenharmony_ci				 * block was newly allocated we cannot do that;
9908c2ecf20Sopenharmony_ci				 * the start of the fs block must be zeroed out
9918c2ecf20Sopenharmony_ci				 * on-disk
9928c2ecf20Sopenharmony_ci				 */
9938c2ecf20Sopenharmony_ci				if (!buffer_new(map_bh))
9948c2ecf20Sopenharmony_ci					sdio->next_block_for_io += dio_remainder;
9958c2ecf20Sopenharmony_ci				sdio->blocks_available -= dio_remainder;
9968c2ecf20Sopenharmony_ci			}
9978c2ecf20Sopenharmony_cido_holes:
9988c2ecf20Sopenharmony_ci			/* Handle holes */
9998c2ecf20Sopenharmony_ci			if (!buffer_mapped(map_bh)) {
10008c2ecf20Sopenharmony_ci				loff_t i_size_aligned;
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci				/* AKPM: eargh, -ENOTBLK is a hack */
10038c2ecf20Sopenharmony_ci				if (dio->op == REQ_OP_WRITE) {
10048c2ecf20Sopenharmony_ci					put_page(page);
10058c2ecf20Sopenharmony_ci					return -ENOTBLK;
10068c2ecf20Sopenharmony_ci				}
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci				/*
10098c2ecf20Sopenharmony_ci				 * Be sure to account for a partial block as the
10108c2ecf20Sopenharmony_ci				 * last block in the file
10118c2ecf20Sopenharmony_ci				 */
10128c2ecf20Sopenharmony_ci				i_size_aligned = ALIGN(i_size_read(dio->inode),
10138c2ecf20Sopenharmony_ci							1 << blkbits);
10148c2ecf20Sopenharmony_ci				if (sdio->block_in_file >=
10158c2ecf20Sopenharmony_ci						i_size_aligned >> blkbits) {
10168c2ecf20Sopenharmony_ci					/* We hit eof */
10178c2ecf20Sopenharmony_ci					put_page(page);
10188c2ecf20Sopenharmony_ci					goto out;
10198c2ecf20Sopenharmony_ci				}
10208c2ecf20Sopenharmony_ci				zero_user(page, from, 1 << blkbits);
10218c2ecf20Sopenharmony_ci				sdio->block_in_file++;
10228c2ecf20Sopenharmony_ci				from += 1 << blkbits;
10238c2ecf20Sopenharmony_ci				dio->result += 1 << blkbits;
10248c2ecf20Sopenharmony_ci				goto next_block;
10258c2ecf20Sopenharmony_ci			}
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci			/*
10288c2ecf20Sopenharmony_ci			 * If we're performing IO which has an alignment which
10298c2ecf20Sopenharmony_ci			 * is finer than the underlying fs, go check to see if
10308c2ecf20Sopenharmony_ci			 * we must zero out the start of this block.
10318c2ecf20Sopenharmony_ci			 */
10328c2ecf20Sopenharmony_ci			if (unlikely(sdio->blkfactor && !sdio->start_zero_done))
10338c2ecf20Sopenharmony_ci				dio_zero_block(dio, sdio, 0, map_bh);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci			/*
10368c2ecf20Sopenharmony_ci			 * Work out, in this_chunk_blocks, how much disk we
10378c2ecf20Sopenharmony_ci			 * can add to this page
10388c2ecf20Sopenharmony_ci			 */
10398c2ecf20Sopenharmony_ci			this_chunk_blocks = sdio->blocks_available;
10408c2ecf20Sopenharmony_ci			u = (to - from) >> blkbits;
10418c2ecf20Sopenharmony_ci			if (this_chunk_blocks > u)
10428c2ecf20Sopenharmony_ci				this_chunk_blocks = u;
10438c2ecf20Sopenharmony_ci			u = sdio->final_block_in_request - sdio->block_in_file;
10448c2ecf20Sopenharmony_ci			if (this_chunk_blocks > u)
10458c2ecf20Sopenharmony_ci				this_chunk_blocks = u;
10468c2ecf20Sopenharmony_ci			this_chunk_bytes = this_chunk_blocks << blkbits;
10478c2ecf20Sopenharmony_ci			BUG_ON(this_chunk_bytes == 0);
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci			if (this_chunk_blocks == sdio->blocks_available)
10508c2ecf20Sopenharmony_ci				sdio->boundary = buffer_boundary(map_bh);
10518c2ecf20Sopenharmony_ci			ret = submit_page_section(dio, sdio, page,
10528c2ecf20Sopenharmony_ci						  from,
10538c2ecf20Sopenharmony_ci						  this_chunk_bytes,
10548c2ecf20Sopenharmony_ci						  sdio->next_block_for_io,
10558c2ecf20Sopenharmony_ci						  map_bh);
10568c2ecf20Sopenharmony_ci			if (ret) {
10578c2ecf20Sopenharmony_ci				put_page(page);
10588c2ecf20Sopenharmony_ci				goto out;
10598c2ecf20Sopenharmony_ci			}
10608c2ecf20Sopenharmony_ci			sdio->next_block_for_io += this_chunk_blocks;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci			sdio->block_in_file += this_chunk_blocks;
10638c2ecf20Sopenharmony_ci			from += this_chunk_bytes;
10648c2ecf20Sopenharmony_ci			dio->result += this_chunk_bytes;
10658c2ecf20Sopenharmony_ci			sdio->blocks_available -= this_chunk_blocks;
10668c2ecf20Sopenharmony_cinext_block:
10678c2ecf20Sopenharmony_ci			BUG_ON(sdio->block_in_file > sdio->final_block_in_request);
10688c2ecf20Sopenharmony_ci			if (sdio->block_in_file == sdio->final_block_in_request)
10698c2ecf20Sopenharmony_ci				break;
10708c2ecf20Sopenharmony_ci		}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci		/* Drop the ref which was taken in get_user_pages() */
10738c2ecf20Sopenharmony_ci		put_page(page);
10748c2ecf20Sopenharmony_ci	}
10758c2ecf20Sopenharmony_ciout:
10768c2ecf20Sopenharmony_ci	return ret;
10778c2ecf20Sopenharmony_ci}
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_cistatic inline int drop_refcount(struct dio *dio)
10808c2ecf20Sopenharmony_ci{
10818c2ecf20Sopenharmony_ci	int ret2;
10828c2ecf20Sopenharmony_ci	unsigned long flags;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	/*
10858c2ecf20Sopenharmony_ci	 * Sync will always be dropping the final ref and completing the
10868c2ecf20Sopenharmony_ci	 * operation.  AIO can if it was a broken operation described above or
10878c2ecf20Sopenharmony_ci	 * in fact if all the bios race to complete before we get here.  In
10888c2ecf20Sopenharmony_ci	 * that case dio_complete() translates the EIOCBQUEUED into the proper
10898c2ecf20Sopenharmony_ci	 * return code that the caller will hand to ->complete().
10908c2ecf20Sopenharmony_ci	 *
10918c2ecf20Sopenharmony_ci	 * This is managed by the bio_lock instead of being an atomic_t so that
10928c2ecf20Sopenharmony_ci	 * completion paths can drop their ref and use the remaining count to
10938c2ecf20Sopenharmony_ci	 * decide to wake the submission path atomically.
10948c2ecf20Sopenharmony_ci	 */
10958c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dio->bio_lock, flags);
10968c2ecf20Sopenharmony_ci	ret2 = --dio->refcount;
10978c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dio->bio_lock, flags);
10988c2ecf20Sopenharmony_ci	return ret2;
10998c2ecf20Sopenharmony_ci}
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci/*
11028c2ecf20Sopenharmony_ci * This is a library function for use by filesystem drivers.
11038c2ecf20Sopenharmony_ci *
11048c2ecf20Sopenharmony_ci * The locking rules are governed by the flags parameter:
11058c2ecf20Sopenharmony_ci *  - if the flags value contains DIO_LOCKING we use a fancy locking
11068c2ecf20Sopenharmony_ci *    scheme for dumb filesystems.
11078c2ecf20Sopenharmony_ci *    For writes this function is called under i_mutex and returns with
11088c2ecf20Sopenharmony_ci *    i_mutex held, for reads, i_mutex is not held on entry, but it is
11098c2ecf20Sopenharmony_ci *    taken and dropped again before returning.
11108c2ecf20Sopenharmony_ci *  - if the flags value does NOT contain DIO_LOCKING we don't use any
11118c2ecf20Sopenharmony_ci *    internal locking but rather rely on the filesystem to synchronize
11128c2ecf20Sopenharmony_ci *    direct I/O reads/writes versus each other and truncate.
11138c2ecf20Sopenharmony_ci *
11148c2ecf20Sopenharmony_ci * To help with locking against truncate we incremented the i_dio_count
11158c2ecf20Sopenharmony_ci * counter before starting direct I/O, and decrement it once we are done.
11168c2ecf20Sopenharmony_ci * Truncate can wait for it to reach zero to provide exclusion.  It is
11178c2ecf20Sopenharmony_ci * expected that filesystem provide exclusion between new direct I/O
11188c2ecf20Sopenharmony_ci * and truncates.  For DIO_LOCKING filesystems this is done by i_mutex,
11198c2ecf20Sopenharmony_ci * but other filesystems need to take care of this on their own.
11208c2ecf20Sopenharmony_ci *
11218c2ecf20Sopenharmony_ci * NOTE: if you pass "sdio" to anything by pointer make sure that function
11228c2ecf20Sopenharmony_ci * is always inlined. Otherwise gcc is unable to split the structure into
11238c2ecf20Sopenharmony_ci * individual fields and will generate much worse code. This is important
11248c2ecf20Sopenharmony_ci * for the whole file.
11258c2ecf20Sopenharmony_ci */
11268c2ecf20Sopenharmony_cistatic inline ssize_t
11278c2ecf20Sopenharmony_cido_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
11288c2ecf20Sopenharmony_ci		      struct block_device *bdev, struct iov_iter *iter,
11298c2ecf20Sopenharmony_ci		      get_block_t get_block, dio_iodone_t end_io,
11308c2ecf20Sopenharmony_ci		      dio_submit_t submit_io, int flags)
11318c2ecf20Sopenharmony_ci{
11328c2ecf20Sopenharmony_ci	unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
11338c2ecf20Sopenharmony_ci	unsigned blkbits = i_blkbits;
11348c2ecf20Sopenharmony_ci	unsigned blocksize_mask = (1 << blkbits) - 1;
11358c2ecf20Sopenharmony_ci	ssize_t retval = -EINVAL;
11368c2ecf20Sopenharmony_ci	const size_t count = iov_iter_count(iter);
11378c2ecf20Sopenharmony_ci	loff_t offset = iocb->ki_pos;
11388c2ecf20Sopenharmony_ci	const loff_t end = offset + count;
11398c2ecf20Sopenharmony_ci	struct dio *dio;
11408c2ecf20Sopenharmony_ci	struct dio_submit sdio = { 0, };
11418c2ecf20Sopenharmony_ci	struct buffer_head map_bh = { 0, };
11428c2ecf20Sopenharmony_ci	struct blk_plug plug;
11438c2ecf20Sopenharmony_ci	unsigned long align = offset | iov_iter_alignment(iter);
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	/*
11468c2ecf20Sopenharmony_ci	 * Avoid references to bdev if not absolutely needed to give
11478c2ecf20Sopenharmony_ci	 * the early prefetch in the caller enough time.
11488c2ecf20Sopenharmony_ci	 */
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	/* watch out for a 0 len io from a tricksy fs */
11518c2ecf20Sopenharmony_ci	if (iov_iter_rw(iter) == READ && !count)
11528c2ecf20Sopenharmony_ci		return 0;
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	dio = kmem_cache_alloc(dio_cache, GFP_KERNEL);
11558c2ecf20Sopenharmony_ci	if (!dio)
11568c2ecf20Sopenharmony_ci		return -ENOMEM;
11578c2ecf20Sopenharmony_ci	/*
11588c2ecf20Sopenharmony_ci	 * Believe it or not, zeroing out the page array caused a .5%
11598c2ecf20Sopenharmony_ci	 * performance regression in a database benchmark.  So, we take
11608c2ecf20Sopenharmony_ci	 * care to only zero out what's needed.
11618c2ecf20Sopenharmony_ci	 */
11628c2ecf20Sopenharmony_ci	memset(dio, 0, offsetof(struct dio, pages));
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	dio->flags = flags;
11658c2ecf20Sopenharmony_ci	if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) {
11668c2ecf20Sopenharmony_ci		/* will be released by direct_io_worker */
11678c2ecf20Sopenharmony_ci		inode_lock(inode);
11688c2ecf20Sopenharmony_ci	}
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	/* Once we sampled i_size check for reads beyond EOF */
11718c2ecf20Sopenharmony_ci	dio->i_size = i_size_read(inode);
11728c2ecf20Sopenharmony_ci	if (iov_iter_rw(iter) == READ && offset >= dio->i_size) {
11738c2ecf20Sopenharmony_ci		retval = 0;
11748c2ecf20Sopenharmony_ci		goto fail_dio;
11758c2ecf20Sopenharmony_ci	}
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	if (align & blocksize_mask) {
11788c2ecf20Sopenharmony_ci		if (bdev)
11798c2ecf20Sopenharmony_ci			blkbits = blksize_bits(bdev_logical_block_size(bdev));
11808c2ecf20Sopenharmony_ci		blocksize_mask = (1 << blkbits) - 1;
11818c2ecf20Sopenharmony_ci		if (align & blocksize_mask)
11828c2ecf20Sopenharmony_ci			goto fail_dio;
11838c2ecf20Sopenharmony_ci	}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) {
11868c2ecf20Sopenharmony_ci		struct address_space *mapping = iocb->ki_filp->f_mapping;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci		retval = filemap_write_and_wait_range(mapping, offset, end - 1);
11898c2ecf20Sopenharmony_ci		if (retval)
11908c2ecf20Sopenharmony_ci			goto fail_dio;
11918c2ecf20Sopenharmony_ci	}
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	/*
11948c2ecf20Sopenharmony_ci	 * For file extending writes updating i_size before data writeouts
11958c2ecf20Sopenharmony_ci	 * complete can expose uninitialized blocks in dumb filesystems.
11968c2ecf20Sopenharmony_ci	 * In that case we need to wait for I/O completion even if asked
11978c2ecf20Sopenharmony_ci	 * for an asynchronous write.
11988c2ecf20Sopenharmony_ci	 */
11998c2ecf20Sopenharmony_ci	if (is_sync_kiocb(iocb))
12008c2ecf20Sopenharmony_ci		dio->is_async = false;
12018c2ecf20Sopenharmony_ci	else if (iov_iter_rw(iter) == WRITE && end > i_size_read(inode))
12028c2ecf20Sopenharmony_ci		dio->is_async = false;
12038c2ecf20Sopenharmony_ci	else
12048c2ecf20Sopenharmony_ci		dio->is_async = true;
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	dio->inode = inode;
12078c2ecf20Sopenharmony_ci	if (iov_iter_rw(iter) == WRITE) {
12088c2ecf20Sopenharmony_ci		dio->op = REQ_OP_WRITE;
12098c2ecf20Sopenharmony_ci		dio->op_flags = REQ_SYNC | REQ_IDLE;
12108c2ecf20Sopenharmony_ci		if (iocb->ki_flags & IOCB_NOWAIT)
12118c2ecf20Sopenharmony_ci			dio->op_flags |= REQ_NOWAIT;
12128c2ecf20Sopenharmony_ci	} else {
12138c2ecf20Sopenharmony_ci		dio->op = REQ_OP_READ;
12148c2ecf20Sopenharmony_ci	}
12158c2ecf20Sopenharmony_ci	if (iocb->ki_flags & IOCB_HIPRI)
12168c2ecf20Sopenharmony_ci		dio->op_flags |= REQ_HIPRI;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	/*
12198c2ecf20Sopenharmony_ci	 * For AIO O_(D)SYNC writes we need to defer completions to a workqueue
12208c2ecf20Sopenharmony_ci	 * so that we can call ->fsync.
12218c2ecf20Sopenharmony_ci	 */
12228c2ecf20Sopenharmony_ci	if (dio->is_async && iov_iter_rw(iter) == WRITE) {
12238c2ecf20Sopenharmony_ci		retval = 0;
12248c2ecf20Sopenharmony_ci		if (iocb->ki_flags & IOCB_DSYNC)
12258c2ecf20Sopenharmony_ci			retval = dio_set_defer_completion(dio);
12268c2ecf20Sopenharmony_ci		else if (!dio->inode->i_sb->s_dio_done_wq) {
12278c2ecf20Sopenharmony_ci			/*
12288c2ecf20Sopenharmony_ci			 * In case of AIO write racing with buffered read we
12298c2ecf20Sopenharmony_ci			 * need to defer completion. We can't decide this now,
12308c2ecf20Sopenharmony_ci			 * however the workqueue needs to be initialized here.
12318c2ecf20Sopenharmony_ci			 */
12328c2ecf20Sopenharmony_ci			retval = sb_init_dio_done_wq(dio->inode->i_sb);
12338c2ecf20Sopenharmony_ci		}
12348c2ecf20Sopenharmony_ci		if (retval)
12358c2ecf20Sopenharmony_ci			goto fail_dio;
12368c2ecf20Sopenharmony_ci	}
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	/*
12398c2ecf20Sopenharmony_ci	 * Will be decremented at I/O completion time.
12408c2ecf20Sopenharmony_ci	 */
12418c2ecf20Sopenharmony_ci	inode_dio_begin(inode);
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	retval = 0;
12448c2ecf20Sopenharmony_ci	sdio.blkbits = blkbits;
12458c2ecf20Sopenharmony_ci	sdio.blkfactor = i_blkbits - blkbits;
12468c2ecf20Sopenharmony_ci	sdio.block_in_file = offset >> blkbits;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	sdio.get_block = get_block;
12498c2ecf20Sopenharmony_ci	dio->end_io = end_io;
12508c2ecf20Sopenharmony_ci	sdio.submit_io = submit_io;
12518c2ecf20Sopenharmony_ci	sdio.final_block_in_bio = -1;
12528c2ecf20Sopenharmony_ci	sdio.next_block_for_io = -1;
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	dio->iocb = iocb;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	spin_lock_init(&dio->bio_lock);
12578c2ecf20Sopenharmony_ci	dio->refcount = 1;
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ;
12608c2ecf20Sopenharmony_ci	sdio.iter = iter;
12618c2ecf20Sopenharmony_ci	sdio.final_block_in_request = end >> blkbits;
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	/*
12648c2ecf20Sopenharmony_ci	 * In case of non-aligned buffers, we may need 2 more
12658c2ecf20Sopenharmony_ci	 * pages since we need to zero out first and last block.
12668c2ecf20Sopenharmony_ci	 */
12678c2ecf20Sopenharmony_ci	if (unlikely(sdio.blkfactor))
12688c2ecf20Sopenharmony_ci		sdio.pages_in_io = 2;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	sdio.pages_in_io += iov_iter_npages(iter, INT_MAX);
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	blk_start_plug(&plug);
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	retval = do_direct_IO(dio, &sdio, &map_bh);
12758c2ecf20Sopenharmony_ci	if (retval)
12768c2ecf20Sopenharmony_ci		dio_cleanup(dio, &sdio);
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	if (retval == -ENOTBLK) {
12798c2ecf20Sopenharmony_ci		/*
12808c2ecf20Sopenharmony_ci		 * The remaining part of the request will be
12818c2ecf20Sopenharmony_ci		 * be handled by buffered I/O when we return
12828c2ecf20Sopenharmony_ci		 */
12838c2ecf20Sopenharmony_ci		retval = 0;
12848c2ecf20Sopenharmony_ci	}
12858c2ecf20Sopenharmony_ci	/*
12868c2ecf20Sopenharmony_ci	 * There may be some unwritten disk at the end of a part-written
12878c2ecf20Sopenharmony_ci	 * fs-block-sized block.  Go zero that now.
12888c2ecf20Sopenharmony_ci	 */
12898c2ecf20Sopenharmony_ci	dio_zero_block(dio, &sdio, 1, &map_bh);
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	if (sdio.cur_page) {
12928c2ecf20Sopenharmony_ci		ssize_t ret2;
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci		ret2 = dio_send_cur_page(dio, &sdio, &map_bh);
12958c2ecf20Sopenharmony_ci		if (retval == 0)
12968c2ecf20Sopenharmony_ci			retval = ret2;
12978c2ecf20Sopenharmony_ci		put_page(sdio.cur_page);
12988c2ecf20Sopenharmony_ci		sdio.cur_page = NULL;
12998c2ecf20Sopenharmony_ci	}
13008c2ecf20Sopenharmony_ci	if (sdio.bio)
13018c2ecf20Sopenharmony_ci		dio_bio_submit(dio, &sdio);
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	blk_finish_plug(&plug);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	/*
13068c2ecf20Sopenharmony_ci	 * It is possible that, we return short IO due to end of file.
13078c2ecf20Sopenharmony_ci	 * In that case, we need to release all the pages we got hold on.
13088c2ecf20Sopenharmony_ci	 */
13098c2ecf20Sopenharmony_ci	dio_cleanup(dio, &sdio);
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	/*
13128c2ecf20Sopenharmony_ci	 * All block lookups have been performed. For READ requests
13138c2ecf20Sopenharmony_ci	 * we can let i_mutex go now that its achieved its purpose
13148c2ecf20Sopenharmony_ci	 * of protecting us from looking up uninitialized blocks.
13158c2ecf20Sopenharmony_ci	 */
13168c2ecf20Sopenharmony_ci	if (iov_iter_rw(iter) == READ && (dio->flags & DIO_LOCKING))
13178c2ecf20Sopenharmony_ci		inode_unlock(dio->inode);
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	/*
13208c2ecf20Sopenharmony_ci	 * The only time we want to leave bios in flight is when a successful
13218c2ecf20Sopenharmony_ci	 * partial aio read or full aio write have been setup.  In that case
13228c2ecf20Sopenharmony_ci	 * bio completion will call aio_complete.  The only time it's safe to
13238c2ecf20Sopenharmony_ci	 * call aio_complete is when we return -EIOCBQUEUED, so we key on that.
13248c2ecf20Sopenharmony_ci	 * This had *better* be the only place that raises -EIOCBQUEUED.
13258c2ecf20Sopenharmony_ci	 */
13268c2ecf20Sopenharmony_ci	BUG_ON(retval == -EIOCBQUEUED);
13278c2ecf20Sopenharmony_ci	if (dio->is_async && retval == 0 && dio->result &&
13288c2ecf20Sopenharmony_ci	    (iov_iter_rw(iter) == READ || dio->result == count))
13298c2ecf20Sopenharmony_ci		retval = -EIOCBQUEUED;
13308c2ecf20Sopenharmony_ci	else
13318c2ecf20Sopenharmony_ci		dio_await_completion(dio);
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	if (drop_refcount(dio) == 0) {
13348c2ecf20Sopenharmony_ci		retval = dio_complete(dio, retval, DIO_COMPLETE_INVALIDATE);
13358c2ecf20Sopenharmony_ci	} else
13368c2ecf20Sopenharmony_ci		BUG_ON(retval != -EIOCBQUEUED);
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	return retval;
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_cifail_dio:
13418c2ecf20Sopenharmony_ci	if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ)
13428c2ecf20Sopenharmony_ci		inode_unlock(inode);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	kmem_cache_free(dio_cache, dio);
13458c2ecf20Sopenharmony_ci	return retval;
13468c2ecf20Sopenharmony_ci}
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_cissize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
13498c2ecf20Sopenharmony_ci			     struct block_device *bdev, struct iov_iter *iter,
13508c2ecf20Sopenharmony_ci			     get_block_t get_block,
13518c2ecf20Sopenharmony_ci			     dio_iodone_t end_io, dio_submit_t submit_io,
13528c2ecf20Sopenharmony_ci			     int flags)
13538c2ecf20Sopenharmony_ci{
13548c2ecf20Sopenharmony_ci	/*
13558c2ecf20Sopenharmony_ci	 * The block device state is needed in the end to finally
13568c2ecf20Sopenharmony_ci	 * submit everything.  Since it's likely to be cache cold
13578c2ecf20Sopenharmony_ci	 * prefetch it here as first thing to hide some of the
13588c2ecf20Sopenharmony_ci	 * latency.
13598c2ecf20Sopenharmony_ci	 *
13608c2ecf20Sopenharmony_ci	 * Attempt to prefetch the pieces we likely need later.
13618c2ecf20Sopenharmony_ci	 */
13628c2ecf20Sopenharmony_ci	prefetch(&bdev->bd_disk->part_tbl);
13638c2ecf20Sopenharmony_ci	prefetch(bdev->bd_disk->queue);
13648c2ecf20Sopenharmony_ci	prefetch((char *)bdev->bd_disk->queue + SMP_CACHE_BYTES);
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_ci	return do_blockdev_direct_IO(iocb, inode, bdev, iter, get_block,
13678c2ecf20Sopenharmony_ci				     end_io, submit_io, flags);
13688c2ecf20Sopenharmony_ci}
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__blockdev_direct_IO);
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_cistatic __init int dio_init(void)
13738c2ecf20Sopenharmony_ci{
13748c2ecf20Sopenharmony_ci	dio_cache = KMEM_CACHE(dio, SLAB_PANIC);
13758c2ecf20Sopenharmony_ci	return 0;
13768c2ecf20Sopenharmony_ci}
13778c2ecf20Sopenharmony_cimodule_init(dio_init)
1378