iomap.c 36 KB

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