Commits

Ivan Voras  committed d9d432c

Rename ggatel to ggvd and split READ, WRITE, DELETE ops into separate functions

  • Participants
  • Parent commits c96d8d3
  • Tags starting_point

Comments (0)

Files changed (1)

 static unsigned sectorsize = 0;
 static unsigned timeout = G_GATE_TIMEOUT;
 
+
 static void
 usage(void)
 {
 	exit(EXIT_FAILURE);
 }
 
+
 static int
 g_gate_openflags(unsigned ggflags)
 {
 	return (O_RDWR);
 }
 
+
+/**
+ * Handles BIO_READ requests from ggate.
+ */
+static int
+ggvd_read(int fd, struct g_gate_ctl_io *ggio, size_t *bsize) {
+	int error = 0;
+
+	if ((size_t)ggio->gctl_length > *bsize) {
+		ggio->gctl_data = realloc(ggio->gctl_data, ggio->gctl_length);
+		if (ggio->gctl_data != NULL)
+			*bsize = ggio->gctl_length;
+		else
+			error = ENOMEM;
+	}
+	if (error == 0) {
+		if (pread(fd, ggio->gctl_data, ggio->gctl_length, ggio->gctl_offset) == -1)
+			error = errno;
+	}
+	return (error);
+}
+
+
+/**
+ * Handles BIO_WRITE requests from ggate.
+ */
+static int
+ggvd_write(int fd, struct g_gate_ctl_io *ggio, size_t *bsize) {
+	if (pwrite(fd, ggio->gctl_data, ggio->gctl_length, ggio->gctl_offset) == -1)
+		return (errno);
+	return (0);
+}
+
+
+/**
+ * Handles BIO_DELETE requests from ggate.
+ */
+static int
+ggvd_delete(int fd, struct g_gate_ctl_io *ggio, size_t *bsize) {
+	return ggvd_write(fd, ggio, bsize);
+}
+
+
 static void
-g_gatel_serve(int fd)
+ggvd_serve(int fd)
 {
 	struct g_gate_ctl_io ggio;
 	size_t bsize;
 			err(EXIT_FAILURE, "Cannot daemonize");
 		}
 	}
-	g_gate_log(LOG_DEBUG, "Worker created: %u.", getpid());
+	g_gate_log(LOG_DEBUG, "Worker process created: %u", getpid());
 	ggio.gctl_version = G_GATE_VERSION;
 	ggio.gctl_unit = unit;
 	bsize = sectorsize;
 			    strerror(error));
 		}
 
-		error = 0;
 		switch (ggio.gctl_cmd) {
 		case BIO_READ:
-			if ((size_t)ggio.gctl_length > bsize) {
-				ggio.gctl_data = realloc(ggio.gctl_data,
-				    ggio.gctl_length);
-				if (ggio.gctl_data != NULL)
-					bsize = ggio.gctl_length;
-				else
-					error = ENOMEM;
-			}
-			if (error == 0) {
-				if (pread(fd, ggio.gctl_data, ggio.gctl_length,
-				    ggio.gctl_offset) == -1) {
-					error = errno;
-				}
-			}
+			error = ggvd_read(fd, &ggio, &bsize);
+			break;
+		case BIO_WRITE:
+			error = ggvd_write(fd, &ggio, &bsize);
 			break;
 		case BIO_DELETE:
-		case BIO_WRITE:
-			if (pwrite(fd, ggio.gctl_data, ggio.gctl_length,
-			    ggio.gctl_offset) == -1) {
-				error = errno;
-			}
+			error = ggvd_delete(fd, &ggio, &bsize);
 			break;
 		default:
 			error = EOPNOTSUPP;
 }
 
 static void
-g_gatel_create(void)
+ggvd_create(void)
 {
 	struct g_gate_ctl_create ggioc;
 	int fd;
 	ggioc.gctl_mediasize = g_gate_mediasize(fd);
 	if (sectorsize == 0)
 		sectorsize = g_gate_sectorsize(fd);
+	if (ggioc.gctl_mediasize % sectorsize != 0)
+		warnx("Device size is not a multiple of sector size: "
+		    "%jd, %u", (intmax_t)ggioc.gctl_mediasize, sectorsize);
 	ggioc.gctl_sectorsize = sectorsize;
 	ggioc.gctl_timeout = timeout;
 	ggioc.gctl_flags = flags;
 	if (unit == -1)
 		printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
 	unit = ggioc.gctl_unit;
-	g_gatel_serve(fd);
+	ggvd_serve(fd);
 }
 
 static void
-g_gatel_rescue(void)
+ggvd_rescue(void)
 {
 	struct g_gate_ctl_cancel ggioc;
 	int fd;
 	ggioc.gctl_seq = 0;
 	g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
 
-	g_gatel_serve(fd);
+	ggvd_serve(fd);
 }
 
 int
 		g_gate_load_module();
 		g_gate_open_device();
 		path = argv[0];
-		g_gatel_create();
+		ggvd_create();
 		break;
 	case RESCUE:
 		if (argc != 1)
 		}
 		g_gate_open_device();
 		path = argv[0];
-		g_gatel_rescue();
+		ggvd_rescue();
 		break;
 	case DESTROY:
 		if (unit == -1) {