blob: 0a807bbb2b4af7f6688594e46826aadad6dcc59a [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,
69 loff_t start, loff_t len, const struct iomap_ops *ops)
70{
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
78 ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
79 if (ret)
80 return ret;
81
82 if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
83 ret = filemap_write_and_wait(inode->i_mapping);
84 if (ret)
85 return ret;
86 }
87
88 while (len > 0) {
89 ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
90 iomap_fiemap_actor);
91 /* inode with no (attribute) mapping will give ENOENT */
92 if (ret == -ENOENT)
93 break;
94 if (ret < 0)
95 return ret;
96 if (ret == 0)
97 break;
98
99 start += ret;
100 len -= ret;
101 }
102
103 if (ctx.prev.type != IOMAP_HOLE) {
104 ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
105 if (ret < 0)
106 return ret;
107 }
108
109 return 0;
110}
111EXPORT_SYMBOL_GPL(iomap_fiemap);
112
113static loff_t
114iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700115 void *data, struct iomap *iomap, struct iomap *srcmap)
Darrick J. Wong5157fb82019-07-15 08:50:58 -0700116{
117 sector_t *bno = data, addr;
118
119 if (iomap->type == IOMAP_MAPPED) {
120 addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
Ritesh Harjanib75dfde2020-04-30 07:57:46 -0700121 *bno = addr;
Darrick J. Wong5157fb82019-07-15 08:50:58 -0700122 }
123 return 0;
124}
125
126/* legacy ->bmap interface. 0 is the error return (!) */
127sector_t
128iomap_bmap(struct address_space *mapping, sector_t bno,
129 const struct iomap_ops *ops)
130{
131 struct inode *inode = mapping->host;
132 loff_t pos = bno << inode->i_blkbits;
133 unsigned blocksize = i_blocksize(inode);
Darrick J. Wong2b91b282019-11-07 07:28:18 -0800134 int ret;
Darrick J. Wong5157fb82019-07-15 08:50:58 -0700135
136 if (filemap_write_and_wait(mapping))
137 return 0;
138
139 bno = 0;
Darrick J. Wong2b91b282019-11-07 07:28:18 -0800140 ret = iomap_apply(inode, pos, blocksize, 0, ops, &bno,
141 iomap_bmap_actor);
142 if (ret)
143 return 0;
Darrick J. Wong5157fb82019-07-15 08:50:58 -0700144 return bno;
145}
146EXPORT_SYMBOL_GPL(iomap_bmap);