gfs2_grow opens the rindex file through the metafs so it needs a way to populate a lgfs2_rgrps_t with existing resource groups. Add lgfs2_rindex_read_fd() to read in the rindex entries from a fd.
Also const-ify the return types of lgfs2_rgrp_index() and lgfs2_rgrp_rgrp() to warn users that they shouldn't change the contents of libgfs2-managed data. Signed-off-by: Andrew Price <anpr...@redhat.com> --- gfs2/libgfs2/libgfs2.h | 5 +++-- gfs2/libgfs2/rgrp.c | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h index b70c36d..455f01d 100644 --- a/gfs2/libgfs2/libgfs2.h +++ b/gfs2/libgfs2/libgfs2.h @@ -192,14 +192,15 @@ typedef struct _lgfs2_rgrps *lgfs2_rgrps_t; extern lgfs2_rgrps_t lgfs2_rgrps_init(unsigned bsize, uint64_t devlen, uint64_t align, uint64_t offset); extern void lgfs2_rgrps_free(lgfs2_rgrps_t *rgs); extern uint64_t lgfs2_rindex_entry_new(lgfs2_rgrps_t rgs, struct gfs2_rindex *entry, uint64_t addr, uint32_t len); +extern unsigned lgfs2_rindex_read_fd(int fd, lgfs2_rgrps_t rgs); extern uint64_t lgfs2_rgrp_align_addr(const lgfs2_rgrps_t rgs, uint64_t addr); extern uint32_t lgfs2_rgrp_align_len(const lgfs2_rgrps_t rgs, uint32_t len); extern unsigned lgfs2_rgsize_for_data(uint64_t blksreq, unsigned bsize); extern uint32_t lgfs2_rgrps_plan(const lgfs2_rgrps_t rgs, uint64_t space, uint32_t tgtsize); extern lgfs2_rgrp_t lgfs2_rgrps_append(lgfs2_rgrps_t rgs, struct gfs2_rindex *entry); extern int lgfs2_rgrp_write(lgfs2_rgrps_t rgs, int fd, lgfs2_rgrp_t rg); -extern struct gfs2_rindex *lgfs2_rgrp_index(lgfs2_rgrp_t rg); -extern struct gfs2_rgrp *lgfs2_rgrp_rgrp(lgfs2_rgrp_t rg); +extern const struct gfs2_rindex *lgfs2_rgrp_index(lgfs2_rgrp_t rg); +extern const struct gfs2_rgrp *lgfs2_rgrp_rgrp(lgfs2_rgrp_t rg); extern lgfs2_rgrp_t lgfs2_rgrp_first(lgfs2_rgrps_t rgs); extern lgfs2_rgrp_t lgfs2_rgrp_last(lgfs2_rgrps_t rgs); extern lgfs2_rgrp_t lgfs2_rgrp_next(lgfs2_rgrp_t rg); diff --git a/gfs2/libgfs2/rgrp.c b/gfs2/libgfs2/rgrp.c index 02ab450..cae7a32 100644 --- a/gfs2/libgfs2/rgrp.c +++ b/gfs2/libgfs2/rgrp.c @@ -275,8 +275,8 @@ uint32_t lgfs2_rgrp_align_len(const lgfs2_rgrps_t rgs, uint32_t len) * rgs: The resource groups descriptor * space: The number of remaining blocks to be allocated * tgtsize: The target resource group size in blocks - * Returns the larger of the calculated resource group sizes or 0 if the - * smaller would be less than GFS2_MIN_RGSIZE. + * Returns the larger of the calculated resource group sizes, in blocks, or 0 + * if the smaller would be less than GFS2_MIN_RGSIZE. */ uint32_t lgfs2_rgrps_plan(const lgfs2_rgrps_t rgs, uint64_t space, uint32_t tgtsize) { @@ -361,6 +361,41 @@ lgfs2_rgrps_t lgfs2_rgrps_init(unsigned bsize, uint64_t devlen, uint64_t align, } /** + * Populate a set of resource groups from a gfs2 rindex file. + * fd: An open file descriptor for the rindex file. + * rgs: The set of resource groups. + * Returns the number of resource groups added to the set or 0 on error with + * errno set. + */ +unsigned lgfs2_rindex_read_fd(int fd, lgfs2_rgrps_t rgs) +{ + unsigned count = 0; + char buf[sizeof(struct gfs2_rindex)]; + + errno = EINVAL; + if (fd < 0 || rgs == NULL) + return 0; + + while (1) { + lgfs2_rgrp_t rg; + struct gfs2_rindex ri; + ssize_t ret = read(fd, buf, sizeof(struct gfs2_rindex)); + if (ret == 0) + break; + + if (ret != sizeof(struct gfs2_rindex)) + return 0; + + gfs2_rindex_in(&ri, buf); + rg = lgfs2_rgrps_append(rgs, &ri); + if (rg == NULL) + return 0; + count++; + } + return count; +} + +/** * Free a set of resource groups created with lgfs2_rgrps_append() etc. This * does not write any dirty buffers to disk. See lgfs2_rgrp_write(). * rgs: A pointer to the set of resource groups to be freed. @@ -428,7 +463,7 @@ uint64_t lgfs2_rindex_entry_new(lgfs2_rgrps_t rgs, struct gfs2_rindex *ri, uint6 /** * Return the rindex structure relating to a a resource group. */ -struct gfs2_rindex *lgfs2_rgrp_index(lgfs2_rgrp_t rg) +const struct gfs2_rindex *lgfs2_rgrp_index(lgfs2_rgrp_t rg) { return &rg->ri; } @@ -436,7 +471,7 @@ struct gfs2_rindex *lgfs2_rgrp_index(lgfs2_rgrp_t rg) /** * Return the rgrp structure relating to a a resource group. */ -struct gfs2_rgrp *lgfs2_rgrp_rgrp(lgfs2_rgrp_t rg) +const struct gfs2_rgrp *lgfs2_rgrp_rgrp(lgfs2_rgrp_t rg) { return &rg->rg; } -- 1.8.5.3