direct-io.c 38 KB

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