| 1 | // SPDX-License-Identifier: GPL-2.0 | 
|---|
| 2 | /* | 
|---|
| 3 | * Copyright (C) 2010 Red Hat, Inc. | 
|---|
| 4 | * Copyright (c) 2016-2021 Christoph Hellwig. | 
|---|
| 5 | */ | 
|---|
| 6 | #include <linux/iomap.h> | 
|---|
| 7 | #include "trace.h" | 
|---|
| 8 |  | 
|---|
| 9 | static inline void iomap_iter_reset_iomap(struct iomap_iter *iter) | 
|---|
| 10 | { | 
|---|
| 11 | iter->status = 0; | 
|---|
| 12 | memset(s: &iter->iomap, c: 0, n: sizeof(iter->iomap)); | 
|---|
| 13 | memset(s: &iter->srcmap, c: 0, n: sizeof(iter->srcmap)); | 
|---|
| 14 | } | 
|---|
| 15 |  | 
|---|
| 16 | /* | 
|---|
| 17 | * Advance the current iterator position and output the length remaining for the | 
|---|
| 18 | * current mapping. | 
|---|
| 19 | */ | 
|---|
| 20 | int iomap_iter_advance(struct iomap_iter *iter, u64 *count) | 
|---|
| 21 | { | 
|---|
| 22 | if (WARN_ON_ONCE(*count > iomap_length(iter))) | 
|---|
| 23 | return -EIO; | 
|---|
| 24 | iter->pos += *count; | 
|---|
| 25 | iter->len -= *count; | 
|---|
| 26 | *count = iomap_length(iter); | 
|---|
| 27 | return 0; | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | static inline void iomap_iter_done(struct iomap_iter *iter) | 
|---|
| 31 | { | 
|---|
| 32 | WARN_ON_ONCE(iter->iomap.offset > iter->pos); | 
|---|
| 33 | WARN_ON_ONCE(iter->iomap.length == 0); | 
|---|
| 34 | WARN_ON_ONCE(iter->iomap.offset + iter->iomap.length <= iter->pos); | 
|---|
| 35 | WARN_ON_ONCE(iter->iomap.flags & IOMAP_F_STALE); | 
|---|
| 36 |  | 
|---|
| 37 | iter->iter_start_pos = iter->pos; | 
|---|
| 38 |  | 
|---|
| 39 | trace_iomap_iter_dstmap(inode: iter->inode, iomap: &iter->iomap); | 
|---|
| 40 | if (iter->srcmap.type != IOMAP_HOLE) | 
|---|
| 41 | trace_iomap_iter_srcmap(inode: iter->inode, iomap: &iter->srcmap); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | /** | 
|---|
| 45 | * iomap_iter - iterate over a ranges in a file | 
|---|
| 46 | * @iter: iteration structue | 
|---|
| 47 | * @ops: iomap ops provided by the file system | 
|---|
| 48 | * | 
|---|
| 49 | * Iterate over filesystem-provided space mappings for the provided file range. | 
|---|
| 50 | * | 
|---|
| 51 | * This function handles cleanup of resources acquired for iteration when the | 
|---|
| 52 | * filesystem indicates there are no more space mappings, which means that this | 
|---|
| 53 | * function must be called in a loop that continues as long it returns a | 
|---|
| 54 | * positive value.  If 0 or a negative value is returned, the caller must not | 
|---|
| 55 | * return to the loop body.  Within a loop body, there are two ways to break out | 
|---|
| 56 | * of the loop body:  leave @iter.status unchanged, or set it to a negative | 
|---|
| 57 | * errno. | 
|---|
| 58 | */ | 
|---|
| 59 | int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops) | 
|---|
| 60 | { | 
|---|
| 61 | bool stale = iter->iomap.flags & IOMAP_F_STALE; | 
|---|
| 62 | ssize_t advanced; | 
|---|
| 63 | u64 olen; | 
|---|
| 64 | int ret; | 
|---|
| 65 |  | 
|---|
| 66 | trace_iomap_iter(iter, ops, _RET_IP_); | 
|---|
| 67 |  | 
|---|
| 68 | if (!iter->iomap.length) | 
|---|
| 69 | goto begin; | 
|---|
| 70 |  | 
|---|
| 71 | /* | 
|---|
| 72 | * Calculate how far the iter was advanced and the original length bytes | 
|---|
| 73 | * for ->iomap_end(). | 
|---|
| 74 | */ | 
|---|
| 75 | advanced = iter->pos - iter->iter_start_pos; | 
|---|
| 76 | olen = iter->len + advanced; | 
|---|
| 77 |  | 
|---|
| 78 | if (ops->iomap_end) { | 
|---|
| 79 | ret = ops->iomap_end(iter->inode, iter->iter_start_pos, | 
|---|
| 80 | iomap_length_trim(iter, pos: iter->iter_start_pos, | 
|---|
| 81 | len: olen), | 
|---|
| 82 | advanced, iter->flags, &iter->iomap); | 
|---|
| 83 | if (ret < 0 && !advanced) | 
|---|
| 84 | return ret; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /* detect old return semantics where this would advance */ | 
|---|
| 88 | if (WARN_ON_ONCE(iter->status > 0)) | 
|---|
| 89 | iter->status = -EIO; | 
|---|
| 90 |  | 
|---|
| 91 | /* | 
|---|
| 92 | * Use iter->len to determine whether to continue onto the next mapping. | 
|---|
| 93 | * Explicitly terminate on error status or if the current iter has not | 
|---|
| 94 | * advanced at all (i.e. no work was done for some reason) unless the | 
|---|
| 95 | * mapping has been marked stale and needs to be reprocessed. | 
|---|
| 96 | */ | 
|---|
| 97 | if (iter->status < 0) | 
|---|
| 98 | ret = iter->status; | 
|---|
| 99 | else if (iter->len == 0 || (!advanced && !stale)) | 
|---|
| 100 | ret = 0; | 
|---|
| 101 | else | 
|---|
| 102 | ret = 1; | 
|---|
| 103 | iomap_iter_reset_iomap(iter); | 
|---|
| 104 | if (ret <= 0) | 
|---|
| 105 | return ret; | 
|---|
| 106 |  | 
|---|
| 107 | begin: | 
|---|
| 108 | ret = ops->iomap_begin(iter->inode, iter->pos, iter->len, iter->flags, | 
|---|
| 109 | &iter->iomap, &iter->srcmap); | 
|---|
| 110 | if (ret < 0) | 
|---|
| 111 | return ret; | 
|---|
| 112 | iomap_iter_done(iter); | 
|---|
| 113 | return 1; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|