[DRBD-cvs] r1595 - trunk/user

svn at svn.drbd.org svn at svn.drbd.org
Fri Oct 15 23:12:23 CEST 2004


Author: phil
Date: 2004-10-15 23:12:21 +0200 (Fri, 15 Oct 2004)
New Revision: 1595

Modified:
   trunk/user/Makefile
   trunk/user/drbdmeta.c
   trunk/user/drbdsetup.c
   trunk/user/drbdtool_common.c
   trunk/user/drbdtool_common.h
Log:
drbdmeta can read format-07 generation counts,
and can print them by now.


Modified: trunk/user/Makefile
===================================================================
--- trunk/user/Makefile	2004-10-15 18:11:14 UTC (rev 1594)
+++ trunk/user/Makefile	2004-10-15 21:12:21 UTC (rev 1595)
@@ -17,7 +17,7 @@
 # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 #
 
-CFLAGS = -c -Wall -I../drbd $(shell glib-config --cflags)
+CFLAGS = -g -c -Wall -I../drbd $(shell glib-config --cflags)
 CC = gcc
 
 drbdadm-obj = drbdadm_scanner.o drbdadm_parser.o drbdadm_main.o \
@@ -53,7 +53,7 @@
 	rm -f drbdadm_scanner.c
 	rm -f drbdadm_parser.c
 	rm -f drbdadm_parser.h
-	rm -f drbdsetup drbdadm *.o
+	rm -f drbdsetup drbdadm drbdmeta *.o
 	rm -f *~
 
 distclean: clean

Modified: trunk/user/drbdmeta.c
===================================================================
--- trunk/user/drbdmeta.c	2004-10-15 18:11:14 UTC (rev 1594)
+++ trunk/user/drbdmeta.c	2004-10-15 21:12:21 UTC (rev 1595)
@@ -21,10 +21,14 @@
    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
  */
+#include <linux/fs.h> // for BLKGETSIZE64
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/ioctl.h>
 #include <fcntl.h>
+#define __USE_LARGEFILE64
 #include <unistd.h>
+#include <errno.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -160,10 +164,24 @@
 int v07_open(conf_t config)
 {
 	struct conf_07* cfg = (struct conf_07*) config;
+	struct stat sb;
 
 	cfg->fd = open(cfg->device_name,O_RDWR);
 
-	return (cfg->fd != -1) ;
+	if(!cfg->fd == -1) return 0;
+
+	if(fstat(cfg->fd, &sb)) {
+		PERROR("fstat() failed");
+		exit(20);
+	}
+
+	if(!S_ISBLK(sb.st_mode)) {
+		fprintf(stderr, "'%s' is not a block device!\n", 
+			cfg->device_name);
+		exit(20);
+	}
+
+	return 1;
 }
 
 int v07_close(conf_t config)
@@ -178,7 +196,7 @@
 	struct meta_data *m;
 
 	m = malloc(sizeof(struct meta_data ));
-	memset(m,sizeof(struct meta_data ),1);
+	memset(m,0,sizeof(struct meta_data ));
   
 	return m;  
 }
@@ -192,15 +210,55 @@
 	free(m);
 }
 
+#define MD_RESERVED_SIZE_07 ( (typeof(guint64))128 * (1<<20) )
 
+
+guint64 bdev_size(int fd)
+{
+	guint64 size64; // size in byte.
+	long size;    // size in sectors.
+	int err;
+
+	err=ioctl(fd,BLKGETSIZE64,&size64);
+	if(err) {
+		if (errno == EINVAL)  {
+			printf("INFO: falling back to BLKGETSIZE\n");
+			err=ioctl(fd,BLKGETSIZE,&size);
+			if(err) {
+				perror("ioctl(,BLKGETSIZE,) failed");
+				exit(20);
+			}
+			size64 = (typeof(guint64))512 * size;
+		} else {
+			perror("ioctl(,BLKGETSIZE64,) failed");
+			exit(20);
+		}
+	}
+
+	return size64;
+}
+
 int v07_read(conf_t config, struct meta_data * m)
 {
 	struct conf_07* cfg = (struct conf_07*) config;
 	struct meta_data_on_disk_07 * buffer;
 	int rr,i,bmw;
+	guint64 offset;
 
 	buffer = malloc(sizeof(struct meta_data_on_disk_07));
-  
+
+	if(cfg->index == -1) {
+		offset = ( bdev_size(cfg->fd) & ~((1<<12)-1) )
+			- MD_RESERVED_SIZE_07;
+	} else {
+		offset = MD_RESERVED_SIZE_07 * cfg->index;
+	}
+	
+	if(lseek64(cfg->fd,offset,SEEK_SET) == -1) {
+		PERROR("lseek() failed");
+		exit(20);
+	}
+
 	rr = read(cfg->fd, buffer, sizeof(struct meta_data_on_disk_07));
 	if( rr != sizeof(struct meta_data_on_disk_07)) {
 		PERROR("read failed");
@@ -307,9 +365,21 @@
 	exit(0);
 }
 
+int drbd_fd;
+char* drbd_dev_name;
+
+void cleanup(void) 
+{
+	if(drbd_fd == -1) {
+		dt_release_lockfile_dev_name(drbd_dev_name);
+	} else {
+		dt_close_drbd_device(drbd_fd);
+	}	
+}
+
 int main(int argc, char** argv)
 {
-	int i,ai,drbd_fd;
+	int i,ai;
 	struct format* fmt = NULL;
 	struct meta_cmd* command = NULL;
 	conf_t fcfg;
@@ -323,7 +393,19 @@
 	if (argc < 4) print_usage();
 
 	ai = 1;
-	drbd_fd=dt_open_drbd_device(argv[ai++]); // This creates the lock file.
+	drbd_dev_name=argv[ai++];
+	drbd_fd=dt_open_drbd_device(drbd_dev_name,1); // Create the lock file.
+	atexit(cleanup);
+	if(drbd_fd > -1) {
+		int fd2 = open(drbd_dev_name,O_RDWR);
+		// I want to avoid DRBD specific ioctls here...
+		if(fd2) {
+			fprintf(stderr,"Device '%s' is configured!",
+				drbd_dev_name);
+			exit(20);
+		}
+		close(fd2);
+	}
 
 	for (i = 0; i < ARRY_SIZE(formats); i++ ) {
 		if( !strcmp(formats[i].name,argv[ai]) ) {
@@ -333,11 +415,12 @@
 	}
 	if(fmt == NULL) {
 		fprintf(stderr,"Unknown format '%s'.\n",argv[ai]);
+		exit(20);
 	}
 	ai++;
 
 	fcfg = malloc(fmt->conf_size);
-	fmt->parse(fcfg,argv+2,&ai);
+	fmt->parse(fcfg,argv+ai,&ai);
 
 	for (i = 0; i < ARRY_SIZE(cmds); i++ ) {
 		if( !strcmp(cmds[i].name,argv[ai]) ) {
@@ -347,11 +430,11 @@
 	}
 	if(command == NULL) {
 		fprintf(stderr,"Unknown command '%s'.\n",argv[ai]);
+		exit(20);
 	}
 	ai++;
 
 	command->function(fmt,fcfg);
 
-	dt_close_drbd_device(drbd_fd);
 	return 0;
 }

Modified: trunk/user/drbdsetup.c
===================================================================
--- trunk/user/drbdsetup.c	2004-10-15 18:11:14 UTC (rev 1594)
+++ trunk/user/drbdsetup.c	2004-10-15 21:12:21 UTC (rev 1595)
@@ -342,7 +342,7 @@
 {
   int err,drbd_fd,version;
 
-  drbd_fd=dt_open_drbd_device(device);
+  drbd_fd=dt_open_drbd_device(device,0);
 
   err=ioctl(drbd_fd,DRBD_IOCTL_GET_VERSION,&version);
   if(err)

Modified: trunk/user/drbdtool_common.c
===================================================================
--- trunk/user/drbdtool_common.c	2004-10-15 18:11:14 UTC (rev 1594)
+++ trunk/user/drbdtool_common.c	2004-10-15 21:12:21 UTC (rev 1595)
@@ -79,7 +79,7 @@
   
   snprintf(lfname,39,"/var/lock/drbd-%d-%d.pid",major,minor);
 
-  while ( (fd = open(lfname,O_CREAT|O_EXCL|O_WRONLY)) == -1 )
+  while ( (fd = open(lfname,O_CREAT|O_EXCL|O_WRONLY,00644)) == -1 )
     {
       fd = open(lfname,O_RDONLY);
       if(fd == -1 )
@@ -93,7 +93,7 @@
       errno = 0;
       kill(pid,0);
       if(errno == ESRCH) {
-	fprintf(stderr,"Stale lock file found and removed.");
+	fprintf(stderr,"Stale lock file found and removed.\n");
 	remove(lfname);
       } else {
 	fprintf(stderr,"A drbd tool with pid %d has the device locked.\n",pid);
@@ -106,19 +106,19 @@
   fclose(fi);
 }
 
-int dt_open_drbd_device(const char* device)
+int dt_open_drbd_device(const char* device,int open_may_fail)
 {
   int drbd_fd,err;
   struct stat drbd_stat;
 
   drbd_fd=open(device,O_RDONLY);
-  if(drbd_fd==-1)
+  if(drbd_fd==-1 && !open_may_fail)
     {
       PERROR("can not open %s", device);
       exit(20);
     }
 
-  err=fstat(drbd_fd, &drbd_stat);
+  err=stat(device, &drbd_stat);
   if(err)
     {
       PERROR("fstat(%s) failed",device);
@@ -130,7 +130,7 @@
       exit(20);
     }
 
-  create_lockfile_mm(major(drbd_stat.st_dev),minor(drbd_stat.st_dev));
+  create_lockfile_mm(major(drbd_stat.st_rdev),minor(drbd_stat.st_rdev));
 
   return drbd_fd;
 }
@@ -145,14 +145,34 @@
   if(err)
     {
       PERROR("fstat() failed");
+      exit(20);
     }
 
   snprintf(lfname,39,"/var/lock/drbd-%d-%d.pid",
-	   major(drbd_stat.st_dev),minor(drbd_stat.st_dev));
+	   major(drbd_stat.st_rdev),minor(drbd_stat.st_rdev));
 
   remove(lfname);
 }
 
+void dt_release_lockfile_dev_name(const char* device)
+{
+  int err;
+  struct stat drbd_stat;
+  char lfname[40];
+
+  err=stat(device, &drbd_stat);
+  if(err)
+    {
+      PERROR("stat() failed");
+      exit(20);
+    }
+
+  snprintf(lfname,39,"/var/lock/drbd-%d-%d.pid",
+	   major(drbd_stat.st_rdev),minor(drbd_stat.st_rdev));
+
+  remove(lfname);
+}
+
 int dt_close_drbd_device(int drbd_fd)
 {
   dt_release_lockfile(drbd_fd);

Modified: trunk/user/drbdtool_common.h
===================================================================
--- trunk/user/drbdtool_common.h	2004-10-15 18:11:14 UTC (rev 1594)
+++ trunk/user/drbdtool_common.h	2004-10-15 21:12:21 UTC (rev 1595)
@@ -9,7 +9,8 @@
 struct option;
 
 extern void dt_release_lockfile(int drbd_fd);
-extern int dt_open_drbd_device(const char* device);
+extern void dt_release_lockfile_dev_name(const char* device);
+extern int dt_open_drbd_device(const char* device,int open_may_fail);
 extern int dt_close_drbd_device(int drbd_fd);
 extern unsigned long m_strtol(const char* s,int def_mult);
 const char* make_optstring(struct option *options);



More information about the drbd-cvs mailing list