blob: a845c012b50c675470789b6041d64c7fc04073e7 [file] [log] [blame]
Darrick J. Wong56a17892019-07-15 08:50:58 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2017 Red Hat, Inc.
Christoph Hellwig40670d12021-08-10 18:33:11 -07004 * Copyright (c) 2018-2021 Christoph Hellwig.
Darrick J. Wong56a17892019-07-15 08:50:58 -07005 */
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 Hellwig40670d12021-08-10 18:33:11 -070013static loff_t iomap_seek_hole_iter(const struct iomap_iter *iter,
14 loff_t *hole_pos)
Darrick J. Wong56a17892019-07-15 08:50:58 -070015{
Christoph Hellwig40670d12021-08-10 18:33:11 -070016 loff_t length = iomap_length(iter);
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -080017
Christoph Hellwig40670d12021-08-10 18:33:11 -070018 switch (iter->iomap.type) {
Darrick J. Wong56a17892019-07-15 08:50:58 -070019 case IOMAP_UNWRITTEN:
Christoph Hellwig40670d12021-08-10 18:33:11 -070020 *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. Wong56a17892019-07-15 08:50:58 -070023 return length;
Christoph Hellwig40670d12021-08-10 18:33:11 -070024 return 0;
Darrick J. Wong56a17892019-07-15 08:50:58 -070025 case IOMAP_HOLE:
Christoph Hellwig40670d12021-08-10 18:33:11 -070026 *hole_pos = iter->pos;
Darrick J. Wong56a17892019-07-15 08:50:58 -070027 return 0;
28 default:
29 return length;
30 }
31}
32
33loff_t
Christoph Hellwig40670d12021-08-10 18:33:11 -070034iomap_seek_hole(struct inode *inode, loff_t pos, const struct iomap_ops *ops)
Darrick J. Wong56a17892019-07-15 08:50:58 -070035{
36 loff_t size = i_size_read(inode);
Christoph Hellwig40670d12021-08-10 18:33:11 -070037 struct iomap_iter iter = {
38 .inode = inode,
39 .pos = pos,
40 .flags = IOMAP_REPORT,
41 };
42 int ret;
Darrick J. Wong56a17892019-07-15 08:50:58 -070043
44 /* Nothing to be found before or beyond the end of the file. */
Christoph Hellwig40670d12021-08-10 18:33:11 -070045 if (pos < 0 || pos >= size)
Darrick J. Wong56a17892019-07-15 08:50:58 -070046 return -ENXIO;
47
Christoph Hellwig40670d12021-08-10 18:33:11 -070048 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. Wong56a17892019-07-15 08:50:58 -070056}
57EXPORT_SYMBOL_GPL(iomap_seek_hole);
58
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070059static loff_t iomap_seek_data_iter(const struct iomap_iter *iter,
60 loff_t *hole_pos)
Darrick J. Wong56a17892019-07-15 08:50:58 -070061{
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070062 loff_t length = iomap_length(iter);
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -080063
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070064 switch (iter->iomap.type) {
Darrick J. Wong56a17892019-07-15 08:50:58 -070065 case IOMAP_HOLE:
66 return length;
67 case IOMAP_UNWRITTEN:
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070068 *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. Wong56a17892019-07-15 08:50:58 -070071 return length;
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070072 return 0;
Darrick J. Wong56a17892019-07-15 08:50:58 -070073 default:
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070074 *hole_pos = iter->pos;
Darrick J. Wong56a17892019-07-15 08:50:58 -070075 return 0;
76 }
77}
78
79loff_t
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070080iomap_seek_data(struct inode *inode, loff_t pos, const struct iomap_ops *ops)
Darrick J. Wong56a17892019-07-15 08:50:58 -070081{
82 loff_t size = i_size_read(inode);
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070083 struct iomap_iter iter = {
84 .inode = inode,
85 .pos = pos,
86 .flags = IOMAP_REPORT,
87 };
88 int ret;
Darrick J. Wong56a17892019-07-15 08:50:58 -070089
90 /* Nothing to be found before or beyond the end of the file. */
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070091 if (pos < 0 || pos >= size)
Darrick J. Wong56a17892019-07-15 08:50:58 -070092 return -ENXIO;
93
Christoph Hellwigc4740bf2021-08-10 18:33:12 -070094 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 Hellwig3ac1d422021-07-15 09:58:04 -0700101 /* We've reached the end of the file without finding data */
102 return -ENXIO;
Darrick J. Wong56a17892019-07-15 08:50:58 -0700103}
104EXPORT_SYMBOL_GPL(iomap_seek_data);