xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>, Wei Liu <wl@xen.org>
Subject: [Xen-devel] [RFC PATCH 3/5] libs: add libxenfs
Date: Wed, 11 Sep 2019 08:19:59 +0200	[thread overview]
Message-ID: <20190911062001.25931-4-jgross@suse.com> (raw)
In-Reply-To: <20190911062001.25931-1-jgross@suse.com>

Add the new library libxenfs for access to the hypervisor filesystem.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/Rules.mk                |   6 ++
 tools/libs/Makefile           |   1 +
 tools/libs/fs/Makefile        |  14 +++
 tools/libs/fs/core.c          | 198 ++++++++++++++++++++++++++++++++++++++++++
 tools/libs/fs/include/xenfs.h |  57 ++++++++++++
 tools/libs/fs/libxenfs.map    |   8 ++
 tools/libs/fs/xenfs.pc.in     |  10 +++
 7 files changed, 294 insertions(+)
 create mode 100644 tools/libs/fs/Makefile
 create mode 100644 tools/libs/fs/core.c
 create mode 100644 tools/libs/fs/include/xenfs.h
 create mode 100644 tools/libs/fs/libxenfs.map
 create mode 100644 tools/libs/fs/xenfs.pc.in

diff --git a/tools/Rules.mk b/tools/Rules.mk
index cf8935d6a3..60ab97e80f 100644
--- a/tools/Rules.mk
+++ b/tools/Rules.mk
@@ -19,6 +19,7 @@ XEN_LIBXENGNTTAB   = $(XEN_ROOT)/tools/libs/gnttab
 XEN_LIBXENCALL     = $(XEN_ROOT)/tools/libs/call
 XEN_LIBXENFOREIGNMEMORY = $(XEN_ROOT)/tools/libs/foreignmemory
 XEN_LIBXENDEVICEMODEL = $(XEN_ROOT)/tools/libs/devicemodel
+XEN_LIBXENFS       = $(XEN_ROOT)/tools/libs/fs
 XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
 XEN_XENLIGHT       = $(XEN_ROOT)/tools/libxl
 # Currently libxlutil lives in the same directory as libxenlight
@@ -134,6 +135,11 @@ SHDEPS_libxendevicemodel = $(SHLIB_libxentoollog) $(SHLIB_libxentoolcore) $(SHLI
 LDLIBS_libxendevicemodel = $(SHDEPS_libxendevicemodel) $(XEN_LIBXENDEVICEMODEL)/libxendevicemodel$(libextension)
 SHLIB_libxendevicemodel  = $(SHDEPS_libxendevicemodel) -Wl,-rpath-link=$(XEN_LIBXENDEVICEMODEL)
 
+CFLAGS_libxenfs = -I$(XEN_LIBXENFS)/include $(CFLAGS_xeninclude)
+SHDEPS_libxenfs = $(SHLIB_libxentoollog) $(SHLIB_libxentoolcore) $(SHLIB_xencall)
+LDLIBS_libxenfs = $(SHDEPS_libxenfs) $(XEN_LIBXENFS)/libxenfs$(libextension)
+SHLIB_libxenfs  = $(SHDEPS_libxenfs) -Wl,-rpath-link=$(XEN_LIBXENFS)
+
 # code which compiles against libxenctrl get __XEN_TOOLS__ and
 # therefore sees the unstable hypercall interfaces.
 CFLAGS_libxenctrl = -I$(XEN_LIBXC)/include $(CFLAGS_libxentoollog) $(CFLAGS_libxenforeignmemory) $(CFLAGS_libxendevicemodel) $(CFLAGS_xeninclude) -D__XEN_TOOLS__
diff --git a/tools/libs/Makefile b/tools/libs/Makefile
index 88901e7341..e21fd0516e 100644
--- a/tools/libs/Makefile
+++ b/tools/libs/Makefile
@@ -9,6 +9,7 @@ SUBDIRS-y += gnttab
 SUBDIRS-y += call
 SUBDIRS-y += foreignmemory
 SUBDIRS-y += devicemodel
+SUBDIRS-y += fs
 
 ifeq ($(CONFIG_RUMP),y)
 SUBDIRS-y := toolcore
diff --git a/tools/libs/fs/Makefile b/tools/libs/fs/Makefile
new file mode 100644
index 0000000000..26ac677bcc
--- /dev/null
+++ b/tools/libs/fs/Makefile
@@ -0,0 +1,14 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+MAJOR    = 1
+MINOR    = 0
+LIBNAME  := fs
+USELIBS  := toollog toolcore call
+
+SRCS-y                 += core.c
+
+include ../libs.mk
+
+$(PKG_CONFIG_LOCAL): PKG_CONFIG_INCDIR = $(XEN_LIBXENFS)/include
+$(PKG_CONFIG_LOCAL): PKG_CONFIG_CFLAGS_LOCAL = $(CFLAGS_xeninclude)
diff --git a/tools/libs/fs/core.c b/tools/libs/fs/core.c
new file mode 100644
index 0000000000..f202a6bd99
--- /dev/null
+++ b/tools/libs/fs/core.c
@@ -0,0 +1,198 @@
+/*
+ * Copyright (c) 2019 SUSE Software Solutions Germany GmbH
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define __XEN_TOOLS__ 1
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <xentoollog.h>
+#include <xenfs.h>
+#include <xencall.h>
+
+#include <xentoolcore_internal.h>
+
+struct xenfs_handle {
+    xentoollog_logger *logger, *logger_tofree;
+    unsigned int flags;
+    xencall_handle *xcall;
+};
+
+xenfs_handle *xenfs_open(xentoollog_logger *logger,
+                         unsigned open_flags)
+{
+    xenfs_handle *fshdl = calloc(1, sizeof(*fshdl));
+
+    if (!fshdl)
+        return NULL;
+
+    fshdl->flags = open_flags;
+    fshdl->logger = logger;
+    fshdl->logger_tofree = NULL;
+
+    if (!fshdl->logger) {
+        fshdl->logger = fshdl->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!fshdl->logger)
+            goto err;
+    }
+
+    fshdl->xcall = xencall_open(fshdl->logger, 0);
+    if (!fshdl->xcall)
+        goto err;
+
+
+    return fshdl;
+
+err:
+    xtl_logger_destroy(fshdl->logger_tofree);
+    xencall_close(fshdl->xcall);
+    free(fshdl);
+    return NULL;
+}
+
+int xenfs_close(xenfs_handle *fshdl)
+{
+    if (!fshdl)
+        return 0;
+
+    xencall_close(fshdl->xcall);
+    xtl_logger_destroy(fshdl->logger_tofree);
+    free(fshdl);
+    return 0;
+}
+
+static void *xenfs_read_any(xenfs_handle *fshdl, const char *path,
+                            unsigned int cmd)
+{
+    char *buf = NULL, *path_buf = NULL;
+    int ret;
+    int sz, path_sz;
+
+    if (!fshdl) {
+        errno = EBADF;
+        goto out;
+    }
+
+    path_sz = strlen(path) + 1;
+    if (path_sz > XEN_FS_MAX_PATHLEN)
+    {
+        errno = ENAMETOOLONG;
+        goto out;
+    }
+    path_buf = xencall_alloc_buffer(fshdl->xcall, path_sz);
+    if (!path_buf) {
+        errno = ENOMEM;
+        goto out;
+    }
+    strcpy(path_buf, path);
+
+    for (sz = 4096; sz > 0; sz = ret) {
+        if (buf)
+            xencall_free_buffer(fshdl->xcall, buf);
+
+        buf = xencall_alloc_buffer(fshdl->xcall, sz);
+        if (!buf) {
+            errno = ENOMEM;
+            goto out;
+        }
+
+        ret = xencall5(fshdl->xcall, __HYPERVISOR_filesystem_op, cmd,
+                       (unsigned long)path_buf, path_sz,
+                       (unsigned long)buf, sz);
+    }
+
+    if (ret < 0) {
+        errno = -ret;
+        xencall_free_buffer(fshdl->xcall, buf);
+        buf = NULL;
+        goto out;
+    }
+
+ out:
+    ret = errno;
+    xencall_free_buffer(fshdl->xcall, path_buf);
+    errno = ret;
+
+    return buf;
+}
+
+char *xenfs_read(xenfs_handle *fshdl, const char *path)
+{
+    char *buf, *ret_buf = NULL;
+    int ret;
+
+    buf = xenfs_read_any(fshdl, path, XEN_FS_OP_read_contents);
+    if (buf)
+        ret_buf = strdup(buf);
+
+    ret = errno;
+    xencall_free_buffer(fshdl->xcall, buf);
+    errno = ret;
+
+    return ret_buf;
+}
+
+struct xenfs_dirent *xenfs_readdir(xenfs_handle *fshdl, const char *path,
+                                   unsigned int *num_entries)
+{
+    void *buf, *curr;
+    int ret;
+    char *names;
+    struct xenfs_dirent *ret_buf = NULL;
+    unsigned int n, name_sz = 0;
+    struct xen_fs_direntry *entry;
+
+    buf = xenfs_read_any(fshdl, path, XEN_FS_OP_read_dir);
+    if (!buf)
+        goto out;
+
+    curr = buf;
+    for (n = 1;; n++) {
+        entry = curr;
+        name_sz += strlen(entry->name) + 1;
+        if (!entry->off_next)
+            break;
+
+        curr += entry->off_next;
+    }
+
+    ret_buf = malloc(n * sizeof(*ret_buf) + name_sz);
+    if (!ret_buf)
+        goto out;
+
+    *num_entries = n;
+    names = (char *)(ret_buf  + n);
+    curr = buf;
+    for (n = 0; n < *num_entries; n++) {
+        entry = curr;
+        ret_buf[n].name = names;
+        ret_buf[n].is_dir = entry->flags & XEN_FS_ISDIR;
+        strcpy(names, entry->name);
+        names += strlen(entry->name) + 1;
+        curr += entry->off_next;
+    }
+
+ out:
+    ret = errno;
+    xencall_free_buffer(fshdl->xcall, buf);
+    errno = ret;
+
+    return ret_buf;
+}
diff --git a/tools/libs/fs/include/xenfs.h b/tools/libs/fs/include/xenfs.h
new file mode 100644
index 0000000000..000919ed5f
--- /dev/null
+++ b/tools/libs/fs/include/xenfs.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2019 SUSE Software Solutions Germany GmbH
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef XENFS_H
+#define XENFS_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include <xen/xen.h>
+#include <xen/filesystem.h>
+
+/* Callers who don't care don't need to #include <xentoollog.h> */
+struct xentoollog_logger;
+
+typedef struct xenfs_handle xenfs_handle;
+
+struct xenfs_dirent {
+    char *name;
+    bool is_dir;
+};
+
+xenfs_handle *xenfs_open(struct xentoollog_logger *logger,
+                         unsigned int open_flags);
+int xenfs_close(xenfs_handle *fshdl);
+
+/* Returned buffer should be freed via free(). */
+char *xenfs_read(xenfs_handle *fshdl, const char *path);
+
+/* Returned buffer should be freed via free(). */
+struct xenfs_dirent *xenfs_readdir(xenfs_handle *fshdl, const char *path,
+                                   unsigned int *num_entries);
+
+#endif /* XENFS_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libs/fs/libxenfs.map b/tools/libs/fs/libxenfs.map
new file mode 100644
index 0000000000..ac42d9163e
--- /dev/null
+++ b/tools/libs/fs/libxenfs.map
@@ -0,0 +1,8 @@
+VERS_1.0 {
+	global:
+		xenfs_open;
+		xenfs_close;
+		xenfs_read;
+		xenfs_readdir;
+	local: *; /* Do not expose anything by default */
+};
diff --git a/tools/libs/fs/xenfs.pc.in b/tools/libs/fs/xenfs.pc.in
new file mode 100644
index 0000000000..ea3c17b253
--- /dev/null
+++ b/tools/libs/fs/xenfs.pc.in
@@ -0,0 +1,10 @@
+prefix=@@prefix@@
+includedir=@@incdir@@
+libdir=@@libdir@@
+
+Name: Xenfs
+Description: The Xenfs library for Xen hypervisor
+Version: @@version@@
+Cflags: -I${includedir} @@cflagslocal@@
+Libs: @@libsflag@@${libdir} -lxenfs
+Requires.private: xentoolcore,xentoollog,xencall
-- 
2.16.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2019-09-11  6:20 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-11  6:19 [Xen-devel] [RFC PATCH 0/5] Add hypervisor sysfs-like support Juergen Gross
2019-09-11  6:19 ` [Xen-devel] [RFC PATCH 1/5] docs: add feature document for Xen " Juergen Gross
2019-09-11  9:28   ` Jan Beulich
2019-09-11  9:29     ` Juergen Gross
2019-09-11  6:19 ` [Xen-devel] [RFC PATCH 2/5] xen: add basic hypervisor filesystem support Juergen Gross
2019-09-11  6:19 ` Juergen Gross [this message]
2019-09-11  6:20 ` [Xen-devel] [RFC PATCH 4/5] tools: add xenfs tool Juergen Gross
2019-09-11  9:30   ` Jan Beulich
2019-09-11  9:57     ` Juergen Gross
2019-09-11 10:07       ` Jan Beulich
2019-09-11 11:34         ` Juergen Gross
2019-09-11 11:50           ` Jan Beulich
2019-09-11 12:41             ` Juergen Gross
2019-09-11  6:20 ` [Xen-devel] [RFC PATCH 5/5] xen: add /buildinfo/config entry to hypervisor filesystem Juergen Gross
2019-09-11  9:24 ` [Xen-devel] [RFC PATCH 0/5] Add hypervisor sysfs-like support Jan Beulich
2019-09-11 10:02   ` Juergen Gross
2019-09-11 11:17 ` Andrew Cooper
2019-09-11 11:29   ` Juergen Gross
2019-09-11 11:54     ` Jan Beulich
2019-09-11 13:01       ` Juergen Gross
2019-09-11 15:01         ` Jan Beulich
2019-09-11 15:06           ` Juergen Gross
2019-09-11 15:20             ` Jan Beulich
2019-09-11 15:26               ` Juergen Gross

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=20190911062001.25931-4-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.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).