Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2017 Red Hat, Inc. |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 4 | * Copyright (c) 2018-2021 Christoph Hellwig. |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/compiler.h> |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/iomap.h> |
| 10 | #include <linux/pagemap.h> |
| 11 | #include <linux/pagevec.h> |
| 12 | |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 13 | static loff_t iomap_seek_hole_iter(const struct iomap_iter *iter, |
| 14 | loff_t *hole_pos) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 15 | { |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 16 | loff_t length = iomap_length(iter); |
Matthew Wilcox (Oracle) | 54fa39a | 2021-02-25 17:15:52 -0800 | [diff] [blame] | 17 | |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 18 | switch (iter->iomap.type) { |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 19 | case IOMAP_UNWRITTEN: |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 20 | *hole_pos = mapping_seek_hole_data(iter->inode->i_mapping, |
| 21 | iter->pos, iter->pos + length, SEEK_HOLE); |
| 22 | if (*hole_pos == iter->pos + length) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 23 | return length; |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 24 | return 0; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 25 | case IOMAP_HOLE: |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 26 | *hole_pos = iter->pos; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 27 | return 0; |
| 28 | default: |
| 29 | return length; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | loff_t |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 34 | iomap_seek_hole(struct inode *inode, loff_t pos, const struct iomap_ops *ops) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 35 | { |
| 36 | loff_t size = i_size_read(inode); |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 37 | struct iomap_iter iter = { |
| 38 | .inode = inode, |
| 39 | .pos = pos, |
| 40 | .flags = IOMAP_REPORT, |
| 41 | }; |
| 42 | int ret; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 43 | |
| 44 | /* Nothing to be found before or beyond the end of the file. */ |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 45 | if (pos < 0 || pos >= size) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 46 | return -ENXIO; |
| 47 | |
Christoph Hellwig | 40670d1 | 2021-08-10 18:33:11 -0700 | [diff] [blame] | 48 | iter.len = size - pos; |
| 49 | while ((ret = iomap_iter(&iter, ops)) > 0) |
| 50 | iter.processed = iomap_seek_hole_iter(&iter, &pos); |
| 51 | if (ret < 0) |
| 52 | return ret; |
| 53 | if (iter.len) /* found hole before EOF */ |
| 54 | return pos; |
| 55 | return size; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 56 | } |
| 57 | EXPORT_SYMBOL_GPL(iomap_seek_hole); |
| 58 | |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 59 | static loff_t iomap_seek_data_iter(const struct iomap_iter *iter, |
| 60 | loff_t *hole_pos) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 61 | { |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 62 | loff_t length = iomap_length(iter); |
Matthew Wilcox (Oracle) | 54fa39a | 2021-02-25 17:15:52 -0800 | [diff] [blame] | 63 | |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 64 | switch (iter->iomap.type) { |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 65 | case IOMAP_HOLE: |
| 66 | return length; |
| 67 | case IOMAP_UNWRITTEN: |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 68 | *hole_pos = mapping_seek_hole_data(iter->inode->i_mapping, |
| 69 | iter->pos, iter->pos + length, SEEK_DATA); |
| 70 | if (*hole_pos < 0) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 71 | return length; |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 72 | return 0; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 73 | default: |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 74 | *hole_pos = iter->pos; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | loff_t |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 80 | iomap_seek_data(struct inode *inode, loff_t pos, const struct iomap_ops *ops) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 81 | { |
| 82 | loff_t size = i_size_read(inode); |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 83 | struct iomap_iter iter = { |
| 84 | .inode = inode, |
| 85 | .pos = pos, |
| 86 | .flags = IOMAP_REPORT, |
| 87 | }; |
| 88 | int ret; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 89 | |
| 90 | /* Nothing to be found before or beyond the end of the file. */ |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 91 | if (pos < 0 || pos >= size) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 92 | return -ENXIO; |
| 93 | |
Christoph Hellwig | c4740bf | 2021-08-10 18:33:12 -0700 | [diff] [blame] | 94 | iter.len = size - pos; |
| 95 | while ((ret = iomap_iter(&iter, ops)) > 0) |
| 96 | iter.processed = iomap_seek_data_iter(&iter, &pos); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | if (iter.len) /* found data before EOF */ |
| 100 | return pos; |
Christoph Hellwig | 3ac1d42 | 2021-07-15 09:58:04 -0700 | [diff] [blame] | 101 | /* We've reached the end of the file without finding data */ |
| 102 | return -ENXIO; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 103 | } |
| 104 | EXPORT_SYMBOL_GPL(iomap_seek_data); |