direct-io.c 35 KB

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