direct-io.c 39 KB

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