Skip to content

Commit

Permalink
Switch from ioctl to dm target messages for PID blacklist/whitelist m…
Browse files Browse the repository at this point in the history
…anipulation

In mainline 4.4 the ioctl() callback in the DM target has been removed
in favour of a prepare_ioctl() which selects the underlying device,
all ioctls are assume to apply to that.  In discussions with upstream on
resolving this it was suggested that the correct mechanism for this
kind of target focussed ioctl is actually DM target messages.  This
patch converts the blacklist/whitelist manipulation over to these DM
target messages.

It also adds rather primative support to the flashcache_setioctl helper to
switch to DM target messages when the existing ioctls are not supported
(ENOTTY).  This is handled by calling out to dmsetup which offers a
message command to form these requests.

I would envisage it would be possible to reduce flashcache_setioctl to a
simple shell script in the future once there is no possibility of these
tools being used with a kernel supporting the ioctl only.

This should fix the hard parts of issue facebookarchive#215.

fixes: facebookarchive#215
Signed-off-by: Andy Whitcroft <apw@ubuntu.com>
  • Loading branch information
Andy Whitcroft committed Feb 1, 2016
1 parent ca7a593 commit da72604
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/flashcache_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,12 @@ static struct target_type flashcache_target = {
.dtr = flashcache_dtr,
.map = flashcache_map,
.status = flashcache_status,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,4,0)
.ioctl = flashcache_ioctl,
#else
.prepare_ioctl = flashcache_prepare_ioctl,
.message = flashcache_message,
#endif
.iterate_devices = flashcache_iterate_devices,
};

Expand Down
78 changes: 78 additions & 0 deletions src/flashcache_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,82 @@ skip_sequential_io(struct cache_c *dmc, struct bio *bio)
* exit, for cases where the process dies after marking itself
* non-cacheable.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
int
flashcache_prepare_ioctl(struct dm_target *ti,
struct block_device **bdev, fmode_t *mode)
{
struct cache_c *dmc = (struct cache_c *) ti->private;

*bdev = dmc->disk_dev->bdev;

return 0;
}

int
flashcache_message(struct dm_target *ti, unsigned argc, char **argv)
{
struct cache_c *dmc = (struct cache_c *) ti->private;
int list;
long long upid;
pid_t pid = 0;

/*
* whitelist|blacklist add|del <pid>
* whitelist|blacklist delall
*/
if (argc < 2)
return -EINVAL;

/* Decode the primary command. */
if (strcmp(argv[0], "whitelist") == 0) {
list = FLASHCACHE_WHITELIST;
} else if (strcmp(argv[0], "blacklist") == 0) {
list = FLASHCACHE_BLACKLIST;
} else {
return -EINVAL;
}

/* Decode the sub-command. */
if (strcmp(argv[1], "add") == 0) {
int rr;
if (argc != 3)
return -EINVAL;

/* Decode the pid. */
if (kstrtoull(argv[2], 10, &upid))
return -EINVAL;
pid = (pid_t)upid;

flashcache_add_pid(dmc, pid, list);
return 0;

} else if (strcmp(argv[1], "del") == 0) {
if (argc != 3)
return -EINVAL;

/* Decode the pid. */
if (kstrtoull(argv[2], 10, &upid))
return -EINVAL;
pid = (pid_t)upid;

flashcache_del_pid(dmc, pid, list);
return 0;

} else if (strcmp(argv[1], "delall") == 0) {
if (argc != 2)
return -EINVAL;

flashcache_del_all_pids(dmc, list, 0);
return 0;

} else {
return -EINVAL;
}
}

#else

int
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
flashcache_ioctl(struct dm_target *ti, struct inode *inode,
Expand Down Expand Up @@ -561,3 +637,5 @@ flashcache_ioctl(struct dm_target *ti, unsigned int cmd, unsigned long arg)
}

}

#endif
6 changes: 6 additions & 0 deletions src/flashcache_ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ enum {
#define FLASHCACHEDELALLWHITELIST _IOW(FLASHCACHE_IOCTL, FLASHCACHEDELWHITELISTALL_CMD, pid_t)

#ifdef __KERNEL__
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
int flashcache_message(struct dm_target *ti, unsigned argc, char **argv);
int flashcache_prepare_ioctl(struct dm_target *ti,
struct block_device **bdev, fmode_t *mode);
#else
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
int flashcache_ioctl(struct dm_target *ti, struct inode *inode,
struct file *filp, unsigned int cmd,
Expand All @@ -59,6 +64,7 @@ int flashcache_ioctl(struct dm_target *ti, struct inode *inode,
int flashcache_ioctl(struct dm_target *ti, unsigned int cmd,
unsigned long arg);
#endif
#endif
void flashcache_pid_expiry_all_locked(struct cache_c *dmc);
int flashcache_uncacheable(struct cache_c *dmc, struct bio *bio);
void seq_io_remove_from_lru(struct cache_c *dmc, struct sequential_io *seqio);
Expand Down
60 changes: 59 additions & 1 deletion src/utils/flashcache_setioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <linux/types.h>
#include <flashcache_ioctl.h>

Expand All @@ -42,6 +43,55 @@ void usage(char *pname)
exit(1);
}

void dm_message(char *cachedev, char list, char action, pid_t pid)
{
char pidstr[32];

char *argv[] = {
"/sbin/dmsetup",
"message",
cachedev,
"0",
/* command */ NULL,
/* sub-command */ NULL,
/* pid */ NULL,
NULL,
};

switch (list) {
case 'w':
argv[4] = "whitelist";
break;
case 'b':
argv[4] = "blacklist";
break;
default:
return;
}

switch (action) {
case 'a':
argv[5] = "add";
break;
case 'r':
argv[5] = "del";
break;
case 'c':
argv[5] = "delall";
break;
}

switch (action) {
case 'a':
case 'r':
snprintf(pidstr, sizeof(pidstr), "%lld", (long long)pid);
argv[6] = pidstr;
break;
}

execv(argv[0], argv);
}

int
main(int argc, char **argv)
{
Expand All @@ -50,6 +100,7 @@ main(int argc, char **argv)
intmax_t pidmax;
char *tmp;
pid_t pid;
int err;

while ((c = getopt(argc, argv, "carb:w:")) != -1) {
switch (c) {
Expand Down Expand Up @@ -125,11 +176,18 @@ main(int argc, char **argv)
break;
}
}
err = errno;
close(cache_fd);
/*
* Failed with an error indicating the ioctl was not appropriate for the device
* switch to using DM messages.
*/
if (result < 0 && err == ENOTTY) {
dm_message(cachedev, list, action, pid);
}
if (result < 0) {
fprintf(stderr, "ioctl failed on %s\n", cachedev);
exit(1);
}
return 0;
}

0 comments on commit da72604

Please sign in to comment.