xref: /openbmc/linux/fs/direct-io.c (revision 5e25c269e17de4c5a23ce886cda612b01365a944)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * fs/direct-io.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * O_DIRECT
71da177e4SLinus Torvalds  *
8e1f8e874SFrancois Cami  * 04Jul2002	Andrew Morton
91da177e4SLinus Torvalds  *		Initial version
101da177e4SLinus Torvalds  * 11Sep2002	janetinc@us.ibm.com
111da177e4SLinus Torvalds  * 		added readv/writev support.
12e1f8e874SFrancois Cami  * 29Oct2002	Andrew Morton
131da177e4SLinus Torvalds  *		rewrote bio_add_page() support.
141da177e4SLinus Torvalds  * 30Oct2002	pbadari@us.ibm.com
151da177e4SLinus Torvalds  *		added support for non-aligned IO.
161da177e4SLinus Torvalds  * 06Nov2002	pbadari@us.ibm.com
171da177e4SLinus Torvalds  *		added asynchronous IO support.
181da177e4SLinus Torvalds  * 21Jul2003	nathans@sgi.com
191da177e4SLinus Torvalds  *		added IO completion notifier.
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/module.h>
241da177e4SLinus Torvalds #include <linux/types.h>
251da177e4SLinus Torvalds #include <linux/fs.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/highmem.h>
291da177e4SLinus Torvalds #include <linux/pagemap.h>
3098c4d57dSAndrew Morton #include <linux/task_io_accounting_ops.h>
311da177e4SLinus Torvalds #include <linux/bio.h>
321da177e4SLinus Torvalds #include <linux/wait.h>
331da177e4SLinus Torvalds #include <linux/err.h>
341da177e4SLinus Torvalds #include <linux/blkdev.h>
351da177e4SLinus Torvalds #include <linux/buffer_head.h>
361da177e4SLinus Torvalds #include <linux/rwsem.h>
371da177e4SLinus Torvalds #include <linux/uio.h>
3860063497SArun Sharma #include <linux/atomic.h>
3965dd2aa9SAndi Kleen #include <linux/prefetch.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * How many user pages to map in one call to get_user_pages().  This determines
43cde1ecb3SAndi Kleen  * the size of a structure in the slab cache
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds #define DIO_PAGES	64
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /*
481da177e4SLinus Torvalds  * This code generally works in units of "dio_blocks".  A dio_block is
491da177e4SLinus Torvalds  * somewhere between the hard sector size and the filesystem block size.  it
501da177e4SLinus Torvalds  * is determined on a per-invocation basis.   When talking to the filesystem
511da177e4SLinus Torvalds  * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
521da177e4SLinus Torvalds  * down by dio->blkfactor.  Similarly, fs-blocksize quantities are converted
531da177e4SLinus Torvalds  * to bio_block quantities by shifting left by blkfactor.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * If blkfactor is zero then the user's request was aligned to the filesystem's
561da177e4SLinus Torvalds  * blocksize.
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds 
59eb28be2bSAndi Kleen /* dio_state only used in the submission path */
60eb28be2bSAndi Kleen 
61eb28be2bSAndi Kleen struct dio_submit {
621da177e4SLinus Torvalds 	struct bio *bio;		/* bio under assembly */
631da177e4SLinus Torvalds 	unsigned blkbits;		/* doesn't change */
641da177e4SLinus Torvalds 	unsigned blkfactor;		/* When we're using an alignment which
651da177e4SLinus Torvalds 					   is finer than the filesystem's soft
661da177e4SLinus Torvalds 					   blocksize, this specifies how much
671da177e4SLinus Torvalds 					   finer.  blkfactor=2 means 1/4-block
681da177e4SLinus Torvalds 					   alignment.  Does not change */
691da177e4SLinus Torvalds 	unsigned start_zero_done;	/* flag: sub-blocksize zeroing has
701da177e4SLinus Torvalds 					   been performed at the start of a
711da177e4SLinus Torvalds 					   write */
721da177e4SLinus Torvalds 	int pages_in_io;		/* approximate total IO pages */
731da177e4SLinus Torvalds 	sector_t block_in_file;		/* Current offset into the underlying
741da177e4SLinus Torvalds 					   file in dio_block units. */
751da177e4SLinus Torvalds 	unsigned blocks_available;	/* At block_in_file.  changes */
760dc2bc49SAndi Kleen 	int reap_counter;		/* rate limit reaping */
771da177e4SLinus Torvalds 	sector_t final_block_in_request;/* doesn't change */
781da177e4SLinus Torvalds 	int boundary;			/* prev block is at a boundary */
791d8fa7a2SBadari Pulavarty 	get_block_t *get_block;		/* block mapping function */
80facd07b0SJosef Bacik 	dio_submit_t *submit_io;	/* IO submition function */
81eb28be2bSAndi Kleen 
82facd07b0SJosef Bacik 	loff_t logical_offset_in_bio;	/* current first logical block in bio */
831da177e4SLinus Torvalds 	sector_t final_block_in_bio;	/* current final block in bio + 1 */
841da177e4SLinus Torvalds 	sector_t next_block_for_io;	/* next block to be put under IO,
851da177e4SLinus Torvalds 					   in dio_blocks units */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	/*
881da177e4SLinus Torvalds 	 * Deferred addition of a page to the dio.  These variables are
891da177e4SLinus Torvalds 	 * private to dio_send_cur_page(), submit_page_section() and
901da177e4SLinus Torvalds 	 * dio_bio_add_page().
911da177e4SLinus Torvalds 	 */
921da177e4SLinus Torvalds 	struct page *cur_page;		/* The page */
931da177e4SLinus Torvalds 	unsigned cur_page_offset;	/* Offset into it, in bytes */
941da177e4SLinus Torvalds 	unsigned cur_page_len;		/* Nr of bytes at cur_page_offset */
951da177e4SLinus Torvalds 	sector_t cur_page_block;	/* Where it starts */
96facd07b0SJosef Bacik 	loff_t cur_page_fs_offset;	/* Offset in file */
971da177e4SLinus Torvalds 
987b2c99d1SAl Viro 	struct iov_iter *iter;
9923aee091SJeff Moyer 	/*
10023aee091SJeff Moyer 	 * Page queue.  These variables belong to dio_refill_pages() and
10123aee091SJeff Moyer 	 * dio_get_page().
10223aee091SJeff Moyer 	 */
10323aee091SJeff Moyer 	unsigned head;			/* next page to process */
10423aee091SJeff Moyer 	unsigned tail;			/* last valid page + 1 */
1057b2c99d1SAl Viro 	size_t from, to;
106eb28be2bSAndi Kleen };
107eb28be2bSAndi Kleen 
108eb28be2bSAndi Kleen /* dio_state communicated between submission path and end_io */
109eb28be2bSAndi Kleen struct dio {
110eb28be2bSAndi Kleen 	int flags;			/* doesn't change */
1118a4c1e42SMike Christie 	int op;
1128a4c1e42SMike Christie 	int op_flags;
11315c4f638SJens Axboe 	blk_qc_t bio_cookie;
11474d46992SChristoph Hellwig 	struct gendisk *bio_disk;
1150dc2bc49SAndi Kleen 	struct inode *inode;
116eb28be2bSAndi Kleen 	loff_t i_size;			/* i_size when submitted */
117eb28be2bSAndi Kleen 	dio_iodone_t *end_io;		/* IO completion function */
118eb28be2bSAndi Kleen 
11918772641SAndi Kleen 	void *private;			/* copy from map_bh.b_private */
120eb28be2bSAndi Kleen 
121eb28be2bSAndi Kleen 	/* BIO completion state */
122eb28be2bSAndi Kleen 	spinlock_t bio_lock;		/* protects BIO fields below */
1230dc2bc49SAndi Kleen 	int page_errors;		/* errno from get_user_pages() */
1240dc2bc49SAndi Kleen 	int is_async;			/* is IO async ? */
1257b7a8665SChristoph Hellwig 	bool defer_completion;		/* defer AIO completion to workqueue? */
12653cbf3b1SMing Lei 	bool should_dirty;		/* if pages should be dirtied */
1270dc2bc49SAndi Kleen 	int io_error;			/* IO error in completion path */
128eb28be2bSAndi Kleen 	unsigned long refcount;		/* direct_io_worker() and bios */
129eb28be2bSAndi Kleen 	struct bio *bio_list;		/* singly linked via bi_private */
130eb28be2bSAndi Kleen 	struct task_struct *waiter;	/* waiting task (NULL if none) */
131eb28be2bSAndi Kleen 
132eb28be2bSAndi Kleen 	/* AIO related stuff */
133eb28be2bSAndi Kleen 	struct kiocb *iocb;		/* kiocb */
134eb28be2bSAndi Kleen 	ssize_t result;                 /* IO result */
135eb28be2bSAndi Kleen 
13623aee091SJeff Moyer 	/*
13723aee091SJeff Moyer 	 * pages[] (and any fields placed after it) are not zeroed out at
13823aee091SJeff Moyer 	 * allocation time.  Don't add new fields after pages[] unless you
13923aee091SJeff Moyer 	 * wish that they not be zeroed.
14023aee091SJeff Moyer 	 */
1417b7a8665SChristoph Hellwig 	union {
14223aee091SJeff Moyer 		struct page *pages[DIO_PAGES];	/* page buffer */
1437b7a8665SChristoph Hellwig 		struct work_struct complete_work;/* deferred AIO completion */
1447b7a8665SChristoph Hellwig 	};
1456e8267f5SAndi Kleen } ____cacheline_aligned_in_smp;
1466e8267f5SAndi Kleen 
1476e8267f5SAndi Kleen static struct kmem_cache *dio_cache __read_mostly;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
1501da177e4SLinus Torvalds  * How many pages are in the queue?
1511da177e4SLinus Torvalds  */
152eb28be2bSAndi Kleen static inline unsigned dio_pages_present(struct dio_submit *sdio)
1531da177e4SLinus Torvalds {
154eb28be2bSAndi Kleen 	return sdio->tail - sdio->head;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds  * Go grab and pin some userspace pages.   Typically we'll get 64 at a time.
1591da177e4SLinus Torvalds  */
160ba253fbfSAndi Kleen static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio)
1611da177e4SLinus Torvalds {
1627b2c99d1SAl Viro 	ssize_t ret;
1631da177e4SLinus Torvalds 
1642c80929cSMiklos Szeredi 	ret = iov_iter_get_pages(sdio->iter, dio->pages, LONG_MAX, DIO_PAGES,
1657b2c99d1SAl Viro 				&sdio->from);
1661da177e4SLinus Torvalds 
1678a4c1e42SMike Christie 	if (ret < 0 && sdio->blocks_available && (dio->op == REQ_OP_WRITE)) {
168557ed1faSNick Piggin 		struct page *page = ZERO_PAGE(0);
1691da177e4SLinus Torvalds 		/*
1701da177e4SLinus Torvalds 		 * A memory fault, but the filesystem has some outstanding
1711da177e4SLinus Torvalds 		 * mapped blocks.  We need to use those blocks up to avoid
1721da177e4SLinus Torvalds 		 * leaking stale data in the file.
1731da177e4SLinus Torvalds 		 */
1741da177e4SLinus Torvalds 		if (dio->page_errors == 0)
1751da177e4SLinus Torvalds 			dio->page_errors = ret;
17609cbfeafSKirill A. Shutemov 		get_page(page);
177b5810039SNick Piggin 		dio->pages[0] = page;
178eb28be2bSAndi Kleen 		sdio->head = 0;
179eb28be2bSAndi Kleen 		sdio->tail = 1;
1807b2c99d1SAl Viro 		sdio->from = 0;
1817b2c99d1SAl Viro 		sdio->to = PAGE_SIZE;
1827b2c99d1SAl Viro 		return 0;
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	if (ret >= 0) {
1867b2c99d1SAl Viro 		iov_iter_advance(sdio->iter, ret);
1877b2c99d1SAl Viro 		ret += sdio->from;
188eb28be2bSAndi Kleen 		sdio->head = 0;
1897b2c99d1SAl Viro 		sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1907b2c99d1SAl Viro 		sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1;
1917b2c99d1SAl Viro 		return 0;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 	return ret;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  * Get another userspace page.  Returns an ERR_PTR on error.  Pages are
1981da177e4SLinus Torvalds  * buffered inside the dio so that we can call get_user_pages() against a
1991da177e4SLinus Torvalds  * decent number of pages, less frequently.  To provide nicer use of the
2001da177e4SLinus Torvalds  * L1 cache.
2011da177e4SLinus Torvalds  */
202ba253fbfSAndi Kleen static inline struct page *dio_get_page(struct dio *dio,
2036fcc5420SBoaz Harrosh 					struct dio_submit *sdio)
2041da177e4SLinus Torvalds {
205eb28be2bSAndi Kleen 	if (dio_pages_present(sdio) == 0) {
2061da177e4SLinus Torvalds 		int ret;
2071da177e4SLinus Torvalds 
208eb28be2bSAndi Kleen 		ret = dio_refill_pages(dio, sdio);
2091da177e4SLinus Torvalds 		if (ret)
2101da177e4SLinus Torvalds 			return ERR_PTR(ret);
211eb28be2bSAndi Kleen 		BUG_ON(dio_pages_present(sdio) == 0);
2121da177e4SLinus Torvalds 	}
2136fcc5420SBoaz Harrosh 	return dio->pages[sdio->head];
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2166d544bb4SZach Brown /**
2176d544bb4SZach Brown  * dio_complete() - called when all DIO BIO I/O has been completed
2186d544bb4SZach Brown  * @offset: the byte offset in the file of the completed operation
2196d544bb4SZach Brown  *
2207b7a8665SChristoph Hellwig  * This drops i_dio_count, lets interested parties know that a DIO operation
2217b7a8665SChristoph Hellwig  * has completed, and calculates the resulting return code for the operation.
2226d544bb4SZach Brown  *
2236d544bb4SZach Brown  * It lets the filesystem know if it registered an interest earlier via
2246d544bb4SZach Brown  * get_block.  Pass the private field of the map buffer_head so that
2256d544bb4SZach Brown  * filesystems can use it to hold additional state between get_block calls and
2266d544bb4SZach Brown  * dio_complete.
2271da177e4SLinus Torvalds  */
228716b9bc0SChristoph Hellwig static ssize_t dio_complete(struct dio *dio, ssize_t ret, bool is_async)
2291da177e4SLinus Torvalds {
230716b9bc0SChristoph Hellwig 	loff_t offset = dio->iocb->ki_pos;
2316d544bb4SZach Brown 	ssize_t transferred = 0;
232332391a9SLukas Czerner 	int err;
2336d544bb4SZach Brown 
2348459d86aSZach Brown 	/*
2358459d86aSZach Brown 	 * AIO submission can race with bio completion to get here while
2368459d86aSZach Brown 	 * expecting to have the last io completed by bio completion.
2378459d86aSZach Brown 	 * In that case -EIOCBQUEUED is in fact not an error we want
2388459d86aSZach Brown 	 * to preserve through this call.
2398459d86aSZach Brown 	 */
2408459d86aSZach Brown 	if (ret == -EIOCBQUEUED)
2418459d86aSZach Brown 		ret = 0;
2428459d86aSZach Brown 
2436d544bb4SZach Brown 	if (dio->result) {
2446d544bb4SZach Brown 		transferred = dio->result;
2456d544bb4SZach Brown 
2466d544bb4SZach Brown 		/* Check for short read case */
2478a4c1e42SMike Christie 		if ((dio->op == REQ_OP_READ) &&
2488a4c1e42SMike Christie 		    ((offset + transferred) > dio->i_size))
2496d544bb4SZach Brown 			transferred = dio->i_size - offset;
2504038acdbSAl Viro 		/* ignore EFAULT if some IO has been done */
2514038acdbSAl Viro 		if (unlikely(ret == -EFAULT) && transferred)
2524038acdbSAl Viro 			ret = 0;
2536d544bb4SZach Brown 	}
2546d544bb4SZach Brown 
2556d544bb4SZach Brown 	if (ret == 0)
2566d544bb4SZach Brown 		ret = dio->page_errors;
2576d544bb4SZach Brown 	if (ret == 0)
2586d544bb4SZach Brown 		ret = dio->io_error;
2596d544bb4SZach Brown 	if (ret == 0)
2606d544bb4SZach Brown 		ret = transferred;
2616d544bb4SZach Brown 
262*5e25c269SEryu Guan 	if (dio->end_io) {
263*5e25c269SEryu Guan 		// XXX: ki_pos??
264*5e25c269SEryu Guan 		err = dio->end_io(dio->iocb, offset, ret, dio->private);
265*5e25c269SEryu Guan 		if (err)
266*5e25c269SEryu Guan 			ret = err;
267*5e25c269SEryu Guan 	}
268*5e25c269SEryu Guan 
269332391a9SLukas Czerner 	/*
270332391a9SLukas Czerner 	 * Try again to invalidate clean pages which might have been cached by
271332391a9SLukas Czerner 	 * non-direct readahead, or faulted in by get_user_pages() if the source
272332391a9SLukas Czerner 	 * of the write was an mmap'ed region of the file we're writing.  Either
273332391a9SLukas Czerner 	 * one is a pretty crazy thing to do, so we don't support it 100%.  If
274332391a9SLukas Czerner 	 * this invalidation fails, tough, the write still worked...
275*5e25c269SEryu Guan 	 *
276*5e25c269SEryu Guan 	 * And this page cache invalidation has to be after dio->end_io(), as
277*5e25c269SEryu Guan 	 * some filesystems convert unwritten extents to real allocations in
278*5e25c269SEryu Guan 	 * end_io() when necessary, otherwise a racing buffer read would cache
279*5e25c269SEryu Guan 	 * zeros from unwritten extents.
280332391a9SLukas Czerner 	 */
281332391a9SLukas Czerner 	if (ret > 0 && dio->op == REQ_OP_WRITE &&
282332391a9SLukas Czerner 	    dio->inode->i_mapping->nrpages) {
283332391a9SLukas Czerner 		err = invalidate_inode_pages2_range(dio->inode->i_mapping,
284332391a9SLukas Czerner 					offset >> PAGE_SHIFT,
285332391a9SLukas Czerner 					(offset + ret - 1) >> PAGE_SHIFT);
286332391a9SLukas Czerner 		WARN_ON_ONCE(err);
287332391a9SLukas Czerner 	}
288332391a9SLukas Czerner 
289fe0f07d0SJens Axboe 	if (!(dio->flags & DIO_SKIP_DIO_COUNT))
290fe0f07d0SJens Axboe 		inode_dio_end(dio->inode);
291fe0f07d0SJens Axboe 
29202afc27fSChristoph Hellwig 	if (is_async) {
293e2592217SChristoph Hellwig 		/*
294e2592217SChristoph Hellwig 		 * generic_write_sync expects ki_pos to have been updated
295e2592217SChristoph Hellwig 		 * already, but the submission path only does this for
296e2592217SChristoph Hellwig 		 * synchronous I/O.
297e2592217SChristoph Hellwig 		 */
298e2592217SChristoph Hellwig 		dio->iocb->ki_pos += transferred;
29902afc27fSChristoph Hellwig 
3008a4c1e42SMike Christie 		if (dio->op == REQ_OP_WRITE)
301e2592217SChristoph Hellwig 			ret = generic_write_sync(dio->iocb,  transferred);
30204b2fa9fSChristoph Hellwig 		dio->iocb->ki_complete(dio->iocb, ret, 0);
30302afc27fSChristoph Hellwig 	}
30440e2e973SChristoph Hellwig 
3057b7a8665SChristoph Hellwig 	kmem_cache_free(dio_cache, dio);
3066d544bb4SZach Brown 	return ret;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3097b7a8665SChristoph Hellwig static void dio_aio_complete_work(struct work_struct *work)
3107b7a8665SChristoph Hellwig {
3117b7a8665SChristoph Hellwig 	struct dio *dio = container_of(work, struct dio, complete_work);
3127b7a8665SChristoph Hellwig 
313716b9bc0SChristoph Hellwig 	dio_complete(dio, 0, true);
3147b7a8665SChristoph Hellwig }
3157b7a8665SChristoph Hellwig 
3164e4cbee9SChristoph Hellwig static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio);
3177b7a8665SChristoph Hellwig 
3181da177e4SLinus Torvalds /*
3191da177e4SLinus Torvalds  * Asynchronous IO callback.
3201da177e4SLinus Torvalds  */
3214246a0b6SChristoph Hellwig static void dio_bio_end_aio(struct bio *bio)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct dio *dio = bio->bi_private;
3245eb6c7a2SZach Brown 	unsigned long remaining;
3255eb6c7a2SZach Brown 	unsigned long flags;
326332391a9SLukas Czerner 	bool defer_completion = false;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	/* cleanup the bio */
3291da177e4SLinus Torvalds 	dio_bio_complete(dio, bio);
3300273201eSZach Brown 
3315eb6c7a2SZach Brown 	spin_lock_irqsave(&dio->bio_lock, flags);
3325eb6c7a2SZach Brown 	remaining = --dio->refcount;
3335eb6c7a2SZach Brown 	if (remaining == 1 && dio->waiter)
33420258b2bSZach Brown 		wake_up_process(dio->waiter);
3355eb6c7a2SZach Brown 	spin_unlock_irqrestore(&dio->bio_lock, flags);
33620258b2bSZach Brown 
3378459d86aSZach Brown 	if (remaining == 0) {
338332391a9SLukas Czerner 		/*
339332391a9SLukas Czerner 		 * Defer completion when defer_completion is set or
340332391a9SLukas Czerner 		 * when the inode has pages mapped and this is AIO write.
341332391a9SLukas Czerner 		 * We need to invalidate those pages because there is a
342332391a9SLukas Czerner 		 * chance they contain stale data in the case buffered IO
343332391a9SLukas Czerner 		 * went in between AIO submission and completion into the
344332391a9SLukas Czerner 		 * same region.
345332391a9SLukas Czerner 		 */
346332391a9SLukas Czerner 		if (dio->result)
347332391a9SLukas Czerner 			defer_completion = dio->defer_completion ||
348332391a9SLukas Czerner 					   (dio->op == REQ_OP_WRITE &&
349332391a9SLukas Czerner 					    dio->inode->i_mapping->nrpages);
350332391a9SLukas Czerner 		if (defer_completion) {
3517b7a8665SChristoph Hellwig 			INIT_WORK(&dio->complete_work, dio_aio_complete_work);
3527b7a8665SChristoph Hellwig 			queue_work(dio->inode->i_sb->s_dio_done_wq,
3537b7a8665SChristoph Hellwig 				   &dio->complete_work);
3547b7a8665SChristoph Hellwig 		} else {
355716b9bc0SChristoph Hellwig 			dio_complete(dio, 0, true);
3567b7a8665SChristoph Hellwig 		}
3578459d86aSZach Brown 	}
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds /*
3611da177e4SLinus Torvalds  * The BIO completion handler simply queues the BIO up for the process-context
3621da177e4SLinus Torvalds  * handler.
3631da177e4SLinus Torvalds  *
3641da177e4SLinus Torvalds  * During I/O bi_private points at the dio.  After I/O, bi_private is used to
3651da177e4SLinus Torvalds  * implement a singly-linked list of completed BIOs, at dio->bio_list.
3661da177e4SLinus Torvalds  */
3674246a0b6SChristoph Hellwig static void dio_bio_end_io(struct bio *bio)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct dio *dio = bio->bi_private;
3701da177e4SLinus Torvalds 	unsigned long flags;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	spin_lock_irqsave(&dio->bio_lock, flags);
3731da177e4SLinus Torvalds 	bio->bi_private = dio->bio_list;
3741da177e4SLinus Torvalds 	dio->bio_list = bio;
3755eb6c7a2SZach Brown 	if (--dio->refcount == 1 && dio->waiter)
3761da177e4SLinus Torvalds 		wake_up_process(dio->waiter);
3771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dio->bio_lock, flags);
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
380facd07b0SJosef Bacik /**
381facd07b0SJosef Bacik  * dio_end_io - handle the end io action for the given bio
382facd07b0SJosef Bacik  * @bio: The direct io bio thats being completed
383facd07b0SJosef Bacik  *
384facd07b0SJosef Bacik  * This is meant to be called by any filesystem that uses their own dio_submit_t
385facd07b0SJosef Bacik  * so that the DIO specific endio actions are dealt with after the filesystem
386facd07b0SJosef Bacik  * has done it's completion work.
387facd07b0SJosef Bacik  */
3884055351cSChristoph Hellwig void dio_end_io(struct bio *bio)
389facd07b0SJosef Bacik {
390facd07b0SJosef Bacik 	struct dio *dio = bio->bi_private;
391facd07b0SJosef Bacik 
392facd07b0SJosef Bacik 	if (dio->is_async)
3934246a0b6SChristoph Hellwig 		dio_bio_end_aio(bio);
394facd07b0SJosef Bacik 	else
3954246a0b6SChristoph Hellwig 		dio_bio_end_io(bio);
396facd07b0SJosef Bacik }
397facd07b0SJosef Bacik EXPORT_SYMBOL_GPL(dio_end_io);
398facd07b0SJosef Bacik 
399ba253fbfSAndi Kleen static inline void
400eb28be2bSAndi Kleen dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
401eb28be2bSAndi Kleen 	      struct block_device *bdev,
4021da177e4SLinus Torvalds 	      sector_t first_sector, int nr_vecs)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct bio *bio;
4051da177e4SLinus Torvalds 
40620d9600cSDavid Dillow 	/*
40720d9600cSDavid Dillow 	 * bio_alloc() is guaranteed to return a bio when called with
40871baba4bSMel Gorman 	 * __GFP_RECLAIM and we request a valid number of vectors.
40920d9600cSDavid Dillow 	 */
4101da177e4SLinus Torvalds 	bio = bio_alloc(GFP_KERNEL, nr_vecs);
4111da177e4SLinus Torvalds 
41274d46992SChristoph Hellwig 	bio_set_dev(bio, bdev);
4134f024f37SKent Overstreet 	bio->bi_iter.bi_sector = first_sector;
4148a4c1e42SMike Christie 	bio_set_op_attrs(bio, dio->op, dio->op_flags);
4151da177e4SLinus Torvalds 	if (dio->is_async)
4161da177e4SLinus Torvalds 		bio->bi_end_io = dio_bio_end_aio;
4171da177e4SLinus Torvalds 	else
4181da177e4SLinus Torvalds 		bio->bi_end_io = dio_bio_end_io;
4191da177e4SLinus Torvalds 
42045d06cf7SJens Axboe 	bio->bi_write_hint = dio->iocb->ki_hint;
42145d06cf7SJens Axboe 
422eb28be2bSAndi Kleen 	sdio->bio = bio;
423eb28be2bSAndi Kleen 	sdio->logical_offset_in_bio = sdio->cur_page_fs_offset;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  * In the AIO read case we speculatively dirty the pages before starting IO.
4281da177e4SLinus Torvalds  * During IO completion, any of these pages which happen to have been written
4291da177e4SLinus Torvalds  * back will be redirtied by bio_check_pages_dirty().
4300273201eSZach Brown  *
4310273201eSZach Brown  * bios hold a dio reference between submit_bio and ->end_io.
4321da177e4SLinus Torvalds  */
433ba253fbfSAndi Kleen static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio)
4341da177e4SLinus Torvalds {
435eb28be2bSAndi Kleen 	struct bio *bio = sdio->bio;
4365eb6c7a2SZach Brown 	unsigned long flags;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	bio->bi_private = dio;
4395eb6c7a2SZach Brown 
4405eb6c7a2SZach Brown 	spin_lock_irqsave(&dio->bio_lock, flags);
4415eb6c7a2SZach Brown 	dio->refcount++;
4425eb6c7a2SZach Brown 	spin_unlock_irqrestore(&dio->bio_lock, flags);
4435eb6c7a2SZach Brown 
4448a4c1e42SMike Christie 	if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty)
4451da177e4SLinus Torvalds 		bio_set_pages_dirty(bio);
4465eb6c7a2SZach Brown 
44774d46992SChristoph Hellwig 	dio->bio_disk = bio->bi_disk;
448c1c53460SJens Axboe 
44915c4f638SJens Axboe 	if (sdio->submit_io) {
4508a4c1e42SMike Christie 		sdio->submit_io(bio, dio->inode, sdio->logical_offset_in_bio);
45115c4f638SJens Axboe 		dio->bio_cookie = BLK_QC_T_NONE;
452c1c53460SJens Axboe 	} else
4534e49ea4aSMike Christie 		dio->bio_cookie = submit_bio(bio);
4541da177e4SLinus Torvalds 
455eb28be2bSAndi Kleen 	sdio->bio = NULL;
456eb28be2bSAndi Kleen 	sdio->boundary = 0;
457eb28be2bSAndi Kleen 	sdio->logical_offset_in_bio = 0;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /*
4611da177e4SLinus Torvalds  * Release any resources in case of a failure
4621da177e4SLinus Torvalds  */
463ba253fbfSAndi Kleen static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio)
4641da177e4SLinus Torvalds {
4657b2c99d1SAl Viro 	while (sdio->head < sdio->tail)
46609cbfeafSKirill A. Shutemov 		put_page(dio->pages[sdio->head++]);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4700273201eSZach Brown  * Wait for the next BIO to complete.  Remove it and return it.  NULL is
4710273201eSZach Brown  * returned once all BIOs have been completed.  This must only be called once
4720273201eSZach Brown  * all bios have been issued so that dio->refcount can only decrease.  This
4730273201eSZach Brown  * requires that that the caller hold a reference on the dio.
4741da177e4SLinus Torvalds  */
4751da177e4SLinus Torvalds static struct bio *dio_await_one(struct dio *dio)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	unsigned long flags;
4780273201eSZach Brown 	struct bio *bio = NULL;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	spin_lock_irqsave(&dio->bio_lock, flags);
4815eb6c7a2SZach Brown 
4825eb6c7a2SZach Brown 	/*
4835eb6c7a2SZach Brown 	 * Wait as long as the list is empty and there are bios in flight.  bio
4845eb6c7a2SZach Brown 	 * completion drops the count, maybe adds to the list, and wakes while
4855eb6c7a2SZach Brown 	 * holding the bio_lock so we don't need set_current_state()'s barrier
4865eb6c7a2SZach Brown 	 * and can call it after testing our condition.
4875eb6c7a2SZach Brown 	 */
4885eb6c7a2SZach Brown 	while (dio->refcount > 1 && dio->bio_list == NULL) {
4895eb6c7a2SZach Brown 		__set_current_state(TASK_UNINTERRUPTIBLE);
4901da177e4SLinus Torvalds 		dio->waiter = current;
4911da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dio->bio_lock, flags);
492c43c83a2SChristoph Hellwig 		if (!(dio->iocb->ki_flags & IOCB_HIPRI) ||
49374d46992SChristoph Hellwig 		    !blk_mq_poll(dio->bio_disk->queue, dio->bio_cookie))
4941da177e4SLinus Torvalds 			io_schedule();
4955eb6c7a2SZach Brown 		/* wake up sets us TASK_RUNNING */
4961da177e4SLinus Torvalds 		spin_lock_irqsave(&dio->bio_lock, flags);
4971da177e4SLinus Torvalds 		dio->waiter = NULL;
4981da177e4SLinus Torvalds 	}
4990273201eSZach Brown 	if (dio->bio_list) {
5001da177e4SLinus Torvalds 		bio = dio->bio_list;
5011da177e4SLinus Torvalds 		dio->bio_list = bio->bi_private;
5020273201eSZach Brown 	}
5031da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dio->bio_lock, flags);
5041da177e4SLinus Torvalds 	return bio;
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /*
5081da177e4SLinus Torvalds  * Process one completed BIO.  No locks are held.
5091da177e4SLinus Torvalds  */
5104e4cbee9SChristoph Hellwig static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio)
5111da177e4SLinus Torvalds {
512cb34e057SKent Overstreet 	struct bio_vec *bvec;
513cb34e057SKent Overstreet 	unsigned i;
5144e4cbee9SChristoph Hellwig 	blk_status_t err = bio->bi_status;
5151da177e4SLinus Torvalds 
51603a07c92SGoldwyn Rodrigues 	if (err) {
51703a07c92SGoldwyn Rodrigues 		if (err == BLK_STS_AGAIN && (bio->bi_opf & REQ_NOWAIT))
51803a07c92SGoldwyn Rodrigues 			dio->io_error = -EAGAIN;
51903a07c92SGoldwyn Rodrigues 		else
520174e27c6SChen, Kenneth W 			dio->io_error = -EIO;
52103a07c92SGoldwyn Rodrigues 	}
5221da177e4SLinus Torvalds 
5238a4c1e42SMike Christie 	if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty) {
5247ddc971fSMike Krinkin 		bio_check_pages_dirty(bio);	/* transfers ownership */
5251da177e4SLinus Torvalds 	} else {
526cb34e057SKent Overstreet 		bio_for_each_segment_all(bvec, bio, i) {
527cb34e057SKent Overstreet 			struct page *page = bvec->bv_page;
5281da177e4SLinus Torvalds 
5298a4c1e42SMike Christie 			if (dio->op == REQ_OP_READ && !PageCompound(page) &&
53053cbf3b1SMing Lei 					dio->should_dirty)
5311da177e4SLinus Torvalds 				set_page_dirty_lock(page);
53209cbfeafSKirill A. Shutemov 			put_page(page);
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 		bio_put(bio);
5351da177e4SLinus Torvalds 	}
5369b81c842SSasha Levin 	return err;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds /*
5400273201eSZach Brown  * Wait on and process all in-flight BIOs.  This must only be called once
5410273201eSZach Brown  * all bios have been issued so that the refcount can only decrease.
5420273201eSZach Brown  * This just waits for all bios to make it through dio_bio_complete.  IO
543beb7dd86SRobert P. J. Day  * errors are propagated through dio->io_error and should be propagated via
5440273201eSZach Brown  * dio_complete().
5451da177e4SLinus Torvalds  */
5466d544bb4SZach Brown static void dio_await_completion(struct dio *dio)
5471da177e4SLinus Torvalds {
5480273201eSZach Brown 	struct bio *bio;
5490273201eSZach Brown 	do {
5500273201eSZach Brown 		bio = dio_await_one(dio);
5510273201eSZach Brown 		if (bio)
5526d544bb4SZach Brown 			dio_bio_complete(dio, bio);
5530273201eSZach Brown 	} while (bio);
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds /*
5571da177e4SLinus Torvalds  * A really large O_DIRECT read or write can generate a lot of BIOs.  So
5581da177e4SLinus Torvalds  * to keep the memory consumption sane we periodically reap any completed BIOs
5591da177e4SLinus Torvalds  * during the BIO generation phase.
5601da177e4SLinus Torvalds  *
5611da177e4SLinus Torvalds  * This also helps to limit the peak amount of pinned userspace memory.
5621da177e4SLinus Torvalds  */
563ba253fbfSAndi Kleen static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	int ret = 0;
5661da177e4SLinus Torvalds 
567eb28be2bSAndi Kleen 	if (sdio->reap_counter++ >= 64) {
5681da177e4SLinus Torvalds 		while (dio->bio_list) {
5691da177e4SLinus Torvalds 			unsigned long flags;
5701da177e4SLinus Torvalds 			struct bio *bio;
5711da177e4SLinus Torvalds 			int ret2;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 			spin_lock_irqsave(&dio->bio_lock, flags);
5741da177e4SLinus Torvalds 			bio = dio->bio_list;
5751da177e4SLinus Torvalds 			dio->bio_list = bio->bi_private;
5761da177e4SLinus Torvalds 			spin_unlock_irqrestore(&dio->bio_lock, flags);
5774e4cbee9SChristoph Hellwig 			ret2 = blk_status_to_errno(dio_bio_complete(dio, bio));
5781da177e4SLinus Torvalds 			if (ret == 0)
5791da177e4SLinus Torvalds 				ret = ret2;
5801da177e4SLinus Torvalds 		}
581eb28be2bSAndi Kleen 		sdio->reap_counter = 0;
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 	return ret;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds /*
5877b7a8665SChristoph Hellwig  * Create workqueue for deferred direct IO completions. We allocate the
5887b7a8665SChristoph Hellwig  * workqueue when it's first needed. This avoids creating workqueue for
5897b7a8665SChristoph Hellwig  * filesystems that don't need it and also allows us to create the workqueue
5907b7a8665SChristoph Hellwig  * late enough so the we can include s_id in the name of the workqueue.
5917b7a8665SChristoph Hellwig  */
592ec1b8260SChristoph Hellwig int sb_init_dio_done_wq(struct super_block *sb)
5937b7a8665SChristoph Hellwig {
59445150c43SOlof Johansson 	struct workqueue_struct *old;
5957b7a8665SChristoph Hellwig 	struct workqueue_struct *wq = alloc_workqueue("dio/%s",
5967b7a8665SChristoph Hellwig 						      WQ_MEM_RECLAIM, 0,
5977b7a8665SChristoph Hellwig 						      sb->s_id);
5987b7a8665SChristoph Hellwig 	if (!wq)
5997b7a8665SChristoph Hellwig 		return -ENOMEM;
6007b7a8665SChristoph Hellwig 	/*
6017b7a8665SChristoph Hellwig 	 * This has to be atomic as more DIOs can race to create the workqueue
6027b7a8665SChristoph Hellwig 	 */
60345150c43SOlof Johansson 	old = cmpxchg(&sb->s_dio_done_wq, NULL, wq);
6047b7a8665SChristoph Hellwig 	/* Someone created workqueue before us? Free ours... */
60545150c43SOlof Johansson 	if (old)
6067b7a8665SChristoph Hellwig 		destroy_workqueue(wq);
6077b7a8665SChristoph Hellwig 	return 0;
6087b7a8665SChristoph Hellwig }
6097b7a8665SChristoph Hellwig 
6107b7a8665SChristoph Hellwig static int dio_set_defer_completion(struct dio *dio)
6117b7a8665SChristoph Hellwig {
6127b7a8665SChristoph Hellwig 	struct super_block *sb = dio->inode->i_sb;
6137b7a8665SChristoph Hellwig 
6147b7a8665SChristoph Hellwig 	if (dio->defer_completion)
6157b7a8665SChristoph Hellwig 		return 0;
6167b7a8665SChristoph Hellwig 	dio->defer_completion = true;
6177b7a8665SChristoph Hellwig 	if (!sb->s_dio_done_wq)
6187b7a8665SChristoph Hellwig 		return sb_init_dio_done_wq(sb);
6197b7a8665SChristoph Hellwig 	return 0;
6207b7a8665SChristoph Hellwig }
6217b7a8665SChristoph Hellwig 
6227b7a8665SChristoph Hellwig /*
6231da177e4SLinus Torvalds  * Call into the fs to map some more disk blocks.  We record the current number
624eb28be2bSAndi Kleen  * of available blocks at sdio->blocks_available.  These are in units of the
62593407472SFabian Frederick  * fs blocksize, i_blocksize(inode).
6261da177e4SLinus Torvalds  *
6271da177e4SLinus Torvalds  * The fs is allowed to map lots of blocks at once.  If it wants to do that,
6281da177e4SLinus Torvalds  * it uses the passed inode-relative block number as the file offset, as usual.
6291da177e4SLinus Torvalds  *
6301d8fa7a2SBadari Pulavarty  * get_block() is passed the number of i_blkbits-sized blocks which direct_io
6311da177e4SLinus Torvalds  * has remaining to do.  The fs should not map more than this number of blocks.
6321da177e4SLinus Torvalds  *
6331da177e4SLinus Torvalds  * If the fs has mapped a lot of blocks, it should populate bh->b_size to
6341da177e4SLinus Torvalds  * indicate how much contiguous disk space has been made available at
6351da177e4SLinus Torvalds  * bh->b_blocknr.
6361da177e4SLinus Torvalds  *
6371da177e4SLinus Torvalds  * If *any* of the mapped blocks are new, then the fs must set buffer_new().
6381da177e4SLinus Torvalds  * This isn't very efficient...
6391da177e4SLinus Torvalds  *
6401da177e4SLinus Torvalds  * In the case of filesystem holes: the fs may return an arbitrarily-large
6411da177e4SLinus Torvalds  * hole by returning an appropriate value in b_size and by clearing
6421da177e4SLinus Torvalds  * buffer_mapped().  However the direct-io code will only process holes one
6431d8fa7a2SBadari Pulavarty  * block at a time - it will repeatedly call get_block() as it walks the hole.
6441da177e4SLinus Torvalds  */
64518772641SAndi Kleen static int get_more_blocks(struct dio *dio, struct dio_submit *sdio,
64618772641SAndi Kleen 			   struct buffer_head *map_bh)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	int ret;
6491da177e4SLinus Torvalds 	sector_t fs_startblk;	/* Into file, in filesystem-sized blocks */
650ae55e1aaSTao Ma 	sector_t fs_endblk;	/* Into file, in filesystem-sized blocks */
6511da177e4SLinus Torvalds 	unsigned long fs_count;	/* Number of filesystem-sized blocks */
6521da177e4SLinus Torvalds 	int create;
653ab73857eSLinus Torvalds 	unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	/*
6561da177e4SLinus Torvalds 	 * If there was a memory error and we've overwritten all the
6571da177e4SLinus Torvalds 	 * mapped blocks then we can now return that memory error
6581da177e4SLinus Torvalds 	 */
6591da177e4SLinus Torvalds 	ret = dio->page_errors;
6601da177e4SLinus Torvalds 	if (ret == 0) {
661eb28be2bSAndi Kleen 		BUG_ON(sdio->block_in_file >= sdio->final_block_in_request);
662eb28be2bSAndi Kleen 		fs_startblk = sdio->block_in_file >> sdio->blkfactor;
663ae55e1aaSTao Ma 		fs_endblk = (sdio->final_block_in_request - 1) >>
664ae55e1aaSTao Ma 					sdio->blkfactor;
665ae55e1aaSTao Ma 		fs_count = fs_endblk - fs_startblk + 1;
6661da177e4SLinus Torvalds 
6673c674e74SNathan Scott 		map_bh->b_state = 0;
668ab73857eSLinus Torvalds 		map_bh->b_size = fs_count << i_blkbits;
6693c674e74SNathan Scott 
6705fe878aeSChristoph Hellwig 		/*
6719ecd10b7SEryu Guan 		 * For writes that could fill holes inside i_size on a
6729ecd10b7SEryu Guan 		 * DIO_SKIP_HOLES filesystem we forbid block creations: only
6739ecd10b7SEryu Guan 		 * overwrites are permitted. We will return early to the caller
6749ecd10b7SEryu Guan 		 * once we see an unmapped buffer head returned, and the caller
6759ecd10b7SEryu Guan 		 * will fall back to buffered I/O.
6765fe878aeSChristoph Hellwig 		 *
6775fe878aeSChristoph Hellwig 		 * Otherwise the decision is left to the get_blocks method,
6785fe878aeSChristoph Hellwig 		 * which may decide to handle it or also return an unmapped
6795fe878aeSChristoph Hellwig 		 * buffer head.
6805fe878aeSChristoph Hellwig 		 */
6818a4c1e42SMike Christie 		create = dio->op == REQ_OP_WRITE;
6825fe878aeSChristoph Hellwig 		if (dio->flags & DIO_SKIP_HOLES) {
6839ecd10b7SEryu Guan 			if (fs_startblk <= ((i_size_read(dio->inode) - 1) >>
6849ecd10b7SEryu Guan 							i_blkbits))
6851da177e4SLinus Torvalds 				create = 0;
6861da177e4SLinus Torvalds 		}
6873c674e74SNathan Scott 
688eb28be2bSAndi Kleen 		ret = (*sdio->get_block)(dio->inode, fs_startblk,
6891da177e4SLinus Torvalds 						map_bh, create);
69018772641SAndi Kleen 
69118772641SAndi Kleen 		/* Store for completion */
69218772641SAndi Kleen 		dio->private = map_bh->b_private;
6937b7a8665SChristoph Hellwig 
6947b7a8665SChristoph Hellwig 		if (ret == 0 && buffer_defer_completion(map_bh))
6957b7a8665SChristoph Hellwig 			ret = dio_set_defer_completion(dio);
6961da177e4SLinus Torvalds 	}
6971da177e4SLinus Torvalds 	return ret;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds /*
7011da177e4SLinus Torvalds  * There is no bio.  Make one now.
7021da177e4SLinus Torvalds  */
703ba253fbfSAndi Kleen static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
70418772641SAndi Kleen 		sector_t start_sector, struct buffer_head *map_bh)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	sector_t sector;
7071da177e4SLinus Torvalds 	int ret, nr_pages;
7081da177e4SLinus Torvalds 
709eb28be2bSAndi Kleen 	ret = dio_bio_reap(dio, sdio);
7101da177e4SLinus Torvalds 	if (ret)
7111da177e4SLinus Torvalds 		goto out;
712eb28be2bSAndi Kleen 	sector = start_sector << (sdio->blkbits - 9);
713b54ffb73SKent Overstreet 	nr_pages = min(sdio->pages_in_io, BIO_MAX_PAGES);
7141da177e4SLinus Torvalds 	BUG_ON(nr_pages <= 0);
71518772641SAndi Kleen 	dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
716eb28be2bSAndi Kleen 	sdio->boundary = 0;
7171da177e4SLinus Torvalds out:
7181da177e4SLinus Torvalds 	return ret;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds /*
7221da177e4SLinus Torvalds  * Attempt to put the current chunk of 'cur_page' into the current BIO.  If
7231da177e4SLinus Torvalds  * that was successful then update final_block_in_bio and take a ref against
7241da177e4SLinus Torvalds  * the just-added page.
7251da177e4SLinus Torvalds  *
7261da177e4SLinus Torvalds  * Return zero on success.  Non-zero means the caller needs to start a new BIO.
7271da177e4SLinus Torvalds  */
728ba253fbfSAndi Kleen static inline int dio_bio_add_page(struct dio_submit *sdio)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	int ret;
7311da177e4SLinus Torvalds 
732eb28be2bSAndi Kleen 	ret = bio_add_page(sdio->bio, sdio->cur_page,
733eb28be2bSAndi Kleen 			sdio->cur_page_len, sdio->cur_page_offset);
734eb28be2bSAndi Kleen 	if (ret == sdio->cur_page_len) {
7351da177e4SLinus Torvalds 		/*
7361da177e4SLinus Torvalds 		 * Decrement count only, if we are done with this page
7371da177e4SLinus Torvalds 		 */
738eb28be2bSAndi Kleen 		if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE)
739eb28be2bSAndi Kleen 			sdio->pages_in_io--;
74009cbfeafSKirill A. Shutemov 		get_page(sdio->cur_page);
741eb28be2bSAndi Kleen 		sdio->final_block_in_bio = sdio->cur_page_block +
742eb28be2bSAndi Kleen 			(sdio->cur_page_len >> sdio->blkbits);
7431da177e4SLinus Torvalds 		ret = 0;
7441da177e4SLinus Torvalds 	} else {
7451da177e4SLinus Torvalds 		ret = 1;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 	return ret;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds /*
7511da177e4SLinus Torvalds  * Put cur_page under IO.  The section of cur_page which is described by
7521da177e4SLinus Torvalds  * cur_page_offset,cur_page_len is put into a BIO.  The section of cur_page
7531da177e4SLinus Torvalds  * starts on-disk at cur_page_block.
7541da177e4SLinus Torvalds  *
7551da177e4SLinus Torvalds  * We take a ref against the page here (on behalf of its presence in the bio).
7561da177e4SLinus Torvalds  *
7571da177e4SLinus Torvalds  * The caller of this function is responsible for removing cur_page from the
7581da177e4SLinus Torvalds  * dio, and for dropping the refcount which came from that presence.
7591da177e4SLinus Torvalds  */
760ba253fbfSAndi Kleen static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio,
76118772641SAndi Kleen 		struct buffer_head *map_bh)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	int ret = 0;
7641da177e4SLinus Torvalds 
765eb28be2bSAndi Kleen 	if (sdio->bio) {
766eb28be2bSAndi Kleen 		loff_t cur_offset = sdio->cur_page_fs_offset;
767eb28be2bSAndi Kleen 		loff_t bio_next_offset = sdio->logical_offset_in_bio +
7684f024f37SKent Overstreet 			sdio->bio->bi_iter.bi_size;
769c2c6ca41SJosef Bacik 
7701da177e4SLinus Torvalds 		/*
771c2c6ca41SJosef Bacik 		 * See whether this new request is contiguous with the old.
772c2c6ca41SJosef Bacik 		 *
773f0940ceeSNamhyung Kim 		 * Btrfs cannot handle having logically non-contiguous requests
774f0940ceeSNamhyung Kim 		 * submitted.  For example if you have
775c2c6ca41SJosef Bacik 		 *
776c2c6ca41SJosef Bacik 		 * Logical:  [0-4095][HOLE][8192-12287]
777f0940ceeSNamhyung Kim 		 * Physical: [0-4095]      [4096-8191]
778c2c6ca41SJosef Bacik 		 *
779c2c6ca41SJosef Bacik 		 * We cannot submit those pages together as one BIO.  So if our
780c2c6ca41SJosef Bacik 		 * current logical offset in the file does not equal what would
781c2c6ca41SJosef Bacik 		 * be the next logical offset in the bio, submit the bio we
782c2c6ca41SJosef Bacik 		 * have.
7831da177e4SLinus Torvalds 		 */
784eb28be2bSAndi Kleen 		if (sdio->final_block_in_bio != sdio->cur_page_block ||
785c2c6ca41SJosef Bacik 		    cur_offset != bio_next_offset)
786eb28be2bSAndi Kleen 			dio_bio_submit(dio, sdio);
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 
789eb28be2bSAndi Kleen 	if (sdio->bio == NULL) {
79018772641SAndi Kleen 		ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
7911da177e4SLinus Torvalds 		if (ret)
7921da177e4SLinus Torvalds 			goto out;
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 
795eb28be2bSAndi Kleen 	if (dio_bio_add_page(sdio) != 0) {
796eb28be2bSAndi Kleen 		dio_bio_submit(dio, sdio);
79718772641SAndi Kleen 		ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
7981da177e4SLinus Torvalds 		if (ret == 0) {
799eb28be2bSAndi Kleen 			ret = dio_bio_add_page(sdio);
8001da177e4SLinus Torvalds 			BUG_ON(ret != 0);
8011da177e4SLinus Torvalds 		}
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds out:
8041da177e4SLinus Torvalds 	return ret;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds /*
8081da177e4SLinus Torvalds  * An autonomous function to put a chunk of a page under deferred IO.
8091da177e4SLinus Torvalds  *
8101da177e4SLinus Torvalds  * The caller doesn't actually know (or care) whether this piece of page is in
8111da177e4SLinus Torvalds  * a BIO, or is under IO or whatever.  We just take care of all possible
8121da177e4SLinus Torvalds  * situations here.  The separation between the logic of do_direct_IO() and
8131da177e4SLinus Torvalds  * that of submit_page_section() is important for clarity.  Please don't break.
8141da177e4SLinus Torvalds  *
8151da177e4SLinus Torvalds  * The chunk of page starts on-disk at blocknr.
8161da177e4SLinus Torvalds  *
8171da177e4SLinus Torvalds  * We perform deferred IO, by recording the last-submitted page inside our
8181da177e4SLinus Torvalds  * private part of the dio structure.  If possible, we just expand the IO
8191da177e4SLinus Torvalds  * across that page here.
8201da177e4SLinus Torvalds  *
8211da177e4SLinus Torvalds  * If that doesn't work out then we put the old page into the bio and add this
8221da177e4SLinus Torvalds  * page to the dio instead.
8231da177e4SLinus Torvalds  */
824ba253fbfSAndi Kleen static inline int
825eb28be2bSAndi Kleen submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page,
82618772641SAndi Kleen 		    unsigned offset, unsigned len, sector_t blocknr,
82718772641SAndi Kleen 		    struct buffer_head *map_bh)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	int ret = 0;
8301da177e4SLinus Torvalds 
8318a4c1e42SMike Christie 	if (dio->op == REQ_OP_WRITE) {
83298c4d57dSAndrew Morton 		/*
83398c4d57dSAndrew Morton 		 * Read accounting is performed in submit_bio()
83498c4d57dSAndrew Morton 		 */
83598c4d57dSAndrew Morton 		task_io_account_write(len);
83698c4d57dSAndrew Morton 	}
83798c4d57dSAndrew Morton 
8381da177e4SLinus Torvalds 	/*
8391da177e4SLinus Torvalds 	 * Can we just grow the current page's presence in the dio?
8401da177e4SLinus Torvalds 	 */
841eb28be2bSAndi Kleen 	if (sdio->cur_page == page &&
842eb28be2bSAndi Kleen 	    sdio->cur_page_offset + sdio->cur_page_len == offset &&
843eb28be2bSAndi Kleen 	    sdio->cur_page_block +
844eb28be2bSAndi Kleen 	    (sdio->cur_page_len >> sdio->blkbits) == blocknr) {
845eb28be2bSAndi Kleen 		sdio->cur_page_len += len;
8461da177e4SLinus Torvalds 		goto out;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	/*
8501da177e4SLinus Torvalds 	 * If there's a deferred page already there then send it.
8511da177e4SLinus Torvalds 	 */
852eb28be2bSAndi Kleen 	if (sdio->cur_page) {
85318772641SAndi Kleen 		ret = dio_send_cur_page(dio, sdio, map_bh);
85409cbfeafSKirill A. Shutemov 		put_page(sdio->cur_page);
855eb28be2bSAndi Kleen 		sdio->cur_page = NULL;
8561da177e4SLinus Torvalds 		if (ret)
857b1058b98SJan Kara 			return ret;
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds 
86009cbfeafSKirill A. Shutemov 	get_page(page);		/* It is in dio */
861eb28be2bSAndi Kleen 	sdio->cur_page = page;
862eb28be2bSAndi Kleen 	sdio->cur_page_offset = offset;
863eb28be2bSAndi Kleen 	sdio->cur_page_len = len;
864eb28be2bSAndi Kleen 	sdio->cur_page_block = blocknr;
865eb28be2bSAndi Kleen 	sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits;
8661da177e4SLinus Torvalds out:
867b1058b98SJan Kara 	/*
868b1058b98SJan Kara 	 * If sdio->boundary then we want to schedule the IO now to
869b1058b98SJan Kara 	 * avoid metadata seeks.
870b1058b98SJan Kara 	 */
871b1058b98SJan Kara 	if (sdio->boundary) {
872b1058b98SJan Kara 		ret = dio_send_cur_page(dio, sdio, map_bh);
873899f0429SAndreas Gruenbacher 		if (sdio->bio)
874b1058b98SJan Kara 			dio_bio_submit(dio, sdio);
87509cbfeafSKirill A. Shutemov 		put_page(sdio->cur_page);
876b1058b98SJan Kara 		sdio->cur_page = NULL;
877b1058b98SJan Kara 	}
8781da177e4SLinus Torvalds 	return ret;
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds /*
8821da177e4SLinus Torvalds  * If we are not writing the entire block and get_block() allocated
8831da177e4SLinus Torvalds  * the block for us, we need to fill-in the unused portion of the
8841da177e4SLinus Torvalds  * block with zeros. This happens only if user-buffer, fileoffset or
8851da177e4SLinus Torvalds  * io length is not filesystem block-size multiple.
8861da177e4SLinus Torvalds  *
8871da177e4SLinus Torvalds  * `end' is zero if we're doing the start of the IO, 1 at the end of the
8881da177e4SLinus Torvalds  * IO.
8891da177e4SLinus Torvalds  */
890ba253fbfSAndi Kleen static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio,
891ba253fbfSAndi Kleen 		int end, struct buffer_head *map_bh)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	unsigned dio_blocks_per_fs_block;
8941da177e4SLinus Torvalds 	unsigned this_chunk_blocks;	/* In dio_blocks */
8951da177e4SLinus Torvalds 	unsigned this_chunk_bytes;
8961da177e4SLinus Torvalds 	struct page *page;
8971da177e4SLinus Torvalds 
898eb28be2bSAndi Kleen 	sdio->start_zero_done = 1;
89918772641SAndi Kleen 	if (!sdio->blkfactor || !buffer_new(map_bh))
9001da177e4SLinus Torvalds 		return;
9011da177e4SLinus Torvalds 
902eb28be2bSAndi Kleen 	dio_blocks_per_fs_block = 1 << sdio->blkfactor;
903eb28be2bSAndi Kleen 	this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1);
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	if (!this_chunk_blocks)
9061da177e4SLinus Torvalds 		return;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	/*
9091da177e4SLinus Torvalds 	 * We need to zero out part of an fs block.  It is either at the
9101da177e4SLinus Torvalds 	 * beginning or the end of the fs block.
9111da177e4SLinus Torvalds 	 */
9121da177e4SLinus Torvalds 	if (end)
9131da177e4SLinus Torvalds 		this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
9141da177e4SLinus Torvalds 
915eb28be2bSAndi Kleen 	this_chunk_bytes = this_chunk_blocks << sdio->blkbits;
9161da177e4SLinus Torvalds 
917557ed1faSNick Piggin 	page = ZERO_PAGE(0);
918eb28be2bSAndi Kleen 	if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes,
91918772641SAndi Kleen 				sdio->next_block_for_io, map_bh))
9201da177e4SLinus Torvalds 		return;
9211da177e4SLinus Torvalds 
922eb28be2bSAndi Kleen 	sdio->next_block_for_io += this_chunk_blocks;
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds /*
9261da177e4SLinus Torvalds  * Walk the user pages, and the file, mapping blocks to disk and generating
9271da177e4SLinus Torvalds  * a sequence of (page,offset,len,block) mappings.  These mappings are injected
9281da177e4SLinus Torvalds  * into submit_page_section(), which takes care of the next stage of submission
9291da177e4SLinus Torvalds  *
9301da177e4SLinus Torvalds  * Direct IO against a blockdev is different from a file.  Because we can
9311da177e4SLinus Torvalds  * happily perform page-sized but 512-byte aligned IOs.  It is important that
9321da177e4SLinus Torvalds  * blockdev IO be able to have fine alignment and large sizes.
9331da177e4SLinus Torvalds  *
9341d8fa7a2SBadari Pulavarty  * So what we do is to permit the ->get_block function to populate bh.b_size
9351da177e4SLinus Torvalds  * with the size of IO which is permitted at this offset and this i_blkbits.
9361da177e4SLinus Torvalds  *
9371da177e4SLinus Torvalds  * For best results, the blockdev should be set up with 512-byte i_blkbits and
9381d8fa7a2SBadari Pulavarty  * it should set b_size to PAGE_SIZE or more inside get_block().  This gives
9391da177e4SLinus Torvalds  * fine alignment but still allows this function to work in PAGE_SIZE units.
9401da177e4SLinus Torvalds  */
94118772641SAndi Kleen static int do_direct_IO(struct dio *dio, struct dio_submit *sdio,
94218772641SAndi Kleen 			struct buffer_head *map_bh)
9431da177e4SLinus Torvalds {
944eb28be2bSAndi Kleen 	const unsigned blkbits = sdio->blkbits;
945dd545b52SChandan Rajendra 	const unsigned i_blkbits = blkbits + sdio->blkfactor;
9461da177e4SLinus Torvalds 	int ret = 0;
9471da177e4SLinus Torvalds 
948eb28be2bSAndi Kleen 	while (sdio->block_in_file < sdio->final_block_in_request) {
9497b2c99d1SAl Viro 		struct page *page;
9507b2c99d1SAl Viro 		size_t from, to;
9516fcc5420SBoaz Harrosh 
9526fcc5420SBoaz Harrosh 		page = dio_get_page(dio, sdio);
9531da177e4SLinus Torvalds 		if (IS_ERR(page)) {
9541da177e4SLinus Torvalds 			ret = PTR_ERR(page);
9551da177e4SLinus Torvalds 			goto out;
9561da177e4SLinus Torvalds 		}
9576fcc5420SBoaz Harrosh 		from = sdio->head ? 0 : sdio->from;
9586fcc5420SBoaz Harrosh 		to = (sdio->head == sdio->tail - 1) ? sdio->to : PAGE_SIZE;
9596fcc5420SBoaz Harrosh 		sdio->head++;
9601da177e4SLinus Torvalds 
9617b2c99d1SAl Viro 		while (from < to) {
9621da177e4SLinus Torvalds 			unsigned this_chunk_bytes;	/* # of bytes mapped */
9631da177e4SLinus Torvalds 			unsigned this_chunk_blocks;	/* # of blocks */
9641da177e4SLinus Torvalds 			unsigned u;
9651da177e4SLinus Torvalds 
966eb28be2bSAndi Kleen 			if (sdio->blocks_available == 0) {
9671da177e4SLinus Torvalds 				/*
9681da177e4SLinus Torvalds 				 * Need to go and map some more disk
9691da177e4SLinus Torvalds 				 */
9701da177e4SLinus Torvalds 				unsigned long blkmask;
9711da177e4SLinus Torvalds 				unsigned long dio_remainder;
9721da177e4SLinus Torvalds 
97318772641SAndi Kleen 				ret = get_more_blocks(dio, sdio, map_bh);
9741da177e4SLinus Torvalds 				if (ret) {
97509cbfeafSKirill A. Shutemov 					put_page(page);
9761da177e4SLinus Torvalds 					goto out;
9771da177e4SLinus Torvalds 				}
9781da177e4SLinus Torvalds 				if (!buffer_mapped(map_bh))
9791da177e4SLinus Torvalds 					goto do_holes;
9801da177e4SLinus Torvalds 
981eb28be2bSAndi Kleen 				sdio->blocks_available =
982f734c89cSJan Kara 						map_bh->b_size >> blkbits;
983eb28be2bSAndi Kleen 				sdio->next_block_for_io =
984eb28be2bSAndi Kleen 					map_bh->b_blocknr << sdio->blkfactor;
985f734c89cSJan Kara 				if (buffer_new(map_bh)) {
986f734c89cSJan Kara 					clean_bdev_aliases(
987f734c89cSJan Kara 						map_bh->b_bdev,
988f734c89cSJan Kara 						map_bh->b_blocknr,
989dd545b52SChandan Rajendra 						map_bh->b_size >> i_blkbits);
990f734c89cSJan Kara 				}
9911da177e4SLinus Torvalds 
992eb28be2bSAndi Kleen 				if (!sdio->blkfactor)
9931da177e4SLinus Torvalds 					goto do_holes;
9941da177e4SLinus Torvalds 
995eb28be2bSAndi Kleen 				blkmask = (1 << sdio->blkfactor) - 1;
996eb28be2bSAndi Kleen 				dio_remainder = (sdio->block_in_file & blkmask);
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 				/*
9991da177e4SLinus Torvalds 				 * If we are at the start of IO and that IO
10001da177e4SLinus Torvalds 				 * starts partway into a fs-block,
10011da177e4SLinus Torvalds 				 * dio_remainder will be non-zero.  If the IO
10021da177e4SLinus Torvalds 				 * is a read then we can simply advance the IO
10031da177e4SLinus Torvalds 				 * cursor to the first block which is to be
10041da177e4SLinus Torvalds 				 * read.  But if the IO is a write and the
10051da177e4SLinus Torvalds 				 * block was newly allocated we cannot do that;
10061da177e4SLinus Torvalds 				 * the start of the fs block must be zeroed out
10071da177e4SLinus Torvalds 				 * on-disk
10081da177e4SLinus Torvalds 				 */
10091da177e4SLinus Torvalds 				if (!buffer_new(map_bh))
1010eb28be2bSAndi Kleen 					sdio->next_block_for_io += dio_remainder;
1011eb28be2bSAndi Kleen 				sdio->blocks_available -= dio_remainder;
10121da177e4SLinus Torvalds 			}
10131da177e4SLinus Torvalds do_holes:
10141da177e4SLinus Torvalds 			/* Handle holes */
10151da177e4SLinus Torvalds 			if (!buffer_mapped(map_bh)) {
101635dc8161SJeff Moyer 				loff_t i_size_aligned;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 				/* AKPM: eargh, -ENOTBLK is a hack */
10198a4c1e42SMike Christie 				if (dio->op == REQ_OP_WRITE) {
102009cbfeafSKirill A. Shutemov 					put_page(page);
10211da177e4SLinus Torvalds 					return -ENOTBLK;
10221da177e4SLinus Torvalds 				}
10231da177e4SLinus Torvalds 
102435dc8161SJeff Moyer 				/*
102535dc8161SJeff Moyer 				 * Be sure to account for a partial block as the
102635dc8161SJeff Moyer 				 * last block in the file
102735dc8161SJeff Moyer 				 */
102835dc8161SJeff Moyer 				i_size_aligned = ALIGN(i_size_read(dio->inode),
102935dc8161SJeff Moyer 							1 << blkbits);
1030eb28be2bSAndi Kleen 				if (sdio->block_in_file >=
103135dc8161SJeff Moyer 						i_size_aligned >> blkbits) {
10321da177e4SLinus Torvalds 					/* We hit eof */
103309cbfeafSKirill A. Shutemov 					put_page(page);
10341da177e4SLinus Torvalds 					goto out;
10351da177e4SLinus Torvalds 				}
10367b2c99d1SAl Viro 				zero_user(page, from, 1 << blkbits);
1037eb28be2bSAndi Kleen 				sdio->block_in_file++;
10387b2c99d1SAl Viro 				from += 1 << blkbits;
10393320c60bSAl Viro 				dio->result += 1 << blkbits;
10401da177e4SLinus Torvalds 				goto next_block;
10411da177e4SLinus Torvalds 			}
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 			/*
10441da177e4SLinus Torvalds 			 * If we're performing IO which has an alignment which
10451da177e4SLinus Torvalds 			 * is finer than the underlying fs, go check to see if
10461da177e4SLinus Torvalds 			 * we must zero out the start of this block.
10471da177e4SLinus Torvalds 			 */
1048eb28be2bSAndi Kleen 			if (unlikely(sdio->blkfactor && !sdio->start_zero_done))
104918772641SAndi Kleen 				dio_zero_block(dio, sdio, 0, map_bh);
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 			/*
10521da177e4SLinus Torvalds 			 * Work out, in this_chunk_blocks, how much disk we
10531da177e4SLinus Torvalds 			 * can add to this page
10541da177e4SLinus Torvalds 			 */
1055eb28be2bSAndi Kleen 			this_chunk_blocks = sdio->blocks_available;
10567b2c99d1SAl Viro 			u = (to - from) >> blkbits;
10571da177e4SLinus Torvalds 			if (this_chunk_blocks > u)
10581da177e4SLinus Torvalds 				this_chunk_blocks = u;
1059eb28be2bSAndi Kleen 			u = sdio->final_block_in_request - sdio->block_in_file;
10601da177e4SLinus Torvalds 			if (this_chunk_blocks > u)
10611da177e4SLinus Torvalds 				this_chunk_blocks = u;
10621da177e4SLinus Torvalds 			this_chunk_bytes = this_chunk_blocks << blkbits;
10631da177e4SLinus Torvalds 			BUG_ON(this_chunk_bytes == 0);
10641da177e4SLinus Torvalds 
1065092c8d46SJan Kara 			if (this_chunk_blocks == sdio->blocks_available)
1066eb28be2bSAndi Kleen 				sdio->boundary = buffer_boundary(map_bh);
1067eb28be2bSAndi Kleen 			ret = submit_page_section(dio, sdio, page,
10687b2c99d1SAl Viro 						  from,
1069eb28be2bSAndi Kleen 						  this_chunk_bytes,
107018772641SAndi Kleen 						  sdio->next_block_for_io,
107118772641SAndi Kleen 						  map_bh);
10721da177e4SLinus Torvalds 			if (ret) {
107309cbfeafSKirill A. Shutemov 				put_page(page);
10741da177e4SLinus Torvalds 				goto out;
10751da177e4SLinus Torvalds 			}
1076eb28be2bSAndi Kleen 			sdio->next_block_for_io += this_chunk_blocks;
10771da177e4SLinus Torvalds 
1078eb28be2bSAndi Kleen 			sdio->block_in_file += this_chunk_blocks;
10797b2c99d1SAl Viro 			from += this_chunk_bytes;
10807b2c99d1SAl Viro 			dio->result += this_chunk_bytes;
1081eb28be2bSAndi Kleen 			sdio->blocks_available -= this_chunk_blocks;
10821da177e4SLinus Torvalds next_block:
1083eb28be2bSAndi Kleen 			BUG_ON(sdio->block_in_file > sdio->final_block_in_request);
1084eb28be2bSAndi Kleen 			if (sdio->block_in_file == sdio->final_block_in_request)
10851da177e4SLinus Torvalds 				break;
10861da177e4SLinus Torvalds 		}
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 		/* Drop the ref which was taken in get_user_pages() */
108909cbfeafSKirill A. Shutemov 		put_page(page);
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds out:
10921da177e4SLinus Torvalds 	return ret;
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
1095847cc637SAndi Kleen static inline int drop_refcount(struct dio *dio)
10961da177e4SLinus Torvalds {
1097847cc637SAndi Kleen 	int ret2;
10985eb6c7a2SZach Brown 	unsigned long flags;
109920258b2bSZach Brown 
11001da177e4SLinus Torvalds 	/*
11018459d86aSZach Brown 	 * Sync will always be dropping the final ref and completing the
11025eb6c7a2SZach Brown 	 * operation.  AIO can if it was a broken operation described above or
11035eb6c7a2SZach Brown 	 * in fact if all the bios race to complete before we get here.  In
11045eb6c7a2SZach Brown 	 * that case dio_complete() translates the EIOCBQUEUED into the proper
110504b2fa9fSChristoph Hellwig 	 * return code that the caller will hand to ->complete().
11065eb6c7a2SZach Brown 	 *
11075eb6c7a2SZach Brown 	 * This is managed by the bio_lock instead of being an atomic_t so that
11085eb6c7a2SZach Brown 	 * completion paths can drop their ref and use the remaining count to
11095eb6c7a2SZach Brown 	 * decide to wake the submission path atomically.
11101da177e4SLinus Torvalds 	 */
11115eb6c7a2SZach Brown 	spin_lock_irqsave(&dio->bio_lock, flags);
11125eb6c7a2SZach Brown 	ret2 = --dio->refcount;
11135eb6c7a2SZach Brown 	spin_unlock_irqrestore(&dio->bio_lock, flags);
1114847cc637SAndi Kleen 	return ret2;
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
1117eafdc7d1SChristoph Hellwig /*
1118eafdc7d1SChristoph Hellwig  * This is a library function for use by filesystem drivers.
1119eafdc7d1SChristoph Hellwig  *
1120eafdc7d1SChristoph Hellwig  * The locking rules are governed by the flags parameter:
1121eafdc7d1SChristoph Hellwig  *  - if the flags value contains DIO_LOCKING we use a fancy locking
1122eafdc7d1SChristoph Hellwig  *    scheme for dumb filesystems.
1123eafdc7d1SChristoph Hellwig  *    For writes this function is called under i_mutex and returns with
1124eafdc7d1SChristoph Hellwig  *    i_mutex held, for reads, i_mutex is not held on entry, but it is
1125eafdc7d1SChristoph Hellwig  *    taken and dropped again before returning.
1126eafdc7d1SChristoph Hellwig  *  - if the flags value does NOT contain DIO_LOCKING we don't use any
1127eafdc7d1SChristoph Hellwig  *    internal locking but rather rely on the filesystem to synchronize
1128eafdc7d1SChristoph Hellwig  *    direct I/O reads/writes versus each other and truncate.
1129df2d6f26SChristoph Hellwig  *
1130df2d6f26SChristoph Hellwig  * To help with locking against truncate we incremented the i_dio_count
1131df2d6f26SChristoph Hellwig  * counter before starting direct I/O, and decrement it once we are done.
1132df2d6f26SChristoph Hellwig  * Truncate can wait for it to reach zero to provide exclusion.  It is
1133df2d6f26SChristoph Hellwig  * expected that filesystem provide exclusion between new direct I/O
1134df2d6f26SChristoph Hellwig  * and truncates.  For DIO_LOCKING filesystems this is done by i_mutex,
1135df2d6f26SChristoph Hellwig  * but other filesystems need to take care of this on their own.
1136ba253fbfSAndi Kleen  *
1137ba253fbfSAndi Kleen  * NOTE: if you pass "sdio" to anything by pointer make sure that function
1138ba253fbfSAndi Kleen  * is always inlined. Otherwise gcc is unable to split the structure into
1139ba253fbfSAndi Kleen  * individual fields and will generate much worse code. This is important
1140ba253fbfSAndi Kleen  * for the whole file.
1141eafdc7d1SChristoph Hellwig  */
114265dd2aa9SAndi Kleen static inline ssize_t
114317f8c842SOmar Sandoval do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
114417f8c842SOmar Sandoval 		      struct block_device *bdev, struct iov_iter *iter,
1145c8b8e32dSChristoph Hellwig 		      get_block_t get_block, dio_iodone_t end_io,
1146facd07b0SJosef Bacik 		      dio_submit_t submit_io, int flags)
11471da177e4SLinus Torvalds {
1148ab73857eSLinus Torvalds 	unsigned i_blkbits = ACCESS_ONCE(inode->i_blkbits);
1149ab73857eSLinus Torvalds 	unsigned blkbits = i_blkbits;
11501da177e4SLinus Torvalds 	unsigned blocksize_mask = (1 << blkbits) - 1;
11511da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
1152af436472SChristoph Hellwig 	size_t count = iov_iter_count(iter);
1153c8b8e32dSChristoph Hellwig 	loff_t offset = iocb->ki_pos;
1154af436472SChristoph Hellwig 	loff_t end = offset + count;
11551da177e4SLinus Torvalds 	struct dio *dio;
1156eb28be2bSAndi Kleen 	struct dio_submit sdio = { 0, };
1157847cc637SAndi Kleen 	struct buffer_head map_bh = { 0, };
1158647d1e4cSFengguang Wu 	struct blk_plug plug;
1159886a3911SAl Viro 	unsigned long align = offset | iov_iter_alignment(iter);
11601da177e4SLinus Torvalds 
116165dd2aa9SAndi Kleen 	/*
116265dd2aa9SAndi Kleen 	 * Avoid references to bdev if not absolutely needed to give
116365dd2aa9SAndi Kleen 	 * the early prefetch in the caller enough time.
116465dd2aa9SAndi Kleen 	 */
11651da177e4SLinus Torvalds 
1166886a3911SAl Viro 	if (align & blocksize_mask) {
11671da177e4SLinus Torvalds 		if (bdev)
116865dd2aa9SAndi Kleen 			blkbits = blksize_bits(bdev_logical_block_size(bdev));
11691da177e4SLinus Torvalds 		blocksize_mask = (1 << blkbits) - 1;
1170886a3911SAl Viro 		if (align & blocksize_mask)
11711da177e4SLinus Torvalds 			goto out;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
1174f9b5570dSChristoph Hellwig 	/* watch out for a 0 len io from a tricksy fs */
117517f8c842SOmar Sandoval 	if (iov_iter_rw(iter) == READ && !iov_iter_count(iter))
1176f9b5570dSChristoph Hellwig 		return 0;
1177f9b5570dSChristoph Hellwig 
11786e8267f5SAndi Kleen 	dio = kmem_cache_alloc(dio_cache, GFP_KERNEL);
11791da177e4SLinus Torvalds 	retval = -ENOMEM;
11801da177e4SLinus Torvalds 	if (!dio)
11811da177e4SLinus Torvalds 		goto out;
118223aee091SJeff Moyer 	/*
118323aee091SJeff Moyer 	 * Believe it or not, zeroing out the page array caused a .5%
118423aee091SJeff Moyer 	 * performance regression in a database benchmark.  So, we take
118523aee091SJeff Moyer 	 * care to only zero out what's needed.
118623aee091SJeff Moyer 	 */
118723aee091SJeff Moyer 	memset(dio, 0, offsetof(struct dio, pages));
11881da177e4SLinus Torvalds 
11895fe878aeSChristoph Hellwig 	dio->flags = flags;
11905fe878aeSChristoph Hellwig 	if (dio->flags & DIO_LOCKING) {
119117f8c842SOmar Sandoval 		if (iov_iter_rw(iter) == READ) {
11925fe878aeSChristoph Hellwig 			struct address_space *mapping =
11935fe878aeSChristoph Hellwig 					iocb->ki_filp->f_mapping;
11941da177e4SLinus Torvalds 
11955fe878aeSChristoph Hellwig 			/* will be released by direct_io_worker */
11965955102cSAl Viro 			inode_lock(inode);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 			retval = filemap_write_and_wait_range(mapping, offset,
11991da177e4SLinus Torvalds 							      end - 1);
12001da177e4SLinus Torvalds 			if (retval) {
12015955102cSAl Viro 				inode_unlock(inode);
12026e8267f5SAndi Kleen 				kmem_cache_free(dio_cache, dio);
12031da177e4SLinus Torvalds 				goto out;
12041da177e4SLinus Torvalds 			}
12051da177e4SLinus Torvalds 		}
1206df2d6f26SChristoph Hellwig 	}
12071da177e4SLinus Torvalds 
120874cedf9bSJan Kara 	/* Once we sampled i_size check for reads beyond EOF */
120974cedf9bSJan Kara 	dio->i_size = i_size_read(inode);
121074cedf9bSJan Kara 	if (iov_iter_rw(iter) == READ && offset >= dio->i_size) {
121174cedf9bSJan Kara 		if (dio->flags & DIO_LOCKING)
12125955102cSAl Viro 			inode_unlock(inode);
121374cedf9bSJan Kara 		kmem_cache_free(dio_cache, dio);
12142d4594acSAl Viro 		retval = 0;
121574cedf9bSJan Kara 		goto out;
121674cedf9bSJan Kara 	}
121774cedf9bSJan Kara 
12185fe878aeSChristoph Hellwig 	/*
121960392573SChristoph Hellwig 	 * For file extending writes updating i_size before data writeouts
122060392573SChristoph Hellwig 	 * complete can expose uninitialized blocks in dumb filesystems.
122160392573SChristoph Hellwig 	 * In that case we need to wait for I/O completion even if asked
122260392573SChristoph Hellwig 	 * for an asynchronous write.
12231da177e4SLinus Torvalds 	 */
122460392573SChristoph Hellwig 	if (is_sync_kiocb(iocb))
122560392573SChristoph Hellwig 		dio->is_async = false;
122660392573SChristoph Hellwig 	else if (!(dio->flags & DIO_ASYNC_EXTEND) &&
122717f8c842SOmar Sandoval 		 iov_iter_rw(iter) == WRITE && end > i_size_read(inode))
122860392573SChristoph Hellwig 		dio->is_async = false;
122960392573SChristoph Hellwig 	else
123060392573SChristoph Hellwig 		dio->is_async = true;
123160392573SChristoph Hellwig 
1232847cc637SAndi Kleen 	dio->inode = inode;
12338a4c1e42SMike Christie 	if (iov_iter_rw(iter) == WRITE) {
12348a4c1e42SMike Christie 		dio->op = REQ_OP_WRITE;
123570fd7614SChristoph Hellwig 		dio->op_flags = REQ_SYNC | REQ_IDLE;
123603a07c92SGoldwyn Rodrigues 		if (iocb->ki_flags & IOCB_NOWAIT)
123703a07c92SGoldwyn Rodrigues 			dio->op_flags |= REQ_NOWAIT;
12388a4c1e42SMike Christie 	} else {
12398a4c1e42SMike Christie 		dio->op = REQ_OP_READ;
12408a4c1e42SMike Christie 	}
124102afc27fSChristoph Hellwig 
124202afc27fSChristoph Hellwig 	/*
124302afc27fSChristoph Hellwig 	 * For AIO O_(D)SYNC writes we need to defer completions to a workqueue
124402afc27fSChristoph Hellwig 	 * so that we can call ->fsync.
124502afc27fSChristoph Hellwig 	 */
1246332391a9SLukas Czerner 	if (dio->is_async && iov_iter_rw(iter) == WRITE) {
1247332391a9SLukas Czerner 		retval = 0;
1248332391a9SLukas Czerner 		if ((iocb->ki_filp->f_flags & O_DSYNC) ||
1249332391a9SLukas Czerner 		    IS_SYNC(iocb->ki_filp->f_mapping->host))
125002afc27fSChristoph Hellwig 			retval = dio_set_defer_completion(dio);
1251332391a9SLukas Czerner 		else if (!dio->inode->i_sb->s_dio_done_wq) {
1252332391a9SLukas Czerner 			/*
1253332391a9SLukas Czerner 			 * In case of AIO write racing with buffered read we
1254332391a9SLukas Czerner 			 * need to defer completion. We can't decide this now,
1255332391a9SLukas Czerner 			 * however the workqueue needs to be initialized here.
1256332391a9SLukas Czerner 			 */
1257332391a9SLukas Czerner 			retval = sb_init_dio_done_wq(dio->inode->i_sb);
1258332391a9SLukas Czerner 		}
125902afc27fSChristoph Hellwig 		if (retval) {
126002afc27fSChristoph Hellwig 			/*
126102afc27fSChristoph Hellwig 			 * We grab i_mutex only for reads so we don't have
126202afc27fSChristoph Hellwig 			 * to release it here
126302afc27fSChristoph Hellwig 			 */
126402afc27fSChristoph Hellwig 			kmem_cache_free(dio_cache, dio);
126502afc27fSChristoph Hellwig 			goto out;
126602afc27fSChristoph Hellwig 		}
126702afc27fSChristoph Hellwig 	}
126802afc27fSChristoph Hellwig 
126902afc27fSChristoph Hellwig 	/*
127002afc27fSChristoph Hellwig 	 * Will be decremented at I/O completion time.
127102afc27fSChristoph Hellwig 	 */
1272fe0f07d0SJens Axboe 	if (!(dio->flags & DIO_SKIP_DIO_COUNT))
1273fe0f07d0SJens Axboe 		inode_dio_begin(inode);
127402afc27fSChristoph Hellwig 
127502afc27fSChristoph Hellwig 	retval = 0;
1276847cc637SAndi Kleen 	sdio.blkbits = blkbits;
1277ab73857eSLinus Torvalds 	sdio.blkfactor = i_blkbits - blkbits;
1278847cc637SAndi Kleen 	sdio.block_in_file = offset >> blkbits;
1279847cc637SAndi Kleen 
1280847cc637SAndi Kleen 	sdio.get_block = get_block;
1281847cc637SAndi Kleen 	dio->end_io = end_io;
1282847cc637SAndi Kleen 	sdio.submit_io = submit_io;
1283847cc637SAndi Kleen 	sdio.final_block_in_bio = -1;
1284847cc637SAndi Kleen 	sdio.next_block_for_io = -1;
1285847cc637SAndi Kleen 
1286847cc637SAndi Kleen 	dio->iocb = iocb;
1287847cc637SAndi Kleen 
1288847cc637SAndi Kleen 	spin_lock_init(&dio->bio_lock);
1289847cc637SAndi Kleen 	dio->refcount = 1;
1290847cc637SAndi Kleen 
129153cbf3b1SMing Lei 	dio->should_dirty = (iter->type == ITER_IOVEC);
12927b2c99d1SAl Viro 	sdio.iter = iter;
12937b2c99d1SAl Viro 	sdio.final_block_in_request =
12947b2c99d1SAl Viro 		(offset + iov_iter_count(iter)) >> blkbits;
12957b2c99d1SAl Viro 
1296847cc637SAndi Kleen 	/*
1297847cc637SAndi Kleen 	 * In case of non-aligned buffers, we may need 2 more
1298847cc637SAndi Kleen 	 * pages since we need to zero out first and last block.
1299847cc637SAndi Kleen 	 */
1300847cc637SAndi Kleen 	if (unlikely(sdio.blkfactor))
1301847cc637SAndi Kleen 		sdio.pages_in_io = 2;
1302847cc637SAndi Kleen 
1303f67da30cSAl Viro 	sdio.pages_in_io += iov_iter_npages(iter, INT_MAX);
1304847cc637SAndi Kleen 
1305647d1e4cSFengguang Wu 	blk_start_plug(&plug);
1306647d1e4cSFengguang Wu 
1307847cc637SAndi Kleen 	retval = do_direct_IO(dio, &sdio, &map_bh);
13087b2c99d1SAl Viro 	if (retval)
1309847cc637SAndi Kleen 		dio_cleanup(dio, &sdio);
1310847cc637SAndi Kleen 
1311847cc637SAndi Kleen 	if (retval == -ENOTBLK) {
1312847cc637SAndi Kleen 		/*
1313847cc637SAndi Kleen 		 * The remaining part of the request will be
1314847cc637SAndi Kleen 		 * be handled by buffered I/O when we return
1315847cc637SAndi Kleen 		 */
1316847cc637SAndi Kleen 		retval = 0;
1317847cc637SAndi Kleen 	}
1318847cc637SAndi Kleen 	/*
1319847cc637SAndi Kleen 	 * There may be some unwritten disk at the end of a part-written
1320847cc637SAndi Kleen 	 * fs-block-sized block.  Go zero that now.
1321847cc637SAndi Kleen 	 */
1322847cc637SAndi Kleen 	dio_zero_block(dio, &sdio, 1, &map_bh);
1323847cc637SAndi Kleen 
1324847cc637SAndi Kleen 	if (sdio.cur_page) {
1325847cc637SAndi Kleen 		ssize_t ret2;
1326847cc637SAndi Kleen 
1327847cc637SAndi Kleen 		ret2 = dio_send_cur_page(dio, &sdio, &map_bh);
1328847cc637SAndi Kleen 		if (retval == 0)
1329847cc637SAndi Kleen 			retval = ret2;
133009cbfeafSKirill A. Shutemov 		put_page(sdio.cur_page);
1331847cc637SAndi Kleen 		sdio.cur_page = NULL;
1332847cc637SAndi Kleen 	}
1333847cc637SAndi Kleen 	if (sdio.bio)
1334847cc637SAndi Kleen 		dio_bio_submit(dio, &sdio);
1335847cc637SAndi Kleen 
1336647d1e4cSFengguang Wu 	blk_finish_plug(&plug);
1337647d1e4cSFengguang Wu 
1338847cc637SAndi Kleen 	/*
1339847cc637SAndi Kleen 	 * It is possible that, we return short IO due to end of file.
1340847cc637SAndi Kleen 	 * In that case, we need to release all the pages we got hold on.
1341847cc637SAndi Kleen 	 */
1342847cc637SAndi Kleen 	dio_cleanup(dio, &sdio);
1343847cc637SAndi Kleen 
1344847cc637SAndi Kleen 	/*
1345847cc637SAndi Kleen 	 * All block lookups have been performed. For READ requests
1346847cc637SAndi Kleen 	 * we can let i_mutex go now that its achieved its purpose
1347847cc637SAndi Kleen 	 * of protecting us from looking up uninitialized blocks.
1348847cc637SAndi Kleen 	 */
134917f8c842SOmar Sandoval 	if (iov_iter_rw(iter) == READ && (dio->flags & DIO_LOCKING))
13505955102cSAl Viro 		inode_unlock(dio->inode);
1351847cc637SAndi Kleen 
1352847cc637SAndi Kleen 	/*
1353847cc637SAndi Kleen 	 * The only time we want to leave bios in flight is when a successful
1354847cc637SAndi Kleen 	 * partial aio read or full aio write have been setup.  In that case
1355847cc637SAndi Kleen 	 * bio completion will call aio_complete.  The only time it's safe to
1356847cc637SAndi Kleen 	 * call aio_complete is when we return -EIOCBQUEUED, so we key on that.
1357847cc637SAndi Kleen 	 * This had *better* be the only place that raises -EIOCBQUEUED.
1358847cc637SAndi Kleen 	 */
1359847cc637SAndi Kleen 	BUG_ON(retval == -EIOCBQUEUED);
1360847cc637SAndi Kleen 	if (dio->is_async && retval == 0 && dio->result &&
136117f8c842SOmar Sandoval 	    (iov_iter_rw(iter) == READ || dio->result == count))
1362847cc637SAndi Kleen 		retval = -EIOCBQUEUED;
1363af436472SChristoph Hellwig 	else
1364847cc637SAndi Kleen 		dio_await_completion(dio);
1365847cc637SAndi Kleen 
1366847cc637SAndi Kleen 	if (drop_refcount(dio) == 0) {
1367716b9bc0SChristoph Hellwig 		retval = dio_complete(dio, retval, false);
1368847cc637SAndi Kleen 	} else
1369847cc637SAndi Kleen 		BUG_ON(retval != -EIOCBQUEUED);
13701da177e4SLinus Torvalds 
13717bb46a67Snpiggin@suse.de out:
13727bb46a67Snpiggin@suse.de 	return retval;
13737bb46a67Snpiggin@suse.de }
137465dd2aa9SAndi Kleen 
137517f8c842SOmar Sandoval ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
137617f8c842SOmar Sandoval 			     struct block_device *bdev, struct iov_iter *iter,
1377c8b8e32dSChristoph Hellwig 			     get_block_t get_block,
137817f8c842SOmar Sandoval 			     dio_iodone_t end_io, dio_submit_t submit_io,
137917f8c842SOmar Sandoval 			     int flags)
138065dd2aa9SAndi Kleen {
138165dd2aa9SAndi Kleen 	/*
138265dd2aa9SAndi Kleen 	 * The block device state is needed in the end to finally
138365dd2aa9SAndi Kleen 	 * submit everything.  Since it's likely to be cache cold
138465dd2aa9SAndi Kleen 	 * prefetch it here as first thing to hide some of the
138565dd2aa9SAndi Kleen 	 * latency.
138665dd2aa9SAndi Kleen 	 *
138765dd2aa9SAndi Kleen 	 * Attempt to prefetch the pieces we likely need later.
138865dd2aa9SAndi Kleen 	 */
138965dd2aa9SAndi Kleen 	prefetch(&bdev->bd_disk->part_tbl);
139065dd2aa9SAndi Kleen 	prefetch(bdev->bd_queue);
139165dd2aa9SAndi Kleen 	prefetch((char *)bdev->bd_queue + SMP_CACHE_BYTES);
139265dd2aa9SAndi Kleen 
1393c8b8e32dSChristoph Hellwig 	return do_blockdev_direct_IO(iocb, inode, bdev, iter, get_block,
139417f8c842SOmar Sandoval 				     end_io, submit_io, flags);
139565dd2aa9SAndi Kleen }
139665dd2aa9SAndi Kleen 
13971da177e4SLinus Torvalds EXPORT_SYMBOL(__blockdev_direct_IO);
13986e8267f5SAndi Kleen 
13996e8267f5SAndi Kleen static __init int dio_init(void)
14006e8267f5SAndi Kleen {
14016e8267f5SAndi Kleen 	dio_cache = KMEM_CACHE(dio, SLAB_PANIC);
14026e8267f5SAndi Kleen 	return 0;
14036e8267f5SAndi Kleen }
14046e8267f5SAndi Kleen module_init(dio_init)
1405