| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/dm-ioctl.h> |
| 5 |
#include <sys/ioctl.h> |
| 6 |
|
| 7 |
#include "dm-util.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "string-util.h" |
| 10 |
|
| 11 |
int dm_deferred_remove_cancel(const char *name) { |
| 12 |
_cleanup_close_ int fd = -EBADF; |
| 13 |
|
| 14 |
struct combined { |
| 15 |
struct dm_ioctl dm_ioctl; |
| 16 |
struct dm_target_msg dm_target_msg; |
| 17 |
} _packed_; |
| 18 |
|
| 19 |
union message { |
| 20 |
struct combined combined; |
| 21 |
struct { |
| 22 |
uint8_t space[offsetof(struct combined, dm_target_msg.message)]; |
| 23 |
char text[STRLEN("@cancel_deferred_remove") + 1]; |
| 24 |
} _packed_; |
| 25 |
} message = { |
| 26 |
.combined.dm_ioctl = { |
| 27 |
.version = { |
| 28 |
DM_VERSION_MAJOR, |
| 29 |
DM_VERSION_MINOR, |
| 30 |
DM_VERSION_PATCHLEVEL |
| 31 |
}, |
| 32 |
.data_size = sizeof(union message), |
| 33 |
.data_start = offsetof(union message, combined.dm_target_msg), |
| 34 |
}, |
| 35 |
}; |
| 36 |
|
| 37 |
assert(name); |
| 38 |
|
| 39 |
if (strlen(name) >= sizeof(message.combined.dm_ioctl.name)) |
| 40 |
return -ENODEV; /* A device with a name longer than this cannot possibly exist */ |
| 41 |
|
| 42 |
strncpy_exact(message.combined.dm_ioctl.name, name, sizeof(message.combined.dm_ioctl.name)); |
| 43 |
strncpy_exact(message.text, "@cancel_deferred_remove", sizeof(message.text)); |
| 44 |
|
| 45 |
fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC); |
| 46 |
if (fd < 0) |
| 47 |
return -errno; |
| 48 |
|
| 49 |
if (ioctl(fd, DM_TARGET_MSG, &message)) |
| 50 |
return -errno; |
| 51 |
|
| 52 |
return 0; |
| 53 |
} |