Skip to content

Commit

Permalink
libglusterfs: add function to unlink but tolerate ENOENT (gluster#3573)
Browse files Browse the repository at this point in the history
Introduce 'gf_unlink()' to wrap 'sys_unlink()' but tolerate
ENOENT, and use the former where appropriate. Related cleanups
as usual.

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
Updates: gluster#1000
  • Loading branch information
dmantipov authored Jul 4, 2022
1 parent a005a54 commit 2c827e5
Show file tree
Hide file tree
Showing 22 changed files with 52 additions and 109 deletions.
17 changes: 17 additions & 0 deletions libglusterfs/src/common-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -4304,3 +4304,20 @@ gf_set_nofile(rlim_t high, rlim_t low)
}

#endif /* not GF_DARWIN_HOST_OS */

/* Like sys_unlink() but tolerate ENOENT. */

gf_boolean_t
gf_unlink(const char *path)
{
int ret = 0;

GF_ASSERT(path);
ret = sys_unlink(path);

if (ret && errno != ENOENT) {
GF_LOG_W(THIS->name, LG_MSG_UNLINK_FAILED(path));
return _gf_false;
}
return _gf_true;
}
3 changes: 3 additions & 0 deletions libglusterfs/src/glusterfs/common-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,9 @@ fop_log_level(glusterfs_fop_t fop, int op_errno);
int
recursive_rmdir(const char *delete_path);

gf_boolean_t
gf_unlink(const char *path);

int
gf_get_index_by_elem(char **array, char *elem);

Expand Down
5 changes: 5 additions & 0 deletions libglusterfs/src/glusterfs/libglusterfs-messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ GLFS_NEW(LIBGLUSTERFS, LG_MSG_NOFILE_FAILED,
GLFS_I64(low), GLFS_I64(high)
)

GLFS_NEW(LIBGLUSTERFS, LG_MSG_UNLINK_FAILED,
"Failed to remove file", 1,
GLFS_STR(path)
)

#define LG_MSG_EPOLL_FD_CREATE_FAILED_STR "epoll fd creation failed"
#define LG_MSG_INVALID_POLL_IN_STR "invalid poll_in value"
#define LG_MSG_INVALID_POLL_OUT_STR "invalid poll_out value"
Expand Down
1 change: 1 addition & 0 deletions libglusterfs/src/libglusterfs.sym
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ gf_tw_mod_timer
gf_tw_mod_timer_pending
gf_uint64_2human_readable
gf_umount_lazy
gf_unlink
gf_update_latency
gf_uuid_clear
gf_uuid_compare
Expand Down
8 changes: 1 addition & 7 deletions libglusterfs/src/store.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,7 @@ gf_store_unlink_tmppath(gf_store_handle_t *shandle)
GF_VALIDATE_OR_GOTO("store", shandle->path, out);

snprintf(tmppath, sizeof(tmppath), "%s.tmp", shandle->path);
ret = sys_unlink(tmppath);
if (ret && (errno != ENOENT)) {
gf_msg("", GF_LOG_ERROR, errno, LG_MSG_FILE_OP_FAILED,
"Failed to mv %s to %s", tmppath, shandle->path);
} else {
ret = 0;
}
ret = gf_unlink(tmppath) ? 0 : -1;
out:
if (shandle && shandle->tmp_fd >= 0) {
sys_close(shandle->tmp_fd);
Expand Down
3 changes: 1 addition & 2 deletions xlators/cluster/dht/src/dht-messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ GLFS_MSGID(
DHT_MSG_CHUNK_SIZE_INFO, DHT_MSG_LAYOUT_FORM_FAILED, DHT_MSG_SUBVOL_ERROR,
DHT_MSG_REGEX_INFO, DHT_MSG_FOPEN_FAILED,
DHT_MSG_SET_HOSTNAME_FAILED, DHT_MSG_BRICK_ERROR, DHT_MSG_SYNCOP_FAILED,
DHT_MSG_MIGRATE_INFO, DHT_MSG_SOCKET_ERROR, DHT_MSG_CREATE_FD_FAILED,
DHT_MSG_MIGRATE_INFO, DHT_MSG_UNUSED_PLACEHOLDER, DHT_MSG_CREATE_FD_FAILED,
DHT_MSG_READDIR_ERROR, DHT_MSG_CHILD_LOC_BUILD_FAILED,
DHT_MSG_SET_SWITCH_PATTERN_ERROR, DHT_MSG_COMPUTE_HASH_FAILED,
DHT_MSG_FIND_LAYOUT_ANOMALIES_ERROR, DHT_MSG_ANOMALIES_INFO,
Expand Down Expand Up @@ -205,7 +205,6 @@ GLFS_MSGID(
#define DHT_MSG_CLANUP_SOURCE_FILE_FAILED_STR "failed to cleanup source file"
#define DHT_MSG_UNLOCK_FILE_FAILED_STR "failed to unlock file"
#define DHT_MSG_REMOVE_XATTR_FAILED_STR "remove xattr failed"
#define DHT_MSG_SOCKET_ERROR_STR "Failed to unlink listener socket"
#define DHT_MSG_HASHED_SUBVOL_GET_FAILED_STR "Failed to get hashed subvolume"
#define DHT_MSG_CACHED_SUBVOL_GET_FAILED_STR "Failed to get cached subvolume"
#define DHT_MSG_MIGRATE_DATA_FAILED_STR "migrate-data failed"
Expand Down
20 changes: 4 additions & 16 deletions xlators/cluster/dht/src/dht-rebalance.c
Original file line number Diff line number Diff line change
Expand Up @@ -2403,30 +2403,19 @@ dht_start_rebalance_task(xlator_t *this, call_frame_t *frame)
return ret;
}

int
static void
gf_listener_stop(xlator_t *this)
{
glusterfs_ctx_t *ctx = NULL;
cmd_args_t *cmd_args = NULL;
int ret = 0;

ctx = this->ctx;
GF_ASSERT(ctx);
cmd_args = &ctx->cmd_args;
if (cmd_args->sock_file) {
ret = sys_unlink(cmd_args->sock_file);
if (ret && (ENOENT == errno)) {
ret = 0;
}
}
GF_ASSERT(cmd_args);

if (ret) {
gf_msg(this->name, GF_LOG_ERROR, errno, DHT_MSG_SOCKET_ERROR,
"Failed to unlink listener "
"socket %s",
cmd_args->sock_file);
}
return ret;
if (cmd_args->sock_file)
gf_unlink(cmd_args->sock_file);
}

static void
Expand Down Expand Up @@ -4390,7 +4379,6 @@ static int
gf_defrag_done(int ret, call_frame_t *sync_frame, void *data)
{
gf_listener_stop(sync_frame->this);

STACK_DESTROY(sync_frame->root);
kill(getpid(), SIGTERM);
return 0;
Expand Down
5 changes: 1 addition & 4 deletions xlators/features/bit-rot/src/stub/bit-rot-stub-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ br_stub_del(xlator_t *this, uuid_t gfid)
EINVAL);
snprintf(gfid_path, sizeof(gfid_path), "%s/%s", priv->stub_basepath,
uuid_utoa(gfid));
ret = sys_unlink(gfid_path);
if (ret && (errno != ENOENT)) {
gf_smsg(this->name, GF_LOG_ERROR, errno, BRS_MSG_BAD_OBJ_UNLINK_FAIL,
"path=%s", gfid_path, NULL);
if (!gf_unlink(gfid_path)) {
ret = -errno;
goto out;
}
Expand Down
4 changes: 1 addition & 3 deletions xlators/features/bit-rot/src/stub/bit-rot-stub-messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ GLFS_MSGID(BITROT_STUB, BRS_MSG_NO_MEMORY, BRS_MSG_SET_EVENT_FAILED,
BRS_MSG_BAD_OBJECT_DIR_READ_FAIL, BRS_MSG_GET_FD_CONTEXT_FAILED,
BRS_MSG_BAD_HANDLE_DIR_NULL, BRS_MSG_BAD_OBJ_THREAD_FAIL,
BRS_MSG_BAD_OBJ_DIR_CLOSE_FAIL, BRS_MSG_LINK_FAIL,
BRS_MSG_BAD_OBJ_UNLINK_FAIL, BRS_MSG_DICT_SET_FAILED,
BRS_MSG_UNUSED_PLACEHOLDER, BRS_MSG_DICT_SET_FAILED,
BRS_MSG_PATH_GET_FAILED, BRS_MSG_NULL_LOCAL,
BRS_MSG_SPAWN_SIGN_THRD_FAILED, BRS_MSG_KILL_SIGN_THREAD,
BRS_MSG_NON_BITD_PID, BRS_MSG_SIGN_PREPARE_FAIL,
Expand Down Expand Up @@ -90,8 +90,6 @@ GLFS_MSGID(BITROT_STUB, BRS_MSG_NO_MEMORY, BRS_MSG_SET_EVENT_FAILED,
#define BRS_MSG_CREATE_FRAME_FAILED_STR "create_frame() failure"
#define BRS_MSG_BAD_OBJ_DIR_CLOSE_FAIL_STR "closedir error"
#define BRS_MSG_LINK_FAIL_STR "failed to record gfid"
#define BRS_MSG_BAD_OBJ_UNLINK_FAIL_STR \
"failed to delete bad object link from quaratine directory"
#define BRS_MSG_BAD_OBJECT_DIR_FAIL_STR "failed stub directory"
#define BRS_MSG_BAD_OBJECT_DIR_SEEK_FAIL_STR \
"seekdir failed. Invalid argument (offset reused from another DIR * " \
Expand Down
8 changes: 1 addition & 7 deletions xlators/features/index/src/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,6 @@ index_entry_create(xlator_t *this, inode_t *inode, char *filename)
int
index_entry_delete(xlator_t *this, uuid_t pgfid, char *filename)
{
int ret = 0;
int op_errno = 0;
char pgfid_path[PATH_MAX] = {0};
char entry_path[PATH_MAX] = {0};
Expand Down Expand Up @@ -929,13 +928,8 @@ index_entry_delete(xlator_t *this, uuid_t pgfid, char *filename)
goto out;
}

ret = sys_unlink(entry_path);
if (ret && (errno != ENOENT)) {
if (!gf_unlink(entry_path))
op_errno = errno;
gf_msg(this->name, GF_LOG_ERROR, op_errno, INDEX_MSG_INDEX_DEL_FAILED,
"%s: failed to delete from index/entry-changes", entry_path);
}

out:
return -op_errno;
}
Expand Down
6 changes: 2 additions & 4 deletions xlators/mgmt/glusterd/src/glusterd-proc-mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ glusterd_proc_stop(glusterd_proc_t *proc, int sig, int flags)
proc->name, strerror(errno));
}
} else {
(void)glusterd_unlink_file(proc->pidfile);
gf_unlink(proc->pidfile);
}
if (flags != PROC_STOP_FORCE)
goto out;
Expand All @@ -129,9 +129,7 @@ glusterd_proc_stop(glusterd_proc_t *proc, int sig, int flags)
pid, strerror(errno));
goto out;
}
ret = glusterd_unlink_file(proc->pidfile);
if (ret)
goto out;
gf_unlink(proc->pidfile);
}

ret = 0;
Expand Down
2 changes: 1 addition & 1 deletion xlators/mgmt/glusterd/src/glusterd-shd-svc-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ glusterd_shd_svcproc_cleanup(glusterd_shdsvc_t *shd)
svc->svc_proc = NULL;
svc->inited = _gf_false;
cds_list_del_init(&svc->mux_svc);
glusterd_unlink_file(svc->proc.pidfile);
gf_unlink(svc->proc.pidfile);

if (svc_proc && cds_list_empty(&svc_proc->svcs)) {
cds_list_del_init(&svc_proc->svc_proc_list);
Expand Down
4 changes: 2 additions & 2 deletions xlators/mgmt/glusterd/src/glusterd-shd-svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ glusterd_shdsvc_create_volfile(glusterd_volinfo_t *volinfo)
/* If volfile exist, delete it. This case happens when we
* change from replica/ec to distribute.
*/
(void)glusterd_unlink_file(filepath);
gf_unlink(filepath);
ret = 0;
goto out;
}
Expand Down Expand Up @@ -774,7 +774,7 @@ glusterd_shdsvc_stop(glusterd_svc_t *svc, int sig)
volinfo->volname, glusterd_proc_get_pid(&svc->proc));
}
svc->online = _gf_false;
(void)glusterd_unlink_file((char *)svc->proc.pidfile);
gf_unlink(svc->proc.pidfile);
glusterd_shd_svcproc_cleanup(shd);
ret = 0;
glusterd_volinfo_unref(volinfo);
Expand Down
15 changes: 2 additions & 13 deletions xlators/mgmt/glusterd/src/glusterd-store.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,17 +646,8 @@ glusterd_store_delete_brick(glusterd_brickinfo_t *brickinfo, char *delete_path)

GF_FREE(tmppath);

ret = sys_unlink(brickpath);
ret = gf_unlink(brickpath) ? 0 : -1;

if ((ret < 0) && (errno != ENOENT)) {
gf_msg_debug(this->name, 0, "Unlink failed on %s", brickpath);
ret = -1;
goto out;
} else {
ret = 0;
}

out:
if (brickinfo->shandle) {
gf_store_handle_destroy(brickinfo->shandle);
brickinfo->shandle = NULL;
Expand Down Expand Up @@ -4335,9 +4326,7 @@ glusterd_store_delete_peerinfo(glusterd_peerinfo_t *peerinfo)
goto out;
}

ret = sys_unlink(filepath);
if (ret && (errno == ENOENT))
ret = 0;
ret = gf_unlink(filepath) ? 0 : -1;

out:
if (peerinfo && peerinfo->shandle) {
Expand Down
2 changes: 1 addition & 1 deletion xlators/mgmt/glusterd/src/glusterd-svc-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ glusterd_shd_svc_mux_init(glusterd_volinfo_t *volinfo, glusterd_svc_t *svc)
out:
if (stop_daemon) {
glusterd_proc_stop(&svc->proc, SIGTERM, PROC_STOP_FORCE);
glusterd_unlink_file(pidfile);
gf_unlink(pidfile);
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion xlators/mgmt/glusterd/src/glusterd-svc-mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ glusterd_svc_stop(glusterd_svc_t *svc, int sig)

if (ret == 0) {
svc->online = _gf_false;
(void)glusterd_unlink_file((char *)svc->conn.sockpath);
gf_unlink(svc->conn.sockpath);
}
gf_msg(THIS->name, GF_LOG_INFO, 0, GD_MSG_SVC_STOP_SUCCESS,
"%s service is stopped", svc->name);
Expand Down
21 changes: 1 addition & 20 deletions xlators/mgmt/glusterd/src/glusterd-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ glusterd_volume_start_glusterfs(glusterd_volinfo_t *volinfo,
*/
glusterd_set_brick_socket_filepath(volinfo, brickinfo, socketpath,
sizeof(socketpath));
(void)glusterd_unlink_file(socketpath);
gf_unlink(socketpath);
rpc = brickinfo->rpc;
if (rpc) {
brickinfo->rpc = NULL;
Expand Down Expand Up @@ -5350,25 +5350,6 @@ glusterd_pending_node_put_rpc(glusterd_pending_node_t *pending_node)
}
}

int32_t
glusterd_unlink_file(char *sockfpath)
{
int ret = 0;

ret = sys_unlink(sockfpath);
if (ret) {
if (ENOENT == errno)
ret = 0;
else
gf_msg(THIS->name, GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED,
"Failed to remove %s"
" error: %s",
sockfpath, strerror(errno));
}

return ret;
}

#ifdef BUILD_GNFS
void
glusterd_nfs_pmap_deregister()
Expand Down
3 changes: 0 additions & 3 deletions xlators/mgmt/glusterd/src/glusterd-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,6 @@ glusterd_launch_synctask(synctask_fn_t fn, void *opaque);
int
glusterd_enable_default_options(glusterd_volinfo_t *volinfo, char *option);

int
glusterd_unlink_file(char *sock_file_path);

int32_t
glusterd_find_brick_mount_path(char *brick_path, char **brick_mount_path);

Expand Down
10 changes: 1 addition & 9 deletions xlators/mgmt/glusterd/src/glusterd-volgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -5539,16 +5539,8 @@ generate_brick_volfiles(glusterd_volinfo_t *volinfo)
}
}
} else {
ret = sys_unlink(tstamp_file);
if (ret == -1 && errno == ENOENT)
ret = 0;
if (ret == -1) {
gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED,
"failed to unlink "
"%s",
tstamp_file);
if (!gf_unlink(tstamp_file))
return -1;
}
}

ret = glusterd_volume_brick_for_each(volinfo, NULL,
Expand Down
8 changes: 1 addition & 7 deletions xlators/mount/fuse/src/fuse-bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -6841,14 +6841,8 @@ init(xlator_t *this_xl)
priv->fuse_dump_fd = -1;
ret = dict_get_str(options, "dump-fuse", &value_string);
if (ret == 0) {
ret = sys_unlink(value_string);
if (ret == -1 && errno != ENOENT) {
gf_log("glusterfs-fuse", GF_LOG_ERROR,
"failed to remove old fuse dump file %s: %s", value_string,
strerror(errno));

if (!gf_unlink(value_string))
goto cleanup_exit;
}
ret = open(value_string, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (ret == -1) {
gf_log("glusterfs-fuse", GF_LOG_ERROR,
Expand Down
2 changes: 1 addition & 1 deletion xlators/nfs/server/src/nfs-messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ GLFS_MSGID(
NFS_MSG_CLNT_CREATE_ERROR, NFS_MSG_NLM_GRACE_PERIOD, NFS_MSG_RPC_CLNT_ERROR,
NFS_MSG_GET_PORT_ERROR, NFS_MSG_NLMCLNT_NOT_FOUND, NFS_MSG_FD_LOOKUP_NULL,
NFS_MSG_SM_NOTIFY, NFS_MSG_NLM_INIT_FAIL, NFS_MSG_START_ERROR,
NFS_MSG_UNLINK_ERROR, NFS_MSG_SHARE_LIST_STORE_FAIL,
NFS_MSG_UNUSED_PLACEHOLDER, NFS_MSG_SHARE_LIST_STORE_FAIL,
NFS_MSG_CLIENT_NOT_FOUND, NFS_MSG_SHARE_CALL_FAIL,
NFS_MSG_UNSHARE_CALL_FAIL, NFS_MSG_GET_PID_FAIL, NFS_MSG_ARG_FREE_FAIL,
NFS_MSG_PMAP_UNSET_FAIL, NFS_MSG_UDP_SERV_FAIL, NFS_MSG_REG_NLMCBK_FAIL,
Expand Down
12 changes: 4 additions & 8 deletions xlators/nfs/server/src/nlm4.c
Original file line number Diff line number Diff line change
Expand Up @@ -2664,10 +2664,8 @@ nlm4svc_init(xlator_t *nfsx)
instead. This is still a theory but we need to thoroughly test it
out. Until then NLM support is non-existent on OSX.
*/
ret = sys_unlink(GF_SM_NOTIFY_PIDFILE);
if (ret == -1 && errno != ENOENT) {
gf_msg(GF_NLM, GF_LOG_ERROR, errno, NFS_MSG_UNLINK_ERROR,
"unable to unlink %s: %d", GF_SM_NOTIFY_PIDFILE, errno);
if (!gf_unlink(GF_SM_NOTIFY_PIDFILE)) {
ret = -1;
goto err;
}
/* temporary work around to restart statd, not distro/OS independent.
Expand Down Expand Up @@ -2701,10 +2699,8 @@ nlm4svc_init(xlator_t *nfsx)
ret = runcmd(KILLALL_CMD, "-9", "rpc.statd", NULL);
}

ret = sys_unlink(GF_RPC_STATD_PIDFILE);
if (ret == -1 && errno != ENOENT) {
gf_msg(GF_NLM, GF_LOG_ERROR, errno, NFS_MSG_UNLINK_ERROR,
"unable to unlink %s", pid_file);
if (!gf_unlink(GF_RPC_STATD_PIDFILE)) {
ret = -1;
goto err;
}

Expand Down

0 comments on commit 2c827e5

Please sign in to comment.