iomap.c 46 KB

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