Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #61 from lastguru1/upstream

Tool to set whitelist/blacklist ioctl
  • Loading branch information...
commit d741759b447d6ce96c014e1c937197dd35735afd 2 parents ef37f60 + 041416a
Mohan Srinivasan mohans authored
Showing with 140 additions and 1 deletion.
  1. +6 −1 src/utils/Makefile
  2. +134 −0 src/utils/flashcache_setioctl.c
7 src/utils/Makefile
View
@@ -1,6 +1,6 @@
COMMIT_REV ?= $(shell git describe --always --abbrev=12)
CFLAGS += -I.. -I. -DCOMMIT_REV="\"$(COMMIT_REV)\"" -g
-PROGRAMS += flashcache_create flashcache_destroy flashcache_load
+PROGRAMS += flashcache_create flashcache_destroy flashcache_load flashcache_setioctl
INSTALL_DIR = $(DESTDIR)/sbin/
.PHONY:all
@@ -21,6 +21,11 @@ flashcache_load: flashcache_load.o
-include flashcache_load.d
+flashcache_setioctl: flashcache_setioctl.o
+ $(CC) $^ -o $@
+
+-include flashcache_setioctl.d
+
%.o: %.c
$(CC) -c $(CFLAGS) $*.c -o $*.o
@$(CC) -MM $(CFLAGS) -MF $*.d -MT $*.o $*.c
134 src/utils/flashcache_setioctl.c
View
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2012, Dmitry Golubev
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name Facebook nor the names of its contributors may be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <linux/types.h>
+#include <flashcache_ioctl.h>
+
+void usage(char *pname)
+{
+ fprintf(stderr, "Usage: %s (-c | -a | -r) (-b pid |-w pid) ssd_devname \n", pname);
+ exit(1);
+}
+
+main(int argc, char **argv)
+{
+ int cache_fd, c, result;
+ char action = ' ', list = ' ', *cachedev, *pname = argv[0];
+ intmax_t pidmax;
+ char *tmp;
+ pid_t pid;
+
+ while ((c = getopt(argc, argv, "carb:w:")) != -1) {
+ switch (c) {
+ case 'c':
+ action = 'c';
+ break;
+ case 'a':
+ action = 'a';
+ break;
+ case 'r':
+ action = 'r';
+ break;
+ case 'b':
+ list = 'b';
+ pidmax = strtoimax(optarg, &tmp, 10);
+ if(tmp == optarg || *tmp != '\0' || pidmax != (pid_t)pidmax) {
+ fprintf(stderr, "Bad PID!\n");
+ exit(1);
+ }
+ else {
+ pid = (pid_t)pidmax;
+ }
+ break;
+ case 'w':
+ list = 'w';
+ pidmax = strtoimax(optarg, &tmp, 10);
+ if(tmp == optarg || *tmp != '\0' || pidmax != (pid_t)pidmax) {
+ fprintf(stderr, "Bad PID!\n");
+ exit(1);
+ }
+ else {
+ pid = (pid_t)pidmax;
+ }
+ break;
+ case '?':
+ usage(pname);
+ }
+ }
+ if (action == ' ')
+ usage(pname);
+ if (list == ' ')
+ usage(pname);
+ if (optind == argc)
+ usage(pname);
+ cachedev = argv[optind++];
+ cache_fd = open(cachedev, O_RDONLY);
+ if (cache_fd < 0) {
+ fprintf(stderr, "Failed to open %s\n", cachedev);
+ exit(1);
+ }
+ if (list == 'w') {
+ switch (action) {
+ case 'a':
+ result=ioctl(cache_fd, FLASHCACHEADDWHITELIST, &pid);
+ break;
+ case 'r':
+ result=ioctl(cache_fd, FLASHCACHEDELWHITELIST, &pid);
+ break;
+ case 'c':
+ result=ioctl(cache_fd, FLASHCACHEDELALLWHITELIST, &pid);
+ break;
+ }
+ }
+ else {
+ switch (action) {
+ case 'a':
+ result=ioctl(cache_fd, FLASHCACHEADDBLACKLIST, &pid);
+ break;
+ case 'r':
+ result=ioctl(cache_fd, FLASHCACHEDELBLACKLIST, &pid);
+ break;
+ case 'c':
+ result=ioctl(cache_fd, FLASHCACHEDELALLBLACKLIST, &pid);
+ break;
+ }
+ }
+ close(cache_fd);
+ if (result < 0) {
+ fprintf(stderr, "ioctl failed on %s\n", cachedev);
+ exit(1);
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.