xref: /openbmc/linux/fs/direct-io.c (revision 20258b2b397031649e4a41922fe803d57017df84)
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  *
81da177e4SLinus Torvalds  * 04Jul2002	akpm@zip.com.au
91da177e4SLinus Torvalds  *		Initial version
101da177e4SLinus Torvalds  * 11Sep2002	janetinc@us.ibm.com
111da177e4SLinus Torvalds  * 		added readv/writev support.
121da177e4SLinus Torvalds  * 29Oct2002	akpm@zip.com.au
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>
381da177e4SLinus Torvalds #include <asm/atomic.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /*
411da177e4SLinus Torvalds  * How many user pages to map in one call to get_user_pages().  This determines
421da177e4SLinus Torvalds  * the size of a structure on the stack.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds #define DIO_PAGES	64
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /*
471da177e4SLinus Torvalds  * This code generally works in units of "dio_blocks".  A dio_block is
481da177e4SLinus Torvalds  * somewhere between the hard sector size and the filesystem block size.  it
491da177e4SLinus Torvalds  * is determined on a per-invocation basis.   When talking to the filesystem
501da177e4SLinus Torvalds  * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
511da177e4SLinus Torvalds  * down by dio->blkfactor.  Similarly, fs-blocksize quantities are converted
521da177e4SLinus Torvalds  * to bio_block quantities by shifting left by blkfactor.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * If blkfactor is zero then the user's request was aligned to the filesystem's
551da177e4SLinus Torvalds  * blocksize.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * lock_type is DIO_LOCKING for regular files on direct-IO-naive filesystems.
581da177e4SLinus Torvalds  * This determines whether we need to do the fancy locking which prevents
591da177e4SLinus Torvalds  * direct-IO from being able to read uninitialised disk blocks.  If its zero
601b1dcc1bSJes Sorensen  * (blockdev) this locking is not done, and if it is DIO_OWN_LOCKING i_mutex is
611da177e4SLinus Torvalds  * not held for the entire direct write (taken briefly, initially, during a
621da177e4SLinus Torvalds  * direct read though, but its never held for the duration of a direct-IO).
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds struct dio {
661da177e4SLinus Torvalds 	/* BIO submission state */
671da177e4SLinus Torvalds 	struct bio *bio;		/* bio under assembly */
681da177e4SLinus Torvalds 	struct inode *inode;
691da177e4SLinus Torvalds 	int rw;
7029504ff3SDaniel McNeil 	loff_t i_size;			/* i_size when submitted */
711da177e4SLinus Torvalds 	int lock_type;			/* doesn't change */
721da177e4SLinus Torvalds 	unsigned blkbits;		/* doesn't change */
731da177e4SLinus Torvalds 	unsigned blkfactor;		/* When we're using an alignment which
741da177e4SLinus Torvalds 					   is finer than the filesystem's soft
751da177e4SLinus Torvalds 					   blocksize, this specifies how much
761da177e4SLinus Torvalds 					   finer.  blkfactor=2 means 1/4-block
771da177e4SLinus Torvalds 					   alignment.  Does not change */
781da177e4SLinus Torvalds 	unsigned start_zero_done;	/* flag: sub-blocksize zeroing has
791da177e4SLinus Torvalds 					   been performed at the start of a
801da177e4SLinus Torvalds 					   write */
811da177e4SLinus Torvalds 	int pages_in_io;		/* approximate total IO pages */
821da177e4SLinus Torvalds 	size_t	size;			/* total request size (doesn't change)*/
831da177e4SLinus Torvalds 	sector_t block_in_file;		/* Current offset into the underlying
841da177e4SLinus Torvalds 					   file in dio_block units. */
851da177e4SLinus Torvalds 	unsigned blocks_available;	/* At block_in_file.  changes */
861da177e4SLinus Torvalds 	sector_t final_block_in_request;/* doesn't change */
871da177e4SLinus Torvalds 	unsigned first_block_in_page;	/* doesn't change, Used only once */
881da177e4SLinus Torvalds 	int boundary;			/* prev block is at a boundary */
891da177e4SLinus Torvalds 	int reap_counter;		/* rate limit reaping */
901d8fa7a2SBadari Pulavarty 	get_block_t *get_block;		/* block mapping function */
911da177e4SLinus Torvalds 	dio_iodone_t *end_io;		/* IO completion function */
921da177e4SLinus Torvalds 	sector_t final_block_in_bio;	/* current final block in bio + 1 */
931da177e4SLinus Torvalds 	sector_t next_block_for_io;	/* next block to be put under IO,
941da177e4SLinus Torvalds 					   in dio_blocks units */
951d8fa7a2SBadari Pulavarty 	struct buffer_head map_bh;	/* last get_block() result */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	/*
981da177e4SLinus Torvalds 	 * Deferred addition of a page to the dio.  These variables are
991da177e4SLinus Torvalds 	 * private to dio_send_cur_page(), submit_page_section() and
1001da177e4SLinus Torvalds 	 * dio_bio_add_page().
1011da177e4SLinus Torvalds 	 */
1021da177e4SLinus Torvalds 	struct page *cur_page;		/* The page */
1031da177e4SLinus Torvalds 	unsigned cur_page_offset;	/* Offset into it, in bytes */
1041da177e4SLinus Torvalds 	unsigned cur_page_len;		/* Nr of bytes at cur_page_offset */
1051da177e4SLinus Torvalds 	sector_t cur_page_block;	/* Where it starts */
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	/*
1081da177e4SLinus Torvalds 	 * Page fetching state. These variables belong to dio_refill_pages().
1091da177e4SLinus Torvalds 	 */
1101da177e4SLinus Torvalds 	int curr_page;			/* changes */
1111da177e4SLinus Torvalds 	int total_pages;		/* doesn't change */
1121da177e4SLinus Torvalds 	unsigned long curr_user_address;/* changes */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	/*
1151da177e4SLinus Torvalds 	 * Page queue.  These variables belong to dio_refill_pages() and
1161da177e4SLinus Torvalds 	 * dio_get_page().
1171da177e4SLinus Torvalds 	 */
1181da177e4SLinus Torvalds 	struct page *pages[DIO_PAGES];	/* page buffer */
1191da177e4SLinus Torvalds 	unsigned head;			/* next page to process */
1201da177e4SLinus Torvalds 	unsigned tail;			/* last valid page + 1 */
1211da177e4SLinus Torvalds 	int page_errors;		/* errno from get_user_pages() */
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	/* BIO completion state */
1240273201eSZach Brown 	atomic_t refcount;		/* direct_io_worker() and bios */
1251da177e4SLinus Torvalds 	spinlock_t bio_lock;		/* protects BIO fields below */
1261da177e4SLinus Torvalds 	struct bio *bio_list;		/* singly linked via bi_private */
1271da177e4SLinus Torvalds 	struct task_struct *waiter;	/* waiting task (NULL if none) */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	/* AIO related stuff */
1301da177e4SLinus Torvalds 	struct kiocb *iocb;		/* kiocb */
1311da177e4SLinus Torvalds 	int is_async;			/* is IO async ? */
132174e27c6SChen, Kenneth W 	int io_error;			/* IO error in completion path */
1331da177e4SLinus Torvalds 	ssize_t result;                 /* IO result */
1341da177e4SLinus Torvalds };
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /*
1371da177e4SLinus Torvalds  * How many pages are in the queue?
1381da177e4SLinus Torvalds  */
1391da177e4SLinus Torvalds static inline unsigned dio_pages_present(struct dio *dio)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	return dio->tail - dio->head;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds  * Go grab and pin some userspace pages.   Typically we'll get 64 at a time.
1461da177e4SLinus Torvalds  */
1471da177e4SLinus Torvalds static int dio_refill_pages(struct dio *dio)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	int ret;
1501da177e4SLinus Torvalds 	int nr_pages;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
1531da177e4SLinus Torvalds 	down_read(&current->mm->mmap_sem);
1541da177e4SLinus Torvalds 	ret = get_user_pages(
1551da177e4SLinus Torvalds 		current,			/* Task for fault acounting */
1561da177e4SLinus Torvalds 		current->mm,			/* whose pages? */
1571da177e4SLinus Torvalds 		dio->curr_user_address,		/* Where from? */
1581da177e4SLinus Torvalds 		nr_pages,			/* How many pages? */
1591da177e4SLinus Torvalds 		dio->rw == READ,		/* Write to memory? */
1601da177e4SLinus Torvalds 		0,				/* force (?) */
1611da177e4SLinus Torvalds 		&dio->pages[0],
1621da177e4SLinus Torvalds 		NULL);				/* vmas */
1631da177e4SLinus Torvalds 	up_read(&current->mm->mmap_sem);
1641da177e4SLinus Torvalds 
165b31dc66aSJens Axboe 	if (ret < 0 && dio->blocks_available && (dio->rw & WRITE)) {
166b5810039SNick Piggin 		struct page *page = ZERO_PAGE(dio->curr_user_address);
1671da177e4SLinus Torvalds 		/*
1681da177e4SLinus Torvalds 		 * A memory fault, but the filesystem has some outstanding
1691da177e4SLinus Torvalds 		 * mapped blocks.  We need to use those blocks up to avoid
1701da177e4SLinus Torvalds 		 * leaking stale data in the file.
1711da177e4SLinus Torvalds 		 */
1721da177e4SLinus Torvalds 		if (dio->page_errors == 0)
1731da177e4SLinus Torvalds 			dio->page_errors = ret;
174b5810039SNick Piggin 		page_cache_get(page);
175b5810039SNick Piggin 		dio->pages[0] = page;
1761da177e4SLinus Torvalds 		dio->head = 0;
1771da177e4SLinus Torvalds 		dio->tail = 1;
1781da177e4SLinus Torvalds 		ret = 0;
1791da177e4SLinus Torvalds 		goto out;
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	if (ret >= 0) {
1831da177e4SLinus Torvalds 		dio->curr_user_address += ret * PAGE_SIZE;
1841da177e4SLinus Torvalds 		dio->curr_page += ret;
1851da177e4SLinus Torvalds 		dio->head = 0;
1861da177e4SLinus Torvalds 		dio->tail = ret;
1871da177e4SLinus Torvalds 		ret = 0;
1881da177e4SLinus Torvalds 	}
1891da177e4SLinus Torvalds out:
1901da177e4SLinus Torvalds 	return ret;
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /*
1941da177e4SLinus Torvalds  * Get another userspace page.  Returns an ERR_PTR on error.  Pages are
1951da177e4SLinus Torvalds  * buffered inside the dio so that we can call get_user_pages() against a
1961da177e4SLinus Torvalds  * decent number of pages, less frequently.  To provide nicer use of the
1971da177e4SLinus Torvalds  * L1 cache.
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds static struct page *dio_get_page(struct dio *dio)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	if (dio_pages_present(dio) == 0) {
2021da177e4SLinus Torvalds 		int ret;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 		ret = dio_refill_pages(dio);
2051da177e4SLinus Torvalds 		if (ret)
2061da177e4SLinus Torvalds 			return ERR_PTR(ret);
2071da177e4SLinus Torvalds 		BUG_ON(dio_pages_present(dio) == 0);
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 	return dio->pages[dio->head++];
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2126d544bb4SZach Brown /**
2136d544bb4SZach Brown  * dio_complete() - called when all DIO BIO I/O has been completed
2146d544bb4SZach Brown  * @offset: the byte offset in the file of the completed operation
2156d544bb4SZach Brown  *
2166d544bb4SZach Brown  * This releases locks as dictated by the locking type, lets interested parties
2176d544bb4SZach Brown  * know that a DIO operation has completed, and calculates the resulting return
2186d544bb4SZach Brown  * code for the operation.
2196d544bb4SZach Brown  *
2206d544bb4SZach Brown  * It lets the filesystem know if it registered an interest earlier via
2216d544bb4SZach Brown  * get_block.  Pass the private field of the map buffer_head so that
2226d544bb4SZach Brown  * filesystems can use it to hold additional state between get_block calls and
2236d544bb4SZach Brown  * dio_complete.
2241da177e4SLinus Torvalds  */
2256d544bb4SZach Brown static int dio_complete(struct dio *dio, loff_t offset, int ret)
2261da177e4SLinus Torvalds {
2276d544bb4SZach Brown 	ssize_t transferred = 0;
2286d544bb4SZach Brown 
2296d544bb4SZach Brown 	if (dio->result) {
2306d544bb4SZach Brown 		transferred = dio->result;
2316d544bb4SZach Brown 
2326d544bb4SZach Brown 		/* Check for short read case */
2336d544bb4SZach Brown 		if ((dio->rw == READ) && ((offset + transferred) > dio->i_size))
2346d544bb4SZach Brown 			transferred = dio->i_size - offset;
2356d544bb4SZach Brown 	}
2366d544bb4SZach Brown 
2371da177e4SLinus Torvalds 	if (dio->end_io && dio->result)
2386d544bb4SZach Brown 		dio->end_io(dio->iocb, offset, transferred,
2396d544bb4SZach Brown 			    dio->map_bh.b_private);
2401da177e4SLinus Torvalds 	if (dio->lock_type == DIO_LOCKING)
241d8aa905bSIngo Molnar 		/* lockdep: non-owner release */
242d8aa905bSIngo Molnar 		up_read_non_owner(&dio->inode->i_alloc_sem);
2436d544bb4SZach Brown 
2446d544bb4SZach Brown 	if (ret == 0)
2456d544bb4SZach Brown 		ret = dio->page_errors;
2466d544bb4SZach Brown 	if (ret == 0)
2476d544bb4SZach Brown 		ret = dio->io_error;
2486d544bb4SZach Brown 	if (ret == 0)
2496d544bb4SZach Brown 		ret = transferred;
2506d544bb4SZach Brown 
2516d544bb4SZach Brown 	return ret;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  * Called when a BIO has been processed.  If the count goes to zero then IO is
2561da177e4SLinus Torvalds  * complete and we can signal this to the AIO layer.
2571da177e4SLinus Torvalds  */
2580273201eSZach Brown static void dio_complete_aio(struct dio *dio)
2591da177e4SLinus Torvalds {
2606d544bb4SZach Brown 	int ret;
26129504ff3SDaniel McNeil 
2626d544bb4SZach Brown 	ret = dio_complete(dio, dio->iocb->ki_pos, 0);
26329504ff3SDaniel McNeil 
2641da177e4SLinus Torvalds 	/* Complete AIO later if falling back to buffered i/o */
2651da177e4SLinus Torvalds 	if (dio->result == dio->size ||
2661da177e4SLinus Torvalds 		((dio->rw == READ) && dio->result)) {
2676d544bb4SZach Brown 		aio_complete(dio->iocb, ret, 0);
2681da177e4SLinus Torvalds 		kfree(dio);
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static int dio_bio_complete(struct dio *dio, struct bio *bio);
2731da177e4SLinus Torvalds /*
2741da177e4SLinus Torvalds  * Asynchronous IO callback.
2751da177e4SLinus Torvalds  */
2761da177e4SLinus Torvalds static int dio_bio_end_aio(struct bio *bio, unsigned int bytes_done, int error)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	struct dio *dio = bio->bi_private;
279*20258b2bSZach Brown 	int waiter_holds_ref = 0;
280*20258b2bSZach Brown 	int remaining;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	if (bio->bi_size)
2831da177e4SLinus Torvalds 		return 1;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* cleanup the bio */
2861da177e4SLinus Torvalds 	dio_bio_complete(dio, bio);
2870273201eSZach Brown 
288*20258b2bSZach Brown 	waiter_holds_ref = !!dio->waiter;
289*20258b2bSZach Brown 	remaining = atomic_sub_return(1, (&dio->refcount));
290*20258b2bSZach Brown 	if (remaining == 1 && waiter_holds_ref)
291*20258b2bSZach Brown 		wake_up_process(dio->waiter);
292*20258b2bSZach Brown 
293*20258b2bSZach Brown 	if (remaining == 0)
2940273201eSZach Brown 		dio_complete_aio(dio);
2950273201eSZach Brown 
2961da177e4SLinus Torvalds 	return 0;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  * The BIO completion handler simply queues the BIO up for the process-context
3011da177e4SLinus Torvalds  * handler.
3021da177e4SLinus Torvalds  *
3031da177e4SLinus Torvalds  * During I/O bi_private points at the dio.  After I/O, bi_private is used to
3041da177e4SLinus Torvalds  * implement a singly-linked list of completed BIOs, at dio->bio_list.
3051da177e4SLinus Torvalds  */
3061da177e4SLinus Torvalds static int dio_bio_end_io(struct bio *bio, unsigned int bytes_done, int error)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	struct dio *dio = bio->bi_private;
3091da177e4SLinus Torvalds 	unsigned long flags;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	if (bio->bi_size)
3121da177e4SLinus Torvalds 		return 1;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	spin_lock_irqsave(&dio->bio_lock, flags);
3151da177e4SLinus Torvalds 	bio->bi_private = dio->bio_list;
3161da177e4SLinus Torvalds 	dio->bio_list = bio;
3170273201eSZach Brown 	if ((atomic_sub_return(1, &dio->refcount) == 1) && dio->waiter)
3181da177e4SLinus Torvalds 		wake_up_process(dio->waiter);
3191da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dio->bio_lock, flags);
3201da177e4SLinus Torvalds 	return 0;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds static int
3241da177e4SLinus Torvalds dio_bio_alloc(struct dio *dio, struct block_device *bdev,
3251da177e4SLinus Torvalds 		sector_t first_sector, int nr_vecs)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct bio *bio;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	bio = bio_alloc(GFP_KERNEL, nr_vecs);
3301da177e4SLinus Torvalds 	if (bio == NULL)
3311da177e4SLinus Torvalds 		return -ENOMEM;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	bio->bi_bdev = bdev;
3341da177e4SLinus Torvalds 	bio->bi_sector = first_sector;
3351da177e4SLinus Torvalds 	if (dio->is_async)
3361da177e4SLinus Torvalds 		bio->bi_end_io = dio_bio_end_aio;
3371da177e4SLinus Torvalds 	else
3381da177e4SLinus Torvalds 		bio->bi_end_io = dio_bio_end_io;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	dio->bio = bio;
3411da177e4SLinus Torvalds 	return 0;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds /*
3451da177e4SLinus Torvalds  * In the AIO read case we speculatively dirty the pages before starting IO.
3461da177e4SLinus Torvalds  * During IO completion, any of these pages which happen to have been written
3471da177e4SLinus Torvalds  * back will be redirtied by bio_check_pages_dirty().
3480273201eSZach Brown  *
3490273201eSZach Brown  * bios hold a dio reference between submit_bio and ->end_io.
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds static void dio_bio_submit(struct dio *dio)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	struct bio *bio = dio->bio;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	bio->bi_private = dio;
3560273201eSZach Brown 	atomic_inc(&dio->refcount);
3571da177e4SLinus Torvalds 	if (dio->is_async && dio->rw == READ)
3581da177e4SLinus Torvalds 		bio_set_pages_dirty(bio);
3591da177e4SLinus Torvalds 	submit_bio(dio->rw, bio);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	dio->bio = NULL;
3621da177e4SLinus Torvalds 	dio->boundary = 0;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /*
3661da177e4SLinus Torvalds  * Release any resources in case of a failure
3671da177e4SLinus Torvalds  */
3681da177e4SLinus Torvalds static void dio_cleanup(struct dio *dio)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	while (dio_pages_present(dio))
3711da177e4SLinus Torvalds 		page_cache_release(dio_get_page(dio));
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3740273201eSZach Brown static int wait_for_more_bios(struct dio *dio)
3750273201eSZach Brown {
3760273201eSZach Brown 	assert_spin_locked(&dio->bio_lock);
3770273201eSZach Brown 
3780273201eSZach Brown 	return (atomic_read(&dio->refcount) > 1) && (dio->bio_list == NULL);
3790273201eSZach Brown }
3800273201eSZach Brown 
3811da177e4SLinus Torvalds /*
3820273201eSZach Brown  * Wait for the next BIO to complete.  Remove it and return it.  NULL is
3830273201eSZach Brown  * returned once all BIOs have been completed.  This must only be called once
3840273201eSZach Brown  * all bios have been issued so that dio->refcount can only decrease.  This
3850273201eSZach Brown  * requires that that the caller hold a reference on the dio.
3861da177e4SLinus Torvalds  */
3871da177e4SLinus Torvalds static struct bio *dio_await_one(struct dio *dio)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	unsigned long flags;
3900273201eSZach Brown 	struct bio *bio = NULL;
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	spin_lock_irqsave(&dio->bio_lock, flags);
3930273201eSZach Brown 	while (wait_for_more_bios(dio)) {
3941da177e4SLinus Torvalds 		set_current_state(TASK_UNINTERRUPTIBLE);
3950273201eSZach Brown 		if (wait_for_more_bios(dio)) {
3961da177e4SLinus Torvalds 			dio->waiter = current;
3971da177e4SLinus Torvalds 			spin_unlock_irqrestore(&dio->bio_lock, flags);
3981da177e4SLinus Torvalds 			io_schedule();
3991da177e4SLinus Torvalds 			spin_lock_irqsave(&dio->bio_lock, flags);
4001da177e4SLinus Torvalds 			dio->waiter = NULL;
4011da177e4SLinus Torvalds 		}
4021da177e4SLinus Torvalds 		set_current_state(TASK_RUNNING);
4031da177e4SLinus Torvalds 	}
4040273201eSZach Brown 	if (dio->bio_list) {
4051da177e4SLinus Torvalds 		bio = dio->bio_list;
4061da177e4SLinus Torvalds 		dio->bio_list = bio->bi_private;
4070273201eSZach Brown 	}
4081da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dio->bio_lock, flags);
4091da177e4SLinus Torvalds 	return bio;
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /*
4131da177e4SLinus Torvalds  * Process one completed BIO.  No locks are held.
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds static int dio_bio_complete(struct dio *dio, struct bio *bio)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
4181da177e4SLinus Torvalds 	struct bio_vec *bvec = bio->bi_io_vec;
4191da177e4SLinus Torvalds 	int page_no;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (!uptodate)
422174e27c6SChen, Kenneth W 		dio->io_error = -EIO;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	if (dio->is_async && dio->rw == READ) {
4251da177e4SLinus Torvalds 		bio_check_pages_dirty(bio);	/* transfers ownership */
4261da177e4SLinus Torvalds 	} else {
4271da177e4SLinus Torvalds 		for (page_no = 0; page_no < bio->bi_vcnt; page_no++) {
4281da177e4SLinus Torvalds 			struct page *page = bvec[page_no].bv_page;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 			if (dio->rw == READ && !PageCompound(page))
4311da177e4SLinus Torvalds 				set_page_dirty_lock(page);
4321da177e4SLinus Torvalds 			page_cache_release(page);
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 		bio_put(bio);
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 	return uptodate ? 0 : -EIO;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds /*
4400273201eSZach Brown  * Wait on and process all in-flight BIOs.  This must only be called once
4410273201eSZach Brown  * all bios have been issued so that the refcount can only decrease.
4420273201eSZach Brown  * This just waits for all bios to make it through dio_bio_complete.  IO
4430273201eSZach Brown  * errors are propogated through dio->io_error and should be propogated via
4440273201eSZach Brown  * dio_complete().
4451da177e4SLinus Torvalds  */
4466d544bb4SZach Brown static void dio_await_completion(struct dio *dio)
4471da177e4SLinus Torvalds {
4480273201eSZach Brown 	struct bio *bio;
4490273201eSZach Brown 	do {
4500273201eSZach Brown 		bio = dio_await_one(dio);
4510273201eSZach Brown 		if (bio)
4526d544bb4SZach Brown 			dio_bio_complete(dio, bio);
4530273201eSZach Brown 	} while (bio);
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds /*
4571da177e4SLinus Torvalds  * A really large O_DIRECT read or write can generate a lot of BIOs.  So
4581da177e4SLinus Torvalds  * to keep the memory consumption sane we periodically reap any completed BIOs
4591da177e4SLinus Torvalds  * during the BIO generation phase.
4601da177e4SLinus Torvalds  *
4611da177e4SLinus Torvalds  * This also helps to limit the peak amount of pinned userspace memory.
4621da177e4SLinus Torvalds  */
4631da177e4SLinus Torvalds static int dio_bio_reap(struct dio *dio)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	int ret = 0;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	if (dio->reap_counter++ >= 64) {
4681da177e4SLinus Torvalds 		while (dio->bio_list) {
4691da177e4SLinus Torvalds 			unsigned long flags;
4701da177e4SLinus Torvalds 			struct bio *bio;
4711da177e4SLinus Torvalds 			int ret2;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 			spin_lock_irqsave(&dio->bio_lock, flags);
4741da177e4SLinus Torvalds 			bio = dio->bio_list;
4751da177e4SLinus Torvalds 			dio->bio_list = bio->bi_private;
4761da177e4SLinus Torvalds 			spin_unlock_irqrestore(&dio->bio_lock, flags);
4771da177e4SLinus Torvalds 			ret2 = dio_bio_complete(dio, bio);
4781da177e4SLinus Torvalds 			if (ret == 0)
4791da177e4SLinus Torvalds 				ret = ret2;
4801da177e4SLinus Torvalds 		}
4811da177e4SLinus Torvalds 		dio->reap_counter = 0;
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 	return ret;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /*
4871da177e4SLinus Torvalds  * Call into the fs to map some more disk blocks.  We record the current number
4881da177e4SLinus Torvalds  * of available blocks at dio->blocks_available.  These are in units of the
4891da177e4SLinus Torvalds  * fs blocksize, (1 << inode->i_blkbits).
4901da177e4SLinus Torvalds  *
4911da177e4SLinus Torvalds  * The fs is allowed to map lots of blocks at once.  If it wants to do that,
4921da177e4SLinus Torvalds  * it uses the passed inode-relative block number as the file offset, as usual.
4931da177e4SLinus Torvalds  *
4941d8fa7a2SBadari Pulavarty  * get_block() is passed the number of i_blkbits-sized blocks which direct_io
4951da177e4SLinus Torvalds  * has remaining to do.  The fs should not map more than this number of blocks.
4961da177e4SLinus Torvalds  *
4971da177e4SLinus Torvalds  * If the fs has mapped a lot of blocks, it should populate bh->b_size to
4981da177e4SLinus Torvalds  * indicate how much contiguous disk space has been made available at
4991da177e4SLinus Torvalds  * bh->b_blocknr.
5001da177e4SLinus Torvalds  *
5011da177e4SLinus Torvalds  * If *any* of the mapped blocks are new, then the fs must set buffer_new().
5021da177e4SLinus Torvalds  * This isn't very efficient...
5031da177e4SLinus Torvalds  *
5041da177e4SLinus Torvalds  * In the case of filesystem holes: the fs may return an arbitrarily-large
5051da177e4SLinus Torvalds  * hole by returning an appropriate value in b_size and by clearing
5061da177e4SLinus Torvalds  * buffer_mapped().  However the direct-io code will only process holes one
5071d8fa7a2SBadari Pulavarty  * block at a time - it will repeatedly call get_block() as it walks the hole.
5081da177e4SLinus Torvalds  */
5091da177e4SLinus Torvalds static int get_more_blocks(struct dio *dio)
5101da177e4SLinus Torvalds {
5111da177e4SLinus Torvalds 	int ret;
5121da177e4SLinus Torvalds 	struct buffer_head *map_bh = &dio->map_bh;
5131da177e4SLinus Torvalds 	sector_t fs_startblk;	/* Into file, in filesystem-sized blocks */
5141da177e4SLinus Torvalds 	unsigned long fs_count;	/* Number of filesystem-sized blocks */
5151da177e4SLinus Torvalds 	unsigned long dio_count;/* Number of dio_block-sized blocks */
5161da177e4SLinus Torvalds 	unsigned long blkmask;
5171da177e4SLinus Torvalds 	int create;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/*
5201da177e4SLinus Torvalds 	 * If there was a memory error and we've overwritten all the
5211da177e4SLinus Torvalds 	 * mapped blocks then we can now return that memory error
5221da177e4SLinus Torvalds 	 */
5231da177e4SLinus Torvalds 	ret = dio->page_errors;
5241da177e4SLinus Torvalds 	if (ret == 0) {
5251da177e4SLinus Torvalds 		BUG_ON(dio->block_in_file >= dio->final_block_in_request);
5261da177e4SLinus Torvalds 		fs_startblk = dio->block_in_file >> dio->blkfactor;
5271da177e4SLinus Torvalds 		dio_count = dio->final_block_in_request - dio->block_in_file;
5281da177e4SLinus Torvalds 		fs_count = dio_count >> dio->blkfactor;
5291da177e4SLinus Torvalds 		blkmask = (1 << dio->blkfactor) - 1;
5301da177e4SLinus Torvalds 		if (dio_count & blkmask)
5311da177e4SLinus Torvalds 			fs_count++;
5321da177e4SLinus Torvalds 
5333c674e74SNathan Scott 		map_bh->b_state = 0;
5343c674e74SNathan Scott 		map_bh->b_size = fs_count << dio->inode->i_blkbits;
5353c674e74SNathan Scott 
536b31dc66aSJens Axboe 		create = dio->rw & WRITE;
5371da177e4SLinus Torvalds 		if (dio->lock_type == DIO_LOCKING) {
5381da177e4SLinus Torvalds 			if (dio->block_in_file < (i_size_read(dio->inode) >>
5391da177e4SLinus Torvalds 							dio->blkbits))
5401da177e4SLinus Torvalds 				create = 0;
5411da177e4SLinus Torvalds 		} else if (dio->lock_type == DIO_NO_LOCKING) {
5421da177e4SLinus Torvalds 			create = 0;
5431da177e4SLinus Torvalds 		}
5443c674e74SNathan Scott 
5451da177e4SLinus Torvalds 		/*
5461da177e4SLinus Torvalds 		 * For writes inside i_size we forbid block creations: only
5471da177e4SLinus Torvalds 		 * overwrites are permitted.  We fall back to buffered writes
5481da177e4SLinus Torvalds 		 * at a higher level for inside-i_size block-instantiating
5491da177e4SLinus Torvalds 		 * writes.
5501da177e4SLinus Torvalds 		 */
5511d8fa7a2SBadari Pulavarty 		ret = (*dio->get_block)(dio->inode, fs_startblk,
5521da177e4SLinus Torvalds 						map_bh, create);
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 	return ret;
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /*
5581da177e4SLinus Torvalds  * There is no bio.  Make one now.
5591da177e4SLinus Torvalds  */
5601da177e4SLinus Torvalds static int dio_new_bio(struct dio *dio, sector_t start_sector)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	sector_t sector;
5631da177e4SLinus Torvalds 	int ret, nr_pages;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	ret = dio_bio_reap(dio);
5661da177e4SLinus Torvalds 	if (ret)
5671da177e4SLinus Torvalds 		goto out;
5681da177e4SLinus Torvalds 	sector = start_sector << (dio->blkbits - 9);
5691da177e4SLinus Torvalds 	nr_pages = min(dio->pages_in_io, bio_get_nr_vecs(dio->map_bh.b_bdev));
5701da177e4SLinus Torvalds 	BUG_ON(nr_pages <= 0);
5711da177e4SLinus Torvalds 	ret = dio_bio_alloc(dio, dio->map_bh.b_bdev, sector, nr_pages);
5721da177e4SLinus Torvalds 	dio->boundary = 0;
5731da177e4SLinus Torvalds out:
5741da177e4SLinus Torvalds 	return ret;
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds /*
5781da177e4SLinus Torvalds  * Attempt to put the current chunk of 'cur_page' into the current BIO.  If
5791da177e4SLinus Torvalds  * that was successful then update final_block_in_bio and take a ref against
5801da177e4SLinus Torvalds  * the just-added page.
5811da177e4SLinus Torvalds  *
5821da177e4SLinus Torvalds  * Return zero on success.  Non-zero means the caller needs to start a new BIO.
5831da177e4SLinus Torvalds  */
5841da177e4SLinus Torvalds static int dio_bio_add_page(struct dio *dio)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	int ret;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	ret = bio_add_page(dio->bio, dio->cur_page,
5891da177e4SLinus Torvalds 			dio->cur_page_len, dio->cur_page_offset);
5901da177e4SLinus Torvalds 	if (ret == dio->cur_page_len) {
5911da177e4SLinus Torvalds 		/*
5921da177e4SLinus Torvalds 		 * Decrement count only, if we are done with this page
5931da177e4SLinus Torvalds 		 */
5941da177e4SLinus Torvalds 		if ((dio->cur_page_len + dio->cur_page_offset) == PAGE_SIZE)
5951da177e4SLinus Torvalds 			dio->pages_in_io--;
5961da177e4SLinus Torvalds 		page_cache_get(dio->cur_page);
5971da177e4SLinus Torvalds 		dio->final_block_in_bio = dio->cur_page_block +
5981da177e4SLinus Torvalds 			(dio->cur_page_len >> dio->blkbits);
5991da177e4SLinus Torvalds 		ret = 0;
6001da177e4SLinus Torvalds 	} else {
6011da177e4SLinus Torvalds 		ret = 1;
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds 	return ret;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds /*
6071da177e4SLinus Torvalds  * Put cur_page under IO.  The section of cur_page which is described by
6081da177e4SLinus Torvalds  * cur_page_offset,cur_page_len is put into a BIO.  The section of cur_page
6091da177e4SLinus Torvalds  * starts on-disk at cur_page_block.
6101da177e4SLinus Torvalds  *
6111da177e4SLinus Torvalds  * We take a ref against the page here (on behalf of its presence in the bio).
6121da177e4SLinus Torvalds  *
6131da177e4SLinus Torvalds  * The caller of this function is responsible for removing cur_page from the
6141da177e4SLinus Torvalds  * dio, and for dropping the refcount which came from that presence.
6151da177e4SLinus Torvalds  */
6161da177e4SLinus Torvalds static int dio_send_cur_page(struct dio *dio)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	int ret = 0;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	if (dio->bio) {
6211da177e4SLinus Torvalds 		/*
6221da177e4SLinus Torvalds 		 * See whether this new request is contiguous with the old
6231da177e4SLinus Torvalds 		 */
6241da177e4SLinus Torvalds 		if (dio->final_block_in_bio != dio->cur_page_block)
6251da177e4SLinus Torvalds 			dio_bio_submit(dio);
6261da177e4SLinus Torvalds 		/*
6271da177e4SLinus Torvalds 		 * Submit now if the underlying fs is about to perform a
6281da177e4SLinus Torvalds 		 * metadata read
6291da177e4SLinus Torvalds 		 */
6301da177e4SLinus Torvalds 		if (dio->boundary)
6311da177e4SLinus Torvalds 			dio_bio_submit(dio);
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	if (dio->bio == NULL) {
6351da177e4SLinus Torvalds 		ret = dio_new_bio(dio, dio->cur_page_block);
6361da177e4SLinus Torvalds 		if (ret)
6371da177e4SLinus Torvalds 			goto out;
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	if (dio_bio_add_page(dio) != 0) {
6411da177e4SLinus Torvalds 		dio_bio_submit(dio);
6421da177e4SLinus Torvalds 		ret = dio_new_bio(dio, dio->cur_page_block);
6431da177e4SLinus Torvalds 		if (ret == 0) {
6441da177e4SLinus Torvalds 			ret = dio_bio_add_page(dio);
6451da177e4SLinus Torvalds 			BUG_ON(ret != 0);
6461da177e4SLinus Torvalds 		}
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds out:
6491da177e4SLinus Torvalds 	return ret;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds /*
6531da177e4SLinus Torvalds  * An autonomous function to put a chunk of a page under deferred IO.
6541da177e4SLinus Torvalds  *
6551da177e4SLinus Torvalds  * The caller doesn't actually know (or care) whether this piece of page is in
6561da177e4SLinus Torvalds  * a BIO, or is under IO or whatever.  We just take care of all possible
6571da177e4SLinus Torvalds  * situations here.  The separation between the logic of do_direct_IO() and
6581da177e4SLinus Torvalds  * that of submit_page_section() is important for clarity.  Please don't break.
6591da177e4SLinus Torvalds  *
6601da177e4SLinus Torvalds  * The chunk of page starts on-disk at blocknr.
6611da177e4SLinus Torvalds  *
6621da177e4SLinus Torvalds  * We perform deferred IO, by recording the last-submitted page inside our
6631da177e4SLinus Torvalds  * private part of the dio structure.  If possible, we just expand the IO
6641da177e4SLinus Torvalds  * across that page here.
6651da177e4SLinus Torvalds  *
6661da177e4SLinus Torvalds  * If that doesn't work out then we put the old page into the bio and add this
6671da177e4SLinus Torvalds  * page to the dio instead.
6681da177e4SLinus Torvalds  */
6691da177e4SLinus Torvalds static int
6701da177e4SLinus Torvalds submit_page_section(struct dio *dio, struct page *page,
6711da177e4SLinus Torvalds 		unsigned offset, unsigned len, sector_t blocknr)
6721da177e4SLinus Torvalds {
6731da177e4SLinus Torvalds 	int ret = 0;
6741da177e4SLinus Torvalds 
67598c4d57dSAndrew Morton 	if (dio->rw & WRITE) {
67698c4d57dSAndrew Morton 		/*
67798c4d57dSAndrew Morton 		 * Read accounting is performed in submit_bio()
67898c4d57dSAndrew Morton 		 */
67998c4d57dSAndrew Morton 		task_io_account_write(len);
68098c4d57dSAndrew Morton 	}
68198c4d57dSAndrew Morton 
6821da177e4SLinus Torvalds 	/*
6831da177e4SLinus Torvalds 	 * Can we just grow the current page's presence in the dio?
6841da177e4SLinus Torvalds 	 */
6851da177e4SLinus Torvalds 	if (	(dio->cur_page == page) &&
6861da177e4SLinus Torvalds 		(dio->cur_page_offset + dio->cur_page_len == offset) &&
6871da177e4SLinus Torvalds 		(dio->cur_page_block +
6881da177e4SLinus Torvalds 			(dio->cur_page_len >> dio->blkbits) == blocknr)) {
6891da177e4SLinus Torvalds 		dio->cur_page_len += len;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 		/*
6921da177e4SLinus Torvalds 		 * If dio->boundary then we want to schedule the IO now to
6931da177e4SLinus Torvalds 		 * avoid metadata seeks.
6941da177e4SLinus Torvalds 		 */
6951da177e4SLinus Torvalds 		if (dio->boundary) {
6961da177e4SLinus Torvalds 			ret = dio_send_cur_page(dio);
6971da177e4SLinus Torvalds 			page_cache_release(dio->cur_page);
6981da177e4SLinus Torvalds 			dio->cur_page = NULL;
6991da177e4SLinus Torvalds 		}
7001da177e4SLinus Torvalds 		goto out;
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	/*
7041da177e4SLinus Torvalds 	 * If there's a deferred page already there then send it.
7051da177e4SLinus Torvalds 	 */
7061da177e4SLinus Torvalds 	if (dio->cur_page) {
7071da177e4SLinus Torvalds 		ret = dio_send_cur_page(dio);
7081da177e4SLinus Torvalds 		page_cache_release(dio->cur_page);
7091da177e4SLinus Torvalds 		dio->cur_page = NULL;
7101da177e4SLinus Torvalds 		if (ret)
7111da177e4SLinus Torvalds 			goto out;
7121da177e4SLinus Torvalds 	}
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	page_cache_get(page);		/* It is in dio */
7151da177e4SLinus Torvalds 	dio->cur_page = page;
7161da177e4SLinus Torvalds 	dio->cur_page_offset = offset;
7171da177e4SLinus Torvalds 	dio->cur_page_len = len;
7181da177e4SLinus Torvalds 	dio->cur_page_block = blocknr;
7191da177e4SLinus Torvalds out:
7201da177e4SLinus Torvalds 	return ret;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /*
7241da177e4SLinus Torvalds  * Clean any dirty buffers in the blockdev mapping which alias newly-created
7251da177e4SLinus Torvalds  * file blocks.  Only called for S_ISREG files - blockdevs do not set
7261da177e4SLinus Torvalds  * buffer_new
7271da177e4SLinus Torvalds  */
7281da177e4SLinus Torvalds static void clean_blockdev_aliases(struct dio *dio)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	unsigned i;
7311da177e4SLinus Torvalds 	unsigned nblocks;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	nblocks = dio->map_bh.b_size >> dio->inode->i_blkbits;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	for (i = 0; i < nblocks; i++) {
7361da177e4SLinus Torvalds 		unmap_underlying_metadata(dio->map_bh.b_bdev,
7371da177e4SLinus Torvalds 					dio->map_bh.b_blocknr + i);
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds /*
7421da177e4SLinus Torvalds  * If we are not writing the entire block and get_block() allocated
7431da177e4SLinus Torvalds  * the block for us, we need to fill-in the unused portion of the
7441da177e4SLinus Torvalds  * block with zeros. This happens only if user-buffer, fileoffset or
7451da177e4SLinus Torvalds  * io length is not filesystem block-size multiple.
7461da177e4SLinus Torvalds  *
7471da177e4SLinus Torvalds  * `end' is zero if we're doing the start of the IO, 1 at the end of the
7481da177e4SLinus Torvalds  * IO.
7491da177e4SLinus Torvalds  */
7501da177e4SLinus Torvalds static void dio_zero_block(struct dio *dio, int end)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	unsigned dio_blocks_per_fs_block;
7531da177e4SLinus Torvalds 	unsigned this_chunk_blocks;	/* In dio_blocks */
7541da177e4SLinus Torvalds 	unsigned this_chunk_bytes;
7551da177e4SLinus Torvalds 	struct page *page;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	dio->start_zero_done = 1;
7581da177e4SLinus Torvalds 	if (!dio->blkfactor || !buffer_new(&dio->map_bh))
7591da177e4SLinus Torvalds 		return;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	dio_blocks_per_fs_block = 1 << dio->blkfactor;
7621da177e4SLinus Torvalds 	this_chunk_blocks = dio->block_in_file & (dio_blocks_per_fs_block - 1);
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	if (!this_chunk_blocks)
7651da177e4SLinus Torvalds 		return;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/*
7681da177e4SLinus Torvalds 	 * We need to zero out part of an fs block.  It is either at the
7691da177e4SLinus Torvalds 	 * beginning or the end of the fs block.
7701da177e4SLinus Torvalds 	 */
7711da177e4SLinus Torvalds 	if (end)
7721da177e4SLinus Torvalds 		this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	this_chunk_bytes = this_chunk_blocks << dio->blkbits;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	page = ZERO_PAGE(dio->curr_user_address);
7771da177e4SLinus Torvalds 	if (submit_page_section(dio, page, 0, this_chunk_bytes,
7781da177e4SLinus Torvalds 				dio->next_block_for_io))
7791da177e4SLinus Torvalds 		return;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	dio->next_block_for_io += this_chunk_blocks;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds /*
7851da177e4SLinus Torvalds  * Walk the user pages, and the file, mapping blocks to disk and generating
7861da177e4SLinus Torvalds  * a sequence of (page,offset,len,block) mappings.  These mappings are injected
7871da177e4SLinus Torvalds  * into submit_page_section(), which takes care of the next stage of submission
7881da177e4SLinus Torvalds  *
7891da177e4SLinus Torvalds  * Direct IO against a blockdev is different from a file.  Because we can
7901da177e4SLinus Torvalds  * happily perform page-sized but 512-byte aligned IOs.  It is important that
7911da177e4SLinus Torvalds  * blockdev IO be able to have fine alignment and large sizes.
7921da177e4SLinus Torvalds  *
7931d8fa7a2SBadari Pulavarty  * So what we do is to permit the ->get_block function to populate bh.b_size
7941da177e4SLinus Torvalds  * with the size of IO which is permitted at this offset and this i_blkbits.
7951da177e4SLinus Torvalds  *
7961da177e4SLinus Torvalds  * For best results, the blockdev should be set up with 512-byte i_blkbits and
7971d8fa7a2SBadari Pulavarty  * it should set b_size to PAGE_SIZE or more inside get_block().  This gives
7981da177e4SLinus Torvalds  * fine alignment but still allows this function to work in PAGE_SIZE units.
7991da177e4SLinus Torvalds  */
8001da177e4SLinus Torvalds static int do_direct_IO(struct dio *dio)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	const unsigned blkbits = dio->blkbits;
8031da177e4SLinus Torvalds 	const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
8041da177e4SLinus Torvalds 	struct page *page;
8051da177e4SLinus Torvalds 	unsigned block_in_page;
8061da177e4SLinus Torvalds 	struct buffer_head *map_bh = &dio->map_bh;
8071da177e4SLinus Torvalds 	int ret = 0;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/* The I/O can start at any block offset within the first page */
8101da177e4SLinus Torvalds 	block_in_page = dio->first_block_in_page;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	while (dio->block_in_file < dio->final_block_in_request) {
8131da177e4SLinus Torvalds 		page = dio_get_page(dio);
8141da177e4SLinus Torvalds 		if (IS_ERR(page)) {
8151da177e4SLinus Torvalds 			ret = PTR_ERR(page);
8161da177e4SLinus Torvalds 			goto out;
8171da177e4SLinus Torvalds 		}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 		while (block_in_page < blocks_per_page) {
8201da177e4SLinus Torvalds 			unsigned offset_in_page = block_in_page << blkbits;
8211da177e4SLinus Torvalds 			unsigned this_chunk_bytes;	/* # of bytes mapped */
8221da177e4SLinus Torvalds 			unsigned this_chunk_blocks;	/* # of blocks */
8231da177e4SLinus Torvalds 			unsigned u;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 			if (dio->blocks_available == 0) {
8261da177e4SLinus Torvalds 				/*
8271da177e4SLinus Torvalds 				 * Need to go and map some more disk
8281da177e4SLinus Torvalds 				 */
8291da177e4SLinus Torvalds 				unsigned long blkmask;
8301da177e4SLinus Torvalds 				unsigned long dio_remainder;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 				ret = get_more_blocks(dio);
8331da177e4SLinus Torvalds 				if (ret) {
8341da177e4SLinus Torvalds 					page_cache_release(page);
8351da177e4SLinus Torvalds 					goto out;
8361da177e4SLinus Torvalds 				}
8371da177e4SLinus Torvalds 				if (!buffer_mapped(map_bh))
8381da177e4SLinus Torvalds 					goto do_holes;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 				dio->blocks_available =
8411da177e4SLinus Torvalds 						map_bh->b_size >> dio->blkbits;
8421da177e4SLinus Torvalds 				dio->next_block_for_io =
8431da177e4SLinus Torvalds 					map_bh->b_blocknr << dio->blkfactor;
8441da177e4SLinus Torvalds 				if (buffer_new(map_bh))
8451da177e4SLinus Torvalds 					clean_blockdev_aliases(dio);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 				if (!dio->blkfactor)
8481da177e4SLinus Torvalds 					goto do_holes;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 				blkmask = (1 << dio->blkfactor) - 1;
8511da177e4SLinus Torvalds 				dio_remainder = (dio->block_in_file & blkmask);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 				/*
8541da177e4SLinus Torvalds 				 * If we are at the start of IO and that IO
8551da177e4SLinus Torvalds 				 * starts partway into a fs-block,
8561da177e4SLinus Torvalds 				 * dio_remainder will be non-zero.  If the IO
8571da177e4SLinus Torvalds 				 * is a read then we can simply advance the IO
8581da177e4SLinus Torvalds 				 * cursor to the first block which is to be
8591da177e4SLinus Torvalds 				 * read.  But if the IO is a write and the
8601da177e4SLinus Torvalds 				 * block was newly allocated we cannot do that;
8611da177e4SLinus Torvalds 				 * the start of the fs block must be zeroed out
8621da177e4SLinus Torvalds 				 * on-disk
8631da177e4SLinus Torvalds 				 */
8641da177e4SLinus Torvalds 				if (!buffer_new(map_bh))
8651da177e4SLinus Torvalds 					dio->next_block_for_io += dio_remainder;
8661da177e4SLinus Torvalds 				dio->blocks_available -= dio_remainder;
8671da177e4SLinus Torvalds 			}
8681da177e4SLinus Torvalds do_holes:
8691da177e4SLinus Torvalds 			/* Handle holes */
8701da177e4SLinus Torvalds 			if (!buffer_mapped(map_bh)) {
8711da177e4SLinus Torvalds 				char *kaddr;
87235dc8161SJeff Moyer 				loff_t i_size_aligned;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 				/* AKPM: eargh, -ENOTBLK is a hack */
875b31dc66aSJens Axboe 				if (dio->rw & WRITE) {
8761da177e4SLinus Torvalds 					page_cache_release(page);
8771da177e4SLinus Torvalds 					return -ENOTBLK;
8781da177e4SLinus Torvalds 				}
8791da177e4SLinus Torvalds 
88035dc8161SJeff Moyer 				/*
88135dc8161SJeff Moyer 				 * Be sure to account for a partial block as the
88235dc8161SJeff Moyer 				 * last block in the file
88335dc8161SJeff Moyer 				 */
88435dc8161SJeff Moyer 				i_size_aligned = ALIGN(i_size_read(dio->inode),
88535dc8161SJeff Moyer 							1 << blkbits);
8861da177e4SLinus Torvalds 				if (dio->block_in_file >=
88735dc8161SJeff Moyer 						i_size_aligned >> blkbits) {
8881da177e4SLinus Torvalds 					/* We hit eof */
8891da177e4SLinus Torvalds 					page_cache_release(page);
8901da177e4SLinus Torvalds 					goto out;
8911da177e4SLinus Torvalds 				}
8921da177e4SLinus Torvalds 				kaddr = kmap_atomic(page, KM_USER0);
8931da177e4SLinus Torvalds 				memset(kaddr + (block_in_page << blkbits),
8941da177e4SLinus Torvalds 						0, 1 << blkbits);
8951da177e4SLinus Torvalds 				flush_dcache_page(page);
8961da177e4SLinus Torvalds 				kunmap_atomic(kaddr, KM_USER0);
8971da177e4SLinus Torvalds 				dio->block_in_file++;
8981da177e4SLinus Torvalds 				block_in_page++;
8991da177e4SLinus Torvalds 				goto next_block;
9001da177e4SLinus Torvalds 			}
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 			/*
9031da177e4SLinus Torvalds 			 * If we're performing IO which has an alignment which
9041da177e4SLinus Torvalds 			 * is finer than the underlying fs, go check to see if
9051da177e4SLinus Torvalds 			 * we must zero out the start of this block.
9061da177e4SLinus Torvalds 			 */
9071da177e4SLinus Torvalds 			if (unlikely(dio->blkfactor && !dio->start_zero_done))
9081da177e4SLinus Torvalds 				dio_zero_block(dio, 0);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 			/*
9111da177e4SLinus Torvalds 			 * Work out, in this_chunk_blocks, how much disk we
9121da177e4SLinus Torvalds 			 * can add to this page
9131da177e4SLinus Torvalds 			 */
9141da177e4SLinus Torvalds 			this_chunk_blocks = dio->blocks_available;
9151da177e4SLinus Torvalds 			u = (PAGE_SIZE - offset_in_page) >> blkbits;
9161da177e4SLinus Torvalds 			if (this_chunk_blocks > u)
9171da177e4SLinus Torvalds 				this_chunk_blocks = u;
9181da177e4SLinus Torvalds 			u = dio->final_block_in_request - dio->block_in_file;
9191da177e4SLinus Torvalds 			if (this_chunk_blocks > u)
9201da177e4SLinus Torvalds 				this_chunk_blocks = u;
9211da177e4SLinus Torvalds 			this_chunk_bytes = this_chunk_blocks << blkbits;
9221da177e4SLinus Torvalds 			BUG_ON(this_chunk_bytes == 0);
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 			dio->boundary = buffer_boundary(map_bh);
9251da177e4SLinus Torvalds 			ret = submit_page_section(dio, page, offset_in_page,
9261da177e4SLinus Torvalds 				this_chunk_bytes, dio->next_block_for_io);
9271da177e4SLinus Torvalds 			if (ret) {
9281da177e4SLinus Torvalds 				page_cache_release(page);
9291da177e4SLinus Torvalds 				goto out;
9301da177e4SLinus Torvalds 			}
9311da177e4SLinus Torvalds 			dio->next_block_for_io += this_chunk_blocks;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 			dio->block_in_file += this_chunk_blocks;
9341da177e4SLinus Torvalds 			block_in_page += this_chunk_blocks;
9351da177e4SLinus Torvalds 			dio->blocks_available -= this_chunk_blocks;
9361da177e4SLinus Torvalds next_block:
937d4569d2eSEric Sesterhenn 			BUG_ON(dio->block_in_file > dio->final_block_in_request);
9381da177e4SLinus Torvalds 			if (dio->block_in_file == dio->final_block_in_request)
9391da177e4SLinus Torvalds 				break;
9401da177e4SLinus Torvalds 		}
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 		/* Drop the ref which was taken in get_user_pages() */
9431da177e4SLinus Torvalds 		page_cache_release(page);
9441da177e4SLinus Torvalds 		block_in_page = 0;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds out:
9471da177e4SLinus Torvalds 	return ret;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds /*
9511b1dcc1bSJes Sorensen  * Releases both i_mutex and i_alloc_sem
9521da177e4SLinus Torvalds  */
9531da177e4SLinus Torvalds static ssize_t
9541da177e4SLinus Torvalds direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
9551da177e4SLinus Torvalds 	const struct iovec *iov, loff_t offset, unsigned long nr_segs,
9561d8fa7a2SBadari Pulavarty 	unsigned blkbits, get_block_t get_block, dio_iodone_t end_io,
9571da177e4SLinus Torvalds 	struct dio *dio)
9581da177e4SLinus Torvalds {
9591da177e4SLinus Torvalds 	unsigned long user_addr;
9601da177e4SLinus Torvalds 	int seg;
9611da177e4SLinus Torvalds 	ssize_t ret = 0;
9621da177e4SLinus Torvalds 	ssize_t ret2;
9631da177e4SLinus Torvalds 	size_t bytes;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	dio->bio = NULL;
9661da177e4SLinus Torvalds 	dio->inode = inode;
9671da177e4SLinus Torvalds 	dio->rw = rw;
9681da177e4SLinus Torvalds 	dio->blkbits = blkbits;
9691da177e4SLinus Torvalds 	dio->blkfactor = inode->i_blkbits - blkbits;
9701da177e4SLinus Torvalds 	dio->start_zero_done = 0;
9711da177e4SLinus Torvalds 	dio->size = 0;
9721da177e4SLinus Torvalds 	dio->block_in_file = offset >> blkbits;
9731da177e4SLinus Torvalds 	dio->blocks_available = 0;
9741da177e4SLinus Torvalds 	dio->cur_page = NULL;
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	dio->boundary = 0;
9771da177e4SLinus Torvalds 	dio->reap_counter = 0;
9781d8fa7a2SBadari Pulavarty 	dio->get_block = get_block;
9791da177e4SLinus Torvalds 	dio->end_io = end_io;
9801da177e4SLinus Torvalds 	dio->map_bh.b_private = NULL;
9811da177e4SLinus Torvalds 	dio->final_block_in_bio = -1;
9821da177e4SLinus Torvalds 	dio->next_block_for_io = -1;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	dio->page_errors = 0;
985174e27c6SChen, Kenneth W 	dio->io_error = 0;
9861da177e4SLinus Torvalds 	dio->result = 0;
9871da177e4SLinus Torvalds 	dio->iocb = iocb;
98829504ff3SDaniel McNeil 	dio->i_size = i_size_read(inode);
9891da177e4SLinus Torvalds 
9900273201eSZach Brown 	atomic_set(&dio->refcount, 1);
9911da177e4SLinus Torvalds 	spin_lock_init(&dio->bio_lock);
9921da177e4SLinus Torvalds 	dio->bio_list = NULL;
9931da177e4SLinus Torvalds 	dio->waiter = NULL;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	/*
9961da177e4SLinus Torvalds 	 * In case of non-aligned buffers, we may need 2 more
9971da177e4SLinus Torvalds 	 * pages since we need to zero out first and last block.
9981da177e4SLinus Torvalds 	 */
9991da177e4SLinus Torvalds 	if (unlikely(dio->blkfactor))
10001da177e4SLinus Torvalds 		dio->pages_in_io = 2;
10011da177e4SLinus Torvalds 	else
10021da177e4SLinus Torvalds 		dio->pages_in_io = 0;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	for (seg = 0; seg < nr_segs; seg++) {
10051da177e4SLinus Torvalds 		user_addr = (unsigned long)iov[seg].iov_base;
10061da177e4SLinus Torvalds 		dio->pages_in_io +=
10071da177e4SLinus Torvalds 			((user_addr+iov[seg].iov_len +PAGE_SIZE-1)/PAGE_SIZE
10081da177e4SLinus Torvalds 				- user_addr/PAGE_SIZE);
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	for (seg = 0; seg < nr_segs; seg++) {
10121da177e4SLinus Torvalds 		user_addr = (unsigned long)iov[seg].iov_base;
10131da177e4SLinus Torvalds 		dio->size += bytes = iov[seg].iov_len;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 		/* Index into the first page of the first block */
10161da177e4SLinus Torvalds 		dio->first_block_in_page = (user_addr & ~PAGE_MASK) >> blkbits;
10171da177e4SLinus Torvalds 		dio->final_block_in_request = dio->block_in_file +
10181da177e4SLinus Torvalds 						(bytes >> blkbits);
10191da177e4SLinus Torvalds 		/* Page fetching state */
10201da177e4SLinus Torvalds 		dio->head = 0;
10211da177e4SLinus Torvalds 		dio->tail = 0;
10221da177e4SLinus Torvalds 		dio->curr_page = 0;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 		dio->total_pages = 0;
10251da177e4SLinus Torvalds 		if (user_addr & (PAGE_SIZE-1)) {
10261da177e4SLinus Torvalds 			dio->total_pages++;
10271da177e4SLinus Torvalds 			bytes -= PAGE_SIZE - (user_addr & (PAGE_SIZE - 1));
10281da177e4SLinus Torvalds 		}
10291da177e4SLinus Torvalds 		dio->total_pages += (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
10301da177e4SLinus Torvalds 		dio->curr_user_address = user_addr;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 		ret = do_direct_IO(dio);
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 		dio->result += iov[seg].iov_len -
10351da177e4SLinus Torvalds 			((dio->final_block_in_request - dio->block_in_file) <<
10361da177e4SLinus Torvalds 					blkbits);
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 		if (ret) {
10391da177e4SLinus Torvalds 			dio_cleanup(dio);
10401da177e4SLinus Torvalds 			break;
10411da177e4SLinus Torvalds 		}
10421da177e4SLinus Torvalds 	} /* end iovec loop */
10431da177e4SLinus Torvalds 
1044b31dc66aSJens Axboe 	if (ret == -ENOTBLK && (rw & WRITE)) {
10451da177e4SLinus Torvalds 		/*
10461da177e4SLinus Torvalds 		 * The remaining part of the request will be
10471da177e4SLinus Torvalds 		 * be handled by buffered I/O when we return
10481da177e4SLinus Torvalds 		 */
10491da177e4SLinus Torvalds 		ret = 0;
10501da177e4SLinus Torvalds 	}
10511da177e4SLinus Torvalds 	/*
10521da177e4SLinus Torvalds 	 * There may be some unwritten disk at the end of a part-written
10531da177e4SLinus Torvalds 	 * fs-block-sized block.  Go zero that now.
10541da177e4SLinus Torvalds 	 */
10551da177e4SLinus Torvalds 	dio_zero_block(dio, 1);
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	if (dio->cur_page) {
10581da177e4SLinus Torvalds 		ret2 = dio_send_cur_page(dio);
10591da177e4SLinus Torvalds 		if (ret == 0)
10601da177e4SLinus Torvalds 			ret = ret2;
10611da177e4SLinus Torvalds 		page_cache_release(dio->cur_page);
10621da177e4SLinus Torvalds 		dio->cur_page = NULL;
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 	if (dio->bio)
10651da177e4SLinus Torvalds 		dio_bio_submit(dio);
10661da177e4SLinus Torvalds 
106717a7b1d7SZach Brown 	/* All IO is now issued, send it on its way */
106817a7b1d7SZach Brown 	blk_run_address_space(inode->i_mapping);
106917a7b1d7SZach Brown 
10701da177e4SLinus Torvalds 	/*
10711da177e4SLinus Torvalds 	 * It is possible that, we return short IO due to end of file.
10721da177e4SLinus Torvalds 	 * In that case, we need to release all the pages we got hold on.
10731da177e4SLinus Torvalds 	 */
10741da177e4SLinus Torvalds 	dio_cleanup(dio);
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	/*
10771da177e4SLinus Torvalds 	 * All block lookups have been performed. For READ requests
10781b1dcc1bSJes Sorensen 	 * we can let i_mutex go now that its achieved its purpose
10791da177e4SLinus Torvalds 	 * of protecting us from looking up uninitialized blocks.
10801da177e4SLinus Torvalds 	 */
10811da177e4SLinus Torvalds 	if ((rw == READ) && (dio->lock_type == DIO_LOCKING))
10821b1dcc1bSJes Sorensen 		mutex_unlock(&dio->inode->i_mutex);
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	/*
10851da177e4SLinus Torvalds 	 * OK, all BIOs are submitted, so we can decrement bio_count to truly
10861da177e4SLinus Torvalds 	 * reflect the number of to-be-processed BIOs.
10871da177e4SLinus Torvalds 	 */
10881da177e4SLinus Torvalds 	if (dio->is_async) {
10891da177e4SLinus Torvalds 		int should_wait = 0;
10901da177e4SLinus Torvalds 
1091b31dc66aSJens Axboe 		if (dio->result < dio->size && (rw & WRITE)) {
10921da177e4SLinus Torvalds 			dio->waiter = current;
10931da177e4SLinus Torvalds 			should_wait = 1;
10941da177e4SLinus Torvalds 		}
10951da177e4SLinus Torvalds 		if (ret == 0)
10961da177e4SLinus Torvalds 			ret = dio->result;
10970273201eSZach Brown 
1098*20258b2bSZach Brown 		if (should_wait)
1099*20258b2bSZach Brown 			dio_await_completion(dio);
1100*20258b2bSZach Brown 
11010273201eSZach Brown 		/* this can free the dio */
11020273201eSZach Brown 		if (atomic_dec_and_test(&dio->refcount))
11030273201eSZach Brown 			dio_complete_aio(dio);
11040273201eSZach Brown 
1105*20258b2bSZach Brown 		if (should_wait)
11061da177e4SLinus Torvalds 			kfree(dio);
11071da177e4SLinus Torvalds 	} else {
11086d544bb4SZach Brown 		dio_await_completion(dio);
11091da177e4SLinus Torvalds 
11106d544bb4SZach Brown 		ret = dio_complete(dio, offset, ret);
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 		/* We could have also come here on an AIO file extend */
1113b31dc66aSJens Axboe 		if (!is_sync_kiocb(iocb) && (rw & WRITE) &&
11141da177e4SLinus Torvalds 		    ret >= 0 && dio->result == dio->size)
11151da177e4SLinus Torvalds 			/*
11161da177e4SLinus Torvalds 			 * For AIO writes where we have completed the
11171da177e4SLinus Torvalds 			 * i/o, we have to mark the the aio complete.
11181da177e4SLinus Torvalds 			 */
11191da177e4SLinus Torvalds 			aio_complete(iocb, ret, 0);
11200273201eSZach Brown 
11210273201eSZach Brown 		if (atomic_dec_and_test(&dio->refcount))
11221da177e4SLinus Torvalds 			kfree(dio);
11230273201eSZach Brown 		else
11240273201eSZach Brown 			BUG();
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 	return ret;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds /*
11301da177e4SLinus Torvalds  * This is a library function for use by filesystem drivers.
11311da177e4SLinus Torvalds  * The locking rules are governed by the dio_lock_type parameter.
11321da177e4SLinus Torvalds  *
11331da177e4SLinus Torvalds  * DIO_NO_LOCKING (no locking, for raw block device access)
11341b1dcc1bSJes Sorensen  * For writes, i_mutex is not held on entry; it is never taken.
11351da177e4SLinus Torvalds  *
11361da177e4SLinus Torvalds  * DIO_LOCKING (simple locking for regular files)
11373fb962bdSNathan Scott  * For writes we are called under i_mutex and return with i_mutex held, even
11383fb962bdSNathan Scott  * though it is internally dropped.
11391b1dcc1bSJes Sorensen  * For reads, i_mutex is not held on entry, but it is taken and dropped before
11401da177e4SLinus Torvalds  * returning.
11411da177e4SLinus Torvalds  *
11421da177e4SLinus Torvalds  * DIO_OWN_LOCKING (filesystem provides synchronisation and handling of
11431da177e4SLinus Torvalds  *	uninitialised data, allowing parallel direct readers and writers)
11441b1dcc1bSJes Sorensen  * For writes we are called without i_mutex, return without it, never touch it.
11453fb962bdSNathan Scott  * For reads we are called under i_mutex and return with i_mutex held, even
11463fb962bdSNathan Scott  * though it may be internally dropped.
11471da177e4SLinus Torvalds  *
11481da177e4SLinus Torvalds  * Additional i_alloc_sem locking requirements described inline below.
11491da177e4SLinus Torvalds  */
11501da177e4SLinus Torvalds ssize_t
11511da177e4SLinus Torvalds __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
11521da177e4SLinus Torvalds 	struct block_device *bdev, const struct iovec *iov, loff_t offset,
11531d8fa7a2SBadari Pulavarty 	unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
11541da177e4SLinus Torvalds 	int dio_lock_type)
11551da177e4SLinus Torvalds {
11561da177e4SLinus Torvalds 	int seg;
11571da177e4SLinus Torvalds 	size_t size;
11581da177e4SLinus Torvalds 	unsigned long addr;
11591da177e4SLinus Torvalds 	unsigned blkbits = inode->i_blkbits;
11601da177e4SLinus Torvalds 	unsigned bdev_blkbits = 0;
11611da177e4SLinus Torvalds 	unsigned blocksize_mask = (1 << blkbits) - 1;
11621da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
11631da177e4SLinus Torvalds 	loff_t end = offset;
11641da177e4SLinus Torvalds 	struct dio *dio;
11653fb962bdSNathan Scott 	int release_i_mutex = 0;
11663fb962bdSNathan Scott 	int acquire_i_mutex = 0;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	if (rw & WRITE)
1169b31dc66aSJens Axboe 		rw = WRITE_SYNC;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	if (bdev)
11721da177e4SLinus Torvalds 		bdev_blkbits = blksize_bits(bdev_hardsect_size(bdev));
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	if (offset & blocksize_mask) {
11751da177e4SLinus Torvalds 		if (bdev)
11761da177e4SLinus Torvalds 			 blkbits = bdev_blkbits;
11771da177e4SLinus Torvalds 		blocksize_mask = (1 << blkbits) - 1;
11781da177e4SLinus Torvalds 		if (offset & blocksize_mask)
11791da177e4SLinus Torvalds 			goto out;
11801da177e4SLinus Torvalds 	}
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	/* Check the memory alignment.  Blocks cannot straddle pages */
11831da177e4SLinus Torvalds 	for (seg = 0; seg < nr_segs; seg++) {
11841da177e4SLinus Torvalds 		addr = (unsigned long)iov[seg].iov_base;
11851da177e4SLinus Torvalds 		size = iov[seg].iov_len;
11861da177e4SLinus Torvalds 		end += size;
11871da177e4SLinus Torvalds 		if ((addr & blocksize_mask) || (size & blocksize_mask))  {
11881da177e4SLinus Torvalds 			if (bdev)
11891da177e4SLinus Torvalds 				 blkbits = bdev_blkbits;
11901da177e4SLinus Torvalds 			blocksize_mask = (1 << blkbits) - 1;
11911da177e4SLinus Torvalds 			if ((addr & blocksize_mask) || (size & blocksize_mask))
11921da177e4SLinus Torvalds 				goto out;
11931da177e4SLinus Torvalds 		}
11941da177e4SLinus Torvalds 	}
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	dio = kmalloc(sizeof(*dio), GFP_KERNEL);
11971da177e4SLinus Torvalds 	retval = -ENOMEM;
11981da177e4SLinus Torvalds 	if (!dio)
11991da177e4SLinus Torvalds 		goto out;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	/*
12021da177e4SLinus Torvalds 	 * For block device access DIO_NO_LOCKING is used,
12031da177e4SLinus Torvalds 	 *	neither readers nor writers do any locking at all
12041da177e4SLinus Torvalds 	 * For regular files using DIO_LOCKING,
12051b1dcc1bSJes Sorensen 	 *	readers need to grab i_mutex and i_alloc_sem
12061b1dcc1bSJes Sorensen 	 *	writers need to grab i_alloc_sem only (i_mutex is already held)
12071da177e4SLinus Torvalds 	 * For regular files using DIO_OWN_LOCKING,
12081da177e4SLinus Torvalds 	 *	neither readers nor writers take any locks here
12091da177e4SLinus Torvalds 	 */
12101da177e4SLinus Torvalds 	dio->lock_type = dio_lock_type;
12111da177e4SLinus Torvalds 	if (dio_lock_type != DIO_NO_LOCKING) {
12121da177e4SLinus Torvalds 		/* watch out for a 0 len io from a tricksy fs */
12131da177e4SLinus Torvalds 		if (rw == READ && end > offset) {
12141da177e4SLinus Torvalds 			struct address_space *mapping;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 			mapping = iocb->ki_filp->f_mapping;
12171da177e4SLinus Torvalds 			if (dio_lock_type != DIO_OWN_LOCKING) {
12181b1dcc1bSJes Sorensen 				mutex_lock(&inode->i_mutex);
12193fb962bdSNathan Scott 				release_i_mutex = 1;
12201da177e4SLinus Torvalds 			}
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 			retval = filemap_write_and_wait_range(mapping, offset,
12231da177e4SLinus Torvalds 							      end - 1);
12241da177e4SLinus Torvalds 			if (retval) {
12251da177e4SLinus Torvalds 				kfree(dio);
12261da177e4SLinus Torvalds 				goto out;
12271da177e4SLinus Torvalds 			}
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 			if (dio_lock_type == DIO_OWN_LOCKING) {
12301b1dcc1bSJes Sorensen 				mutex_unlock(&inode->i_mutex);
12313fb962bdSNathan Scott 				acquire_i_mutex = 1;
12321da177e4SLinus Torvalds 			}
12331da177e4SLinus Torvalds 		}
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 		if (dio_lock_type == DIO_LOCKING)
1236d8aa905bSIngo Molnar 			/* lockdep: not the owner will release it */
1237d8aa905bSIngo Molnar 			down_read_non_owner(&inode->i_alloc_sem);
12381da177e4SLinus Torvalds 	}
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	/*
12411da177e4SLinus Torvalds 	 * For file extending writes updating i_size before data
12421da177e4SLinus Torvalds 	 * writeouts complete can expose uninitialized blocks. So
12431da177e4SLinus Torvalds 	 * even for AIO, we need to wait for i/o to complete before
12441da177e4SLinus Torvalds 	 * returning in this case.
12451da177e4SLinus Torvalds 	 */
1246b31dc66aSJens Axboe 	dio->is_async = !is_sync_kiocb(iocb) && !((rw & WRITE) &&
12471da177e4SLinus Torvalds 		(end > i_size_read(inode)));
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	retval = direct_io_worker(rw, iocb, inode, iov, offset,
12501d8fa7a2SBadari Pulavarty 				nr_segs, blkbits, get_block, end_io, dio);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	if (rw == READ && dio_lock_type == DIO_LOCKING)
12533fb962bdSNathan Scott 		release_i_mutex = 0;
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds out:
12563fb962bdSNathan Scott 	if (release_i_mutex)
12571b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
12583fb962bdSNathan Scott 	else if (acquire_i_mutex)
12593fb962bdSNathan Scott 		mutex_lock(&inode->i_mutex);
12601da177e4SLinus Torvalds 	return retval;
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds EXPORT_SYMBOL(__blockdev_direct_IO);
1263