iomap.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  1. /*
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. * Copyright (c) 2016-2018 Christoph Hellwig.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/compiler.h>
  16. #include <linux/fs.h>
  17. #include <linux/iomap.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/gfp.h>
  20. #include <linux/migrate.h>
  21. #include <linux/mm.h>
  22. #include <linux/mm_inline.h>
  23. #include <linux/swap.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/pagevec.h>
  26. #include <linux/file.h>
  27. #include <linux/uio.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/task_io_accounting_ops.h>
  31. #include <linux/dax.h>
  32. #include <linux/sched/signal.h>
  33. #include <linux/swap.h>
  34. #include "internal.h"
  35. /*
  36. * Execute a iomap write on a segment of the mapping that spans a
  37. * contiguous range of pages that have identical block mapping state.
  38. *
  39. * This avoids the need to map pages individually, do individual allocations
  40. * for each page and most importantly avoid the need for filesystem specific
  41. * locking per page. Instead, all the operations are amortised over the entire
  42. * range of pages. It is assumed that the filesystems will lock whatever
  43. * resources they require in the iomap_begin call, and release them in the
  44. * iomap_end call.
  45. */
  46. loff_t
  47. iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags,
  48. const struct iomap_ops *ops, void *data, iomap_actor_t actor)
  49. {
  50. struct iomap iomap = { 0 };
  51. loff_t written = 0, ret;
  52. /*
  53. * Need to map a range from start position for length bytes. This can
  54. * span multiple pages - it is only guaranteed to return a range of a
  55. * single type of pages (e.g. all into a hole, all mapped or all
  56. * unwritten). Failure at this point has nothing to undo.
  57. *
  58. * If allocation is required for this range, reserve the space now so
  59. * that the allocation is guaranteed to succeed later on. Once we copy
  60. * the data into the page cache pages, then we cannot fail otherwise we
  61. * expose transient stale data. If the reserve fails, we can safely
  62. * back out at this point as there is nothing to undo.
  63. */
  64. ret = ops->iomap_begin(inode, pos, length, flags, &iomap);
  65. if (ret)
  66. return ret;
  67. if (WARN_ON(iomap.offset > pos))
  68. return -EIO;
  69. if (WARN_ON(iomap.length == 0))
  70. return -EIO;
  71. /*
  72. * Cut down the length to the one actually provided by the filesystem,
  73. * as it might not be able to give us the whole size that we requested.
  74. */
  75. if (iomap.offset + iomap.length < pos + length)
  76. length = iomap.offset + iomap.length - pos;
  77. /*
  78. * Now that we have guaranteed that the space allocation will succeed.
  79. * we can do the copy-in page by page without having to worry about
  80. * failures exposing transient data.
  81. */
  82. written = actor(inode, pos, length, data, &iomap);
  83. /*
  84. * Now the data has been copied, commit the range we've copied. This
  85. * should not fail unless the filesystem has had a fatal error.
  86. */
  87. if (ops->iomap_end) {
  88. ret = ops->iomap_end(inode, pos, length,
  89. written > 0 ? written : 0,
  90. flags, &iomap);
  91. }
  92. return written ? written : ret;
  93. }
  94. static sector_t
  95. iomap_sector(struct iomap *iomap, loff_t pos)
  96. {
  97. return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
  98. }
  99. static struct iomap_page *
  100. iomap_page_create(struct inode *inode, struct page *page)
  101. {
  102. struct iomap_page *iop = to_iomap_page(page);
  103. if (iop || i_blocksize(inode) == PAGE_SIZE)
  104. return iop;
  105. iop = kmalloc(sizeof(*iop), GFP_NOFS | __GFP_NOFAIL);
  106. atomic_set(&iop->read_count, 0);
  107. atomic_set(&iop->write_count, 0);
  108. spin_lock_init(&iop->uptodate_lock);
  109. bitmap_zero(iop->uptodate, PAGE_SIZE / SECTOR_SIZE);
  110. /*
  111. * migrate_page_move_mapping() assumes that pages with private data have
  112. * their count elevated by 1.
  113. */
  114. get_page(page);
  115. set_page_private(page, (unsigned long)iop);
  116. SetPagePrivate(page);
  117. return iop;
  118. }
  119. static void
  120. iomap_page_release(struct page *page)
  121. {
  122. struct iomap_page *iop = to_iomap_page(page);
  123. if (!iop)
  124. return;
  125. WARN_ON_ONCE(atomic_read(&iop->read_count));
  126. WARN_ON_ONCE(atomic_read(&iop->write_count));
  127. ClearPagePrivate(page);
  128. set_page_private(page, 0);
  129. put_page(page);
  130. kfree(iop);
  131. }
  132. /*
  133. * Calculate the range inside the page that we actually need to read.
  134. */
  135. static void
  136. iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
  137. loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
  138. {
  139. loff_t orig_pos = *pos;
  140. loff_t isize = i_size_read(inode);
  141. unsigned block_bits = inode->i_blkbits;
  142. unsigned block_size = (1 << block_bits);
  143. unsigned poff = offset_in_page(*pos);
  144. unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
  145. unsigned first = poff >> block_bits;
  146. unsigned last = (poff + plen - 1) >> block_bits;
  147. /*
  148. * If the block size is smaller than the page size we need to check the
  149. * per-block uptodate status and adjust the offset and length if needed
  150. * to avoid reading in already uptodate ranges.
  151. */
  152. if (iop) {
  153. unsigned int i;
  154. /* move forward for each leading block marked uptodate */
  155. for (i = first; i <= last; i++) {
  156. if (!test_bit(i, iop->uptodate))
  157. break;
  158. *pos += block_size;
  159. poff += block_size;
  160. plen -= block_size;
  161. first++;
  162. }
  163. /* truncate len if we find any trailing uptodate block(s) */
  164. for ( ; i <= last; i++) {
  165. if (test_bit(i, iop->uptodate)) {
  166. plen -= (last - i + 1) * block_size;
  167. last = i - 1;
  168. break;
  169. }
  170. }
  171. }
  172. /*
  173. * If the extent spans the block that contains the i_size we need to
  174. * handle both halves separately so that we properly zero data in the
  175. * page cache for blocks that are entirely outside of i_size.
  176. */
  177. if (orig_pos <= isize && orig_pos + length > isize) {
  178. unsigned end = offset_in_page(isize - 1) >> block_bits;
  179. if (first <= end && last > end)
  180. plen -= (last - end) * block_size;
  181. }
  182. *offp = poff;
  183. *lenp = plen;
  184. }
  185. static void
  186. iomap_iop_set_range_uptodate(struct page *page, unsigned off, unsigned len)
  187. {
  188. struct iomap_page *iop = to_iomap_page(page);
  189. struct inode *inode = page->mapping->host;
  190. unsigned first = off >> inode->i_blkbits;
  191. unsigned last = (off + len - 1) >> inode->i_blkbits;
  192. bool uptodate = true;
  193. unsigned long flags;
  194. unsigned int i;
  195. spin_lock_irqsave(&iop->uptodate_lock, flags);
  196. for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
  197. if (i >= first && i <= last)
  198. set_bit(i, iop->uptodate);
  199. else if (!test_bit(i, iop->uptodate))
  200. uptodate = false;
  201. }
  202. if (uptodate)
  203. SetPageUptodate(page);
  204. spin_unlock_irqrestore(&iop->uptodate_lock, flags);
  205. }
  206. static void
  207. iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
  208. {
  209. if (PageError(page))
  210. return;
  211. if (page_has_private(page))
  212. iomap_iop_set_range_uptodate(page, off, len);
  213. else
  214. SetPageUptodate(page);
  215. }
  216. static void
  217. iomap_read_finish(struct iomap_page *iop, struct page *page)
  218. {
  219. if (!iop || atomic_dec_and_test(&iop->read_count))
  220. unlock_page(page);
  221. }
  222. static void
  223. iomap_read_page_end_io(struct bio_vec *bvec, int error)
  224. {
  225. struct page *page = bvec->bv_page;
  226. struct iomap_page *iop = to_iomap_page(page);
  227. if (unlikely(error)) {
  228. ClearPageUptodate(page);
  229. SetPageError(page);
  230. } else {
  231. iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
  232. }
  233. iomap_read_finish(iop, page);
  234. }
  235. static void
  236. iomap_read_inline_data(struct inode *inode, struct page *page,
  237. struct iomap *iomap)
  238. {
  239. size_t size = i_size_read(inode);
  240. void *addr;
  241. if (PageUptodate(page))
  242. return;
  243. BUG_ON(page->index);
  244. BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data));
  245. addr = kmap_atomic(page);
  246. memcpy(addr, iomap->inline_data, size);
  247. memset(addr + size, 0, PAGE_SIZE - size);
  248. kunmap_atomic(addr);
  249. SetPageUptodate(page);
  250. }
  251. static void
  252. iomap_read_end_io(struct bio *bio)
  253. {
  254. int error = blk_status_to_errno(bio->bi_status);
  255. struct bio_vec *bvec;
  256. int i;
  257. bio_for_each_segment_all(bvec, bio, i)
  258. iomap_read_page_end_io(bvec, error);
  259. bio_put(bio);
  260. }
  261. struct iomap_readpage_ctx {
  262. struct page *cur_page;
  263. bool cur_page_in_bio;
  264. bool is_readahead;
  265. struct bio *bio;
  266. struct list_head *pages;
  267. };
  268. static loff_t
  269. iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  270. struct iomap *iomap)
  271. {
  272. struct iomap_readpage_ctx *ctx = data;
  273. struct page *page = ctx->cur_page;
  274. struct iomap_page *iop = iomap_page_create(inode, page);
  275. bool is_contig = false;
  276. loff_t orig_pos = pos;
  277. unsigned poff, plen;
  278. sector_t sector;
  279. if (iomap->type == IOMAP_INLINE) {
  280. WARN_ON_ONCE(pos);
  281. iomap_read_inline_data(inode, page, iomap);
  282. return PAGE_SIZE;
  283. }
  284. /* zero post-eof blocks as the page may be mapped */
  285. iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
  286. if (plen == 0)
  287. goto done;
  288. if (iomap->type != IOMAP_MAPPED || pos >= i_size_read(inode)) {
  289. zero_user(page, poff, plen);
  290. iomap_set_range_uptodate(page, poff, plen);
  291. goto done;
  292. }
  293. ctx->cur_page_in_bio = true;
  294. /*
  295. * Try to merge into a previous segment if we can.
  296. */
  297. sector = iomap_sector(iomap, pos);
  298. if (ctx->bio && bio_end_sector(ctx->bio) == sector) {
  299. if (__bio_try_merge_page(ctx->bio, page, plen, poff))
  300. goto done;
  301. is_contig = true;
  302. }
  303. /*
  304. * If we start a new segment we need to increase the read count, and we
  305. * need to do so before submitting any previous full bio to make sure
  306. * that we don't prematurely unlock the page.
  307. */
  308. if (iop)
  309. atomic_inc(&iop->read_count);
  310. if (!ctx->bio || !is_contig || bio_full(ctx->bio)) {
  311. gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
  312. int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
  313. if (ctx->bio)
  314. submit_bio(ctx->bio);
  315. if (ctx->is_readahead) /* same as readahead_gfp_mask */
  316. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  317. ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
  318. ctx->bio->bi_opf = REQ_OP_READ;
  319. if (ctx->is_readahead)
  320. ctx->bio->bi_opf |= REQ_RAHEAD;
  321. ctx->bio->bi_iter.bi_sector = sector;
  322. bio_set_dev(ctx->bio, iomap->bdev);
  323. ctx->bio->bi_end_io = iomap_read_end_io;
  324. }
  325. __bio_add_page(ctx->bio, page, plen, poff);
  326. done:
  327. /*
  328. * Move the caller beyond our range so that it keeps making progress.
  329. * For that we have to include any leading non-uptodate ranges, but
  330. * we can skip trailing ones as they will be handled in the next
  331. * iteration.
  332. */
  333. return pos - orig_pos + plen;
  334. }
  335. int
  336. iomap_readpage(struct page *page, const struct iomap_ops *ops)
  337. {
  338. struct iomap_readpage_ctx ctx = { .cur_page = page };
  339. struct inode *inode = page->mapping->host;
  340. unsigned poff;
  341. loff_t ret;
  342. for (poff = 0; poff < PAGE_SIZE; poff += ret) {
  343. ret = iomap_apply(inode, page_offset(page) + poff,
  344. PAGE_SIZE - poff, 0, ops, &ctx,
  345. iomap_readpage_actor);
  346. if (ret <= 0) {
  347. WARN_ON_ONCE(ret == 0);
  348. SetPageError(page);
  349. break;
  350. }
  351. }
  352. if (ctx.bio) {
  353. submit_bio(ctx.bio);
  354. WARN_ON_ONCE(!ctx.cur_page_in_bio);
  355. } else {
  356. WARN_ON_ONCE(ctx.cur_page_in_bio);
  357. unlock_page(page);
  358. }
  359. /*
  360. * Just like mpage_readpages and block_read_full_page we always
  361. * return 0 and just mark the page as PageError on errors. This
  362. * should be cleaned up all through the stack eventually.
  363. */
  364. return 0;
  365. }
  366. EXPORT_SYMBOL_GPL(iomap_readpage);
  367. static struct page *
  368. iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
  369. loff_t length, loff_t *done)
  370. {
  371. while (!list_empty(pages)) {
  372. struct page *page = lru_to_page(pages);
  373. if (page_offset(page) >= (u64)pos + length)
  374. break;
  375. list_del(&page->lru);
  376. if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
  377. GFP_NOFS))
  378. return page;
  379. /*
  380. * If we already have a page in the page cache at index we are
  381. * done. Upper layers don't care if it is uptodate after the
  382. * readpages call itself as every page gets checked again once
  383. * actually needed.
  384. */
  385. *done += PAGE_SIZE;
  386. put_page(page);
  387. }
  388. return NULL;
  389. }
  390. static loff_t
  391. iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
  392. void *data, struct iomap *iomap)
  393. {
  394. struct iomap_readpage_ctx *ctx = data;
  395. loff_t done, ret;
  396. for (done = 0; done < length; done += ret) {
  397. if (ctx->cur_page && offset_in_page(pos + done) == 0) {
  398. if (!ctx->cur_page_in_bio)
  399. unlock_page(ctx->cur_page);
  400. put_page(ctx->cur_page);
  401. ctx->cur_page = NULL;
  402. }
  403. if (!ctx->cur_page) {
  404. ctx->cur_page = iomap_next_page(inode, ctx->pages,
  405. pos, length, &done);
  406. if (!ctx->cur_page)
  407. break;
  408. ctx->cur_page_in_bio = false;
  409. }
  410. ret = iomap_readpage_actor(inode, pos + done, length - done,
  411. ctx, iomap);
  412. }
  413. return done;
  414. }
  415. int
  416. iomap_readpages(struct address_space *mapping, struct list_head *pages,
  417. unsigned nr_pages, const struct iomap_ops *ops)
  418. {
  419. struct iomap_readpage_ctx ctx = {
  420. .pages = pages,
  421. .is_readahead = true,
  422. };
  423. loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
  424. loff_t last = page_offset(list_entry(pages->next, struct page, lru));
  425. loff_t length = last - pos + PAGE_SIZE, ret = 0;
  426. while (length > 0) {
  427. ret = iomap_apply(mapping->host, pos, length, 0, ops,
  428. &ctx, iomap_readpages_actor);
  429. if (ret <= 0) {
  430. WARN_ON_ONCE(ret == 0);
  431. goto done;
  432. }
  433. pos += ret;
  434. length -= ret;
  435. }
  436. ret = 0;
  437. done:
  438. if (ctx.bio)
  439. submit_bio(ctx.bio);
  440. if (ctx.cur_page) {
  441. if (!ctx.cur_page_in_bio)
  442. unlock_page(ctx.cur_page);
  443. put_page(ctx.cur_page);
  444. }
  445. /*
  446. * Check that we didn't lose a page due to the arcance calling
  447. * conventions..
  448. */
  449. WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
  450. return ret;
  451. }
  452. EXPORT_SYMBOL_GPL(iomap_readpages);
  453. /*
  454. * iomap_is_partially_uptodate checks whether blocks within a page are
  455. * uptodate or not.
  456. *
  457. * Returns true if all blocks which correspond to a file portion
  458. * we want to read within the page are uptodate.
  459. */
  460. int
  461. iomap_is_partially_uptodate(struct page *page, unsigned long from,
  462. unsigned long count)
  463. {
  464. struct iomap_page *iop = to_iomap_page(page);
  465. struct inode *inode = page->mapping->host;
  466. unsigned len, first, last;
  467. unsigned i;
  468. /* Limit range to one page */
  469. len = min_t(unsigned, PAGE_SIZE - from, count);
  470. /* First and last blocks in range within page */
  471. first = from >> inode->i_blkbits;
  472. last = (from + len - 1) >> inode->i_blkbits;
  473. if (iop) {
  474. for (i = first; i <= last; i++)
  475. if (!test_bit(i, iop->uptodate))
  476. return 0;
  477. return 1;
  478. }
  479. return 0;
  480. }
  481. EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
  482. int
  483. iomap_releasepage(struct page *page, gfp_t gfp_mask)
  484. {
  485. /*
  486. * mm accommodates an old ext3 case where clean pages might not have had
  487. * the dirty bit cleared. Thus, it can send actual dirty pages to
  488. * ->releasepage() via shrink_active_list(), skip those here.
  489. */
  490. if (PageDirty(page) || PageWriteback(page))
  491. return 0;
  492. iomap_page_release(page);
  493. return 1;
  494. }
  495. EXPORT_SYMBOL_GPL(iomap_releasepage);
  496. void
  497. iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
  498. {
  499. /*
  500. * If we are invalidating the entire page, clear the dirty state from it
  501. * and release it to avoid unnecessary buildup of the LRU.
  502. */
  503. if (offset == 0 && len == PAGE_SIZE) {
  504. WARN_ON_ONCE(PageWriteback(page));
  505. cancel_dirty_page(page);
  506. iomap_page_release(page);
  507. }
  508. }
  509. EXPORT_SYMBOL_GPL(iomap_invalidatepage);
  510. #ifdef CONFIG_MIGRATION
  511. int
  512. iomap_migrate_page(struct address_space *mapping, struct page *newpage,
  513. struct page *page, enum migrate_mode mode)
  514. {
  515. int ret;
  516. ret = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
  517. if (ret != MIGRATEPAGE_SUCCESS)
  518. return ret;
  519. if (page_has_private(page)) {
  520. ClearPagePrivate(page);
  521. get_page(newpage);
  522. set_page_private(newpage, page_private(page));
  523. set_page_private(page, 0);
  524. put_page(page);
  525. SetPagePrivate(newpage);
  526. }
  527. if (mode != MIGRATE_SYNC_NO_COPY)
  528. migrate_page_copy(newpage, page);
  529. else
  530. migrate_page_states(newpage, page);
  531. return MIGRATEPAGE_SUCCESS;
  532. }
  533. EXPORT_SYMBOL_GPL(iomap_migrate_page);
  534. #endif /* CONFIG_MIGRATION */
  535. static void
  536. iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
  537. {
  538. loff_t i_size = i_size_read(inode);
  539. /*
  540. * Only truncate newly allocated pages beyoned EOF, even if the
  541. * write started inside the existing inode size.
  542. */
  543. if (pos + len > i_size)
  544. truncate_pagecache_range(inode, max(pos, i_size), pos + len);
  545. }
  546. static int
  547. iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page,
  548. unsigned poff, unsigned plen, unsigned from, unsigned to,
  549. struct iomap *iomap)
  550. {
  551. struct bio_vec bvec;
  552. struct bio bio;
  553. if (iomap->type != IOMAP_MAPPED || block_start >= i_size_read(inode)) {
  554. zero_user_segments(page, poff, from, to, poff + plen);
  555. iomap_set_range_uptodate(page, poff, plen);
  556. return 0;
  557. }
  558. bio_init(&bio, &bvec, 1);
  559. bio.bi_opf = REQ_OP_READ;
  560. bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
  561. bio_set_dev(&bio, iomap->bdev);
  562. __bio_add_page(&bio, page, plen, poff);
  563. return submit_bio_wait(&bio);
  564. }
  565. static int
  566. __iomap_write_begin(struct inode *inode, loff_t pos, unsigned len,
  567. struct page *page, struct iomap *iomap)
  568. {
  569. struct iomap_page *iop = iomap_page_create(inode, page);
  570. loff_t block_size = i_blocksize(inode);
  571. loff_t block_start = pos & ~(block_size - 1);
  572. loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1);
  573. unsigned from = offset_in_page(pos), to = from + len, poff, plen;
  574. int status = 0;
  575. if (PageUptodate(page))
  576. return 0;
  577. do {
  578. iomap_adjust_read_range(inode, iop, &block_start,
  579. block_end - block_start, &poff, &plen);
  580. if (plen == 0)
  581. break;
  582. if ((from > poff && from < poff + plen) ||
  583. (to > poff && to < poff + plen)) {
  584. status = iomap_read_page_sync(inode, block_start, page,
  585. poff, plen, from, to, iomap);
  586. if (status)
  587. break;
  588. }
  589. } while ((block_start += plen) < block_end);
  590. return status;
  591. }
  592. static int
  593. iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
  594. struct page **pagep, struct iomap *iomap)
  595. {
  596. pgoff_t index = pos >> PAGE_SHIFT;
  597. struct page *page;
  598. int status = 0;
  599. BUG_ON(pos + len > iomap->offset + iomap->length);
  600. if (fatal_signal_pending(current))
  601. return -EINTR;
  602. page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
  603. if (!page)
  604. return -ENOMEM;
  605. if (iomap->type == IOMAP_INLINE)
  606. iomap_read_inline_data(inode, page, iomap);
  607. else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
  608. status = __block_write_begin_int(page, pos, len, NULL, iomap);
  609. else
  610. status = __iomap_write_begin(inode, pos, len, page, iomap);
  611. if (unlikely(status)) {
  612. unlock_page(page);
  613. put_page(page);
  614. page = NULL;
  615. iomap_write_failed(inode, pos, len);
  616. }
  617. *pagep = page;
  618. return status;
  619. }
  620. int
  621. iomap_set_page_dirty(struct page *page)
  622. {
  623. struct address_space *mapping = page_mapping(page);
  624. int newly_dirty;
  625. if (unlikely(!mapping))
  626. return !TestSetPageDirty(page);
  627. /*
  628. * Lock out page->mem_cgroup migration to keep PageDirty
  629. * synchronized with per-memcg dirty page counters.
  630. */
  631. lock_page_memcg(page);
  632. newly_dirty = !TestSetPageDirty(page);
  633. if (newly_dirty)
  634. __set_page_dirty(page, mapping, 0);
  635. unlock_page_memcg(page);
  636. if (newly_dirty)
  637. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  638. return newly_dirty;
  639. }
  640. EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
  641. static int
  642. __iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
  643. unsigned copied, struct page *page, struct iomap *iomap)
  644. {
  645. flush_dcache_page(page);
  646. /*
  647. * The blocks that were entirely written will now be uptodate, so we
  648. * don't have to worry about a readpage reading them and overwriting a
  649. * partial write. However if we have encountered a short write and only
  650. * partially written into a block, it will not be marked uptodate, so a
  651. * readpage might come in and destroy our partial write.
  652. *
  653. * Do the simplest thing, and just treat any short write to a non
  654. * uptodate page as a zero-length write, and force the caller to redo
  655. * the whole thing.
  656. */
  657. if (unlikely(copied < len && !PageUptodate(page))) {
  658. copied = 0;
  659. } else {
  660. iomap_set_range_uptodate(page, offset_in_page(pos), len);
  661. iomap_set_page_dirty(page);
  662. }
  663. return __generic_write_end(inode, pos, copied, page);
  664. }
  665. static int
  666. iomap_write_end_inline(struct inode *inode, struct page *page,
  667. struct iomap *iomap, loff_t pos, unsigned copied)
  668. {
  669. void *addr;
  670. WARN_ON_ONCE(!PageUptodate(page));
  671. BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data));
  672. addr = kmap_atomic(page);
  673. memcpy(iomap->inline_data + pos, addr + pos, copied);
  674. kunmap_atomic(addr);
  675. mark_inode_dirty(inode);
  676. __generic_write_end(inode, pos, copied, page);
  677. return copied;
  678. }
  679. static int
  680. iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
  681. unsigned copied, struct page *page, struct iomap *iomap)
  682. {
  683. int ret;
  684. if (iomap->type == IOMAP_INLINE) {
  685. ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
  686. } else if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
  687. ret = generic_write_end(NULL, inode->i_mapping, pos, len,
  688. copied, page, NULL);
  689. } else {
  690. ret = __iomap_write_end(inode, pos, len, copied, page, iomap);
  691. }
  692. if (iomap->page_done)
  693. iomap->page_done(inode, pos, copied, page, iomap);
  694. if (ret < len)
  695. iomap_write_failed(inode, pos, len);
  696. return ret;
  697. }
  698. static loff_t
  699. iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  700. struct iomap *iomap)
  701. {
  702. struct iov_iter *i = data;
  703. long status = 0;
  704. ssize_t written = 0;
  705. unsigned int flags = AOP_FLAG_NOFS;
  706. do {
  707. struct page *page;
  708. unsigned long offset; /* Offset into pagecache page */
  709. unsigned long bytes; /* Bytes to write to page */
  710. size_t copied; /* Bytes copied from user */
  711. offset = offset_in_page(pos);
  712. bytes = min_t(unsigned long, PAGE_SIZE - offset,
  713. iov_iter_count(i));
  714. again:
  715. if (bytes > length)
  716. bytes = length;
  717. /*
  718. * Bring in the user page that we will copy from _first_.
  719. * Otherwise there's a nasty deadlock on copying from the
  720. * same page as we're writing to, without it being marked
  721. * up-to-date.
  722. *
  723. * Not only is this an optimisation, but it is also required
  724. * to check that the address is actually valid, when atomic
  725. * usercopies are used, below.
  726. */
  727. if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
  728. status = -EFAULT;
  729. break;
  730. }
  731. status = iomap_write_begin(inode, pos, bytes, flags, &page,
  732. iomap);
  733. if (unlikely(status))
  734. break;
  735. if (mapping_writably_mapped(inode->i_mapping))
  736. flush_dcache_page(page);
  737. copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
  738. flush_dcache_page(page);
  739. status = iomap_write_end(inode, pos, bytes, copied, page,
  740. iomap);
  741. if (unlikely(status < 0))
  742. break;
  743. copied = status;
  744. cond_resched();
  745. iov_iter_advance(i, copied);
  746. if (unlikely(copied == 0)) {
  747. /*
  748. * If we were unable to copy any data at all, we must
  749. * fall back to a single segment length write.
  750. *
  751. * If we didn't fallback here, we could livelock
  752. * because not all segments in the iov can be copied at
  753. * once without a pagefault.
  754. */
  755. bytes = min_t(unsigned long, PAGE_SIZE - offset,
  756. iov_iter_single_seg_count(i));
  757. goto again;
  758. }
  759. pos += copied;
  760. written += copied;
  761. length -= copied;
  762. balance_dirty_pages_ratelimited(inode->i_mapping);
  763. } while (iov_iter_count(i) && length);
  764. return written ? written : status;
  765. }
  766. ssize_t
  767. iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
  768. const struct iomap_ops *ops)
  769. {
  770. struct inode *inode = iocb->ki_filp->f_mapping->host;
  771. loff_t pos = iocb->ki_pos, ret = 0, written = 0;
  772. while (iov_iter_count(iter)) {
  773. ret = iomap_apply(inode, pos, iov_iter_count(iter),
  774. IOMAP_WRITE, ops, iter, iomap_write_actor);
  775. if (ret <= 0)
  776. break;
  777. pos += ret;
  778. written += ret;
  779. }
  780. return written ? written : ret;
  781. }
  782. EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
  783. static struct page *
  784. __iomap_read_page(struct inode *inode, loff_t offset)
  785. {
  786. struct address_space *mapping = inode->i_mapping;
  787. struct page *page;
  788. page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
  789. if (IS_ERR(page))
  790. return page;
  791. if (!PageUptodate(page)) {
  792. put_page(page);
  793. return ERR_PTR(-EIO);
  794. }
  795. return page;
  796. }
  797. static loff_t
  798. iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  799. struct iomap *iomap)
  800. {
  801. long status = 0;
  802. ssize_t written = 0;
  803. do {
  804. struct page *page, *rpage;
  805. unsigned long offset; /* Offset into pagecache page */
  806. unsigned long bytes; /* Bytes to write to page */
  807. offset = offset_in_page(pos);
  808. bytes = min_t(loff_t, PAGE_SIZE - offset, length);
  809. rpage = __iomap_read_page(inode, pos);
  810. if (IS_ERR(rpage))
  811. return PTR_ERR(rpage);
  812. status = iomap_write_begin(inode, pos, bytes,
  813. AOP_FLAG_NOFS, &page, iomap);
  814. put_page(rpage);
  815. if (unlikely(status))
  816. return status;
  817. WARN_ON_ONCE(!PageUptodate(page));
  818. status = iomap_write_end(inode, pos, bytes, bytes, page, iomap);
  819. if (unlikely(status <= 0)) {
  820. if (WARN_ON_ONCE(status == 0))
  821. return -EIO;
  822. return status;
  823. }
  824. cond_resched();
  825. pos += status;
  826. written += status;
  827. length -= status;
  828. balance_dirty_pages_ratelimited(inode->i_mapping);
  829. } while (length);
  830. return written;
  831. }
  832. int
  833. iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
  834. const struct iomap_ops *ops)
  835. {
  836. loff_t ret;
  837. while (len) {
  838. ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
  839. iomap_dirty_actor);
  840. if (ret <= 0)
  841. return ret;
  842. pos += ret;
  843. len -= ret;
  844. }
  845. return 0;
  846. }
  847. EXPORT_SYMBOL_GPL(iomap_file_dirty);
  848. static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
  849. unsigned bytes, struct iomap *iomap)
  850. {
  851. struct page *page;
  852. int status;
  853. status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
  854. iomap);
  855. if (status)
  856. return status;
  857. zero_user(page, offset, bytes);
  858. mark_page_accessed(page);
  859. return iomap_write_end(inode, pos, bytes, bytes, page, iomap);
  860. }
  861. static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
  862. struct iomap *iomap)
  863. {
  864. return __dax_zero_page_range(iomap->bdev, iomap->dax_dev,
  865. iomap_sector(iomap, pos & PAGE_MASK), offset, bytes);
  866. }
  867. static loff_t
  868. iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
  869. void *data, struct iomap *iomap)
  870. {
  871. bool *did_zero = data;
  872. loff_t written = 0;
  873. int status;
  874. /* already zeroed? we're done. */
  875. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  876. return count;
  877. do {
  878. unsigned offset, bytes;
  879. offset = offset_in_page(pos);
  880. bytes = min_t(loff_t, PAGE_SIZE - offset, count);
  881. if (IS_DAX(inode))
  882. status = iomap_dax_zero(pos, offset, bytes, iomap);
  883. else
  884. status = iomap_zero(inode, pos, offset, bytes, iomap);
  885. if (status < 0)
  886. return status;
  887. pos += bytes;
  888. count -= bytes;
  889. written += bytes;
  890. if (did_zero)
  891. *did_zero = true;
  892. } while (count > 0);
  893. return written;
  894. }
  895. int
  896. iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
  897. const struct iomap_ops *ops)
  898. {
  899. loff_t ret;
  900. while (len > 0) {
  901. ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
  902. ops, did_zero, iomap_zero_range_actor);
  903. if (ret <= 0)
  904. return ret;
  905. pos += ret;
  906. len -= ret;
  907. }
  908. return 0;
  909. }
  910. EXPORT_SYMBOL_GPL(iomap_zero_range);
  911. int
  912. iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
  913. const struct iomap_ops *ops)
  914. {
  915. unsigned int blocksize = i_blocksize(inode);
  916. unsigned int off = pos & (blocksize - 1);
  917. /* Block boundary? Nothing to do */
  918. if (!off)
  919. return 0;
  920. return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
  921. }
  922. EXPORT_SYMBOL_GPL(iomap_truncate_page);
  923. static loff_t
  924. iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
  925. void *data, struct iomap *iomap)
  926. {
  927. struct page *page = data;
  928. int ret;
  929. if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
  930. ret = __block_write_begin_int(page, pos, length, NULL, iomap);
  931. if (ret)
  932. return ret;
  933. block_commit_write(page, 0, length);
  934. } else {
  935. WARN_ON_ONCE(!PageUptodate(page));
  936. iomap_page_create(inode, page);
  937. set_page_dirty(page);
  938. }
  939. return length;
  940. }
  941. int iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
  942. {
  943. struct page *page = vmf->page;
  944. struct inode *inode = file_inode(vmf->vma->vm_file);
  945. unsigned long length;
  946. loff_t offset, size;
  947. ssize_t ret;
  948. lock_page(page);
  949. size = i_size_read(inode);
  950. if ((page->mapping != inode->i_mapping) ||
  951. (page_offset(page) > size)) {
  952. /* We overload EFAULT to mean page got truncated */
  953. ret = -EFAULT;
  954. goto out_unlock;
  955. }
  956. /* page is wholly or partially inside EOF */
  957. if (((page->index + 1) << PAGE_SHIFT) > size)
  958. length = offset_in_page(size);
  959. else
  960. length = PAGE_SIZE;
  961. offset = page_offset(page);
  962. while (length > 0) {
  963. ret = iomap_apply(inode, offset, length,
  964. IOMAP_WRITE | IOMAP_FAULT, ops, page,
  965. iomap_page_mkwrite_actor);
  966. if (unlikely(ret <= 0))
  967. goto out_unlock;
  968. offset += ret;
  969. length -= ret;
  970. }
  971. wait_for_stable_page(page);
  972. return VM_FAULT_LOCKED;
  973. out_unlock:
  974. unlock_page(page);
  975. return block_page_mkwrite_return(ret);
  976. }
  977. EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
  978. struct fiemap_ctx {
  979. struct fiemap_extent_info *fi;
  980. struct iomap prev;
  981. };
  982. static int iomap_to_fiemap(struct fiemap_extent_info *fi,
  983. struct iomap *iomap, u32 flags)
  984. {
  985. switch (iomap->type) {
  986. case IOMAP_HOLE:
  987. /* skip holes */
  988. return 0;
  989. case IOMAP_DELALLOC:
  990. flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
  991. break;
  992. case IOMAP_MAPPED:
  993. break;
  994. case IOMAP_UNWRITTEN:
  995. flags |= FIEMAP_EXTENT_UNWRITTEN;
  996. break;
  997. case IOMAP_INLINE:
  998. flags |= FIEMAP_EXTENT_DATA_INLINE;
  999. break;
  1000. }
  1001. if (iomap->flags & IOMAP_F_MERGED)
  1002. flags |= FIEMAP_EXTENT_MERGED;
  1003. if (iomap->flags & IOMAP_F_SHARED)
  1004. flags |= FIEMAP_EXTENT_SHARED;
  1005. return fiemap_fill_next_extent(fi, iomap->offset,
  1006. iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
  1007. iomap->length, flags);
  1008. }
  1009. static loff_t
  1010. iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  1011. struct iomap *iomap)
  1012. {
  1013. struct fiemap_ctx *ctx = data;
  1014. loff_t ret = length;
  1015. if (iomap->type == IOMAP_HOLE)
  1016. return length;
  1017. ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
  1018. ctx->prev = *iomap;
  1019. switch (ret) {
  1020. case 0: /* success */
  1021. return length;
  1022. case 1: /* extent array full */
  1023. return 0;
  1024. default:
  1025. return ret;
  1026. }
  1027. }
  1028. int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
  1029. loff_t start, loff_t len, const struct iomap_ops *ops)
  1030. {
  1031. struct fiemap_ctx ctx;
  1032. loff_t ret;
  1033. memset(&ctx, 0, sizeof(ctx));
  1034. ctx.fi = fi;
  1035. ctx.prev.type = IOMAP_HOLE;
  1036. ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
  1037. if (ret)
  1038. return ret;
  1039. if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
  1040. ret = filemap_write_and_wait(inode->i_mapping);
  1041. if (ret)
  1042. return ret;
  1043. }
  1044. while (len > 0) {
  1045. ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
  1046. iomap_fiemap_actor);
  1047. /* inode with no (attribute) mapping will give ENOENT */
  1048. if (ret == -ENOENT)
  1049. break;
  1050. if (ret < 0)
  1051. return ret;
  1052. if (ret == 0)
  1053. break;
  1054. start += ret;
  1055. len -= ret;
  1056. }
  1057. if (ctx.prev.type != IOMAP_HOLE) {
  1058. ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
  1059. if (ret < 0)
  1060. return ret;
  1061. }
  1062. return 0;
  1063. }
  1064. EXPORT_SYMBOL_GPL(iomap_fiemap);
  1065. /*
  1066. * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
  1067. * Returns true if found and updates @lastoff to the offset in file.
  1068. */
  1069. static bool
  1070. page_seek_hole_data(struct inode *inode, struct page *page, loff_t *lastoff,
  1071. int whence)
  1072. {
  1073. const struct address_space_operations *ops = inode->i_mapping->a_ops;
  1074. unsigned int bsize = i_blocksize(inode), off;
  1075. bool seek_data = whence == SEEK_DATA;
  1076. loff_t poff = page_offset(page);
  1077. if (WARN_ON_ONCE(*lastoff >= poff + PAGE_SIZE))
  1078. return false;
  1079. if (*lastoff < poff) {
  1080. /*
  1081. * Last offset smaller than the start of the page means we found
  1082. * a hole:
  1083. */
  1084. if (whence == SEEK_HOLE)
  1085. return true;
  1086. *lastoff = poff;
  1087. }
  1088. /*
  1089. * Just check the page unless we can and should check block ranges:
  1090. */
  1091. if (bsize == PAGE_SIZE || !ops->is_partially_uptodate)
  1092. return PageUptodate(page) == seek_data;
  1093. lock_page(page);
  1094. if (unlikely(page->mapping != inode->i_mapping))
  1095. goto out_unlock_not_found;
  1096. for (off = 0; off < PAGE_SIZE; off += bsize) {
  1097. if (offset_in_page(*lastoff) >= off + bsize)
  1098. continue;
  1099. if (ops->is_partially_uptodate(page, off, bsize) == seek_data) {
  1100. unlock_page(page);
  1101. return true;
  1102. }
  1103. *lastoff = poff + off + bsize;
  1104. }
  1105. out_unlock_not_found:
  1106. unlock_page(page);
  1107. return false;
  1108. }
  1109. /*
  1110. * Seek for SEEK_DATA / SEEK_HOLE in the page cache.
  1111. *
  1112. * Within unwritten extents, the page cache determines which parts are holes
  1113. * and which are data: uptodate buffer heads count as data; everything else
  1114. * counts as a hole.
  1115. *
  1116. * Returns the resulting offset on successs, and -ENOENT otherwise.
  1117. */
  1118. static loff_t
  1119. page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length,
  1120. int whence)
  1121. {
  1122. pgoff_t index = offset >> PAGE_SHIFT;
  1123. pgoff_t end = DIV_ROUND_UP(offset + length, PAGE_SIZE);
  1124. loff_t lastoff = offset;
  1125. struct pagevec pvec;
  1126. if (length <= 0)
  1127. return -ENOENT;
  1128. pagevec_init(&pvec);
  1129. do {
  1130. unsigned nr_pages, i;
  1131. nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, &index,
  1132. end - 1);
  1133. if (nr_pages == 0)
  1134. break;
  1135. for (i = 0; i < nr_pages; i++) {
  1136. struct page *page = pvec.pages[i];
  1137. if (page_seek_hole_data(inode, page, &lastoff, whence))
  1138. goto check_range;
  1139. lastoff = page_offset(page) + PAGE_SIZE;
  1140. }
  1141. pagevec_release(&pvec);
  1142. } while (index < end);
  1143. /* When no page at lastoff and we are not done, we found a hole. */
  1144. if (whence != SEEK_HOLE)
  1145. goto not_found;
  1146. check_range:
  1147. if (lastoff < offset + length)
  1148. goto out;
  1149. not_found:
  1150. lastoff = -ENOENT;
  1151. out:
  1152. pagevec_release(&pvec);
  1153. return lastoff;
  1154. }
  1155. static loff_t
  1156. iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length,
  1157. void *data, struct iomap *iomap)
  1158. {
  1159. switch (iomap->type) {
  1160. case IOMAP_UNWRITTEN:
  1161. offset = page_cache_seek_hole_data(inode, offset, length,
  1162. SEEK_HOLE);
  1163. if (offset < 0)
  1164. return length;
  1165. /* fall through */
  1166. case IOMAP_HOLE:
  1167. *(loff_t *)data = offset;
  1168. return 0;
  1169. default:
  1170. return length;
  1171. }
  1172. }
  1173. loff_t
  1174. iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
  1175. {
  1176. loff_t size = i_size_read(inode);
  1177. loff_t length = size - offset;
  1178. loff_t ret;
  1179. /* Nothing to be found before or beyond the end of the file. */
  1180. if (offset < 0 || offset >= size)
  1181. return -ENXIO;
  1182. while (length > 0) {
  1183. ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
  1184. &offset, iomap_seek_hole_actor);
  1185. if (ret < 0)
  1186. return ret;
  1187. if (ret == 0)
  1188. break;
  1189. offset += ret;
  1190. length -= ret;
  1191. }
  1192. return offset;
  1193. }
  1194. EXPORT_SYMBOL_GPL(iomap_seek_hole);
  1195. static loff_t
  1196. iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length,
  1197. void *data, struct iomap *iomap)
  1198. {
  1199. switch (iomap->type) {
  1200. case IOMAP_HOLE:
  1201. return length;
  1202. case IOMAP_UNWRITTEN:
  1203. offset = page_cache_seek_hole_data(inode, offset, length,
  1204. SEEK_DATA);
  1205. if (offset < 0)
  1206. return length;
  1207. /*FALLTHRU*/
  1208. default:
  1209. *(loff_t *)data = offset;
  1210. return 0;
  1211. }
  1212. }
  1213. loff_t
  1214. iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
  1215. {
  1216. loff_t size = i_size_read(inode);
  1217. loff_t length = size - offset;
  1218. loff_t ret;
  1219. /* Nothing to be found before or beyond the end of the file. */
  1220. if (offset < 0 || offset >= size)
  1221. return -ENXIO;
  1222. while (length > 0) {
  1223. ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
  1224. &offset, iomap_seek_data_actor);
  1225. if (ret < 0)
  1226. return ret;
  1227. if (ret == 0)
  1228. break;
  1229. offset += ret;
  1230. length -= ret;
  1231. }
  1232. if (length <= 0)
  1233. return -ENXIO;
  1234. return offset;
  1235. }
  1236. EXPORT_SYMBOL_GPL(iomap_seek_data);
  1237. /*
  1238. * Private flags for iomap_dio, must not overlap with the public ones in
  1239. * iomap.h:
  1240. */
  1241. #define IOMAP_DIO_WRITE_FUA (1 << 28)
  1242. #define IOMAP_DIO_NEED_SYNC (1 << 29)
  1243. #define IOMAP_DIO_WRITE (1 << 30)
  1244. #define IOMAP_DIO_DIRTY (1 << 31)
  1245. struct iomap_dio {
  1246. struct kiocb *iocb;
  1247. iomap_dio_end_io_t *end_io;
  1248. loff_t i_size;
  1249. loff_t size;
  1250. atomic_t ref;
  1251. unsigned flags;
  1252. int error;
  1253. bool wait_for_completion;
  1254. union {
  1255. /* used during submission and for synchronous completion: */
  1256. struct {
  1257. struct iov_iter *iter;
  1258. struct task_struct *waiter;
  1259. struct request_queue *last_queue;
  1260. blk_qc_t cookie;
  1261. } submit;
  1262. /* used for aio completion: */
  1263. struct {
  1264. struct work_struct work;
  1265. } aio;
  1266. };
  1267. };
  1268. static ssize_t iomap_dio_complete(struct iomap_dio *dio)
  1269. {
  1270. struct kiocb *iocb = dio->iocb;
  1271. struct inode *inode = file_inode(iocb->ki_filp);
  1272. loff_t offset = iocb->ki_pos;
  1273. ssize_t ret;
  1274. if (dio->end_io) {
  1275. ret = dio->end_io(iocb,
  1276. dio->error ? dio->error : dio->size,
  1277. dio->flags);
  1278. } else {
  1279. ret = dio->error;
  1280. }
  1281. if (likely(!ret)) {
  1282. ret = dio->size;
  1283. /* check for short read */
  1284. if (offset + ret > dio->i_size &&
  1285. !(dio->flags & IOMAP_DIO_WRITE))
  1286. ret = dio->i_size - offset;
  1287. iocb->ki_pos += ret;
  1288. }
  1289. /*
  1290. * Try again to invalidate clean pages which might have been cached by
  1291. * non-direct readahead, or faulted in by get_user_pages() if the source
  1292. * of the write was an mmap'ed region of the file we're writing. Either
  1293. * one is a pretty crazy thing to do, so we don't support it 100%. If
  1294. * this invalidation fails, tough, the write still worked...
  1295. *
  1296. * And this page cache invalidation has to be after dio->end_io(), as
  1297. * some filesystems convert unwritten extents to real allocations in
  1298. * end_io() when necessary, otherwise a racing buffer read would cache
  1299. * zeros from unwritten extents.
  1300. */
  1301. if (!dio->error &&
  1302. (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
  1303. int err;
  1304. err = invalidate_inode_pages2_range(inode->i_mapping,
  1305. offset >> PAGE_SHIFT,
  1306. (offset + dio->size - 1) >> PAGE_SHIFT);
  1307. if (err)
  1308. dio_warn_stale_pagecache(iocb->ki_filp);
  1309. }
  1310. /*
  1311. * If this is a DSYNC write, make sure we push it to stable storage now
  1312. * that we've written data.
  1313. */
  1314. if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
  1315. ret = generic_write_sync(iocb, ret);
  1316. inode_dio_end(file_inode(iocb->ki_filp));
  1317. kfree(dio);
  1318. return ret;
  1319. }
  1320. static void iomap_dio_complete_work(struct work_struct *work)
  1321. {
  1322. struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
  1323. struct kiocb *iocb = dio->iocb;
  1324. iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
  1325. }
  1326. /*
  1327. * Set an error in the dio if none is set yet. We have to use cmpxchg
  1328. * as the submission context and the completion context(s) can race to
  1329. * update the error.
  1330. */
  1331. static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
  1332. {
  1333. cmpxchg(&dio->error, 0, ret);
  1334. }
  1335. static void iomap_dio_bio_end_io(struct bio *bio)
  1336. {
  1337. struct iomap_dio *dio = bio->bi_private;
  1338. bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
  1339. if (bio->bi_status)
  1340. iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
  1341. if (atomic_dec_and_test(&dio->ref)) {
  1342. if (dio->wait_for_completion) {
  1343. struct task_struct *waiter = dio->submit.waiter;
  1344. WRITE_ONCE(dio->submit.waiter, NULL);
  1345. wake_up_process(waiter);
  1346. } else if (dio->flags & IOMAP_DIO_WRITE) {
  1347. struct inode *inode = file_inode(dio->iocb->ki_filp);
  1348. INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
  1349. queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
  1350. } else {
  1351. iomap_dio_complete_work(&dio->aio.work);
  1352. }
  1353. }
  1354. if (should_dirty) {
  1355. bio_check_pages_dirty(bio);
  1356. } else {
  1357. struct bio_vec *bvec;
  1358. int i;
  1359. bio_for_each_segment_all(bvec, bio, i)
  1360. put_page(bvec->bv_page);
  1361. bio_put(bio);
  1362. }
  1363. }
  1364. static blk_qc_t
  1365. iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
  1366. unsigned len)
  1367. {
  1368. struct page *page = ZERO_PAGE(0);
  1369. struct bio *bio;
  1370. bio = bio_alloc(GFP_KERNEL, 1);
  1371. bio_set_dev(bio, iomap->bdev);
  1372. bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
  1373. bio->bi_private = dio;
  1374. bio->bi_end_io = iomap_dio_bio_end_io;
  1375. get_page(page);
  1376. __bio_add_page(bio, page, len, 0);
  1377. bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE);
  1378. atomic_inc(&dio->ref);
  1379. return submit_bio(bio);
  1380. }
  1381. static loff_t
  1382. iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
  1383. struct iomap_dio *dio, struct iomap *iomap)
  1384. {
  1385. unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
  1386. unsigned int fs_block_size = i_blocksize(inode), pad;
  1387. unsigned int align = iov_iter_alignment(dio->submit.iter);
  1388. struct iov_iter iter;
  1389. struct bio *bio;
  1390. bool need_zeroout = false;
  1391. bool use_fua = false;
  1392. int nr_pages, ret = 0;
  1393. size_t copied = 0;
  1394. if ((pos | length | align) & ((1 << blkbits) - 1))
  1395. return -EINVAL;
  1396. if (iomap->type == IOMAP_UNWRITTEN) {
  1397. dio->flags |= IOMAP_DIO_UNWRITTEN;
  1398. need_zeroout = true;
  1399. }
  1400. if (iomap->flags & IOMAP_F_SHARED)
  1401. dio->flags |= IOMAP_DIO_COW;
  1402. if (iomap->flags & IOMAP_F_NEW) {
  1403. need_zeroout = true;
  1404. } else if (iomap->type == IOMAP_MAPPED) {
  1405. /*
  1406. * Use a FUA write if we need datasync semantics, this is a pure
  1407. * data IO that doesn't require any metadata updates (including
  1408. * after IO completion such as unwritten extent conversion) and
  1409. * the underlying device supports FUA. This allows us to avoid
  1410. * cache flushes on IO completion.
  1411. */
  1412. if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
  1413. (dio->flags & IOMAP_DIO_WRITE_FUA) &&
  1414. blk_queue_fua(bdev_get_queue(iomap->bdev)))
  1415. use_fua = true;
  1416. }
  1417. /*
  1418. * Operate on a partial iter trimmed to the extent we were called for.
  1419. * We'll update the iter in the dio once we're done with this extent.
  1420. */
  1421. iter = *dio->submit.iter;
  1422. iov_iter_truncate(&iter, length);
  1423. nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
  1424. if (nr_pages <= 0)
  1425. return nr_pages;
  1426. if (need_zeroout) {
  1427. /* zero out from the start of the block to the write offset */
  1428. pad = pos & (fs_block_size - 1);
  1429. if (pad)
  1430. iomap_dio_zero(dio, iomap, pos - pad, pad);
  1431. }
  1432. do {
  1433. size_t n;
  1434. if (dio->error) {
  1435. iov_iter_revert(dio->submit.iter, copied);
  1436. return 0;
  1437. }
  1438. bio = bio_alloc(GFP_KERNEL, nr_pages);
  1439. bio_set_dev(bio, iomap->bdev);
  1440. bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
  1441. bio->bi_write_hint = dio->iocb->ki_hint;
  1442. bio->bi_ioprio = dio->iocb->ki_ioprio;
  1443. bio->bi_private = dio;
  1444. bio->bi_end_io = iomap_dio_bio_end_io;
  1445. ret = bio_iov_iter_get_pages(bio, &iter);
  1446. if (unlikely(ret)) {
  1447. /*
  1448. * We have to stop part way through an IO. We must fall
  1449. * through to the sub-block tail zeroing here, otherwise
  1450. * this short IO may expose stale data in the tail of
  1451. * the block we haven't written data to.
  1452. */
  1453. bio_put(bio);
  1454. goto zero_tail;
  1455. }
  1456. n = bio->bi_iter.bi_size;
  1457. if (dio->flags & IOMAP_DIO_WRITE) {
  1458. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
  1459. if (use_fua)
  1460. bio->bi_opf |= REQ_FUA;
  1461. else
  1462. dio->flags &= ~IOMAP_DIO_WRITE_FUA;
  1463. task_io_account_write(n);
  1464. } else {
  1465. bio->bi_opf = REQ_OP_READ;
  1466. if (dio->flags & IOMAP_DIO_DIRTY)
  1467. bio_set_pages_dirty(bio);
  1468. }
  1469. iov_iter_advance(dio->submit.iter, n);
  1470. dio->size += n;
  1471. pos += n;
  1472. copied += n;
  1473. nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
  1474. atomic_inc(&dio->ref);
  1475. dio->submit.last_queue = bdev_get_queue(iomap->bdev);
  1476. dio->submit.cookie = submit_bio(bio);
  1477. } while (nr_pages);
  1478. /*
  1479. * We need to zeroout the tail of a sub-block write if the extent type
  1480. * requires zeroing or the write extends beyond EOF. If we don't zero
  1481. * the block tail in the latter case, we can expose stale data via mmap
  1482. * reads of the EOF block.
  1483. */
  1484. zero_tail:
  1485. if (need_zeroout ||
  1486. ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
  1487. /* zero out from the end of the write to the end of the block */
  1488. pad = pos & (fs_block_size - 1);
  1489. if (pad)
  1490. iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
  1491. }
  1492. return copied ? copied : ret;
  1493. }
  1494. static loff_t
  1495. iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
  1496. {
  1497. length = iov_iter_zero(length, dio->submit.iter);
  1498. dio->size += length;
  1499. return length;
  1500. }
  1501. static loff_t
  1502. iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
  1503. struct iomap_dio *dio, struct iomap *iomap)
  1504. {
  1505. struct iov_iter *iter = dio->submit.iter;
  1506. size_t copied;
  1507. BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
  1508. if (dio->flags & IOMAP_DIO_WRITE) {
  1509. loff_t size = inode->i_size;
  1510. if (pos > size)
  1511. memset(iomap->inline_data + size, 0, pos - size);
  1512. copied = copy_from_iter(iomap->inline_data + pos, length, iter);
  1513. if (copied) {
  1514. if (pos + copied > size)
  1515. i_size_write(inode, pos + copied);
  1516. mark_inode_dirty(inode);
  1517. }
  1518. } else {
  1519. copied = copy_to_iter(iomap->inline_data + pos, length, iter);
  1520. }
  1521. dio->size += copied;
  1522. return copied;
  1523. }
  1524. static loff_t
  1525. iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
  1526. void *data, struct iomap *iomap)
  1527. {
  1528. struct iomap_dio *dio = data;
  1529. switch (iomap->type) {
  1530. case IOMAP_HOLE:
  1531. if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
  1532. return -EIO;
  1533. return iomap_dio_hole_actor(length, dio);
  1534. case IOMAP_UNWRITTEN:
  1535. if (!(dio->flags & IOMAP_DIO_WRITE))
  1536. return iomap_dio_hole_actor(length, dio);
  1537. return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
  1538. case IOMAP_MAPPED:
  1539. return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
  1540. case IOMAP_INLINE:
  1541. return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
  1542. default:
  1543. WARN_ON_ONCE(1);
  1544. return -EIO;
  1545. }
  1546. }
  1547. /*
  1548. * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
  1549. * is being issued as AIO or not. This allows us to optimise pure data writes
  1550. * to use REQ_FUA rather than requiring generic_write_sync() to issue a
  1551. * REQ_FLUSH post write. This is slightly tricky because a single request here
  1552. * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
  1553. * may be pure data writes. In that case, we still need to do a full data sync
  1554. * completion.
  1555. */
  1556. ssize_t
  1557. iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
  1558. const struct iomap_ops *ops, iomap_dio_end_io_t end_io)
  1559. {
  1560. struct address_space *mapping = iocb->ki_filp->f_mapping;
  1561. struct inode *inode = file_inode(iocb->ki_filp);
  1562. size_t count = iov_iter_count(iter);
  1563. loff_t pos = iocb->ki_pos, start = pos;
  1564. loff_t end = iocb->ki_pos + count - 1, ret = 0;
  1565. unsigned int flags = IOMAP_DIRECT;
  1566. bool wait_for_completion = is_sync_kiocb(iocb);
  1567. struct blk_plug plug;
  1568. struct iomap_dio *dio;
  1569. lockdep_assert_held(&inode->i_rwsem);
  1570. if (!count)
  1571. return 0;
  1572. dio = kmalloc(sizeof(*dio), GFP_KERNEL);
  1573. if (!dio)
  1574. return -ENOMEM;
  1575. dio->iocb = iocb;
  1576. atomic_set(&dio->ref, 1);
  1577. dio->size = 0;
  1578. dio->i_size = i_size_read(inode);
  1579. dio->end_io = end_io;
  1580. dio->error = 0;
  1581. dio->flags = 0;
  1582. dio->submit.iter = iter;
  1583. dio->submit.waiter = current;
  1584. dio->submit.cookie = BLK_QC_T_NONE;
  1585. dio->submit.last_queue = NULL;
  1586. if (iov_iter_rw(iter) == READ) {
  1587. if (pos >= dio->i_size)
  1588. goto out_free_dio;
  1589. if (iter->type == ITER_IOVEC)
  1590. dio->flags |= IOMAP_DIO_DIRTY;
  1591. } else {
  1592. flags |= IOMAP_WRITE;
  1593. dio->flags |= IOMAP_DIO_WRITE;
  1594. /* for data sync or sync, we need sync completion processing */
  1595. if (iocb->ki_flags & IOCB_DSYNC)
  1596. dio->flags |= IOMAP_DIO_NEED_SYNC;
  1597. /*
  1598. * For datasync only writes, we optimistically try using FUA for
  1599. * this IO. Any non-FUA write that occurs will clear this flag,
  1600. * hence we know before completion whether a cache flush is
  1601. * necessary.
  1602. */
  1603. if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
  1604. dio->flags |= IOMAP_DIO_WRITE_FUA;
  1605. }
  1606. if (iocb->ki_flags & IOCB_NOWAIT) {
  1607. if (filemap_range_has_page(mapping, start, end)) {
  1608. ret = -EAGAIN;
  1609. goto out_free_dio;
  1610. }
  1611. flags |= IOMAP_NOWAIT;
  1612. }
  1613. ret = filemap_write_and_wait_range(mapping, start, end);
  1614. if (ret)
  1615. goto out_free_dio;
  1616. /*
  1617. * Try to invalidate cache pages for the range we're direct
  1618. * writing. If this invalidation fails, tough, the write will
  1619. * still work, but racing two incompatible write paths is a
  1620. * pretty crazy thing to do, so we don't support it 100%.
  1621. */
  1622. ret = invalidate_inode_pages2_range(mapping,
  1623. start >> PAGE_SHIFT, end >> PAGE_SHIFT);
  1624. if (ret)
  1625. dio_warn_stale_pagecache(iocb->ki_filp);
  1626. ret = 0;
  1627. if (iov_iter_rw(iter) == WRITE && !wait_for_completion &&
  1628. !inode->i_sb->s_dio_done_wq) {
  1629. ret = sb_init_dio_done_wq(inode->i_sb);
  1630. if (ret < 0)
  1631. goto out_free_dio;
  1632. }
  1633. inode_dio_begin(inode);
  1634. blk_start_plug(&plug);
  1635. do {
  1636. ret = iomap_apply(inode, pos, count, flags, ops, dio,
  1637. iomap_dio_actor);
  1638. if (ret <= 0) {
  1639. /* magic error code to fall back to buffered I/O */
  1640. if (ret == -ENOTBLK) {
  1641. wait_for_completion = true;
  1642. ret = 0;
  1643. }
  1644. break;
  1645. }
  1646. pos += ret;
  1647. if (iov_iter_rw(iter) == READ && pos >= dio->i_size) {
  1648. /*
  1649. * We only report that we've read data up to i_size.
  1650. * Revert iter to a state corresponding to that as
  1651. * some callers (such as splice code) rely on it.
  1652. */
  1653. iov_iter_revert(iter, pos - dio->i_size);
  1654. break;
  1655. }
  1656. } while ((count = iov_iter_count(iter)) > 0);
  1657. blk_finish_plug(&plug);
  1658. if (ret < 0)
  1659. iomap_dio_set_error(dio, ret);
  1660. /*
  1661. * If all the writes we issued were FUA, we don't need to flush the
  1662. * cache on IO completion. Clear the sync flag for this case.
  1663. */
  1664. if (dio->flags & IOMAP_DIO_WRITE_FUA)
  1665. dio->flags &= ~IOMAP_DIO_NEED_SYNC;
  1666. /*
  1667. * We are about to drop our additional submission reference, which
  1668. * might be the last reference to the dio. There are three three
  1669. * different ways we can progress here:
  1670. *
  1671. * (a) If this is the last reference we will always complete and free
  1672. * the dio ourselves.
  1673. * (b) If this is not the last reference, and we serve an asynchronous
  1674. * iocb, we must never touch the dio after the decrement, the
  1675. * I/O completion handler will complete and free it.
  1676. * (c) If this is not the last reference, but we serve a synchronous
  1677. * iocb, the I/O completion handler will wake us up on the drop
  1678. * of the final reference, and we will complete and free it here
  1679. * after we got woken by the I/O completion handler.
  1680. */
  1681. dio->wait_for_completion = wait_for_completion;
  1682. if (!atomic_dec_and_test(&dio->ref)) {
  1683. if (!wait_for_completion)
  1684. return -EIOCBQUEUED;
  1685. for (;;) {
  1686. set_current_state(TASK_UNINTERRUPTIBLE);
  1687. if (!READ_ONCE(dio->submit.waiter))
  1688. break;
  1689. if (!(iocb->ki_flags & IOCB_HIPRI) ||
  1690. !dio->submit.last_queue ||
  1691. !blk_poll(dio->submit.last_queue,
  1692. dio->submit.cookie))
  1693. io_schedule();
  1694. }
  1695. __set_current_state(TASK_RUNNING);
  1696. }
  1697. return iomap_dio_complete(dio);
  1698. out_free_dio:
  1699. kfree(dio);
  1700. return ret;
  1701. }
  1702. EXPORT_SYMBOL_GPL(iomap_dio_rw);
  1703. /* Swapfile activation */
  1704. #ifdef CONFIG_SWAP
  1705. struct iomap_swapfile_info {
  1706. struct iomap iomap; /* accumulated iomap */
  1707. struct swap_info_struct *sis;
  1708. uint64_t lowest_ppage; /* lowest physical addr seen (pages) */
  1709. uint64_t highest_ppage; /* highest physical addr seen (pages) */
  1710. unsigned long nr_pages; /* number of pages collected */
  1711. int nr_extents; /* extent count */
  1712. };
  1713. /*
  1714. * Collect physical extents for this swap file. Physical extents reported to
  1715. * the swap code must be trimmed to align to a page boundary. The logical
  1716. * offset within the file is irrelevant since the swapfile code maps logical
  1717. * page numbers of the swap device to the physical page-aligned extents.
  1718. */
  1719. static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi)
  1720. {
  1721. struct iomap *iomap = &isi->iomap;
  1722. unsigned long nr_pages;
  1723. uint64_t first_ppage;
  1724. uint64_t first_ppage_reported;
  1725. uint64_t next_ppage;
  1726. int error;
  1727. /*
  1728. * Round the start up and the end down so that the physical
  1729. * extent aligns to a page boundary.
  1730. */
  1731. first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT;
  1732. next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >>
  1733. PAGE_SHIFT;
  1734. /* Skip too-short physical extents. */
  1735. if (first_ppage >= next_ppage)
  1736. return 0;
  1737. nr_pages = next_ppage - first_ppage;
  1738. /*
  1739. * Calculate how much swap space we're adding; the first page contains
  1740. * the swap header and doesn't count. The mm still wants that first
  1741. * page fed to add_swap_extent, however.
  1742. */
  1743. first_ppage_reported = first_ppage;
  1744. if (iomap->offset == 0)
  1745. first_ppage_reported++;
  1746. if (isi->lowest_ppage > first_ppage_reported)
  1747. isi->lowest_ppage = first_ppage_reported;
  1748. if (isi->highest_ppage < (next_ppage - 1))
  1749. isi->highest_ppage = next_ppage - 1;
  1750. /* Add extent, set up for the next call. */
  1751. error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage);
  1752. if (error < 0)
  1753. return error;
  1754. isi->nr_extents += error;
  1755. isi->nr_pages += nr_pages;
  1756. return 0;
  1757. }
  1758. /*
  1759. * Accumulate iomaps for this swap file. We have to accumulate iomaps because
  1760. * swap only cares about contiguous page-aligned physical extents and makes no
  1761. * distinction between written and unwritten extents.
  1762. */
  1763. static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
  1764. loff_t count, void *data, struct iomap *iomap)
  1765. {
  1766. struct iomap_swapfile_info *isi = data;
  1767. int error;
  1768. switch (iomap->type) {
  1769. case IOMAP_MAPPED:
  1770. case IOMAP_UNWRITTEN:
  1771. /* Only real or unwritten extents. */
  1772. break;
  1773. case IOMAP_INLINE:
  1774. /* No inline data. */
  1775. pr_err("swapon: file is inline\n");
  1776. return -EINVAL;
  1777. default:
  1778. pr_err("swapon: file has unallocated extents\n");
  1779. return -EINVAL;
  1780. }
  1781. /* No uncommitted metadata or shared blocks. */
  1782. if (iomap->flags & IOMAP_F_DIRTY) {
  1783. pr_err("swapon: file is not committed\n");
  1784. return -EINVAL;
  1785. }
  1786. if (iomap->flags & IOMAP_F_SHARED) {
  1787. pr_err("swapon: file has shared extents\n");
  1788. return -EINVAL;
  1789. }
  1790. /* Only one bdev per swap file. */
  1791. if (iomap->bdev != isi->sis->bdev) {
  1792. pr_err("swapon: file is on multiple devices\n");
  1793. return -EINVAL;
  1794. }
  1795. if (isi->iomap.length == 0) {
  1796. /* No accumulated extent, so just store it. */
  1797. memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
  1798. } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) {
  1799. /* Append this to the accumulated extent. */
  1800. isi->iomap.length += iomap->length;
  1801. } else {
  1802. /* Otherwise, add the retained iomap and store this one. */
  1803. error = iomap_swapfile_add_extent(isi);
  1804. if (error)
  1805. return error;
  1806. memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
  1807. }
  1808. return count;
  1809. }
  1810. /*
  1811. * Iterate a swap file's iomaps to construct physical extents that can be
  1812. * passed to the swapfile subsystem.
  1813. */
  1814. int iomap_swapfile_activate(struct swap_info_struct *sis,
  1815. struct file *swap_file, sector_t *pagespan,
  1816. const struct iomap_ops *ops)
  1817. {
  1818. struct iomap_swapfile_info isi = {
  1819. .sis = sis,
  1820. .lowest_ppage = (sector_t)-1ULL,
  1821. };
  1822. struct address_space *mapping = swap_file->f_mapping;
  1823. struct inode *inode = mapping->host;
  1824. loff_t pos = 0;
  1825. loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE);
  1826. loff_t ret;
  1827. /*
  1828. * Persist all file mapping metadata so that we won't have any
  1829. * IOMAP_F_DIRTY iomaps.
  1830. */
  1831. ret = vfs_fsync(swap_file, 1);
  1832. if (ret)
  1833. return ret;
  1834. while (len > 0) {
  1835. ret = iomap_apply(inode, pos, len, IOMAP_REPORT,
  1836. ops, &isi, iomap_swapfile_activate_actor);
  1837. if (ret <= 0)
  1838. return ret;
  1839. pos += ret;
  1840. len -= ret;
  1841. }
  1842. if (isi.iomap.length) {
  1843. ret = iomap_swapfile_add_extent(&isi);
  1844. if (ret)
  1845. return ret;
  1846. }
  1847. *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage;
  1848. sis->max = isi.nr_pages;
  1849. sis->pages = isi.nr_pages - 1;
  1850. sis->highest_bit = isi.nr_pages - 1;
  1851. return isi.nr_extents;
  1852. }
  1853. EXPORT_SYMBOL_GPL(iomap_swapfile_activate);
  1854. #endif /* CONFIG_SWAP */
  1855. static loff_t
  1856. iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
  1857. void *data, struct iomap *iomap)
  1858. {
  1859. sector_t *bno = data, addr;
  1860. if (iomap->type == IOMAP_MAPPED) {
  1861. addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
  1862. if (addr > INT_MAX)
  1863. WARN(1, "would truncate bmap result\n");
  1864. else
  1865. *bno = addr;
  1866. }
  1867. return 0;
  1868. }
  1869. /* legacy ->bmap interface. 0 is the error return (!) */
  1870. sector_t
  1871. iomap_bmap(struct address_space *mapping, sector_t bno,
  1872. const struct iomap_ops *ops)
  1873. {
  1874. struct inode *inode = mapping->host;
  1875. loff_t pos = bno << inode->i_blkbits;
  1876. unsigned blocksize = i_blocksize(inode);
  1877. if (filemap_write_and_wait(mapping))
  1878. return 0;
  1879. bno = 0;
  1880. iomap_apply(inode, pos, blocksize, 0, ops, &bno, iomap_bmap_actor);
  1881. return bno;
  1882. }
  1883. EXPORT_SYMBOL_GPL(iomap_bmap);