iomap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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/file.h>
  24. #include <linux/uio.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/dax.h>
  28. #include "internal.h"
  29. /*
  30. * Execute a iomap write on a segment of the mapping that spans a
  31. * contiguous range of pages that have identical block mapping state.
  32. *
  33. * This avoids the need to map pages individually, do individual allocations
  34. * for each page and most importantly avoid the need for filesystem specific
  35. * locking per page. Instead, all the operations are amortised over the entire
  36. * range of pages. It is assumed that the filesystems will lock whatever
  37. * resources they require in the iomap_begin call, and release them in the
  38. * iomap_end call.
  39. */
  40. loff_t
  41. iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags,
  42. struct iomap_ops *ops, void *data, iomap_actor_t actor)
  43. {
  44. struct iomap iomap = { 0 };
  45. loff_t written = 0, ret;
  46. /*
  47. * Need to map a range from start position for length bytes. This can
  48. * span multiple pages - it is only guaranteed to return a range of a
  49. * single type of pages (e.g. all into a hole, all mapped or all
  50. * unwritten). Failure at this point has nothing to undo.
  51. *
  52. * If allocation is required for this range, reserve the space now so
  53. * that the allocation is guaranteed to succeed later on. Once we copy
  54. * the data into the page cache pages, then we cannot fail otherwise we
  55. * expose transient stale data. If the reserve fails, we can safely
  56. * back out at this point as there is nothing to undo.
  57. */
  58. ret = ops->iomap_begin(inode, pos, length, flags, &iomap);
  59. if (ret)
  60. return ret;
  61. if (WARN_ON(iomap.offset > pos))
  62. return -EIO;
  63. /*
  64. * Cut down the length to the one actually provided by the filesystem,
  65. * as it might not be able to give us the whole size that we requested.
  66. */
  67. if (iomap.offset + iomap.length < pos + length)
  68. length = iomap.offset + iomap.length - pos;
  69. /*
  70. * Now that we have guaranteed that the space allocation will succeed.
  71. * we can do the copy-in page by page without having to worry about
  72. * failures exposing transient data.
  73. */
  74. written = actor(inode, pos, length, data, &iomap);
  75. /*
  76. * Now the data has been copied, commit the range we've copied. This
  77. * should not fail unless the filesystem has had a fatal error.
  78. */
  79. if (ops->iomap_end) {
  80. ret = ops->iomap_end(inode, pos, length,
  81. written > 0 ? written : 0,
  82. flags, &iomap);
  83. }
  84. return written ? written : ret;
  85. }
  86. static void
  87. iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
  88. {
  89. loff_t i_size = i_size_read(inode);
  90. /*
  91. * Only truncate newly allocated pages beyoned EOF, even if the
  92. * write started inside the existing inode size.
  93. */
  94. if (pos + len > i_size)
  95. truncate_pagecache_range(inode, max(pos, i_size), pos + len);
  96. }
  97. static int
  98. iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
  99. struct page **pagep, struct iomap *iomap)
  100. {
  101. pgoff_t index = pos >> PAGE_SHIFT;
  102. struct page *page;
  103. int status = 0;
  104. BUG_ON(pos + len > iomap->offset + iomap->length);
  105. if (fatal_signal_pending(current))
  106. return -EINTR;
  107. page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
  108. if (!page)
  109. return -ENOMEM;
  110. status = __block_write_begin_int(page, pos, len, NULL, iomap);
  111. if (unlikely(status)) {
  112. unlock_page(page);
  113. put_page(page);
  114. page = NULL;
  115. iomap_write_failed(inode, pos, len);
  116. }
  117. *pagep = page;
  118. return status;
  119. }
  120. static int
  121. iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
  122. unsigned copied, struct page *page)
  123. {
  124. int ret;
  125. ret = generic_write_end(NULL, inode->i_mapping, pos, len,
  126. copied, page, NULL);
  127. if (ret < len)
  128. iomap_write_failed(inode, pos, len);
  129. return ret;
  130. }
  131. static loff_t
  132. iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  133. struct iomap *iomap)
  134. {
  135. struct iov_iter *i = data;
  136. long status = 0;
  137. ssize_t written = 0;
  138. unsigned int flags = AOP_FLAG_NOFS;
  139. /*
  140. * Copies from kernel address space cannot fail (NFSD is a big user).
  141. */
  142. if (!iter_is_iovec(i))
  143. flags |= AOP_FLAG_UNINTERRUPTIBLE;
  144. do {
  145. struct page *page;
  146. unsigned long offset; /* Offset into pagecache page */
  147. unsigned long bytes; /* Bytes to write to page */
  148. size_t copied; /* Bytes copied from user */
  149. offset = (pos & (PAGE_SIZE - 1));
  150. bytes = min_t(unsigned long, PAGE_SIZE - offset,
  151. iov_iter_count(i));
  152. again:
  153. if (bytes > length)
  154. bytes = length;
  155. /*
  156. * Bring in the user page that we will copy from _first_.
  157. * Otherwise there's a nasty deadlock on copying from the
  158. * same page as we're writing to, without it being marked
  159. * up-to-date.
  160. *
  161. * Not only is this an optimisation, but it is also required
  162. * to check that the address is actually valid, when atomic
  163. * usercopies are used, below.
  164. */
  165. if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
  166. status = -EFAULT;
  167. break;
  168. }
  169. status = iomap_write_begin(inode, pos, bytes, flags, &page,
  170. iomap);
  171. if (unlikely(status))
  172. break;
  173. if (mapping_writably_mapped(inode->i_mapping))
  174. flush_dcache_page(page);
  175. copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
  176. flush_dcache_page(page);
  177. status = iomap_write_end(inode, pos, bytes, copied, page);
  178. if (unlikely(status < 0))
  179. break;
  180. copied = status;
  181. cond_resched();
  182. iov_iter_advance(i, copied);
  183. if (unlikely(copied == 0)) {
  184. /*
  185. * If we were unable to copy any data at all, we must
  186. * fall back to a single segment length write.
  187. *
  188. * If we didn't fallback here, we could livelock
  189. * because not all segments in the iov can be copied at
  190. * once without a pagefault.
  191. */
  192. bytes = min_t(unsigned long, PAGE_SIZE - offset,
  193. iov_iter_single_seg_count(i));
  194. goto again;
  195. }
  196. pos += copied;
  197. written += copied;
  198. length -= copied;
  199. balance_dirty_pages_ratelimited(inode->i_mapping);
  200. } while (iov_iter_count(i) && length);
  201. return written ? written : status;
  202. }
  203. ssize_t
  204. iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
  205. struct iomap_ops *ops)
  206. {
  207. struct inode *inode = iocb->ki_filp->f_mapping->host;
  208. loff_t pos = iocb->ki_pos, ret = 0, written = 0;
  209. while (iov_iter_count(iter)) {
  210. ret = iomap_apply(inode, pos, iov_iter_count(iter),
  211. IOMAP_WRITE, ops, iter, iomap_write_actor);
  212. if (ret <= 0)
  213. break;
  214. pos += ret;
  215. written += ret;
  216. }
  217. return written ? written : ret;
  218. }
  219. EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
  220. static struct page *
  221. __iomap_read_page(struct inode *inode, loff_t offset)
  222. {
  223. struct address_space *mapping = inode->i_mapping;
  224. struct page *page;
  225. page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
  226. if (IS_ERR(page))
  227. return page;
  228. if (!PageUptodate(page)) {
  229. put_page(page);
  230. return ERR_PTR(-EIO);
  231. }
  232. return page;
  233. }
  234. static loff_t
  235. iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  236. struct iomap *iomap)
  237. {
  238. long status = 0;
  239. ssize_t written = 0;
  240. do {
  241. struct page *page, *rpage;
  242. unsigned long offset; /* Offset into pagecache page */
  243. unsigned long bytes; /* Bytes to write to page */
  244. offset = (pos & (PAGE_SIZE - 1));
  245. bytes = min_t(loff_t, PAGE_SIZE - offset, length);
  246. rpage = __iomap_read_page(inode, pos);
  247. if (IS_ERR(rpage))
  248. return PTR_ERR(rpage);
  249. status = iomap_write_begin(inode, pos, bytes,
  250. AOP_FLAG_NOFS | AOP_FLAG_UNINTERRUPTIBLE,
  251. &page, iomap);
  252. put_page(rpage);
  253. if (unlikely(status))
  254. return status;
  255. WARN_ON_ONCE(!PageUptodate(page));
  256. status = iomap_write_end(inode, pos, bytes, bytes, page);
  257. if (unlikely(status <= 0)) {
  258. if (WARN_ON_ONCE(status == 0))
  259. return -EIO;
  260. return status;
  261. }
  262. cond_resched();
  263. pos += status;
  264. written += status;
  265. length -= status;
  266. balance_dirty_pages_ratelimited(inode->i_mapping);
  267. } while (length);
  268. return written;
  269. }
  270. int
  271. iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
  272. struct iomap_ops *ops)
  273. {
  274. loff_t ret;
  275. while (len) {
  276. ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
  277. iomap_dirty_actor);
  278. if (ret <= 0)
  279. return ret;
  280. pos += ret;
  281. len -= ret;
  282. }
  283. return 0;
  284. }
  285. EXPORT_SYMBOL_GPL(iomap_file_dirty);
  286. static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
  287. unsigned bytes, struct iomap *iomap)
  288. {
  289. struct page *page;
  290. int status;
  291. status = iomap_write_begin(inode, pos, bytes,
  292. AOP_FLAG_UNINTERRUPTIBLE | AOP_FLAG_NOFS, &page, iomap);
  293. if (status)
  294. return status;
  295. zero_user(page, offset, bytes);
  296. mark_page_accessed(page);
  297. return iomap_write_end(inode, pos, bytes, bytes, page);
  298. }
  299. static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
  300. struct iomap *iomap)
  301. {
  302. sector_t sector = iomap->blkno +
  303. (((pos & ~(PAGE_SIZE - 1)) - iomap->offset) >> 9);
  304. return __dax_zero_page_range(iomap->bdev, sector, offset, bytes);
  305. }
  306. static loff_t
  307. iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
  308. void *data, struct iomap *iomap)
  309. {
  310. bool *did_zero = data;
  311. loff_t written = 0;
  312. int status;
  313. /* already zeroed? we're done. */
  314. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  315. return count;
  316. do {
  317. unsigned offset, bytes;
  318. offset = pos & (PAGE_SIZE - 1); /* Within page */
  319. bytes = min_t(loff_t, PAGE_SIZE - offset, count);
  320. if (IS_DAX(inode))
  321. status = iomap_dax_zero(pos, offset, bytes, iomap);
  322. else
  323. status = iomap_zero(inode, pos, offset, bytes, iomap);
  324. if (status < 0)
  325. return status;
  326. pos += bytes;
  327. count -= bytes;
  328. written += bytes;
  329. if (did_zero)
  330. *did_zero = true;
  331. } while (count > 0);
  332. return written;
  333. }
  334. int
  335. iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
  336. struct iomap_ops *ops)
  337. {
  338. loff_t ret;
  339. while (len > 0) {
  340. ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
  341. ops, did_zero, iomap_zero_range_actor);
  342. if (ret <= 0)
  343. return ret;
  344. pos += ret;
  345. len -= ret;
  346. }
  347. return 0;
  348. }
  349. EXPORT_SYMBOL_GPL(iomap_zero_range);
  350. int
  351. iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
  352. struct iomap_ops *ops)
  353. {
  354. unsigned int blocksize = i_blocksize(inode);
  355. unsigned int off = pos & (blocksize - 1);
  356. /* Block boundary? Nothing to do */
  357. if (!off)
  358. return 0;
  359. return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
  360. }
  361. EXPORT_SYMBOL_GPL(iomap_truncate_page);
  362. static loff_t
  363. iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
  364. void *data, struct iomap *iomap)
  365. {
  366. struct page *page = data;
  367. int ret;
  368. ret = __block_write_begin_int(page, pos, length, NULL, iomap);
  369. if (ret)
  370. return ret;
  371. block_commit_write(page, 0, length);
  372. return length;
  373. }
  374. int iomap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  375. struct iomap_ops *ops)
  376. {
  377. struct page *page = vmf->page;
  378. struct inode *inode = file_inode(vma->vm_file);
  379. unsigned long length;
  380. loff_t offset, size;
  381. ssize_t ret;
  382. lock_page(page);
  383. size = i_size_read(inode);
  384. if ((page->mapping != inode->i_mapping) ||
  385. (page_offset(page) > size)) {
  386. /* We overload EFAULT to mean page got truncated */
  387. ret = -EFAULT;
  388. goto out_unlock;
  389. }
  390. /* page is wholly or partially inside EOF */
  391. if (((page->index + 1) << PAGE_SHIFT) > size)
  392. length = size & ~PAGE_MASK;
  393. else
  394. length = PAGE_SIZE;
  395. offset = page_offset(page);
  396. while (length > 0) {
  397. ret = iomap_apply(inode, offset, length, IOMAP_WRITE,
  398. ops, page, iomap_page_mkwrite_actor);
  399. if (unlikely(ret <= 0))
  400. goto out_unlock;
  401. offset += ret;
  402. length -= ret;
  403. }
  404. set_page_dirty(page);
  405. wait_for_stable_page(page);
  406. return 0;
  407. out_unlock:
  408. unlock_page(page);
  409. return ret;
  410. }
  411. EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
  412. struct fiemap_ctx {
  413. struct fiemap_extent_info *fi;
  414. struct iomap prev;
  415. };
  416. static int iomap_to_fiemap(struct fiemap_extent_info *fi,
  417. struct iomap *iomap, u32 flags)
  418. {
  419. switch (iomap->type) {
  420. case IOMAP_HOLE:
  421. /* skip holes */
  422. return 0;
  423. case IOMAP_DELALLOC:
  424. flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
  425. break;
  426. case IOMAP_UNWRITTEN:
  427. flags |= FIEMAP_EXTENT_UNWRITTEN;
  428. break;
  429. case IOMAP_MAPPED:
  430. break;
  431. }
  432. if (iomap->flags & IOMAP_F_MERGED)
  433. flags |= FIEMAP_EXTENT_MERGED;
  434. if (iomap->flags & IOMAP_F_SHARED)
  435. flags |= FIEMAP_EXTENT_SHARED;
  436. return fiemap_fill_next_extent(fi, iomap->offset,
  437. iomap->blkno != IOMAP_NULL_BLOCK ? iomap->blkno << 9: 0,
  438. iomap->length, flags);
  439. }
  440. static loff_t
  441. iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  442. struct iomap *iomap)
  443. {
  444. struct fiemap_ctx *ctx = data;
  445. loff_t ret = length;
  446. if (iomap->type == IOMAP_HOLE)
  447. return length;
  448. ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
  449. ctx->prev = *iomap;
  450. switch (ret) {
  451. case 0: /* success */
  452. return length;
  453. case 1: /* extent array full */
  454. return 0;
  455. default:
  456. return ret;
  457. }
  458. }
  459. int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
  460. loff_t start, loff_t len, struct iomap_ops *ops)
  461. {
  462. struct fiemap_ctx ctx;
  463. loff_t ret;
  464. memset(&ctx, 0, sizeof(ctx));
  465. ctx.fi = fi;
  466. ctx.prev.type = IOMAP_HOLE;
  467. ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
  468. if (ret)
  469. return ret;
  470. if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
  471. ret = filemap_write_and_wait(inode->i_mapping);
  472. if (ret)
  473. return ret;
  474. }
  475. while (len > 0) {
  476. ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
  477. iomap_fiemap_actor);
  478. /* inode with no (attribute) mapping will give ENOENT */
  479. if (ret == -ENOENT)
  480. break;
  481. if (ret < 0)
  482. return ret;
  483. if (ret == 0)
  484. break;
  485. start += ret;
  486. len -= ret;
  487. }
  488. if (ctx.prev.type != IOMAP_HOLE) {
  489. ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
  490. if (ret < 0)
  491. return ret;
  492. }
  493. return 0;
  494. }
  495. EXPORT_SYMBOL_GPL(iomap_fiemap);