Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Goffredo Baroncelli <kreijack@libero.it>
To: chris.mason@oracle.com
Cc: linux-btrfs@vger.kernel.org
Subject: [PATCH 2/5][REPOST][BTRFS-PROGS] Be more verbose when a ioctl returns an error
Date: Sun, 5 Dec 2010 18:47:38 +0100	[thread overview]
Message-ID: <201012051847.39119.kreijack@libero.it> (raw)

[-- Attachment #1: Type: text/plain, Size: 13344 bytes --]

Hi all,

this patch makes the command "btrfs" more verbose when a btrfs ioctl return an 
error. The error code is printed as text message by the strerror(errno) 
function.

Example:

	# btrfs subvol create /tmp/1
	Create subvolume '/tmp/1'
	# btrfs subvol create /tmp/1/2
	Create subvolume '/tmp/1/2'
	# btrfs subvol delete 
	$ sudo btrfs subvol delete  /tmp/1
	Delete subvolume '/tmp/1'
	ERROR: cannot delete '/tmp/1' - Directory not empty

You can pull the patch also from 
	http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git 
branch 
	strerror

Comments are welcome.

Reagrds
G.Baroncelli

diff --git a/btrfs-list.c b/btrfs-list.c
index 93766a8..abcc2f4 100644
--- a/btrfs-list.c
+++ b/btrfs-list.c
@@ -265,7 +265,7 @@ static int resolve_root(struct root_lookup *rl, struct 
root_info *ri)
 static int lookup_ino_path(int fd, struct root_info *ri)
 {
 	struct btrfs_ioctl_ino_lookup_args args;
-	int ret;
+	int ret, e;
 
 	if (ri->path)
 		return 0;
@@ -275,9 +275,11 @@ static int lookup_ino_path(int fd, struct root_info *ri)
 	args.objectid = ri->dir_id;
 
 	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
+	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: Failed to lookup path for root 
%llu\n",
-			(unsigned long long)ri->ref_tree);
+		fprintf(stderr, "ERROR: Failed to lookup path for root %llu - 
%s\n",
+			(unsigned long long)ri->ref_tree,
+			strerror(e));
 		return ret;
 	}
 
@@ -320,15 +322,18 @@ static u64 find_root_gen(int fd)
 	unsigned long off = 0;
 	u64 max_found = 0;
 	int i;
+	int e;
 
 	memset(&ino_args, 0, sizeof(ino_args));
 	ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
 
 	/* this ioctl fills in ino_args->treeid */
 	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
+	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: Failed to lookup path for dirid 
%llu\n",
-			(unsigned long long)BTRFS_FIRST_FREE_OBJECTID);
+		fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - 
%s\n",
+			(unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
+			strerror(e));
 		return 0;
 	}
 
@@ -351,8 +356,10 @@ static u64 find_root_gen(int fd)
 
 	while (1) {
 		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+		e = errno;
 		if (ret < 0) {
-			fprintf(stderr, "ERROR: can't perform the search\n");
+			fprintf(stderr, "ERROR: can't perform the search - 
%s\n",
+				strerror(e));
 			return 0;
 		}
 		/* the ioctl returns the number of item it found in nr_items 
*/
@@ -407,14 +414,16 @@ static char *__ino_resolve(int fd, u64 dirid)
 	struct btrfs_ioctl_ino_lookup_args args;
 	int ret;
 	char *full;
+	int e;
 
 	memset(&args, 0, sizeof(args));
 	args.objectid = dirid;
 
 	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
+	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: Failed to lookup path for dirid 
%llu\n",
-			(unsigned long long)dirid);
+		fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - 
%s\n",
+			(unsigned long long)dirid, strerror(e) );
 		return ERR_PTR(ret);
 	}
 
@@ -472,6 +481,7 @@ static char *ino_resolve(int fd, u64 ino, u64 
*cache_dirid, char 
**cache_name)
 	struct btrfs_ioctl_search_header *sh;
 	unsigned long off = 0;
 	int namelen;
+	int e;
 
 	memset(&args, 0, sizeof(args));
 
@@ -490,8 +500,10 @@ static char *ino_resolve(int fd, u64 ino, u64 
*cache_dirid, char 
**cache_name)
 	sk->nr_items = 1;
 
 	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+	e = errno;
 	if (ret < 0) {
-		fprintf(stderr, "ERROR: can't perform the search\n");
+		fprintf(stderr, "ERROR: can't perform the search - %s\n",
+			strerror(e));
 		return NULL;
 	}
 	/* the ioctl returns the number of item it found in nr_items */
@@ -550,6 +562,7 @@ int list_subvols(int fd)
 	char *name;
 	u64 dir_id;
 	int i;
+	int e;
 
 	root_lookup_init(&root_lookup);
 
@@ -578,8 +591,10 @@ int list_subvols(int fd)
 
 	while(1) {
 		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+		e = errno;
 		if (ret < 0) {
-			fprintf(stderr, "ERROR: can't perform the search\n");
+			fprintf(stderr, "ERROR: can't perform the search - 
%s\n",
+				strerror(e));
 			return ret;
 		}
 		/* the ioctl returns the number of item it found in nr_items 
*/
@@ -747,6 +762,7 @@ int find_updated_files(int fd, u64 root_id, u64 
oldest_gen)
 	u64 found_gen;
 	u64 max_found = 0;
 	int i;
+	int e;
 	u64 cache_dirid = 0;
 	u64 cache_ino = 0;
 	char *cache_dir_name = NULL;
@@ -773,8 +789,10 @@ int find_updated_files(int fd, u64 root_id, u64 
oldest_gen)
 	max_found = find_root_gen(fd);
 	while(1) {
 		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+		e = errno;
 		if (ret < 0) {
-			fprintf(stderr, "ERROR: can't perform the search\n");
+			fprintf(stderr, "ERROR: can't perform the search- 
%s\n",
+				strerror(e));
 			return ret;
 		}
 		/* the ioctl returns the number of item it found in nr_items 
*/
diff --git a/btrfs_cmds.c b/btrfs_cmds.c
index 8031c58..3765a10 100644
--- a/btrfs_cmds.c
+++ b/btrfs_cmds.c
@@ -156,6 +156,7 @@ int do_defrag(int ac, char **av)
 	int verbose = 0;
 	int fancy_ioctl = 0;
 	struct btrfs_ioctl_defrag_range_args range;
+	int e=0;
 
 	optind = 1;
 	while(1) {
@@ -219,19 +220,21 @@ int do_defrag(int ac, char **av)
 		}
 		if (!fancy_ioctl) {
 			ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
+			e=errno;
 		} else {
 			ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
 			if (ret && errno == ENOTTY) {
-				fprintf(stderr, "defrag range ioctl not "
+				fprintf(stderr, "ERROR: defrag range ioctl not 
"
 					"supported in this kernel, please try 
"
 					"without any options.\n");
 				errors++;
+				close(fd);
 				break;
 			}
 		}
 		if (ret) {
-			fprintf(stderr, "ioctl failed on %s ret %d errno 
%d\n",
-				av[i], ret, errno);
+			fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
+				av[i], strerror(e));
 			errors++;
 		}
 		close(fd);
@@ -310,7 +313,7 @@ int do_subvol_list(int argc, char **argv)
 int do_clone(int argc, char **argv)
 {
 	char	*subvol, *dst;
-	int	res, fd, fddst, len;
+	int	res, fd, fddst, len, e;
 	char	*newname;
 	char	*dstdir;
 
@@ -377,12 +380,14 @@ int do_clone(int argc, char **argv)
 	args.fd = fd;
 	strcpy(args.name, newname);
 	res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args);
+	e = errno;
 
 	close(fd);
 	close(fddst);
 
 	if(res < 0 ){
-		fprintf( stderr, "ERROR: cannot snapshot '%s'\n",subvol);
+		fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
+			subvol, strerror(e));
 		return 11;
 	}
 
@@ -392,7 +397,7 @@ int do_clone(int argc, char **argv)
 
 int do_delete_subvolume(int argc, char **argv)
 {
-	int	res, fd, len;
+	int	res, fd, len, e;
 	struct btrfs_ioctl_vol_args	args;
 	char	*dname, *vname, *cpath;
 	char	*path = argv[1];
@@ -438,11 +443,13 @@ int do_delete_subvolume(int argc, char **argv)
 	printf("Delete subvolume '%s/%s'\n", dname, vname);
 	strcpy(args.name, vname);
 	res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
+	e = errno;
 
 	close(fd);
 
 	if(res < 0 ){
-		fprintf( stderr, "ERROR: cannot delete '%s/%s'\n",dname, 
vname);
+		fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
+			dname, vname, strerror(e));
 		return 11;
 	}
 
@@ -452,7 +459,7 @@ int do_delete_subvolume(int argc, char **argv)
 
 int do_create_subvol(int argc, char **argv)
 {
-	int	res, fddst, len;
+	int	res, fddst, len, e;
 	char	*newname;
 	char	*dstdir;
 	struct btrfs_ioctl_vol_args	args;
@@ -492,11 +499,13 @@ int do_create_subvol(int argc, char **argv)
 	printf("Create subvolume '%s/%s'\n", dstdir, newname);
 	strcpy(args.name, newname);
 	res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
+	e = errno;
 
 	close(fddst);
 
 	if(res < 0 ){
-		fprintf( stderr, "ERROR: cannot create subvolume\n");
+		fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
+			strerror(e));
 		return 11;
 	}
 
@@ -506,7 +515,7 @@ int do_create_subvol(int argc, char **argv)
 
 int do_fssync(int argc, char **argv)
 {
-	int fd, res;
+	int 	fd, res, e;
 	char	*path = argv[1];
 
 	fd = open_file_or_dir(path);
@@ -517,9 +526,11 @@ int do_fssync(int argc, char **argv)
 
 	printf("FSSync '%s'\n", path);
 	res = ioctl(fd, BTRFS_IOC_SYNC);
+	e = errno;
 	close(fd);
 	if( res < 0 ){
-		fprintf(stderr, "ERROR: unable to fs-syncing '%s'\n", path);
+		fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n", 
+			path, strerror(e));
 		return 16;
 	}
 
@@ -528,7 +539,7 @@ int do_fssync(int argc, char **argv)
 
 int do_scan(int argc, char **argv)
 {
-	int	i, fd;
+	int	i, fd, e;
 	if(argc<=1){
 		int ret;
 
@@ -560,10 +571,12 @@ int do_scan(int argc, char **argv)
 		 * a btrfs filesystem from an I/O error !!!
 		 */
 		ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
+		e = errno;
 
 		if( ret < 0 ){
 			close(fd);
-			fprintf(stderr, "ERROR: unable to scan the device 
'%s'\n", argv[i]);
+			fprintf(stderr, "ERROR: unable to scan the device '%s' 
- %s\n", 
+				argv[i], strerror(e));
 			return 11;
 		}
 	}
@@ -577,7 +590,7 @@ int do_resize(int argc, char **argv)
 {
 
 	struct btrfs_ioctl_vol_args	args;
-	int	fd, res, len;
+	int	fd, res, len, e;
 	char	*amount=argv[1], *path=argv[2];
 
 	fd = open_file_or_dir(path);
@@ -595,9 +608,11 @@ int do_resize(int argc, char **argv)
 	printf("Resize '%s' of '%s'\n", path, amount);
 	strcpy(args.name, amount);
 	res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
+	e = errno;
 	close(fd);
 	if( res < 0 ){
-		fprintf(stderr, "ERROR: unable to resize '%s'\n", path);
+		fprintf(stderr, "ERROR: unable to resize '%s' - %s\n", 
+			path, strerror(e));
 		return 30;
 	}
 	return 0;
@@ -691,7 +706,7 @@ int do_add_volume(int nargs, char **args)
 {
 
 	char	*mntpnt = args[nargs-1];
-	int	i, fdmnt, ret=0;
+	int	i, fdmnt, ret=0, e;
 
 
 	fdmnt = open_file_or_dir(mntpnt);
@@ -738,8 +753,10 @@ int do_add_volume(int nargs, char **args)
 
 		strcpy(ioctl_args.name, args[i]);
 		res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
+		e = errno;
 		if(res<0){
-			fprintf(stderr, "ERROR: error adding the device 
'%s'\n", args[i]);
+			fprintf(stderr, "ERROR: error adding the device '%s' - 
%s\n", 
+				args[i], strerror(e));
 			ret++;
 		}
 
@@ -756,7 +773,7 @@ int do_add_volume(int nargs, char **args)
 int do_balance(int argc, char **argv)
 {
 
-	int	fdmnt, ret=0;
+	int	fdmnt, ret=0, e;
 	struct btrfs_ioctl_vol_args args;
 	char	*path = argv[1];
 
@@ -768,9 +785,11 @@ int do_balance(int argc, char **argv)
 
 	memset(&args, 0, sizeof(args));
 	ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
+	e = errno;
 	close(fdmnt);
 	if(ret<0){
-		fprintf(stderr, "ERROR: balancing '%s'\n", path);
+		fprintf(stderr, "ERROR: error during balancing '%s' - %s\n", 
+			path, strerror(e));
 
 		return 19;
 	}
@@ -780,7 +799,7 @@ int do_remove_volume(int nargs, char **args)
 {
 
 	char	*mntpnt = args[nargs-1];
-	int	i, fdmnt, ret=0;
+	int	i, fdmnt, ret=0, e;
 
 	fdmnt = open_file_or_dir(mntpnt);
 	if (fdmnt < 0) {
@@ -794,8 +813,10 @@ int do_remove_volume(int nargs, char **args)
 
 		strcpy(arg.name, args[i]);
 		res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
+		e = errno;
 		if(res<0){
-			fprintf(stderr, "ERROR: error removing the device 
'%s'\n", args[i]);
+			fprintf(stderr, "ERROR: error removing the device '%s' 
- %s\n", 
+				args[i], strerror(e));
 			ret++;
 		}
 	}
@@ -809,7 +830,7 @@ int do_remove_volume(int nargs, char **args)
 
 int do_set_default_subvol(int nargs, char **argv)
 {
-	int	ret=0, fd;
+	int	ret=0, fd, e;
 	u64	objectid;
 	char	*path = argv[2];
 	char	*subvolid = argv[1];
@@ -826,9 +847,11 @@ int do_set_default_subvol(int nargs, char **argv)
 		return 30;
 	}
 	ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
+	e = errno;
 	close(fd);
 	if( ret < 0 ){
-		fprintf(stderr, "ERROR: unable to set a new default 
subvolume\n");
+		fprintf(stderr, "ERROR: unable to set a new default subvolume 
- %s\n",
+			strerror(e));
 		return 30;
 	}
 	return 0;
@@ -840,6 +863,7 @@ int do_df_filesystem(int nargs, char **argv)
 	u64 count = 0, i;
 	int ret;
 	int fd;
+	int e;
 	char *path = argv[1];
 
 	fd = open_file_or_dir(path);
@@ -874,7 +898,11 @@ int do_df_filesystem(int nargs, char **argv)
 	sargs->total_spaces = 0;
 
 	ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
+	e = errno;
 	if (ret) {
+		fprintf(stderr, "ERROR: couldn't get space info - %s\n",
+			strerror(e));
+		close(fd);
 		free(sargs);
 		return ret;
 	}
diff --git a/utils.c b/utils.c
index fd894f3..3e22403 100644
--- a/utils.c
+++ b/utils.c
@@ -821,6 +821,7 @@ void btrfs_register_one_device(char *fname)
 	struct btrfs_ioctl_vol_args args;
 	int fd;
 	int ret;
+	int e;
 
 	fd = open("/dev/btrfs-control", O_RDONLY);
 	if (fd < 0) {
@@ -830,6 +831,11 @@ void btrfs_register_one_device(char *fname)
 	}
 	strcpy(args.name, fname);
 	ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
+	e = errno;
+	if(ret<0){
+		fprintf(stderr, "ERROR: unable to scan the device '%s' - 
%s\n",
+			fname, strerror(e));
+	}
 	close(fd);
 }
 




-- 
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <kreijack@inwind.it>
Key fingerprint = 4769 7E51 5293 D36C 814E  C054 BF04 F161 3DC5 0512

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 190 bytes --]

             reply	other threads:[~2010-12-05 17:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-05 17:47 Goffredo Baroncelli [this message]
2011-06-12 21:51 ` [PATCH 2/5][REPOST][BTRFS-PROGS] Be more verbose when a ioctl returns an error Hugo Mills
2011-06-13 16:44   ` Goffredo Baroncelli
2011-06-13 16:51     ` Hugo Mills

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201012051847.39119.kreijack@libero.it \
    --to=kreijack@libero.it \
    --cc=chris.mason@oracle.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox