direct-io.c 35 KB

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