[DRBD-cvs] svn commit by phil - r2078 - in trunk: drbd user -
Finaly implemented the usage counting of resources.
drbd-cvs at lists.linbit.com
drbd-cvs at lists.linbit.com
Thu Feb 23 18:46:30 CET 2006
Author: phil
Date: 2006-02-23 18:46:28 +0100 (Thu, 23 Feb 2006)
New Revision: 2078
Modified:
trunk/drbd/drbd_main.c
trunk/user/drbdadm.h
trunk/user/drbdadm_main.c
trunk/user/drbdadm_usage_cnt.c
trunk/user/drbdmeta.c
trunk/user/drbdtool_common.c
trunk/user/drbdtool_common.h
Log:
Finaly implemented the usage counting of resources.
Modified: trunk/drbd/drbd_main.c
===================================================================
--- trunk/drbd/drbd_main.c 2006-02-22 22:09:36 UTC (rev 2077)
+++ trunk/drbd/drbd_main.c 2006-02-23 17:46:28 UTC (rev 2078)
@@ -2401,6 +2401,7 @@
struct meta_data_on_disk {
u64 la_size; // last agreed size.
u64 uuid[UUID_SIZE]; // UUIDs.
+ u64 device_uuid;
u32 flags; // MDF
u32 magic;
u32 md_size_sect;
Modified: trunk/user/drbdadm.h
===================================================================
--- trunk/user/drbdadm.h 2006-02-22 22:09:36 UTC (rev 2077)
+++ trunk/user/drbdadm.h 2006-02-23 17:46:28 UTC (rev 2078)
@@ -85,6 +85,7 @@
extern int adm_connect(struct d_resource* ,const char* );
extern int adm_resize(struct d_resource* ,const char* );
extern int adm_syncer(struct d_resource* ,const char* );
+extern int admm_generic(struct d_resource* ,const char* );
extern int m_system(char**,int );
extern struct d_option* find_opt(struct d_option*,char*);
extern void validate_resource(struct d_resource *);
@@ -93,7 +94,9 @@
extern void schedule_dcmd( int (* function)(struct d_resource*,const char* ),
struct d_resource* res,
int order);
+
extern void uc_node(enum usage_count_type type);
+extern int adm_create_md(struct d_resource* res ,const char* cmd);
extern char* config_file;
extern int config_valid;
@@ -107,7 +110,10 @@
extern char ss_buffer[255];
extern struct utsname nodeinfo;
+extern char* setup_opts[10];
+extern int soi;
+
/* ssprintf() places the result of the printf in the current stack
frame and sets ptr to the resulting string. If the current stack
frame is destroyed (=function returns), the allocated memory is
Modified: trunk/user/drbdadm_main.c
===================================================================
--- trunk/user/drbdadm_main.c 2006-02-22 22:09:36 UTC (rev 2077)
+++ trunk/user/drbdadm_main.c 2006-02-23 17:46:28 UTC (rev 2078)
@@ -97,7 +97,7 @@
static int sh_ll_dev(struct d_resource* ,const char* );
static int sh_md_dev(struct d_resource* ,const char* );
static int sh_md_idx(struct d_resource* ,const char* );
-static int admm_generic(struct d_resource* ,const char* );
+ int admm_generic(struct d_resource* ,const char* );
static int adm_khelper(struct d_resource* ,const char* );
static int adm_generic_b(struct d_resource* ,const char* );
static int hidden_cmds(struct d_resource* ,const char* );
@@ -198,7 +198,7 @@
{ "state", adm_generic_s, 1,1,0 },
{ "cstate", adm_generic_s, 1,1,1 },
{ "dump", adm_dump, 1,1,1 },
- { "create-md", admm_generic, 1,1,0 },
+ { "create-md", adm_create_md, 1,1,0 },
{ "show-gi", adm_generic_b, 1,1,0 },
{ "get-gi", adm_generic_b, 1,1,0 },
{ "dump-md", admm_generic, 1,1,0 },
@@ -628,7 +628,7 @@
return m_system(argv,SLEEPS_SHORT);
}
-static int admm_generic(struct d_resource* res ,const char* cmd)
+int admm_generic(struct d_resource* res ,const char* cmd)
{
char* argv[20];
int argc=0,i;
Modified: trunk/user/drbdadm_usage_cnt.c
===================================================================
--- trunk/user/drbdadm_usage_cnt.c 2006-02-22 22:09:36 UTC (rev 2077)
+++ trunk/user/drbdadm_usage_cnt.c 2006-02-23 17:46:28 UTC (rev 2078)
@@ -276,14 +276,6 @@
return 0;
}
-static int insert_resource(u64 node_uuid, u64 res_uuid, u64 res_size) {
- char *req_buf;
- ssprintf( req_buf, "GET http://"HTTP_HOST"/cgi-bin/insert_usage.pl?"
- "nu="U64"&ru="U64"&rs="U64" HTTP/1.0\n\n",
- node_uuid, res_uuid, res_size);
- return make_get_request(req_buf);
-}
-
static void url_encode(char* in, char* out)
{
char *h = "0123456789abcdef";
@@ -394,3 +386,106 @@
}
}
+/* For our purpose (finding the revision) SLURP_SIZE is always enough.
+ */
+char* run_adm_function( int (* function)(struct d_resource*,const char* ),
+ struct d_resource* res ,const char* cmd)
+{
+ const int SLURP_SIZE = 4096;
+ int rr,pipes[2];
+ char* buffer;
+ pid_t pid;
+
+ buffer = malloc(SLURP_SIZE);
+ if(!buffer) return 0;
+
+ if(pipe(pipes)) return 0;
+
+ pid = fork();
+ if(pid == -1) {
+ fprintf(stderr,"Can not fork\n");
+ exit(E_exec_error);
+ }
+ if(pid == 0) {
+ // child
+ close(pipes[0]); // close reading end
+ dup2(pipes[1],1); // 1 = stdout
+ close(pipes[1]);
+ exit(function(res,cmd));
+ }
+ close(pipes[1]); // close writing end
+
+ rr = read(pipes[0], buffer, SLURP_SIZE-1);
+ if( rr == -1) {
+ free(buffer);
+ // FIXME cleanup
+ return 0;
+ }
+ buffer[rr]=0;
+ close(pipes[0]);
+
+ waitpid(pid,0,0);
+
+ return buffer;
+}
+
+int adm_create_md(struct d_resource* res ,const char* cmd)
+{
+ char answer[ANSWER_SIZE];
+ struct node_info ni;
+ u64 device_uuid=0;
+ u64 device_size=0;
+ char *req_buf;
+ int send=0;
+ char *tb;
+ int rv,fd;
+
+ tb = run_adm_function(admm_generic, res, "read-dev-uuid");
+ device_uuid = strto_u64(tb,NULL,16);
+ free(tb);
+
+ rv = admm_generic(res, cmd); // cmd is "create-md".
+
+ if(!device_uuid) {
+ get_random_bytes(&device_uuid, sizeof(u64));
+ }
+
+ fd = open(res->me->disk,O_RDONLY);
+ if( fd != -1) {
+ device_size = bdev_size(fd);
+ close(fd);
+ }
+
+ if( read_node_id(&ni) && device_size ) {
+ if( global_options.usage_count == UC_YES ) send = 1;
+ if( global_options.usage_count == UC_ASK ) {
+ printf(
+"\n"
+"\t\t--== Creating metadata ==--\n"
+"As with nodes we count the total number of devices mirrored by DRBD at\n"
+"at http://"HTTP_HOST".\n\n"
+"The conter works completely anonymous. A random number gets created for\n"
+"this device, and that randomer number and the devices size will be sent.\n\n"
+"http://"HTTP_HOST"/cgi-bin/insert_usage.pl?nu="U64"&ru="U64"&rs="U64"\n\n"
+"Enter 'no' to opt out, or just press [return] to continue:",
+ ni.node_uuid,device_uuid,device_size
+ );
+ fgets(answer,ANSWER_SIZE,stdin);
+ if(strcmp(answer,"no")) send = 1;
+ }
+ }
+
+ if (send) {
+ ssprintf(req_buf,"GET http://"HTTP_HOST"/cgi-bin/insert_usage.pl?"
+ "nu="U64"&ru="U64"&rs="U64" HTTP/1.0\n\n",
+ ni.node_uuid, device_uuid, device_size);
+ make_get_request(req_buf);
+ }
+
+ ssprintf( setup_opts[0], X64(016), device_uuid);
+ soi=1;
+ admm_generic(res, "write-dev-uuid");
+
+ return rv;
+}
+
Modified: trunk/user/drbdmeta.c
===================================================================
--- trunk/user/drbdmeta.c 2006-02-22 22:09:36 UTC (rev 2077)
+++ trunk/user/drbdmeta.c 2006-02-23 17:46:28 UTC (rev 2078)
@@ -40,8 +40,8 @@
#include <unistd.h>
#include <fcntl.h>
-#include <linux/fs.h> /* for BLKGETSIZE64 */
#include <linux/drbd.h> /* only use DRBD_MAGIC from here! */
+#include <linux/fs.h> /* for BLKFLSBUF */
#include "drbd_endian.h"
#include "drbdtool_common.h"
@@ -233,6 +233,7 @@
/* Since DRBD 0.8 we have uuid instead of gc */
u64 uuid[UUID_SIZE];
u32 flags;
+ u64 device_uuid;
};
/*
@@ -311,31 +312,6 @@
int (*outdate_gi) (struct md_cpu *md);
};
-u64 bdev_size(int fd)
-{
- u64 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 = (u64)512 *size;
- } else {
- perror("ioctl(,BLKGETSIZE64,) failed");
- exit(20);
- }
- }
-
- return size64;
-}
-
void *my_mmap(const char* func, const unsigned int line, const char* what,
size_t length, int prot , int flags, int fd, __off64_t offset)
{
@@ -547,13 +523,15 @@
struct __attribute__ ((packed)) md_on_disk_08 {
be_u64 la_sect; /* last agreed size. */
be_u64 uuid[UUID_SIZE]; // UUIDs.
+ be_u64 device_uuid;
be_u32 flags;
be_u32 magic;
be_u32 md_size_sect;
be_s32 al_offset; /* signed sector offset to this block */
be_u32 al_nr_extents; /* important for restoring the AL */
be_s32 bm_offset; /* signed sector offset to the bitmap, from here */
- char reserved[8 * 512 - (8*(UUID_SIZE+1)+4*6)];
+
+ char reserved[8 * 512 - (8*(UUID_SIZE+2)+4*6)];
};
void md_disk_08_to_cpu(struct md_cpu *cpu, const struct md_on_disk_08 *disk)
@@ -564,6 +542,7 @@
cpu->la_sect = be64_to_cpu(disk->la_sect.be);
for ( i=Current ; i<UUID_SIZE ; i++ )
cpu->uuid[i] = be64_to_cpu(disk->uuid[i].be);
+ cpu->device_uuid = be64_to_cpu(disk->device_uuid.be);
cpu->flags = be32_to_cpu(disk->flags.be);
cpu->magic = be32_to_cpu(disk->magic.be);
cpu->md_size_sect = be32_to_cpu(disk->md_size_sect.be);
@@ -579,6 +558,7 @@
for ( i=Current ; i<UUID_SIZE ; i++ ) {
disk->uuid[i].be = cpu_to_be64(cpu->uuid[i]);
}
+ disk->device_uuid.be = cpu_to_be64(cpu->device_uuid);
disk->flags.be = cpu_to_be32(cpu->flags);
disk->magic.be = cpu_to_be32(cpu->magic);
disk->md_size_sect.be = cpu_to_be32(cpu->md_size_sect);
@@ -771,7 +751,10 @@
int meta_create_md(struct format *cfg, char **argv, int argc);
int meta_outdate(struct format *cfg, char **argv, int argc);
int meta_set_gi(struct format *cfg, char **argv, int argc);
+int meta_read_dev_uuid(struct format *cfg, char **argv, int argc);
+int meta_write_dev_uuid(struct format *cfg, char **argv, int argc);
+
struct meta_cmd cmds[] = {
{"get-gi", 0, meta_get_gi, 1},
{"show-gi", 0, meta_show_gi, 1},
@@ -782,6 +765,8 @@
* implicit convert from v07 to v08 by create-md
* see comments there */
{"outdate", 0, meta_outdate, 1},
+ {"read-dev-uuid", "VAL", meta_read_dev_uuid, 0},
+ {"write-dev-uuid", "VAL", meta_write_dev_uuid, 0},
{"set-gi", ":::VAL:VAL:...", meta_set_gi, 0},
};
@@ -2126,6 +2111,45 @@
}
#endif
+int meta_read_dev_uuid(struct format *cfg, char **argv __attribute((unused)), int argc)
+{
+ if (argc > 0) {
+ fprintf(stderr, "Ignoring additional arguments\n");
+ }
+
+ if (cfg->ops->open(cfg))
+ return -1;
+
+ printf(X64(016)"\n",cfg->md.device_uuid);
+
+ return cfg->ops->close(cfg);
+}
+
+int meta_write_dev_uuid(struct format *cfg, char **argv, int argc)
+{
+ int err;
+
+ if (argc > 1) {
+ fprintf(stderr, "Ignoring additional arguments\n");
+ }
+ if (argc < 1) {
+ fprintf(stderr, "Required Argument missing\n");
+ exit(10);
+ }
+
+ if (cfg->ops->open(cfg))
+ return -1;
+
+ cfg->md.device_uuid = strto_u64(argv[0],NULL,16);
+
+ err = cfg->ops->md_cpu_to_disk(cfg);
+ err = cfg->ops->close(cfg) || err;
+ if (err)
+ fprintf(stderr, "update failed\n");
+
+ return err;
+}
+
char *progname = NULL;
void print_usage_and_exit()
{
Modified: trunk/user/drbdtool_common.c
===================================================================
--- trunk/user/drbdtool_common.c 2006-02-22 22:09:36 UTC (rev 2077)
+++ trunk/user/drbdtool_common.c 2006-02-23 17:46:28 UTC (rev 2078)
@@ -13,8 +13,8 @@
#include <stdlib.h>
#include <ctype.h>
#include <linux/drbd.h>
+#include <linux/fs.h> /* for BLKGETSIZE64 */
-#include "drbd_endian.h"
#include "drbdtool_common.h"
char* ppsize(char* buf, size_t size)
@@ -393,3 +393,28 @@
*sp=0;
return 1;
}
+
+u64 bdev_size(int fd)
+{
+ u64 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 = (u64)512 *size;
+ } else {
+ perror("ioctl(,BLKGETSIZE64,) failed");
+ exit(20);
+ }
+ }
+
+ return size64;
+}
Modified: trunk/user/drbdtool_common.h
===================================================================
--- trunk/user/drbdtool_common.h 2006-02-22 22:09:36 UTC (rev 2077)
+++ trunk/user/drbdtool_common.h 2006-02-23 17:46:28 UTC (rev 2078)
@@ -2,6 +2,7 @@
#define DRBDTOOL_COMMON_H
#include <asm/types.h>
+#include "drbd_endian.h"
#define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
@@ -37,5 +38,5 @@
extern void dt_pretty_print_uuids(const __u64* uuid, unsigned int flags);
extern int fget_token(char *s, int size, FILE* stream);
extern int sget_token(char *s, int size, const char** text);
-
+extern u64 bdev_size(int fd);
#endif
More information about the drbd-cvs
mailing list