linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.cz
Subject: [PATCH v2 2/5] btrfs-progs: add framework to check features supported by sysfs
Date: Mon, 23 Nov 2015 20:56:15 +0800	[thread overview]
Message-ID: <1448283378-10579-3-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1448283378-10579-1-git-send-email-anand.jain@oracle.com>

This adds a framework to check the /sys/fs/btrfs/features for the list
of supported features by the btrfs kernel. Which I hope by using it the
mkfs and btrfs-convert could tune to set features as supported by the
running kernel.


Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 utils.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 utils.h |  1 +
 2 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/utils.c b/utils.c
index 24042e5..48a1989 100644
--- a/utils.c
+++ b/utils.c
@@ -577,22 +577,23 @@ out:
  */
 static const struct btrfs_fs_feature {
 	const char *name;
+	const char *name_ker;
 	u64 flag;
 	const char *desc;
 	const char *min_ker_ver;
 } mkfs_features[] = {
-	{ "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
+	{ "mixed-bg", "mixed_groups", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
 		"mixed data and metadata block groups", "2.7.31"},
-	{ "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
+	{ "extref", "extended_iref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
 		"increased hardlink limit per file to 65536", "3.7"},
-	{ "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
+	{ "raid56", "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
 		"raid56 extended format", "3.9"},
-	{ "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
+	{ "skinny-metadata", "skinny_metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
 		"reduced-size metadata extent refs", "3.10"},
-	{ "no-holes", BTRFS_FEATURE_INCOMPAT_NO_HOLES,
+	{ "no-holes", "no_holes", BTRFS_FEATURE_INCOMPAT_NO_HOLES,
 		"no explicit hole extents for files", "3.14"},
 	/* Keep this one last */
-	{ "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
+	{ "list-all", "", BTRFS_FEATURE_LIST_ALL, NULL }
 };
 
 static int parse_one_fs_feature(const char *name, u64 *flags)
@@ -602,10 +603,12 @@ static int parse_one_fs_feature(const char *name, u64 *flags)
 
 	for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
 		if (name[0] == '^' &&
-			!strcmp(mkfs_features[i].name, name + 1)) {
+			(!strcmp(mkfs_features[i].name, name + 1) ||
+			!strcmp(mkfs_features[i].name_ker, name + 1))) {
 			*flags &= ~ mkfs_features[i].flag;
 			found = 1;
-		} else if (!strcmp(mkfs_features[i].name, name)) {
+		} else if (!strcmp(mkfs_features[i].name, name) ||
+			!strcmp(mkfs_features[i].name_ker, name)) {
 			*flags |= mkfs_features[i].flag;
 			found = 1;
 		}
@@ -3147,3 +3150,50 @@ u64 btrfs_features_allowed_by_kernel(void)
 	}
 	return (features);
 }
+
+int check_or_load_btrfs_ko()
+{
+	int fd;
+
+	/*
+	 * open will load btrfs kernel module if its not loaded,
+	 * and if the kernel has CONFIG auto load set?
+	 */
+	fd = open("/dev/btrfs-control", O_RDONLY);
+	if (fd < 0)
+		return -errno;
+
+	close(fd);
+	return 0;
+}
+
+int btrfs_features_allowed_by_sysfs(u64 *features)
+{
+	int ret;
+	DIR *dir;
+	struct dirent *ent;
+
+	ret = check_or_load_btrfs_ko();
+	if (ret) {
+		/* returns, -errno */
+		return ret;
+	}
+
+	dir = opendir("/sys/fs/btrfs/features");
+	if (!dir) {
+		/*
+		 * An old kernel which does not support sysfs/features
+		 */
+		return -errno;
+	}
+
+	*features = 0;
+	while((ent = readdir(dir)) != NULL) {
+		if (!strcmp(".", ent->d_name) ||
+				!strcmp("..", ent->d_name))
+			continue;
+		parse_one_fs_feature(ent->d_name, features);
+	}
+	closedir(dir);
+	return 0;
+}
diff --git a/utils.h b/utils.h
index 9044643..af0aa31 100644
--- a/utils.h
+++ b/utils.h
@@ -105,6 +105,7 @@ char* btrfs_parse_fs_features(char *namelist, u64 *flags);
 void btrfs_process_fs_features(u64 flags);
 void btrfs_parse_features_to_string(char *buf, u64 flags);
 u64 btrfs_features_allowed_by_kernel(void);
+int btrfs_features_allowed_by_sysfs(u64 *features);
 
 struct btrfs_mkfs_config {
 	char *label;
-- 
2.6.2


  parent reply	other threads:[~2015-11-23 12:56 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-23 12:56 [PATCH v2 0/5] Make btrfs-progs really compatible with any kernel version Anand Jain
2015-11-23 12:56 ` [PATCH v2 1/5] btrfs-progs: introduce framework to check kernel supported features Anand Jain
2015-11-24 14:39   ` Mike Fleetwood
2015-11-24 20:21     ` Austin S Hemmelgarn
2015-11-26 17:38       ` David Sterba
2015-11-30 12:30         ` Austin S Hemmelgarn
2015-11-25 10:58   ` [PATCH v3 " Anand Jain
2015-11-23 12:56 ` Anand Jain [this message]
2015-11-23 12:56 ` [PATCH v2 3/5] btrfs-progs: kernel based default features for mkfs Anand Jain
2015-11-23 15:57   ` Christoph Anton Mitterer
2015-11-23 16:05     ` Austin S Hemmelgarn
2015-11-23 16:14       ` Christoph Anton Mitterer
2015-11-23 16:55         ` Austin S Hemmelgarn
2015-11-23 12:56 ` [PATCH v2 4/5] btrfs-progs: kernel based default features for btrfs-convert Anand Jain
2015-11-23 12:56 ` [PATCH 5/5] btrfs-progs: add warning when we fail to read sysfs or version Anand Jain
2015-11-23 17:56 ` [PATCH v2 0/5] Make btrfs-progs really compatible with any kernel version David Sterba
2015-11-23 20:14   ` Austin S Hemmelgarn
2015-11-24  6:29     ` Duncan
2015-11-24 13:22   ` Anand Jain
2015-12-04  1:44   ` Liu Bo
2015-12-04  2:08     ` Qu Wenruo
2015-12-04  2:53       ` Liu Bo
2015-12-04  3:57         ` Qu Wenruo
2015-12-04 18:23           ` Liu Bo
2015-12-04 14:19       ` David Sterba
2015-12-05  5:12         ` Anand Jain
2015-11-24 13:04 ` Anand Jain
2016-11-08 13:14 ` Anand Jain
2016-11-14 12:13   ` David Sterba
2016-11-22  8:54     ` Anand Jain
2016-11-22 13:16       ` David Sterba
2016-11-23  3:00         ` Anand Jain
2016-11-23 10:31           ` David Sterba

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=1448283378-10579-3-git-send-email-anand.jain@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=dsterba@suse.cz \
    --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;
as well as URLs for NNTP newsgroup(s).