xref: /OK3568_Linux_fs/kernel/fs/direct-io.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * fs/direct-io.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2002, Linus Torvalds.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * O_DIRECT
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * 04Jul2002	Andrew Morton
10*4882a593Smuzhiyun  *		Initial version
11*4882a593Smuzhiyun  * 11Sep2002	janetinc@us.ibm.com
12*4882a593Smuzhiyun  * 		added readv/writev support.
13*4882a593Smuzhiyun  * 29Oct2002	Andrew Morton
14*4882a593Smuzhiyun  *		rewrote bio_add_page() support.
15*4882a593Smuzhiyun  * 30Oct2002	pbadari@us.ibm.com
16*4882a593Smuzhiyun  *		added support for non-aligned IO.
17*4882a593Smuzhiyun  * 06Nov2002	pbadari@us.ibm.com
18*4882a593Smuzhiyun  *		added asynchronous IO support.
19*4882a593Smuzhiyun  * 21Jul2003	nathans@sgi.com
20*4882a593Smuzhiyun  *		added IO completion notifier.
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/module.h>
25*4882a593Smuzhiyun #include <linux/types.h>
26*4882a593Smuzhiyun #include <linux/fs.h>
27*4882a593Smuzhiyun #include <linux/fscrypt.h>
28*4882a593Smuzhiyun #include <linux/mm.h>
29*4882a593Smuzhiyun #include <linux/slab.h>
30*4882a593Smuzhiyun #include <linux/highmem.h>
31*4882a593Smuzhiyun #include <linux/pagemap.h>
32*4882a593Smuzhiyun #include <linux/task_io_accounting_ops.h>
33*4882a593Smuzhiyun #include <linux/bio.h>
34*4882a593Smuzhiyun #include <linux/wait.h>
35*4882a593Smuzhiyun #include <linux/err.h>
36*4882a593Smuzhiyun #include <linux/blkdev.h>
37*4882a593Smuzhiyun #include <linux/buffer_head.h>
38*4882a593Smuzhiyun #include <linux/rwsem.h>
39*4882a593Smuzhiyun #include <linux/uio.h>
40*4882a593Smuzhiyun #include <linux/atomic.h>
41*4882a593Smuzhiyun #include <linux/prefetch.h>
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #include "internal.h"
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * How many user pages to map in one call to get_user_pages().  This determines
47*4882a593Smuzhiyun  * the size of a structure in the slab cache
48*4882a593Smuzhiyun  */
49*4882a593Smuzhiyun #define DIO_PAGES	64
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun  * Flags for dio_complete()
53*4882a593Smuzhiyun  */
54*4882a593Smuzhiyun #define DIO_COMPLETE_ASYNC		0x01	/* This is async IO */
55*4882a593Smuzhiyun #define DIO_COMPLETE_INVALIDATE		0x02	/* Can invalidate pages */
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun  * This code generally works in units of "dio_blocks".  A dio_block is
59*4882a593Smuzhiyun  * somewhere between the hard sector size and the filesystem block size.  it
60*4882a593Smuzhiyun  * is determined on a per-invocation basis.   When talking to the filesystem
61*4882a593Smuzhiyun  * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
62*4882a593Smuzhiyun  * down by dio->blkfactor.  Similarly, fs-blocksize quantities are converted
63*4882a593Smuzhiyun  * to bio_block quantities by shifting left by blkfactor.
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * If blkfactor is zero then the user's request was aligned to the filesystem's
66*4882a593Smuzhiyun  * blocksize.
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* dio_state only used in the submission path */
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun struct dio_submit {
72*4882a593Smuzhiyun 	struct bio *bio;		/* bio under assembly */
73*4882a593Smuzhiyun 	unsigned blkbits;		/* doesn't change */
74*4882a593Smuzhiyun 	unsigned blkfactor;		/* When we're using an alignment which
75*4882a593Smuzhiyun 					   is finer than the filesystem's soft
76*4882a593Smuzhiyun 					   blocksize, this specifies how much
77*4882a593Smuzhiyun 					   finer.  blkfactor=2 means 1/4-block
78*4882a593Smuzhiyun 					   alignment.  Does not change */
79*4882a593Smuzhiyun 	unsigned start_zero_done;	/* flag: sub-blocksize zeroing has
80*4882a593Smuzhiyun 					   been performed at the start of a
81*4882a593Smuzhiyun 					   write */
82*4882a593Smuzhiyun 	int pages_in_io;		/* approximate total IO pages */
83*4882a593Smuzhiyun 	sector_t block_in_file;		/* Current offset into the underlying
84*4882a593Smuzhiyun 					   file in dio_block units. */
85*4882a593Smuzhiyun 	unsigned blocks_available;	/* At block_in_file.  changes */
86*4882a593Smuzhiyun 	int reap_counter;		/* rate limit reaping */
87*4882a593Smuzhiyun 	sector_t final_block_in_request;/* doesn't change */
88*4882a593Smuzhiyun 	int boundary;			/* prev block is at a boundary */
89*4882a593Smuzhiyun 	get_block_t *get_block;		/* block mapping function */
90*4882a593Smuzhiyun 	dio_submit_t *submit_io;	/* IO submition function */
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	loff_t logical_offset_in_bio;	/* current first logical block in bio */
93*4882a593Smuzhiyun 	sector_t final_block_in_bio;	/* current final block in bio + 1 */
94*4882a593Smuzhiyun 	sector_t next_block_for_io;	/* next block to be put under IO,
95*4882a593Smuzhiyun 					   in dio_blocks units */
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/*
98*4882a593Smuzhiyun 	 * Deferred addition of a page to the dio.  These variables are
99*4882a593Smuzhiyun 	 * private to dio_send_cur_page(), submit_page_section() and
100*4882a593Smuzhiyun 	 * dio_bio_add_page().
101*4882a593Smuzhiyun 	 */
102*4882a593Smuzhiyun 	struct page *cur_page;		/* The page */
103*4882a593Smuzhiyun 	unsigned cur_page_offset;	/* Offset into it, in bytes */
104*4882a593Smuzhiyun 	unsigned cur_page_len;		/* Nr of bytes at cur_page_offset */
105*4882a593Smuzhiyun 	sector_t cur_page_block;	/* Where it starts */
106*4882a593Smuzhiyun 	loff_t cur_page_fs_offset;	/* Offset in file */
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	struct iov_iter *iter;
109*4882a593Smuzhiyun 	/*
110*4882a593Smuzhiyun 	 * Page queue.  These variables belong to dio_refill_pages() and
111*4882a593Smuzhiyun 	 * dio_get_page().
112*4882a593Smuzhiyun 	 */
113*4882a593Smuzhiyun 	unsigned head;			/* next page to process */
114*4882a593Smuzhiyun 	unsigned tail;			/* last valid page + 1 */
115*4882a593Smuzhiyun 	size_t from, to;
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /* dio_state communicated between submission path and end_io */
119*4882a593Smuzhiyun struct dio {
120*4882a593Smuzhiyun 	int flags;			/* doesn't change */
121*4882a593Smuzhiyun 	int op;
122*4882a593Smuzhiyun 	int op_flags;
123*4882a593Smuzhiyun 	blk_qc_t bio_cookie;
124*4882a593Smuzhiyun 	struct gendisk *bio_disk;
125*4882a593Smuzhiyun 	struct inode *inode;
126*4882a593Smuzhiyun 	loff_t i_size;			/* i_size when submitted */
127*4882a593Smuzhiyun 	dio_iodone_t *end_io;		/* IO completion function */
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	void *private;			/* copy from map_bh.b_private */
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	/* BIO completion state */
132*4882a593Smuzhiyun 	spinlock_t bio_lock;		/* protects BIO fields below */
133*4882a593Smuzhiyun 	int page_errors;		/* errno from get_user_pages() */
134*4882a593Smuzhiyun 	int is_async;			/* is IO async ? */
135*4882a593Smuzhiyun 	bool defer_completion;		/* defer AIO completion to workqueue? */
136*4882a593Smuzhiyun 	bool should_dirty;		/* if pages should be dirtied */
137*4882a593Smuzhiyun 	int io_error;			/* IO error in completion path */
138*4882a593Smuzhiyun 	unsigned long refcount;		/* direct_io_worker() and bios */
139*4882a593Smuzhiyun 	struct bio *bio_list;		/* singly linked via bi_private */
140*4882a593Smuzhiyun 	struct task_struct *waiter;	/* waiting task (NULL if none) */
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* AIO related stuff */
143*4882a593Smuzhiyun 	struct kiocb *iocb;		/* kiocb */
144*4882a593Smuzhiyun 	ssize_t result;                 /* IO result */
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/*
147*4882a593Smuzhiyun 	 * pages[] (and any fields placed after it) are not zeroed out at
148*4882a593Smuzhiyun 	 * allocation time.  Don't add new fields after pages[] unless you
149*4882a593Smuzhiyun 	 * wish that they not be zeroed.
150*4882a593Smuzhiyun 	 */
151*4882a593Smuzhiyun 	union {
152*4882a593Smuzhiyun 		struct page *pages[DIO_PAGES];	/* page buffer */
153*4882a593Smuzhiyun 		struct work_struct complete_work;/* deferred AIO completion */
154*4882a593Smuzhiyun 	};
155*4882a593Smuzhiyun } ____cacheline_aligned_in_smp;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun static struct kmem_cache *dio_cache __read_mostly;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /*
160*4882a593Smuzhiyun  * How many pages are in the queue?
161*4882a593Smuzhiyun  */
dio_pages_present(struct dio_submit * sdio)162*4882a593Smuzhiyun static inline unsigned dio_pages_present(struct dio_submit *sdio)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	return sdio->tail - sdio->head;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /*
168*4882a593Smuzhiyun  * Go grab and pin some userspace pages.   Typically we'll get 64 at a time.
169*4882a593Smuzhiyun  */
dio_refill_pages(struct dio * dio,struct dio_submit * sdio)170*4882a593Smuzhiyun static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	ssize_t ret;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	ret = iov_iter_get_pages(sdio->iter, dio->pages, LONG_MAX, DIO_PAGES,
175*4882a593Smuzhiyun 				&sdio->from);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	if (ret < 0 && sdio->blocks_available && (dio->op == REQ_OP_WRITE)) {
178*4882a593Smuzhiyun 		struct page *page = ZERO_PAGE(0);
179*4882a593Smuzhiyun 		/*
180*4882a593Smuzhiyun 		 * A memory fault, but the filesystem has some outstanding
181*4882a593Smuzhiyun 		 * mapped blocks.  We need to use those blocks up to avoid
182*4882a593Smuzhiyun 		 * leaking stale data in the file.
183*4882a593Smuzhiyun 		 */
184*4882a593Smuzhiyun 		if (dio->page_errors == 0)
185*4882a593Smuzhiyun 			dio->page_errors = ret;
186*4882a593Smuzhiyun 		get_page(page);
187*4882a593Smuzhiyun 		dio->pages[0] = page;
188*4882a593Smuzhiyun 		sdio->head = 0;
189*4882a593Smuzhiyun 		sdio->tail = 1;
190*4882a593Smuzhiyun 		sdio->from = 0;
191*4882a593Smuzhiyun 		sdio->to = PAGE_SIZE;
192*4882a593Smuzhiyun 		return 0;
193*4882a593Smuzhiyun 	}
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	if (ret >= 0) {
196*4882a593Smuzhiyun 		iov_iter_advance(sdio->iter, ret);
197*4882a593Smuzhiyun 		ret += sdio->from;
198*4882a593Smuzhiyun 		sdio->head = 0;
199*4882a593Smuzhiyun 		sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
200*4882a593Smuzhiyun 		sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1;
201*4882a593Smuzhiyun 		return 0;
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun 	return ret;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /*
207*4882a593Smuzhiyun  * Get another userspace page.  Returns an ERR_PTR on error.  Pages are
208*4882a593Smuzhiyun  * buffered inside the dio so that we can call get_user_pages() against a
209*4882a593Smuzhiyun  * decent number of pages, less frequently.  To provide nicer use of the
210*4882a593Smuzhiyun  * L1 cache.
211*4882a593Smuzhiyun  */
dio_get_page(struct dio * dio,struct dio_submit * sdio)212*4882a593Smuzhiyun static inline struct page *dio_get_page(struct dio *dio,
213*4882a593Smuzhiyun 					struct dio_submit *sdio)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	if (dio_pages_present(sdio) == 0) {
216*4882a593Smuzhiyun 		int ret;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 		ret = dio_refill_pages(dio, sdio);
219*4882a593Smuzhiyun 		if (ret)
220*4882a593Smuzhiyun 			return ERR_PTR(ret);
221*4882a593Smuzhiyun 		BUG_ON(dio_pages_present(sdio) == 0);
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 	return dio->pages[sdio->head];
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * dio_complete() - called when all DIO BIO I/O has been completed
228*4882a593Smuzhiyun  *
229*4882a593Smuzhiyun  * This drops i_dio_count, lets interested parties know that a DIO operation
230*4882a593Smuzhiyun  * has completed, and calculates the resulting return code for the operation.
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  * It lets the filesystem know if it registered an interest earlier via
233*4882a593Smuzhiyun  * get_block.  Pass the private field of the map buffer_head so that
234*4882a593Smuzhiyun  * filesystems can use it to hold additional state between get_block calls and
235*4882a593Smuzhiyun  * dio_complete.
236*4882a593Smuzhiyun  */
dio_complete(struct dio * dio,ssize_t ret,unsigned int flags)237*4882a593Smuzhiyun static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	loff_t offset = dio->iocb->ki_pos;
240*4882a593Smuzhiyun 	ssize_t transferred = 0;
241*4882a593Smuzhiyun 	int err;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/*
244*4882a593Smuzhiyun 	 * AIO submission can race with bio completion to get here while
245*4882a593Smuzhiyun 	 * expecting to have the last io completed by bio completion.
246*4882a593Smuzhiyun 	 * In that case -EIOCBQUEUED is in fact not an error we want
247*4882a593Smuzhiyun 	 * to preserve through this call.
248*4882a593Smuzhiyun 	 */
249*4882a593Smuzhiyun 	if (ret == -EIOCBQUEUED)
250*4882a593Smuzhiyun 		ret = 0;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	if (dio->result) {
253*4882a593Smuzhiyun 		transferred = dio->result;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 		/* Check for short read case */
256*4882a593Smuzhiyun 		if ((dio->op == REQ_OP_READ) &&
257*4882a593Smuzhiyun 		    ((offset + transferred) > dio->i_size))
258*4882a593Smuzhiyun 			transferred = dio->i_size - offset;
259*4882a593Smuzhiyun 		/* ignore EFAULT if some IO has been done */
260*4882a593Smuzhiyun 		if (unlikely(ret == -EFAULT) && transferred)
261*4882a593Smuzhiyun 			ret = 0;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	if (ret == 0)
265*4882a593Smuzhiyun 		ret = dio->page_errors;
266*4882a593Smuzhiyun 	if (ret == 0)
267*4882a593Smuzhiyun 		ret = dio->io_error;
268*4882a593Smuzhiyun 	if (ret == 0)
269*4882a593Smuzhiyun 		ret = transferred;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	if (dio->end_io) {
272*4882a593Smuzhiyun 		// XXX: ki_pos??
273*4882a593Smuzhiyun 		err = dio->end_io(dio->iocb, offset, ret, dio->private);
274*4882a593Smuzhiyun 		if (err)
275*4882a593Smuzhiyun 			ret = err;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	/*
279*4882a593Smuzhiyun 	 * Try again to invalidate clean pages which might have been cached by
280*4882a593Smuzhiyun 	 * non-direct readahead, or faulted in by get_user_pages() if the source
281*4882a593Smuzhiyun 	 * of the write was an mmap'ed region of the file we're writing.  Either
282*4882a593Smuzhiyun 	 * one is a pretty crazy thing to do, so we don't support it 100%.  If
283*4882a593Smuzhiyun 	 * this invalidation fails, tough, the write still worked...
284*4882a593Smuzhiyun 	 *
285*4882a593Smuzhiyun 	 * And this page cache invalidation has to be after dio->end_io(), as
286*4882a593Smuzhiyun 	 * some filesystems convert unwritten extents to real allocations in
287*4882a593Smuzhiyun 	 * end_io() when necessary, otherwise a racing buffer read would cache
288*4882a593Smuzhiyun 	 * zeros from unwritten extents.
289*4882a593Smuzhiyun 	 */
290*4882a593Smuzhiyun 	if (flags & DIO_COMPLETE_INVALIDATE &&
291*4882a593Smuzhiyun 	    ret > 0 && dio->op == REQ_OP_WRITE &&
292*4882a593Smuzhiyun 	    dio->inode->i_mapping->nrpages) {
293*4882a593Smuzhiyun 		err = invalidate_inode_pages2_range(dio->inode->i_mapping,
294*4882a593Smuzhiyun 					offset >> PAGE_SHIFT,
295*4882a593Smuzhiyun 					(offset + ret - 1) >> PAGE_SHIFT);
296*4882a593Smuzhiyun 		if (err)
297*4882a593Smuzhiyun 			dio_warn_stale_pagecache(dio->iocb->ki_filp);
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	inode_dio_end(dio->inode);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	if (flags & DIO_COMPLETE_ASYNC) {
303*4882a593Smuzhiyun 		/*
304*4882a593Smuzhiyun 		 * generic_write_sync expects ki_pos to have been updated
305*4882a593Smuzhiyun 		 * already, but the submission path only does this for
306*4882a593Smuzhiyun 		 * synchronous I/O.
307*4882a593Smuzhiyun 		 */
308*4882a593Smuzhiyun 		dio->iocb->ki_pos += transferred;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 		if (ret > 0 && dio->op == REQ_OP_WRITE)
311*4882a593Smuzhiyun 			ret = generic_write_sync(dio->iocb, ret);
312*4882a593Smuzhiyun 		dio->iocb->ki_complete(dio->iocb, ret, 0);
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	kmem_cache_free(dio_cache, dio);
316*4882a593Smuzhiyun 	return ret;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
dio_aio_complete_work(struct work_struct * work)319*4882a593Smuzhiyun static void dio_aio_complete_work(struct work_struct *work)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	struct dio *dio = container_of(work, struct dio, complete_work);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	dio_complete(dio, 0, DIO_COMPLETE_ASYNC | DIO_COMPLETE_INVALIDATE);
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /*
329*4882a593Smuzhiyun  * Asynchronous IO callback.
330*4882a593Smuzhiyun  */
dio_bio_end_aio(struct bio * bio)331*4882a593Smuzhiyun static void dio_bio_end_aio(struct bio *bio)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	struct dio *dio = bio->bi_private;
334*4882a593Smuzhiyun 	unsigned long remaining;
335*4882a593Smuzhiyun 	unsigned long flags;
336*4882a593Smuzhiyun 	bool defer_completion = false;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	/* cleanup the bio */
339*4882a593Smuzhiyun 	dio_bio_complete(dio, bio);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	spin_lock_irqsave(&dio->bio_lock, flags);
342*4882a593Smuzhiyun 	remaining = --dio->refcount;
343*4882a593Smuzhiyun 	if (remaining == 1 && dio->waiter)
344*4882a593Smuzhiyun 		wake_up_process(dio->waiter);
345*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dio->bio_lock, flags);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	if (remaining == 0) {
348*4882a593Smuzhiyun 		/*
349*4882a593Smuzhiyun 		 * Defer completion when defer_completion is set or
350*4882a593Smuzhiyun 		 * when the inode has pages mapped and this is AIO write.
351*4882a593Smuzhiyun 		 * We need to invalidate those pages because there is a
352*4882a593Smuzhiyun 		 * chance they contain stale data in the case buffered IO
353*4882a593Smuzhiyun 		 * went in between AIO submission and completion into the
354*4882a593Smuzhiyun 		 * same region.
355*4882a593Smuzhiyun 		 */
356*4882a593Smuzhiyun 		if (dio->result)
357*4882a593Smuzhiyun 			defer_completion = dio->defer_completion ||
358*4882a593Smuzhiyun 					   (dio->op == REQ_OP_WRITE &&
359*4882a593Smuzhiyun 					    dio->inode->i_mapping->nrpages);
360*4882a593Smuzhiyun 		if (defer_completion) {
361*4882a593Smuzhiyun 			INIT_WORK(&dio->complete_work, dio_aio_complete_work);
362*4882a593Smuzhiyun 			queue_work(dio->inode->i_sb->s_dio_done_wq,
363*4882a593Smuzhiyun 				   &dio->complete_work);
364*4882a593Smuzhiyun 		} else {
365*4882a593Smuzhiyun 			dio_complete(dio, 0, DIO_COMPLETE_ASYNC);
366*4882a593Smuzhiyun 		}
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun  * The BIO completion handler simply queues the BIO up for the process-context
372*4882a593Smuzhiyun  * handler.
373*4882a593Smuzhiyun  *
374*4882a593Smuzhiyun  * During I/O bi_private points at the dio.  After I/O, bi_private is used to
375*4882a593Smuzhiyun  * implement a singly-linked list of completed BIOs, at dio->bio_list.
376*4882a593Smuzhiyun  */
dio_bio_end_io(struct bio * bio)377*4882a593Smuzhiyun static void dio_bio_end_io(struct bio *bio)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	struct dio *dio = bio->bi_private;
380*4882a593Smuzhiyun 	unsigned long flags;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	spin_lock_irqsave(&dio->bio_lock, flags);
383*4882a593Smuzhiyun 	bio->bi_private = dio->bio_list;
384*4882a593Smuzhiyun 	dio->bio_list = bio;
385*4882a593Smuzhiyun 	if (--dio->refcount == 1 && dio->waiter)
386*4882a593Smuzhiyun 		wake_up_process(dio->waiter);
387*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dio->bio_lock, flags);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun static inline void
dio_bio_alloc(struct dio * dio,struct dio_submit * sdio,struct block_device * bdev,sector_t first_sector,int nr_vecs)391*4882a593Smuzhiyun dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
392*4882a593Smuzhiyun 	      struct block_device *bdev,
393*4882a593Smuzhiyun 	      sector_t first_sector, int nr_vecs)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	struct bio *bio;
396*4882a593Smuzhiyun 	struct inode *inode = dio->inode;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	/*
399*4882a593Smuzhiyun 	 * bio_alloc() is guaranteed to return a bio when allowed to sleep and
400*4882a593Smuzhiyun 	 * we request a valid number of vectors.
401*4882a593Smuzhiyun 	 */
402*4882a593Smuzhiyun 	bio = bio_alloc(GFP_KERNEL, nr_vecs);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	fscrypt_set_bio_crypt_ctx(bio, inode,
405*4882a593Smuzhiyun 				  sdio->cur_page_fs_offset >> inode->i_blkbits,
406*4882a593Smuzhiyun 				  GFP_KERNEL);
407*4882a593Smuzhiyun 	bio_set_dev(bio, bdev);
408*4882a593Smuzhiyun 	bio->bi_iter.bi_sector = first_sector;
409*4882a593Smuzhiyun 	bio_set_op_attrs(bio, dio->op, dio->op_flags);
410*4882a593Smuzhiyun 	if (dio->is_async)
411*4882a593Smuzhiyun 		bio->bi_end_io = dio_bio_end_aio;
412*4882a593Smuzhiyun 	else
413*4882a593Smuzhiyun 		bio->bi_end_io = dio_bio_end_io;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	bio->bi_write_hint = dio->iocb->ki_hint;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	sdio->bio = bio;
418*4882a593Smuzhiyun 	sdio->logical_offset_in_bio = sdio->cur_page_fs_offset;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun /*
422*4882a593Smuzhiyun  * In the AIO read case we speculatively dirty the pages before starting IO.
423*4882a593Smuzhiyun  * During IO completion, any of these pages which happen to have been written
424*4882a593Smuzhiyun  * back will be redirtied by bio_check_pages_dirty().
425*4882a593Smuzhiyun  *
426*4882a593Smuzhiyun  * bios hold a dio reference between submit_bio and ->end_io.
427*4882a593Smuzhiyun  */
dio_bio_submit(struct dio * dio,struct dio_submit * sdio)428*4882a593Smuzhiyun static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	struct bio *bio = sdio->bio;
431*4882a593Smuzhiyun 	unsigned long flags;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	bio->bi_private = dio;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	spin_lock_irqsave(&dio->bio_lock, flags);
436*4882a593Smuzhiyun 	dio->refcount++;
437*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dio->bio_lock, flags);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty)
440*4882a593Smuzhiyun 		bio_set_pages_dirty(bio);
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	dio->bio_disk = bio->bi_disk;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	if (sdio->submit_io) {
445*4882a593Smuzhiyun 		sdio->submit_io(bio, dio->inode, sdio->logical_offset_in_bio);
446*4882a593Smuzhiyun 		dio->bio_cookie = BLK_QC_T_NONE;
447*4882a593Smuzhiyun 	} else
448*4882a593Smuzhiyun 		dio->bio_cookie = submit_bio(bio);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	sdio->bio = NULL;
451*4882a593Smuzhiyun 	sdio->boundary = 0;
452*4882a593Smuzhiyun 	sdio->logical_offset_in_bio = 0;
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun  * Release any resources in case of a failure
457*4882a593Smuzhiyun  */
dio_cleanup(struct dio * dio,struct dio_submit * sdio)458*4882a593Smuzhiyun static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	while (sdio->head < sdio->tail)
461*4882a593Smuzhiyun 		put_page(dio->pages[sdio->head++]);
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /*
465*4882a593Smuzhiyun  * Wait for the next BIO to complete.  Remove it and return it.  NULL is
466*4882a593Smuzhiyun  * returned once all BIOs have been completed.  This must only be called once
467*4882a593Smuzhiyun  * all bios have been issued so that dio->refcount can only decrease.  This
468*4882a593Smuzhiyun  * requires that that the caller hold a reference on the dio.
469*4882a593Smuzhiyun  */
dio_await_one(struct dio * dio)470*4882a593Smuzhiyun static struct bio *dio_await_one(struct dio *dio)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	unsigned long flags;
473*4882a593Smuzhiyun 	struct bio *bio = NULL;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	spin_lock_irqsave(&dio->bio_lock, flags);
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	/*
478*4882a593Smuzhiyun 	 * Wait as long as the list is empty and there are bios in flight.  bio
479*4882a593Smuzhiyun 	 * completion drops the count, maybe adds to the list, and wakes while
480*4882a593Smuzhiyun 	 * holding the bio_lock so we don't need set_current_state()'s barrier
481*4882a593Smuzhiyun 	 * and can call it after testing our condition.
482*4882a593Smuzhiyun 	 */
483*4882a593Smuzhiyun 	while (dio->refcount > 1 && dio->bio_list == NULL) {
484*4882a593Smuzhiyun 		__set_current_state(TASK_UNINTERRUPTIBLE);
485*4882a593Smuzhiyun 		dio->waiter = current;
486*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dio->bio_lock, flags);
487*4882a593Smuzhiyun 		if (!(dio->iocb->ki_flags & IOCB_HIPRI) ||
488*4882a593Smuzhiyun 		    !blk_poll(dio->bio_disk->queue, dio->bio_cookie, true))
489*4882a593Smuzhiyun 			blk_io_schedule();
490*4882a593Smuzhiyun 		/* wake up sets us TASK_RUNNING */
491*4882a593Smuzhiyun 		spin_lock_irqsave(&dio->bio_lock, flags);
492*4882a593Smuzhiyun 		dio->waiter = NULL;
493*4882a593Smuzhiyun 	}
494*4882a593Smuzhiyun 	if (dio->bio_list) {
495*4882a593Smuzhiyun 		bio = dio->bio_list;
496*4882a593Smuzhiyun 		dio->bio_list = bio->bi_private;
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dio->bio_lock, flags);
499*4882a593Smuzhiyun 	return bio;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun /*
503*4882a593Smuzhiyun  * Process one completed BIO.  No locks are held.
504*4882a593Smuzhiyun  */
dio_bio_complete(struct dio * dio,struct bio * bio)505*4882a593Smuzhiyun static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	blk_status_t err = bio->bi_status;
508*4882a593Smuzhiyun 	bool should_dirty = dio->op == REQ_OP_READ && dio->should_dirty;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	if (err) {
511*4882a593Smuzhiyun 		if (err == BLK_STS_AGAIN && (bio->bi_opf & REQ_NOWAIT))
512*4882a593Smuzhiyun 			dio->io_error = -EAGAIN;
513*4882a593Smuzhiyun 		else
514*4882a593Smuzhiyun 			dio->io_error = -EIO;
515*4882a593Smuzhiyun 	}
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	if (dio->is_async && should_dirty) {
518*4882a593Smuzhiyun 		bio_check_pages_dirty(bio);	/* transfers ownership */
519*4882a593Smuzhiyun 	} else {
520*4882a593Smuzhiyun 		bio_release_pages(bio, should_dirty);
521*4882a593Smuzhiyun 		bio_put(bio);
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun 	return err;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun  * Wait on and process all in-flight BIOs.  This must only be called once
528*4882a593Smuzhiyun  * all bios have been issued so that the refcount can only decrease.
529*4882a593Smuzhiyun  * This just waits for all bios to make it through dio_bio_complete.  IO
530*4882a593Smuzhiyun  * errors are propagated through dio->io_error and should be propagated via
531*4882a593Smuzhiyun  * dio_complete().
532*4882a593Smuzhiyun  */
dio_await_completion(struct dio * dio)533*4882a593Smuzhiyun static void dio_await_completion(struct dio *dio)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun 	struct bio *bio;
536*4882a593Smuzhiyun 	do {
537*4882a593Smuzhiyun 		bio = dio_await_one(dio);
538*4882a593Smuzhiyun 		if (bio)
539*4882a593Smuzhiyun 			dio_bio_complete(dio, bio);
540*4882a593Smuzhiyun 	} while (bio);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun  * A really large O_DIRECT read or write can generate a lot of BIOs.  So
545*4882a593Smuzhiyun  * to keep the memory consumption sane we periodically reap any completed BIOs
546*4882a593Smuzhiyun  * during the BIO generation phase.
547*4882a593Smuzhiyun  *
548*4882a593Smuzhiyun  * This also helps to limit the peak amount of pinned userspace memory.
549*4882a593Smuzhiyun  */
dio_bio_reap(struct dio * dio,struct dio_submit * sdio)550*4882a593Smuzhiyun static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	int ret = 0;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	if (sdio->reap_counter++ >= 64) {
555*4882a593Smuzhiyun 		while (dio->bio_list) {
556*4882a593Smuzhiyun 			unsigned long flags;
557*4882a593Smuzhiyun 			struct bio *bio;
558*4882a593Smuzhiyun 			int ret2;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 			spin_lock_irqsave(&dio->bio_lock, flags);
561*4882a593Smuzhiyun 			bio = dio->bio_list;
562*4882a593Smuzhiyun 			dio->bio_list = bio->bi_private;
563*4882a593Smuzhiyun 			spin_unlock_irqrestore(&dio->bio_lock, flags);
564*4882a593Smuzhiyun 			ret2 = blk_status_to_errno(dio_bio_complete(dio, bio));
565*4882a593Smuzhiyun 			if (ret == 0)
566*4882a593Smuzhiyun 				ret = ret2;
567*4882a593Smuzhiyun 		}
568*4882a593Smuzhiyun 		sdio->reap_counter = 0;
569*4882a593Smuzhiyun 	}
570*4882a593Smuzhiyun 	return ret;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun /*
574*4882a593Smuzhiyun  * Create workqueue for deferred direct IO completions. We allocate the
575*4882a593Smuzhiyun  * workqueue when it's first needed. This avoids creating workqueue for
576*4882a593Smuzhiyun  * filesystems that don't need it and also allows us to create the workqueue
577*4882a593Smuzhiyun  * late enough so the we can include s_id in the name of the workqueue.
578*4882a593Smuzhiyun  */
sb_init_dio_done_wq(struct super_block * sb)579*4882a593Smuzhiyun int sb_init_dio_done_wq(struct super_block *sb)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun 	struct workqueue_struct *old;
582*4882a593Smuzhiyun 	struct workqueue_struct *wq = alloc_workqueue("dio/%s",
583*4882a593Smuzhiyun 						      WQ_MEM_RECLAIM, 0,
584*4882a593Smuzhiyun 						      sb->s_id);
585*4882a593Smuzhiyun 	if (!wq)
586*4882a593Smuzhiyun 		return -ENOMEM;
587*4882a593Smuzhiyun 	/*
588*4882a593Smuzhiyun 	 * This has to be atomic as more DIOs can race to create the workqueue
589*4882a593Smuzhiyun 	 */
590*4882a593Smuzhiyun 	old = cmpxchg(&sb->s_dio_done_wq, NULL, wq);
591*4882a593Smuzhiyun 	/* Someone created workqueue before us? Free ours... */
592*4882a593Smuzhiyun 	if (old)
593*4882a593Smuzhiyun 		destroy_workqueue(wq);
594*4882a593Smuzhiyun 	return 0;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun 
dio_set_defer_completion(struct dio * dio)597*4882a593Smuzhiyun static int dio_set_defer_completion(struct dio *dio)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun 	struct super_block *sb = dio->inode->i_sb;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	if (dio->defer_completion)
602*4882a593Smuzhiyun 		return 0;
603*4882a593Smuzhiyun 	dio->defer_completion = true;
604*4882a593Smuzhiyun 	if (!sb->s_dio_done_wq)
605*4882a593Smuzhiyun 		return sb_init_dio_done_wq(sb);
606*4882a593Smuzhiyun 	return 0;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun /*
610*4882a593Smuzhiyun  * Call into the fs to map some more disk blocks.  We record the current number
611*4882a593Smuzhiyun  * of available blocks at sdio->blocks_available.  These are in units of the
612*4882a593Smuzhiyun  * fs blocksize, i_blocksize(inode).
613*4882a593Smuzhiyun  *
614*4882a593Smuzhiyun  * The fs is allowed to map lots of blocks at once.  If it wants to do that,
615*4882a593Smuzhiyun  * it uses the passed inode-relative block number as the file offset, as usual.
616*4882a593Smuzhiyun  *
617*4882a593Smuzhiyun  * get_block() is passed the number of i_blkbits-sized blocks which direct_io
618*4882a593Smuzhiyun  * has remaining to do.  The fs should not map more than this number of blocks.
619*4882a593Smuzhiyun  *
620*4882a593Smuzhiyun  * If the fs has mapped a lot of blocks, it should populate bh->b_size to
621*4882a593Smuzhiyun  * indicate how much contiguous disk space has been made available at
622*4882a593Smuzhiyun  * bh->b_blocknr.
623*4882a593Smuzhiyun  *
624*4882a593Smuzhiyun  * If *any* of the mapped blocks are new, then the fs must set buffer_new().
625*4882a593Smuzhiyun  * This isn't very efficient...
626*4882a593Smuzhiyun  *
627*4882a593Smuzhiyun  * In the case of filesystem holes: the fs may return an arbitrarily-large
628*4882a593Smuzhiyun  * hole by returning an appropriate value in b_size and by clearing
629*4882a593Smuzhiyun  * buffer_mapped().  However the direct-io code will only process holes one
630*4882a593Smuzhiyun  * block at a time - it will repeatedly call get_block() as it walks the hole.
631*4882a593Smuzhiyun  */
get_more_blocks(struct dio * dio,struct dio_submit * sdio,struct buffer_head * map_bh)632*4882a593Smuzhiyun static int get_more_blocks(struct dio *dio, struct dio_submit *sdio,
633*4882a593Smuzhiyun 			   struct buffer_head *map_bh)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun 	int ret;
636*4882a593Smuzhiyun 	sector_t fs_startblk;	/* Into file, in filesystem-sized blocks */
637*4882a593Smuzhiyun 	sector_t fs_endblk;	/* Into file, in filesystem-sized blocks */
638*4882a593Smuzhiyun 	unsigned long fs_count;	/* Number of filesystem-sized blocks */
639*4882a593Smuzhiyun 	int create;
640*4882a593Smuzhiyun 	unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor;
641*4882a593Smuzhiyun 	loff_t i_size;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	/*
644*4882a593Smuzhiyun 	 * If there was a memory error and we've overwritten all the
645*4882a593Smuzhiyun 	 * mapped blocks then we can now return that memory error
646*4882a593Smuzhiyun 	 */
647*4882a593Smuzhiyun 	ret = dio->page_errors;
648*4882a593Smuzhiyun 	if (ret == 0) {
649*4882a593Smuzhiyun 		BUG_ON(sdio->block_in_file >= sdio->final_block_in_request);
650*4882a593Smuzhiyun 		fs_startblk = sdio->block_in_file >> sdio->blkfactor;
651*4882a593Smuzhiyun 		fs_endblk = (sdio->final_block_in_request - 1) >>
652*4882a593Smuzhiyun 					sdio->blkfactor;
653*4882a593Smuzhiyun 		fs_count = fs_endblk - fs_startblk + 1;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 		map_bh->b_state = 0;
656*4882a593Smuzhiyun 		map_bh->b_size = fs_count << i_blkbits;
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 		/*
659*4882a593Smuzhiyun 		 * For writes that could fill holes inside i_size on a
660*4882a593Smuzhiyun 		 * DIO_SKIP_HOLES filesystem we forbid block creations: only
661*4882a593Smuzhiyun 		 * overwrites are permitted. We will return early to the caller
662*4882a593Smuzhiyun 		 * once we see an unmapped buffer head returned, and the caller
663*4882a593Smuzhiyun 		 * will fall back to buffered I/O.
664*4882a593Smuzhiyun 		 *
665*4882a593Smuzhiyun 		 * Otherwise the decision is left to the get_blocks method,
666*4882a593Smuzhiyun 		 * which may decide to handle it or also return an unmapped
667*4882a593Smuzhiyun 		 * buffer head.
668*4882a593Smuzhiyun 		 */
669*4882a593Smuzhiyun 		create = dio->op == REQ_OP_WRITE;
670*4882a593Smuzhiyun 		if (dio->flags & DIO_SKIP_HOLES) {
671*4882a593Smuzhiyun 			i_size = i_size_read(dio->inode);
672*4882a593Smuzhiyun 			if (i_size && fs_startblk <= (i_size - 1) >> i_blkbits)
673*4882a593Smuzhiyun 				create = 0;
674*4882a593Smuzhiyun 		}
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 		ret = (*sdio->get_block)(dio->inode, fs_startblk,
677*4882a593Smuzhiyun 						map_bh, create);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 		/* Store for completion */
680*4882a593Smuzhiyun 		dio->private = map_bh->b_private;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 		if (ret == 0 && buffer_defer_completion(map_bh))
683*4882a593Smuzhiyun 			ret = dio_set_defer_completion(dio);
684*4882a593Smuzhiyun 	}
685*4882a593Smuzhiyun 	return ret;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun /*
689*4882a593Smuzhiyun  * There is no bio.  Make one now.
690*4882a593Smuzhiyun  */
dio_new_bio(struct dio * dio,struct dio_submit * sdio,sector_t start_sector,struct buffer_head * map_bh)691*4882a593Smuzhiyun static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
692*4882a593Smuzhiyun 		sector_t start_sector, struct buffer_head *map_bh)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	sector_t sector;
695*4882a593Smuzhiyun 	int ret, nr_pages;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	ret = dio_bio_reap(dio, sdio);
698*4882a593Smuzhiyun 	if (ret)
699*4882a593Smuzhiyun 		goto out;
700*4882a593Smuzhiyun 	sector = start_sector << (sdio->blkbits - 9);
701*4882a593Smuzhiyun 	nr_pages = min(sdio->pages_in_io, BIO_MAX_PAGES);
702*4882a593Smuzhiyun 	BUG_ON(nr_pages <= 0);
703*4882a593Smuzhiyun 	dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
704*4882a593Smuzhiyun 	sdio->boundary = 0;
705*4882a593Smuzhiyun out:
706*4882a593Smuzhiyun 	return ret;
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun /*
710*4882a593Smuzhiyun  * Attempt to put the current chunk of 'cur_page' into the current BIO.  If
711*4882a593Smuzhiyun  * that was successful then update final_block_in_bio and take a ref against
712*4882a593Smuzhiyun  * the just-added page.
713*4882a593Smuzhiyun  *
714*4882a593Smuzhiyun  * Return zero on success.  Non-zero means the caller needs to start a new BIO.
715*4882a593Smuzhiyun  */
dio_bio_add_page(struct dio_submit * sdio)716*4882a593Smuzhiyun static inline int dio_bio_add_page(struct dio_submit *sdio)
717*4882a593Smuzhiyun {
718*4882a593Smuzhiyun 	int ret;
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	ret = bio_add_page(sdio->bio, sdio->cur_page,
721*4882a593Smuzhiyun 			sdio->cur_page_len, sdio->cur_page_offset);
722*4882a593Smuzhiyun 	if (ret == sdio->cur_page_len) {
723*4882a593Smuzhiyun 		/*
724*4882a593Smuzhiyun 		 * Decrement count only, if we are done with this page
725*4882a593Smuzhiyun 		 */
726*4882a593Smuzhiyun 		if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE)
727*4882a593Smuzhiyun 			sdio->pages_in_io--;
728*4882a593Smuzhiyun 		get_page(sdio->cur_page);
729*4882a593Smuzhiyun 		sdio->final_block_in_bio = sdio->cur_page_block +
730*4882a593Smuzhiyun 			(sdio->cur_page_len >> sdio->blkbits);
731*4882a593Smuzhiyun 		ret = 0;
732*4882a593Smuzhiyun 	} else {
733*4882a593Smuzhiyun 		ret = 1;
734*4882a593Smuzhiyun 	}
735*4882a593Smuzhiyun 	return ret;
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun /*
739*4882a593Smuzhiyun  * Put cur_page under IO.  The section of cur_page which is described by
740*4882a593Smuzhiyun  * cur_page_offset,cur_page_len is put into a BIO.  The section of cur_page
741*4882a593Smuzhiyun  * starts on-disk at cur_page_block.
742*4882a593Smuzhiyun  *
743*4882a593Smuzhiyun  * We take a ref against the page here (on behalf of its presence in the bio).
744*4882a593Smuzhiyun  *
745*4882a593Smuzhiyun  * The caller of this function is responsible for removing cur_page from the
746*4882a593Smuzhiyun  * dio, and for dropping the refcount which came from that presence.
747*4882a593Smuzhiyun  */
dio_send_cur_page(struct dio * dio,struct dio_submit * sdio,struct buffer_head * map_bh)748*4882a593Smuzhiyun static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio,
749*4882a593Smuzhiyun 		struct buffer_head *map_bh)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	int ret = 0;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	if (sdio->bio) {
754*4882a593Smuzhiyun 		loff_t cur_offset = sdio->cur_page_fs_offset;
755*4882a593Smuzhiyun 		loff_t bio_next_offset = sdio->logical_offset_in_bio +
756*4882a593Smuzhiyun 			sdio->bio->bi_iter.bi_size;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 		/*
759*4882a593Smuzhiyun 		 * See whether this new request is contiguous with the old.
760*4882a593Smuzhiyun 		 *
761*4882a593Smuzhiyun 		 * Btrfs cannot handle having logically non-contiguous requests
762*4882a593Smuzhiyun 		 * submitted.  For example if you have
763*4882a593Smuzhiyun 		 *
764*4882a593Smuzhiyun 		 * Logical:  [0-4095][HOLE][8192-12287]
765*4882a593Smuzhiyun 		 * Physical: [0-4095]      [4096-8191]
766*4882a593Smuzhiyun 		 *
767*4882a593Smuzhiyun 		 * We cannot submit those pages together as one BIO.  So if our
768*4882a593Smuzhiyun 		 * current logical offset in the file does not equal what would
769*4882a593Smuzhiyun 		 * be the next logical offset in the bio, submit the bio we
770*4882a593Smuzhiyun 		 * have.
771*4882a593Smuzhiyun 		 *
772*4882a593Smuzhiyun 		 * When fscrypt inline encryption is used, data unit number
773*4882a593Smuzhiyun 		 * (DUN) contiguity is also required.  Normally that's implied
774*4882a593Smuzhiyun 		 * by logical contiguity.  However, certain IV generation
775*4882a593Smuzhiyun 		 * methods (e.g. IV_INO_LBLK_32) don't guarantee it.  So, we
776*4882a593Smuzhiyun 		 * must explicitly check fscrypt_mergeable_bio() too.
777*4882a593Smuzhiyun 		 */
778*4882a593Smuzhiyun 		if (sdio->final_block_in_bio != sdio->cur_page_block ||
779*4882a593Smuzhiyun 		    cur_offset != bio_next_offset ||
780*4882a593Smuzhiyun 		    !fscrypt_mergeable_bio(sdio->bio, dio->inode,
781*4882a593Smuzhiyun 					   cur_offset >> dio->inode->i_blkbits))
782*4882a593Smuzhiyun 			dio_bio_submit(dio, sdio);
783*4882a593Smuzhiyun 	}
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	if (sdio->bio == NULL) {
786*4882a593Smuzhiyun 		ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
787*4882a593Smuzhiyun 		if (ret)
788*4882a593Smuzhiyun 			goto out;
789*4882a593Smuzhiyun 	}
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	if (dio_bio_add_page(sdio) != 0) {
792*4882a593Smuzhiyun 		dio_bio_submit(dio, sdio);
793*4882a593Smuzhiyun 		ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
794*4882a593Smuzhiyun 		if (ret == 0) {
795*4882a593Smuzhiyun 			ret = dio_bio_add_page(sdio);
796*4882a593Smuzhiyun 			BUG_ON(ret != 0);
797*4882a593Smuzhiyun 		}
798*4882a593Smuzhiyun 	}
799*4882a593Smuzhiyun out:
800*4882a593Smuzhiyun 	return ret;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun /*
804*4882a593Smuzhiyun  * An autonomous function to put a chunk of a page under deferred IO.
805*4882a593Smuzhiyun  *
806*4882a593Smuzhiyun  * The caller doesn't actually know (or care) whether this piece of page is in
807*4882a593Smuzhiyun  * a BIO, or is under IO or whatever.  We just take care of all possible
808*4882a593Smuzhiyun  * situations here.  The separation between the logic of do_direct_IO() and
809*4882a593Smuzhiyun  * that of submit_page_section() is important for clarity.  Please don't break.
810*4882a593Smuzhiyun  *
811*4882a593Smuzhiyun  * The chunk of page starts on-disk at blocknr.
812*4882a593Smuzhiyun  *
813*4882a593Smuzhiyun  * We perform deferred IO, by recording the last-submitted page inside our
814*4882a593Smuzhiyun  * private part of the dio structure.  If possible, we just expand the IO
815*4882a593Smuzhiyun  * across that page here.
816*4882a593Smuzhiyun  *
817*4882a593Smuzhiyun  * If that doesn't work out then we put the old page into the bio and add this
818*4882a593Smuzhiyun  * page to the dio instead.
819*4882a593Smuzhiyun  */
820*4882a593Smuzhiyun static inline int
submit_page_section(struct dio * dio,struct dio_submit * sdio,struct page * page,unsigned offset,unsigned len,sector_t blocknr,struct buffer_head * map_bh)821*4882a593Smuzhiyun submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page,
822*4882a593Smuzhiyun 		    unsigned offset, unsigned len, sector_t blocknr,
823*4882a593Smuzhiyun 		    struct buffer_head *map_bh)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun 	int ret = 0;
826*4882a593Smuzhiyun 	int boundary = sdio->boundary;	/* dio_send_cur_page may clear it */
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	if (dio->op == REQ_OP_WRITE) {
829*4882a593Smuzhiyun 		/*
830*4882a593Smuzhiyun 		 * Read accounting is performed in submit_bio()
831*4882a593Smuzhiyun 		 */
832*4882a593Smuzhiyun 		task_io_account_write(len);
833*4882a593Smuzhiyun 	}
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	/*
836*4882a593Smuzhiyun 	 * Can we just grow the current page's presence in the dio?
837*4882a593Smuzhiyun 	 */
838*4882a593Smuzhiyun 	if (sdio->cur_page == page &&
839*4882a593Smuzhiyun 	    sdio->cur_page_offset + sdio->cur_page_len == offset &&
840*4882a593Smuzhiyun 	    sdio->cur_page_block +
841*4882a593Smuzhiyun 	    (sdio->cur_page_len >> sdio->blkbits) == blocknr) {
842*4882a593Smuzhiyun 		sdio->cur_page_len += len;
843*4882a593Smuzhiyun 		goto out;
844*4882a593Smuzhiyun 	}
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	/*
847*4882a593Smuzhiyun 	 * If there's a deferred page already there then send it.
848*4882a593Smuzhiyun 	 */
849*4882a593Smuzhiyun 	if (sdio->cur_page) {
850*4882a593Smuzhiyun 		ret = dio_send_cur_page(dio, sdio, map_bh);
851*4882a593Smuzhiyun 		put_page(sdio->cur_page);
852*4882a593Smuzhiyun 		sdio->cur_page = NULL;
853*4882a593Smuzhiyun 		if (ret)
854*4882a593Smuzhiyun 			return ret;
855*4882a593Smuzhiyun 	}
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	get_page(page);		/* It is in dio */
858*4882a593Smuzhiyun 	sdio->cur_page = page;
859*4882a593Smuzhiyun 	sdio->cur_page_offset = offset;
860*4882a593Smuzhiyun 	sdio->cur_page_len = len;
861*4882a593Smuzhiyun 	sdio->cur_page_block = blocknr;
862*4882a593Smuzhiyun 	sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits;
863*4882a593Smuzhiyun out:
864*4882a593Smuzhiyun 	/*
865*4882a593Smuzhiyun 	 * If boundary then we want to schedule the IO now to
866*4882a593Smuzhiyun 	 * avoid metadata seeks.
867*4882a593Smuzhiyun 	 */
868*4882a593Smuzhiyun 	if (boundary) {
869*4882a593Smuzhiyun 		ret = dio_send_cur_page(dio, sdio, map_bh);
870*4882a593Smuzhiyun 		if (sdio->bio)
871*4882a593Smuzhiyun 			dio_bio_submit(dio, sdio);
872*4882a593Smuzhiyun 		put_page(sdio->cur_page);
873*4882a593Smuzhiyun 		sdio->cur_page = NULL;
874*4882a593Smuzhiyun 	}
875*4882a593Smuzhiyun 	return ret;
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun /*
879*4882a593Smuzhiyun  * If we are not writing the entire block and get_block() allocated
880*4882a593Smuzhiyun  * the block for us, we need to fill-in the unused portion of the
881*4882a593Smuzhiyun  * block with zeros. This happens only if user-buffer, fileoffset or
882*4882a593Smuzhiyun  * io length is not filesystem block-size multiple.
883*4882a593Smuzhiyun  *
884*4882a593Smuzhiyun  * `end' is zero if we're doing the start of the IO, 1 at the end of the
885*4882a593Smuzhiyun  * IO.
886*4882a593Smuzhiyun  */
dio_zero_block(struct dio * dio,struct dio_submit * sdio,int end,struct buffer_head * map_bh)887*4882a593Smuzhiyun static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio,
888*4882a593Smuzhiyun 		int end, struct buffer_head *map_bh)
889*4882a593Smuzhiyun {
890*4882a593Smuzhiyun 	unsigned dio_blocks_per_fs_block;
891*4882a593Smuzhiyun 	unsigned this_chunk_blocks;	/* In dio_blocks */
892*4882a593Smuzhiyun 	unsigned this_chunk_bytes;
893*4882a593Smuzhiyun 	struct page *page;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	sdio->start_zero_done = 1;
896*4882a593Smuzhiyun 	if (!sdio->blkfactor || !buffer_new(map_bh))
897*4882a593Smuzhiyun 		return;
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	dio_blocks_per_fs_block = 1 << sdio->blkfactor;
900*4882a593Smuzhiyun 	this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1);
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	if (!this_chunk_blocks)
903*4882a593Smuzhiyun 		return;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	/*
906*4882a593Smuzhiyun 	 * We need to zero out part of an fs block.  It is either at the
907*4882a593Smuzhiyun 	 * beginning or the end of the fs block.
908*4882a593Smuzhiyun 	 */
909*4882a593Smuzhiyun 	if (end)
910*4882a593Smuzhiyun 		this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	this_chunk_bytes = this_chunk_blocks << sdio->blkbits;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	page = ZERO_PAGE(0);
915*4882a593Smuzhiyun 	if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes,
916*4882a593Smuzhiyun 				sdio->next_block_for_io, map_bh))
917*4882a593Smuzhiyun 		return;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	sdio->next_block_for_io += this_chunk_blocks;
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun /*
923*4882a593Smuzhiyun  * Walk the user pages, and the file, mapping blocks to disk and generating
924*4882a593Smuzhiyun  * a sequence of (page,offset,len,block) mappings.  These mappings are injected
925*4882a593Smuzhiyun  * into submit_page_section(), which takes care of the next stage of submission
926*4882a593Smuzhiyun  *
927*4882a593Smuzhiyun  * Direct IO against a blockdev is different from a file.  Because we can
928*4882a593Smuzhiyun  * happily perform page-sized but 512-byte aligned IOs.  It is important that
929*4882a593Smuzhiyun  * blockdev IO be able to have fine alignment and large sizes.
930*4882a593Smuzhiyun  *
931*4882a593Smuzhiyun  * So what we do is to permit the ->get_block function to populate bh.b_size
932*4882a593Smuzhiyun  * with the size of IO which is permitted at this offset and this i_blkbits.
933*4882a593Smuzhiyun  *
934*4882a593Smuzhiyun  * For best results, the blockdev should be set up with 512-byte i_blkbits and
935*4882a593Smuzhiyun  * it should set b_size to PAGE_SIZE or more inside get_block().  This gives
936*4882a593Smuzhiyun  * fine alignment but still allows this function to work in PAGE_SIZE units.
937*4882a593Smuzhiyun  */
do_direct_IO(struct dio * dio,struct dio_submit * sdio,struct buffer_head * map_bh)938*4882a593Smuzhiyun static int do_direct_IO(struct dio *dio, struct dio_submit *sdio,
939*4882a593Smuzhiyun 			struct buffer_head *map_bh)
940*4882a593Smuzhiyun {
941*4882a593Smuzhiyun 	const unsigned blkbits = sdio->blkbits;
942*4882a593Smuzhiyun 	const unsigned i_blkbits = blkbits + sdio->blkfactor;
943*4882a593Smuzhiyun 	int ret = 0;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	while (sdio->block_in_file < sdio->final_block_in_request) {
946*4882a593Smuzhiyun 		struct page *page;
947*4882a593Smuzhiyun 		size_t from, to;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 		page = dio_get_page(dio, sdio);
950*4882a593Smuzhiyun 		if (IS_ERR(page)) {
951*4882a593Smuzhiyun 			ret = PTR_ERR(page);
952*4882a593Smuzhiyun 			goto out;
953*4882a593Smuzhiyun 		}
954*4882a593Smuzhiyun 		from = sdio->head ? 0 : sdio->from;
955*4882a593Smuzhiyun 		to = (sdio->head == sdio->tail - 1) ? sdio->to : PAGE_SIZE;
956*4882a593Smuzhiyun 		sdio->head++;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 		while (from < to) {
959*4882a593Smuzhiyun 			unsigned this_chunk_bytes;	/* # of bytes mapped */
960*4882a593Smuzhiyun 			unsigned this_chunk_blocks;	/* # of blocks */
961*4882a593Smuzhiyun 			unsigned u;
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 			if (sdio->blocks_available == 0) {
964*4882a593Smuzhiyun 				/*
965*4882a593Smuzhiyun 				 * Need to go and map some more disk
966*4882a593Smuzhiyun 				 */
967*4882a593Smuzhiyun 				unsigned long blkmask;
968*4882a593Smuzhiyun 				unsigned long dio_remainder;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 				ret = get_more_blocks(dio, sdio, map_bh);
971*4882a593Smuzhiyun 				if (ret) {
972*4882a593Smuzhiyun 					put_page(page);
973*4882a593Smuzhiyun 					goto out;
974*4882a593Smuzhiyun 				}
975*4882a593Smuzhiyun 				if (!buffer_mapped(map_bh))
976*4882a593Smuzhiyun 					goto do_holes;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 				sdio->blocks_available =
979*4882a593Smuzhiyun 						map_bh->b_size >> blkbits;
980*4882a593Smuzhiyun 				sdio->next_block_for_io =
981*4882a593Smuzhiyun 					map_bh->b_blocknr << sdio->blkfactor;
982*4882a593Smuzhiyun 				if (buffer_new(map_bh)) {
983*4882a593Smuzhiyun 					clean_bdev_aliases(
984*4882a593Smuzhiyun 						map_bh->b_bdev,
985*4882a593Smuzhiyun 						map_bh->b_blocknr,
986*4882a593Smuzhiyun 						map_bh->b_size >> i_blkbits);
987*4882a593Smuzhiyun 				}
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 				if (!sdio->blkfactor)
990*4882a593Smuzhiyun 					goto do_holes;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 				blkmask = (1 << sdio->blkfactor) - 1;
993*4882a593Smuzhiyun 				dio_remainder = (sdio->block_in_file & blkmask);
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 				/*
996*4882a593Smuzhiyun 				 * If we are at the start of IO and that IO
997*4882a593Smuzhiyun 				 * starts partway into a fs-block,
998*4882a593Smuzhiyun 				 * dio_remainder will be non-zero.  If the IO
999*4882a593Smuzhiyun 				 * is a read then we can simply advance the IO
1000*4882a593Smuzhiyun 				 * cursor to the first block which is to be
1001*4882a593Smuzhiyun 				 * read.  But if the IO is a write and the
1002*4882a593Smuzhiyun 				 * block was newly allocated we cannot do that;
1003*4882a593Smuzhiyun 				 * the start of the fs block must be zeroed out
1004*4882a593Smuzhiyun 				 * on-disk
1005*4882a593Smuzhiyun 				 */
1006*4882a593Smuzhiyun 				if (!buffer_new(map_bh))
1007*4882a593Smuzhiyun 					sdio->next_block_for_io += dio_remainder;
1008*4882a593Smuzhiyun 				sdio->blocks_available -= dio_remainder;
1009*4882a593Smuzhiyun 			}
1010*4882a593Smuzhiyun do_holes:
1011*4882a593Smuzhiyun 			/* Handle holes */
1012*4882a593Smuzhiyun 			if (!buffer_mapped(map_bh)) {
1013*4882a593Smuzhiyun 				loff_t i_size_aligned;
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 				/* AKPM: eargh, -ENOTBLK is a hack */
1016*4882a593Smuzhiyun 				if (dio->op == REQ_OP_WRITE) {
1017*4882a593Smuzhiyun 					put_page(page);
1018*4882a593Smuzhiyun 					return -ENOTBLK;
1019*4882a593Smuzhiyun 				}
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 				/*
1022*4882a593Smuzhiyun 				 * Be sure to account for a partial block as the
1023*4882a593Smuzhiyun 				 * last block in the file
1024*4882a593Smuzhiyun 				 */
1025*4882a593Smuzhiyun 				i_size_aligned = ALIGN(i_size_read(dio->inode),
1026*4882a593Smuzhiyun 							1 << blkbits);
1027*4882a593Smuzhiyun 				if (sdio->block_in_file >=
1028*4882a593Smuzhiyun 						i_size_aligned >> blkbits) {
1029*4882a593Smuzhiyun 					/* We hit eof */
1030*4882a593Smuzhiyun 					put_page(page);
1031*4882a593Smuzhiyun 					goto out;
1032*4882a593Smuzhiyun 				}
1033*4882a593Smuzhiyun 				zero_user(page, from, 1 << blkbits);
1034*4882a593Smuzhiyun 				sdio->block_in_file++;
1035*4882a593Smuzhiyun 				from += 1 << blkbits;
1036*4882a593Smuzhiyun 				dio->result += 1 << blkbits;
1037*4882a593Smuzhiyun 				goto next_block;
1038*4882a593Smuzhiyun 			}
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 			/*
1041*4882a593Smuzhiyun 			 * If we're performing IO which has an alignment which
1042*4882a593Smuzhiyun 			 * is finer than the underlying fs, go check to see if
1043*4882a593Smuzhiyun 			 * we must zero out the start of this block.
1044*4882a593Smuzhiyun 			 */
1045*4882a593Smuzhiyun 			if (unlikely(sdio->blkfactor && !sdio->start_zero_done))
1046*4882a593Smuzhiyun 				dio_zero_block(dio, sdio, 0, map_bh);
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 			/*
1049*4882a593Smuzhiyun 			 * Work out, in this_chunk_blocks, how much disk we
1050*4882a593Smuzhiyun 			 * can add to this page
1051*4882a593Smuzhiyun 			 */
1052*4882a593Smuzhiyun 			this_chunk_blocks = sdio->blocks_available;
1053*4882a593Smuzhiyun 			u = (to - from) >> blkbits;
1054*4882a593Smuzhiyun 			if (this_chunk_blocks > u)
1055*4882a593Smuzhiyun 				this_chunk_blocks = u;
1056*4882a593Smuzhiyun 			u = sdio->final_block_in_request - sdio->block_in_file;
1057*4882a593Smuzhiyun 			if (this_chunk_blocks > u)
1058*4882a593Smuzhiyun 				this_chunk_blocks = u;
1059*4882a593Smuzhiyun 			this_chunk_bytes = this_chunk_blocks << blkbits;
1060*4882a593Smuzhiyun 			BUG_ON(this_chunk_bytes == 0);
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 			if (this_chunk_blocks == sdio->blocks_available)
1063*4882a593Smuzhiyun 				sdio->boundary = buffer_boundary(map_bh);
1064*4882a593Smuzhiyun 			ret = submit_page_section(dio, sdio, page,
1065*4882a593Smuzhiyun 						  from,
1066*4882a593Smuzhiyun 						  this_chunk_bytes,
1067*4882a593Smuzhiyun 						  sdio->next_block_for_io,
1068*4882a593Smuzhiyun 						  map_bh);
1069*4882a593Smuzhiyun 			if (ret) {
1070*4882a593Smuzhiyun 				put_page(page);
1071*4882a593Smuzhiyun 				goto out;
1072*4882a593Smuzhiyun 			}
1073*4882a593Smuzhiyun 			sdio->next_block_for_io += this_chunk_blocks;
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 			sdio->block_in_file += this_chunk_blocks;
1076*4882a593Smuzhiyun 			from += this_chunk_bytes;
1077*4882a593Smuzhiyun 			dio->result += this_chunk_bytes;
1078*4882a593Smuzhiyun 			sdio->blocks_available -= this_chunk_blocks;
1079*4882a593Smuzhiyun next_block:
1080*4882a593Smuzhiyun 			BUG_ON(sdio->block_in_file > sdio->final_block_in_request);
1081*4882a593Smuzhiyun 			if (sdio->block_in_file == sdio->final_block_in_request)
1082*4882a593Smuzhiyun 				break;
1083*4882a593Smuzhiyun 		}
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 		/* Drop the ref which was taken in get_user_pages() */
1086*4882a593Smuzhiyun 		put_page(page);
1087*4882a593Smuzhiyun 	}
1088*4882a593Smuzhiyun out:
1089*4882a593Smuzhiyun 	return ret;
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun 
drop_refcount(struct dio * dio)1092*4882a593Smuzhiyun static inline int drop_refcount(struct dio *dio)
1093*4882a593Smuzhiyun {
1094*4882a593Smuzhiyun 	int ret2;
1095*4882a593Smuzhiyun 	unsigned long flags;
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	/*
1098*4882a593Smuzhiyun 	 * Sync will always be dropping the final ref and completing the
1099*4882a593Smuzhiyun 	 * operation.  AIO can if it was a broken operation described above or
1100*4882a593Smuzhiyun 	 * in fact if all the bios race to complete before we get here.  In
1101*4882a593Smuzhiyun 	 * that case dio_complete() translates the EIOCBQUEUED into the proper
1102*4882a593Smuzhiyun 	 * return code that the caller will hand to ->complete().
1103*4882a593Smuzhiyun 	 *
1104*4882a593Smuzhiyun 	 * This is managed by the bio_lock instead of being an atomic_t so that
1105*4882a593Smuzhiyun 	 * completion paths can drop their ref and use the remaining count to
1106*4882a593Smuzhiyun 	 * decide to wake the submission path atomically.
1107*4882a593Smuzhiyun 	 */
1108*4882a593Smuzhiyun 	spin_lock_irqsave(&dio->bio_lock, flags);
1109*4882a593Smuzhiyun 	ret2 = --dio->refcount;
1110*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dio->bio_lock, flags);
1111*4882a593Smuzhiyun 	return ret2;
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun /*
1115*4882a593Smuzhiyun  * This is a library function for use by filesystem drivers.
1116*4882a593Smuzhiyun  *
1117*4882a593Smuzhiyun  * The locking rules are governed by the flags parameter:
1118*4882a593Smuzhiyun  *  - if the flags value contains DIO_LOCKING we use a fancy locking
1119*4882a593Smuzhiyun  *    scheme for dumb filesystems.
1120*4882a593Smuzhiyun  *    For writes this function is called under i_mutex and returns with
1121*4882a593Smuzhiyun  *    i_mutex held, for reads, i_mutex is not held on entry, but it is
1122*4882a593Smuzhiyun  *    taken and dropped again before returning.
1123*4882a593Smuzhiyun  *  - if the flags value does NOT contain DIO_LOCKING we don't use any
1124*4882a593Smuzhiyun  *    internal locking but rather rely on the filesystem to synchronize
1125*4882a593Smuzhiyun  *    direct I/O reads/writes versus each other and truncate.
1126*4882a593Smuzhiyun  *
1127*4882a593Smuzhiyun  * To help with locking against truncate we incremented the i_dio_count
1128*4882a593Smuzhiyun  * counter before starting direct I/O, and decrement it once we are done.
1129*4882a593Smuzhiyun  * Truncate can wait for it to reach zero to provide exclusion.  It is
1130*4882a593Smuzhiyun  * expected that filesystem provide exclusion between new direct I/O
1131*4882a593Smuzhiyun  * and truncates.  For DIO_LOCKING filesystems this is done by i_mutex,
1132*4882a593Smuzhiyun  * but other filesystems need to take care of this on their own.
1133*4882a593Smuzhiyun  *
1134*4882a593Smuzhiyun  * NOTE: if you pass "sdio" to anything by pointer make sure that function
1135*4882a593Smuzhiyun  * is always inlined. Otherwise gcc is unable to split the structure into
1136*4882a593Smuzhiyun  * individual fields and will generate much worse code. This is important
1137*4882a593Smuzhiyun  * for the whole file.
1138*4882a593Smuzhiyun  */
1139*4882a593Smuzhiyun static inline ssize_t
do_blockdev_direct_IO(struct kiocb * iocb,struct inode * inode,struct block_device * bdev,struct iov_iter * iter,get_block_t get_block,dio_iodone_t end_io,dio_submit_t submit_io,int flags)1140*4882a593Smuzhiyun do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
1141*4882a593Smuzhiyun 		      struct block_device *bdev, struct iov_iter *iter,
1142*4882a593Smuzhiyun 		      get_block_t get_block, dio_iodone_t end_io,
1143*4882a593Smuzhiyun 		      dio_submit_t submit_io, int flags)
1144*4882a593Smuzhiyun {
1145*4882a593Smuzhiyun 	unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
1146*4882a593Smuzhiyun 	unsigned blkbits = i_blkbits;
1147*4882a593Smuzhiyun 	unsigned blocksize_mask = (1 << blkbits) - 1;
1148*4882a593Smuzhiyun 	ssize_t retval = -EINVAL;
1149*4882a593Smuzhiyun 	const size_t count = iov_iter_count(iter);
1150*4882a593Smuzhiyun 	loff_t offset = iocb->ki_pos;
1151*4882a593Smuzhiyun 	const loff_t end = offset + count;
1152*4882a593Smuzhiyun 	struct dio *dio;
1153*4882a593Smuzhiyun 	struct dio_submit sdio = { 0, };
1154*4882a593Smuzhiyun 	struct buffer_head map_bh = { 0, };
1155*4882a593Smuzhiyun 	struct blk_plug plug;
1156*4882a593Smuzhiyun 	unsigned long align = offset | iov_iter_alignment(iter);
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	/*
1159*4882a593Smuzhiyun 	 * Avoid references to bdev if not absolutely needed to give
1160*4882a593Smuzhiyun 	 * the early prefetch in the caller enough time.
1161*4882a593Smuzhiyun 	 */
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	/* watch out for a 0 len io from a tricksy fs */
1164*4882a593Smuzhiyun 	if (iov_iter_rw(iter) == READ && !count)
1165*4882a593Smuzhiyun 		return 0;
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	dio = kmem_cache_alloc(dio_cache, GFP_KERNEL);
1168*4882a593Smuzhiyun 	if (!dio)
1169*4882a593Smuzhiyun 		return -ENOMEM;
1170*4882a593Smuzhiyun 	/*
1171*4882a593Smuzhiyun 	 * Believe it or not, zeroing out the page array caused a .5%
1172*4882a593Smuzhiyun 	 * performance regression in a database benchmark.  So, we take
1173*4882a593Smuzhiyun 	 * care to only zero out what's needed.
1174*4882a593Smuzhiyun 	 */
1175*4882a593Smuzhiyun 	memset(dio, 0, offsetof(struct dio, pages));
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	dio->flags = flags;
1178*4882a593Smuzhiyun 	if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) {
1179*4882a593Smuzhiyun 		/* will be released by direct_io_worker */
1180*4882a593Smuzhiyun 		inode_lock(inode);
1181*4882a593Smuzhiyun 	}
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	/* Once we sampled i_size check for reads beyond EOF */
1184*4882a593Smuzhiyun 	dio->i_size = i_size_read(inode);
1185*4882a593Smuzhiyun 	if (iov_iter_rw(iter) == READ && offset >= dio->i_size) {
1186*4882a593Smuzhiyun 		retval = 0;
1187*4882a593Smuzhiyun 		goto fail_dio;
1188*4882a593Smuzhiyun 	}
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun 	if (align & blocksize_mask) {
1191*4882a593Smuzhiyun 		if (bdev)
1192*4882a593Smuzhiyun 			blkbits = blksize_bits(bdev_logical_block_size(bdev));
1193*4882a593Smuzhiyun 		blocksize_mask = (1 << blkbits) - 1;
1194*4882a593Smuzhiyun 		if (align & blocksize_mask)
1195*4882a593Smuzhiyun 			goto fail_dio;
1196*4882a593Smuzhiyun 	}
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ) {
1199*4882a593Smuzhiyun 		struct address_space *mapping = iocb->ki_filp->f_mapping;
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 		retval = filemap_write_and_wait_range(mapping, offset, end - 1);
1202*4882a593Smuzhiyun 		if (retval)
1203*4882a593Smuzhiyun 			goto fail_dio;
1204*4882a593Smuzhiyun 	}
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	/*
1207*4882a593Smuzhiyun 	 * For file extending writes updating i_size before data writeouts
1208*4882a593Smuzhiyun 	 * complete can expose uninitialized blocks in dumb filesystems.
1209*4882a593Smuzhiyun 	 * In that case we need to wait for I/O completion even if asked
1210*4882a593Smuzhiyun 	 * for an asynchronous write.
1211*4882a593Smuzhiyun 	 */
1212*4882a593Smuzhiyun 	if (is_sync_kiocb(iocb))
1213*4882a593Smuzhiyun 		dio->is_async = false;
1214*4882a593Smuzhiyun 	else if (iov_iter_rw(iter) == WRITE && end > i_size_read(inode))
1215*4882a593Smuzhiyun 		dio->is_async = false;
1216*4882a593Smuzhiyun 	else
1217*4882a593Smuzhiyun 		dio->is_async = true;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	dio->inode = inode;
1220*4882a593Smuzhiyun 	if (iov_iter_rw(iter) == WRITE) {
1221*4882a593Smuzhiyun 		dio->op = REQ_OP_WRITE;
1222*4882a593Smuzhiyun 		dio->op_flags = REQ_SYNC | REQ_IDLE;
1223*4882a593Smuzhiyun 		if (iocb->ki_flags & IOCB_NOWAIT)
1224*4882a593Smuzhiyun 			dio->op_flags |= REQ_NOWAIT;
1225*4882a593Smuzhiyun 	} else {
1226*4882a593Smuzhiyun 		dio->op = REQ_OP_READ;
1227*4882a593Smuzhiyun 	}
1228*4882a593Smuzhiyun 	if (iocb->ki_flags & IOCB_HIPRI)
1229*4882a593Smuzhiyun 		dio->op_flags |= REQ_HIPRI;
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 	/*
1232*4882a593Smuzhiyun 	 * For AIO O_(D)SYNC writes we need to defer completions to a workqueue
1233*4882a593Smuzhiyun 	 * so that we can call ->fsync.
1234*4882a593Smuzhiyun 	 */
1235*4882a593Smuzhiyun 	if (dio->is_async && iov_iter_rw(iter) == WRITE) {
1236*4882a593Smuzhiyun 		retval = 0;
1237*4882a593Smuzhiyun 		if (iocb->ki_flags & IOCB_DSYNC)
1238*4882a593Smuzhiyun 			retval = dio_set_defer_completion(dio);
1239*4882a593Smuzhiyun 		else if (!dio->inode->i_sb->s_dio_done_wq) {
1240*4882a593Smuzhiyun 			/*
1241*4882a593Smuzhiyun 			 * In case of AIO write racing with buffered read we
1242*4882a593Smuzhiyun 			 * need to defer completion. We can't decide this now,
1243*4882a593Smuzhiyun 			 * however the workqueue needs to be initialized here.
1244*4882a593Smuzhiyun 			 */
1245*4882a593Smuzhiyun 			retval = sb_init_dio_done_wq(dio->inode->i_sb);
1246*4882a593Smuzhiyun 		}
1247*4882a593Smuzhiyun 		if (retval)
1248*4882a593Smuzhiyun 			goto fail_dio;
1249*4882a593Smuzhiyun 	}
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	/*
1252*4882a593Smuzhiyun 	 * Will be decremented at I/O completion time.
1253*4882a593Smuzhiyun 	 */
1254*4882a593Smuzhiyun 	inode_dio_begin(inode);
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	retval = 0;
1257*4882a593Smuzhiyun 	sdio.blkbits = blkbits;
1258*4882a593Smuzhiyun 	sdio.blkfactor = i_blkbits - blkbits;
1259*4882a593Smuzhiyun 	sdio.block_in_file = offset >> blkbits;
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun 	sdio.get_block = get_block;
1262*4882a593Smuzhiyun 	dio->end_io = end_io;
1263*4882a593Smuzhiyun 	sdio.submit_io = submit_io;
1264*4882a593Smuzhiyun 	sdio.final_block_in_bio = -1;
1265*4882a593Smuzhiyun 	sdio.next_block_for_io = -1;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	dio->iocb = iocb;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	spin_lock_init(&dio->bio_lock);
1270*4882a593Smuzhiyun 	dio->refcount = 1;
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ;
1273*4882a593Smuzhiyun 	sdio.iter = iter;
1274*4882a593Smuzhiyun 	sdio.final_block_in_request = end >> blkbits;
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	/*
1277*4882a593Smuzhiyun 	 * In case of non-aligned buffers, we may need 2 more
1278*4882a593Smuzhiyun 	 * pages since we need to zero out first and last block.
1279*4882a593Smuzhiyun 	 */
1280*4882a593Smuzhiyun 	if (unlikely(sdio.blkfactor))
1281*4882a593Smuzhiyun 		sdio.pages_in_io = 2;
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	sdio.pages_in_io += iov_iter_npages(iter, INT_MAX);
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	blk_start_plug(&plug);
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	retval = do_direct_IO(dio, &sdio, &map_bh);
1288*4882a593Smuzhiyun 	if (retval)
1289*4882a593Smuzhiyun 		dio_cleanup(dio, &sdio);
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	if (retval == -ENOTBLK) {
1292*4882a593Smuzhiyun 		/*
1293*4882a593Smuzhiyun 		 * The remaining part of the request will be
1294*4882a593Smuzhiyun 		 * be handled by buffered I/O when we return
1295*4882a593Smuzhiyun 		 */
1296*4882a593Smuzhiyun 		retval = 0;
1297*4882a593Smuzhiyun 	}
1298*4882a593Smuzhiyun 	/*
1299*4882a593Smuzhiyun 	 * There may be some unwritten disk at the end of a part-written
1300*4882a593Smuzhiyun 	 * fs-block-sized block.  Go zero that now.
1301*4882a593Smuzhiyun 	 */
1302*4882a593Smuzhiyun 	dio_zero_block(dio, &sdio, 1, &map_bh);
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	if (sdio.cur_page) {
1305*4882a593Smuzhiyun 		ssize_t ret2;
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 		ret2 = dio_send_cur_page(dio, &sdio, &map_bh);
1308*4882a593Smuzhiyun 		if (retval == 0)
1309*4882a593Smuzhiyun 			retval = ret2;
1310*4882a593Smuzhiyun 		put_page(sdio.cur_page);
1311*4882a593Smuzhiyun 		sdio.cur_page = NULL;
1312*4882a593Smuzhiyun 	}
1313*4882a593Smuzhiyun 	if (sdio.bio)
1314*4882a593Smuzhiyun 		dio_bio_submit(dio, &sdio);
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 	blk_finish_plug(&plug);
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	/*
1319*4882a593Smuzhiyun 	 * It is possible that, we return short IO due to end of file.
1320*4882a593Smuzhiyun 	 * In that case, we need to release all the pages we got hold on.
1321*4882a593Smuzhiyun 	 */
1322*4882a593Smuzhiyun 	dio_cleanup(dio, &sdio);
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	/*
1325*4882a593Smuzhiyun 	 * All block lookups have been performed. For READ requests
1326*4882a593Smuzhiyun 	 * we can let i_mutex go now that its achieved its purpose
1327*4882a593Smuzhiyun 	 * of protecting us from looking up uninitialized blocks.
1328*4882a593Smuzhiyun 	 */
1329*4882a593Smuzhiyun 	if (iov_iter_rw(iter) == READ && (dio->flags & DIO_LOCKING))
1330*4882a593Smuzhiyun 		inode_unlock(dio->inode);
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	/*
1333*4882a593Smuzhiyun 	 * The only time we want to leave bios in flight is when a successful
1334*4882a593Smuzhiyun 	 * partial aio read or full aio write have been setup.  In that case
1335*4882a593Smuzhiyun 	 * bio completion will call aio_complete.  The only time it's safe to
1336*4882a593Smuzhiyun 	 * call aio_complete is when we return -EIOCBQUEUED, so we key on that.
1337*4882a593Smuzhiyun 	 * This had *better* be the only place that raises -EIOCBQUEUED.
1338*4882a593Smuzhiyun 	 */
1339*4882a593Smuzhiyun 	BUG_ON(retval == -EIOCBQUEUED);
1340*4882a593Smuzhiyun 	if (dio->is_async && retval == 0 && dio->result &&
1341*4882a593Smuzhiyun 	    (iov_iter_rw(iter) == READ || dio->result == count))
1342*4882a593Smuzhiyun 		retval = -EIOCBQUEUED;
1343*4882a593Smuzhiyun 	else
1344*4882a593Smuzhiyun 		dio_await_completion(dio);
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	if (drop_refcount(dio) == 0) {
1347*4882a593Smuzhiyun 		retval = dio_complete(dio, retval, DIO_COMPLETE_INVALIDATE);
1348*4882a593Smuzhiyun 	} else
1349*4882a593Smuzhiyun 		BUG_ON(retval != -EIOCBQUEUED);
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	return retval;
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun fail_dio:
1354*4882a593Smuzhiyun 	if (dio->flags & DIO_LOCKING && iov_iter_rw(iter) == READ)
1355*4882a593Smuzhiyun 		inode_unlock(inode);
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 	kmem_cache_free(dio_cache, dio);
1358*4882a593Smuzhiyun 	return retval;
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun 
__blockdev_direct_IO(struct kiocb * iocb,struct inode * inode,struct block_device * bdev,struct iov_iter * iter,get_block_t get_block,dio_iodone_t end_io,dio_submit_t submit_io,int flags)1361*4882a593Smuzhiyun ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
1362*4882a593Smuzhiyun 			     struct block_device *bdev, struct iov_iter *iter,
1363*4882a593Smuzhiyun 			     get_block_t get_block,
1364*4882a593Smuzhiyun 			     dio_iodone_t end_io, dio_submit_t submit_io,
1365*4882a593Smuzhiyun 			     int flags)
1366*4882a593Smuzhiyun {
1367*4882a593Smuzhiyun 	/*
1368*4882a593Smuzhiyun 	 * The block device state is needed in the end to finally
1369*4882a593Smuzhiyun 	 * submit everything.  Since it's likely to be cache cold
1370*4882a593Smuzhiyun 	 * prefetch it here as first thing to hide some of the
1371*4882a593Smuzhiyun 	 * latency.
1372*4882a593Smuzhiyun 	 *
1373*4882a593Smuzhiyun 	 * Attempt to prefetch the pieces we likely need later.
1374*4882a593Smuzhiyun 	 */
1375*4882a593Smuzhiyun 	prefetch(&bdev->bd_disk->part_tbl);
1376*4882a593Smuzhiyun 	prefetch(bdev->bd_disk->queue);
1377*4882a593Smuzhiyun 	prefetch((char *)bdev->bd_disk->queue + SMP_CACHE_BYTES);
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	return do_blockdev_direct_IO(iocb, inode, bdev, iter, get_block,
1380*4882a593Smuzhiyun 				     end_io, submit_io, flags);
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun EXPORT_SYMBOL_NS(__blockdev_direct_IO, ANDROID_GKI_VFS_EXPORT_ONLY);
1384*4882a593Smuzhiyun 
dio_init(void)1385*4882a593Smuzhiyun static __init int dio_init(void)
1386*4882a593Smuzhiyun {
1387*4882a593Smuzhiyun 	dio_cache = KMEM_CACHE(dio, SLAB_PANIC);
1388*4882a593Smuzhiyun 	return 0;
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun module_init(dio_init)
1391