gfs2-utils: RHEL7 - gfs2l: Check unchecked returns in openfs
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=ced7d62f...
Commit: ced7d62fde2e84f1a5fa216b494f608beb42167d
Parent: d7154963adc558244b67e9c7bd449ae6ff94e367
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Fri Oct 10 17:01:30 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
gfs2l: Check unchecked returns in openfs
Fix some coverity complaints that return values from compute_constants()
and lgfs2_get_dev_info() weren't being checked. Also avoid unnecessary
memory management for the gfs2_sbd.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/libgfs2/gfs2l.c | 45 ++++++++++++++++++++++-----------------------
1 files changed, 22 insertions(+), 23 deletions(-)
diff --git a/gfs2/libgfs2/gfs2l.c b/gfs2/libgfs2/gfs2l.c
index eda9531..5d9afd8 100644
--- a/gfs2/libgfs2/gfs2l.c
+++ b/gfs2/libgfs2/gfs2l.c
@@ -96,36 +96,38 @@ static int getopts(int argc, char *argv[], struct cmdopts *opts)
return 0;
}
-static struct gfs2_sbd *openfs(const char *path)
+static int openfs(const char *path, struct gfs2_sbd *sdp)
{
int fd;
int ret;
int sane;
int count;
- struct gfs2_sbd *sdp = calloc(1, sizeof(struct gfs2_sbd));
- if (sdp == NULL) {
- perror("calloc");
- return NULL;
- }
fd = open(path, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Failed to open %s\n", path);
- free(sdp);
- return NULL;
+ return 1;
}
memset(sdp, 0, sizeof(*sdp));
sdp->bsize = GFS2_BASIC_BLOCK;
sdp->device_fd = fd;
- compute_constants(sdp);
- lgfs2_get_dev_info(fd, &sdp->dinfo);
+ ret = compute_constants(sdp);
+ if (ret != 0) {
+ perror("Bad constants");
+ return 1;
+ }
+ ret = lgfs2_get_dev_info(fd, &sdp->dinfo);
+ if (ret != 0) {
+ perror("Failed to gather device info");
+ return 1;
+ }
fix_device_geometry(sdp);
ret = read_sb(sdp);
if (ret != 0) {
perror("Could not read sb");
- return NULL;
+ return 1;
}
sdp->master_dir = lgfs2_inode_read(sdp, sdp->sd_sb.sb_master_dir.no_addr);
@@ -135,17 +137,16 @@ static struct gfs2_sbd *openfs(const char *path)
rindex_read(sdp, 0, &count, &sane);
} else {
perror("Failed to look up rindex");
- free(sdp);
- return NULL;
+ return 1;
}
- return sdp;
+ return 0;
}
int main(int argc, char *argv[])
{
int ret;
struct cmdopts opts = {NULL, NULL};
- struct gfs2_sbd *sdp;
+ struct gfs2_sbd sbd;
struct lgfs2_lang_result *result;
struct lgfs2_lang_state *state;
@@ -158,10 +159,8 @@ int main(int argc, char *argv[])
exit(0);
}
- sdp = openfs(argv[optind]);
- if (sdp == NULL) {
+ if (openfs(argv[optind], &sbd))
exit(1);
- }
state = lgfs2_lang_init();
if (state == NULL) {
@@ -176,9 +175,9 @@ int main(int argc, char *argv[])
return ret;
}
- for (result = lgfs2_lang_result_next(state, sdp);
+ for (result = lgfs2_lang_result_next(state, &sbd);
result != NULL;
- result = lgfs2_lang_result_next(state, sdp)) {
+ result = lgfs2_lang_result_next(state, &sbd)) {
if (result == NULL) {
fprintf(stderr, "Failed to interpret script\n");
return -1;
@@ -187,9 +186,9 @@ int main(int argc, char *argv[])
lgfs2_lang_result_free(&result);
}
- gfs2_rgrp_free(&sdp->rgtree);
- inode_put(&sdp->md.riinode);
- inode_put(&sdp->master_dir);
+ gfs2_rgrp_free(&sbd.rgtree);
+ inode_put(&sbd.md.riinode);
+ inode_put(&sbd.master_dir);
lgfs2_lang_free(&state);
free(opts.fspath);
return 0;
9 years
gfs2-utils: RHEL7 - libgfs2: Remove orig_journals from struct gfs2_sbd
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=d7154963...
Commit: d7154963adc558244b67e9c7bd449ae6ff94e367
Parent: 65d7c65f12cf4e6e551af516ba3e5e51fd51a0c8
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Fri Oct 10 16:23:27 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
libgfs2: Remove orig_journals from struct gfs2_sbd
The last remaining user of this field is easily changed to use its own
orig_journals variable instead.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/convert/gfs2_convert.c | 5 +++--
gfs2/libgfs2/libgfs2.h | 2 --
2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/gfs2/convert/gfs2_convert.c b/gfs2/convert/gfs2_convert.c
index 8b86663..c0980bc 100644
--- a/gfs2/convert/gfs2_convert.c
+++ b/gfs2/convert/gfs2_convert.c
@@ -114,6 +114,7 @@ uint32_t gfs2_max_height;
uint32_t gfs2_max_jheight;
uint64_t jindex_addr = 0, rindex_addr = 0;
int print_level = MSG_NOTICE;
+unsigned orig_journals = 0;
/* ------------------------------------------------------------------------- */
/* This function is for libgfs's sake. */
@@ -1513,7 +1514,7 @@ static int read_gfs1_jiindex(struct gfs2_sbd *sdp)
log_crit(_("journal inode size invalid\n"));
goto fail;
}
- sdp->md.journals = sdp->orig_journals = j;
+ sdp->md.journals = orig_journals = j;
return 0;
fail:
@@ -1821,7 +1822,7 @@ static int journ_space_to_rg(struct gfs2_sbd *sdp)
mh.mh_format = GFS2_FORMAT_RB;
log_notice(_("Converting journal space to rg space.\n"));
/* Go through each journal, converting them one by one */
- for (j = 0; j < sdp->orig_journals; j++) { /* for each journal */
+ for (j = 0; j < orig_journals; j++) { /* for each journal */
uint64_t size;
jndx = &sd_jindex[j];
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
index 9ea7eb9..45ec8b1 100644
--- a/gfs2/libgfs2/libgfs2.h
+++ b/gfs2/libgfs2/libgfs2.h
@@ -321,8 +321,6 @@ struct gfs2_sbd {
struct osi_root rgtree;
struct osi_root rgcalc;
- unsigned int orig_journals;
-
struct gfs2_inode *master_dir;
struct master_dir md;
9 years
gfs2-utils: RHEL7 - gfs2_jadd: Don't use fixed size buffers for paths
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=65d7c65f...
Commit: 65d7c65f12cf4e6e551af516ba3e5e51fd51a0c8
Parent: c319aec6eed4238492b1ed707c08d79ca2df6bef
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Fri Oct 10 12:04:40 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
gfs2_jadd: Don't use fixed size buffers for paths
This fixes some more PATH_MAX usages by dynamically allocating the space
to store metafs path strings. Previously the strings were rebuilt each
time they were required using sprintf and fixed size buffers. Remove the
sprintfs by storing the strings in the opts structure and pass around a
pointer to it instead of the gfs2_sbd where possible. This reduces the
number of uses of sbd.orig_journals which should be removed eventually
as it's application-specific.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/mkfs/main_jadd.c | 189 ++++++++++++++++++++++++++-----------------------
1 files changed, 99 insertions(+), 90 deletions(-)
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
index 4968ec9..6507e03 100644
--- a/gfs2/mkfs/main_jadd.c
+++ b/gfs2/mkfs/main_jadd.c
@@ -24,11 +24,15 @@
#include "libgfs2.h"
#include "gfs2_mkfs.h"
-#define BUF_SIZE 4096
#define RANDOM(values) ((values) * (random() / (RAND_MAX + 1.0)))
struct jadd_opts {
char *path;
+ char *new_inode;
+ char *per_node;
+ char *jindex;
+ unsigned orig_journals;
+ unsigned journals;
unsigned quiet:1;
unsigned debug:1;
};
@@ -57,22 +61,45 @@ make_jdata(int fd, const char *value)
}
}
-static int
-rename2system(struct gfs2_sbd *sdp, const char *new_dir, const char *new_name)
+static int rename2system(struct jadd_opts *opts, const char *new_dir, const char *new_name)
{
- char oldpath[PATH_MAX], newpath[PATH_MAX];
- int error = 0;
- error = snprintf(oldpath, PATH_MAX, "%s/new_inode",
- sdp->metafs_path);
- if (error >= PATH_MAX)
- die("rename2system (1)\n");
-
- error = snprintf(newpath, PATH_MAX, "%s/%s/%s",
- sdp->metafs_path, new_dir, new_name);
- if (error >= PATH_MAX)
- die("rename2system (2)\n");
-
- return rename(oldpath, newpath);
+ char *newpath;
+ int ret;
+
+ if (asprintf(&newpath, "%s/%s", new_dir, new_name) < 0) {
+ perror(_("Failed to allocate new path"));
+ return -1;
+ }
+
+ ret = rename(opts->new_inode, newpath);
+ free(newpath);
+ return ret;
+}
+
+static int build_paths(const char *metafs_path, struct jadd_opts *opts)
+{
+ int i = 0;
+ struct {
+ char **path;
+ const char *tail;
+ } p[] = {
+ { &opts->new_inode, "new_inode" },
+ { &opts->per_node, "per_node" },
+ { &opts->jindex, "jindex" },
+ { NULL, NULL}
+ };
+
+ while (p[i].path != NULL) {
+ if (asprintf(p[i].path, "%s/%s", metafs_path, p[i].tail) < 0) {
+ while (i > 0)
+ free(*p[--i].path);
+ return 1;
+ }
+ if (opts->debug)
+ printf("%d: %s\n", i, *p[i].path);
+ i++;
+ }
+ return 0;
}
/**
@@ -143,7 +170,7 @@ static void decode_arguments(int argc, char *argv[], struct gfs2_sbd *sdp, struc
sdp->jsize = atoi(optarg);
break;
case 'j':
- sdp->md.journals = atoi(optarg);
+ opts->journals = atoi(optarg);
break;
case 'q':
opts->quiet = 1;
@@ -187,9 +214,9 @@ static void decode_arguments(int argc, char *argv[], struct gfs2_sbd *sdp, struc
}
}
-static void verify_arguments(struct gfs2_sbd *sdp)
+static void verify_arguments(struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
- if (!sdp->md.journals)
+ if (!opts->journals)
die( _("no journals specified\n"));
if (sdp->jsize < 32 || sdp->jsize > 1024)
die( _("bad journal size\n"));
@@ -197,13 +224,7 @@ static void verify_arguments(struct gfs2_sbd *sdp)
die( _("bad quota change size\n"));
}
-/**
- * print_results - print out summary information
- * @sdp: the command line
- *
- */
-
-static void print_results(struct gfs2_sbd *sdp, struct jadd_opts *opts)
+static void print_results(struct jadd_opts *opts)
{
if (opts->debug)
printf("\n");
@@ -211,22 +232,16 @@ static void print_results(struct gfs2_sbd *sdp, struct jadd_opts *opts)
return;
printf( _("Filesystem: %s\n"), opts->path);
- printf( _("Old Journals: %u\n"), sdp->orig_journals);
- printf( _("New Journals: %u\n"), sdp->md.journals);
-
+ printf( _("Old Journals: %u\n"), opts->orig_journals);
+ printf( _("New Journals: %u\n"), opts->journals);
}
-static int
-create_new_inode(struct gfs2_sbd *sdp)
+static int create_new_inode(struct jadd_opts *opts)
{
- char name[PATH_MAX];
+ char *name = opts->new_inode;
int fd;
int error;
- error = snprintf(name, PATH_MAX, "%s/new_inode", sdp->metafs_path);
- if (error >= PATH_MAX)
- die("create_new_inode (1)\n");
-
for (;;) {
fd = open(name, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
if (fd >= 0)
@@ -242,18 +257,17 @@ create_new_inode(struct gfs2_sbd *sdp)
exit(EXIT_FAILURE);
}
}
-
+
return fd;
}
-static void
-add_ir(struct gfs2_sbd *sdp)
+static void add_ir(struct jadd_opts *opts)
{
int fd;
char new_name[256];
int error;
- fd = create_new_inode(sdp);
+ fd = create_new_inode(opts);
{
struct gfs2_inum_range ir;
@@ -265,26 +279,25 @@ add_ir(struct gfs2_sbd *sdp)
exit(EXIT_FAILURE);
}
}
-
+
close(fd);
-
- sprintf(new_name, "inum_range%u", sdp->md.journals);
- error = rename2system(sdp, "per_node", new_name);
+
+ sprintf(new_name, "inum_range%u", opts->journals);
+ error = rename2system(opts, opts->per_node, new_name);
if (error < 0 && errno != EEXIST){
perror("add_ir rename2system");
exit(EXIT_FAILURE);
}
}
-static void
-add_sc(struct gfs2_sbd *sdp)
+static void add_sc(struct jadd_opts *opts)
{
int fd;
char new_name[256];
int error;
-
- fd = create_new_inode(sdp);
-
+
+ fd = create_new_inode(opts);
+
{
struct gfs2_statfs_change sc;
make_jdata(fd, "set");
@@ -298,23 +311,22 @@ add_sc(struct gfs2_sbd *sdp)
}
close(fd);
-
- sprintf(new_name, "statfs_change%u", sdp->md.journals);
- error = rename2system(sdp, "per_node", new_name);
+
+ sprintf(new_name, "statfs_change%u", opts->journals);
+ error = rename2system(opts, opts->per_node, new_name);
if (error < 0 && errno != EEXIST){
perror("add_sc rename2system");
exit(EXIT_FAILURE);
}
}
-static void
-add_qc(struct gfs2_sbd *sdp)
+static void add_qc(struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
int fd;
char new_name[256];
int error;
- fd = create_new_inode(sdp);
+ fd = create_new_inode(opts);
{
char buf[sdp->bsize];
@@ -359,8 +371,8 @@ add_qc(struct gfs2_sbd *sdp)
close(fd);
- sprintf(new_name, "quota_change%u", sdp->md.journals);
- error = rename2system(sdp, "per_node", new_name);
+ sprintf(new_name, "quota_change%u", opts->journals);
+ error = rename2system(opts, opts->per_node, new_name);
if (error < 0 && errno != EEXIST){
perror("add_qc rename2system");
exit(EXIT_FAILURE);
@@ -377,16 +389,13 @@ static void gather_info(struct gfs2_sbd *sdp, struct jadd_opts *opts)
sdp->bsize = statbuf.f_bsize;
}
-static void
-find_current_journals(struct gfs2_sbd *sdp)
+static void find_current_journals(struct jadd_opts *opts)
{
- char jindex[PATH_MAX];
struct dirent *dp;
DIR *dirp;
- int existing_journals = 0;
+ unsigned existing_journals = 0;
- sprintf(jindex, "%s/jindex", sdp->metafs_path);
- dirp = opendir(jindex);
+ dirp = opendir(opts->jindex);
if (!dirp) {
perror("jindex");
exit(EXIT_FAILURE);
@@ -400,21 +409,20 @@ find_current_journals(struct gfs2_sbd *sdp)
}
close:
closedir(dirp);
- if (existing_journals <= 0) {
+ if (existing_journals == 0) {
die( _("No journals found. Did you run mkfs.gfs2 correctly?\n"));
}
- sdp->orig_journals = existing_journals;
+ opts->orig_journals = existing_journals;
}
-static void
-add_j(struct gfs2_sbd *sdp)
+static void add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
int fd;
char new_name[256];
int error;
- fd = create_new_inode(sdp);
+ fd = create_new_inode(opts);
{
char buf[sdp->bsize];
@@ -469,22 +477,15 @@ add_j(struct gfs2_sbd *sdp)
}
close(fd);
-
- sprintf(new_name, "journal%u", sdp->md.journals);
- error = rename2system(sdp, "jindex", new_name);
+
+ sprintf(new_name, "journal%u", opts->journals);
+ error = rename2system(opts, opts->jindex, new_name);
if (error < 0 && errno != EEXIST){
perror("add_j rename2system");
exit(EXIT_FAILURE);
}
}
-/**
- * main_jadd - do everything
- * @argc:
- * @argv:
- *
- */
-
void main_jadd(int argc, char *argv[])
{
struct jadd_opts opts = {0};
@@ -495,10 +496,10 @@ void main_jadd(int argc, char *argv[])
memset(sdp, 0, sizeof(struct gfs2_sbd));
sdp->jsize = GFS2_DEFAULT_JSIZE;
sdp->qcsize = GFS2_DEFAULT_QCSIZE;
- sdp->md.journals = 1;
+ opts.journals = 1;
decode_arguments(argc, argv, sdp, &opts);
- verify_arguments(sdp);
+ verify_arguments(sdp, &opts);
sbd.path_fd = lgfs2_open_mnt_dir(opts.path, O_RDONLY|O_CLOEXEC, &mnt);
if (sbd.path_fd < 0) {
@@ -516,28 +517,36 @@ void main_jadd(int argc, char *argv[])
exit(EXIT_FAILURE);
}
+ if (build_paths(sdp->metafs_path, &opts)) {
+ perror(_("Failed to build paths"));
+ exit(EXIT_FAILURE);
+ }
+
if (compute_constants(sdp)) {
perror(_("Bad constants (1)"));
exit(EXIT_FAILURE);
}
- find_current_journals(sdp);
+ find_current_journals(&opts);
- total = sdp->orig_journals + sdp->md.journals;
- for (sdp->md.journals = sdp->orig_journals;
- sdp->md.journals < total;
- sdp->md.journals++) {
+ total = opts.orig_journals + opts.journals;
+ for (opts.journals = opts.orig_journals;
+ opts.journals < total;
+ opts.journals++) {
if (metafs_interrupted) {
cleanup_metafs(&sbd);
exit(130);
}
- add_ir(sdp);
- add_sc(sdp);
- add_qc(sdp);
- add_j(sdp);
+ add_ir(&opts);
+ add_sc(&opts);
+ add_qc(sdp, &opts);
+ add_j(sdp, &opts);
}
+ free(opts.new_inode);
+ free(opts.per_node);
+ free(opts.jindex);
close(sdp->path_fd);
cleanup_metafs(sdp);
sync();
- print_results(sdp, &opts);
+ print_results(&opts);
}
9 years
gfs2-utils: RHEL7 - gfs2_grow: Don't use PATH_MAX in main_grow
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=c319aec6...
Commit: c319aec6eed4238492b1ed707c08d79ca2df6bef
Parent: 5bde01ccacb073f287d05930f33de6ab4c485e9f
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Thu Oct 9 23:35:23 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
gfs2_grow: Don't use PATH_MAX in main_grow
Although "large enough" for most cases, PATH_MAX is an arbitrary limit
for a file path and doesn't reflect reality. Use asprintf(3) to allocate
only as much memory as we need to hold the path to the rindex. open(2)
will tell us if it's too long.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/mkfs/main_grow.c | 24 +++++++++++++++++++-----
1 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/gfs2/mkfs/main_grow.c b/gfs2/mkfs/main_grow.c
index 79ba56a..33618de 100644
--- a/gfs2/mkfs/main_grow.c
+++ b/gfs2/mkfs/main_grow.c
@@ -320,11 +320,28 @@ static void print_info(struct gfs2_sbd *sdp, char *device, char *mnt_path)
(unsigned long long)(fsgrowth * sdp->bsize) / MB);
}
+static int open_rindex(char *metafs_path, int mode)
+{
+ char *path;
+ int fd;
+
+ if (asprintf(&path, "%s/rindex", metafs_path) < 0) {
+ perror(_("Failed to open rindex"));
+ return -1;
+ }
+ fd = open(path, (mode | O_CLOEXEC));
+ if (fd < 0) {
+ perror(path);
+ fprintf(stderr, _("Please run fsck.gfs2\n"));
+ }
+ free(path);
+ return fd;
+}
+
void main_grow(int argc, char *argv[])
{
struct gfs2_sbd sbd, *sdp = &sbd;
int rindex_fd;
- char rindex_name[PATH_MAX];
int error = EXIT_SUCCESS;
int devflags = (test ? O_RDONLY : O_RDWR) | O_CLOEXEC;
@@ -376,12 +393,9 @@ void main_grow(int argc, char *argv[])
perror(_("Failed to mount GFS2 meta file system"));
exit(EXIT_FAILURE);
}
-
- sprintf(rindex_name, "%s/rindex", sdp->metafs_path);
- rindex_fd = open(rindex_name, (test ? O_RDONLY : O_RDWR) | O_CLOEXEC);
+ rindex_fd = open_rindex(sdp->metafs_path, (test ? O_RDONLY : O_RDWR));
if (rindex_fd < 0) {
cleanup_metafs(sdp);
- perror(_("GFS2 rindex not found. Please run fsck.gfs2.\n"));
exit(EXIT_FAILURE);
}
/* Get master dinode */
9 years
gfs2-utils: RHEL7 - libgfs2: metafs_path improvements
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=5bde01cc...
Commit: 5bde01ccacb073f287d05930f33de6ab4c485e9f
Parent: 2e94ddbdf1f60a0099cc4e76ff4f58b34deec21b
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Tue Oct 7 23:23:21 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
libgfs2: metafs_path improvements
Switch the metafs_path field in struct gfs2_sbd to be a pointer instead
of a char[PATH_MAX] and use strdup to set it in mount_gfs2_meta. Also
rework the metafs mounting and cleanup code to back out more robustly on
errors and set/reset signal handlers through a single function.
Note the SIGSEGV handler has been removed: when a SIGSEGV handler
returns, execution restarts at the bad instruction which triggered it,
so handling it could only ever cause an infinite loop.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/libgfs2/libgfs2.h | 2 +-
gfs2/libgfs2/misc.c | 66 +++++++++++++++++++++++++++++-------------------
2 files changed, 41 insertions(+), 27 deletions(-)
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
index 84aeaf0..9ea7eb9 100644
--- a/gfs2/libgfs2/libgfs2.h
+++ b/gfs2/libgfs2/libgfs2.h
@@ -327,7 +327,7 @@ struct gfs2_sbd {
struct master_dir md;
int metafs_fd;
- char metafs_path[PATH_MAX]; /* where metafs is mounted */
+ char *metafs_path; /* where metafs is mounted */
struct special_blocks eattr_blocks;
uint64_t rg_one_length;
diff --git a/gfs2/libgfs2/misc.c b/gfs2/libgfs2/misc.c
index 6feccad..c72af55 100644
--- a/gfs2/libgfs2/misc.c
+++ b/gfs2/libgfs2/misc.c
@@ -210,8 +210,10 @@ static int lock_for_admin(struct gfs2_sbd *sdp)
return -1;
error = flock(sdp->metafs_fd, LOCK_EX);
- if (error)
+ if (error) {
+ close(sdp->metafs_fd);
return -1;
+ }
if (cfg_debug)
printf("Got it.\n");
return 0;
@@ -222,40 +224,57 @@ static void sighandler(int error)
metafs_interrupted = 1;
}
-int mount_gfs2_meta(struct gfs2_sbd *sdp, const char *path)
+static void setsigs(void (*handler)(int))
{
- int ret;
- struct sigaction sa = { .sa_handler = &sighandler };
-
- memset(sdp->metafs_path, 0, PATH_MAX);
- snprintf(sdp->metafs_path, PATH_MAX - 1, "/tmp/.gfs2meta.XXXXXX");
-
- if(!mkdtemp(sdp->metafs_path))
- return -1;
+ struct sigaction sa = { .sa_handler = handler };
sigaction(SIGINT, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGABRT, &sa, NULL);
- sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGCONT, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
- ret = mount(path, sdp->metafs_path, "gfs2meta", 0, NULL);
- if (ret) {
- rmdir(sdp->metafs_path);
+}
+
+int mount_gfs2_meta(struct gfs2_sbd *sdp, const char *path)
+{
+ int ret;
+
+ sdp->metafs_path = strdup("/tmp/.gfs2meta.XXXXXX");
+ if (sdp->metafs_path == NULL)
return -1;
- }
+
+ if(!mkdtemp(sdp->metafs_path))
+ goto err_free;
+
+ setsigs(sighandler);
+
+ ret = mount(path, sdp->metafs_path, "gfs2meta", 0, NULL);
+ if (ret)
+ goto err_rmdir;
+
if (lock_for_admin(sdp))
- return -1;
+ goto err_umount;
+
return 0;
+
+err_umount:
+ if (umount(sdp->metafs_path))
+ fprintf(stderr, "Could not unmount %s: %s\n", sdp->metafs_path, strerror(errno));
+ setsigs(SIG_DFL);
+err_rmdir:
+ rmdir(sdp->metafs_path);
+err_free:
+ free(sdp->metafs_path);
+ sdp->metafs_path = NULL;
+ return -1;
}
void cleanup_metafs(struct gfs2_sbd *sdp)
{
int ret;
- struct sigaction sa = { .sa_handler = SIG_DFL };
if (sdp->metafs_fd <= 0)
return;
@@ -268,14 +287,9 @@ void cleanup_metafs(struct gfs2_sbd *sdp)
sdp->metafs_path, strerror(errno));
else
rmdir(sdp->metafs_path);
- sigaction(SIGINT, &sa, NULL);
- sigaction(SIGILL, &sa, NULL);
- sigaction(SIGTERM, &sa, NULL);
- sigaction(SIGHUP, &sa, NULL);
- sigaction(SIGABRT, &sa, NULL);
- sigaction(SIGSEGV, &sa, NULL);
- sigaction(SIGCONT, &sa, NULL);
- sigaction(SIGUSR1, &sa, NULL);
- sigaction(SIGUSR2, &sa, NULL);
+
+ setsigs(SIG_DFL);
metafs_interrupted = 0;
+ free(sdp->metafs_path);
+ sdp->metafs_path = NULL;
}
9 years
gfs2-utils: RHEL7 - libgfs2: Remove path_name from struct gfs2_sbd
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=2e94ddbd...
Commit: 2e94ddbdf1f60a0099cc4e76ff4f58b34deec21b
Parent: 64ae906eb5b681a2a2cf56502ba6ba5269fb3871
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Tue Oct 7 21:23:29 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
libgfs2: Remove path_name from struct gfs2_sbd
This is another gfs2_sbd field which needn't be kept in libgfs2. In the
gfs2_jadd case, create a new structure in which to store command
options, including the mount point path.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/libgfs2/libgfs2.h | 4 +--
gfs2/libgfs2/misc.c | 4 +-
gfs2/mkfs/main_grow.c | 9 +++----
gfs2/mkfs/main_jadd.c | 53 ++++++++++++++++++++++++-----------------------
4 files changed, 34 insertions(+), 36 deletions(-)
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
index bcd4201..84aeaf0 100644
--- a/gfs2/libgfs2/libgfs2.h
+++ b/gfs2/libgfs2/libgfs2.h
@@ -281,8 +281,6 @@ struct gfs2_sbd {
unsigned int rgsize; /* Size of resource groups (in MB) */
unsigned int qcsize; /* Size of quota change files (in MB) */
- char *path_name;
-
/* Constants */
uint32_t sd_fsb2bb;
@@ -680,7 +678,7 @@ extern int lgfs2_open_mnt_dev(const char *path, int flags, struct mntent **mnt);
extern int lgfs2_open_mnt_dir(const char *path, int flags, struct mntent **mnt);
extern int find_gfs2_meta(struct gfs2_sbd *sdp);
extern int dir_exists(const char *dir);
-extern int mount_gfs2_meta(struct gfs2_sbd *sdp);
+extern int mount_gfs2_meta(struct gfs2_sbd *sdp, const char *path);
extern void cleanup_metafs(struct gfs2_sbd *sdp);
extern int is_fsname(char *name);
diff --git a/gfs2/libgfs2/misc.c b/gfs2/libgfs2/misc.c
index a66ca4f..6feccad 100644
--- a/gfs2/libgfs2/misc.c
+++ b/gfs2/libgfs2/misc.c
@@ -222,7 +222,7 @@ static void sighandler(int error)
metafs_interrupted = 1;
}
-int mount_gfs2_meta(struct gfs2_sbd *sdp)
+int mount_gfs2_meta(struct gfs2_sbd *sdp, const char *path)
{
int ret;
struct sigaction sa = { .sa_handler = &sighandler };
@@ -242,7 +242,7 @@ int mount_gfs2_meta(struct gfs2_sbd *sdp)
sigaction(SIGCONT, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
- ret = mount(sdp->path_name, sdp->metafs_path, "gfs2meta", 0, NULL);
+ ret = mount(path, sdp->metafs_path, "gfs2meta", 0, NULL);
if (ret) {
rmdir(sdp->metafs_path);
return -1;
diff --git a/gfs2/mkfs/main_grow.c b/gfs2/mkfs/main_grow.c
index 4c1225c..79ba56a 100644
--- a/gfs2/mkfs/main_grow.c
+++ b/gfs2/mkfs/main_grow.c
@@ -306,9 +306,9 @@ trunc:
/**
* print_info - Print out various bits of (interesting?) information
*/
-static void print_info(struct gfs2_sbd *sdp, char *device)
+static void print_info(struct gfs2_sbd *sdp, char *device, char *mnt_path)
{
- log_notice("FS: %-25s%s\n", _("Mount point:"), sdp->path_name);
+ log_notice("FS: %-25s%s\n", _("Mount point:"), mnt_path);
log_notice("FS: %-25s%s\n", _("Device:"), device);
log_notice("FS: %-25s%llu (0x%llx)\n", _("Size:"),
(unsigned long long)fssize, (unsigned long long)fssize);
@@ -352,7 +352,6 @@ void main_grow(int argc, char *argv[])
fprintf(stderr, _("%s: not a mounted gfs2 file system\n"), argv[optind]);
continue;
}
- sdp->path_name = mnt->mnt_dir;
if (lgfs2_get_dev_info(sdp->device_fd, &sdp->dinfo) < 0) {
perror(mnt->mnt_fsname);
@@ -373,7 +372,7 @@ void main_grow(int argc, char *argv[])
exit(EXIT_FAILURE);
}
fix_device_geometry(sdp);
- if (mount_gfs2_meta(sdp)) {
+ if (mount_gfs2_meta(sdp, mnt->mnt_dir)) {
perror(_("Failed to mount GFS2 meta file system"));
exit(EXIT_FAILURE);
}
@@ -429,7 +428,7 @@ void main_grow(int argc, char *argv[])
error = -1;
goto out;
}
- print_info(sdp, mnt->mnt_fsname);
+ print_info(sdp, mnt->mnt_fsname, mnt->mnt_dir);
rgcount = initialize_new_portion(sdp, rgs);
if (rgcount == 0 || metafs_interrupted)
goto out;
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
index d468a6f..4968ec9 100644
--- a/gfs2/mkfs/main_jadd.c
+++ b/gfs2/mkfs/main_jadd.c
@@ -27,8 +27,11 @@
#define BUF_SIZE 4096
#define RANDOM(values) ((values) * (random() / (RAND_MAX + 1.0)))
-static int quiet = 0;
-static int debug = 0;
+struct jadd_opts {
+ char *path;
+ unsigned quiet:1;
+ unsigned debug:1;
+};
static void
make_jdata(int fd, const char *value)
@@ -116,7 +119,7 @@ static void print_usage(const char *prog_name)
*
*/
-static void decode_arguments(int argc, char *argv[], struct gfs2_sbd *sdp)
+static void decode_arguments(int argc, char *argv[], struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
int cont = TRUE;
int optchar;
@@ -129,7 +132,7 @@ static void decode_arguments(int argc, char *argv[], struct gfs2_sbd *sdp)
sdp->qcsize = atoi(optarg);
break;
case 'D':
- debug = 1;
+ opts->debug = 1;
lgfs2_set_debug(1);
break;
case 'h':
@@ -143,7 +146,7 @@ static void decode_arguments(int argc, char *argv[], struct gfs2_sbd *sdp)
sdp->md.journals = atoi(optarg);
break;
case 'q':
- quiet = 1;
+ opts->quiet = 1;
break;
case 'V':
printf("gfs2_jadd %s (built %s %s)\n", VERSION,
@@ -166,7 +169,7 @@ static void decode_arguments(int argc, char *argv[], struct gfs2_sbd *sdp)
}
if (optind < argc) {
- sdp->path_name = argv[optind];
+ opts->path = argv[optind];
optind++;
} else
die( _("no path specified (try -h for help)\n"));
@@ -174,13 +177,13 @@ static void decode_arguments(int argc, char *argv[], struct gfs2_sbd *sdp)
if (optind < argc)
die( _("Unrecognized argument: %s\n"), argv[optind]);
- if (debug) {
+ if (opts->debug) {
printf( _("Command Line Arguments:\n"));
printf(" qcsize = %u\n", sdp->qcsize);
printf(" jsize = %u\n", sdp->jsize);
printf(" journals = %u\n", sdp->md.journals);
- printf(" quiet = %d\n", quiet);
- printf(" path = %s\n", sdp->path_name);
+ printf(" quiet = %u\n", opts->quiet);
+ printf(" path = %s\n", opts->path);
}
}
@@ -200,14 +203,14 @@ static void verify_arguments(struct gfs2_sbd *sdp)
*
*/
-static void print_results(struct gfs2_sbd *sdp)
+static void print_results(struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
- if (debug)
+ if (opts->debug)
printf("\n");
- else if (quiet)
+ else if (opts->quiet)
return;
- printf( _("Filesystem: %s\n"), sdp->path_name);
+ printf( _("Filesystem: %s\n"), opts->path);
printf( _("Old Journals: %u\n"), sdp->orig_journals);
printf( _("New Journals: %u\n"), sdp->md.journals);
@@ -364,12 +367,11 @@ add_qc(struct gfs2_sbd *sdp)
}
}
-static void
-gather_info(struct gfs2_sbd *sdp)
+static void gather_info(struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
struct statfs statbuf;
- if (statfs(sdp->path_name, &statbuf) < 0) {
- perror(sdp->path_name);
+ if (statfs(opts->path, &statbuf) < 0) {
+ perror(opts->path);
exit(EXIT_FAILURE);
}
sdp->bsize = statbuf.f_bsize;
@@ -485,6 +487,7 @@ add_j(struct gfs2_sbd *sdp)
void main_jadd(int argc, char *argv[])
{
+ struct jadd_opts opts = {0};
struct gfs2_sbd sbd, *sdp = &sbd;
struct mntent *mnt;
unsigned int total;
@@ -494,23 +497,21 @@ void main_jadd(int argc, char *argv[])
sdp->qcsize = GFS2_DEFAULT_QCSIZE;
sdp->md.journals = 1;
- decode_arguments(argc, argv, sdp);
+ decode_arguments(argc, argv, sdp, &opts);
verify_arguments(sdp);
- sbd.path_fd = lgfs2_open_mnt_dir(sbd.path_name, O_RDONLY|O_CLOEXEC, &mnt);
+ sbd.path_fd = lgfs2_open_mnt_dir(opts.path, O_RDONLY|O_CLOEXEC, &mnt);
if (sbd.path_fd < 0) {
- fprintf(stderr, _("Error looking up mount '%s': %s\n"), sbd.path_name, strerror(errno));
+ fprintf(stderr, _("Error looking up mount '%s': %s\n"), opts.path, strerror(errno));
exit(EXIT_FAILURE);
}
if (mnt == NULL) {
- fprintf(stderr, _("%s: not a mounted gfs2 file system\n"), sbd.path_name);
+ fprintf(stderr, _("%s: not a mounted gfs2 file system\n"), opts.path);
exit(EXIT_FAILURE);
}
- sbd.path_name = mnt->mnt_dir;
-
- gather_info(sdp);
+ gather_info(sdp, &opts);
- if (mount_gfs2_meta(sdp)) {
+ if (mount_gfs2_meta(sdp, mnt->mnt_dir)) {
perror("GFS2 metafs");
exit(EXIT_FAILURE);
}
@@ -538,5 +539,5 @@ void main_jadd(int argc, char *argv[])
close(sdp->path_fd);
cleanup_metafs(sdp);
sync();
- print_results(sdp);
+ print_results(sdp, &opts);
}
9 years
gfs2-utils: RHEL7 - libgfs2: Remove device_name from struct gfs2_sbd
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=64ae906e...
Commit: 64ae906eb5b681a2a2cf56502ba6ba5269fb3871
Parent: 87b61dcd77566240e57d00765edd533be1b6b8bb
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Tue Oct 7 20:30:08 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
libgfs2: Remove device_name from struct gfs2_sbd
Remove this field from struct gfs2_sbd because,
* It arbitrarily limits the path length
* It bloats the struct by PATH_MAX (commonly 4096) chars
* It isn't used by libgfs2 itself
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/edit/hexedit.c | 6 ++----
gfs2/edit/savemeta.c | 3 ++-
gfs2/fsck/initialize.c | 2 +-
gfs2/libgfs2/libgfs2.h | 1 -
gfs2/mkfs/main_grow.c | 10 ++++------
gfs2/mkfs/main_jadd.c | 1 -
6 files changed, 9 insertions(+), 14 deletions(-)
diff --git a/gfs2/edit/hexedit.c b/gfs2/edit/hexedit.c
index b316065..dd4b625 100644
--- a/gfs2/edit/hexedit.c
+++ b/gfs2/edit/hexedit.c
@@ -1000,7 +1000,7 @@ static void read_superblock(int fd)
if (!sbd.bsize)
sbd.bsize = GFS2_DEFAULT_BSIZE;
if (lgfs2_get_dev_info(fd, &sbd.dinfo)) {
- perror(sbd.device_name);
+ perror(device);
exit(-1);
}
if(compute_constants(&sbd)) {
@@ -1401,8 +1401,7 @@ uint64_t check_keywords(const char *kword)
if (sbd.gfs1)
fprintf(stderr, "This is GFS1; there's no master directory.\n");
else if (!sbd.sd_sb.sb_master_dir.no_addr) {
- fprintf(stderr, "GFS2 master directory not found on %s\n",
- sbd.device_name);
+ fprintf(stderr, "GFS2 master directory not found on %s\n", device);
exit(-1);
} else
blk = sbd.sd_sb.sb_master_dir.no_addr;
@@ -2541,7 +2540,6 @@ int main(int argc, char *argv[])
read_superblock(fd);
max_block = lseek(fd, 0, SEEK_END) / sbd.bsize;
- strcpy(sbd.device_name, device);
if (sbd.gfs1)
edit_row[GFS2_MODE]++;
else
diff --git a/gfs2/edit/savemeta.c b/gfs2/edit/savemeta.c
index 5147ee5..5d30d28 100644
--- a/gfs2/edit/savemeta.c
+++ b/gfs2/edit/savemeta.c
@@ -62,6 +62,7 @@ static uint64_t journal_blocks[MAX_JOURNALS_SAVED];
static uint64_t gfs1_journal_size = 0; /* in blocks */
static int journals_found = 0;
int print_level = MSG_NOTICE;
+extern char *device;
static int block_is_a_journal(void)
{
@@ -737,7 +738,7 @@ void savemeta(char *out_fn, int saveoption, int gziplevel)
if (!sbd.gfs1)
sbd.bsize = GFS2_DEFAULT_BSIZE;
if (lgfs2_get_dev_info(sbd.device_fd, &sbd.dinfo)) {
- perror(sbd.device_name);
+ perror(device);
exit(-1);
}
fix_device_geometry(&sbd);
diff --git a/gfs2/fsck/initialize.c b/gfs2/fsck/initialize.c
index 4dedec2..4e52262 100644
--- a/gfs2/fsck/initialize.c
+++ b/gfs2/fsck/initialize.c
@@ -1608,7 +1608,7 @@ int initialize(struct gfs2_sbd *sdp, int force_check, int preen,
}
if (lgfs2_get_dev_info(sdp->device_fd, &sdp->dinfo)) {
- perror(sdp->device_name);
+ perror(opts.device);
return FSCK_ERROR;
}
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
index 26ad2c6..bcd4201 100644
--- a/gfs2/libgfs2/libgfs2.h
+++ b/gfs2/libgfs2/libgfs2.h
@@ -281,7 +281,6 @@ struct gfs2_sbd {
unsigned int rgsize; /* Size of resource groups (in MB) */
unsigned int qcsize; /* Size of quota change files (in MB) */
- char device_name[PATH_MAX];
char *path_name;
/* Constants */
diff --git a/gfs2/mkfs/main_grow.c b/gfs2/mkfs/main_grow.c
index c4f5055..4c1225c 100644
--- a/gfs2/mkfs/main_grow.c
+++ b/gfs2/mkfs/main_grow.c
@@ -305,12 +305,11 @@ trunc:
/**
* print_info - Print out various bits of (interesting?) information
- *
*/
-static void print_info(struct gfs2_sbd *sdp)
+static void print_info(struct gfs2_sbd *sdp, char *device)
{
log_notice("FS: %-25s%s\n", _("Mount point:"), sdp->path_name);
- log_notice("FS: %-25s%s\n", _("Device:"), sdp->device_name);
+ log_notice("FS: %-25s%s\n", _("Device:"), device);
log_notice("FS: %-25s%llu (0x%llx)\n", _("Size:"),
(unsigned long long)fssize, (unsigned long long)fssize);
log_notice("FS: %-25s%u (0x%x)\n", _("New resource group size:"), rgsize, rgsize);
@@ -354,10 +353,9 @@ void main_grow(int argc, char *argv[])
continue;
}
sdp->path_name = mnt->mnt_dir;
- strncpy(sdp->device_name, mnt->mnt_fsname, PATH_MAX - 1);
if (lgfs2_get_dev_info(sdp->device_fd, &sdp->dinfo) < 0) {
- perror(sdp->device_name);
+ perror(mnt->mnt_fsname);
exit(EXIT_FAILURE);
}
sdp->sd_sb.sb_bsize = GFS2_DEFAULT_BSIZE;
@@ -431,7 +429,7 @@ void main_grow(int argc, char *argv[])
error = -1;
goto out;
}
- print_info(sdp);
+ print_info(sdp, mnt->mnt_fsname);
rgcount = initialize_new_portion(sdp, rgs);
if (rgcount == 0 || metafs_interrupted)
goto out;
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
index 4338e8d..d468a6f 100644
--- a/gfs2/mkfs/main_jadd.c
+++ b/gfs2/mkfs/main_jadd.c
@@ -507,7 +507,6 @@ void main_jadd(int argc, char *argv[])
exit(EXIT_FAILURE);
}
sbd.path_name = mnt->mnt_dir;
- strncpy(sbd.device_name, mnt->mnt_fsname, PATH_MAX - 1);
gather_info(sdp);
9 years
gfs2-utils: RHEL7 - gfs2l: Switch to lgfs2_field_assign
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=87b61dcd...
Commit: 87b61dcd77566240e57d00765edd533be1b6b8bb
Parent: 6113ce4496f12abe77caae9b3263e8cfcb7a1372
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Tue Oct 7 13:06:28 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
gfs2l: Switch to lgfs2_field_assign
The new lgfs2_field_assign function was based on code in gfs2l, which
can now be converted to use the new libgfs2 functions.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/libgfs2/lang.c | 78 +++++++++++++++------------------------------------
gfs2/libgfs2/meta.c | 6 +++-
2 files changed, 28 insertions(+), 56 deletions(-)
diff --git a/gfs2/libgfs2/lang.c b/gfs2/libgfs2/lang.c
index b4a743f..13333a1 100644
--- a/gfs2/libgfs2/lang.c
+++ b/gfs2/libgfs2/lang.c
@@ -471,7 +471,7 @@ invalid:
static int ast_field_set(struct gfs2_buffer_head *bh, const struct lgfs2_metafield *field,
struct ast_node *val)
{
- char *fieldp = (char *)bh->iov.iov_base + field->offset;
+ int err = 0;
if (field->flags & LGFS2_MFF_UUID) {
uint8_t uuid[16];
@@ -479,56 +479,21 @@ static int ast_field_set(struct gfs2_buffer_head *bh, const struct lgfs2_metafie
if (ret != AST_INTERP_SUCCESS)
return ret;
-
- memcpy(fieldp, uuid, 16);
- bmodified(bh);
- return AST_INTERP_SUCCESS;
+ err = lgfs2_field_assign(bh->b_data, field, uuid);
+ } else if (field->flags & LGFS2_MFF_STRING) {
+ err = lgfs2_field_assign(bh->b_data, field, val->ast_str);
+ } else {
+ err = lgfs2_field_assign(bh->b_data, field, &val->ast_num);
}
- if ((field->flags & LGFS2_MFF_STRING) && strlen(val->ast_str) > field->length) {
- fprintf(stderr, "String '%s' is too long for field '%s'\n", val->ast_str, field->name);
+ if (err) {
+ fprintf(stderr, "Invalid field assignment: %s (size %d) = %s\n",
+ field->name, field->length, val->ast_text);
return AST_INTERP_INVAL;
}
- if (field->flags & (LGFS2_MFF_STRING|LGFS2_MFF_UUID)) {
- strncpy(fieldp, val->ast_str, field->length - 1);
- fieldp[field->length - 1] = '\0';
- bmodified(bh);
- return AST_INTERP_SUCCESS;
- } else {
- // Numeric fields
- switch(field->length) {
- case 1:
- if (val->ast_num > UINT8_MAX)
- break;
- *fieldp = (uint8_t)val->ast_num;
- bmodified(bh);
- return AST_INTERP_SUCCESS;
- case 2:
- if (val->ast_num > UINT16_MAX)
- break;
- *(uint16_t *)fieldp = cpu_to_be16((uint16_t)val->ast_num);
- bmodified(bh);
- return AST_INTERP_SUCCESS;
- case 4:
- if (val->ast_num > UINT32_MAX)
- break;
- *(uint32_t *)fieldp = cpu_to_be32((uint32_t)val->ast_num);
- bmodified(bh);
- return AST_INTERP_SUCCESS;
- case 8:
- *(uint64_t *)fieldp = cpu_to_be64((uint64_t)val->ast_num);
- bmodified(bh);
- return AST_INTERP_SUCCESS;
- default:
- // This should never happen
- return AST_INTERP_ERR;
- }
- }
-
- fprintf(stderr, "Invalid field assignment: %s (size %d) = %s\n",
- field->name, field->length, val->ast_text);
- return AST_INTERP_INVAL;
+ bmodified(bh);
+ return AST_INTERP_SUCCESS;
}
static const struct lgfs2_metadata *lang_find_mtype(struct ast_node *node, struct gfs2_buffer_head *bh, unsigned ver)
@@ -558,7 +523,6 @@ static struct lgfs2_lang_result *ast_interp_set(struct lgfs2_lang_state *state,
struct ast_node *fieldspec;
struct ast_node *fieldname;
struct ast_node *fieldval;
- int i = 0;
int ret = 0;
unsigned ver = sbd->gfs1 ? LGFS2_MD_GFS1 : LGFS2_MD_GFS2;
@@ -592,17 +556,21 @@ static struct lgfs2_lang_result *ast_interp_set(struct lgfs2_lang_state *state,
for (fieldspec = lookup->ast_right;
fieldspec != NULL && fieldspec->ast_type == AST_EX_FIELDSPEC;
fieldspec = fieldspec->ast_left) {
+ const struct lgfs2_metafield *mfield;
fieldname = fieldspec->ast_right;
fieldval = fieldname->ast_right;
- for (i = 0; i < result->lr_mtype->nfields; i++) {
- if (!strcmp(result->lr_mtype->fields[i].name, fieldname->ast_str)) {
- ret = ast_field_set(result->lr_bh, &result->lr_mtype->fields[i], fieldval);
- if (ret != AST_INTERP_SUCCESS) {
- goto out_err;
- }
- break;
- }
+
+ mfield = lgfs2_find_mfield_name(fieldname->ast_str, result->lr_mtype);
+ if (mfield == NULL) {
+ fprintf(stderr, "No field '%s' found in '%s'\n",
+ fieldname->ast_str, result->lr_mtype->name);
+ goto out_err;
+ }
+
+ ret = ast_field_set(result->lr_bh, mfield, fieldval);
+ if (ret != AST_INTERP_SUCCESS) {
+ goto out_err;
}
}
diff --git a/gfs2/libgfs2/meta.c b/gfs2/libgfs2/meta.c
index f0eed3d..79de64d 100644
--- a/gfs2/libgfs2/meta.c
+++ b/gfs2/libgfs2/meta.c
@@ -901,7 +901,12 @@ int lgfs2_field_assign(char *blk, const struct lgfs2_metafield *field, const voi
return 0;
}
+ errno = EINVAL;
if (field->flags & LGFS2_MFF_STRING) {
+ size_t len = strnlen(val, field->length);
+
+ if (len >= field->length)
+ return 1;
strncpy(fieldp, val, field->length - 1);
fieldp[field->length - 1] = '\0';
return 0;
@@ -925,6 +930,5 @@ int lgfs2_field_assign(char *blk, const struct lgfs2_metafield *field, const voi
break;
}
- errno = EINVAL;
return 1;
}
9 years
gfs2-utils: RHEL7 - gfs2_edit: Use metadata description to print and assign fields
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=6113ce44...
Commit: 6113ce4496f12abe77caae9b3263e8cfcb7a1372
Parent: 237716c40d0b2152573431f0d3305cab07118c8b
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Tue Oct 7 11:25:10 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
gfs2_edit: Use metadata description to print and assign fields
Replace the large number of separate field assignment and printing
functions in gfs2_edit with the new functions in libgfs2. This reduces
the amount of code involved dramatically and fixes three coverity
complaints related to the use of the 'checkassigns' macro.
This relates to the form: gfs2_edit -p 1234 field foo [bar] /dev/baz
The 'device' variable has been changed from char[NAME_MAX] to a pointer
to an argv member, so that now we can check whether the optional [bar]
argument is missing properly, i.e. no longer assume the device path will
begin with '/'. It also means the device name is no longer limited to
255 chars.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/edit/gfs2hex.c | 1 -
gfs2/edit/hexedit.c | 465 +++++----------------------------------------------
gfs2/edit/hexedit.h | 1 -
3 files changed, 43 insertions(+), 424 deletions(-)
diff --git a/gfs2/edit/gfs2hex.c b/gfs2/edit/gfs2hex.c
index b6075f4..dc6bb8f 100644
--- a/gfs2/edit/gfs2hex.c
+++ b/gfs2/edit/gfs2hex.c
@@ -48,7 +48,6 @@ struct gfs2_sbd sbd;
uint64_t starting_blk;
struct blkstack_info blockstack[BLOCK_STACK_SIZE];
int identify = FALSE;
-char device[NAME_MAX];
uint64_t max_block = 0;
int start_row[DMODES], end_row[DMODES], lines_per_row[DMODES];
struct gfs_sb *sbd1;
diff --git a/gfs2/edit/hexedit.c b/gfs2/edit/hexedit.c
index 0982f7b..b316065 100644
--- a/gfs2/edit/hexedit.c
+++ b/gfs2/edit/hexedit.c
@@ -44,291 +44,7 @@ int pgnum;
int details = 0;
long int gziplevel = 9;
static int termcols;
-
-/* for assigning numeric fields: */
-#define checkassign(strfield, struct, member, value) do { \
- if (strcmp(#member, strfield) == 0) { \
- struct->member = (typeof(struct->member)) value; \
- return 0; \
- } \
- } while(0)
-
-/* for assigning string fields: */
-#define checkassigns(strfield, struct, member, val) do { \
- if (strcmp(#member, strfield) == 0) { \
- memset(struct->member, 0, sizeof(struct->member)); \
- strncpy((char *)struct->member, (char *)val, \
- sizeof(struct->member)); \
- return 0; \
- } \
- } while(0)
-
-/* for printing numeric fields: */
-#define checkprint(strfield, struct, member) do { \
- if (strcmp(#member, strfield) == 0) { \
- if (dmode == HEX_MODE) \
- printf("0x%llx\n", \
- (unsigned long long)struct->member); \
- else \
- printf("%llu\n", \
- (unsigned long long)struct->member); \
- return 0; \
- } \
- } while(0)
-
-/* for printing string fields: */
-#define checkprints(strfield, struct, member) do { \
- if (strcmp(#member, strfield) == 0) { \
- printf("%s\n", struct->member); \
- return 0; \
- } \
- } while(0)
-
-
-/* -------------------------------------------------------------------------
- * field-related functions:
- * ------------------------------------------------------------------------- */
-static int gfs2_sb_printval(struct gfs2_sb *lsb, const char *strfield)
-{
- checkprint(strfield, lsb, sb_fs_format);
- checkprint(strfield, lsb, sb_multihost_format);
- checkprint(strfield, lsb, __pad0);
- checkprint(strfield, lsb, sb_bsize);
- checkprint(strfield, lsb, sb_bsize_shift);
- checkprint(strfield, lsb, __pad1);
- checkprint(strfield, lsb, sb_master_dir.no_addr);
- checkprint(strfield, lsb, __pad2.no_addr);
- checkprint(strfield, lsb, sb_root_dir.no_addr);
- checkprints(strfield, lsb, sb_lockproto);
- checkprints(strfield, lsb, sb_locktable);
- checkprint(strfield, lsb, __pad3.no_addr);
- checkprint(strfield, lsb, __pad4.no_addr);
-#ifdef GFS2_HAS_UUID
- if (strcmp(strfield, "sb_uuid") == 0) {
- printf("%s\n", str_uuid(lsb->sb_uuid));
- return 0;
- }
-#endif
-
- return -1;
-}
-
-static int gfs2_sb_assignval(struct gfs2_sb *lsb, const char *strfield,
- uint64_t value)
-{
- checkassign(strfield, lsb, sb_fs_format, value);
- checkassign(strfield, lsb, sb_multihost_format, value);
- checkassign(strfield, lsb, __pad0, value);
- checkassign(strfield, lsb, sb_bsize, value);
- checkassign(strfield, lsb, sb_bsize_shift, value);
- checkassign(strfield, lsb, __pad1, value);
- checkassign(strfield, lsb, sb_master_dir.no_addr, value);
- checkassign(strfield, lsb, __pad2.no_addr, value);
- checkassign(strfield, lsb, sb_root_dir.no_addr, value);
- checkassign(strfield, lsb, __pad3.no_addr, value);
- checkassign(strfield, lsb, __pad4.no_addr, value);
-
- return -1;
-}
-
-static int gfs2_sb_assigns(struct gfs2_sb *lsb, const char *strfield,
- const char *val)
-{
- checkassigns(strfield, lsb, sb_lockproto, val);
- checkassigns(strfield, lsb, sb_locktable, val);
-#ifdef GFS2_HAS_UUID
- checkassigns(strfield, lsb, sb_uuid, val);
-#endif
- return -1;
-}
-
-static int gfs2_dinode_printval(struct gfs2_dinode *dip, const char *strfield)
-{
- checkprint(strfield, dip, di_mode);
- checkprint(strfield, dip, di_uid);
- checkprint(strfield, dip, di_gid);
- checkprint(strfield, dip, di_nlink);
- checkprint(strfield, dip, di_size);
- checkprint(strfield, dip, di_blocks);
- checkprint(strfield, dip, di_atime);
- checkprint(strfield, dip, di_mtime);
- checkprint(strfield, dip, di_ctime);
- checkprint(strfield, dip, di_major);
- checkprint(strfield, dip, di_minor);
- checkprint(strfield, dip, di_goal_meta);
- checkprint(strfield, dip, di_goal_data);
- checkprint(strfield, dip, di_flags);
- checkprint(strfield, dip, di_payload_format);
- checkprint(strfield, dip, di_height);
- checkprint(strfield, dip, di_depth);
- checkprint(strfield, dip, di_entries);
- checkprint(strfield, dip, di_eattr);
-
- return -1;
-}
-
-static int gfs2_dinode_assignval(struct gfs2_dinode *dia, const char *strfield,
- uint64_t value)
-{
- checkassign(strfield, dia, di_mode, value);
- checkassign(strfield, dia, di_uid, value);
- checkassign(strfield, dia, di_gid, value);
- checkassign(strfield, dia, di_nlink, value);
- checkassign(strfield, dia, di_size, value);
- checkassign(strfield, dia, di_blocks, value);
- checkassign(strfield, dia, di_atime, value);
- checkassign(strfield, dia, di_mtime, value);
- checkassign(strfield, dia, di_ctime, value);
- checkassign(strfield, dia, di_major, value);
- checkassign(strfield, dia, di_minor, value);
- checkassign(strfield, dia, di_goal_meta, value);
- checkassign(strfield, dia, di_goal_data, value);
- checkassign(strfield, dia, di_flags, value);
- checkassign(strfield, dia, di_payload_format, value);
- checkassign(strfield, dia, di_height, value);
- checkassign(strfield, dia, di_depth, value);
- checkassign(strfield, dia, di_entries, value);
- checkassign(strfield, dia, di_eattr, value);
-
- return -1;
-}
-
-static int gfs2_rgrp_printval(struct gfs2_rgrp *rg, const char *strfield)
-{
- checkprint(strfield, rg, rg_flags);
- checkprint(strfield, rg, rg_free);
- checkprint(strfield, rg, rg_dinodes);
-
- return -1;
-}
-
-static int gfs2_rgrp_assignval(struct gfs2_rgrp *rg, const char *strfield,
- uint64_t value)
-{
- checkassign(strfield, rg, rg_flags, value);
- checkassign(strfield, rg, rg_free, value);
- checkassign(strfield, rg, rg_dinodes, value);
-
- return -1;
-}
-
-static int gfs2_leaf_printval(struct gfs2_leaf *lf, const char *strfield)
-{
- checkprint(strfield, lf, lf_depth);
- checkprint(strfield, lf, lf_entries);
- checkprint(strfield, lf, lf_dirent_format);
- checkprint(strfield, lf, lf_next);
-#ifdef GFS2_HAS_LEAF_HINTS
- checkprint(strfield, lf, lf_inode);
- checkprint(strfield, lf, lf_dist);
- checkprint(strfield, lf, lf_nsec);
- checkprint(strfield, lf, lf_sec);
- checkprints(strfield, lf, lf_reserved2);
-#else
- checkprints(strfield, lf, lf_reserved);
-#endif
-
- return -1;
-}
-
-static int gfs2_leaf_assignval(struct gfs2_leaf *lf, const char *strfield,
- uint64_t value)
-{
- checkassign(strfield, lf, lf_depth, value);
- checkassign(strfield, lf, lf_entries, value);
- checkassign(strfield, lf, lf_dirent_format, value);
- checkassign(strfield, lf, lf_next, value);
-#ifdef GFS2_HAS_LEAF_HINTS
- checkassign(strfield, lf, lf_inode, value);
- checkassign(strfield, lf, lf_dist, value);
- checkassign(strfield, lf, lf_nsec, value);
- checkassign(strfield, lf, lf_sec, value);
-#endif
-
- return -1;
-}
-
-static int gfs2_leaf_assigns(struct gfs2_leaf *lf, const char *strfield,
- const char *val)
-{
- checkassigns(strfield, lf, lf_reserved, val);
-
- return -1;
-}
-
-static int gfs2_lh_printval(struct gfs2_log_header *lh, const char *strfield)
-{
- checkprint(strfield, lh, lh_sequence);
- checkprint(strfield, lh, lh_flags);
- checkprint(strfield, lh, lh_tail);
- checkprint(strfield, lh, lh_blkno);
- checkprint(strfield, lh, lh_hash);
-
- return -1;
-}
-
-static int gfs2_lh_assignval(struct gfs2_log_header *lh, const char *strfield,
- uint64_t value)
-{
- checkassign(strfield, lh, lh_sequence, value);
- checkassign(strfield, lh, lh_flags, value);
- checkassign(strfield, lh, lh_tail, value);
- checkassign(strfield, lh, lh_blkno, value);
- checkassign(strfield, lh, lh_hash, value);
-
- return -1;
-}
-
-static int gfs2_ld_printval(struct gfs2_log_descriptor *ld,
- const char *strfield)
-{
- checkprint(strfield, ld, ld_type);
- checkprint(strfield, ld, ld_length);
- checkprint(strfield, ld, ld_data1);
- checkprint(strfield, ld, ld_data2);
- checkprints(strfield, ld, ld_reserved);
-
- return -1;
-}
-
-static int gfs2_ld_assignval(struct gfs2_log_descriptor *ld,
- const char *strfield, uint64_t value)
-{
- checkassign(strfield, ld, ld_type, value);
- checkassign(strfield, ld, ld_length, value);
- checkassign(strfield, ld, ld_data1, value);
- checkassign(strfield, ld, ld_data2, value);
-
- return -1;
-}
-
-static int gfs2_ld_assigns(struct gfs2_log_descriptor *ld,
- const char *strfield, const char *val)
-{
- checkassigns(strfield, ld, ld_reserved, val);
-
- return -1;
-}
-
-static int gfs2_qc_printval(struct gfs2_quota_change *qc,
- const char *strfield)
-{
- checkprint(strfield, qc, qc_change);
- checkprint(strfield, qc, qc_flags);
- checkprint(strfield, qc, qc_id);
-
- return -1;
-}
-
-static int gfs2_qc_assignval(struct gfs2_quota_change *qc,
- const char *strfield, uint64_t value)
-{
- checkassign(strfield, qc, qc_change, value);
- checkassign(strfield, qc, qc_flags, value);
- checkassign(strfield, qc, qc_id, value);
-
- return -1;
-}
+char *device = NULL;
/* ------------------------------------------------------------------------- */
/* erase - clear the screen */
@@ -2071,152 +1787,59 @@ static void find_change_block_alloc(int *newval)
exit(0);
}
-/* ------------------------------------------------------------------------ */
-/* process request to print a certain field from a previously pushed block */
-/* ------------------------------------------------------------------------ */
+/**
+ * process request to print a certain field from a previously pushed block
+ */
static void process_field(const char *field, const char *nstr)
{
uint64_t fblock;
struct gfs2_buffer_head *rbh;
int type;
- struct gfs2_rgrp rg;
- struct gfs2_leaf leaf;
- struct gfs2_sb lsb;
- struct gfs2_log_header lh;
- struct gfs2_log_descriptor ld;
- struct gfs2_quota_change qc;
- int setval = 0, setstring = 0;
- uint64_t newval = 0;
-
- if (nstr[0] == '/') {
- setval = 0;
- } else if (nstr[0] == '0' && nstr[1] == 'x') {
- sscanf(nstr, "%"SCNx64, &newval);
- setval = 1;
- } else {
- newval = (uint64_t)atoll(nstr);
- setval = 1;
- }
- if (setval && newval == 0 && nstr[0] != '0')
- setstring = 1;
+ const struct lgfs2_metadata *mtype;
+ const struct lgfs2_metafield *mfield;
+
fblock = blockstack[blockhist % BLOCK_STACK_SIZE].block;
rbh = bread(&sbd, fblock);
type = get_block_type(rbh, NULL);
- switch (type) {
- case GFS2_METATYPE_SB:
- gfs2_sb_in(&lsb, rbh);
- if (setval) {
- if (setstring)
- gfs2_sb_assigns(&lsb, field, nstr);
- else
- gfs2_sb_assignval(&lsb, field, newval);
- gfs2_sb_out(&lsb, rbh->b_data);
- bmodified(rbh);
- if (!termlines)
- gfs2_sb_printval(&lsb, field);
- } else {
- if (!termlines && gfs2_sb_printval(&lsb, field))
- printf("Field '%s' not found.\n", field);
- }
- break;
- case GFS2_METATYPE_RG:
- gfs2_rgrp_in(&rg, rbh);
- if (setval) {
- gfs2_rgrp_assignval(&rg, field, newval);
- gfs2_rgrp_out_bh(&rg, rbh);
- if (!termlines)
- gfs2_rgrp_printval(&rg, field);
- } else {
- if (!termlines && gfs2_rgrp_printval(&rg, field))
- printf("Field '%s' not found.\n", field);
- }
- break;
- case GFS2_METATYPE_RB:
- if (!termlines)
- print_block_type(fblock, type,
- " which is not implemented");
- break;
- case GFS2_METATYPE_DI:
- gfs2_dinode_in(&di, rbh);
- if (setval) {
- gfs2_dinode_assignval(&di, field, newval);
- gfs2_dinode_out(&di, rbh);
- if (!termlines)
- gfs2_dinode_printval(&di, field);
- } else {
- if (!termlines && gfs2_dinode_printval(&di, field))
- printf("Field '%s' not found.\n", field);
- }
- break;
- case GFS2_METATYPE_IN:
- if (!setval && !setstring)
- print_block_type(fblock, type,
- " which is not implemented");
- break;
- case GFS2_METATYPE_LF:
- gfs2_leaf_in(&leaf, rbh);
- if (setval) {
- if (setstring)
- gfs2_leaf_assigns(&leaf, field, nstr);
- else
- gfs2_leaf_assignval(&leaf, field, newval);
- gfs2_leaf_out(&leaf, rbh);
- if (!termlines)
- gfs2_leaf_printval(&leaf, field);
- } else {
- if (!termlines && gfs2_leaf_printval(&leaf, field))
- printf("Field '%s' not found.\n", field);
- }
- break;
- case GFS2_METATYPE_LH:
- gfs2_log_header_in(&lh, rbh);
- if (setval) {
- gfs2_lh_assignval(&lh, field, newval);
- gfs2_log_header_out(&lh, rbh);
- if (!termlines)
- gfs2_lh_printval(&lh, field);
+
+ mtype = lgfs2_find_mtype(type, sbd.gfs1 ? LGFS2_MD_GFS1 : LGFS2_MD_GFS2);
+ if (mtype == NULL) {
+ fprintf(stderr, "Metadata type '%d' invalid\n", type);
+ exit(1);
+ }
+
+ mfield = lgfs2_find_mfield_name(field, mtype);
+ if (mfield == NULL) {
+ fprintf(stderr, "No field '%s' in block type '%s'\n", field, mtype->name);
+ exit(1);
+ }
+
+ if (nstr != device) {
+ int err = 0;
+ if (mfield->flags & (LGFS2_MFF_UUID|LGFS2_MFF_STRING)) {
+ err = lgfs2_field_assign(rbh->b_data, mfield, nstr);
} else {
- if (!termlines && gfs2_lh_printval(&lh, field))
- printf("Field '%s' not found.\n", field);
- }
- break;
- case GFS2_METATYPE_LD:
- gfs2_log_descriptor_in(&ld, rbh);
- if (setval) {
- if (setstring)
- gfs2_ld_assigns(&ld, field, nstr);
+ uint64_t val = 0;
+ err = sscanf(nstr, "%"SCNi64, &val);
+ if (err == 1)
+ err = lgfs2_field_assign(rbh->b_data, mfield, &val);
else
- gfs2_ld_assignval(&ld, field, newval);
- gfs2_log_descriptor_out(&ld, rbh);
- if (!termlines)
- gfs2_ld_printval(&ld, field);
- } else {
- if (!termlines && gfs2_ld_printval(&ld, field))
- printf("Field '%s' not found.\n", field);
+ err = -1;
}
- break;
- case GFS2_METATYPE_QC:
- gfs2_quota_change_in(&qc, rbh);
- if (setval) {
- gfs2_qc_assignval(&qc, field, newval);
- gfs2_quota_change_out(&qc, rbh);
- if (!termlines)
- gfs2_qc_printval(&qc, field);
- } else {
- if (!termlines && gfs2_qc_printval(&qc, field))
- printf("Field '%s' not found.\n", field);
+ if (err != 0) {
+ fprintf(stderr, "Could not set '%s' to '%s': %s\n", field, nstr,
+ strerror(errno));
+ exit(1);
}
- break;
- case GFS2_METATYPE_JD: /* journaled data */
- case GFS2_METATYPE_EA: /* extended attribute */
- case GFS2_METATYPE_ED: /* extended attribute */
- case GFS2_METATYPE_LB:
- default:
- if (!termlines)
- print_block_type(fblock, type,
- " which is not implemented");
- break;
+ bmodified(rbh);
}
+
+ if (!termlines) {
+ char str[GFS2_LOCKNAME_LEN] = "";
+ lgfs2_field_str(str, GFS2_LOCKNAME_LEN, rbh->b_data, mfield, (dmode == HEX_MODE));
+ printf("%s\n", str);
+ }
+
brelse(rbh);
fsync(sbd.device_fd);
exit(0);
@@ -2681,9 +2304,8 @@ static void parameterpass1(int argc, char *argv[], int i)
termlines = 0;
else if (!strcasecmp(argv[i], "-x"))
dmode = HEX_MODE;
- else if (!device[0] && strchr(argv[i],'/')) {
- strncpy(device, argv[i], NAME_MAX-1);
- device[NAME_MAX-1] = '\0';
+ else if (device == NULL && strchr(argv[i],'/')) {
+ device = argv[i];
}
}
@@ -2907,7 +2529,6 @@ int main(int argc, char *argv[])
edit_row[GFS2_MODE] = 10; /* Start off at root inode
pointer in superblock */
- memset(device, 0, sizeof(device));
termlines = 30; /* assume interactive mode until we find -p */
process_parameters(argc, argv, 0);
if (dmode == INIT_MODE)
diff --git a/gfs2/edit/hexedit.h b/gfs2/edit/hexedit.h
index f23c581..3e9ff5f 100644
--- a/gfs2/edit/hexedit.h
+++ b/gfs2/edit/hexedit.h
@@ -57,7 +57,6 @@ extern struct gfs2_dinode di;
extern int screen_chunk_size; /* how much of the 4K can fit on screen */
extern int gfs2_struct_type;
extern uint64_t block_in_mem;
-extern char device[NAME_MAX];
extern int identify;
extern int color_scheme;
extern WINDOW *wind;
9 years
gfs2-utils: RHEL7 - libgfs2: Add generic field assignment and print functions
by Andrew Price
Gitweb: http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=237716c4...
Commit: 237716c40d0b2152573431f0d3305cab07118c8b
Parent: 29daf41307107693525caa692f9e74639757f650
Author: Andrew Price <anprice(a)redhat.com>
AuthorDate: Tue Oct 7 11:19:18 2014 +0100
Committer: Andrew Price <anprice(a)redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100
libgfs2: Add generic field assignment and print functions
Add lgfs2_field_str() to convert the value of any metadata field to a
string, and lgfs2_field_assign() to assign any metadata field a value.
lgfs2_find_mfield_name() is also added to make it easier to look up a
lgfs2_metafield by its name.
Signed-off-by: Andrew Price <anprice(a)redhat.com>
---
gfs2/libgfs2/libgfs2.h | 3 ++
gfs2/libgfs2/meta.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 0 deletions(-)
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
index 23ab210..26ad2c6 100644
--- a/gfs2/libgfs2/libgfs2.h
+++ b/gfs2/libgfs2/libgfs2.h
@@ -373,6 +373,9 @@ extern const unsigned lgfs2_ld1_type_size;
extern int lgfs2_selfcheck(void);
extern const struct lgfs2_metadata *lgfs2_find_mtype(uint32_t mh_type, const unsigned versions);
extern const struct lgfs2_metadata *lgfs2_find_mtype_name(const char *name, const unsigned versions);
+extern const struct lgfs2_metafield *lgfs2_find_mfield_name(const char *name, const struct lgfs2_metadata *mtype);
+extern int lgfs2_field_str(char *str, const size_t size, const char *blk, const struct lgfs2_metafield *field, int hex);
+extern int lgfs2_field_assign(char *blk, const struct lgfs2_metafield *field, const void *val);
/* block_list.c */
diff --git a/gfs2/libgfs2/meta.c b/gfs2/libgfs2/meta.c
index 4305393..f0eed3d 100644
--- a/gfs2/libgfs2/meta.c
+++ b/gfs2/libgfs2/meta.c
@@ -734,6 +734,19 @@ const struct lgfs2_metadata lgfs2_metadata[] = {
const unsigned lgfs2_metadata_size = ARRAY_SIZE(lgfs2_metadata);
+const struct lgfs2_metafield *lgfs2_find_mfield_name(const char *name, const struct lgfs2_metadata *mtype)
+{
+ int j;
+ const struct lgfs2_metafield *f;
+
+ for (j = 0; j < mtype->nfields; j++) {
+ f = &mtype->fields[j];
+ if (strcmp(f->name, name) == 0)
+ return f;
+ }
+ return NULL;
+}
+
static int check_metadata_sizes(void)
{
unsigned offset;
@@ -844,3 +857,74 @@ const struct lgfs2_metadata *lgfs2_find_mtype_name(const char *name, const unsig
return NULL;
}
+
+int lgfs2_field_str(char *str, const size_t size, const char *blk, const struct lgfs2_metafield *field, int hex)
+{
+ const char *fieldp = blk + field->offset;
+
+ errno = EINVAL;
+ if (str == NULL)
+ return 1;
+
+ if (field->flags & LGFS2_MFF_UUID) {
+ snprintf(str, size, "%s", str_uuid((unsigned char *)fieldp));
+ } else if (field->flags & LGFS2_MFF_STRING) {
+ snprintf(str, size, "%s", fieldp);
+ } else {
+ switch(field->length) {
+ case sizeof(uint8_t):
+ snprintf(str, size, hex? "%"PRIx8 : "%"PRIu8, *(uint8_t *)fieldp);
+ break;
+ case sizeof(uint16_t):
+ snprintf(str, size, hex? "%"PRIx16 : "%"PRIu16, be16_to_cpu(*(uint16_t *)fieldp));
+ break;
+ case sizeof(uint32_t):
+ snprintf(str, size, hex? "%"PRIx32 : "%"PRIu32, be32_to_cpu(*(uint32_t *)fieldp));
+ break;
+ case sizeof(uint64_t):
+ snprintf(str, size, hex? "%"PRIx64 : "%"PRIu64, be64_to_cpu(*(uint64_t *)fieldp));
+ break;
+ default:
+ break;
+ }
+ }
+ str[size - 1] = '\0';
+ return 0;
+}
+
+int lgfs2_field_assign(char *blk, const struct lgfs2_metafield *field, const void *val)
+{
+ char *fieldp = blk + field->offset;
+
+ if (field->flags & LGFS2_MFF_UUID) {
+ memcpy(fieldp, val, 16);
+ return 0;
+ }
+
+ if (field->flags & LGFS2_MFF_STRING) {
+ strncpy(fieldp, val, field->length - 1);
+ fieldp[field->length - 1] = '\0';
+ return 0;
+ }
+
+ switch(field->length) {
+ case sizeof(uint8_t):
+ *fieldp = *(uint8_t *)val;
+ return 0;
+ case sizeof(uint16_t):
+ *(uint16_t *)fieldp = cpu_to_be16(*(uint16_t *)val);
+ return 0;
+ case sizeof(uint32_t):
+ *(uint32_t *)fieldp = cpu_to_be32(*(uint32_t *)val);
+ return 0;
+ case sizeof(uint64_t):
+ *(uint64_t *)fieldp = cpu_to_be64(*(uint64_t *)val);
+ return 0;
+ default:
+ /* Will never happen */
+ break;
+ }
+
+ errno = EINVAL;
+ return 1;
+}
9 years