aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/fs/iomap/iter.c
blob: 8e0746ad80bd11ffeffed3822679a255bcb02e5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2010 Red Hat, Inc.
 * Copyright (c) 2016-2021 Christoph Hellwig.
 */
#include <linux/fs.h>
#include <linux/iomap.h>
#include "trace.h"

static inline void iomap_iter_reset_iomap(struct iomap_iter *iter)
{
	iter->processed = 0;
	memset(&iter->iomap, 0, sizeof(iter->iomap));
	memset(&iter->srcmap, 0, sizeof(iter->srcmap));
}

/*
 * Advance to the next range we need to map.
 *
 * If the iomap is marked IOMAP_F_STALE, it means the existing map was not fully
 * processed - it was aborted because the extent the iomap spanned may have been
 * changed during the operation. In this case, the iteration behaviour is to
 * remap the unprocessed range of the iter, and that means we may need to remap
 * even when we've made no progress (i.e. count = 0). Hence the "finished
 * iterating" case needs to distinguish between (count = 0) meaning we are done
 * and (count = 0 && stale) meaning we need to remap the entire remaining range.
 */
static inline int iomap_iter_advance(struct iomap_iter *iter, s64 count)
{
	if (WARN_ON_ONCE(count > iomap_length(iter)))
		return -EIO;
	iter->pos += count;
	iter->len -= count;
	return 0;
}

static inline void iomap_iter_done(struct iomap_iter *iter)
{
	WARN_ON_ONCE(iter->iomap.offset > iter->pos);
	WARN_ON_ONCE(iter->iomap.length == 0);
	WARN_ON_ONCE(iter->iomap.offset + iter->iomap.length <= iter->pos);
	WARN_ON_ONCE(iter->iomap.flags & IOMAP_F_STALE);

	trace_iomap_iter_dstmap(iter->inode, &iter->iomap);
	if (iter->srcmap.type != IOMAP_HOLE)
		trace_iomap_iter_srcmap(iter->inode, &iter->srcmap);
}

/**
 * iomap_iter - iterate over a ranges in a file
 * @iter: iteration structue
 * @ops: iomap ops provided by the file system
 *
 * Iterate over filesystem-provided space mappings for the provided file range.
 *
 * This function handles cleanup of resources acquired for iteration when the
 * filesystem indicates there are no more space mappings, which means that this
 * function must be called in a loop that continues as long it returns a
 * positive value.  If 0 or a negative value is returned, the caller must not
 * return to the loop body.  Within a loop body, there are two ways to break out
 * of the loop body:  leave @iter.processed unchanged, or set it to a negative
 * errno.
 */
int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops)
{
	bool stale = iter->iomap.flags & IOMAP_F_STALE;
	s64 processed;
	int ret;

	trace_iomap_iter(iter, ops, _RET_IP_);

	if (!iter->iomap.length)
		goto begin;

	if (ops->iomap_end) {
		ret = ops->iomap_end(iter->inode, iter->pos, iomap_length(iter),
				iter->processed > 0 ? iter->processed : 0,
				iter->flags, &iter->iomap);
		if (ret < 0 && !iter->processed)
			return ret;
	}

	processed = iter->processed;
	if (processed < 0) {
		iomap_iter_reset_iomap(iter);
		return processed;
	}

	/*
	 * Advance the iter and clear state from the previous iteration. Use
	 * iter->len to determine whether to continue onto the next mapping.
	 * Explicitly terminate in the case where the current iter has not
	 * advanced at all (i.e. no work was done for some reason) unless the
	 * mapping has been marked stale and needs to be reprocessed.
	 */
	ret = iomap_iter_advance(iter, processed);
	if (!ret && iter->len > 0)
		ret = 1;
	if (ret > 0 && !iter->processed && !stale)
		ret = 0;
	iomap_iter_reset_iomap(iter);
	if (ret <= 0)
		return ret;

begin:
	ret = ops->iomap_begin(iter->inode, iter->pos, iter->len, iter->flags,
			       &iter->iomap, &iter->srcmap);
	if (ret < 0)
		return ret;
	iomap_iter_done(iter);
	return 1;
}