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>
11 struct fiemap_extent_info *fi;
15 static int iomap_to_fiemap(struct fiemap_extent_info *fi,
16 struct iomap *iomap, u32 flags)
18 switch (iomap->type) {
23 flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
28 flags |= FIEMAP_EXTENT_UNWRITTEN;
31 flags |= FIEMAP_EXTENT_DATA_INLINE;
35 if (iomap->flags & IOMAP_F_MERGED)
36 flags |= FIEMAP_EXTENT_MERGED;
37 if (iomap->flags & IOMAP_F_SHARED)
38 flags |= FIEMAP_EXTENT_SHARED;
40 return fiemap_fill_next_extent(fi, iomap->offset,
41 iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
42 iomap->length, flags);
46 iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
49 struct fiemap_ctx *ctx = data;
52 if (iomap->type == IOMAP_HOLE)
55 ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
60 case 1: /* extent array full */
67 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
68 loff_t start, loff_t len, const struct iomap_ops *ops)
70 struct fiemap_ctx ctx;
73 memset(&ctx, 0, sizeof(ctx));
75 ctx.prev.type = IOMAP_HOLE;
77 ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
81 if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
82 ret = filemap_write_and_wait(inode->i_mapping);
88 ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
90 /* inode with no (attribute) mapping will give ENOENT */
102 if (ctx.prev.type != IOMAP_HOLE) {
103 ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
110 EXPORT_SYMBOL_GPL(iomap_fiemap);
113 iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
114 void *data, struct iomap *iomap)
116 sector_t *bno = data, addr;
118 if (iomap->type == IOMAP_MAPPED) {
119 addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
121 WARN(1, "would truncate bmap result\n");
128 /* legacy ->bmap interface. 0 is the error return (!) */
130 iomap_bmap(struct address_space *mapping, sector_t bno,
131 const struct iomap_ops *ops)
133 struct inode *inode = mapping->host;
134 loff_t pos = bno << inode->i_blkbits;
135 unsigned blocksize = i_blocksize(inode);
137 if (filemap_write_and_wait(mapping))
141 iomap_apply(inode, pos, blocksize, 0, ops, &bno, iomap_bmap_actor);
144 EXPORT_SYMBOL_GPL(iomap_bmap);