direct-io.c 38 KB

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