Skip to content

Commit

Permalink
Replace "none" with macro
Browse files Browse the repository at this point in the history
String "none" is used many times throughout the code.
Replace "none" strings with predefined macro.

Add str_is_none() for comparing strings with "none".
Replace str(n)cmp calls with function.

Signed-off-by: Mateusz Kusiak <[email protected]>
Signed-off-by: Mariusz Tkaczyk <[email protected]>
  • Loading branch information
dancesWithMachines authored and mtkaczyk committed Jan 24, 2024
1 parent 90fd700 commit b823c8f
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 29 deletions.
3 changes: 1 addition & 2 deletions Assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -2015,8 +2015,7 @@ int assemble_container_content(struct supertype *st, int mdfd,
if (dev)
continue;
/* Don't want this one any more */
if (sysfs_set_str(sra, dev2, "slot", "none") < 0 &&
errno == EBUSY) {
if (sysfs_set_str(sra, dev2, "slot", STR_COMMON_NONE) < 0 && errno == EBUSY) {
pr_err("Cannot remove old device %s: not updating %s\n", dev2->sys_name, sra->sys_name);
sysfs_free(sra);
return 1;
Expand Down
4 changes: 2 additions & 2 deletions Build.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int Build(struct mddev_ident *ident, struct mddev_dev *devlist, struct shape *s,
return 1;
}

map_update(&map, fd2devnm(mdfd), "none", uuid, chosen_name);
map_update(&map, fd2devnm(mdfd), STR_COMMON_NONE, uuid, chosen_name);
map_unlock(&map);

array.level = s->level;
Expand Down Expand Up @@ -111,7 +111,7 @@ int Build(struct mddev_ident *ident, struct mddev_dev *devlist, struct shape *s,
goto abort;
}

if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
if (s->bitmap_file && str_is_none(s->bitmap_file) == true)
s->bitmap_file = NULL;
if (s->bitmap_file && s->level <= 0) {
pr_err("bitmaps not meaningful with level %s\n",
Expand Down
2 changes: 1 addition & 1 deletion Create.c
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ int Create(struct supertype *st, struct mddev_ident *ident, int subdevs,
pr_err("automatically enabling write-intent bitmap on large array\n");
s->bitmap_file = "internal";
}
if (s->bitmap_file && strcmp(s->bitmap_file, "none") == 0)
if (s->bitmap_file && str_is_none(s->bitmap_file) == true)
s->bitmap_file = NULL;

if (s->consistency_policy == CONSISTENCY_POLICY_PPL &&
Expand Down
15 changes: 7 additions & 8 deletions Grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
return 1;
}
if (bmf.pathname[0]) {
if (strcmp(s->bitmap_file,"none") == 0) {
if (str_is_none(s->bitmap_file) == true) {
if (ioctl(fd, SET_BITMAP_FILE, -1) != 0) {
pr_err("failed to remove bitmap %s\n",
bmf.pathname);
Expand All @@ -325,7 +325,7 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
return 1;
}
if (array.state & (1 << MD_SB_BITMAP_PRESENT)) {
if (strcmp(s->bitmap_file, "none")==0) {
if (str_is_none(s->bitmap_file) == true) {
array.state &= ~(1 << MD_SB_BITMAP_PRESENT);
if (md_set_array_info(fd, &array) != 0) {
if (array.state & (1 << MD_SB_CLUSTERED))
Expand All @@ -340,7 +340,7 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
return 1;
}

if (strcmp(s->bitmap_file, "none") == 0) {
if (str_is_none(s->bitmap_file) == true) {
pr_err("no bitmap found on %s\n", devname);
return 1;
}
Expand Down Expand Up @@ -1067,7 +1067,7 @@ int remove_disks_for_takeover(struct supertype *st,
remaining = sd->next;

sysfs_set_str(sra, sd, "state", "faulty");
sysfs_set_str(sra, sd, "slot", "none");
sysfs_set_str(sra, sd, "slot", STR_COMMON_NONE);
/* for external metadata disks should be removed in mdmon */
if (!st->ss->external)
sysfs_set_str(sra, sd, "state", "remove");
Expand Down Expand Up @@ -2145,8 +2145,7 @@ int Grow_reshape(char *devname, int fd,
* a backport has been arranged.
*/
if (sra == NULL ||
sysfs_set_str(sra, NULL, "resync_start",
"none") < 0)
sysfs_set_str(sra, NULL, "resync_start", STR_COMMON_NONE) < 0)
pr_err("--assume-clean not supported with --grow on this kernel\n");
}
md_get_array_info(fd, &array);
Expand Down Expand Up @@ -4159,8 +4158,8 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
* it was just a device failure that leaves us degraded but
* functioning.
*/
if (sysfs_get_str(info, NULL, "reshape_position", buf,
sizeof(buf)) < 0 || strncmp(buf, "none", 4) != 0) {
if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0 ||
str_is_none(buf) == false) {
/* The abort might only be temporary. Wait up to 10
* seconds for fd to contain a valid number again.
*/
Expand Down
2 changes: 1 addition & 1 deletion Incremental.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
continue;

if (d->disk.raid_disk > -1)
sysfs_set_str(sra, d, "slot", "none");
sysfs_set_str(sra, d, "slot", STR_COMMON_NONE);
if (sysfs_set_str(sra, d, "state", "remove") == 0)
if (verbose >= 0)
pr_err("removing old device %s from %s\n",
Expand Down
2 changes: 1 addition & 1 deletion Manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
unsigned long long max_completed;
sysfs_get_ll(mdi, NULL, "reshape_position", &curr);
sysfs_fd_get_str(scfd, buf, sizeof(buf));
if (strncmp(buf, "none", 4) == 0) {
if (str_is_none(buf) == true) {
/* Either reshape has aborted, or hasn't
* quite started yet. Wait a bit and
* check 'sync_action' to see.
Expand Down
2 changes: 1 addition & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ void load_conffile(void)
dl_add(list, dl_strdup("partitions"));
devline(list);
free_line(list);
} else if (strcmp(conffile, "none") != 0) {
} else if (str_is_none(conffile) == false) {
f = fopen(conffile, "r");
/* Debian chose to relocate mdadm.conf into /etc/mdadm/.
* To allow Debian users to compile from clean source and still
Expand Down
4 changes: 2 additions & 2 deletions maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ mapping_t faultylayout[] = {

{ "clear", ClearErrors},
{ "flush", ClearFaults},
{ "none", ClearErrors},
{ STR_COMMON_NONE, ClearErrors},
{ "default", ClearErrors},
{ NULL, UnSet }
};

mapping_t consistency_policies[] = {
{ "unknown", CONSISTENCY_POLICY_UNKNOWN},
{ "none", CONSISTENCY_POLICY_NONE},
{ STR_COMMON_NONE, CONSISTENCY_POLICY_NONE},
{ "resync", CONSISTENCY_POLICY_RESYNC},
{ "bitmap", CONSISTENCY_POLICY_BITMAP},
{ "journal", CONSISTENCY_POLICY_JOURNAL},
Expand Down
7 changes: 3 additions & 4 deletions mdadm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ int main(int argc, char *argv[])
exit(2);
}
if (strcmp(optarg, "internal") == 0 ||
strcmp(optarg, "none") == 0 ||
strcmp(optarg, STR_COMMON_NONE) == 0 ||
strchr(optarg, '/') != NULL) {
s.bitmap_file = optarg;
continue;
Expand Down Expand Up @@ -1234,13 +1234,12 @@ int main(int argc, char *argv[])
pr_err("PPL consistency policy is only supported for RAID level 5.\n");
exit(2);
} else if (s.consistency_policy == CONSISTENCY_POLICY_BITMAP &&
(!s.bitmap_file ||
strcmp(s.bitmap_file, "none") == 0)) {
(!s.bitmap_file || str_is_none(s.bitmap_file) == true)) {
pr_err("--bitmap is required for consistency policy: %s\n",
map_num_s(consistency_policies, s.consistency_policy));
exit(2);
} else if (s.bitmap_file &&
strcmp(s.bitmap_file, "none") != 0 &&
str_is_none(s.bitmap_file) == false &&
s.consistency_policy != CONSISTENCY_POLICY_BITMAP &&
s.consistency_policy != CONSISTENCY_POLICY_JOURNAL) {
pr_err("--bitmap is not compatible with consistency policy: %s\n",
Expand Down
16 changes: 16 additions & 0 deletions mdadm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2027,3 +2027,19 @@ static inline int is_container(const int level)
return 1;
return 0;
}

#define STR_COMMON_NONE "none"

/**
* str_is_none() - check if @str starts with "none".
* @str: string
*
* return:
* true if string starts with "none", false otherwise.
*/
static inline bool str_is_none(char *str)
{
if (strncmp(str, STR_COMMON_NONE, sizeof(STR_COMMON_NONE) - 1) == 0)
return true;
return false;
}
4 changes: 2 additions & 2 deletions monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static void read_resync_start(int fd, unsigned long long *v)
dprintf("Failed to read resync_start (%d)\n", fd);
return;
}
if (strncmp(buf, "none", 4) == 0)
if (str_is_none(buf) == true)
*v = MaxSector;
else
*v = strtoull(buf, NULL, 10);
Expand Down Expand Up @@ -600,7 +600,7 @@ static int read_and_act(struct active_array *a, fd_set *fds)
"reshape_position",
buf,
sizeof(buf)) >= 0) &&
strncmp(buf, "none", 4) == 0)
str_is_none(buf) == true)
a->last_checkpoint = a->info.component_size;
}
a->container->ss->set_array_state(a, a->curr_state <= clean);
Expand Down
4 changes: 2 additions & 2 deletions super-intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ static void print_imsm_dev(struct intel_super *super,
printf("\n");
printf(" Failed disk : ");
if (map->failed_disk_num == 0xff)
printf("none");
printf(STR_COMMON_NONE);
else
printf("%i", map->failed_disk_num);
printf("\n");
Expand Down Expand Up @@ -12086,7 +12086,7 @@ static int read_completed(int fd, unsigned long long *val)
return ret;

ret = COMPLETED_OK;
if (strncmp(buf, "none", 4) == 0) {
if (str_is_none(buf) == true) {
ret = COMPLETED_NONE;
} else if (strncmp(buf, "delayed", 7) == 0) {
ret = COMPLETED_DELAYED;
Expand Down
4 changes: 2 additions & 2 deletions sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
strcpy(base, "metadata_version");
if (load_sys(fname, buf, sizeof(buf)))
goto abort;
if (strncmp(buf, "none", 4) == 0) {
if (str_is_none(buf) == true) {
sra->array.major_version =
sra->array.minor_version = -1;
strcpy(sra->text_version, "");
Expand Down Expand Up @@ -244,7 +244,7 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
goto abort;
if (strncmp(buf, "file", 4) == 0)
sra->bitmap_offset = 1;
else if (strncmp(buf, "none", 4) == 0)
else if (str_is_none(buf) == true)
sra->bitmap_offset = 0;
else if (buf[0] == '+')
sra->bitmap_offset = strtol(buf+1, NULL, 10);
Expand Down
2 changes: 1 addition & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ int remove_disk(int mdfd, struct supertype *st,

/* Remove the disk given by 'info' from the array */
if (st->ss->external)
rv = sysfs_set_str(sra, info, "slot", "none");
rv = sysfs_set_str(sra, info, "slot", STR_COMMON_NONE);
else
rv = ioctl(mdfd, HOT_REMOVE_DISK, makedev(info->disk.major,
info->disk.minor));
Expand Down

0 comments on commit b823c8f

Please sign in to comment.