1 Upstream-Status: Inappropriate [Backport]
2 From b3007332100e01ca84c161b6c75f0a414ab4611b Mon Sep 17 00:00:00 2001
3 From: Goffredo Baroncelli <kreijack@libero.it>
4 Date: Mon, 20 Dec 2010 20:06:19 +0000
5 Subject: [PATCH 05/15] Improve error handling in the btrfs command
9 below is enclosed a trivial patch, which has the aim to improve the error
10 reporting of the "btrfs" command.
14 http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git
20 I changed every printf("some-error") to something like:
23 fprintf(stderr, "ERROR: .... - %s", strerror(e));
27 1) all the error are reported to standard error
28 2) At the end of the message is printed the error as returned by the system.
30 The change is quite simple, I replaced every printf("some-error") to the line
31 above. I don't touched anything other.
32 I also integrated a missing "printf" on the basis of the Ben patch.
34 This patch leads the btrfs command to be more "user friendly" :-)
39 btrfs-list.c | 40 ++++++++++++++++++++++--------
40 btrfs_cmds.c | 77 ++++++++++++++++++++++++++++++++++++++++-----------------
42 3 files changed, 89 insertions(+), 34 deletions(-)
44 Signed-off-by: Chris Mason <chris.mason@oracle.com>
46 btrfs-list.c | 40 ++++++++++++++++++++++--------
47 btrfs_cmds.c | 77 ++++++++++++++++++++++++++++++++++++++++-----------------
49 3 files changed, 89 insertions(+), 34 deletions(-)
51 diff --git a/btrfs-list.c b/btrfs-list.c
52 index 93766a8..abcc2f4 100644
55 @@ -265,7 +265,7 @@ static int resolve_root(struct root_lookup *rl, struct root_info *ri)
56 static int lookup_ino_path(int fd, struct root_info *ri)
58 struct btrfs_ioctl_ino_lookup_args args;
64 @@ -275,9 +275,11 @@ static int lookup_ino_path(int fd, struct root_info *ri)
65 args.objectid = ri->dir_id;
67 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
70 - fprintf(stderr, "ERROR: Failed to lookup path for root %llu\n",
71 - (unsigned long long)ri->ref_tree);
72 + fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
73 + (unsigned long long)ri->ref_tree,
78 @@ -320,15 +322,18 @@ static u64 find_root_gen(int fd)
79 unsigned long off = 0;
84 memset(&ino_args, 0, sizeof(ino_args));
85 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
87 /* this ioctl fills in ino_args->treeid */
88 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
91 - fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu\n",
92 - (unsigned long long)BTRFS_FIRST_FREE_OBJECTID);
93 + fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
94 + (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
99 @@ -351,8 +356,10 @@ static u64 find_root_gen(int fd)
102 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
105 - fprintf(stderr, "ERROR: can't perform the search\n");
106 + fprintf(stderr, "ERROR: can't perform the search - %s\n",
110 /* the ioctl returns the number of item it found in nr_items */
111 @@ -407,14 +414,16 @@ static char *__ino_resolve(int fd, u64 dirid)
112 struct btrfs_ioctl_ino_lookup_args args;
117 memset(&args, 0, sizeof(args));
118 args.objectid = dirid;
120 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
123 - fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu\n",
124 - (unsigned long long)dirid);
125 + fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
126 + (unsigned long long)dirid, strerror(e) );
130 @@ -472,6 +481,7 @@ static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
131 struct btrfs_ioctl_search_header *sh;
132 unsigned long off = 0;
136 memset(&args, 0, sizeof(args));
138 @@ -490,8 +500,10 @@ static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
141 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
144 - fprintf(stderr, "ERROR: can't perform the search\n");
145 + fprintf(stderr, "ERROR: can't perform the search - %s\n",
149 /* the ioctl returns the number of item it found in nr_items */
150 @@ -550,6 +562,7 @@ int list_subvols(int fd)
156 root_lookup_init(&root_lookup);
158 @@ -578,8 +591,10 @@ int list_subvols(int fd)
161 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
164 - fprintf(stderr, "ERROR: can't perform the search\n");
165 + fprintf(stderr, "ERROR: can't perform the search - %s\n",
169 /* the ioctl returns the number of item it found in nr_items */
170 @@ -747,6 +762,7 @@ int find_updated_files(int fd, u64 root_id, u64 oldest_gen)
177 char *cache_dir_name = NULL;
178 @@ -773,8 +789,10 @@ int find_updated_files(int fd, u64 root_id, u64 oldest_gen)
179 max_found = find_root_gen(fd);
181 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
184 - fprintf(stderr, "ERROR: can't perform the search\n");
185 + fprintf(stderr, "ERROR: can't perform the search- %s\n",
189 /* the ioctl returns the number of item it found in nr_items */
190 diff --git a/btrfs_cmds.c b/btrfs_cmds.c
191 index fffb423..775bfe1 100644
194 @@ -156,6 +156,7 @@ int do_defrag(int ac, char **av)
197 struct btrfs_ioctl_defrag_range_args range;
202 @@ -219,19 +220,21 @@ int do_defrag(int ac, char **av)
205 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
208 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
209 if (ret && errno == ENOTTY) {
210 - fprintf(stderr, "defrag range ioctl not "
211 + fprintf(stderr, "ERROR: defrag range ioctl not "
212 "supported in this kernel, please try "
213 "without any options.\n");
220 - fprintf(stderr, "ioctl failed on %s ret %d errno %d\n",
221 - av[i], ret, errno);
222 + fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
223 + av[i], strerror(e));
227 @@ -310,7 +313,7 @@ int do_subvol_list(int argc, char **argv)
228 int do_clone(int argc, char **argv)
231 - int res, fd, fddst, len;
232 + int res, fd, fddst, len, e;
236 @@ -377,12 +380,14 @@ int do_clone(int argc, char **argv)
238 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
239 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args);
246 - fprintf( stderr, "ERROR: cannot snapshot '%s'\n",subvol);
247 + fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
248 + subvol, strerror(e));
252 @@ -392,7 +397,7 @@ int do_clone(int argc, char **argv)
254 int do_delete_subvolume(int argc, char **argv)
257 + int res, fd, len, e;
258 struct btrfs_ioctl_vol_args args;
259 char *dname, *vname, *cpath;
260 char *path = argv[1];
261 @@ -438,11 +443,13 @@ int do_delete_subvolume(int argc, char **argv)
262 printf("Delete subvolume '%s/%s'\n", dname, vname);
263 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
264 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
270 - fprintf( stderr, "ERROR: cannot delete '%s/%s'\n",dname, vname);
271 + fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
272 + dname, vname, strerror(e));
276 @@ -452,7 +459,7 @@ int do_delete_subvolume(int argc, char **argv)
278 int do_create_subvol(int argc, char **argv)
280 - int res, fddst, len;
281 + int res, fddst, len, e;
284 struct btrfs_ioctl_vol_args args;
285 @@ -492,11 +499,13 @@ int do_create_subvol(int argc, char **argv)
286 printf("Create subvolume '%s/%s'\n", dstdir, newname);
287 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
288 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
294 - fprintf( stderr, "ERROR: cannot create subvolume\n");
295 + fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
300 @@ -506,7 +515,7 @@ int do_create_subvol(int argc, char **argv)
302 int do_fssync(int argc, char **argv)
306 char *path = argv[1];
308 fd = open_file_or_dir(path);
309 @@ -517,9 +526,11 @@ int do_fssync(int argc, char **argv)
311 printf("FSSync '%s'\n", path);
312 res = ioctl(fd, BTRFS_IOC_SYNC);
316 - fprintf(stderr, "ERROR: unable to fs-syncing '%s'\n", path);
317 + fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
318 + path, strerror(e));
322 @@ -528,7 +539,7 @@ int do_fssync(int argc, char **argv)
324 int do_scan(int argc, char **argv)
331 @@ -560,10 +571,12 @@ int do_scan(int argc, char **argv)
332 * a btrfs filesystem from an I/O error !!!
334 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
339 - fprintf(stderr, "ERROR: unable to scan the device '%s'\n", argv[i]);
340 + fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
341 + argv[i], strerror(e));
345 @@ -577,7 +590,7 @@ int do_resize(int argc, char **argv)
348 struct btrfs_ioctl_vol_args args;
350 + int fd, res, len, e;
351 char *amount=argv[1], *path=argv[2];
353 fd = open_file_or_dir(path);
354 @@ -595,9 +608,11 @@ int do_resize(int argc, char **argv)
355 printf("Resize '%s' of '%s'\n", path, amount);
356 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
357 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
361 - fprintf(stderr, "ERROR: unable to resize '%s'\n", path);
362 + fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
363 + path, strerror(e));
367 @@ -691,7 +706,7 @@ int do_add_volume(int nargs, char **args)
370 char *mntpnt = args[nargs-1];
371 - int i, fdmnt, ret=0;
372 + int i, fdmnt, ret=0, e;
375 fdmnt = open_file_or_dir(mntpnt);
376 @@ -738,8 +753,10 @@ int do_add_volume(int nargs, char **args)
378 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
379 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
382 - fprintf(stderr, "ERROR: error adding the device '%s'\n", args[i]);
383 + fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
384 + args[i], strerror(e));
388 @@ -756,7 +773,7 @@ int do_add_volume(int nargs, char **args)
389 int do_balance(int argc, char **argv)
393 + int fdmnt, ret=0, e;
394 struct btrfs_ioctl_vol_args args;
395 char *path = argv[1];
397 @@ -768,9 +785,11 @@ int do_balance(int argc, char **argv)
399 memset(&args, 0, sizeof(args));
400 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
404 - fprintf(stderr, "ERROR: balancing '%s'\n", path);
405 + fprintf(stderr, "ERROR: error during balancing '%s' - %s\n",
406 + path, strerror(e));
410 @@ -780,7 +799,7 @@ int do_remove_volume(int nargs, char **args)
413 char *mntpnt = args[nargs-1];
414 - int i, fdmnt, ret=0;
415 + int i, fdmnt, ret=0, e;
417 fdmnt = open_file_or_dir(mntpnt);
419 @@ -794,8 +813,10 @@ int do_remove_volume(int nargs, char **args)
421 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
422 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
425 - fprintf(stderr, "ERROR: error removing the device '%s'\n", args[i]);
426 + fprintf(stderr, "ERROR: error removing the device '%s' - %s\n",
427 + args[i], strerror(e));
431 @@ -809,7 +830,7 @@ int do_remove_volume(int nargs, char **args)
433 int do_set_default_subvol(int nargs, char **argv)
438 char *path = argv[2];
439 char *subvolid = argv[1];
440 @@ -826,9 +847,11 @@ int do_set_default_subvol(int nargs, char **argv)
443 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
447 - fprintf(stderr, "ERROR: unable to set a new default subvolume\n");
448 + fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
453 @@ -840,6 +863,7 @@ int do_df_filesystem(int nargs, char **argv)
458 char *path = argv[1];
460 fd = open_file_or_dir(path);
461 @@ -856,7 +880,10 @@ int do_df_filesystem(int nargs, char **argv)
462 sargs->total_spaces = 0;
464 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
467 + fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
468 + path, strerror(e));
472 @@ -874,7 +901,11 @@ int do_df_filesystem(int nargs, char **argv)
473 sargs->total_spaces = 0;
475 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
478 + fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
479 + path, strerror(e));
484 diff --git a/utils.c b/utils.c
485 index d8c3dcc..2a15d86 100644
488 @@ -821,6 +821,7 @@ void btrfs_register_one_device(char *fname)
489 struct btrfs_ioctl_vol_args args;
494 fd = open("/dev/btrfs-control", O_RDONLY);
496 @@ -830,6 +831,11 @@ void btrfs_register_one_device(char *fname)
498 strncpy(args.name, fname, BTRFS_PATH_NAME_MAX);
499 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
502 + fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
503 + fname, strerror(e));