[DRBD-cvs] r1590 - trunk/user

svn at svn.drbd.org svn at svn.drbd.org
Thu Oct 14 15:33:00 CEST 2004


Author: phil
Date: 2004-10-14 15:32:57 +0200 (Thu, 14 Oct 2004)
New Revision: 1590

Added:
   trunk/user/drbdmeta.c
   trunk/user/drbdtool_common.c
   trunk/user/drbdtool_common.h
Modified:
   trunk/user/Makefile
   trunk/user/drbdadm_adjust.c
   trunk/user/drbdsetup.c
Log:
Breaking everything. First checkin of completely unfinished 
drbdmeta command.


Modified: trunk/user/Makefile
===================================================================
--- trunk/user/Makefile	2004-10-13 15:03:28 UTC (rev 1589)
+++ trunk/user/Makefile	2004-10-14 13:32:57 UTC (rev 1590)
@@ -17,16 +17,18 @@
 # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 #
 
-CFLAGS = -c -Wall -I../drbd
+CFLAGS = -c -Wall -I../drbd $(shell glib-config --cflags)
 CC = gcc
 
 drbdadm-obj = drbdadm_scanner.o drbdadm_parser.o drbdadm_main.o \
-	      drbdadm_adjust.o ../drbd/drbd_buildtag.o
+	      drbdadm_adjust.o drbdtool_common.o ../drbd/drbd_buildtag.o
 
-drbdsetup-obj = drbdsetup.o ../drbd/drbd_buildtag.o
+drbdsetup-obj = drbdsetup.o drbdtool_common.o ../drbd/drbd_buildtag.o
 
-all: drbdsetup drbdadm
+drbdmeta-obj = drbdmeta.o drbdtool_common.o ../drbd/drbd_buildtag.o
 
+all: drbdsetup drbdadm drbdmeta
+
 drbdadm: $(drbdadm-obj)
 	$(CC) -o $@ $^ 
 
@@ -44,6 +46,9 @@
 drbdsetup: $(drbdsetup-obj)
 	$(CC) -o $@ $^ 
 
+drbdmeta: $(drbdmeta-obj)
+	$(CC) -o $@ $^ 
+
 clean:
 	rm -f drbdadm_scanner.c
 	rm -f drbdadm_parser.c

Modified: trunk/user/drbdadm_adjust.c
===================================================================
--- trunk/user/drbdadm_adjust.c	2004-10-13 15:03:28 UTC (rev 1589)
+++ trunk/user/drbdadm_adjust.c	2004-10-14 13:32:57 UTC (rev 1590)
@@ -34,6 +34,7 @@
 #include <stdarg.h>
 
 #include "drbdadm.h"
+#include "drbdtool_common.h"
 
 #define PERROR(fmt, args...) \
 do { fprintf(stderr,fmt ": ", ##args); perror(0); } while (0)
@@ -74,32 +75,6 @@
   return fdopen(pipes[0],"r");
 }
 
-
-static unsigned long m_strtol(const char* s,int def_mult)
-{
-  char *e = (char*)s;
-  unsigned long r;
-
-  r = strtol(s,&e,0);
-  switch(*e)
-    {
-    case 0:
-      return r;
-    case 'K':
-    case 'k':
-      return r*(1024/def_mult);
-    case 'M':
-    case 'm':
-      return r*1024*(1024/def_mult);
-    case 'G':
-    case 'g':
-      return r*1024*1024*(1024/def_mult);
-    default:
-      fprintf(stderr,"%s is not a valid number\n",s);
-      exit(E_config_invalid);
-    }
-}
-
 int check_opt_b(FILE *in,char* name,struct d_option* base)
 {
   struct d_option* o;

Added: trunk/user/drbdmeta.c
===================================================================
--- trunk/user/drbdmeta.c	2004-10-13 15:03:28 UTC (rev 1589)
+++ trunk/user/drbdmeta.c	2004-10-14 13:32:57 UTC (rev 1590)
@@ -0,0 +1,229 @@
+/*
+   drbdmeta.c
+
+   This file is part of drbd by Philipp Reisner.
+
+   Copyright (C) 1999-2004, Philipp Reisner <philipp.reisner at linbit.com>.
+        Initial author.
+
+   drbd is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   drbd is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with drbd; see the file COPYING.  If not, write to
+   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+
+ */
+#include <unistd.h>
+#include <string.h>
+#include "drbdtool_common.h"
+#include <glib.h>  // gint32, GINT64_FROM_BE()
+
+#define ALIGN(x,a) ( ((x) + (a)-1) &~ ((a)-1) )
+
+char* basename = 0;
+
+enum MetaDataFlags {
+	__MDF_Consistent,
+	__MDF_PrimaryInd,
+	__MDF_ConnectedInd,
+	__MDF_FullSync,
+};
+#define MDF_Consistent      (1<<__MDF_Consistent)
+#define MDF_PrimaryInd      (1<<__MDF_PrimaryInd)
+#define MDF_ConnectedInd    (1<<__MDF_ConnectedInd)
+#define MDF_FullSync        (1<<__MDF_FullSync)
+
+enum MetaDataIndex {
+	Flags,          /* Consistency flag,connected-ind,primary-ind */
+	HumanCnt,       /* human-intervention-count */
+	TimeoutCnt,     /* timout-count */
+	ConnectedCnt,   /* connected-count */
+	ArbitraryCnt,   /* arbitrary-count */
+	GEN_CNT_SIZE	// MUST BE LAST! (and Flags must stay first...)
+};
+
+struct meta_data {
+  guint32 gc[GEN_CNT_SIZE];   // v06
+
+  guint64 la_size;            // v07
+  int bm_size;            // v07
+  unsigned long *bitmap;  // v07
+  int al_size;            // v07
+  unsigned int  *act_log; // v07
+};
+
+struct meta_data_on_disk_07 {
+	guint64 la_size;           // last agreed size.
+	guint32 gc[GEN_CNT_SIZE];  // generation counter
+	guint32 magic;
+	guint32 md_size;
+	guint32 al_offset;         // offset to this block
+	guint32 al_nr_extents;     // important for restoring the AL
+	guint32 bm_offset;         // offset to the bitmap, from here
+};
+
+struct conf_06 {
+  int fd;
+  int minor;
+};
+
+struct conf_07 {
+  int fd;
+  char *device_name;
+  int index;
+};
+
+typedef void* conf_t;
+
+struct version {
+  const char* name;
+  int (* parse)(conf_t, char **argv);
+  int (* open) (conf_t);
+  int (* close)(conf_t);
+  struct meta_data * (* alloc) (conf_t);
+  void (* free) (conf_t, struct meta_data *);
+  int (* read) (conf_t, struct meta_data *);
+  int (* write)(conf_t, struct meta_data *);
+};
+
+struct meta_cmd {
+  const char* name;
+  int (* function)(void *,char* );
+  int show_in_usage;
+};
+
+int meta_dump(void* v, char* c)
+{
+  return 0;
+}
+
+struct meta_cmd cmds[] = {
+  { "dump",    meta_dump,      1 }
+};
+
+int v07_parse(conf_t config, char **argv);
+int v07_open(conf_t config);
+int v07_close(conf_t config);
+struct meta_data * vxx_alloc(conf_t config);
+void vxx_free(conf_t config, struct meta_data * m);
+int v07_read(conf_t config, struct meta_data *);
+int v07_write(conf_t config, struct meta_data *);
+
+struct version versions[] = {
+  { "v07",v07_parse,v07_open,v07_close,vxx_alloc,vxx_free,v07_read,v07_write},
+};
+
+/* capacity in units of 512 byte (AKA sectors)
+ */
+int bm_words(unsigned long capacity)
+{
+  unsigned long bits;
+  int words;
+
+  //bits  = ALIGN(capacity,BM_SECTORS_PER_BIT) >> (BM_BLOCK_SIZE_B-9);
+  bits = ALIGN(capacity,8) >> 3;
+  words = ALIGN(bits,64) >> LN2_BPL;
+
+  return words;
+}
+
+int v07_parse(conf_t config, char **argv)
+{
+  struct conf_07* cfg = (struct conf_07*) config;
+  char *e;
+
+  cfg.device_name = strdup(argv[0]);
+  e = argv[1];
+  cfg.index = strtol(argv[1],&e,0);
+  if(*e != 0) {
+    fprintf(stderr,"'%s' is not a valid index number.\n",argv[1]);
+    return 0;
+  }
+  return 1;
+}
+
+int v07_open(conf_t config)
+{
+  struct conf_07* cfg = (struct conf_07*) config;
+
+  cfg.fd = open(cfg.device_name,O_RDWR);
+
+  return (cfg.fd != -1) ;
+}
+
+int v07_close(conf_t config)
+{
+  struct conf_07* cfg = (struct conf_07*) config;
+
+  return close(cfg.fd) == 0;
+}
+
+struct meta_data * vxx_alloc(conf_t config)
+{
+  struct meta_data *m;
+
+  m = malloc(sizeof(struct meta_data ));
+  memset(m,sizeof(struct meta_data ),1);
+  
+  return m;  
+}
+
+void vxx_free(conf_t config, struct meta_data * m)
+{
+  struct meta_data *m;
+
+  if(m->bitmap)  free(m->bitmap);
+  if(m->act_log) free(m->act_log);
+
+  free(m);
+}
+
+
+int v07_read(conf_t config, struct meta_data * m)
+{
+  struct meta_data_on_disk * buffer;
+  int rr,i,bmw;
+
+  buffer = malloc(sizeof(struct meta_data_on_disk));
+  
+  rr = read(cfg.fd, buffer, sizeof(struct meta_data_on_disk));
+  if( rr != sizeof(struct meta_data_on_disk)) {
+    PERROR("read failed");
+    exit(20);
+  }
+    
+  for (i = Flags; i < GEN_CNT_SIZE; i++)
+    m->gc[i] = GINT32_FROM_BE(buffer->gc[Flags]);
+
+  m->la_size = GINT64_FROM_BE(buffer->la_size);
+  bmw = bm_words(m->la_size);
+
+  
+}
+
+int v07_write(conf_t config)
+{
+}
+
+
+int main(int argc, char** argv)
+{
+  int drbd_fd;
+
+  if ( (basename = strrchr(argv[0],'/')) )
+      argv[0] = ++basename;
+  else
+      basename = argv[0];
+
+  chdir("/");
+
+  drbd_fd=dt_open_drbd_device(device); // This creates the lock file.
+}

Modified: trunk/user/drbdsetup.c
===================================================================
--- trunk/user/drbdsetup.c	2004-10-13 15:03:28 UTC (rev 1589)
+++ trunk/user/drbdsetup.c	2004-10-14 13:32:57 UTC (rev 1590)
@@ -47,6 +47,7 @@
 #include <unistd.h>
 #include <dirent.h>
 #include <mntent.h>
+#include "drbdtool_common.h"
 
 #define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
 
@@ -83,10 +84,7 @@
 # define PRINT_ARGV
 #endif
 
-#define PERROR(fmt, args...) \
-do { fprintf(stderr,fmt ": " , ##args); perror(0); } while (0)
 
-
 // some globals
 char* basename = 0;
 
@@ -207,31 +205,6 @@
   return retval;
 }
 
-unsigned long m_strtol(const char* s,int def_mult)
-{
-  char *e = (char*)s;
-  unsigned long r;
-
-  r = strtol(s,&e,0);
-  switch(*e)
-    {
-    case 0:
-      return r;
-    case 'K':
-    case 'k':
-      return r*(1024/def_mult);
-    case 'M':
-    case 'm':
-      return r*1024*(1024/def_mult);
-    case 'G':
-    case 'g':
-      return r*1024*1024*(1024/def_mult);
-    default:
-      fprintf(stderr,"%s is not a valid number\n",s);
-      exit(20);
-    }
-}
-
 const char* addr_part(const char* s)
 {
   static char buffer[200];
@@ -390,27 +363,10 @@
 
 int open_drbd_device(const char* device)
 {
-  int drbd_fd,err,version;
-  struct stat drbd_stat;
+  int err,drbd_fd,version;
 
-  drbd_fd=open(device,O_RDONLY);
-  if(drbd_fd==-1)
-    {
-      PERROR("can not open %s", device);
-      exit(20);
-    }
+  drbd_fd=dt_open_drbd_device(device);
 
-
-  err=fstat(drbd_fd, &drbd_stat);
-  if(err)
-    {
-      PERROR("fstat(%s) failed",device);
-    }
-  if(!S_ISBLK(drbd_stat.st_mode))
-    {
-      fprintf(stderr, "%s is not a block device!\n", device);
-      exit(20);
-    }
   err=ioctl(drbd_fd,DRBD_IOCTL_GET_VERSION,&version);
   if(err)
     {

Added: trunk/user/drbdtool_common.c
===================================================================
--- trunk/user/drbdtool_common.c	2004-10-13 15:03:28 UTC (rev 1589)
+++ trunk/user/drbdtool_common.c	2004-10-14 13:32:57 UTC (rev 1590)
@@ -0,0 +1,117 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "drbdtool_common.h"
+
+unsigned long m_strtol(const char* s,int def_mult)
+{
+  char *e = (char*)s;
+  unsigned long r;
+
+  r = strtol(s,&e,0);
+  switch(*e)
+    {
+    case 0:
+      return r;
+    case 'K':
+    case 'k':
+      return r*(1024/def_mult);
+    case 'M':
+    case 'm':
+      return r*1024*(1024/def_mult);
+    case 'G':
+    case 'g':
+      return r*1024*1024*(1024/def_mult);
+    default:
+      fprintf(stderr,"%s is not a valid number\n",s);
+      exit(20);
+    }
+}
+
+void create_lockfile_mm(int major, int minor)
+{
+  char lfname[40];
+  int fd,pid;
+  FILE* fi;
+  
+  snprintf(lfname,39,"/var/lock/drbd-%d-%d.pid",major,minor);
+
+  while ( (fd = open(lfname,O_CREAT|O_EXCL|O_WRONLY)) == -1 )
+    {
+      fd = open(lfname,O_RDONLY);
+      if(fd == -1 )
+	{
+	  PERROR("Creation and open(,O_RDONLY) of lockfile failed");
+	  exit(20);
+	}
+      fi = fdopen(fd,"r");
+      fscanf(fi,"%d",&pid);
+      fclose(fi);
+      errno = 0;
+      kill(pid,0);
+      if(errno == ESRCH) {
+	fprintf(stderr,"Stale lock file found and removed.");
+	remove(lfname);
+      } else {
+	fprintf(stderr,"A drbd tool with pid %d has the device locked.\n",pid);
+	exit(20);
+      }
+    }
+
+  fi = fdopen(fd,"w");
+  fprintf(fi,"%d\n",getpid());
+  fclose(fi);
+}
+
+void dt_release_lockfile(int drbd_fd)
+{
+  int err;
+  struct stat drbd_stat;
+  char lfname[40];
+
+  err=fstat(drbd_fd, &drbd_stat);
+  if(err)
+    {
+      PERROR("fstat() failed");
+    }
+
+  snprintf(lfname,39,"/var/lock/drbd-%d-%d.pid",
+	   major(drbd_stat.st_dev),minor(drbd_stat.st_dev));
+
+  remove(lfname);
+}
+
+int dt_open_drbd_device(const char* device)
+{
+  int drbd_fd,err;
+  struct stat drbd_stat;
+
+  drbd_fd=open(device,O_RDONLY);
+  if(drbd_fd==-1)
+    {
+      PERROR("can not open %s", device);
+      exit(20);
+    }
+
+  err=fstat(drbd_fd, &drbd_stat);
+  if(err)
+    {
+      PERROR("fstat(%s) failed",device);
+    }
+
+  if(!S_ISBLK(drbd_stat.st_mode))
+    {
+      fprintf(stderr, "%s is not a block device!\n", device);
+      exit(20);
+    }
+
+  create_lockfile_mm(major(drbd_stat.st_dev),minor(drbd_stat.st_dev));
+
+  return drbd_fd;
+}

Added: trunk/user/drbdtool_common.h
===================================================================
--- trunk/user/drbdtool_common.h	2004-10-13 15:03:28 UTC (rev 1589)
+++ trunk/user/drbdtool_common.h	2004-10-14 13:32:57 UTC (rev 1590)
@@ -0,0 +1,11 @@
+#ifndef DRBDTOOL_COMMON_H
+#define DRBDTOOL_COMMON_H
+
+#define PERROR(fmt, args...) \
+do { fprintf(stderr,fmt ": " , ##args); perror(0); } while (0)
+
+extern void dt_release_lockfile(int drbd_fd);
+extern int dt_open_drbd_device(const char* device);
+extern unsigned long m_strtol(const char* s,int def_mult);
+
+#endif



More information about the drbd-cvs mailing list