direct-io.c 36 KB

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