direct-io.c 36 KB

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