direct-io.c 37 KB

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