direct-io.c 35 KB

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