[Ocfs2-tools-commits] smushran commits r1115 - in branches/ocfs2-tools-1.2: debugfs.ocfs2 libocfs2 libocfs2/include

svn-commits at oss.oracle.com svn-commits at oss.oracle.com
Tue Nov 8 16:15:13 CST 2005


Author: smushran
Signed-off-by: mfasheh
Date: 2005-11-08 16:15:11 -0600 (Tue, 08 Nov 2005)
New Revision: 1115

Added:
   branches/ocfs2-tools-1.2/libocfs2/lockid.c
Modified:
   branches/ocfs2-tools-1.2/debugfs.ocfs2/commands.c
   branches/ocfs2-tools-1.2/debugfs.ocfs2/dump.c
   branches/ocfs2-tools-1.2/debugfs.ocfs2/main.c
   branches/ocfs2-tools-1.2/debugfs.ocfs2/utils.c
   branches/ocfs2-tools-1.2/libocfs2/Makefile
   branches/ocfs2-tools-1.2/libocfs2/dlm.c
   branches/ocfs2-tools-1.2/libocfs2/include/ocfs2.h
   branches/ocfs2-tools-1.2/libocfs2/ocfs2_err.et
Log:
lockres encode and decode added to debugfs and libocfs2
Signed-Off-By: mfasheh

Modified: branches/ocfs2-tools-1.2/debugfs.ocfs2/commands.c
===================================================================
--- branches/ocfs2-tools-1.2/debugfs.ocfs2/commands.c	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/debugfs.ocfs2/commands.c	2005-11-08 22:15:11 UTC (rev 1115)
@@ -60,6 +60,7 @@
 static void do_extent (char **args);
 static void do_chroot (char **args);
 static void do_slotmap (char **args);
+static void do_encode_lockres (char **args);
 
 dbgfs_gbls gbls;
 
@@ -84,7 +85,8 @@
 	{ "rdump",	do_rdump },
 	{ "slotmap",	do_slotmap },
 	{ "stat",	do_stat },
-	{ "stats",	do_stats }
+	{ "stats",	do_stats },
+	{ "encode",	do_encode_lockres }
 };
 
 /*
@@ -580,6 +582,7 @@
 	printf ("close\t\t\t\t\tClose a device\n");
 	printf ("curdev\t\t\t\t\tShow current device\n");
 	printf ("dump [-p] <filespec> <outfile>\t\tDumps file to outfile on a mounted fs\n");
+	printf ("encode <filespec>\t\t\tShow lock name\n");
 	printf ("extent <block#>\t\t\t\tShow extent block\n");
 	printf ("group <block#>\t\t\t\tShow chain group\n");
 	printf ("help, ?\t\t\t\t\tThis information\n");
@@ -1065,3 +1068,52 @@
 
 	return ;
 }
+
+/*
+ * do_encode_lockres()
+ *
+ */
+static void do_encode_lockres (char **args)
+{
+	ocfs2_dinode *inode;
+	uint64_t blkno;
+	char *buf = NULL;
+	errcode_t ret = 0;
+	char suprlock[50] = "\0";
+	char metalock[50] = "\0";
+	char datalock[50] = "\0";
+
+	if (process_inode_args(args, &blkno))
+		return ;
+
+	if (blkno == OCFS2_SUPER_BLOCK_BLKNO) {
+		ret = ocfs2_encode_lockres(OCFS2_LOCK_TYPE_SUPER, blkno, 0,
+					   suprlock);
+	} else {
+		buf = gbls.blockbuf;
+		ret = ocfs2_read_inode(gbls.fs, blkno, buf);
+		if (!ret) {
+			inode = (ocfs2_dinode *)buf;
+			ocfs2_encode_lockres(OCFS2_LOCK_TYPE_META, blkno,
+					     inode->i_generation, metalock);
+			ocfs2_encode_lockres(OCFS2_LOCK_TYPE_DATA, blkno,
+					     inode->i_generation, datalock);
+		}
+	}
+
+	if (ret) {
+		com_err(args[0], ret, " ");
+		return ;
+	}
+
+	printf("\t");
+	if (*suprlock)
+		printf("%s ", suprlock);
+	if (*metalock)
+		printf("%s ", metalock);
+	if (*datalock)
+		printf("%s ", datalock);
+	printf("\n");
+
+	return ;
+}

Modified: branches/ocfs2-tools-1.2/debugfs.ocfs2/dump.c
===================================================================
--- branches/ocfs2-tools-1.2/debugfs.ocfs2/dump.c	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/debugfs.ocfs2/dump.c	2005-11-08 22:15:11 UTC (rev 1115)
@@ -175,10 +175,11 @@
 	if (in->i_flags & OCFS2_DEALLOC_FL)
 		g_string_append (flags, "Dealloc ");
 
-	fprintf(out, "\tInode: %"PRIu64"   Mode: 0%0o   Generation: %u\n",
-	        in->i_blkno, mode, in->i_generation);
+	fprintf(out, "\tInode: %"PRIu64"   Mode: 0%0o   Generation: %u (0x%x)\n",
+	        in->i_blkno, mode, in->i_generation, in->i_generation);
 
-	fprintf(out, "\tFS Generation: %u\n",in->i_fs_generation);
+	fprintf(out, "\tFS Generation: %u (0x%x)\n", in->i_fs_generation,
+		in->i_fs_generation);
 
 	fprintf(out, "\tType: %s   Flags: %s\n", str, flags->str);
 

Modified: branches/ocfs2-tools-1.2/debugfs.ocfs2/main.c
===================================================================
--- branches/ocfs2-tools-1.2/debugfs.ocfs2/main.c	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/debugfs.ocfs2/main.c	2005-11-08 22:15:11 UTC (rev 1115)
@@ -29,6 +29,10 @@
 
 extern dbgfs_gbls gbls;
 
+static int decodemode = 0;
+static int encodemode = 0;
+static int arg_ind = 0;
+
 static int logmode = 0;
 struct log_entry {
 	char *mask;
@@ -43,6 +47,8 @@
 static void usage (char *progname)
 {
 	g_print ("usage: %s -l [<logentry> ... [allow|off|deny]] ...\n", progname);
+	g_print ("usage: %s -d, --decode <lockres>\n", progname);
+	g_print ("usage: %s -e, --encode <lock type> <block num> <generation>\n", progname);
 	g_print ("usage: %s [-f cmdfile] [-V] [-w] [-n] [-?] [device]\n", progname);
 	g_print ("\t-f, --file <cmdfile>\tExecute commands in cmdfile\n");
 	g_print ("\t-w, --write\t\tOpen in read-write mode instead of the default of read-only\n");
@@ -107,6 +113,70 @@
 	}
 }
 
+static void process_decode_lockres(int argc, char **argv, int startind)
+{
+	int i;
+	errcode_t ret;
+	enum ocfs2_lock_type type;
+	uint64_t blkno;
+	uint32_t generation;
+
+	if (startind + 1 > argc) {
+		usage(gbls.progname);
+		exit(1);
+	}
+
+	for (i = startind; i < argc; ++i) {
+		ret = ocfs2_decode_lockres(argv[i], -1, &type, &blkno,
+					   &generation);
+		if (ret) {
+			com_err(gbls.progname, ret, " ");
+			continue;
+		}
+
+		printf("Lockres:    %s\n", argv[i]);
+		printf("Type:       %s\n",
+		       ocfs2_get_lock_type_string(type));
+		printf("Block:      %llu\n", blkno);
+		printf("Generation: 0x%08x\n", generation);
+		printf("\n");
+	}
+
+	return ;
+}
+
+/* [M|D|S] [blkno] [generation] */
+static void process_encode_lockres(int argc, char **argv, int startind)
+{
+	int i;
+	errcode_t ret;
+	enum ocfs2_lock_type type;
+	uint64_t blkno;
+	uint32_t generation;
+	char lockres[50];
+
+	if (startind + 3 > argc) {
+		usage(gbls.progname);
+		exit(1);
+	}
+
+	i = startind;
+
+	type = ocfs2_get_lock_type(argv[i++][0]);
+	blkno = strtoull(argv[i++], NULL, 0);
+	generation = strtoul(argv[i++], NULL, 0);
+
+	ret = ocfs2_encode_lockres(type, blkno, generation, lockres);
+	if (ret) {
+		com_err(gbls.progname, ret, " ");
+		return ;
+	}
+
+	printf("%s\n", lockres);
+
+	return ;
+}
+
 /*
  * get_options()
  *
@@ -121,11 +191,16 @@
 		{ "write", 0, 0, '?' },
 		{ "log", 0, 0, 'l' },
 		{ "noprompt", 0, 0, 'n' },
+		{ "decode", 0, 0, 'd' },
+		{ "encode", 0, 0, 'e' },
 		{ 0, 0, 0, 0}
 	};
 
 	while (1) {
-		c = getopt_long(argc, argv, "lf:V?wn", long_options, NULL);
+		if (decodemode || encodemode || logmode)
+			break;
+
+		c = getopt_long(argc, argv, "lf:deV?wn", long_options, NULL);
 		if (c == -1)
 			break;
 
@@ -138,6 +213,14 @@
 			}
 			break;
 
+ 		case 'd':
+ 			decodemode++;
+ 			break;
+ 
+ 		case 'e':
+ 			encodemode++;
+ 			break;
+ 
                 case 'l':
                         logmode++;
                         break;
@@ -167,13 +250,16 @@
 		}
 	}
 
-	if (optind < argc) {
-		if (!logmode)
-			opts->device = strdup(argv[optind]);
-		else
-			fill_log_list(argc, argv, optind);
-        }
-
+ 	if (optind < argc) {
+ 		if (logmode)
+ 			fill_log_list(argc, argv, optind);
+ 		else
+ 			opts->device = strdup(argv[optind]);
+ 	}
+ 
+ 	if (decodemode || encodemode)
+ 		arg_ind = optind;
+ 
 	return ;
 }
 
@@ -284,16 +370,28 @@
 	gbls.progname = basename(argv[0]);
 
 	get_options(argc, argv, &opts);
+
 	if (logmode) {
 		run_logmode();
 		goto bail;
 	}
 
+	if (decodemode) {
+		process_decode_lockres(argc, argv, arg_ind);
+		goto bail;
+	}
+
+	if (encodemode) {
+		process_encode_lockres(argc, argv, arg_ind);
+		goto bail;
+	}
+
 	gbls.allow_write = opts.allow_write;
 	if (!opts.cmd_file)
 		gbls.interactive++;
 
-	print_version (gbls.progname);
+	if (!opts.no_prompt)
+		print_version (gbls.progname);
 
 	if (opts.device) {
 		line = g_strdup_printf ("open %s", opts.device);

Modified: branches/ocfs2-tools-1.2/debugfs.ocfs2/utils.c
===================================================================
--- branches/ocfs2-tools-1.2/debugfs.ocfs2/utils.c	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/debugfs.ocfs2/utils.c	2005-11-08 22:15:11 UTC (rev 1115)
@@ -253,6 +253,13 @@
 
 	len = strlen(str);
 	if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
+		if (ocfs2_get_lock_type(str[1]) < OCFS2_NUM_LOCK_TYPES) {
+			if (!ocfs2_decode_lockres(str+1, len-2, NULL, blkno,
+						  NULL))
+				return 0;
+			else
+				return -1;
+		}
 		*blkno = strtoul(str+1, &end, 0);
 		if (*end=='>')
 			return 0;

Modified: branches/ocfs2-tools-1.2/libocfs2/Makefile
===================================================================
--- branches/ocfs2-tools-1.2/libocfs2/Makefile	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/libocfs2/Makefile	2005-11-08 22:15:11 UTC (rev 1115)
@@ -79,7 +79,8 @@
 	sysfile.c	\
 	truncate.c	\
 	unix_io.c	\
-	unlink.c
+	unlink.c	\
+	lockid.c
 
 HFILES =				\
 	include/bitmap.h		\

Modified: branches/ocfs2-tools-1.2/libocfs2/dlm.c
===================================================================
--- branches/ocfs2-tools-1.2/libocfs2/dlm.c	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/libocfs2/dlm.c	2005-11-08 22:15:11 UTC (rev 1115)
@@ -26,26 +26,10 @@
 #define _XOPEN_SOURCE 600 /* Triggers magic in features.h */
 #define _LARGEFILE64_SOURCE
                                                                                                                                                          
-#define __USE_MISC
-#include <string.h>
-#include <inttypes.h>
-
 #include "ocfs2.h"
-#include "ocfs2_lockid.h"
 
 #define DEFAULT_DLMFS_PATH	"/dlm/"
 
-static void ocfs2_build_lock_name(ocfs2_filesys *fs, enum ocfs2_lock_type type,
-				  uint64_t blkno, uint32_t generation,
-				  char *lock_name)
-{
-	snprintf(lock_name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016"PRIx64"%08x",
-		 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
-		 blkno, generation);
-
-	return ;
-}
-
 static errcode_t ocfs2_get_journal_blkno(ocfs2_filesys *fs, uint64_t *jrnl_blkno)
 {
 	ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
@@ -160,8 +144,8 @@
 	char lock_name[OCFS2_LOCK_ID_MAX_LEN];
 	errcode_t ret;
 
-	ocfs2_build_lock_name(fs, OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
-			      0, lock_name);
+	ocfs2_encode_lockres(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
+			     0, lock_name);
 
 	ret = o2dlm_lock(fs->fs_dlm_ctxt, lock_name,
 			 O2DLM_TRYLOCK, O2DLM_LEVEL_EXMODE);
@@ -174,8 +158,8 @@
 	char lock_name[OCFS2_LOCK_ID_MAX_LEN];
 	errcode_t ret;
 
-	ocfs2_build_lock_name(fs, OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
-			      0, lock_name);
+	ocfs2_encode_lockres(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
+			     0, lock_name);
 
 	ret = o2dlm_unlock(fs->fs_dlm_ctxt, lock_name);
 	
@@ -190,8 +174,8 @@
 	char lock_name[OCFS2_LOCK_ID_MAX_LEN];
 	errcode_t ret;
 
-	ocfs2_build_lock_name(fs, OCFS2_LOCK_TYPE_META, ci->ci_blkno,
-			      ci->ci_inode->i_generation, lock_name);
+	ocfs2_encode_lockres(OCFS2_LOCK_TYPE_META, ci->ci_blkno,
+			     ci->ci_inode->i_generation, lock_name);
 
 	ret = o2dlm_lock(fs->fs_dlm_ctxt, lock_name, flags, level);
 
@@ -204,8 +188,8 @@
 	char lock_name[OCFS2_LOCK_ID_MAX_LEN];
 	errcode_t ret;
 
-	ocfs2_build_lock_name(fs, OCFS2_LOCK_TYPE_META, ci->ci_blkno,
-			      ci->ci_inode->i_generation, lock_name);
+	ocfs2_encode_lockres(OCFS2_LOCK_TYPE_META, ci->ci_blkno,
+			     ci->ci_inode->i_generation, lock_name);
 
 	ret = o2dlm_unlock(fs->fs_dlm_ctxt, lock_name);
 

Modified: branches/ocfs2-tools-1.2/libocfs2/include/ocfs2.h
===================================================================
--- branches/ocfs2-tools-1.2/libocfs2/include/ocfs2.h	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/libocfs2/include/ocfs2.h	2005-11-08 22:15:11 UTC (rev 1115)
@@ -38,6 +38,7 @@
 #include <stdint.h>
 #include <sys/stat.h>
 #include <time.h>
+#include <string.h>
 
 #include <limits.h>
 
@@ -63,6 +64,8 @@
 #include <ocfs2/jbd.h>
 #endif
 
+#include "ocfs2_lockid.h"
+
 #define OCFS2_LIB_FEATURE_INCOMPAT_SUPP		(OCFS2_FEATURE_INCOMPAT_SUPP | OCFS2_FEATURE_INCOMPAT_HEARTBEAT_DEV)
 #define OCFS2_LIB_FEATURE_RO_COMPAT_SUPP	OCFS2_FEATURE_RO_COMPAT_SUPP
 
@@ -577,6 +580,17 @@
 
 void ocfs2_swap_slot_map(int16_t *map, loff_t num_slots);
 
+enum ocfs2_lock_type ocfs2_get_lock_type(char c);
+
+char *ocfs2_get_lock_type_string(enum ocfs2_lock_type type);
+
+errcode_t ocfs2_encode_lockres(enum ocfs2_lock_type type, uint64_t blkno,
+			       uint32_t generation, char *lockres);
+
+errcode_t ocfs2_decode_lockres(char *lockres, int len, enum ocfs2_lock_type *type,
+			       uint64_t *blkno, uint32_t *generation);
+
+
 /* 
  * ${foo}_to_${bar} is a floor function.  blocks_to_clusters will
  * returns the cluster that contains a block, not the number of clusters

Added: branches/ocfs2-tools-1.2/libocfs2/lockid.c
===================================================================
--- branches/ocfs2-tools-1.2/libocfs2/lockid.c	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/libocfs2/lockid.c	2005-11-08 22:15:11 UTC (rev 1115)
@@ -0,0 +1,121 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * lockid.c
+ *
+ * Encode and decode lockres name
+ *
+ * Copyright (C) 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License, version 2,  as published by the Free Software Foundation.
+ * 
+ * This program 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 this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include "ocfs2.h"
+
+#include <string.h>
+#include <inttypes.h>
+
+enum ocfs2_lock_type ocfs2_get_lock_type(char c)
+{
+	switch (c) {
+	case 'M':
+		return OCFS2_LOCK_TYPE_META;
+	case 'D':
+		return OCFS2_LOCK_TYPE_DATA;
+	case 'S':
+		return OCFS2_LOCK_TYPE_SUPER;
+	case 'R':
+		return OCFS2_LOCK_TYPE_RENAME;
+	default:
+		return OCFS2_NUM_LOCK_TYPES;
+	}
+}
+
+char *ocfs2_get_lock_type_string(enum ocfs2_lock_type type)
+{
+	switch (type) {
+	case OCFS2_LOCK_TYPE_META:
+		return "Metadata";
+	case OCFS2_LOCK_TYPE_DATA:
+		return "Data";
+	case OCFS2_LOCK_TYPE_SUPER:
+		return "Superblock";
+	case OCFS2_LOCK_TYPE_RENAME:
+		return "Rename";
+	default:
+		return NULL;
+	}
+}
+
+errcode_t ocfs2_encode_lockres(enum ocfs2_lock_type type, uint64_t blkno,
+			       uint32_t generation, char *lockres)
+{
+	if (type >= OCFS2_NUM_LOCK_TYPES)
+		return OCFS2_ET_INVALID_LOCKRES;
+
+	blkno = (type == OCFS2_LOCK_TYPE_RENAME) ? 0 : blkno;
+	generation = ((type == OCFS2_LOCK_TYPE_SUPER) ||
+		      (type == OCFS2_LOCK_TYPE_RENAME)) ? 0 : generation;
+
+	snprintf(lockres, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016"PRIx64"%08x",
+		 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
+		 blkno, generation);
+
+	return 0;
+}
+
+errcode_t ocfs2_decode_lockres(char *lockres, int len, enum ocfs2_lock_type *type,
+			       uint64_t *blkno, uint32_t *generation)
+{
+	char *lock = NULL;
+	errcode_t ret = OCFS2_ET_NO_MEMORY;
+	char blkstr[20];
+	int i = 0;
+	
+	if (len != -1) {
+		lock = calloc(len+1, 1);
+		if (!lock)
+			goto bail;
+		strncpy(lock, lockres, len);
+	} else
+		lock = lockres;
+
+	ret = OCFS2_ET_INVALID_LOCKRES;
+	
+	if ((strlen(lock) + 1) != OCFS2_LOCK_ID_MAX_LEN)
+		goto bail;
+
+	if (ocfs2_get_lock_type(lock[0]) >= OCFS2_NUM_LOCK_TYPES)
+		goto bail;
+
+	if (type)
+		*type = ocfs2_get_lock_type(lock[i]);
+
+	i = 1 + strlen(OCFS2_LOCK_ID_PAD);
+	memset(blkstr, 0, sizeof(blkstr));
+	memcpy(blkstr, &lock[i], 16);
+	if (blkno)
+		*blkno = strtoull(blkstr, NULL, 16);
+
+	i += 16;
+	if (generation)
+		*generation = strtoul(&lock[i], NULL, 16);
+
+	ret = 0;
+bail:
+	if (len != -1 && lock)
+		free(lock);
+	return ret;
+}

Modified: branches/ocfs2-tools-1.2/libocfs2/ocfs2_err.et
===================================================================
--- branches/ocfs2-tools-1.2/libocfs2/ocfs2_err.et	2005-11-08 22:14:11 UTC (rev 1114)
+++ branches/ocfs2-tools-1.2/libocfs2/ocfs2_err.et	2005-11-08 22:15:11 UTC (rev 1115)
@@ -156,4 +156,7 @@
 ec	OCFS2_ET_BLOCK_SIZE_TOO_SMALL_FOR_HARDWARE,
 	"The block size is smaller than the sector size on this device"
 
+ec	OCFS2_ET_INVALID_LOCKRES,
+	"The lock name is invalid"
+
 	end



More information about the Ocfs2-tools-commits mailing list