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. |
| 4 | * Copyright (c) 2018 Christoph Hellwig. |
| 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 | |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 13 | static loff_t |
Matthew Wilcox (Oracle) | 54fa39a | 2021-02-25 17:15:52 -0800 | [diff] [blame] | 14 | iomap_seek_hole_actor(struct inode *inode, loff_t start, loff_t length, |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 15 | void *data, struct iomap *iomap, struct iomap *srcmap) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 16 | { |
Matthew Wilcox (Oracle) | 54fa39a | 2021-02-25 17:15:52 -0800 | [diff] [blame] | 17 | loff_t offset = start; |
| 18 | |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 19 | switch (iomap->type) { |
| 20 | case IOMAP_UNWRITTEN: |
Matthew Wilcox (Oracle) | 54fa39a | 2021-02-25 17:15:52 -0800 | [diff] [blame] | 21 | offset = mapping_seek_hole_data(inode->i_mapping, start, |
| 22 | start + length, SEEK_HOLE); |
| 23 | if (offset == start + length) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 24 | return length; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 25 | fallthrough; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 26 | case IOMAP_HOLE: |
| 27 | *(loff_t *)data = offset; |
| 28 | return 0; |
| 29 | default: |
| 30 | return length; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | loff_t |
| 35 | iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops) |
| 36 | { |
| 37 | loff_t size = i_size_read(inode); |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 38 | loff_t ret; |
| 39 | |
| 40 | /* Nothing to be found before or beyond the end of the file. */ |
| 41 | if (offset < 0 || offset >= size) |
| 42 | return -ENXIO; |
| 43 | |
Christoph Hellwig | 49694d1 | 2021-07-15 09:58:04 -0700 | [diff] [blame] | 44 | while (offset < size) { |
| 45 | ret = iomap_apply(inode, offset, size - offset, IOMAP_REPORT, |
| 46 | ops, &offset, iomap_seek_hole_actor); |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 47 | if (ret < 0) |
| 48 | return ret; |
| 49 | if (ret == 0) |
| 50 | break; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 51 | offset += ret; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | return offset; |
| 55 | } |
| 56 | EXPORT_SYMBOL_GPL(iomap_seek_hole); |
| 57 | |
| 58 | static loff_t |
Matthew Wilcox (Oracle) | 54fa39a | 2021-02-25 17:15:52 -0800 | [diff] [blame] | 59 | iomap_seek_data_actor(struct inode *inode, loff_t start, loff_t length, |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 60 | void *data, struct iomap *iomap, struct iomap *srcmap) |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 61 | { |
Matthew Wilcox (Oracle) | 54fa39a | 2021-02-25 17:15:52 -0800 | [diff] [blame] | 62 | loff_t offset = start; |
| 63 | |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 64 | switch (iomap->type) { |
| 65 | case IOMAP_HOLE: |
| 66 | return length; |
| 67 | case IOMAP_UNWRITTEN: |
Matthew Wilcox (Oracle) | 54fa39a | 2021-02-25 17:15:52 -0800 | [diff] [blame] | 68 | offset = mapping_seek_hole_data(inode->i_mapping, start, |
| 69 | start + length, SEEK_DATA); |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 70 | if (offset < 0) |
| 71 | return length; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 72 | fallthrough; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 73 | default: |
| 74 | *(loff_t *)data = offset; |
| 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | loff_t |
| 80 | iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops) |
| 81 | { |
| 82 | loff_t size = i_size_read(inode); |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 83 | loff_t ret; |
| 84 | |
| 85 | /* Nothing to be found before or beyond the end of the file. */ |
| 86 | if (offset < 0 || offset >= size) |
| 87 | return -ENXIO; |
| 88 | |
Christoph Hellwig | 3ac1d42 | 2021-07-15 09:58:04 -0700 | [diff] [blame] | 89 | while (offset < size) { |
| 90 | ret = iomap_apply(inode, offset, size - offset, IOMAP_REPORT, |
| 91 | ops, &offset, iomap_seek_data_actor); |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 92 | if (ret < 0) |
| 93 | return ret; |
| 94 | if (ret == 0) |
Christoph Hellwig | 3ac1d42 | 2021-07-15 09:58:04 -0700 | [diff] [blame] | 95 | return offset; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 96 | offset += ret; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Christoph Hellwig | 3ac1d42 | 2021-07-15 09:58:04 -0700 | [diff] [blame] | 99 | /* We've reached the end of the file without finding data */ |
| 100 | return -ENXIO; |
Darrick J. Wong | 56a1789 | 2019-07-15 08:50:58 -0700 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL_GPL(iomap_seek_data); |