[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sanlock-devel
Subject:    [PATCH v3 2/4] Accept values instead of flags for sector and aling parameters.
From:       Vojtech Juranek <vjuranek () redhat ! com>
Date:       2019-04-30 14:34:21
Message-ID: 20190430143423.18929-3-vjuranek () redhat ! com
[Download RAW message or body]

Currently appropriate sanlock flags are accepted for sector size and
alignment parameters. This can be confusing for users, which may try
to pass values instead of flags and this approach also exposes sanlock
internal implementation details.

In order to have consistent and more intuitive API, values should be
accepted for sector size and alignment parameters. This patch replaces
flags by values in calls which accept sector size and alignment.

This patch also adds validation of provided values. If the value is
not the supported one, error with error code EINVAL is thrown.

The supported values are:
sector: 512, 4096
align: 1048576, 2097152, 4194304, 8388608
---
 python/example.py    |   4 +-
 python/sanlock.c     | 144 +++++++++++++++++++++++++++++++++----------
 tests/python_test.py |  17 ++---
 3 files changed, 125 insertions(+), 40 deletions(-)

diff --git a/python/example.py b/python/example.py
index 4fe845d..cb8f3e9 100644
--- a/python/example.py
+++ b/python/example.py
@@ -29,10 +29,10 @@ def main():
     fd = sanlock.register()
 
     print "Initializing '%s'" % (LOCKSPACE_NAME,)
-    sanlock.write_lockspace(LOCKSPACE_NAME, disk, max_hosts=0, iotimeout=0, \
align=sanlock.ALIGN1M, sector=sanlock.SECTOR512) +    \
sanlock.write_lockspace(LOCKSPACE_NAME, disk, max_hosts=0, iotimeout=0, \
align=1048576, sector=512)  
     print "Initializing '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME)
-    sanlock.write_resource(LOCKSPACE_NAME, RESOURCE_NAME, SNLK_DISKS, \
align=sanlock.ALIGN1M, sector=sanlock.SECTOR512) +    \
sanlock.write_resource(LOCKSPACE_NAME, RESOURCE_NAME, SNLK_DISKS, align=1048576, \
sector=512)  
     print "Acquiring the id '%i' on '%s'" % (HOST_ID, LOCKSPACE_NAME)
     sanlock.add_lockspace(LOCKSPACE_NAME, HOST_ID, disk)
diff --git a/python/sanlock.c b/python/sanlock.c
index 2ad4497..b57b51d 100644
--- a/python/sanlock.c
+++ b/python/sanlock.c
@@ -53,6 +53,7 @@ __set_exception(int en, char *msg)
     if (en < 0 && en > -200) {
         en = -en;
         err_name = strerror(en);
+
     } else {
         /* Safe to call without releasing the GIL. */
         err_name = sanlock_strerror(en);
@@ -135,6 +136,54 @@ exit_fail:
     return -1;
 }
 
+enum sector_size {SECTOR512 = 512, SECTOR4K = 4096};
+
+static uint32_t
+__sector_to_flag(int sector)
+{
+    uint32_t flag = 0;
+  
+    switch (sector) {
+    case SECTOR512:
+        flag = SANLK_LSF_SECTOR512;
+        break;
+    case SECTOR4K:
+        flag = SANLK_LSF_SECTOR4K;
+        break;
+    default:
+        __set_exception(EINVAL, "Invalid sector size value");
+    }
+
+    return flag;
+}
+
+enum alignment {ALIGN1M = 1048576, ALIGN2M = 2097152, ALIGN4M = 4194304, ALIGN8M = \
8388608}; +
+static uint32_t
+__align_to_flag(long align)
+{
+    uint32_t flag = 0;
+  
+    switch (align) {
+    case ALIGN1M:
+        flag = SANLK_RES_ALIGN1M;
+        break;
+    case ALIGN2M:
+        flag = SANLK_RES_ALIGN2M;
+        break;
+    case ALIGN4M:
+        flag = SANLK_RES_ALIGN4M;
+        break;
+    case ALIGN8M:
+        flag = SANLK_RES_ALIGN8M;
+        break;
+    default:
+        __set_exception(EINVAL, "Invalid alignment value");
+    }
+
+    return flag;
+}
+
 static PyObject *
 __hosts_to_list(struct sanlk_host *hss, int hss_count)
 {
@@ -361,15 +410,17 @@ exit_fail:
 /* write_lockspace */
 PyDoc_STRVAR(pydoc_write_lockspace, "\
 write_lockspace(lockspace, path, offset=0, max_hosts=0, iotimeout=0, \
-align=ALIGN1M, sector=SECTOR512)\n\
-Initialize or update a device to be used as sanlock lockspace.");
+align=1048576, sector=512)\n\
+Initialize or update a device to be used as sanlock lockspace.\n\
+Align can be one of (1048576, 2097152, 4194304, 8388608).\n\
+Sector can be one of (512, 4096).");
 
 static PyObject *
 py_write_lockspace(PyObject *self __unused, PyObject *args, PyObject *keywds)
 {
-    int rv, max_hosts = 0;
-    uint32_t io_timeout = 0;
-    uint32_t  align=SANLK_LSF_ALIGN1M, sector=SANLK_LSF_SECTOR512;
+    int rv, max_hosts = 0, sector = SECTOR512;
+    long align = ALIGN1M;
+    uint32_t io_timeout = 0, flag = 0;
     const char *lockspace, *path;
     struct sanlk_lockspace ls;
 
@@ -380,7 +431,7 @@ py_write_lockspace(PyObject *self __unused, PyObject *args, \
PyObject *keywds)  memset(&ls, 0, sizeof(struct sanlk_lockspace));
 
     /* parse python tuple */
-    if (!PyArg_ParseTupleAndKeywords(args, keywds, "ss|kiIII", kwlist,
+    if (!PyArg_ParseTupleAndKeywords(args, keywds, "ss|kiIli", kwlist,
         &lockspace, &path, &ls.host_id_disk.offset, &max_hosts,
         &io_timeout, &align, &sector)) {
         return NULL;
@@ -391,9 +442,14 @@ py_write_lockspace(PyObject *self __unused, PyObject *args, \
PyObject *keywds)  strncpy(ls.host_id_disk.path, path, SANLK_PATH_LEN - 1);
 
     /* set alignment/sector flags */
-    ls.flags |= align;
-    ls.flags |= sector;
+    if ((flag = __align_to_flag(align)) == 0)
+        return NULL;
+    ls.flags |= flag;
 
+    if ((flag = __sector_to_flag(sector)) == 0)
+        return NULL;
+    ls.flags |= flag;
+    
     /* write sanlock lockspace (gil disabled) */
     Py_BEGIN_ALLOW_THREADS
     rv = sanlock_write_lockspace(&ls, max_hosts, 0, io_timeout);
@@ -409,15 +465,17 @@ py_write_lockspace(PyObject *self __unused, PyObject *args, \
PyObject *keywds)  
 /* read_lockspace */
 PyDoc_STRVAR(pydoc_read_lockspace, "\
-read_lockspace(path, offset=0, align=ALIGN1M, sector=SECTOR512)\n -> dict\n\
-Read the lockspace information from a device at a specific offset.");
+read_lockspace(path, offset=0, align=1048576, sector=512)\n -> dict\n\
+Read the lockspace information from a device at a specific offset.\n\
+Align can be one of (1048576, 2097152, 4194304, 8388608).\n\
+Sector can be one of (512, 4096).");
 
 static PyObject *
 py_read_lockspace(PyObject *self __unused, PyObject *args, PyObject *keywds)
 {
-    int rv;
-    uint32_t io_timeout = 0;
-    uint32_t  align=SANLK_LSF_ALIGN1M, sector=SANLK_LSF_SECTOR512;
+    int rv, sector = SECTOR512;
+    long align = ALIGN1M;
+    uint32_t io_timeout = 0, flag = 0;
     const char *path;
     struct sanlk_lockspace ls;
     PyObject *ls_info = NULL, *ls_entry = NULL;
@@ -428,7 +486,7 @@ py_read_lockspace(PyObject *self __unused, PyObject *args, \
PyObject *keywds)  memset(&ls, 0, sizeof(struct sanlk_lockspace));
 
     /* parse python tuple */
-    if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|kII", kwlist,
+    if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|kli", kwlist,
         &path, &ls.host_id_disk.offset, &align, &sector)) {
         return NULL;
     }
@@ -437,8 +495,13 @@ py_read_lockspace(PyObject *self __unused, PyObject *args, \
PyObject *keywds)  strncpy(ls.host_id_disk.path, path, SANLK_PATH_LEN - 1);
 
     /* set alignment/sector flags */
-    ls.flags |= align;
-    ls.flags |= sector;
+    if ((flag = __align_to_flag(align)) == 0)
+        return NULL;
+    ls.flags |= flag;
+
+    if ((flag = __sector_to_flag(sector)) == 0)
+        return NULL;
+    ls.flags |= flag;
 
     /* read sanlock lockspace (gil disabled) */
     Py_BEGIN_ALLOW_THREADS
@@ -481,14 +544,17 @@ exit_fail:
 
 /* read_resource */
 PyDoc_STRVAR(pydoc_read_resource, "\
-read_resource(path, offset=0, align=ALIGN1M, sector=SECTOR512) -> dict\n\
-Read the resource information from a device at a specific offset.");
+read_resource(path, offset=0, align=1048576, sector=512) -> dict\n\
+Read the resource information from a device at a specific offset.\n\
+Align can be one of (1048576, 2097152, 4194304, 8388608).\n\
+Sector can be one of (512, 4096).");
 
 static PyObject *
 py_read_resource(PyObject *self __unused, PyObject *args, PyObject *keywds)
 {
-    int rv, rs_len;
-    uint32_t  align=SANLK_RES_ALIGN1M, sector=SANLK_RES_SECTOR512;
+    int rv, rs_len, sector = SECTOR512;
+    long align = ALIGN1M;
+    uint32_t flag = 0;
     const char *path;
     struct sanlk_resource *rs;
     PyObject *rs_info = NULL, *rs_entry = NULL;
@@ -509,7 +575,7 @@ py_read_resource(PyObject *self __unused, PyObject *args, \
PyObject *keywds)  rs->num_disks = 1;
 
     /* parse python tuple */
-    if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|kII", kwlist,
+    if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|kli", kwlist,
         &path, &(rs->disks[0].offset), &align, &sector)) {
         goto exit_fail;
     }
@@ -518,8 +584,15 @@ py_read_resource(PyObject *self __unused, PyObject *args, \
PyObject *keywds)  strncpy(rs->disks[0].path, path, SANLK_PATH_LEN - 1);
 
     /* set alignment/sector flags */
-    rs->flags |= align;
-    rs->flags |= sector;
+    if ((flag = __align_to_flag(align)) != 0)
+        rs->flags |= flag;
+    else
+        goto exit_fail;
+
+    if ((flag = __sector_to_flag(sector)) != 0)
+        rs->flags |= flag;
+    else
+        goto exit_fail;
 
     /* read sanlock resource (gil disabled) */
     Py_BEGIN_ALLOW_THREADS
@@ -573,27 +646,29 @@ exit_fail:
 /* write_resource */
 PyDoc_STRVAR(pydoc_write_resource, "\
 write_resource(lockspace, resource, disks, max_hosts=0, num_hosts=0, \
-clear=False, align=ALIGN1M, sector=SECTOR512)\n\
+clear=False, align=1048576, sector=512)\n\
 Initialize a device to be used as sanlock resource.\n\
 The disks must be in the format: [(path, offset), ... ].\n\
 If clear is True, the resource is cleared so subsequent read will\n\
-return an error.");
+return an error.\n\
+Align can be one of (1048576, 2097152, 4194304, 8388608).\n\
+Sector can be one of (512, 4096).");
 
 static PyObject *
 py_write_resource(PyObject *self __unused, PyObject *args, PyObject *keywds)
 {
-    int rv, max_hosts = 0, num_hosts = 0, clear = 0;
-    uint32_t  align=SANLK_RES_ALIGN1M, sector=SANLK_RES_SECTOR512;
+    int rv, max_hosts = 0, num_hosts = 0, clear = 0, sector = SECTOR512;
+    long align = ALIGN1M;
     const char *lockspace, *resource;
     struct sanlk_resource *rs;
     PyObject *disks;
-    uint32_t flags = 0;
+    uint32_t flags = 0, flag = 0;
 
     static char *kwlist[] = {"lockspace", "resource", "disks", "max_hosts",
                                 "num_hosts", "clear", "align", "sector", NULL};
 
     /* parse python tuple */
-    if (!PyArg_ParseTupleAndKeywords(args, keywds, "ssO!|iiiII",
+    if (!PyArg_ParseTupleAndKeywords(args, keywds, "ssO!|iiili",
         kwlist, &lockspace, &resource, &PyList_Type, &disks, &max_hosts,
         &num_hosts, &clear, &align, &sector)) {
         return NULL;
@@ -609,8 +684,15 @@ py_write_resource(PyObject *self __unused, PyObject *args, \
PyObject *keywds)  strncpy(rs->name, resource, SANLK_NAME_LEN);
 
     /* set alignment/sector flags */
-    rs->flags |= align;
-    rs->flags |= sector;
+    if ((flag = __align_to_flag(align)) != 0)
+        rs->flags |= flag;
+    else
+        goto exit_fail;
+
+    if ((flag = __sector_to_flag(sector)) != 0)
+        rs->flags |= flag;
+    else
+        goto exit_fail;
 
     if (clear) {
         flags |= SANLK_WRITE_CLEAR;
diff --git a/tests/python_test.py b/tests/python_test.py
index 6393222..1bb714a 100644
--- a/tests/python_test.py
+++ b/tests/python_test.py
@@ -23,6 +23,9 @@ LARGE_FILE_SIZE = 1024**4
 LOCKSPACE_SIZE = 1024**2
 MIN_RES_SIZE = 1024**2
 
+ALIGNMENT_1M = 1024**2
+SECTOR_SIZE_512 = 512
+
 
 @pytest.mark.parametrize("size,offset", [
     # Smallest offset.
@@ -35,11 +38,11 @@ def test_write_lockspace(tmpdir, sanlock_daemon, size, offset):
     util.create_file(path, size)
 
     sanlock.write_lockspace(
-        "name", path, offset=offset, iotimeout=1, align=sanlock.ALIGN1M,
-        sector=sanlock.SECTOR512)
+        "name", path, offset=offset, iotimeout=1, align=ALIGNMENT_1M,
+        sector=SECTOR_SIZE_512)
 
-    ls = sanlock.read_lockspace(
-        path, offset=offset, align=sanlock.ALIGN1M, sector=sanlock.SECTOR512)
+    ls = sanlock.read_lockspace(path, offset=offset, align=ALIGNMENT_1M,
+                                sector=SECTOR_SIZE_512)
     assert ls == {"iotimeout": 1, "lockspace": "name"}
 
     acquired = sanlock.inq_lockspace(
@@ -68,11 +71,11 @@ def test_write_resource(tmpdir, sanlock_daemon, size, offset):
     disks = [(path, offset)]
 
     sanlock.write_resource(
-        "ls_name", "res_name", disks, align=sanlock.ALIGN1M,
-        sector=sanlock.SECTOR512)
+        "ls_name", "res_name", disks, align=ALIGNMENT_1M,
+        sector=SECTOR_SIZE_512)
 
     res = sanlock.read_resource(
-        path, offset=offset, align=sanlock.ALIGN1M, sector=sanlock.SECTOR512)
+        path, offset=offset, align=ALIGNMENT_1M, sector=SECTOR_SIZE_512)
     assert res == {
         "lockspace": "ls_name",
         "resource": "res_name",
-- 
2.20.1
_______________________________________________
sanlock-devel mailing list -- sanlock-devel@lists.fedorahosted.org
To unsubscribe send an email to sanlock-devel-leave@lists.fedorahosted.org
Fedora Code of Conduct: https://getfedora.org/code-of-conduct.html
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedorahosted.org/archives/list/sanlock-devel@lists.fedorahosted.org



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic