iomap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. typedef loff_t (*iomap_actor_t)(struct inode *inode, loff_t pos, loff_t len,
  30. void *data, struct iomap *iomap);
  31. /*
  32. * Execute a iomap write on a segment of the mapping that spans a
  33. * contiguous range of pages that have identical block mapping state.
  34. *
  35. * This avoids the need to map pages individually, do individual allocations
  36. * for each page and most importantly avoid the need for filesystem specific
  37. * locking per page. Instead, all the operations are amortised over the entire
  38. * range of pages. It is assumed that the filesystems will lock whatever
  39. * resources they require in the iomap_begin call, and release them in the
  40. * iomap_end call.
  41. */
  42. static loff_t
  43. iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags,
  44. struct iomap_ops *ops, void *data, iomap_actor_t actor)
  45. {
  46. struct iomap iomap = { 0 };
  47. loff_t written = 0, ret;
  48. /*
  49. * Need to map a range from start position for length bytes. This can
  50. * span multiple pages - it is only guaranteed to return a range of a
  51. * single type of pages (e.g. all into a hole, all mapped or all
  52. * unwritten). Failure at this point has nothing to undo.
  53. *
  54. * If allocation is required for this range, reserve the space now so
  55. * that the allocation is guaranteed to succeed later on. Once we copy
  56. * the data into the page cache pages, then we cannot fail otherwise we
  57. * expose transient stale data. If the reserve fails, we can safely
  58. * back out at this point as there is nothing to undo.
  59. */
  60. ret = ops->iomap_begin(inode, pos, length, flags, &iomap);
  61. if (ret)
  62. return ret;
  63. if (WARN_ON(iomap.offset > pos))
  64. return -EIO;
  65. /*
  66. * Cut down the length to the one actually provided by the filesystem,
  67. * as it might not be able to give us the whole size that we requested.
  68. */
  69. if (iomap.offset + iomap.length < pos + length)
  70. length = iomap.offset + iomap.length - pos;
  71. /*
  72. * Now that we have guaranteed that the space allocation will succeed.
  73. * we can do the copy-in page by page without having to worry about
  74. * failures exposing transient data.
  75. */
  76. written = actor(inode, pos, length, data, &iomap);
  77. /*
  78. * Now the data has been copied, commit the range we've copied. This
  79. * should not fail unless the filesystem has had a fatal error.
  80. */
  81. if (ops->iomap_end) {
  82. ret = ops->iomap_end(inode, pos, length,
  83. written > 0 ? written : 0,
  84. flags, &iomap);
  85. }
  86. return written ? written : ret;
  87. }
  88. static void
  89. iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
  90. {
  91. loff_t i_size = i_size_read(inode);
  92. /*
  93. * Only truncate newly allocated pages beyoned EOF, even if the
  94. * write started inside the existing inode size.
  95. */
  96. if (pos + len > i_size)
  97. truncate_pagecache_range(inode, max(pos, i_size), pos + len);
  98. }
  99. static int
  100. iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
  101. struct page **pagep, struct iomap *iomap)
  102. {
  103. pgoff_t index = pos >> PAGE_SHIFT;
  104. struct page *page;
  105. int status = 0;
  106. BUG_ON(pos + len > iomap->offset + iomap->length);
  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 int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
  221. unsigned bytes, struct iomap *iomap)
  222. {
  223. struct page *page;
  224. int status;
  225. status = iomap_write_begin(inode, pos, bytes,
  226. AOP_FLAG_UNINTERRUPTIBLE | AOP_FLAG_NOFS, &page, iomap);
  227. if (status)
  228. return status;
  229. zero_user(page, offset, bytes);
  230. mark_page_accessed(page);
  231. return iomap_write_end(inode, pos, bytes, bytes, page);
  232. }
  233. static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
  234. struct iomap *iomap)
  235. {
  236. sector_t sector = iomap->blkno +
  237. (((pos & ~(PAGE_SIZE - 1)) - iomap->offset) >> 9);
  238. return __dax_zero_page_range(iomap->bdev, sector, offset, bytes);
  239. }
  240. static loff_t
  241. iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
  242. void *data, struct iomap *iomap)
  243. {
  244. bool *did_zero = data;
  245. loff_t written = 0;
  246. int status;
  247. /* already zeroed? we're done. */
  248. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  249. return count;
  250. do {
  251. unsigned offset, bytes;
  252. offset = pos & (PAGE_SIZE - 1); /* Within page */
  253. bytes = min_t(unsigned, PAGE_SIZE - offset, count);
  254. if (IS_DAX(inode))
  255. status = iomap_dax_zero(pos, offset, bytes, iomap);
  256. else
  257. status = iomap_zero(inode, pos, offset, bytes, iomap);
  258. if (status < 0)
  259. return status;
  260. pos += bytes;
  261. count -= bytes;
  262. written += bytes;
  263. if (did_zero)
  264. *did_zero = true;
  265. } while (count > 0);
  266. return written;
  267. }
  268. int
  269. iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
  270. struct iomap_ops *ops)
  271. {
  272. loff_t ret;
  273. while (len > 0) {
  274. ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
  275. ops, did_zero, iomap_zero_range_actor);
  276. if (ret <= 0)
  277. return ret;
  278. pos += ret;
  279. len -= ret;
  280. }
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(iomap_zero_range);
  284. int
  285. iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
  286. struct iomap_ops *ops)
  287. {
  288. unsigned blocksize = (1 << inode->i_blkbits);
  289. unsigned off = pos & (blocksize - 1);
  290. /* Block boundary? Nothing to do */
  291. if (!off)
  292. return 0;
  293. return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
  294. }
  295. EXPORT_SYMBOL_GPL(iomap_truncate_page);
  296. static loff_t
  297. iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
  298. void *data, struct iomap *iomap)
  299. {
  300. struct page *page = data;
  301. int ret;
  302. ret = __block_write_begin_int(page, pos & ~PAGE_MASK, length,
  303. NULL, iomap);
  304. if (ret)
  305. return ret;
  306. block_commit_write(page, 0, length);
  307. return length;
  308. }
  309. int iomap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  310. struct iomap_ops *ops)
  311. {
  312. struct page *page = vmf->page;
  313. struct inode *inode = file_inode(vma->vm_file);
  314. unsigned long length;
  315. loff_t offset, size;
  316. ssize_t ret;
  317. lock_page(page);
  318. size = i_size_read(inode);
  319. if ((page->mapping != inode->i_mapping) ||
  320. (page_offset(page) > size)) {
  321. /* We overload EFAULT to mean page got truncated */
  322. ret = -EFAULT;
  323. goto out_unlock;
  324. }
  325. /* page is wholly or partially inside EOF */
  326. if (((page->index + 1) << PAGE_SHIFT) > size)
  327. length = size & ~PAGE_MASK;
  328. else
  329. length = PAGE_SIZE;
  330. offset = page_offset(page);
  331. while (length > 0) {
  332. ret = iomap_apply(inode, offset, length, IOMAP_WRITE,
  333. ops, page, iomap_page_mkwrite_actor);
  334. if (unlikely(ret <= 0))
  335. goto out_unlock;
  336. offset += ret;
  337. length -= ret;
  338. }
  339. set_page_dirty(page);
  340. wait_for_stable_page(page);
  341. return 0;
  342. out_unlock:
  343. unlock_page(page);
  344. return ret;
  345. }
  346. EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
  347. struct fiemap_ctx {
  348. struct fiemap_extent_info *fi;
  349. struct iomap prev;
  350. };
  351. static int iomap_to_fiemap(struct fiemap_extent_info *fi,
  352. struct iomap *iomap, u32 flags)
  353. {
  354. switch (iomap->type) {
  355. case IOMAP_HOLE:
  356. /* skip holes */
  357. return 0;
  358. case IOMAP_DELALLOC:
  359. flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
  360. break;
  361. case IOMAP_UNWRITTEN:
  362. flags |= FIEMAP_EXTENT_UNWRITTEN;
  363. break;
  364. case IOMAP_MAPPED:
  365. break;
  366. }
  367. return fiemap_fill_next_extent(fi, iomap->offset,
  368. iomap->blkno != IOMAP_NULL_BLOCK ? iomap->blkno << 9: 0,
  369. iomap->length, flags | FIEMAP_EXTENT_MERGED);
  370. }
  371. static loff_t
  372. iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  373. struct iomap *iomap)
  374. {
  375. struct fiemap_ctx *ctx = data;
  376. loff_t ret = length;
  377. if (iomap->type == IOMAP_HOLE)
  378. return length;
  379. ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
  380. ctx->prev = *iomap;
  381. switch (ret) {
  382. case 0: /* success */
  383. return length;
  384. case 1: /* extent array full */
  385. return 0;
  386. default:
  387. return ret;
  388. }
  389. }
  390. int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
  391. loff_t start, loff_t len, struct iomap_ops *ops)
  392. {
  393. struct fiemap_ctx ctx;
  394. loff_t ret;
  395. memset(&ctx, 0, sizeof(ctx));
  396. ctx.fi = fi;
  397. ctx.prev.type = IOMAP_HOLE;
  398. ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
  399. if (ret)
  400. return ret;
  401. if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
  402. ret = filemap_write_and_wait(inode->i_mapping);
  403. if (ret)
  404. return ret;
  405. }
  406. while (len > 0) {
  407. ret = iomap_apply(inode, start, len, 0, ops, &ctx,
  408. iomap_fiemap_actor);
  409. /* inode with no (attribute) mapping will give ENOENT */
  410. if (ret == -ENOENT)
  411. break;
  412. if (ret < 0)
  413. return ret;
  414. if (ret == 0)
  415. break;
  416. start += ret;
  417. len -= ret;
  418. }
  419. if (ctx.prev.type != IOMAP_HOLE) {
  420. ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
  421. if (ret < 0)
  422. return ret;
  423. }
  424. return 0;
  425. }
  426. EXPORT_SYMBOL_GPL(iomap_fiemap);