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