[Ocfs2-tools-commits] smushran commits r500 - in trunk: debugfs.ocfs2 debugfs.ocfs2/include libocfs2

svn-commits at oss.oracle.com svn-commits at oss.oracle.com
Thu Dec 16 17:19:50 CST 2004


Author: smushran
Date: 2004-12-16 17:19:48 -0600 (Thu, 16 Dec 2004)
New Revision: 500

Modified:
   trunk/debugfs.ocfs2/Cscope.make
   trunk/debugfs.ocfs2/commands.c
   trunk/debugfs.ocfs2/dump.c
   trunk/debugfs.ocfs2/include/dump.h
   trunk/debugfs.ocfs2/include/utils.h
   trunk/debugfs.ocfs2/utils.c
   trunk/libocfs2/fileio.c
Log:
ls -l added in debugfs
ocfs2_read_file improved
read_whole_file added in debugfs

Modified: trunk/debugfs.ocfs2/Cscope.make
===================================================================
--- trunk/debugfs.ocfs2/Cscope.make	2004-12-16 21:54:51 UTC (rev 499)
+++ trunk/debugfs.ocfs2/Cscope.make	2004-12-16 23:19:48 UTC (rev 500)
@@ -3,8 +3,8 @@
 	rm -f cscope.*
 	echo "-k" >> cscope.files
 	echo "-I inc" >> cscope.files
-	find . -maxdepth 2 -name '*.c' -print >>cscope.files
-	find . -maxdepth 2 -name '*.h' -print >>cscope.files
-	find ../libocfs2/ -maxdepth 2 -name '*.h' -print >>cscope.files
-	find ../libocfs2/ -maxdepth 2 -name '*.c' -print >>cscope.files
+	find . -name '*.c' -print >>cscope.files
+	find . -name '*.h' -print >>cscope.files
+	find ../libocfs2/ -name '*.h' -print >>cscope.files
+	find ../libocfs2/ -name '*.c' -print >>cscope.files
 	cscope -b

Modified: trunk/debugfs.ocfs2/commands.c
===================================================================
--- trunk/debugfs.ocfs2/commands.c	2004-12-16 21:54:51 UTC (rev 499)
+++ trunk/debugfs.ocfs2/commands.c	2004-12-16 23:19:48 UTC (rev 500)
@@ -57,10 +57,10 @@
 static void do_cat (char **args);
 static void do_lcd (char **args);
 static void do_curdev (char **args);
-static void do_super (char **args);
-static void do_inode (char **args);
+static void do_stats (char **args);
+static void do_stat (char **args);
 static void do_hb (char **args);
-static void do_journal (char **args);
+static void do_logdump (char **args);
 static void do_group (char **args);
 static void do_extent (char **args);
 static void do_chroot (char **args);
@@ -98,15 +98,15 @@
 
   { "curdev", do_curdev },
 
-  { "stats", do_super },
+  { "stats", do_stats },
 
-  { "stat", do_inode },
+  { "stat", do_stat },
 
   { "nodes", do_hb },
   { "publish", do_hb },
   { "vote", do_hb },
 
-  { "logdump", do_journal },
+  { "logdump", do_logdump },
 
   { "group", do_group },
   { "extent", do_extent }
@@ -174,24 +174,62 @@
 {
 	errcode_t ret;
 	char *opts = args[1];
+
+	if (check_device_open())
+		return -1;
+
+	if (!opts) {
+		fprintf(stderr, "usage: %s <filepath>\n", args[0]);
+		return -1;
+	}
+
+	ret = string_to_inode(gbls.fs, gbls.root_blkno, gbls.cwd_blkno,
+			      opts, blkno);
+	if (ret) {
+		com_err(args[0], ret, "'%s'", opts);
+		return -1;
+	}
+
+	if (*blkno >= gbls.max_blocks) {
+		fprintf(stderr, "%s: Block number is larger than volume size\n",
+			args[0]);
+		return -1;
+	}
+
+	return 0;
+}
+
+/*
+ * process_ls_args()
+ *
+ */
+static int process_ls_args(char **args, uint64_t *blkno, int *long_opt)
+{
+	errcode_t ret;
 	char *def = ".";
+	char *opts;
+	int ind = 1;
 
 	if (check_device_open())
 		return -1;
 
-	if (!opts) {
-		if (!strncasecmp(args[0], "ls", 2))
-			opts = def;
-		else {
-			fprintf(stderr, "usage: %s <filepath>\n", args[0]);
-			return -1;
+	if (args[ind]) {
+		if (!strncasecmp(args[1], "-l", 2)) {
+			*long_opt = 1;
+			++ind;
 		}
 	}
 
+	if (args[ind])
+		opts = args[ind];
+	else
+		opts = def;
+
+
 	ret = string_to_inode(gbls.fs, gbls.root_blkno, gbls.cwd_blkno,
 			      opts, blkno);
 	if (ret) {
-		com_err(args[0], ret, " ");
+		com_err(args[0], ret, "'%s'", opts);
 		return -1;
 	}
 
@@ -490,10 +528,10 @@
 static void do_ls (char **args)
 {
 	uint64_t blkno;
-	FILE *out = NULL;
 	errcode_t ret = 0;
+	list_dir_opts ls_opts = { gbls.fs, NULL, 0, NULL };
 
-	if (process_inode_args(args, &blkno))
+	if (process_ls_args(args, &blkno, &ls_opts.long_opt))
 		return ;
 
 	ret = ocfs2_check_directory(gbls.fs, blkno);
@@ -502,17 +540,25 @@
 		return ;
 	}
 
-	out = open_pager ();
-	fprintf(out, "\t%-15s %-4s %-4s %-2s %-4s\n",
-		"Inode", "Rlen", "Nlen", "Ty", "Name");
+	if (ls_opts.long_opt) {
+		ret = ocfs2_malloc_block(gbls.fs->fs_io, &ls_opts.buf);
+		if (ret) {
+			com_err(args[0], ret, " ");
+			return ;
+		}
+	}
 
+	ls_opts.out = open_pager();
 	ret = ocfs2_dir_iterate(gbls.fs, blkno, 0, NULL,
-				dump_dir_entry, out);
+				dump_dir_entry, (void *)&ls_opts);
 	if (ret)
 		com_err(args[0], ret, " ");
 
-	close_pager (out);
+	close_pager(ls_opts.out);
 
+	if (ls_opts.buf)
+		ocfs2_free(&ls_opts.buf);
+
 	return ;
 }
 
@@ -582,7 +628,7 @@
 	printf ("stats [-h]\t\t\t\tShow superblock\n");
 	printf ("stat <filepath>\t\t\t\tShow inode\n");
 //	printf ("pwd\t\t\t\tPrint working directory\n");
-	printf ("ls <filepath>\t\t\t\tList directory\n");
+	printf ("ls [-l] <filepath>\t\t\tList directory\n");
 	printf ("cd <filepath>\t\t\t\tChange directory\n");
 	printf ("chroot <filepath>\t\t\tChange root\n");
 	printf ("cat <filepath>\t\t\t\tPrints file on stdout\n");
@@ -624,10 +670,10 @@
 }
 
 /*
- * do_super()
+ * do_stats()
  *
  */
-static void do_super (char **args)
+static void do_stats (char **args)
 {
 	char *opts = args[1];
 	FILE *out;
@@ -652,10 +698,10 @@
 }
 
 /*
- * do_inode()
+ * do_stat()
  *
  */
-static void do_inode (char **args)
+static void do_stat (char **args)
 {
 	ocfs2_dinode *inode;
 	uint64_t blkno;
@@ -819,10 +865,10 @@
 }
 
 /*
- * do_journal()
+ * do_logdump()
  *
  */
-static void do_journal (char **args)
+static void do_logdump (char **args)
 {
 	char *logbuf = NULL;
 	uint64_t blkno = 0;
@@ -838,8 +884,7 @@
 		return ;
 
 	blkno = gbls.jrnl_blkno[nodenum];
-
-	ret = ocfs2_read_whole_file(gbls.fs, blkno, &logbuf, &len);
+	ret = read_whole_file(gbls.fs, blkno, &logbuf, &len);
 	if (ret) {
 		com_err(args[0], ret, " ");
 		goto bail;

Modified: trunk/debugfs.ocfs2/dump.c
===================================================================
--- trunk/debugfs.ocfs2/dump.c	2004-12-16 21:54:51 UTC (rev 499)
+++ trunk/debugfs.ocfs2/dump.c	2004-12-16 23:19:48 UTC (rev 500)
@@ -283,13 +283,30 @@
 int  dump_dir_entry (struct ocfs2_dir_entry *rec, int offset, int blocksize,
 		     char *buf, void *priv_data)
 {
-	FILE *out = priv_data;
+	list_dir_opts *ls = (list_dir_opts *)priv_data;
 	char tmp = rec->name[rec->name_len];
+	ocfs2_dinode *di;
+	char perms[20];
+	char timestr[40];
 
 	rec->name[rec->name_len] = '\0';
-	fprintf(out, "\t%-15"PRIu64" %-4u %-4u %-2u %s\n",
-			rec->inode, rec->rec_len,
-			rec->name_len, rec->file_type, rec->name);
+
+	if (!ls->long_opt) {
+		fprintf(ls->out, "\t%-15"PRIu64" %-4u %-4u %-2u %s\n", rec->inode,
+			rec->rec_len, rec->name_len, rec->file_type, rec->name);
+	} else {
+		memset(ls->buf, 0, ls->fs->fs_blocksize);
+		ocfs2_read_inode(ls->fs, rec->inode, ls->buf);
+		di = (ocfs2_dinode *)ls->buf;
+
+		inode_perms_to_str(di->i_mode, perms, sizeof(perms));
+		inode_time_to_str(di->i_mtime, timestr, sizeof(timestr));
+
+		fprintf(ls->out, "\t%-15"PRIu64" %10s %3u %5u %5u %15"PRIu64" %s %s\n",
+		       	rec->inode, perms, di->i_links_count, di->i_uid, di->i_gid,
+			di->i_size, timestr, rec->name);
+	}
+
 	rec->name[rec->name_len] = tmp;
 
 	return 0;

Modified: trunk/debugfs.ocfs2/include/dump.h
===================================================================
--- trunk/debugfs.ocfs2/include/dump.h	2004-12-16 21:54:51 UTC (rev 499)
+++ trunk/debugfs.ocfs2/include/dump.h	2004-12-16 23:19:48 UTC (rev 500)
@@ -26,6 +26,13 @@
 #ifndef __DUMP_H__
 #define __DUMP_H__
 
+typedef struct _list_dir_opts {
+	ocfs2_filesys *fs;
+	FILE *out;
+	int long_opt;
+	char *buf;
+} list_dir_opts;
+
 void dump_super_block (FILE *out, ocfs2_super_block *sb);
 void dump_local_alloc (FILE *out, ocfs2_local_alloc *loc);
 void dump_inode (FILE *out, ocfs2_dinode *in);

Modified: trunk/debugfs.ocfs2/include/utils.h
===================================================================
--- trunk/debugfs.ocfs2/include/utils.h	2004-12-16 21:54:51 UTC (rev 499)
+++ trunk/debugfs.ocfs2/include/utils.h	2004-12-16 23:19:48 UTC (rev 500)
@@ -37,5 +37,9 @@
 			  uint64_t cwd_blkno, char *str, uint64_t *blkno);
 errcode_t dump_file(ocfs2_filesys *fs, uint64_t ino, int fd, char *out_file,
 		    int preserve);
+errcode_t read_whole_file(ocfs2_filesys *fs, uint64_t ino, char **buf,
+			  uint32_t *buflen);
+void inode_perms_to_str(uint16_t mode, char *str, int len);
+void inode_time_to_str(uint64_t mtime, char *str, int len);
 
 #endif		/* __UTILS_H__ */

Modified: trunk/debugfs.ocfs2/utils.c
===================================================================
--- trunk/debugfs.ocfs2/utils.c	2004-12-16 21:54:51 UTC (rev 499)
+++ trunk/debugfs.ocfs2/utils.c	2004-12-16 23:19:48 UTC (rev 500)
@@ -330,7 +330,6 @@
 	uint32_t wrote;
 	ocfs2_cached_inode *ci = NULL;
 	uint64_t offset = 0;
-	uint64_t filesize;
 
 	ret = ocfs2_read_cached_inode(fs, ino, &ci);
 	if (ret)
@@ -340,7 +339,7 @@
 	if (ret)
 		goto bail;
 
-	buflen = fs->fs_clustersize;
+	buflen = 1024 * 1024;
 
 	ret = ocfs2_malloc_blocks(fs->fs_io,
 				  (buflen >>
@@ -349,23 +348,24 @@
 	if (ret)
 		goto bail;
 
-	filesize = ci->ci_inode->i_size;
-
-	while (filesize) {
+	while (1) {
 		ret = ocfs2_file_read(ci, buf, buflen, offset, &got);
 		if (ret)
 			goto bail;
 
-		if (filesize < got)
-			got = filesize;
+		if (!got)
+			break;
 
 		wrote = write(fd, buf, got);
 		if (wrote != got) {
 			ret = errno;
 			goto bail;
 		}
-		offset += got;
-		filesize -= got;
+
+		if (got < buflen)
+			break;
+		else
+			offset += got;
 	}
 
 	if (preserve)
@@ -380,3 +380,123 @@
 		ocfs2_free_cached_inode(fs, ci);
 	return ret;
 }
+
+
+/*
+ * read_whole_file()
+ *
+ */
+errcode_t read_whole_file(ocfs2_filesys *fs, uint64_t ino, char **buf, uint32_t *buflen)
+{
+	errcode_t ret;
+	uint32_t got;
+	ocfs2_cached_inode *ci = NULL;
+
+	ret = ocfs2_read_cached_inode(fs, ino, &ci);
+	if (ret)
+		goto bail;
+
+	ret = ocfs2_extent_map_init(fs, ci);
+	if (ret)
+		goto bail;
+
+	/* bail if file size is larger than reasonable :-) */
+	if (ci->ci_inode->i_size > 100 * 1024 * 1024) {
+		ret = OCFS2_ET_INTERNAL_FAILURE;
+		goto bail;
+	}
+
+	*buflen = (((ci->ci_inode->i_size + fs->fs_blocksize - 1) >>
+		    OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits) <<
+		   OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits);
+
+	ret = ocfs2_malloc_blocks(fs->fs_io,
+				  (*buflen >>
+				   OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits),
+				  buf);
+	if (ret)
+		goto bail;
+
+	ret = ocfs2_file_read(ci, *buf, *buflen, 0, &got);
+	if (ret)
+		goto bail;
+
+bail:
+	if (ci)
+		ocfs2_free_cached_inode(fs, ci);
+	return ret;
+}
+
+/*
+ * inode_perms_to_str()
+ *
+ */
+void inode_perms_to_str(uint16_t mode, char *str, int len)
+{
+	if (len < 11)
+		DBGFS_FATAL("internal error");
+
+	if (S_ISREG(mode))
+		str[0] = '-';
+	else if (S_ISDIR(mode))
+		str[0] = 'd';
+	else if (S_ISLNK(mode))
+		str[0] = 'l';
+	else if (S_ISCHR(mode))
+		str[0] = 'c';
+	else if (S_ISBLK(mode))
+		str[0] = 'b';
+	else if (S_ISFIFO(mode))
+		str[0] = 'f';
+	else if (S_ISSOCK(mode))
+		str[0] = 's';
+	else
+		str[0] = '-';
+
+	str[1] = (mode & S_IRUSR) ? 'r' : '-';
+	str[2] = (mode & S_IWUSR) ? 'w' : '-';
+	if (mode & S_ISUID)
+		str[3] = (mode & S_IXUSR) ? 's' : 'S';
+	else
+		str[3] = (mode & S_IXUSR) ? 'x' : '-';
+	
+	str[4] = (mode & S_IRGRP) ? 'r' : '-';
+	str[5] = (mode & S_IWGRP) ? 'w' : '-';
+	if (mode & S_ISGID)
+		str[6] = (mode & S_IXGRP) ? 's' : 'S';
+	else
+		str[6] = (mode & S_IXGRP) ? 'x' : '-';
+	
+	str[7] = (mode & S_IROTH) ? 'r' : '-';
+	str[8] = (mode & S_IWOTH) ? 'w' : '-';
+	if (mode & S_ISVTX)
+		str[9] = (mode & S_IXOTH) ? 't' : 'T';
+	else
+		str[9] = (mode & S_IXOTH) ? 'x' : '-';
+
+	str[10] = '\0';
+
+	return ;
+}
+
+/*
+ * inode_time_to_str()
+ *
+ */
+void inode_time_to_str(uint64_t timeval, char *str, int len)
+{
+	time_t tt = (time_t) timeval;
+	struct tm *tm;
+
+	static const char *month_str[] = {
+		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+	tm = localtime(&tt);
+
+	snprintf(str, len, "%2d-%s-%4d %02d:%02d", tm->tm_mday,
+		 month_str[tm->tm_mon], 1900 + tm->tm_year,
+		 tm->tm_hour, tm->tm_min);
+
+	return ;
+}

Modified: trunk/libocfs2/fileio.c
===================================================================
--- trunk/libocfs2/fileio.c	2004-12-16 21:54:51 UTC (rev 499)
+++ trunk/libocfs2/fileio.c	2004-12-16 23:19:48 UTC (rev 500)
@@ -132,6 +132,7 @@
 	uint64_t	v_blkno;
 	uint64_t	p_blkno;
 	uint32_t	tmp;
+	uint64_t	num_blocks;
 
 	/* o_direct requires aligned io */
 	tmp = fs->fs_blocksize - 1;
@@ -143,6 +144,15 @@
 	v_blkno = offset >> OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
 	*got = 0;
 
+	num_blocks = (ci->ci_inode->i_size + fs->fs_blocksize - 1) >>
+			OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
+
+	if (v_blkno >= num_blocks)
+		return 0;
+
+	if (v_blkno + wanted_blocks > num_blocks)
+		wanted_blocks = (uint32_t) (num_blocks - v_blkno);
+
 	while(wanted_blocks) {
 		ret = ocfs2_extent_map_get_blocks(ci, v_blkno, 1,
 						  &p_blkno, &contig_blocks);
@@ -156,13 +166,18 @@
 		if (ret)
 			return ret;
 
-		tmp = contig_blocks << OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits;
-		*got += tmp;
+		*got += (contig_blocks <<
+			 OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits);
 		wanted_blocks -= contig_blocks;
 
 		if (wanted_blocks) {
-			ptr += tmp;
+			ptr += (contig_blocks <<
+				OCFS2_RAW_SB(fs->fs_super)->s_blocksize_bits);
 			v_blkno += (uint64_t)contig_blocks;
+		} else {
+			if (*got + offset > ci->ci_inode->i_size)
+				*got = (uint32_t) (ci->ci_inode->i_size - offset);
+			/* break */
 		}
 	}
 



More information about the Ocfs2-tools-commits mailing list