direct-io.c 39 KB

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