On Sun, May 12, 2019 at 6:54 PM Nir Soffer <nirsof@gmail.com> wrote:
If sanlock_read_resource_owners() is called with wrong sector size, fail
instead of falling back to actual sector size. This is more consistent
with sanlock_read_resource() and other other functions.

Signed-off-by: Nir Soffer <nsoffer@redhat.com>
---
 src/resource.c       | 22 ++++++++++++++++------
 tests/python_test.py |  1 -
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/src/resource.c b/src/resource.c
index a783106..3959b35 100644
--- a/src/resource.c
+++ b/src/resource.c
@@ -167,22 +167,20 @@ int read_resource_owners(struct task *task, struct token *token,
        uint64_t host_id;
        uint32_t checksum;
        char *lease_buf_dblock;
        char *lease_buf = NULL;
        char *hosts_buf = NULL;
+       int sector_size_set = token->sector_size != 0;
probably: const int sector_size_set...
        int align_size;
        int host_count = 0;
        int i, rv;

        disk = &token->disks[0];

-       /*
-        * We don't know the sector_size of the resource until the leader
-        * record has been read, start with the smaller size.
-        */
+       /* If sector size not set, start with the smaller one. */

-       if (!token->sector_size) {
+       if (!sector_size_set) {
                token->sector_size = 512;
                token->align_size = sector_size_to_align_size_old(512);
        }

        /* we could in-line paxos_read_buf here like we do in read_mode_block */
@@ -204,13 +202,25 @@ int read_resource_owners(struct task *task, struct token *token,

        align_size = leader_align_size_from_flag(leader.flags);
        if (!align_size)
                align_size = sector_size_to_align_size_old(leader.sector_size);

+       /* If caller specified values are incorrect, fail. */
+
+       if (sector_size_set && token->sector_size != leader.sector_size) {
+               log_errot(token, "read_resource_owners invalid sector_size: %d actual: %d",
+                         token->sector_size, leader.sector_size);
+               rv = -EINVAL;
+               goto out;
+       }
+
+       /*
+        * If the caller did not specify sector size and our guess was wrong,
+        * retry with the actual value
+        */
        if ((token->sector_size != leader.sector_size) ||
            (token->align_size != align_size)) {
-               /* initial sizes were wrong */
                log_debug("read_resource_owners rereading with correct sizses");
                token->sector_size = leader.sector_size;
                token->align_size  = align_size;
                free(lease_buf);
                lease_buf = NULL;
diff --git a/tests/python_test.py b/tests/python_test.py
index 72e3a1d..6f8f0bc 100644
--- a/tests/python_test.py
+++ b/tests/python_test.py
@@ -217,11 +217,10 @@ def test_read_resource_4k_invalid_sector_size(sanlock_daemon, user_4k_path):
     with pytest.raises(sanlock.SanlockException) as e:
         sanlock.read_resource(user_4k_path, sector=SECTOR_SIZE_512)
     assert e.value.errno == errno.EINVAL


-@pytest.mark.xfail(reason="fallback hides wrong value from caller")
 def test_read_resource_owners_4k_invalid_sector_size(
         sanlock_daemon, user_4k_path):
     disks = [(user_4k_path, 0)]

     sanlock.write_resource(
--
2.17.2