Christoph Hellwig | 73ce6ab | 2019-04-28 08:34:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2010 Red Hat, Inc. |
Christoph Hellwig | 72b4daa | 2018-06-19 15:10:57 -0700 | [diff] [blame] | 4 | * Copyright (c) 2016-2018 Christoph Hellwig. |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/compiler.h> |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/iomap.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 10 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 11 | /* |
| 12 | * Execute a iomap write on a segment of the mapping that spans a |
| 13 | * contiguous range of pages that have identical block mapping state. |
| 14 | * |
| 15 | * This avoids the need to map pages individually, do individual allocations |
| 16 | * for each page and most importantly avoid the need for filesystem specific |
| 17 | * locking per page. Instead, all the operations are amortised over the entire |
| 18 | * range of pages. It is assumed that the filesystems will lock whatever |
| 19 | * resources they require in the iomap_begin call, and release them in the |
| 20 | * iomap_end call. |
| 21 | */ |
Christoph Hellwig | befb503 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 22 | loff_t |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 23 | iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 24 | const struct iomap_ops *ops, void *data, iomap_actor_t actor) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 25 | { |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame^] | 26 | struct iomap iomap = { .type = IOMAP_HOLE }; |
| 27 | struct iomap srcmap = { .type = IOMAP_HOLE }; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 28 | loff_t written = 0, ret; |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame^] | 29 | u64 end; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Need to map a range from start position for length bytes. This can |
| 33 | * span multiple pages - it is only guaranteed to return a range of a |
| 34 | * single type of pages (e.g. all into a hole, all mapped or all |
| 35 | * unwritten). Failure at this point has nothing to undo. |
| 36 | * |
| 37 | * If allocation is required for this range, reserve the space now so |
| 38 | * that the allocation is guaranteed to succeed later on. Once we copy |
| 39 | * the data into the page cache pages, then we cannot fail otherwise we |
| 40 | * expose transient stale data. If the reserve fails, we can safely |
| 41 | * back out at this point as there is nothing to undo. |
| 42 | */ |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame^] | 43 | ret = ops->iomap_begin(inode, pos, length, flags, &iomap, &srcmap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 44 | if (ret) |
| 45 | return ret; |
| 46 | if (WARN_ON(iomap.offset > pos)) |
| 47 | return -EIO; |
Darrick J. Wong | 0c6dda7 | 2018-01-26 11:11:20 -0800 | [diff] [blame] | 48 | if (WARN_ON(iomap.length == 0)) |
| 49 | return -EIO; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Cut down the length to the one actually provided by the filesystem, |
| 53 | * as it might not be able to give us the whole size that we requested. |
| 54 | */ |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame^] | 55 | end = iomap.offset + iomap.length; |
| 56 | if (srcmap.type != IOMAP_HOLE) |
| 57 | end = min(end, srcmap.offset + srcmap.length); |
| 58 | if (pos + length > end) |
| 59 | length = end - pos; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 60 | |
| 61 | /* |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame^] | 62 | * Now that we have guaranteed that the space allocation will succeed, |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 63 | * we can do the copy-in page by page without having to worry about |
| 64 | * failures exposing transient data. |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame^] | 65 | * |
| 66 | * To support COW operations, we read in data for partially blocks from |
| 67 | * the srcmap if the file system filled it in. In that case we the |
| 68 | * length needs to be limited to the earlier of the ends of the iomaps. |
| 69 | * If the file system did not provide a srcmap we pass in the normal |
| 70 | * iomap into the actors so that they don't need to have special |
| 71 | * handling for the two cases. |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 72 | */ |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame^] | 73 | written = actor(inode, pos, length, data, &iomap, |
| 74 | srcmap.type != IOMAP_HOLE ? &srcmap : &iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Now the data has been copied, commit the range we've copied. This |
| 78 | * should not fail unless the filesystem has had a fatal error. |
| 79 | */ |
Christoph Hellwig | f20ac7a | 2016-08-17 08:42:34 +1000 | [diff] [blame] | 80 | if (ops->iomap_end) { |
| 81 | ret = ops->iomap_end(inode, pos, length, |
| 82 | written > 0 ? written : 0, |
| 83 | flags, &iomap); |
| 84 | } |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 85 | |
| 86 | return written ? written : ret; |
| 87 | } |