linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Omar Sandoval <osandov@osandov.com>
To: linux-btrfs@vger.kernel.org
Cc: kernel-team@fb.com
Subject: [PATCH v2 06/27] libbtrfsutil: add btrfs_util_create_subvolume()
Date: Thu, 15 Feb 2018 11:04:51 -0800	[thread overview]
Message-ID: <afd80fc716f0242909ca573abdf0ba3ae432b6e4.1518720598.git.osandov@fb.com> (raw)
In-Reply-To: <cover.1518720598.git.osandov@fb.com>
In-Reply-To: <cover.1518720598.git.osandov@fb.com>

From: Omar Sandoval <osandov@fb.com>

Doing the ioctl() directly isn't too bad, but passing in a full path is
more convenient than opening the parent and passing the path component.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 libbtrfsutil/btrfsutil.h                    |  34 +++++++++
 libbtrfsutil/python/btrfsutilpy.h           |   1 +
 libbtrfsutil/python/module.c                |   8 ++
 libbtrfsutil/python/subvolume.c             |  29 +++++++
 libbtrfsutil/python/tests/test_qgroup.py    |  11 +++
 libbtrfsutil/python/tests/test_subvolume.py |  49 +++++++++++-
 libbtrfsutil/subvolume.c                    | 114 ++++++++++++++++++++++++++++
 7 files changed, 245 insertions(+), 1 deletion(-)

diff --git a/libbtrfsutil/btrfsutil.h b/libbtrfsutil/btrfsutil.h
index 76bf0b60..5d67f111 100644
--- a/libbtrfsutil/btrfsutil.h
+++ b/libbtrfsutil/btrfsutil.h
@@ -105,6 +105,40 @@ enum btrfs_util_error btrfs_util_subvolume_id_fd(int fd, uint64_t *id_ret);
 
 struct btrfs_util_qgroup_inherit;
 
+/**
+ * btrfs_util_create_subvolume() - Create a new subvolume.
+ * @path: Where to create the subvolume.
+ * @flags: Must be zero.
+ * @async_transid: If not NULL, create the subvolume asynchronously (i.e.,
+ * without waiting for it to commit it to disk) and return the transaction ID
+ * that it was created in. This transaction ID can be waited on with
+ * btrfs_util_wait_sync().
+ * @qgroup_inherit: Qgroups to inherit from, or NULL.
+ *
+ * Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
+ */
+enum btrfs_util_error btrfs_util_create_subvolume(const char *path, int flags,
+						  uint64_t *async_transid,
+						  struct btrfs_util_qgroup_inherit *qgroup_inherit);
+
+/**
+ * btrfs_util_create_subvolume_fd() - Create a new subvolume given its parent
+ * and name.
+ * @parent_fd: File descriptor of the parent directory where the subvolume
+ * should be created.
+ * @name: Name of the subvolume to create.
+ * @flags: See btrfs_util_create_subvolume().
+ * @async_transid: See btrfs_util_create_subvolume().
+ * @qgroup_inherit: See btrfs_util_create_subvolume().
+ *
+ * Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
+ */
+enum btrfs_util_error btrfs_util_create_subvolume_fd(int parent_fd,
+						     const char *name,
+						     int flags,
+						     uint64_t *async_transid,
+						     struct btrfs_util_qgroup_inherit *qgroup_inherit);
+
 /**
  * btrfs_util_create_qgroup_inherit() - Create a qgroup inheritance specifier
  * for btrfs_util_create_subvolume() or btrfs_util_create_snapshot().
diff --git a/libbtrfsutil/python/btrfsutilpy.h b/libbtrfsutil/python/btrfsutilpy.h
index a36f2671..87d47ae0 100644
--- a/libbtrfsutil/python/btrfsutilpy.h
+++ b/libbtrfsutil/python/btrfsutilpy.h
@@ -60,6 +60,7 @@ void SetFromBtrfsUtilErrorWithPaths(enum btrfs_util_error err,
 
 PyObject *is_subvolume(PyObject *self, PyObject *args, PyObject *kwds);
 PyObject *subvolume_id(PyObject *self, PyObject *args, PyObject *kwds);
+PyObject *create_subvolume(PyObject *self, PyObject *args, PyObject *kwds);
 
 void add_module_constants(PyObject *m);
 
diff --git a/libbtrfsutil/python/module.c b/libbtrfsutil/python/module.c
index de7d17a1..69bba704 100644
--- a/libbtrfsutil/python/module.c
+++ b/libbtrfsutil/python/module.c
@@ -144,6 +144,14 @@ static PyMethodDef btrfsutil_methods[] = {
 	 "Get the ID of the subvolume containing a file.\n\n"
 	 "Arguments:\n"
 	 "path -- string, bytes, path-like object, or open file descriptor"},
+	{"create_subvolume", (PyCFunction)create_subvolume,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "create_subvolume(path, async=False)\n\n"
+	 "Create a new subvolume.\n\n"
+	 "Arguments:\n"
+	 "path -- string, bytes, or path-like object\n"
+	 "async -- create the subvolume without waiting for it to commit to\n"
+	 "disk and return the transaction ID"},
 	{},
 };
 
diff --git a/libbtrfsutil/python/subvolume.c b/libbtrfsutil/python/subvolume.c
index 4aab06a5..6f2080ee 100644
--- a/libbtrfsutil/python/subvolume.c
+++ b/libbtrfsutil/python/subvolume.c
@@ -71,3 +71,32 @@ PyObject *subvolume_id(PyObject *self, PyObject *args, PyObject *kwds)
 	path_cleanup(&path);
 	return PyLong_FromUnsignedLongLong(id);
 }
+
+PyObject *create_subvolume(PyObject *self, PyObject *args, PyObject *kwds)
+{
+	static char *keywords[] = {"path", "async", "qgroup_inherit", NULL};
+	struct path_arg path = {.allow_fd = false};
+	enum btrfs_util_error err;
+	int async = 0;
+	QgroupInherit *inherit = NULL;
+	uint64_t transid;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|pO!:create_subvolume",
+					 keywords, &path_converter, &path,
+					 &async, &QgroupInherit_type, &inherit))
+		return NULL;
+
+	err = btrfs_util_create_subvolume(path.path, 0, async ? &transid : NULL,
+					  inherit ? inherit->inherit : NULL);
+	if (err) {
+		SetFromBtrfsUtilErrorWithPath(err, &path);
+		path_cleanup(&path);
+		return NULL;
+	}
+
+	path_cleanup(&path);
+	if (async)
+		return PyLong_FromUnsignedLongLong(transid);
+	else
+		Py_RETURN_NONE;
+}
diff --git a/libbtrfsutil/python/tests/test_qgroup.py b/libbtrfsutil/python/tests/test_qgroup.py
index a590464b..19e6b05a 100644
--- a/libbtrfsutil/python/tests/test_qgroup.py
+++ b/libbtrfsutil/python/tests/test_qgroup.py
@@ -19,6 +19,17 @@ import os
 import unittest
 
 import btrfsutil
+from tests import BtrfsTestCase
+
+
+class TestQgroup(BtrfsTestCase):
+    def test_subvolume_inherit(self):
+        subvol = os.path.join(self.mountpoint, 'subvol')
+
+        inherit = btrfsutil.QgroupInherit()
+        inherit.add_group(5)
+
+        btrfsutil.create_subvolume(subvol, qgroup_inherit=inherit)
 
 
 class TestQgroupInherit(unittest.TestCase):
diff --git a/libbtrfsutil/python/tests/test_subvolume.py b/libbtrfsutil/python/tests/test_subvolume.py
index 44b1d7f0..f6c5958d 100644
--- a/libbtrfsutil/python/tests/test_subvolume.py
+++ b/libbtrfsutil/python/tests/test_subvolume.py
@@ -23,7 +23,7 @@ from pathlib import PurePath
 import traceback
 
 import btrfsutil
-from tests import BtrfsTestCase
+from tests import BtrfsTestCase, HAVE_PATH_LIKE
 
 
 class TestSubvolume(BtrfsTestCase):
@@ -55,3 +55,50 @@ class TestSubvolume(BtrfsTestCase):
         for arg in self.path_or_fd(dir):
             with self.subTest(type=type(arg)):
                 self.assertEqual(btrfsutil.subvolume_id(arg), 5)
+
+    def test_create_subvolume(self):
+        subvol = os.path.join(self.mountpoint, 'subvol')
+
+        btrfsutil.create_subvolume(subvol + '1')
+        self.assertTrue(btrfsutil.is_subvolume(subvol + '1'))
+        btrfsutil.create_subvolume((subvol + '2').encode())
+        self.assertTrue(btrfsutil.is_subvolume(subvol + '2'))
+        if HAVE_PATH_LIKE:
+            btrfsutil.create_subvolume(PurePath(subvol + '3'))
+            self.assertTrue(btrfsutil.is_subvolume(subvol + '3'))
+
+        pwd = os.getcwd()
+        try:
+            os.chdir(self.mountpoint)
+            btrfsutil.create_subvolume('subvol4')
+            self.assertTrue(btrfsutil.is_subvolume('subvol4'))
+        finally:
+            os.chdir(pwd)
+
+        btrfsutil.create_subvolume(subvol + '5/')
+        self.assertTrue(btrfsutil.is_subvolume(subvol + '5'))
+
+        btrfsutil.create_subvolume(subvol + '6//')
+        self.assertTrue(btrfsutil.is_subvolume(subvol + '6'))
+
+        transid = btrfsutil.create_subvolume(subvol + '7', async=True)
+        self.assertTrue(btrfsutil.is_subvolume(subvol + '7'))
+        self.assertGreater(transid, 0)
+
+        # Test creating subvolumes under '/' in a chroot.
+        pid = os.fork()
+        if pid == 0:
+            try:
+                os.chroot(self.mountpoint)
+                os.chdir('/')
+                btrfsutil.create_subvolume('/subvol8')
+                self.assertTrue(btrfsutil.is_subvolume('/subvol8'))
+                with self.assertRaises(btrfsutil.BtrfsUtilError):
+                    btrfsutil.create_subvolume('/')
+                os._exit(0)
+            except Exception:
+                traceback.print_exc()
+                os._exit(1)
+        wstatus = os.waitpid(pid, 0)[1]
+        self.assertTrue(os.WIFEXITED(wstatus))
+        self.assertEqual(os.WEXITSTATUS(wstatus), 0)
diff --git a/libbtrfsutil/subvolume.c b/libbtrfsutil/subvolume.c
index 04988c14..8efbdd43 100644
--- a/libbtrfsutil/subvolume.c
+++ b/libbtrfsutil/subvolume.c
@@ -19,6 +19,8 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
@@ -125,3 +127,115 @@ PUBLIC enum btrfs_util_error btrfs_util_subvolume_id_fd(int fd,
 
 	return BTRFS_UTIL_OK;
 }
+
+static enum btrfs_util_error openat_parent_and_name(int dirfd, const char *path,
+						    char *name, size_t name_len,
+						    int *fd)
+{
+	char *tmp_path, *slash, *dirname, *basename;
+	size_t len;
+
+	/* Ignore trailing slashes. */
+	len = strlen(path);
+	while (len > 1 && path[len - 1] == '/')
+		len--;
+
+	tmp_path = malloc(len + 1);
+	if (!tmp_path)
+		return BTRFS_UTIL_ERROR_NO_MEMORY;
+	memcpy(tmp_path, path, len);
+	tmp_path[len] = '\0';
+
+	slash = memrchr(tmp_path, '/', len);
+	if (slash == tmp_path) {
+		dirname = "/";
+		basename = tmp_path + 1;
+	} else if (slash) {
+		*slash = '\0';
+		dirname = tmp_path;
+		basename = slash + 1;
+	} else {
+		dirname = ".";
+		basename = tmp_path;
+	}
+
+	len = strlen(basename);
+	if (len >= name_len) {
+		errno = ENAMETOOLONG;
+		return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
+	}
+	memcpy(name, basename, len);
+	name[len] = '\0';
+
+	*fd = openat(dirfd, dirname, O_RDONLY | O_DIRECTORY);
+	if (*fd == -1) {
+		free(tmp_path);
+		return BTRFS_UTIL_ERROR_OPEN_FAILED;
+	}
+
+	free(tmp_path);
+	return BTRFS_UTIL_OK;
+}
+
+PUBLIC enum btrfs_util_error btrfs_util_create_subvolume(const char *path,
+							 int flags,
+							 uint64_t *async_transid,
+							 struct btrfs_util_qgroup_inherit *qgroup_inherit)
+{
+	char name[BTRFS_SUBVOL_NAME_MAX + 1];
+	enum btrfs_util_error err;
+	int parent_fd;
+
+	err = openat_parent_and_name(AT_FDCWD, path, name, sizeof(name),
+				     &parent_fd);
+	if (err)
+		return err;
+
+	err = btrfs_util_create_subvolume_fd(parent_fd, name, flags,
+					    async_transid, qgroup_inherit);
+	SAVE_ERRNO_AND_CLOSE(parent_fd);
+	return err;
+}
+
+PUBLIC enum btrfs_util_error btrfs_util_create_subvolume_fd(int parent_fd,
+							    const char *name,
+							    int flags,
+							    uint64_t *async_transid,
+							    struct btrfs_util_qgroup_inherit *qgroup_inherit)
+{
+	struct btrfs_ioctl_vol_args_v2 args = {};
+	size_t len;
+	int ret;
+
+	if (flags) {
+		errno = EINVAL;
+		return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
+	}
+
+	if (async_transid)
+		args.flags |= BTRFS_SUBVOL_CREATE_ASYNC;
+	if (qgroup_inherit) {
+		args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
+		args.qgroup_inherit = (struct btrfs_qgroup_inherit *)qgroup_inherit;
+		args.size = (sizeof(*args.qgroup_inherit) +
+			     args.qgroup_inherit->num_qgroups *
+			     sizeof(args.qgroup_inherit->qgroups[0]));
+	}
+
+	len = strlen(name);
+	if (len >= sizeof(args.name)) {
+		errno = ENAMETOOLONG;
+		return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
+	}
+	memcpy(args.name, name, len);
+	args.name[len] = '\0';
+
+	ret = ioctl(parent_fd, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
+	if (ret == -1)
+		return BTRFS_UTIL_ERROR_SUBVOL_CREATE_FAILED;
+
+	if (async_transid)
+		*async_transid = args.transid;
+
+	return BTRFS_UTIL_OK;
+}
-- 
2.16.1


  parent reply	other threads:[~2018-02-15 19:05 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-15 19:04 [PATCH v2 00/27] btrfs-progs: introduce libbtrfsutil, "btrfs-progs as a library" Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 01/27] btrfs-progs: get rid of undocumented qgroup inheritance options Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 02/27] Add libbtrfsutil Omar Sandoval
2018-02-20 17:32   ` Liu Bo
2018-02-20 18:34     ` David Sterba
2018-02-15 19:04 ` [PATCH v2 03/27] libbtrfsutil: add Python bindings Omar Sandoval
2018-02-21 13:47   ` David Sterba
2018-02-21 18:08     ` Omar Sandoval
2018-02-22  1:44   ` Misono, Tomohiro
2018-02-15 19:04 ` [PATCH v2 04/27] libbtrfsutil: add btrfs_util_is_subvolume() and btrfs_util_subvolume_id() Omar Sandoval
2018-02-21 11:43   ` David Sterba
2018-02-21 13:02     ` David Sterba
2018-02-21 18:13       ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 05/27] libbtrfsutil: add qgroup inheritance helpers Omar Sandoval
2018-02-15 19:04 ` Omar Sandoval [this message]
2018-02-23  8:24   ` [PATCH v2 06/27] libbtrfsutil: add btrfs_util_create_subvolume() Misono, Tomohiro
2018-02-23 22:58     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 07/27] libbtrfsutil: add btrfs_util_subvolume_path() Omar Sandoval
2018-02-23  6:27   ` Misono, Tomohiro
2018-02-23 22:44     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 08/27] libbtrfsutil: add btrfs_util_subvolume_info() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 09/27] libbtrfsutil: add btrfs_util_[gs]et_read_only() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 10/27] libbtrfsutil: add btrfs_util_[gs]et_default_subvolume() Omar Sandoval
2018-02-22  1:55   ` Misono, Tomohiro
2018-02-23 22:40     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 11/27] libbtrfsutil: add subvolume iterator helpers Omar Sandoval
2018-02-23  7:40   ` Misono, Tomohiro
2018-02-23 22:49     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 12/27] libbtrfsutil: add btrfs_util_create_snapshot() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 13/27] libbtrfsutil: add btrfs_util_delete_subvolume() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 14/27] libbtrfsutil: add btrfs_util_deleted_subvolumes() Omar Sandoval
2018-02-23  2:12   ` Misono, Tomohiro
2018-02-23 23:33     ` Omar Sandoval
2018-02-28  4:11       ` Misono, Tomohiro
2018-02-15 19:05 ` [PATCH v2 15/27] libbtrfsutil: add filesystem sync helpers Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 16/27] btrfs-progs: use libbtrfsutil for read-only property Omar Sandoval
2018-02-22  4:23   ` Misono, Tomohiro
2018-02-23 22:41     ` Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 17/27] btrfs-progs: use libbtrfsutil for sync ioctls Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 18/27] btrfs-progs: use libbtrfsutil for set-default Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 19/27] btrfs-progs: use libbtrfsutil for get-default Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 20/27] btrfs-progs: use libbtrfsutil for subvol create and snapshot Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 21/27] btrfs-progs: use libbtrfsutil for subvol delete Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 22/27] btrfs-progs: use libbtrfsutil for subvol show Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 23/27] btrfs-progs: use libbtrfsutil for subvol sync Omar Sandoval
2018-02-22  2:03   ` Misono, Tomohiro
2018-02-23 22:41     ` Omar Sandoval
2018-02-23 23:22       ` David Sterba
2018-02-22  2:09   ` Misono, Tomohiro
2018-02-15 19:05 ` [PATCH v2 24/27] btrfs-progs: replace test_issubvolume() with btrfs_util_is_subvolume() Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 25/27] btrfs-progs: add recursive snapshot/delete using libbtrfsutil Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 26/27] btrfs-progs: use libbtrfsutil for subvolume list Omar Sandoval
2018-02-23  2:26   ` Misono, Tomohiro
2018-02-23 23:05     ` Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 27/27] btrfs-progs: deprecate libbtrfs helpers Omar Sandoval
2018-02-21 15:04   ` David Sterba
2018-02-21 18:19     ` Omar Sandoval
2018-02-20 18:50 ` [PATCH v2 00/27] btrfs-progs: introduce libbtrfsutil, "btrfs-progs as a library" David Sterba
2018-02-21 15:13   ` David Sterba
2018-02-21 18:50     ` Omar Sandoval
2018-02-23 20:28       ` David Sterba
2018-02-26 23:36         ` Omar Sandoval
2018-02-27 15:04           ` David Sterba
2018-02-27 20:48             ` Omar Sandoval

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=afd80fc716f0242909ca573abdf0ba3ae432b6e4.1518720598.git.osandov@fb.com \
    --to=osandov@osandov.com \
    --cc=kernel-team@fb.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;
as well as URLs for NNTP newsgroup(s).