blob: aab070df4a21758ae407bcefc6ac4476423d7674 [file] [log] [blame]
Darrick J. Wong5157fb82019-07-15 08:50:58 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2016-2018 Christoph Hellwig.
4 */
5#include <linux/module.h>
6#include <linux/compiler.h>
7#include <linux/fs.h>
8#include <linux/iomap.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +02009#include <linux/fiemap.h>
Darrick J. Wong5157fb82019-07-15 08:50:58 -070010
Darrick J. Wong5157fb82019-07-15 08:50:58 -070011struct fiemap_ctx {
12 struct fiemap_extent_info *fi;
13 struct iomap prev;
14};
15
16static int iomap_to_fiemap(struct fiemap_extent_info *fi,
17 struct iomap *iomap, u32 flags)
18{
19 switch (iomap->type) {
20 case IOMAP_HOLE:
21 /* skip holes */
22 return 0;
23 case IOMAP_DELALLOC:
24 flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
25 break;
26 case IOMAP_MAPPED:
27 break;
28 case IOMAP_UNWRITTEN:
29 flags |= FIEMAP_EXTENT_UNWRITTEN;
30 break;
31 case IOMAP_INLINE:
32 flags |= FIEMAP_EXTENT_DATA_INLINE;
33 break;
34 }
35
36 if (iomap->flags & IOMAP_F_MERGED)
37 flags |= FIEMAP_EXTENT_MERGED;
38 if (iomap->flags & IOMAP_F_SHARED)
39 flags |= FIEMAP_EXTENT_SHARED;
40
41 return fiemap_fill_next_extent(fi, iomap->offset,
42 iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
43 iomap->length, flags);
44}
45
46static loff_t
47iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -070048 struct iomap *iomap, struct iomap *srcmap)
Darrick J. Wong5157fb82019-07-15 08:50:58 -070049{
50 struct fiemap_ctx *ctx = data;
51 loff_t ret = length;
52
53 if (iomap->type == IOMAP_HOLE)
54 return length;
55
56 ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
57 ctx->prev = *iomap;
58 switch (ret) {
59 case 0: /* success */
60 return length;
61 case 1: /* extent array full */
62 return 0;
63 default:
64 return ret;
65 }
66}
67
68int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
Christoph Hellwig27328812020-05-23 09:30:12 +020069 u64 start, u64 len, const struct iomap_ops *ops)
Darrick J. Wong5157fb82019-07-15 08:50:58 -070070{
71 struct fiemap_ctx ctx;
72 loff_t ret;
73
74 memset(&ctx, 0, sizeof(ctx));
75 ctx.fi = fi;
76 ctx.prev.type = IOMAP_HOLE;
77
Christoph Hellwig45dd0522020-05-23 09:30:14 +020078 ret = fiemap_prep(inode, fi, start, &len, 0);
Darrick J. Wong5157fb82019-07-15 08:50:58 -070079 if (ret)
80 return ret;
81
Darrick J. Wong5157fb82019-07-15 08:50:58 -070082 while (len > 0) {
83 ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
84 iomap_fiemap_actor);
85 /* inode with no (attribute) mapping will give ENOENT */
86 if (ret == -ENOENT)
87 break;
88 if (ret < 0)
89 return ret;
90 if (ret == 0)
91 break;
92
93 start += ret;
94 len -= ret;
95 }
96
97 if (ctx.prev.type != IOMAP_HOLE) {
98 ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
99 if (ret < 0)
100 return ret;
101 }
102
103 return 0;
104}
105EXPORT_SYMBOL_GPL(iomap_fiemap);
106
107static loff_t
108iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700109 void *data, struct iomap *iomap, struct iomap *srcmap)
Darrick J. Wong5157fb82019-07-15 08:50:58 -0700110{
111 sector_t *bno = data, addr;
112
113 if (iomap->type == IOMAP_MAPPED) {
114 addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
Ritesh Harjanib75dfde2020-04-30 07:57:46 -0700115 *bno = addr;
Darrick J. Wong5157fb82019-07-15 08:50:58 -0700116 }
117 return 0;
118}
119
120/* legacy ->bmap interface. 0 is the error return (!) */
121sector_t
122iomap_bmap(struct address_space *mapping, sector_t bno,
123 const struct iomap_ops *ops)
124{
125 struct inode *inode = mapping->host;
126 loff_t pos = bno << inode->i_blkbits;
127 unsigned blocksize = i_blocksize(inode);
Darrick J. Wong2b91b282019-11-07 07:28:18 -0800128 int ret;
Darrick J. Wong5157fb82019-07-15 08:50:58 -0700129
130 if (filemap_write_and_wait(mapping))
131 return 0;
132
133 bno = 0;
Darrick J. Wong2b91b282019-11-07 07:28:18 -0800134 ret = iomap_apply(inode, pos, blocksize, 0, ops, &bno,
135 iomap_bmap_actor);
136 if (ret)
137 return 0;
Darrick J. Wong5157fb82019-07-15 08:50:58 -0700138 return bno;
139}
140EXPORT_SYMBOL_GPL(iomap_bmap);