Skip to content

Commit 8206b92

Browse files
SaltyKitkatkdave
authored andcommitted
btrfs-progs: move tree_id parssing logic to parse-utils
Move it so that we can reuse it in elsewhere, such as the tree-stat subcommand. Pull-request: #1025
1 parent 0e21e4f commit 8206b92

File tree

3 files changed

+101
-95
lines changed

3 files changed

+101
-95
lines changed

cmds/inspect-dump-tree.c

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "common/help.h"
4040
#include "common/device-scan.h"
4141
#include "common/string-utils.h"
42+
#include "common/parse-utils.h"
4243
#include "cmds/commands.h"
4344

4445
static void print_extents(struct extent_buffer *eb)
@@ -137,81 +138,6 @@ static void print_old_roots(struct btrfs_super_block *super)
137138
}
138139
}
139140

140-
/*
141-
* Convert a tree name from various forms to the numerical id if possible
142-
* Accepted forms:
143-
* - case does not matter
144-
* - same as the key name, BTRFS_ROOT_TREE_OBJECTID
145-
* - dtto shortened, BTRFS_ROOT_TREE
146-
* - dtto without prefix, ROOT_TREE
147-
* - common name, ROOT, CHUNK, EXTENT, ...
148-
* - dtto alias, DEVICE for DEV, CHECKSUM for CSUM
149-
*
150-
* Returns 0 if the tree id was not recognized.
151-
*/
152-
static u64 treeid_from_string(const char *str, const char **end)
153-
{
154-
int match = 0;
155-
int i;
156-
u64 id;
157-
static struct treename {
158-
const char *name;
159-
u64 id;
160-
} tn[] = {
161-
{ "ROOT", BTRFS_ROOT_TREE_OBJECTID },
162-
{ "EXTENT", BTRFS_EXTENT_TREE_OBJECTID },
163-
{ "CHUNK", BTRFS_CHUNK_TREE_OBJECTID },
164-
{ "DEVICE", BTRFS_DEV_TREE_OBJECTID },
165-
{ "DEV", BTRFS_DEV_TREE_OBJECTID },
166-
{ "FS", BTRFS_FS_TREE_OBJECTID },
167-
{ "CSUM", BTRFS_CSUM_TREE_OBJECTID },
168-
{ "CHECKSUM", BTRFS_CSUM_TREE_OBJECTID },
169-
{ "QUOTA", BTRFS_QUOTA_TREE_OBJECTID },
170-
{ "UUID", BTRFS_UUID_TREE_OBJECTID },
171-
{ "FREE_SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
172-
{ "FREE-SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
173-
{ "TREE_LOG_FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
174-
{ "TREE-LOG-FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
175-
{ "TREE_LOG", BTRFS_TREE_LOG_OBJECTID },
176-
{ "TREE-LOG", BTRFS_TREE_LOG_OBJECTID },
177-
{ "TREE_RELOC", BTRFS_TREE_RELOC_OBJECTID },
178-
{ "TREE-RELOC", BTRFS_TREE_RELOC_OBJECTID },
179-
{ "DATA_RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID },
180-
{ "DATA-RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID },
181-
{ "BLOCK_GROUP", BTRFS_BLOCK_GROUP_TREE_OBJECTID },
182-
{ "BLOCK-GROUP", BTRFS_BLOCK_GROUP_TREE_OBJECTID },
183-
{ "RAID_STRIPE", BTRFS_RAID_STRIPE_TREE_OBJECTID },
184-
{ "RAID-STRIPE", BTRFS_RAID_STRIPE_TREE_OBJECTID },
185-
};
186-
187-
if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
188-
str += strlen("BTRFS_");
189-
190-
for (i = 0; i < ARRAY_SIZE(tn); i++) {
191-
int len = strlen(tn[i].name);
192-
193-
if (strncasecmp(tn[i].name, str, len) == 0) {
194-
id = tn[i].id;
195-
match = 1;
196-
str += len;
197-
break;
198-
}
199-
}
200-
201-
if (!match)
202-
return 0;
203-
204-
if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
205-
str += strlen("_TREE");
206-
207-
if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
208-
str += strlen("_OBJECTID");
209-
210-
*end = str;
211-
212-
return id;
213-
}
214-
215141
static const char * const cmd_inspect_dump_tree_usage[] = {
216142
"btrfs inspect-internal dump-tree [options] <device> [<device> ..]",
217143
"Dump tree structures from a given device",
@@ -417,27 +343,9 @@ static int cmd_inspect_dump_tree(const struct cmd_struct *cmd,
417343
if (ret < 0)
418344
goto out;
419345
break;
420-
case 't': {
421-
const char *end = NULL;
422-
423-
if (string_is_numerical(optarg))
424-
tree_id = arg_strtou64(optarg);
425-
else
426-
tree_id = treeid_from_string(optarg, &end);
427-
428-
if (!tree_id) {
429-
error("unrecognized tree id: %s",
430-
optarg);
431-
exit(1);
432-
}
433-
434-
if (end && *end) {
435-
error("unexpected tree id suffix of '%s': %s",
436-
optarg, end);
437-
exit(1);
438-
}
346+
case 't':
347+
tree_id = parse_tree_id(optarg);
439348
break;
440-
}
441349
case GETOPT_VAL_FOLLOW:
442350
follow = BTRFS_PRINT_TREE_FOLLOW;
443351
break;

common/parse-utils.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "kernel-shared/ctree.h"
2929
#include "kernel-shared/compression.h"
3030
#include "common/parse-utils.h"
31+
#include "common/string-utils.h"
3132
#include "common/messages.h"
3233
#include "common/utils.h"
3334

@@ -381,3 +382,99 @@ u64 parse_qgroupid_or_path(const char *p)
381382
exit(-1);
382383
}
383384

385+
/*
386+
* Convert a tree name from various forms to the numerical id if possible
387+
* Accepted forms:
388+
* - case does not matter
389+
* - same as the key name, BTRFS_ROOT_TREE_OBJECTID
390+
* - dtto shortened, BTRFS_ROOT_TREE
391+
* - dtto without prefix, ROOT_TREE
392+
* - common name, ROOT, CHUNK, EXTENT, ...
393+
* - dtto alias, DEVICE for DEV, CHECKSUM for CSUM
394+
*
395+
* Returns 0 if the tree id was not recognized.
396+
*/
397+
static u64 tree_id_from_string(const char *str, const char **end)
398+
{
399+
int match = 0;
400+
int i;
401+
u64 id;
402+
static struct treename {
403+
const char *name;
404+
u64 id;
405+
} tn[] = {
406+
{ "ROOT", BTRFS_ROOT_TREE_OBJECTID },
407+
{ "EXTENT", BTRFS_EXTENT_TREE_OBJECTID },
408+
{ "CHUNK", BTRFS_CHUNK_TREE_OBJECTID },
409+
{ "DEVICE", BTRFS_DEV_TREE_OBJECTID },
410+
{ "DEV", BTRFS_DEV_TREE_OBJECTID },
411+
{ "FS", BTRFS_FS_TREE_OBJECTID },
412+
{ "CSUM", BTRFS_CSUM_TREE_OBJECTID },
413+
{ "CHECKSUM", BTRFS_CSUM_TREE_OBJECTID },
414+
{ "QUOTA", BTRFS_QUOTA_TREE_OBJECTID },
415+
{ "UUID", BTRFS_UUID_TREE_OBJECTID },
416+
{ "FREE_SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
417+
{ "FREE-SPACE", BTRFS_FREE_SPACE_TREE_OBJECTID },
418+
{ "TREE_LOG_FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
419+
{ "TREE-LOG-FIXUP", BTRFS_TREE_LOG_FIXUP_OBJECTID },
420+
{ "TREE_LOG", BTRFS_TREE_LOG_OBJECTID },
421+
{ "TREE-LOG", BTRFS_TREE_LOG_OBJECTID },
422+
{ "TREE_RELOC", BTRFS_TREE_RELOC_OBJECTID },
423+
{ "TREE-RELOC", BTRFS_TREE_RELOC_OBJECTID },
424+
{ "DATA_RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID },
425+
{ "DATA-RELOC", BTRFS_DATA_RELOC_TREE_OBJECTID },
426+
{ "BLOCK_GROUP", BTRFS_BLOCK_GROUP_TREE_OBJECTID },
427+
{ "BLOCK-GROUP", BTRFS_BLOCK_GROUP_TREE_OBJECTID },
428+
{ "RAID_STRIPE", BTRFS_RAID_STRIPE_TREE_OBJECTID },
429+
{ "RAID-STRIPE", BTRFS_RAID_STRIPE_TREE_OBJECTID },
430+
};
431+
432+
if (strncasecmp("BTRFS_", str, strlen("BTRFS_")) == 0)
433+
str += strlen("BTRFS_");
434+
435+
for (i = 0; i < ARRAY_SIZE(tn); i++) {
436+
int len = strlen(tn[i].name);
437+
438+
if (strncasecmp(tn[i].name, str, len) == 0) {
439+
id = tn[i].id;
440+
match = 1;
441+
str += len;
442+
break;
443+
}
444+
}
445+
446+
if (!match)
447+
return 0;
448+
449+
if (strncasecmp("_TREE", str, strlen("_TREE")) == 0)
450+
str += strlen("_TREE");
451+
452+
if (strncasecmp("_OBJECTID", str, strlen("_OBJECTID")) == 0)
453+
str += strlen("_OBJECTID");
454+
455+
*end = str;
456+
457+
return id;
458+
}
459+
460+
u64 parse_tree_id(const char* str) {
461+
u64 tree_id = 0;
462+
const char* end = NULL;
463+
464+
if (string_is_numerical(optarg))
465+
tree_id = arg_strtou64(optarg);
466+
else
467+
tree_id = tree_id_from_string(optarg, &end);
468+
469+
if (!tree_id) {
470+
error("unrecognized tree id: %s", optarg);
471+
exit(1);
472+
}
473+
474+
if (end && *end) {
475+
error("unexpected tree id suffix of '%s': %s", optarg, end);
476+
exit(1);
477+
}
478+
479+
return tree_id;
480+
}

common/parse-utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ int parse_bg_profile(const char *profile, u64 *flags);
3030
int parse_compress_type(const char *type);
3131
int parse_qgroupid(const char *str, u64 *qgroupid);
3232
u64 parse_qgroupid_or_path(const char *p);
33+
u64 parse_tree_id(const char* str);
3334
int fls64(u64 x);
3435

3536
#endif

0 commit comments

Comments
 (0)