Skip to content

Commit 628e366

Browse files
agruenbaAstralBob
authored andcommitted
gfs2: Iomap cleanups and improvements
Clean up gfs2_iomap_alloc and gfs2_iomap_get. Document how gfs2_iomap_alloc works: it now needs to be called separately after gfs2_iomap_get where necessary; this will be used later by iomap write. Move gfs2_iomap_ops into bmap.c. Introduce a new gfs2_iomap_get_alloc helper and use it in fallocate_chunk: gfs2_iomap_begin will become unsuitable for fallocate with proper iomap write support. In gfs2_block_map and fallocate_chunk, zero-initialize struct iomap. Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com> Signed-off-by: Bob Peterson <rpeterso@redhat.com>
1 parent 845802b commit 628e366

4 files changed

Lines changed: 126 additions & 95 deletions

File tree

‎fs/gfs2/bmap.c‎

Lines changed: 119 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -572,22 +572,6 @@ static int gfs2_hole_size(struct inode *inode, sector_t lblock, u64 len,
572572
return ret;
573573
}
574574

575-
static inline void bmap_lock(struct gfs2_inode *ip, int create)
576-
{
577-
if (create)
578-
down_write(&ip->i_rw_mutex);
579-
else
580-
down_read(&ip->i_rw_mutex);
581-
}
582-
583-
static inline void bmap_unlock(struct gfs2_inode *ip, int create)
584-
{
585-
if (create)
586-
up_write(&ip->i_rw_mutex);
587-
else
588-
up_read(&ip->i_rw_mutex);
589-
}
590-
591575
static inline __be64 *gfs2_indirect_init(struct metapath *mp,
592576
struct gfs2_glock *gl, unsigned int i,
593577
unsigned offset, u64 bn)
@@ -614,15 +598,11 @@ enum alloc_state {
614598
};
615599

616600
/**
617-
* gfs2_bmap_alloc - Build a metadata tree of the requested height
601+
* gfs2_iomap_alloc - Build a metadata tree of the requested height
618602
* @inode: The GFS2 inode
619-
* @lblock: The logical starting block of the extent
620-
* @bh_map: This is used to return the mapping details
621-
* @zero_new: True if newly allocated blocks should be zeroed
603+
* @iomap: The iomap structure
604+
* @flags: iomap flags
622605
* @mp: The metapath, with proper height information calculated
623-
* @maxlen: The max number of data blocks to alloc
624-
* @dblock: Pointer to return the resulting new block
625-
* @dblks: Pointer to return the number of blocks allocated
626606
*
627607
* In this routine we may have to alloc:
628608
* i) Indirect blocks to grow the metadata tree height
@@ -635,6 +615,13 @@ enum alloc_state {
635615
* blocks are available, there will only be one request per bmap call)
636616
* and uses the state machine to initialise the blocks in order.
637617
*
618+
* Right now, this function will allocate at most one indirect block
619+
* worth of data -- with a default block size of 4K, that's slightly
620+
* less than 2M. If this limitation is ever removed to allow huge
621+
* allocations, we would probably still want to limit the iomap size we
622+
* return to avoid stalling other tasks during huge writes; the next
623+
* iomap iteration would then find the blocks already allocated.
624+
*
638625
* Returns: errno on error
639626
*/
640627

@@ -649,6 +636,7 @@ static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
649636
unsigned dblks = 0;
650637
unsigned ptrs_per_blk;
651638
const unsigned end_of_metadata = mp->mp_fheight - 1;
639+
int ret;
652640
enum alloc_state state;
653641
__be64 *ptr;
654642
__be64 zero_bn = 0;
@@ -659,6 +647,8 @@ static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
659647

660648
gfs2_trans_add_meta(ip->i_gl, dibh);
661649

650+
down_write(&ip->i_rw_mutex);
651+
662652
if (mp->mp_fheight == mp->mp_aheight) {
663653
struct buffer_head *bh;
664654
int eob;
@@ -694,11 +684,10 @@ static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
694684
blks = dblks + iblks;
695685
i = mp->mp_aheight;
696686
do {
697-
int error;
698687
n = blks - alloced;
699-
error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
700-
if (error)
701-
return error;
688+
ret = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
689+
if (ret)
690+
goto out;
702691
alloced += n;
703692
if (state != ALLOC_DATA || gfs2_is_jdata(ip))
704693
gfs2_trans_add_unrevoke(sdp, bn, n);
@@ -754,7 +743,7 @@ static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
754743
dblks = n;
755744
ptr = metapointer(end_of_metadata, mp);
756745
iomap->addr = bn << inode->i_blkbits;
757-
iomap->flags |= IOMAP_F_NEW;
746+
iomap->flags |= IOMAP_F_MERGED | IOMAP_F_NEW;
758747
while (n-- > 0)
759748
*ptr++ = cpu_to_be64(bn++);
760749
break;
@@ -764,8 +753,10 @@ static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
764753
iomap->length = (u64)dblks << inode->i_blkbits;
765754
ip->i_height = mp->mp_fheight;
766755
gfs2_add_inode_blocks(&ip->i_inode, alloced);
767-
gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
768-
return 0;
756+
gfs2_dinode_out(ip, dibh->b_data);
757+
out:
758+
up_write(&ip->i_rw_mutex);
759+
return ret;
769760
}
770761

771762
static void gfs2_stuffed_iomap(struct inode *inode, struct iomap *iomap)
@@ -781,110 +772,130 @@ static void gfs2_stuffed_iomap(struct inode *inode, struct iomap *iomap)
781772
}
782773

783774
/**
784-
* gfs2_iomap_begin - Map blocks from an inode to disk blocks
775+
* gfs2_iomap_get - Map blocks from an inode to disk blocks
785776
* @inode: The inode
786777
* @pos: Starting position in bytes
787778
* @length: Length to map, in bytes
788779
* @flags: iomap flags
789780
* @iomap: The iomap structure
781+
* @mp: The metapath
790782
*
791783
* Returns: errno
792784
*/
793-
int gfs2_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
794-
unsigned flags, struct iomap *iomap)
785+
static int gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
786+
unsigned flags, struct iomap *iomap,
787+
struct metapath *mp)
795788
{
796789
struct gfs2_inode *ip = GFS2_I(inode);
797790
struct gfs2_sbd *sdp = GFS2_SB(inode);
798-
struct metapath mp = { .mp_aheight = 1, };
799791
__be64 *ptr;
800792
sector_t lblock;
801-
sector_t lend;
802-
int ret = 0;
793+
sector_t lblock_stop;
794+
int ret;
803795
int eob;
804-
unsigned int len;
796+
u64 len;
805797
struct buffer_head *bh;
806798
u8 height;
807799

808-
trace_gfs2_iomap_start(ip, pos, length, flags);
809-
if (!length) {
810-
ret = -EINVAL;
811-
goto out;
812-
}
800+
if (!length)
801+
return -EINVAL;
813802

814803
if (gfs2_is_stuffed(ip)) {
815804
if (flags & IOMAP_REPORT) {
805+
if (pos >= i_size_read(inode))
806+
return -ENOENT;
816807
gfs2_stuffed_iomap(inode, iomap);
817-
if (pos >= iomap->length)
818-
ret = -ENOENT;
819-
goto out;
808+
return 0;
820809
}
821810
BUG_ON(!(flags & IOMAP_WRITE));
822811
}
823-
824812
lblock = pos >> inode->i_blkbits;
825-
lend = (pos + length + sdp->sd_sb.sb_bsize - 1) >> inode->i_blkbits;
826-
len = lend - lblock;
827-
828813
iomap->offset = lblock << inode->i_blkbits;
829-
iomap->addr = IOMAP_NULL_ADDR;
830-
iomap->type = IOMAP_HOLE;
831-
iomap->length = (u64)(lend - lblock) << inode->i_blkbits;
832-
iomap->flags = IOMAP_F_MERGED;
833-
bmap_lock(ip, flags & IOMAP_WRITE);
814+
lblock_stop = (pos + length - 1) >> inode->i_blkbits;
815+
len = lblock_stop - lblock + 1;
816+
817+
down_read(&ip->i_rw_mutex);
834818

835-
ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
819+
ret = gfs2_meta_inode_buffer(ip, &mp->mp_bh[0]);
836820
if (ret)
837-
goto out_release;
821+
goto unlock;
838822

839823
height = ip->i_height;
840824
while ((lblock + 1) * sdp->sd_sb.sb_bsize > sdp->sd_heightsize[height])
841825
height++;
842-
find_metapath(sdp, lblock, &mp, height);
826+
find_metapath(sdp, lblock, mp, height);
843827
if (height > ip->i_height || gfs2_is_stuffed(ip))
844828
goto do_alloc;
845829

846-
ret = lookup_metapath(ip, &mp);
830+
ret = lookup_metapath(ip, mp);
847831
if (ret)
848-
goto out_release;
832+
goto unlock;
849833

850-
if (mp.mp_aheight != ip->i_height)
834+
if (mp->mp_aheight != ip->i_height)
851835
goto do_alloc;
852836

853-
ptr = metapointer(ip->i_height - 1, &mp);
837+
ptr = metapointer(ip->i_height - 1, mp);
854838
if (*ptr == 0)
855839
goto do_alloc;
856840

857-
iomap->type = IOMAP_MAPPED;
858-
iomap->addr = be64_to_cpu(*ptr) << inode->i_blkbits;
841+
bh = mp->mp_bh[ip->i_height - 1];
842+
len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, len, &eob);
859843

860-
bh = mp.mp_bh[ip->i_height - 1];
861-
len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, lend - lblock, &eob);
844+
iomap->addr = be64_to_cpu(*ptr) << inode->i_blkbits;
845+
iomap->length = len << inode->i_blkbits;
846+
iomap->type = IOMAP_MAPPED;
847+
iomap->flags = IOMAP_F_MERGED;
862848
if (eob)
863849
iomap->flags |= IOMAP_F_BOUNDARY;
864-
iomap->length = (u64)len << inode->i_blkbits;
865850

866-
out_release:
867-
release_metapath(&mp);
868-
bmap_unlock(ip, flags & IOMAP_WRITE);
869851
out:
870-
trace_gfs2_iomap_end(ip, iomap, ret);
852+
iomap->bdev = inode->i_sb->s_bdev;
853+
unlock:
854+
up_read(&ip->i_rw_mutex);
871855
return ret;
872856

873857
do_alloc:
874-
if (flags & IOMAP_WRITE) {
875-
ret = gfs2_iomap_alloc(inode, iomap, flags, &mp);
876-
} else if (flags & IOMAP_REPORT) {
858+
iomap->addr = IOMAP_NULL_ADDR;
859+
iomap->length = len << inode->i_blkbits;
860+
iomap->type = IOMAP_HOLE;
861+
iomap->flags = 0;
862+
if (flags & IOMAP_REPORT) {
877863
loff_t size = i_size_read(inode);
878864
if (pos >= size)
879865
ret = -ENOENT;
880-
else if (height <= ip->i_height)
881-
ret = gfs2_hole_size(inode, lblock, len, &mp, iomap);
866+
else if (height == ip->i_height)
867+
ret = gfs2_hole_size(inode, lblock, len, mp, iomap);
882868
else
883869
iomap->length = size - pos;
884870
}
885-
goto out_release;
871+
goto out;
886872
}
887873

874+
static int gfs2_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
875+
unsigned flags, struct iomap *iomap)
876+
{
877+
struct gfs2_inode *ip = GFS2_I(inode);
878+
struct metapath mp = { .mp_aheight = 1, };
879+
int ret;
880+
881+
trace_gfs2_iomap_start(ip, pos, length, flags);
882+
if (flags & IOMAP_WRITE) {
883+
ret = gfs2_iomap_get(inode, pos, length, flags, iomap, &mp);
884+
if (!ret && iomap->type == IOMAP_HOLE)
885+
ret = gfs2_iomap_alloc(inode, iomap, flags, &mp);
886+
release_metapath(&mp);
887+
} else {
888+
ret = gfs2_iomap_get(inode, pos, length, flags, iomap, &mp);
889+
release_metapath(&mp);
890+
}
891+
trace_gfs2_iomap_end(ip, iomap, ret);
892+
return ret;
893+
}
894+
895+
const struct iomap_ops gfs2_iomap_ops = {
896+
.iomap_begin = gfs2_iomap_begin,
897+
};
898+
888899
/**
889900
* gfs2_block_map - Map one or more blocks of an inode to a disk block
890901
* @inode: The inode
@@ -910,25 +921,34 @@ int gfs2_block_map(struct inode *inode, sector_t lblock,
910921
struct buffer_head *bh_map, int create)
911922
{
912923
struct gfs2_inode *ip = GFS2_I(inode);
913-
struct iomap iomap;
914-
int ret, flags = 0;
924+
loff_t pos = (loff_t)lblock << inode->i_blkbits;
925+
loff_t length = bh_map->b_size;
926+
struct metapath mp = { .mp_aheight = 1, };
927+
struct iomap iomap = { };
928+
int ret;
915929

916930
clear_buffer_mapped(bh_map);
917931
clear_buffer_new(bh_map);
918932
clear_buffer_boundary(bh_map);
919933
trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
920934

921-
if (create)
922-
flags |= IOMAP_WRITE;
923-
ret = gfs2_iomap_begin(inode, (loff_t)lblock << inode->i_blkbits,
924-
bh_map->b_size, flags, &iomap);
925-
if (ret) {
926-
if (!create && ret == -ENOENT) {
927-
/* Return unmapped buffer beyond the end of file. */
935+
if (create) {
936+
ret = gfs2_iomap_get(inode, pos, length, IOMAP_WRITE, &iomap, &mp);
937+
if (!ret && iomap.type == IOMAP_HOLE)
938+
ret = gfs2_iomap_alloc(inode, &iomap, IOMAP_WRITE, &mp);
939+
release_metapath(&mp);
940+
} else {
941+
ret = gfs2_iomap_get(inode, pos, length, 0, &iomap, &mp);
942+
release_metapath(&mp);
943+
944+
/* Return unmapped buffer beyond the end of file. */
945+
if (ret == -ENOENT) {
928946
ret = 0;
947+
goto out;
929948
}
930-
goto out;
931949
}
950+
if (ret)
951+
goto out;
932952

933953
if (iomap.length > bh_map->b_size) {
934954
iomap.length = bh_map->b_size;
@@ -1143,6 +1163,19 @@ static int trunc_start(struct inode *inode, u64 newsize)
11431163
return error;
11441164
}
11451165

1166+
int gfs2_iomap_get_alloc(struct inode *inode, loff_t pos, loff_t length,
1167+
struct iomap *iomap)
1168+
{
1169+
struct metapath mp = { .mp_aheight = 1, };
1170+
int ret;
1171+
1172+
ret = gfs2_iomap_get(inode, pos, length, IOMAP_WRITE, iomap, &mp);
1173+
if (!ret && iomap->type == IOMAP_HOLE)
1174+
ret = gfs2_iomap_alloc(inode, iomap, IOMAP_WRITE, &mp);
1175+
release_metapath(&mp);
1176+
return ret;
1177+
}
1178+
11461179
/**
11471180
* sweep_bh_for_rgrps - find an rgrp in a meta buffer and free blocks therein
11481181
* @ip: inode

‎fs/gfs2/bmap.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ static inline void gfs2_write_calc_reserv(const struct gfs2_inode *ip,
4646
}
4747
}
4848

49+
extern const struct iomap_ops gfs2_iomap_ops;
50+
4951
extern int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page);
5052
extern int gfs2_block_map(struct inode *inode, sector_t lblock,
5153
struct buffer_head *bh, int create);
52-
extern int gfs2_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
53-
unsigned flags, struct iomap *iomap);
54+
extern int gfs2_iomap_get_alloc(struct inode *inode, loff_t pos, loff_t length,
55+
struct iomap *iomap);
5456
extern int gfs2_extent_map(struct inode *inode, u64 lblock, int *new,
5557
u64 *dblock, unsigned *extlen);
5658
extern int gfs2_setattr_size(struct inode *inode, u64 size);

‎fs/gfs2/file.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
733733
struct gfs2_inode *ip = GFS2_I(inode);
734734
loff_t end = offset + len;
735735
struct buffer_head *dibh;
736-
struct iomap iomap;
736+
struct iomap iomap = { };
737737
int error;
738738

739739
error = gfs2_meta_inode_buffer(ip, &dibh);
@@ -749,8 +749,8 @@ static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
749749
}
750750

751751
while (offset < end) {
752-
error = gfs2_iomap_begin(inode, offset, end - offset,
753-
IOMAP_WRITE, &iomap);
752+
error = gfs2_iomap_get_alloc(inode, offset, end - offset,
753+
&iomap);
754754
if (error)
755755
goto out;
756756
offset = iomap.offset + iomap.length;

‎fs/gfs2/inode.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,10 +2006,6 @@ static int gfs2_getattr(const struct path *path, struct kstat *stat,
20062006
return 0;
20072007
}
20082008

2009-
const struct iomap_ops gfs2_iomap_ops = {
2010-
.iomap_begin = gfs2_iomap_begin,
2011-
};
2012-
20132009
static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
20142010
u64 start, u64 len)
20152011
{

0 commit comments

Comments
 (0)