Compare commits

...

12 Commits

Author SHA1 Message Date
openeuler-ci-bot
ef31c59bd9
!37 backport mkfs.gfs2: Fix strncpy warnings
From: @liyy9 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-12-29 03:26:25 +00:00
openeuler-ci-bot
8a0385eb6d
!34 backport gfs2_jadd: Fix static analysis warnings
From: @liyy9 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-12-25 06:53:13 +00:00
liyuan
784ce06709 backport mkfs.gfs2: Fix strncpy warnings 2023-12-22 02:23:18 +08:00
liyuan
3354ba449c backport gfs2_jadd: Fix static analysis warnings 2023-12-21 21:55:32 +08:00
openeuler-ci-bot
c0b845fb27
!31 gfs2_jadd: error handling overhaul
From: @liyy9 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-11-24 09:23:53 +00:00
openeuler-ci-bot
5b794628cc
!28 backport gfs2_jadd: Handle out-of-space issues
From: @liyy9 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-11-10 09:12:19 +00:00
liyuan
c627ee6af6 gfs2_jadd: error handling overhaul
Handle error conditions better and fail gracefully.
2023-11-07 00:19:03 +08:00
liyuan
ab73851820 backport gfs2_jadd: Handle out-of-space issues 2023-11-06 08:34:30 +08:00
openeuler-ci-bot
c54db5a2c2
!25 backport fsck.gfs2: Fix segfault in build_and_check_metalist
From: @liyy9 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-11-03 03:13:36 +00:00
liyuan
fdb8316f33 backport fsck.gfs2: Fix segfault in build_and_check_metalist 2023-11-02 19:57:34 +08:00
openeuler-ci-bot
8402d73fd3
!22 backport libgfs2: Fix gfs[2]_log_header metadata description
From: @liyy9 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-10-27 02:18:48 +00:00
openeuler-ci-bot
472a5b711a
!19 backport libgfs2: Fix pointer cast byte order issue
From: @liyy9 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-10-20 08:05:15 +00:00
6 changed files with 1013 additions and 1 deletions

View File

@ -0,0 +1,173 @@
From 100c229847e8c596eeb4eb92a71e9efc755e1558 Mon Sep 17 00:00:00 2001
From: Andrew Price <anprice@redhat.com>
Date: Fri, 18 Oct 2019 16:07:44 +0100
Subject: [PATCH] fsck.gfs2: Fix segfault in build_and_check_metalist
In unlikely circumstances, indirect pointer corruption in a 'system'
inode's metadata tree can lead to the inode block state being marked as
'free' in pass1, which causes build_and_check_metalist() to be called in
pass 2. The pass has a NULL ->check_metalist function pointer and so a
segfault occurs when build_and_check_metalist attempts to call it.
Fix the segfault by calling ->check_metalist() only when it's not NULL.
This required some refactoring to make the extra level of if-nesting
easier to implement and read.
Resolves: rhbz#1487726
Signed-off-by: Andrew Price <anprice@redhat.com>
---
gfs2/fsck/metawalk.c | 107 ++++++++++++++++++++-----------------------
gfs2/fsck/metawalk.h | 1 +
2 files changed, 50 insertions(+), 58 deletions(-)
diff --git a/gfs2/fsck/metawalk.c b/gfs2/fsck/metawalk.c
index d256dd2f..5792be56 100644
--- a/gfs2/fsck/metawalk.c
+++ b/gfs2/fsck/metawalk.c
@@ -1210,6 +1210,51 @@ static void file_ra(struct gfs2_inode *ip, struct gfs2_buffer_head *bh,
extlen * sdp->bsize, POSIX_FADV_WILLNEED);
}
+static int do_check_metalist(struct gfs2_inode *ip, uint64_t block, int height,
+ struct gfs2_buffer_head **bhp, struct metawalk_fxns *pass)
+{
+ int was_duplicate = 0;
+ int is_valid = 1;
+ int error;
+
+ if (pass->check_metalist == NULL)
+ return 0;
+
+ error = pass->check_metalist(ip, block, bhp, height, &is_valid,
+ &was_duplicate, pass->private);
+ if (error == meta_error) {
+ stack;
+ log_info("\n");
+ log_info(_("Serious metadata error on block %"PRIu64" (0x%"PRIx64").\n"),
+ block, block);
+ return error;
+ }
+ if (error == meta_skip_further) {
+ log_info("\n");
+ log_info(_("Unrecoverable metadata error on block %"PRIu64" (0x%"PRIx64")\n"),
+ block, block);
+ log_info(_("Further metadata will be skipped.\n"));
+ return error;
+ }
+ if (!is_valid) {
+ log_debug("Skipping rejected block %"PRIu64" (0x%"PRIx64")\n", block, block);
+ if (pass->invalid_meta_is_fatal)
+ return meta_error;
+ return meta_skip_one;
+ }
+ if (was_duplicate) {
+ log_debug("Skipping duplicate %"PRIu64" (0x%"PRIx64")\n", block, block);
+ return meta_skip_one;
+ }
+ if (!valid_block_ip(ip, block)) {
+ log_debug("Skipping invalid block %"PRIu64" (0x%"PRIx64")\n", block, block);
+ if (pass->invalid_meta_is_fatal)
+ return meta_error;
+ return meta_skip_one;
+ }
+ return error;
+}
+
/**
* build_and_check_metalist - check a bunch of indirect blocks
* This includes hash table blocks for directories
@@ -1229,8 +1274,8 @@ static int build_and_check_metalist(struct gfs2_inode *ip, osi_list_t *mlp,
osi_list_t *prev_list, *cur_list, *tmp;
int h, head_size, iblk_type;
uint64_t *ptr, block, *undoptr;
- int error, was_duplicate, is_valid;
int maxptrs;
+ int error;
osi_list_add(&metabh->b_altlist, &mlp[0]);
@@ -1294,65 +1339,11 @@ static int build_and_check_metalist(struct gfs2_inode *ip, osi_list_t *mlp,
continue;
block = be64_to_cpu(*ptr);
- was_duplicate = 0;
- error = pass->check_metalist(ip, block, &nbh,
- h, &is_valid,
- &was_duplicate,
- pass->private);
- /* check_metalist should hold any buffers
- it gets with "bread". */
- if (error == meta_error) {
- stack;
- log_info(_("\nSerious metadata "
- "error on block %llu "
- "(0x%llx).\n"),
- (unsigned long long)block,
- (unsigned long long)block);
+ error = do_check_metalist(ip, block, h, &nbh, pass);
+ if (error == meta_error || error == meta_skip_further)
goto error_undo;
- }
- if (error == meta_skip_further) {
- log_info(_("\nUnrecoverable metadata "
- "error on block %llu "
- "(0x%llx). Further metadata"
- " will be skipped.\n"),
- (unsigned long long)block,
- (unsigned long long)block);
- goto error_undo;
- }
- if (!is_valid) {
- log_debug( _("Skipping rejected block "
- "%llu (0x%llx)\n"),
- (unsigned long long)block,
- (unsigned long long)block);
- if (pass->invalid_meta_is_fatal) {
- error = meta_error;
- goto error_undo;
- }
- continue;
- }
- /* Note that there's a special case in which
- we need to process the metadata block, even
- if it was a duplicate. That's for cases
- where we deleted the last reference as
- metadata. */
- if (was_duplicate) {
- log_debug( _("Skipping duplicate %llu "
- "(0x%llx)\n"),
- (unsigned long long)block,
- (unsigned long long)block);
+ if (error == meta_skip_one)
continue;
- }
- if (!valid_block_ip(ip, block)) {
- log_debug( _("Skipping invalid block "
- "%lld (0x%llx)\n"),
- (unsigned long long)block,
- (unsigned long long)block);
- if (pass->invalid_meta_is_fatal) {
- error = meta_error;
- goto error_undo;
- }
- continue;
- }
if (!nbh)
nbh = bread(ip->i_sbd, block);
osi_list_add_prev(&nbh->b_altlist, cur_list);
diff --git a/gfs2/fsck/metawalk.h b/gfs2/fsck/metawalk.h
index 119efeed..b5a037a3 100644
--- a/gfs2/fsck/metawalk.h
+++ b/gfs2/fsck/metawalk.h
@@ -39,6 +39,7 @@ enum meta_check_rc {
meta_error = -1,
meta_is_good = 0,
meta_skip_further = 1,
+ meta_skip_one = 2,
};
/* metawalk_fxns: function pointers to check various parts of the fs
--
2.33.0

View File

@ -0,0 +1,116 @@
From deb6206755c515086a91faa6b728b076f5b4dc30 Mon Sep 17 00:00:00 2001
From: Abhi Das <adas@redhat.com>
Date: Mon, 11 May 2020 09:22:31 -0500
Subject: [PATCH] gfs2_jadd: Handle out-of-space issues
If gfs2_jadd runs out of disk space while adding journals, it does
not exit gracefully. It partially does its job and bails out when
it hits -ENOSPC. This leaves the metafs mounted and most likely a
corrupted filesystem that even fsck.gfs2 can't fix.
This patch adds a pre-check that ensures that the journals requested
will fit in the available space before proceeding. Note that this is
not foolproof because gfs2_jadd operates on a mounted filesystem.
While it is required that the filesystem be idle (and mounted on only
one node) while gfs2_jadd is being run, there is nothing stopping a
user from having some I/O process competing with gfs2_jadd for disk
blocks and consequently crashing it.
Signed-off-by: Abhi Das <adas@redhat.com>
---
gfs2/mkfs/main_jadd.c | 47 ++++++++++++++++++++++++++++++++++++++++---
1 file changed, 44 insertions(+), 3 deletions(-)
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
index efe91e30..c5424803 100644
--- a/gfs2/mkfs/main_jadd.c
+++ b/gfs2/mkfs/main_jadd.c
@@ -396,6 +396,8 @@ static void gather_info(struct gfs2_sbd *sdp, struct jadd_opts *opts)
exit(EXIT_FAILURE);
}
sdp->bsize = statbuf.f_bsize;
+ sdp->blks_total = statbuf.f_blocks;
+ sdp->blks_alloced = sdp->blks_total - statbuf.f_bfree;
}
static void find_current_journals(struct jadd_opts *opts)
@@ -527,13 +529,43 @@ static void add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
}
}
+static int check_fit(struct gfs2_sbd *sdp, struct jadd_opts *opts)
+{
+ /* Compute how much space we'll need for the new journals
+ * Number of blocks needed per added journal:
+ * 1 block for the ir inode
+ * 1 block for the sc inode
+ * for sizes of the qc and journal inodes, use lgfs2_space_for_data()
+ * to calculate.
+ */
+ uint64_t blks_per_j, total_blks;
+
+ blks_per_j = 1 + 1 +
+ lgfs2_space_for_data(sdp, sdp->bsize, sdp->qcsize << 20) +
+ lgfs2_space_for_data(sdp, sdp->bsize, sdp->jsize << 20);
+ total_blks = opts->journals * blks_per_j;
+
+ if (total_blks > (sdp->blks_total - sdp->blks_alloced)) {
+ printf( _("\nInsufficient space on the device to add %u %uMB "
+ "journals (%uMB QC size)\n\n"),
+ opts->journals, sdp->jsize, sdp->qcsize);
+ printf( _("Required space : %*lu blks (%lu blks per "
+ "journal)\n"), 10, total_blks, blks_per_j);
+ printf( _("Available space : %*lu blks\n\n"), 10,
+ sdp->blks_total - sdp->blks_alloced);
+ errno = ENOSPC;
+ return 1;
+ }
+ return 0;
+}
+
int main(int argc, char *argv[])
{
struct jadd_opts opts = {0};
struct gfs2_sbd sbd, *sdp = &sbd;
struct metafs mfs = {0};
struct mntent *mnt;
- unsigned int total;
+ unsigned int total, ret = 0;
setlocale(LC_ALL, "");
textdomain("gfs2-utils");
@@ -574,6 +606,12 @@ int main(int argc, char *argv[])
}
find_current_journals(&opts);
+ ret = check_fit(sdp, &opts);
+ if (ret) {
+ perror(_("Failed to add journals"));
+ goto out;
+ }
+
total = opts.orig_journals + opts.journals;
for (opts.journals = opts.orig_journals;
opts.journals < total;
@@ -588,13 +626,16 @@ int main(int argc, char *argv[])
add_j(sdp, &opts);
}
+out:
free(opts.new_inode);
free(opts.per_node);
free(opts.jindex);
close(sdp->path_fd);
cleanup_metafs(&mfs);
sync();
- print_results(&opts);
- return 0;
+ if (!ret)
+ print_results(&opts);
+
+ return ret;
}
--
2.33.0

View File

@ -0,0 +1,611 @@
From bb22cafabc0c604fe2e9ac75cbea3b30278d135b Mon Sep 17 00:00:00 2001
From: Abhi Das <adas@redhat.com>
Date: Mon, 11 May 2020 14:41:06 -0500
Subject: [PATCH] gfs2_jadd: error handling overhaul
Handle error conditions better and fail gracefully.
Signed-off-by: Abhi Das <adas@redhat.com>
---
gfs2/mkfs/main_jadd.c | 404 +++++++++++++++++++++++-------------------
1 file changed, 223 insertions(+), 181 deletions(-)
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
index c5424803..ea89c96b 100644
--- a/gfs2/mkfs/main_jadd.c
+++ b/gfs2/mkfs/main_jadd.c
@@ -42,15 +42,13 @@ struct jadd_opts {
#define JA_FL_SET 0
#define JA_FL_CLEAR 1
-static void set_flags(int fd, int op, uint32_t flags)
+static int set_flags(int fd, int op, uint32_t flags)
{
- int err;
uint32_t val;
- err = ioctl(fd, FS_IOC_GETFLAGS, &val);
- if (err) {
+ if (ioctl(fd, FS_IOC_GETFLAGS, &val)) {
perror("GETFLAGS");
- exit(EXIT_FAILURE);
+ return -1;
}
if (op == JA_FL_SET)
@@ -58,11 +56,11 @@ static void set_flags(int fd, int op, uint32_t flags)
else if (op == JA_FL_CLEAR)
val &= ~flags;
- err = ioctl(fd, FS_IOC_SETFLAGS, &val);
- if (err) {
+ if (ioctl(fd, FS_IOC_SETFLAGS, &val)) {
perror("SETFLAGS");
- exit(EXIT_FAILURE);
+ return -1;
}
+ return 0;
}
static int rename2system(struct jadd_opts *opts, const char *new_dir, const char *new_name)
@@ -243,188 +241,214 @@ static void print_results(struct jadd_opts *opts)
static int create_new_inode(struct jadd_opts *opts, uint64_t *addr)
{
char *name = opts->new_inode;
- int fd;
- int error;
+ int fd, error = 0;
for (;;) {
fd = open(name, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
if (fd >= 0)
break;
if (errno == EEXIST) {
- error = unlink(name);
- if (error){
+ if (unlink(name)) {
perror("unlink");
- exit(EXIT_FAILURE);
+ return -1;
}
- } else{
- perror("create");
- exit(EXIT_FAILURE);
+ continue;
}
+ perror("create");
+ return -1;
}
+
if (addr != NULL) {
struct stat st;
- fstat(fd, &st);
+ if ((error = fstat(fd, &st))) {
+ perror("fstat");
+ return close(fd);
+ }
*addr = st.st_ino;
}
return fd;
}
-static void add_ir(struct jadd_opts *opts)
+static int add_ir(struct jadd_opts *opts)
{
- int fd;
+ int fd, error = 0;
char new_name[256];
- int error;
+ struct gfs2_inum_range ir;
- fd = create_new_inode(opts, NULL);
+ if ((fd = create_new_inode(opts, NULL)) < 0)
+ return fd;
- {
- struct gfs2_inum_range ir;
+ if ((error = set_flags(fd, JA_FL_SET, FS_JOURNAL_DATA_FL)))
+ goto close_fd;
- set_flags(fd, JA_FL_SET, FS_JOURNAL_DATA_FL);
- memset(&ir, 0, sizeof(struct gfs2_inum_range));
- if (write(fd, (void*)&ir, sizeof(struct gfs2_inum_range)) !=
- sizeof(struct gfs2_inum_range)) {
- perror("add_ir");
- exit(EXIT_FAILURE);
- }
+ memset(&ir, 0, sizeof(struct gfs2_inum_range));
+ if (write(fd, (void*)&ir, sizeof(struct gfs2_inum_range)) !=
+ sizeof(struct gfs2_inum_range)) {
+ perror("add_ir write");
+ error = -1;
+ goto close_fd;
+ }
+
+ if ((error = fsync(fd))) {
+ perror("add_ir fsync");
+ goto close_fd;
}
- close(fd);
sprintf(new_name, "inum_range%u", opts->journals);
error = rename2system(opts, opts->per_node, new_name);
- if (error < 0 && errno != EEXIST){
+ if (error < 0 && errno != EEXIST) {
perror("add_ir rename2system");
- exit(EXIT_FAILURE);
+ goto close_fd;
}
+close_fd:
+ return close(fd) || error;
}
-static void add_sc(struct jadd_opts *opts)
+static int add_sc(struct jadd_opts *opts)
{
- int fd;
+ int fd, error = 0;
char new_name[256];
- int error;
+ struct gfs2_statfs_change sc;
- fd = create_new_inode(opts, NULL);
+ if ((fd = create_new_inode(opts, NULL)) < 0)
+ return fd;
- {
- struct gfs2_statfs_change sc;
- set_flags(fd, JA_FL_SET, FS_JOURNAL_DATA_FL);
+ if ((error = set_flags(fd, JA_FL_SET, FS_JOURNAL_DATA_FL)))
+ goto close_fd;
- memset(&sc, 0, sizeof(struct gfs2_statfs_change));
- if (write(fd, (void*)&sc, sizeof(struct gfs2_statfs_change)) !=
- sizeof(struct gfs2_statfs_change)) {
- perror("add_sc");
- exit(EXIT_FAILURE);
- }
+ memset(&sc, 0, sizeof(struct gfs2_statfs_change));
+ if (write(fd, (void*)&sc, sizeof(struct gfs2_statfs_change)) !=
+ sizeof(struct gfs2_statfs_change)) {
+ perror("add_sc write");
+ error = -1;
+ goto close_fd;
}
- close(fd);
+ if ((error = fsync(fd))) {
+ perror("add_sc fsync");
+ goto close_fd;
+ }
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);
+ goto close_fd;
}
+close_fd:
+ return close(fd) || error;
}
-static void add_qc(struct gfs2_sbd *sdp, struct jadd_opts *opts)
+static int add_qc(struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
- int fd;
- char new_name[256];
- int error;
-
- fd = create_new_inode(opts, NULL);
-
- {
- char buf[sdp->bsize];
- unsigned int blocks =
- sdp->qcsize << (20 - sdp->sd_sb.sb_bsize_shift);
- unsigned int x;
- struct gfs2_meta_header mh;
-
- set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL);
- memset(buf, 0, sdp->bsize);
-
- for (x=0; x<blocks; x++) {
- if (write(fd, buf, sdp->bsize) != sdp->bsize) {
- perror("add_qc");
- exit(EXIT_FAILURE);
- }
+ int fd, error = 0;
+ char new_name[256], buf[sdp->bsize];
+ unsigned int blocks =
+ sdp->qcsize << (20 - sdp->sd_sb.sb_bsize_shift);
+ unsigned int x;
+ struct gfs2_meta_header mh;
+
+ if ((fd = create_new_inode(opts, NULL)) < 0)
+ return fd;
+
+ if ((error = set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL)))
+ goto close_fd;
+
+ memset(buf, 0, sdp->bsize);
+ for (x=0; x<blocks; x++) {
+ if (write(fd, buf, sdp->bsize) != sdp->bsize) {
+ perror("add_qc write");
+ error = -1;
+ goto close_fd;
}
+ }
- lseek(fd, 0, SEEK_SET);
-
- memset(&mh, 0, sizeof(struct gfs2_meta_header));
- mh.mh_magic = GFS2_MAGIC;
- mh.mh_type = GFS2_METATYPE_QC;
- mh.mh_format = GFS2_FORMAT_QC;
- gfs2_meta_header_out(&mh, buf);
+ if ((error = lseek(fd, 0, SEEK_SET)) < 0) {
+ perror("add_qc lseek");
+ goto close_fd;
+ }
- for (x=0; x<blocks; x++) {
- if (write(fd, buf, sdp->bsize) != sdp->bsize) {
- perror("add_qc");
- exit(EXIT_FAILURE);
- }
+ memset(&mh, 0, sizeof(struct gfs2_meta_header));
+ mh.mh_magic = GFS2_MAGIC;
+ mh.mh_type = GFS2_METATYPE_QC;
+ mh.mh_format = GFS2_FORMAT_QC;
+ gfs2_meta_header_out(&mh, buf);
+
+ for (x=0; x<blocks; x++) {
+ if (write(fd, buf, sdp->bsize) != sdp->bsize) {
+ perror("add_qc write");
+ error = 1;
+ goto close_fd;
}
-
- error = fsync(fd);
- if (error){
+ if ((error = fsync(fd))) {
perror("add_qc fsync");
- exit(EXIT_FAILURE);
+ goto close_fd;
}
}
- close(fd);
-
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);
+ goto close_fd;
}
+close_fd:
+ return close(fd) || error;
}
-static void gather_info(struct gfs2_sbd *sdp, struct jadd_opts *opts)
+static int gather_info(struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
struct statfs statbuf;
+
if (statfs(opts->path, &statbuf) < 0) {
perror(opts->path);
- exit(EXIT_FAILURE);
+ return -1;
}
+
sdp->bsize = statbuf.f_bsize;
sdp->blks_total = statbuf.f_blocks;
sdp->blks_alloced = sdp->blks_total - statbuf.f_bfree;
+
+ return 0;
}
-static void find_current_journals(struct jadd_opts *opts)
+static int find_current_journals(struct jadd_opts *opts)
{
struct dirent *dp;
DIR *dirp;
unsigned existing_journals = 0;
+ int ret = 0;
dirp = opendir(opts->jindex);
if (!dirp) {
perror("jindex");
- exit(EXIT_FAILURE);
+ ret = -1;
+ goto out;
}
while (dirp) {
if ((dp = readdir(dirp)) != NULL) {
if (strncmp(dp->d_name, "journal", 7) == 0)
existing_journals++;
} else
- goto close;
+ goto close_fd;
}
-close:
- closedir(dirp);
+close_fd:
+ if ((ret = closedir(dirp)))
+ goto out;
+
if (existing_journals == 0) {
- die( _("No journals found. Did you run mkfs.gfs2 correctly?\n"));
+ errno = EINVAL;
+ perror("No journals found. Did you run mkfs.gfs2 correctly?\n");
+ ret = -1;
+ goto out;
}
opts->orig_journals = existing_journals;
+out:
+ return ret;
}
#ifdef GFS2_HAS_LH_V2
@@ -450,83 +474,88 @@ static uint64_t find_block_address(int fd, off_t offset, unsigned bsize)
}
#endif
-static void add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
+static int add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
{
- int fd;
- char new_name[256];
- int error;
- uint64_t addr;
-
- fd = create_new_inode(opts, &addr);
-
- {
- char buf[sdp->bsize];
- unsigned int blocks =
- sdp->jsize << (20 - sdp->sd_sb.sb_bsize_shift);
- unsigned int x;
- struct gfs2_log_header lh;
- uint64_t seq = RANDOM(blocks);
- off_t off = 0;
-
- set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL);
- memset(buf, 0, sdp->bsize);
- for (x=0; x<blocks; x++) {
- if (write(fd, buf, sdp->bsize) != sdp->bsize) {
- perror("add_j");
- exit(EXIT_FAILURE);
- }
+ int fd, error = 0;
+ char new_name[256], buf[sdp->bsize];
+ unsigned int x, blocks =
+ sdp->jsize << (20 - sdp->sd_sb.sb_bsize_shift);
+ struct gfs2_log_header lh;
+ uint64_t seq = RANDOM(blocks), addr;
+ off_t off = 0;
+
+ if ((fd = create_new_inode(opts, &addr)) < 0)
+ return fd;
+
+ if ((error = set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL)))
+ goto close_fd;
+
+ memset(buf, 0, sdp->bsize);
+ for (x=0; x<blocks; x++) {
+ if (write(fd, buf, sdp->bsize) != sdp->bsize) {
+ perror("add_j write");
+ error = -1;
+ goto close_fd;
}
+ }
- lseek(fd, 0, SEEK_SET);
+ if ((error = lseek(fd, 0, SEEK_SET)) < 0) {
+ perror("add_j lseek");
+ goto close_fd;
+ }
- memset(&lh, 0, sizeof(struct gfs2_log_header));
- lh.lh_header.mh_magic = GFS2_MAGIC;
- lh.lh_header.mh_type = GFS2_METATYPE_LH;
- lh.lh_header.mh_format = GFS2_FORMAT_LH;
- lh.lh_flags = GFS2_LOG_HEAD_UNMOUNT;
+ memset(&lh, 0, sizeof(struct gfs2_log_header));
+ lh.lh_header.mh_magic = GFS2_MAGIC;
+ lh.lh_header.mh_type = GFS2_METATYPE_LH;
+ lh.lh_header.mh_format = GFS2_FORMAT_LH;
+ lh.lh_flags = GFS2_LOG_HEAD_UNMOUNT;
#ifdef GFS2_HAS_LH_V2
- lh.lh_flags |= GFS2_LOG_HEAD_USERSPACE;
- lh.lh_jinode = addr;
+ lh.lh_flags |= GFS2_LOG_HEAD_USERSPACE;
+ lh.lh_jinode = addr;
#endif
- for (x=0; x<blocks; x++) {
- uint32_t hash;
-
- lh.lh_sequence = seq;
- lh.lh_blkno = x;
- gfs2_log_header_out(&lh, buf);
- hash = lgfs2_log_header_hash(buf);
- ((struct gfs2_log_header *)buf)->lh_hash = cpu_to_be32(hash);
+ for (x=0; x<blocks; x++) {
+ uint32_t hash;
#ifdef GFS2_HAS_LH_V2
- ((struct gfs2_log_header *)buf)->lh_addr = cpu_to_be64(
- find_block_address(fd, off, sdp->bsize));
- hash = lgfs2_log_header_crc(buf, sdp->bsize);
- ((struct gfs2_log_header *)buf)->lh_crc = cpu_to_be32(hash);
+ uint64_t blk_addr = 0;
#endif
- if (write(fd, buf, sdp->bsize) != sdp->bsize) {
- perror("add_j");
- exit(EXIT_FAILURE);
- }
-
- if (++seq == blocks)
- seq = 0;
- off += sdp->bsize;
+ lh.lh_sequence = seq;
+ lh.lh_blkno = x;
+ gfs2_log_header_out(&lh, buf);
+ hash = lgfs2_log_header_hash(buf);
+ ((struct gfs2_log_header *)buf)->lh_hash = cpu_to_be32(hash);
+#ifdef GFS2_HAS_LH_V2
+ if (!(blk_addr = find_block_address(fd, off, sdp->bsize))) {
+ error = -1;
+ goto close_fd;
+ }
+ ((struct gfs2_log_header *)buf)->lh_addr = cpu_to_be64(blk_addr);
+ hash = lgfs2_log_header_crc(buf, sdp->bsize);
+ ((struct gfs2_log_header *)buf)->lh_crc = cpu_to_be32(hash);
+#endif
+ if (write(fd, buf, sdp->bsize) != sdp->bsize) {
+ perror("add_j write");
+ error = -1;
+ goto close_fd;
}
- error = fsync(fd);
- if (error){
+ if (++seq == blocks)
+ seq = 0;
+ off += sdp->bsize;
+
+ if ((error = fsync(fd))) {
perror("add_j fsync");
- exit(EXIT_FAILURE);
+ goto close_fd;
}
}
- close(fd);
-
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);
+ goto close_fd;
}
+close_fd:
+ return close(fd) || error;
}
static int check_fit(struct gfs2_sbd *sdp, struct jadd_opts *opts)
@@ -554,7 +583,7 @@ static int check_fit(struct gfs2_sbd *sdp, struct jadd_opts *opts)
printf( _("Available space : %*lu blks\n\n"), 10,
sdp->blks_total - sdp->blks_alloced);
errno = ENOSPC;
- return 1;
+ return -1;
}
return 0;
}
@@ -581,35 +610,42 @@ int main(int argc, char *argv[])
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"), opts.path, strerror(errno));
- exit(EXIT_FAILURE);
+ fprintf(stderr, "Error looking up mount '%s': %s\n",
+ opts.path, strerror(errno));
+ ret = -1;
+ goto out;
}
if (mnt == NULL) {
- fprintf(stderr, _("%s: not a mounted gfs2 file system\n"), opts.path);
- exit(EXIT_FAILURE);
+ fprintf(stderr, "%s: not a mounted gfs2 file system\n", opts.path);
+ ret = -1;
+ goto close_sb;
}
- gather_info(sdp, &opts);
+
+ if ((ret = gather_info(sdp, &opts)))
+ goto close_sb;
+
mfs.context = copy_context_opt(mnt);
- if (mount_gfs2_meta(&mfs, mnt->mnt_dir, opts.debug)) {
+ if ((ret = mount_gfs2_meta(&mfs, mnt->mnt_dir, opts.debug))) {
perror("GFS2 metafs");
- exit(EXIT_FAILURE);
+ goto close_sb;
}
- if (build_paths(mfs.path, &opts)) {
+ if ((ret = build_paths(mfs.path, &opts))) {
perror(_("Failed to build paths"));
- exit(EXIT_FAILURE);
+ goto umount_meta;
}
- if (compute_constants(sdp)) {
+ if ((ret = compute_constants(sdp))) {
perror(_("Failed to compute file system constants"));
- exit(EXIT_FAILURE);
+ goto free_paths;
}
- find_current_journals(&opts);
- ret = check_fit(sdp, &opts);
- if (ret) {
+ if ((ret = find_current_journals(&opts)))
+ goto free_paths;
+
+ if ((ret = check_fit(sdp, &opts))) {
perror(_("Failed to add journals"));
- goto out;
+ goto free_paths;
}
total = opts.orig_journals + opts.journals;
@@ -617,23 +653,29 @@ int main(int argc, char *argv[])
opts.journals < total;
opts.journals++) {
if (metafs_interrupted) {
- cleanup_metafs(&mfs);
- exit(130);
+ errno = 130;
+ goto free_paths;
}
- add_ir(&opts);
- add_sc(&opts);
- add_qc(sdp, &opts);
- add_j(sdp, &opts);
+ if ((ret = add_ir(&opts)))
+ goto free_paths;
+ if ((ret = add_sc(&opts)))
+ goto free_paths;
+ if ((ret = add_qc(sdp, &opts)))
+ goto free_paths;
+ if ((ret = add_j(sdp, &opts)))
+ goto free_paths;
}
-out:
+free_paths:
free(opts.new_inode);
free(opts.per_node);
free(opts.jindex);
- close(sdp->path_fd);
- cleanup_metafs(&mfs);
+umount_meta:
sync();
-
+ cleanup_metafs(&mfs);
+close_sb:
+ close(sdp->path_fd);
+out:
if (!ret)
print_results(&opts);
--
2.33.0

View File

@ -0,0 +1,56 @@
From 8a0b4b2183e05be4c92612b0a82ed64617e6a742 Mon Sep 17 00:00:00 2001
From: Andrew Price <anprice@redhat.com>
Date: Tue, 9 Jun 2020 15:24:28 +0100
Subject: [PATCH] gfs2_jadd: Fix static analysis warnings
Fix these warnings:
gfs2/mkfs/main_jadd.c:264:8: warning: Although the value stored to
'error' is used in the enclosing expression, the value is never actually
read from 'error'
gfs2/mkfs/main_jadd.c:514:15: warning: Assigned value is garbage or
undefined
Signed-off-by: Andrew Price <anprice@redhat.com>
---
gfs2/mkfs/main_jadd.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
index ea89c96b..03134a61 100644
--- a/gfs2/mkfs/main_jadd.c
+++ b/gfs2/mkfs/main_jadd.c
@@ -241,7 +241,7 @@ static void print_results(struct jadd_opts *opts)
static int create_new_inode(struct jadd_opts *opts, uint64_t *addr)
{
char *name = opts->new_inode;
- int fd, error = 0;
+ int fd;
for (;;) {
fd = open(name, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
@@ -261,9 +261,10 @@ static int create_new_inode(struct jadd_opts *opts, uint64_t *addr)
if (addr != NULL) {
struct stat st;
- if ((error = fstat(fd, &st))) {
+ if (fstat(fd, &st) == -1) {
perror("fstat");
- return close(fd);
+ close(fd);
+ return -1;
}
*addr = st.st_ino;
}
@@ -481,7 +482,7 @@ static int add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
unsigned int x, blocks =
sdp->jsize << (20 - sdp->sd_sb.sb_bsize_shift);
struct gfs2_log_header lh;
- uint64_t seq = RANDOM(blocks), addr;
+ uint64_t seq = RANDOM(blocks), addr = 0;
off_t off = 0;
if ((fd = create_new_inode(opts, &addr)) < 0)
--
2.33.0

View File

@ -0,0 +1,36 @@
From 7cff23e49b7a80486e522fee5a645cd9b143c7c3 Mon Sep 17 00:00:00 2001
From: Andrew Price <anprice@redhat.com>
Date: Sat, 23 May 2020 15:05:43 +0100
Subject: [PATCH] mkfs.gfs2: Fix strncpy warnings
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
New in gcc 10
warning: __builtin_strncpy specified bound 64 equals destination size
[-Wstringop-truncation]
Signed-off-by: Andrew Price <anprice@redhat.com>
---
gfs2/mkfs/main_mkfs.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/gfs2/mkfs/main_mkfs.c b/gfs2/mkfs/main_mkfs.c
index bb8762b0..36fd08e7 100644
--- a/gfs2/mkfs/main_mkfs.c
+++ b/gfs2/mkfs/main_mkfs.c
@@ -1144,8 +1144,8 @@ int main(int argc, char *argv[])
build_root(&sbd);
sb.sb_root_dir = sbd.md.rooti->i_di.di_num;
- strncpy(sb.sb_lockproto, opts.lockproto, GFS2_LOCKNAME_LEN);
- strncpy(sb.sb_locktable, opts.locktable, GFS2_LOCKNAME_LEN);
+ strncpy(sb.sb_lockproto, opts.lockproto, GFS2_LOCKNAME_LEN - 1);
+ strncpy(sb.sb_locktable, opts.locktable, GFS2_LOCKNAME_LEN - 1);
sb.sb_lockproto[GFS2_LOCKNAME_LEN - 1] = '\0';
sb.sb_locktable[GFS2_LOCKNAME_LEN - 1] = '\0';
--
2.33.0

View File

@ -1,6 +1,6 @@
Name: gfs2-utils
Version: 3.2.0
Release: 7
Release: 12
Summary: Global Filesystem Utilities
License: GPLv2+ and LGPLv2+
@ -9,6 +9,11 @@ Source0: https://releases.pagure.org/gfs2-utils/gfs2-utils-%{version}.tar
Patch0001: 0001-libgfs2-Fix-pointer-cast-byte-order-issue.patch
Patch0002: 0002-libgfs2-Fix-gfs-2-_log_header-metadata-description.patch
Patch0003: 0003-fsck.gfs2-Fix-segfault-in-build_and_check_metalist.patch
Patch0004: 0004-gfs2_jadd-Handle-out-of-space-issues.patch
Patch0005: 0005--gfs2_jadd-error-handling-overhaul.patch
Patch0006: 0006-gfs2_jadd-Fix-static-analysis-warnings.patch
Patch0007: 0007-mkfs.gfs2-Fix-strncpy-warnings.patch
BuildRequires: ncurses-devel kernel-headers automake libtool zlib-devel gettext-devel
BuildRequires: bison flex libblkid-devel libuuid-devel check-devel
@ -54,6 +59,21 @@ make -C gfs2 install DESTDIR=%{buildroot}
%exclude %{_mandir}/man8/gfs2_lockcapture.8.gz
%changelog
* Thu Dec 28 2023 liyuanyuan <liyuanyuan@xfusion.com> - 3.2.0-12
- mkfs.gfs2: Fix strncpy warnings
* Tue Dec 12 2023 liyuanyuan <liyuanyuan@xfusion.com> - 3.2.0-11
- gfs2_jadd: Fix static analysis warningS
* Fri Nov 24 2023 liyuanyuan <liyuanyuan@xfusion.com> - 3.2.0-10
- gfs2_jadd: error handling overhaul
* Thu Nov 09 2023 liyuanyuan <liyuanyuan@xfusion.com> - 3.2.0-9
- gfs2_jadd: Handle out-of-space issues
* Thu Nov 02 2023 liyuanyuan <liyuanyuan@xfusion.com> - 3.2.0-8
- fsck.gfs2: Fix segfault in build_and_check_metalist
* Thu Oct 26 2023 liyuanyuan <liyuanyuan@xfusion.com> - 3.2.0-7
- libgfs2: Fix gfs[2]_log_header metadata description