direct-io.c 39 KB

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