Skip to content

Commit

Permalink
Refactor mounted device checks
Browse files Browse the repository at this point in the history
Calling functions now print error messages.
All the mounted devices are printed (not just the first one).

Signed-off-by: Daniel Madej <daniel.madej@huawei.com>
  • Loading branch information
Deixx committed Sep 12, 2024
1 parent a13af54 commit 8d2d689
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions casadm/cas_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@

#define CORE_ADD_MAX_TIMEOUT 30

bool cache_mounts_detected(int cache_id);
bool core_mounts_detected(int cache_id, int core_id);
bool device_mounts_detected(const char *pattern, int cmplen);
void print_mounted_devices(const char *pattern, int cmplen);

/* KCAS_IOCTL_CACHE_CHECK_DEVICE wrapper */
int _check_cache_device(const char *device_path,
Expand Down Expand Up @@ -1119,8 +1119,16 @@ int stop_cache(uint16_t cache_id, int flush)
int status;

/* Don't stop instance with mounted filesystem */
if (cache_mounts_detected(cache_id))
int cmplen = 0;
char pattern[80];

/* verify if any core (or core partition) for this cache is mounted */
cmplen = snprintf(pattern, sizeof(pattern), "/dev/cas%d-", cache_id) - 1;
if (device_mounts_detected(pattern, cmplen)) {
cas_printf(LOG_ERR, "Can't stop cache instance %d due to mounted devices:\n", cache_id);
print_mounted_devices(pattern, cmplen);
return FAILURE;
}

fd = open_ctrl_device();
if (fd == -1)
Expand Down Expand Up @@ -1803,20 +1811,11 @@ int add_core(unsigned int cache_id, unsigned int core_id, const char *core_devic
return SUCCESS;
}

bool _device_mounts_detected(int cache_id, int core_id)
bool device_mounts_detected(const char *pattern, int cmplen)
{
FILE *mtab;
struct mntent *mstruct;
char dev_buf[80];
int no_match = 0, error = 0, cmplen = 0;
if (core_id >= 0) {
/* verify if specific core is mounted */
cmplen = snprintf(dev_buf, sizeof(dev_buf), "/dev/cas%d-%d", cache_id, core_id);
} else {
/* verify if any core from given cache is mounted
do not compare terminating NULL for cache */
cmplen = snprintf(dev_buf, sizeof(dev_buf), "/dev/cas%d-", cache_id) - 1;
}
int no_match = 0, error = 0;

mtab = setmntent("/etc/mtab", "r");
if (!mtab) {
Expand All @@ -1825,38 +1824,39 @@ bool _device_mounts_detected(int cache_id, int core_id)
}

while ((mstruct = getmntent(mtab)) != NULL) {
error = strcmp_s(mstruct->mnt_fsname, cmplen, dev_buf, &no_match);
error = strcmp_s(mstruct->mnt_fsname, cmplen, pattern, &no_match);
/* mstruct->mnt_fsname is /dev/... block device path, not a mountpoint */
if (error != EOK)
return false;
if (no_match)
continue;

if (core_id < 0) {
cas_printf(LOG_ERR,
"Can't stop cache instance %d. Device %s is mounted!\n",
cache_id, mstruct->mnt_fsname);
} else {
cas_printf(LOG_ERR,
"Can't remove core %d from cache %d."
" Device %s is mounted!\n",
core_id, cache_id, mstruct->mnt_fsname);
}

return true;
}

return false;
}

bool cache_mounts_detected(int cache_id)
void print_mounted_devices(const char *pattern, int cmplen)
{
return _device_mounts_detected(cache_id, -1);
}
FILE *mtab;
struct mntent *mstruct;
int no_match = 0, error = 0;

bool core_mounts_detected(int cache_id, int core_id)
{
return _device_mounts_detected(cache_id, core_id);
mtab = setmntent("/etc/mtab", "r");
if (!mtab) {
/* should exist, but if /etc/mtab not found we cannot print mounted devices */
return;
}

while ((mstruct = getmntent(mtab)) != NULL) {
error = strcmp_s(mstruct->mnt_fsname, cmplen, pattern, &no_match);
/* mstruct->mnt_fsname is /dev/... block device path, not a mountpoint */
if (error != EOK || no_match)
continue;

cas_printf(LOG_ERR, "%s\n", mstruct->mnt_fsname);
}
}

int remove_core(unsigned int cache_id, unsigned int core_id,
Expand All @@ -1866,7 +1866,22 @@ int remove_core(unsigned int cache_id, unsigned int core_id,
struct kcas_remove_core cmd;

/* don't even attempt ioctl if filesystem is mounted */
if (core_mounts_detected(cache_id, core_id)) {
bool mounts_detected = false;
int cmplen = 0;
char pattern[80];

/* verify if specific core is mounted */
cmplen = snprintf(pattern, sizeof(pattern), "/dev/cas%d-%d", cache_id, core_id);
if (!(mounts_detected = device_mounts_detected(pattern, cmplen))) {
/* verify if any partition of the core is mounted */
cmplen = snprintf(pattern, sizeof(pattern), "/dev/cas%d-%dp", cache_id, core_id) - 1;
mounts_detected = device_mounts_detected(pattern, cmplen);
}
if (mounts_detected) {
cas_printf(LOG_ERR, "Can't remove core %d from "
"cache %d due to mounted devices:\n",
core_id, cache_id);
print_mounted_devices(pattern, cmplen);
return FAILURE;
}

Expand Down

0 comments on commit 8d2d689

Please sign in to comment.