direct-io.c 38 KB

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