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