From 538395d60689146f40856d3c1251357bd1824357 Mon Sep 17 00:00:00 2001 From: Jianpeng Ma Date: Fri, 6 Feb 2015 09:19:20 +0800 Subject: [PATCH] rbd.py: Add rbd_read2/rbd_write2 funcion which can handle fadvise flags. Signed-off-by: Jianpeng Ma --- src/pybind/rbd.py | 25 +++++++++++++++++++------ src/test/pybind/test_rbd.py | 11 +++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/pybind/rbd.py b/src/pybind/rbd.py index 82f5033e41dea..0a9fb04e772c6 100644 --- a/src/pybind/rbd.py +++ b/src/pybind/rbd.py @@ -682,7 +682,7 @@ class Image(object): if ret != 0: raise make_ex(ret, 'error setting image %s to snapshot %s' % (self.name, name)) - def read(self, offset, length): + def read(self, offset, length, fadvise_flags=0): """ Read data from the image. Raises :class:`InvalidArgument` if part of the range specified is outside the image. @@ -691,12 +691,18 @@ class Image(object): :type offset: int :param length: how many bytes to read :type length: int + :param fadvise_flags: fadvise flags for this read + :type fadvise_flags: int :returns: str - the data read :raises: :class:`InvalidArgument`, :class:`IOError` """ ret_buf = create_string_buffer(length) - ret = self.librbd.rbd_read(self.image, c_uint64(offset), - c_size_t(length), byref(ret_buf)) + if fadvise_flags == 0: + ret = self.librbd.rbd_read(self.image, c_uint64(offset), + c_size_t(length), byref(ret_buf)) + else: + ret = self.librbd.rbd_read2(self.image, c_uint64(offset), + c_size_t(length), byref(ret_buf), c_int(fadvise_flags)) if ret < 0: raise make_ex(ret, 'error reading %s %ld~%ld' % (self.image, offset, length)) return ctypes.string_at(ret_buf, ret) @@ -753,7 +759,7 @@ class Image(object): msg = 'error generating diff from snapshot %s' % from_snapshot raise make_ex(ret, msg) - def write(self, data, offset): + def write(self, data, offset, fadvise_flags=0): """ Write data to the image. Raises :class:`InvalidArgument` if part of the write would fall outside the image. @@ -762,6 +768,8 @@ class Image(object): :type data: str :param offset: where to start writing data :type offset: int + :param fadvise_flags: fadvise flags for this write + :type fadvise_flags: int :returns: int - the number of bytes written :raises: :class:`IncompleteWriteError`, :class:`LogicError`, :class:`InvalidArgument`, :class:`IOError` @@ -769,8 +777,13 @@ class Image(object): if not isinstance(data, str): raise TypeError('data must be a string') length = len(data) - ret = self.librbd.rbd_write(self.image, c_uint64(offset), - c_size_t(length), c_char_p(data)) + if fadvise_flags == 0: + ret = self.librbd.rbd_write(self.image, c_uint64(offset), + c_size_t(length), c_char_p(data)) + else: + ret = self.librbd.rbd_write2(self.image, c_uint64(offset), + c_size_t(length), c_char_p(data), c_int(fadvise_flags)) + if ret == length: return ret elif ret < 0: diff --git a/src/test/pybind/test_rbd.py b/src/test/pybind/test_rbd.py index 84d5902623b09..a66b945324283 100644 --- a/src/test/pybind/test_rbd.py +++ b/src/test/pybind/test_rbd.py @@ -300,10 +300,21 @@ class TestImage(object): data = rand_data(256) self.image.write(data, 0) + def test_write_with_fadivse_flags(self): + data = rand_data(256) + self.image.write(data, 0, LIBRADOS_OP_FLAG_FADVISE_DONTNEED) + self.image.write(data, 0, LIBRADOS_OP_FLAG_FADVISE_NOCACHE) + def test_read(self): data = self.image.read(0, 20) eq(data, '\0' * 20) + def test_read_with_fadivse_flags(self): + data = self.image.read(0, 20, LIBRADOS_OP_FLAG_FADIVSE_DONTNEED) + eq(data, '\0' * 20) + data = self.image.read(0, 20, LIBRADOS_OP_FLAG_FADIVSE_RANDOM) + eq(data, '\0' * 20) + def test_large_write(self): data = rand_data(IMG_SIZE) self.image.write(data, 0) -- 2.39.5