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 /* 10 * Release the iter folio batch. Note that the iomap flag is meant to control 11 * the I/O path for the mapping and may not be set in error situations. 12 */ 13 static inline void iomap_iter_clean_fbatch(const struct iomap_iter *iter, 14 struct iomap *iomap) 15 { 16 if (!iter->fbatch) 17 return; 18 iomap->flags &= ~IOMAP_F_FOLIO_BATCH; 19 if (folio_batch_count(iter->fbatch)) { 20 folio_batch_release(iter->fbatch); 21 folio_batch_reinit(iter->fbatch); 22 } 23 } 24 25 /* Advance the current iterator position and decrement the remaining length */ 26 int iomap_iter_advance(struct iomap_iter *iter, u64 count) 27 { 28 if (WARN_ON_ONCE(count > iomap_length(iter))) 29 return -EIO; 30 iter->pos += count; 31 iter->len -= count; 32 return 0; 33 } 34 35 static inline void iomap_iter_done(struct iomap_iter *iter) 36 { 37 WARN_ON_ONCE(iter->iomap.offset > iter->pos); 38 WARN_ON_ONCE(iter->iomap.length == 0); 39 WARN_ON_ONCE(iter->iomap.offset + iter->iomap.length <= iter->pos); 40 WARN_ON_ONCE(iter->iomap.flags & IOMAP_F_STALE); 41 42 iter->iter_start_pos = iter->pos; 43 44 trace_iomap_iter_dstmap(iter->inode, &iter->iomap); 45 if (iter->srcmap.type != IOMAP_HOLE) 46 trace_iomap_iter_srcmap(iter->inode, &iter->srcmap); 47 } 48 49 /** 50 * iomap_iter_continue - decide whether iteration should continue 51 * @iter: iteration structure 52 * @iomap: the mapping that was just processed 53 * @srcmap: the source mapping that was just processed 54 * @ret: return value from the previous mapping's ->end() callback 55 * 56 * Helper normally called via iomap_iter_next(). Called after the previous 57 * mapping has been finished to determine whether there is more of the file 58 * range left to process. 59 * 60 * Returns 1 if there is more work to do, in which case @iomap and @srcmap are 61 * cleared so the caller can produce the next mapping; zero if the range is 62 * fully consumed; or a negative errno on error. 63 */ 64 int iomap_iter_continue(const struct iomap_iter *iter, struct iomap *iomap, 65 struct iomap *srcmap, int ret) 66 { 67 const bool stale = iomap->flags & IOMAP_F_STALE; 68 const ssize_t advanced = iter->pos - iter->iter_start_pos; 69 70 if (ret < 0 && !advanced) 71 return ret; 72 73 /* 74 * Use iter->len to determine whether to continue onto the next mapping. 75 * Explicitly terminate on error status or if the current iter has not 76 * advanced at all (i.e. no work was done for some reason) unless the 77 * mapping has been marked stale and needs to be reprocessed. 78 */ 79 if (WARN_ON_ONCE(iter->status > 0)) 80 /* detect old return semantics where this would advance */ 81 ret = -EIO; 82 else if (iter->status < 0) 83 ret = iter->status; 84 else if (iter->len == 0 || (!advanced && !stale)) 85 ret = 0; 86 else 87 ret = 1; 88 89 iomap_iter_clean_fbatch(iter, iomap); 90 91 if (ret <= 0) 92 return ret; 93 94 memset(iomap, 0, sizeof(*iomap)); 95 memset(srcmap, 0, sizeof(*srcmap)); 96 97 return ret; 98 } 99 EXPORT_SYMBOL_GPL(iomap_iter_continue); 100 101 /** 102 * iomap_iter - iterate over ranges in a file 103 * @iter: iteration structure 104 * @next: the iomap iteration function for the filesystem 105 * 106 * Iterate over filesystem-provided space mappings for the provided file range. 107 * 108 * This function handles cleanup of resources acquired for iteration when the 109 * filesystem indicates there are no more space mappings, which means that this 110 * function must be called in a loop that continues as long it returns a 111 * positive value. If 0 or a negative value is returned, the caller must not 112 * return to the loop body. Within a loop body, there are two ways to break out 113 * of the loop body: leave @iter.status unchanged, or set it to a negative 114 * errno. 115 */ 116 int iomap_iter(struct iomap_iter *iter, iomap_iter_next_fn next) 117 { 118 int ret; 119 120 trace_iomap_iter(iter, next, _RET_IP_); 121 122 ret = next(iter, &iter->iomap, &iter->srcmap); 123 iter->status = 0; 124 if (ret > 0) 125 iomap_iter_done(iter); 126 else if (ret < 0) 127 iomap_iter_clean_fbatch(iter, &iter->iomap); 128 129 return ret; 130 } 131