Skip to content

Commit d505a96

Browse files
committed
gfs2: Further iomap cleanups
In gfs2_iomap_alloc, set the type of newly allocated extents to IOMAP_MAPPED so that iomap_to_bh will set the bh states correctly: otherwise, the bhs would not be marked as mapped, confusing __mpage_writepage. This means that we need to check for the IOMAP_F_NEW flag in fallocate_chunk now. Further clean up gfs2_iomap_get and implement gfs2_stuffed_iomap here directly. For reads beyond the end of the file, return holes instead of failing with -ENOENT so that we can get rid of that special case in gfs2_block_map. Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com> Reviewed-by: Bob Peterson <rpeterso@redhat.com>
1 parent e184fde commit d505a96

2 files changed

Lines changed: 43 additions & 33 deletions

File tree

‎fs/gfs2/bmap.c‎

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
750750
}
751751
} while (iomap->addr == IOMAP_NULL_ADDR);
752752

753+
iomap->type = IOMAP_MAPPED;
753754
iomap->length = (u64)dblks << inode->i_blkbits;
754755
ip->i_height = mp->mp_fheight;
755756
gfs2_add_inode_blocks(&ip->i_inode, alloced);
@@ -759,17 +760,6 @@ static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
759760
return ret;
760761
}
761762

762-
static void gfs2_stuffed_iomap(struct inode *inode, struct iomap *iomap)
763-
{
764-
struct gfs2_inode *ip = GFS2_I(inode);
765-
766-
iomap->addr = (ip->i_no_addr << inode->i_blkbits) +
767-
sizeof(struct gfs2_dinode);
768-
iomap->offset = 0;
769-
iomap->length = i_size_read(inode);
770-
iomap->type = IOMAP_INLINE;
771-
}
772-
773763
#define IOMAP_F_GFS2_BOUNDARY IOMAP_F_PRIVATE
774764

775765
/**
@@ -789,37 +779,61 @@ static int gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
789779
{
790780
struct gfs2_inode *ip = GFS2_I(inode);
791781
struct gfs2_sbd *sdp = GFS2_SB(inode);
782+
loff_t size = i_size_read(inode);
792783
__be64 *ptr;
793784
sector_t lblock;
794785
sector_t lblock_stop;
795786
int ret;
796787
int eob;
797788
u64 len;
798-
struct buffer_head *bh;
789+
struct buffer_head *dibh = NULL, *bh;
799790
u8 height;
800791

801792
if (!length)
802793
return -EINVAL;
803794

795+
down_read(&ip->i_rw_mutex);
796+
797+
ret = gfs2_meta_inode_buffer(ip, &dibh);
798+
if (ret)
799+
goto unlock;
800+
804801
if (gfs2_is_stuffed(ip)) {
805-
if (flags & IOMAP_REPORT) {
806-
if (pos >= i_size_read(inode))
807-
return -ENOENT;
808-
gfs2_stuffed_iomap(inode, iomap);
809-
return 0;
802+
if (flags & IOMAP_WRITE) {
803+
loff_t max_size = gfs2_max_stuffed_size(ip);
804+
805+
if (pos + length > max_size)
806+
goto unstuff;
807+
iomap->length = max_size;
808+
} else {
809+
if (pos >= size) {
810+
if (flags & IOMAP_REPORT) {
811+
ret = -ENOENT;
812+
goto unlock;
813+
} else {
814+
/* report a hole */
815+
iomap->offset = pos;
816+
iomap->length = length;
817+
goto do_alloc;
818+
}
819+
}
820+
iomap->length = size;
810821
}
811-
BUG_ON(!(flags & IOMAP_WRITE));
822+
iomap->addr = (ip->i_no_addr << inode->i_blkbits) +
823+
sizeof(struct gfs2_dinode);
824+
iomap->type = IOMAP_INLINE;
825+
goto out;
812826
}
827+
828+
unstuff:
813829
lblock = pos >> inode->i_blkbits;
814830
iomap->offset = lblock << inode->i_blkbits;
815831
lblock_stop = (pos + length - 1) >> inode->i_blkbits;
816832
len = lblock_stop - lblock + 1;
833+
iomap->length = len << inode->i_blkbits;
817834

818-
down_read(&ip->i_rw_mutex);
819-
820-
ret = gfs2_meta_inode_buffer(ip, &mp->mp_bh[0]);
821-
if (ret)
822-
goto unlock;
835+
get_bh(dibh);
836+
mp->mp_bh[0] = dibh;
823837

824838
height = ip->i_height;
825839
while ((lblock + 1) * sdp->sd_sb.sb_bsize > sdp->sd_heightsize[height])
@@ -853,21 +867,23 @@ static int gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
853867
iomap->bdev = inode->i_sb->s_bdev;
854868
unlock:
855869
up_read(&ip->i_rw_mutex);
870+
if (dibh)
871+
brelse(dibh);
856872
return ret;
857873

858874
do_alloc:
859875
iomap->addr = IOMAP_NULL_ADDR;
860-
iomap->length = len << inode->i_blkbits;
861876
iomap->type = IOMAP_HOLE;
862-
iomap->flags = 0;
863877
if (flags & IOMAP_REPORT) {
864-
loff_t size = i_size_read(inode);
865878
if (pos >= size)
866879
ret = -ENOENT;
867880
else if (height == ip->i_height)
868881
ret = gfs2_hole_size(inode, lblock, len, mp, iomap);
869882
else
870883
iomap->length = size - pos;
884+
} else if (!(flags & IOMAP_WRITE)) {
885+
if (pos < size && height == ip->i_height)
886+
ret = gfs2_hole_size(inode, lblock, len, mp, iomap);
871887
}
872888
goto out;
873889
}
@@ -941,12 +957,6 @@ int gfs2_block_map(struct inode *inode, sector_t lblock,
941957
} else {
942958
ret = gfs2_iomap_get(inode, pos, length, 0, &iomap, &mp);
943959
release_metapath(&mp);
944-
945-
/* Return unmapped buffer beyond the end of file. */
946-
if (ret == -ENOENT) {
947-
ret = 0;
948-
goto out;
949-
}
950960
}
951961
if (ret)
952962
goto out;

‎fs/gfs2/file.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
754754
if (error)
755755
goto out;
756756
offset = iomap.offset + iomap.length;
757-
if (iomap.type != IOMAP_HOLE)
757+
if (!(iomap.flags & IOMAP_F_NEW))
758758
continue;
759759
error = sb_issue_zeroout(sb, iomap.addr >> inode->i_blkbits,
760760
iomap.length >> inode->i_blkbits,

0 commit comments

Comments
 (0)