1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016-2018 Christoph Hellwig.
5 #include <linux/module.h>
6 #include <linux/compiler.h>
8 #include <linux/iomap.h>
9 #include <linux/fiemap.h>
12 struct fiemap_extent_info *fi;
16 static int iomap_to_fiemap(struct fiemap_extent_info *fi,
17 struct iomap *iomap, u32 flags)
19 switch (iomap->type) {
24 flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
29 flags |= FIEMAP_EXTENT_UNWRITTEN;
32 flags |= FIEMAP_EXTENT_DATA_INLINE;
36 if (iomap->flags & IOMAP_F_MERGED)
37 flags |= FIEMAP_EXTENT_MERGED;
38 if (iomap->flags & IOMAP_F_SHARED)
39 flags |= FIEMAP_EXTENT_SHARED;
41 return fiemap_fill_next_extent(fi, iomap->offset,
42 iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
43 iomap->length, flags);
47 iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
48 struct iomap *iomap, struct iomap *srcmap)
50 struct fiemap_ctx *ctx = data;
53 if (iomap->type == IOMAP_HOLE)
56 ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
61 case 1: /* extent array full */
68 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
69 u64 start, u64 len, const struct iomap_ops *ops)
71 struct fiemap_ctx ctx;
74 memset(&ctx, 0, sizeof(ctx));
76 ctx.prev.type = IOMAP_HOLE;
78 ret = fiemap_prep(inode, fi, start, &len, 0);
83 ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
85 /* inode with no (attribute) mapping will give ENOENT */
97 if (ctx.prev.type != IOMAP_HOLE) {
98 ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
105 EXPORT_SYMBOL_GPL(iomap_fiemap);
108 iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
109 void *data, struct iomap *iomap, struct iomap *srcmap)
111 sector_t *bno = data, addr;
113 if (iomap->type == IOMAP_MAPPED) {
114 addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
120 /* legacy ->bmap interface. 0 is the error return (!) */
122 iomap_bmap(struct address_space *mapping, sector_t bno,
123 const struct iomap_ops *ops)
125 struct inode *inode = mapping->host;
126 loff_t pos = bno << inode->i_blkbits;
127 unsigned blocksize = i_blocksize(inode);
130 if (filemap_write_and_wait(mapping))
134 ret = iomap_apply(inode, pos, blocksize, 0, ops, &bno,
140 EXPORT_SYMBOL_GPL(iomap_bmap);