]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/rbd/rbd.pyx
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / pybind / rbd / rbd.pyx
1 # cython: embedsignature=True
2 """
3 This module is a thin wrapper around librbd.
4
5 It currently provides all the synchronous methods of librbd that do
6 not use callbacks.
7
8 Error codes from librbd are turned into exceptions that subclass
9 :class:`Error`. Almost all methods may raise :class:`Error`
10 (the base class of all rbd exceptions), :class:`PermissionError`
11 and :class:`IOError`, in addition to those documented for the
12 method.
13 """
14 # Copyright 2011 Josh Durgin
15 # Copyright 2015 Hector Martin <marcan@marcan.st>
16
17 import cython
18 import sys
19
20 from cpython cimport PyObject, ref, exc
21 from libc cimport errno
22 from libc.stdint cimport *
23 from libc.stdlib cimport realloc, free
24 from libc.string cimport strdup
25
26 from collections import Iterable
27 from datetime import datetime
28
29 cimport rados
30
31
32 cdef extern from "Python.h":
33 # These are in cpython/string.pxd, but use "object" types instead of
34 # PyObject*, which invokes assumptions in cpython that we need to
35 # legitimately break to implement zero-copy string buffers in Image.read().
36 # This is valid use of the Python API and documented as a special case.
37 PyObject *PyBytes_FromStringAndSize(char *v, Py_ssize_t len) except NULL
38 char* PyBytes_AsString(PyObject *string) except NULL
39 int _PyBytes_Resize(PyObject **string, Py_ssize_t newsize) except -1
40
41 cdef extern from "time.h":
42 ctypedef long int time_t
43 cdef struct timespec:
44 time_t tv_sec
45 long tv_nsec
46
47 cdef extern from "limits.h":
48 cdef uint64_t INT64_MAX
49
50 cdef extern from "rbd/librbd.h" nogil:
51 enum:
52 _RBD_FEATURE_LAYERING "RBD_FEATURE_LAYERING"
53 _RBD_FEATURE_STRIPINGV2 "RBD_FEATURE_STRIPINGV2"
54 _RBD_FEATURE_EXCLUSIVE_LOCK "RBD_FEATURE_EXCLUSIVE_LOCK"
55 _RBD_FEATURE_OBJECT_MAP "RBD_FEATURE_OBJECT_MAP"
56 _RBD_FEATURE_FAST_DIFF "RBD_FEATURE_FAST_DIFF"
57 _RBD_FEATURE_DEEP_FLATTEN "RBD_FEATURE_DEEP_FLATTEN"
58 _RBD_FEATURE_JOURNALING "RBD_FEATURE_JOURNALING"
59 _RBD_FEATURE_DATA_POOL "RBD_FEATURE_DATA_POOL"
60
61 _RBD_FEATURES_INCOMPATIBLE "RBD_FEATURES_INCOMPATIBLE"
62 _RBD_FEATURES_RW_INCOMPATIBLE "RBD_FEATURES_RW_INCOMPATIBLE"
63 _RBD_FEATURES_MUTABLE "RBD_FEATURES_MUTABLE"
64 _RBD_FEATURES_SINGLE_CLIENT "RBD_FEATURES_SINGLE_CLIENT"
65 _RBD_FEATURES_ALL "RBD_FEATURES_ALL"
66
67 _RBD_FLAG_OBJECT_MAP_INVALID "RBD_FLAG_OBJECT_MAP_INVALID"
68 _RBD_FLAG_FAST_DIFF_INVALID "RBD_FLAG_FAST_DIFF_INVALID"
69
70 _RBD_IMAGE_OPTION_FORMAT "RBD_IMAGE_OPTION_FORMAT"
71 _RBD_IMAGE_OPTION_FEATURES "RBD_IMAGE_OPTION_FEATURES"
72 _RBD_IMAGE_OPTION_ORDER "RBD_IMAGE_OPTION_ORDER"
73 _RBD_IMAGE_OPTION_STRIPE_UNIT "RBD_IMAGE_OPTION_STRIPE_UNIT"
74 _RBD_IMAGE_OPTION_STRIPE_COUNT "RBD_IMAGE_OPTION_STRIPE_COUNT"
75 _RBD_IMAGE_OPTION_DATA_POOL "RBD_IMAGE_OPTION_DATA_POOL"
76
77 RBD_MAX_BLOCK_NAME_SIZE
78 RBD_MAX_IMAGE_NAME_SIZE
79
80 ctypedef void* rados_ioctx_t
81 ctypedef void* rbd_image_t
82 ctypedef void* rbd_image_options_t
83 ctypedef void *rbd_completion_t
84
85 ctypedef struct rbd_image_info_t:
86 uint64_t size
87 uint64_t obj_size
88 uint64_t num_objs
89 int order
90 char block_name_prefix[RBD_MAX_BLOCK_NAME_SIZE]
91 uint64_t parent_pool
92 char parent_name[RBD_MAX_IMAGE_NAME_SIZE]
93
94 ctypedef struct rbd_snap_info_t:
95 uint64_t id
96 uint64_t size
97 char *name
98
99 ctypedef enum rbd_mirror_mode_t:
100 _RBD_MIRROR_MODE_DISABLED "RBD_MIRROR_MODE_DISABLED"
101 _RBD_MIRROR_MODE_IMAGE "RBD_MIRROR_MODE_IMAGE"
102 _RBD_MIRROR_MODE_POOL "RBD_MIRROR_MODE_POOL"
103
104 ctypedef struct rbd_mirror_peer_t:
105 char *uuid
106 char *cluster_name
107 char *client_name
108
109 ctypedef enum rbd_mirror_image_state_t:
110 _RBD_MIRROR_IMAGE_DISABLING "RBD_MIRROR_IMAGE_DISABLING"
111 _RBD_MIRROR_IMAGE_ENABLED "RBD_MIRROR_IMAGE_ENABLED"
112 _RBD_MIRROR_IMAGE_DISABLED "RBD_MIRROR_IMAGE_DISABLED"
113
114 ctypedef struct rbd_mirror_image_info_t:
115 char *global_id
116 rbd_mirror_image_state_t state
117 bint primary
118
119 ctypedef enum rbd_mirror_image_status_state_t:
120 _MIRROR_IMAGE_STATUS_STATE_UNKNOWN "MIRROR_IMAGE_STATUS_STATE_UNKNOWN"
121 _MIRROR_IMAGE_STATUS_STATE_ERROR "MIRROR_IMAGE_STATUS_STATE_ERROR"
122 _MIRROR_IMAGE_STATUS_STATE_SYNCING "MIRROR_IMAGE_STATUS_STATE_SYNCING"
123 _MIRROR_IMAGE_STATUS_STATE_STARTING_REPLAY "MIRROR_IMAGE_STATUS_STATE_STARTING_REPLAY"
124 _MIRROR_IMAGE_STATUS_STATE_REPLAYING "MIRROR_IMAGE_STATUS_STATE_REPLAYING"
125 _MIRROR_IMAGE_STATUS_STATE_STOPPING_REPLAY "MIRROR_IMAGE_STATUS_STATE_STOPPING_REPLAY"
126 _MIRROR_IMAGE_STATUS_STATE_STOPPED "MIRROR_IMAGE_STATUS_STATE_STOPPED"
127
128 ctypedef struct rbd_mirror_image_status_t:
129 char *name
130 rbd_mirror_image_info_t info
131 rbd_mirror_image_status_state_t state
132 char *description
133 time_t last_update
134 bint up
135
136 ctypedef enum rbd_lock_mode_t:
137 _RBD_LOCK_MODE_EXCLUSIVE "RBD_LOCK_MODE_EXCLUSIVE"
138 _RBD_LOCK_MODE_SHARED "RBD_LOCK_MODE_SHARED"
139
140 ctypedef enum rbd_trash_image_source_t:
141 _RBD_TRASH_IMAGE_SOURCE_USER "RBD_TRASH_IMAGE_SOURCE_USER",
142 _RBD_TRASH_IMAGE_SOURCE_MIRRORING "RBD_TRASH_IMAGE_SOURCE_MIRRORING"
143
144 ctypedef struct rbd_trash_image_info_t:
145 char *id
146 char *name
147 rbd_trash_image_source_t source
148 time_t deletion_time
149 time_t deferment_end_time
150
151 ctypedef void (*rbd_callback_t)(rbd_completion_t cb, void *arg)
152 ctypedef int (*librbd_progress_fn_t)(uint64_t offset, uint64_t total, void* ptr)
153
154 void rbd_version(int *major, int *minor, int *extra)
155
156 void rbd_image_options_create(rbd_image_options_t* opts)
157 void rbd_image_options_destroy(rbd_image_options_t opts)
158 int rbd_image_options_set_string(rbd_image_options_t opts, int optname,
159 const char* optval)
160 int rbd_image_options_set_uint64(rbd_image_options_t opts, int optname,
161 uint64_t optval)
162 int rbd_image_options_get_string(rbd_image_options_t opts, int optname,
163 char* optval, size_t maxlen)
164 int rbd_image_options_get_uint64(rbd_image_options_t opts, int optname,
165 uint64_t* optval)
166 int rbd_image_options_unset(rbd_image_options_t opts, int optname)
167 void rbd_image_options_clear(rbd_image_options_t opts)
168 int rbd_image_options_is_empty(rbd_image_options_t opts)
169
170 int rbd_list(rados_ioctx_t io, char *names, size_t *size)
171 int rbd_create(rados_ioctx_t io, const char *name, uint64_t size,
172 int *order)
173 int rbd_create4(rados_ioctx_t io, const char *name, uint64_t size,
174 rbd_image_options_t opts)
175 int rbd_clone3(rados_ioctx_t p_ioctx, const char *p_name,
176 const char *p_snapname, rados_ioctx_t c_ioctx,
177 const char *c_name, rbd_image_options_t c_opts)
178 int rbd_remove(rados_ioctx_t io, const char *name)
179 int rbd_rename(rados_ioctx_t src_io_ctx, const char *srcname,
180 const char *destname)
181
182 int rbd_trash_move(rados_ioctx_t io, const char *name, uint64_t delay)
183 int rbd_trash_get(rados_ioctx_t io, const char *id,
184 rbd_trash_image_info_t *info)
185 void rbd_trash_get_cleanup(rbd_trash_image_info_t *info)
186 int rbd_trash_list(rados_ioctx_t io, rbd_trash_image_info_t *trash_entries,
187 size_t *num_entries)
188 void rbd_trash_list_cleanup(rbd_trash_image_info_t *trash_entries,
189 size_t num_entries)
190 int rbd_trash_remove(rados_ioctx_t io, const char *id, int force)
191 int rbd_trash_restore(rados_ioctx_t io, const char *id, const char *name)
192
193 int rbd_mirror_mode_get(rados_ioctx_t io, rbd_mirror_mode_t *mirror_mode)
194 int rbd_mirror_mode_set(rados_ioctx_t io, rbd_mirror_mode_t mirror_mode)
195 int rbd_mirror_peer_add(rados_ioctx_t io, char *uuid,
196 size_t uuid_max_length, const char *cluster_name,
197 const char *client_name)
198 int rbd_mirror_peer_remove(rados_ioctx_t io, const char *uuid)
199 int rbd_mirror_peer_list(rados_ioctx_t io_ctx, rbd_mirror_peer_t *peers,
200 int *max_peers)
201 void rbd_mirror_peer_list_cleanup(rbd_mirror_peer_t *peers, int max_peers)
202 int rbd_mirror_peer_set_client(rados_ioctx_t io, const char *uuid,
203 const char *client_name)
204 int rbd_mirror_peer_set_cluster(rados_ioctx_t io_ctx, const char *uuid,
205 const char *cluster_name)
206 int rbd_mirror_image_status_list(rados_ioctx_t io, const char *start_id,
207 size_t max, char **image_ids,
208 rbd_mirror_image_status_t *images,
209 size_t *len)
210 void rbd_mirror_image_status_list_cleanup(char **image_ids,
211 rbd_mirror_image_status_t *images,
212 size_t len)
213 int rbd_mirror_image_status_summary(rados_ioctx_t io,
214 rbd_mirror_image_status_state_t *states,
215 int *counts, size_t *maxlen)
216
217 int rbd_open(rados_ioctx_t io, const char *name,
218 rbd_image_t *image, const char *snap_name)
219 int rbd_open_read_only(rados_ioctx_t io, const char *name,
220 rbd_image_t *image, const char *snap_name)
221 int rbd_close(rbd_image_t image)
222 int rbd_resize(rbd_image_t image, uint64_t size)
223 int rbd_stat(rbd_image_t image, rbd_image_info_t *info, size_t infosize)
224 int rbd_get_old_format(rbd_image_t image, uint8_t *old)
225 int rbd_get_size(rbd_image_t image, uint64_t *size)
226 int rbd_get_features(rbd_image_t image, uint64_t *features)
227 int rbd_update_features(rbd_image_t image, uint64_t features,
228 uint8_t enabled)
229 int rbd_get_stripe_unit(rbd_image_t image, uint64_t *stripe_unit)
230 int rbd_get_stripe_count(rbd_image_t image, uint64_t *stripe_count)
231 int rbd_get_overlap(rbd_image_t image, uint64_t *overlap)
232 int rbd_get_id(rbd_image_t image, char *id, size_t id_len)
233 int rbd_get_block_name_prefix(rbd_image_t image, char *prefix,
234 size_t prefix_len)
235 int rbd_get_parent_info2(rbd_image_t image,
236 char *parent_poolname, size_t ppoolnamelen,
237 char *parent_name, size_t pnamelen,
238 char *parent_id, size_t pidlen,
239 char *parent_snapname, size_t psnapnamelen)
240 int rbd_get_flags(rbd_image_t image, uint64_t *flags)
241 ssize_t rbd_read2(rbd_image_t image, uint64_t ofs, size_t len,
242 char *buf, int op_flags)
243 ssize_t rbd_write2(rbd_image_t image, uint64_t ofs, size_t len,
244 const char *buf, int op_flags)
245 int rbd_discard(rbd_image_t image, uint64_t ofs, uint64_t len)
246 int rbd_copy3(rbd_image_t src, rados_ioctx_t dest_io_ctx,
247 const char *destname, rbd_image_options_t dest_opts)
248 int rbd_snap_list(rbd_image_t image, rbd_snap_info_t *snaps,
249 int *max_snaps)
250 void rbd_snap_list_end(rbd_snap_info_t *snaps)
251 int rbd_snap_create(rbd_image_t image, const char *snapname)
252 int rbd_snap_remove(rbd_image_t image, const char *snapname)
253 int rbd_snap_remove2(rbd_image_t image, const char *snapname, uint32_t flags,
254 librbd_progress_fn_t cb, void *cbdata)
255 int rbd_snap_rollback(rbd_image_t image, const char *snapname)
256 int rbd_snap_rename(rbd_image_t image, const char *snapname,
257 const char* dstsnapsname)
258 int rbd_snap_protect(rbd_image_t image, const char *snap_name)
259 int rbd_snap_unprotect(rbd_image_t image, const char *snap_name)
260 int rbd_snap_is_protected(rbd_image_t image, const char *snap_name,
261 int *is_protected)
262 int rbd_snap_get_limit(rbd_image_t image, uint64_t *limit)
263 int rbd_snap_set_limit(rbd_image_t image, uint64_t limit)
264 int rbd_snap_get_timestamp(rbd_image_t image, uint64_t snap_id, timespec *timestamp)
265 int rbd_snap_set(rbd_image_t image, const char *snapname)
266 int rbd_flatten(rbd_image_t image)
267 int rbd_rebuild_object_map(rbd_image_t image, librbd_progress_fn_t cb,
268 void *cbdata)
269 ssize_t rbd_list_children(rbd_image_t image, char *pools, size_t *pools_len,
270 char *images, size_t *images_len)
271 ssize_t rbd_list_lockers(rbd_image_t image, int *exclusive,
272 char *tag, size_t *tag_len,
273 char *clients, size_t *clients_len,
274 char *cookies, size_t *cookies_len,
275 char *addrs, size_t *addrs_len)
276 int rbd_lock_exclusive(rbd_image_t image, const char *cookie)
277 int rbd_lock_shared(rbd_image_t image, const char *cookie,
278 const char *tag)
279 int rbd_unlock(rbd_image_t image, const char *cookie)
280 int rbd_break_lock(rbd_image_t image, const char *client,
281 const char *cookie)
282
283 int rbd_is_exclusive_lock_owner(rbd_image_t image, int *is_owner)
284 int rbd_lock_acquire(rbd_image_t image, rbd_lock_mode_t lock_mode)
285 int rbd_lock_release(rbd_image_t image)
286 int rbd_lock_get_owners(rbd_image_t image, rbd_lock_mode_t *lock_mode,
287 char **lock_owners, size_t *max_lock_owners)
288 void rbd_lock_get_owners_cleanup(char **lock_owners,
289 size_t lock_owner_count)
290 int rbd_lock_break(rbd_image_t image, rbd_lock_mode_t lock_mode,
291 char *lock_owner)
292
293 # We use -9000 to propagate Python exceptions. We use except? to make sure
294 # things still work as intended if -9000 happens to be a valid errno value
295 # somewhere.
296 int rbd_diff_iterate2(rbd_image_t image, const char *fromsnapname,
297 uint64_t ofs, uint64_t len,
298 uint8_t include_parent, uint8_t whole_object,
299 int (*cb)(uint64_t, size_t, int, void *)
300 nogil except? -9000,
301 void *arg) except? -9000
302
303 int rbd_flush(rbd_image_t image)
304 int rbd_invalidate_cache(rbd_image_t image)
305
306 int rbd_mirror_image_enable(rbd_image_t image)
307 int rbd_mirror_image_disable(rbd_image_t image, bint force)
308 int rbd_mirror_image_promote(rbd_image_t image, bint force)
309 int rbd_mirror_image_demote(rbd_image_t image)
310 int rbd_mirror_image_resync(rbd_image_t image)
311 int rbd_mirror_image_get_info(rbd_image_t image,
312 rbd_mirror_image_info_t *mirror_image_info,
313 size_t info_size)
314 int rbd_mirror_image_get_status(rbd_image_t image,
315 rbd_mirror_image_status_t *mirror_image_status,
316 size_t status_size)
317
318 int rbd_aio_write2(rbd_image_t image, uint64_t off, size_t len,
319 const char *buf, rbd_completion_t c, int op_flags)
320 int rbd_aio_read2(rbd_image_t image, uint64_t off, size_t len,
321 char *buf, rbd_completion_t c, int op_flags)
322 int rbd_aio_discard(rbd_image_t image, uint64_t off, uint64_t len,
323 rbd_completion_t c)
324
325 int rbd_aio_create_completion(void *cb_arg, rbd_callback_t complete_cb,
326 rbd_completion_t *c)
327 int rbd_aio_is_complete(rbd_completion_t c)
328 int rbd_aio_wait_for_complete(rbd_completion_t c)
329 ssize_t rbd_aio_get_return_value(rbd_completion_t c)
330 void rbd_aio_release(rbd_completion_t c)
331 int rbd_aio_flush(rbd_image_t image, rbd_completion_t c)
332
333 int rbd_metadata_get(rbd_image_t image, const char *key, char *value,
334 size_t *val_len)
335 int rbd_metadata_set(rbd_image_t image, const char *key, const char *value)
336 int rbd_metadata_remove(rbd_image_t image, const char *key)
337 int rbd_metadata_list(rbd_image_t image, const char *start, uint64_t max,
338 char *keys, size_t *key_len, char *values,
339 size_t *vals_len)
340
341 RBD_FEATURE_LAYERING = _RBD_FEATURE_LAYERING
342 RBD_FEATURE_STRIPINGV2 = _RBD_FEATURE_STRIPINGV2
343 RBD_FEATURE_EXCLUSIVE_LOCK = _RBD_FEATURE_EXCLUSIVE_LOCK
344 RBD_FEATURE_OBJECT_MAP = _RBD_FEATURE_OBJECT_MAP
345 RBD_FEATURE_FAST_DIFF = _RBD_FEATURE_FAST_DIFF
346 RBD_FEATURE_DEEP_FLATTEN = _RBD_FEATURE_DEEP_FLATTEN
347 RBD_FEATURE_JOURNALING = _RBD_FEATURE_JOURNALING
348 RBD_FEATURE_DATA_POOL = _RBD_FEATURE_DATA_POOL
349
350 RBD_FEATURES_INCOMPATIBLE = _RBD_FEATURES_INCOMPATIBLE
351 RBD_FEATURES_RW_INCOMPATIBLE = _RBD_FEATURES_RW_INCOMPATIBLE
352 RBD_FEATURES_MUTABLE = _RBD_FEATURES_MUTABLE
353 RBD_FEATURES_SINGLE_CLIENT = _RBD_FEATURES_SINGLE_CLIENT
354 RBD_FEATURES_ALL = _RBD_FEATURES_ALL
355
356 RBD_FLAG_OBJECT_MAP_INVALID = _RBD_FLAG_OBJECT_MAP_INVALID
357
358 RBD_MIRROR_MODE_DISABLED = _RBD_MIRROR_MODE_DISABLED
359 RBD_MIRROR_MODE_IMAGE = _RBD_MIRROR_MODE_IMAGE
360 RBD_MIRROR_MODE_POOL = _RBD_MIRROR_MODE_POOL
361
362 RBD_MIRROR_IMAGE_DISABLING = _RBD_MIRROR_IMAGE_DISABLING
363 RBD_MIRROR_IMAGE_ENABLED = _RBD_MIRROR_IMAGE_ENABLED
364 RBD_MIRROR_IMAGE_DISABLED = _RBD_MIRROR_IMAGE_DISABLED
365
366 MIRROR_IMAGE_STATUS_STATE_UNKNOWN = _MIRROR_IMAGE_STATUS_STATE_UNKNOWN
367 MIRROR_IMAGE_STATUS_STATE_ERROR = _MIRROR_IMAGE_STATUS_STATE_ERROR
368 MIRROR_IMAGE_STATUS_STATE_SYNCING = _MIRROR_IMAGE_STATUS_STATE_SYNCING
369 MIRROR_IMAGE_STATUS_STATE_STARTING_REPLAY = _MIRROR_IMAGE_STATUS_STATE_STARTING_REPLAY
370 MIRROR_IMAGE_STATUS_STATE_REPLAYING = _MIRROR_IMAGE_STATUS_STATE_REPLAYING
371 MIRROR_IMAGE_STATUS_STATE_STOPPING_REPLAY = _MIRROR_IMAGE_STATUS_STATE_STOPPING_REPLAY
372 MIRROR_IMAGE_STATUS_STATE_STOPPED = _MIRROR_IMAGE_STATUS_STATE_STOPPED
373
374 RBD_LOCK_MODE_EXCLUSIVE = _RBD_LOCK_MODE_EXCLUSIVE
375 RBD_LOCK_MODE_SHARED = _RBD_LOCK_MODE_SHARED
376
377 RBD_IMAGE_OPTION_FORMAT = _RBD_IMAGE_OPTION_FORMAT
378 RBD_IMAGE_OPTION_FEATURES = _RBD_IMAGE_OPTION_FEATURES
379 RBD_IMAGE_OPTION_ORDER = _RBD_IMAGE_OPTION_ORDER
380 RBD_IMAGE_OPTION_STRIPE_UNIT = _RBD_IMAGE_OPTION_STRIPE_UNIT
381 RBD_IMAGE_OPTION_STRIPE_COUNT = _RBD_IMAGE_OPTION_STRIPE_COUNT
382 RBD_IMAGE_OPTION_DATA_POOL = _RBD_IMAGE_OPTION_DATA_POOL
383
384 class Error(Exception):
385 pass
386
387
388 class OSError(Error):
389 """ `OSError` class, derived from `Error` """
390 def __init__(self, errno, strerror):
391 self.errno = errno
392 self.strerror = strerror
393
394 def __str__(self):
395 return '[Errno {0}] {1}'.format(self.errno, self.strerror)
396
397
398 class PermissionError(OSError):
399 pass
400
401
402 class ImageNotFound(OSError):
403 pass
404
405
406 class ImageExists(OSError):
407 pass
408
409
410 class IOError(OSError):
411 pass
412
413
414 class NoSpace(OSError):
415 pass
416
417
418 class IncompleteWriteError(OSError):
419 pass
420
421
422 class InvalidArgument(OSError):
423 pass
424
425
426 class LogicError(Error):
427 pass
428
429
430 class ReadOnlyImage(OSError):
431 pass
432
433
434 class ImageBusy(OSError):
435 pass
436
437
438 class ImageHasSnapshots(OSError):
439 pass
440
441
442 class FunctionNotSupported(OSError):
443 pass
444
445
446 class ArgumentOutOfRange(OSError):
447 pass
448
449
450 class ConnectionShutdown(OSError):
451 pass
452
453
454 class Timeout(OSError):
455 pass
456
457 class DiskQuotaExceeded(OSError):
458 pass
459
460
461 cdef errno_to_exception = {
462 errno.EPERM : PermissionError,
463 errno.ENOENT : ImageNotFound,
464 errno.EIO : IOError,
465 errno.ENOSPC : NoSpace,
466 errno.EEXIST : ImageExists,
467 errno.EINVAL : InvalidArgument,
468 errno.EROFS : ReadOnlyImage,
469 errno.EBUSY : ImageBusy,
470 errno.ENOTEMPTY : ImageHasSnapshots,
471 errno.ENOSYS : FunctionNotSupported,
472 errno.EDOM : ArgumentOutOfRange,
473 errno.ESHUTDOWN : ConnectionShutdown,
474 errno.ETIMEDOUT : Timeout,
475 errno.EDQUOT : DiskQuotaExceeded,
476 }
477
478 cdef make_ex(ret, msg):
479 """
480 Translate a librbd return code into an exception.
481
482 :param ret: the return code
483 :type ret: int
484 :param msg: the error message to use
485 :type msg: str
486 :returns: a subclass of :class:`Error`
487 """
488 ret = abs(ret)
489 if ret in errno_to_exception:
490 return errno_to_exception[ret](ret, msg)
491 else:
492 return Error(ret, msg + (": error code %d" % ret))
493
494
495 cdef rados_ioctx_t convert_ioctx(rados.Ioctx ioctx) except? NULL:
496 return <rados_ioctx_t>ioctx.io
497
498 cdef int no_op_progress_callback(uint64_t offset, uint64_t total, void* ptr) nogil:
499 return 0
500
501 def cstr(val, name, encoding="utf-8", opt=False):
502 """
503 Create a byte string from a Python string
504
505 :param basestring val: Python string
506 :param str name: Name of the string parameter, for exceptions
507 :param str encoding: Encoding to use
508 :param bool opt: If True, None is allowed
509 :rtype: bytes
510 :raises: :class:`InvalidArgument`
511 """
512 if opt and val is None:
513 return None
514 if isinstance(val, bytes):
515 return val
516 elif isinstance(val, unicode):
517 return val.encode(encoding)
518 else:
519 raise InvalidArgument('%s must be a string' % name)
520
521 def decode_cstr(val, encoding="utf-8"):
522 """
523 Decode a byte string into a Python string.
524
525 :param bytes val: byte string
526 :rtype: unicode or None
527 """
528 if val is None:
529 return None
530
531 return val.decode(encoding)
532
533
534 cdef char* opt_str(s) except? NULL:
535 if s is None:
536 return NULL
537 return s
538
539 cdef void* realloc_chk(void* ptr, size_t size) except NULL:
540 cdef void *ret = realloc(ptr, size)
541 if ret == NULL:
542 raise MemoryError("realloc failed")
543 return ret
544
545 cdef class Completion
546
547 cdef void __aio_complete_cb(rbd_completion_t completion, void *args) with gil:
548 """
549 Callback to oncomplete() for asynchronous operations
550 """
551 cdef Completion cb = <Completion>args
552 cb._complete()
553
554
555 cdef class Completion(object):
556 """completion object"""
557
558 cdef:
559 object image
560 object oncomplete
561 rbd_completion_t rbd_comp
562 PyObject* buf
563 bint persisted
564 object exc_info
565
566 def __cinit__(self, image, object oncomplete):
567 self.oncomplete = oncomplete
568 self.image = image
569 self.persisted = False
570
571 def is_complete(self):
572 """
573 Has an asynchronous operation completed?
574
575 This does not imply that the callback has finished.
576
577 :returns: True if the operation is completed
578 """
579 with nogil:
580 ret = rbd_aio_is_complete(self.rbd_comp)
581 return ret == 1
582
583 def wait_for_complete_and_cb(self):
584 """
585 Wait for an asynchronous operation to complete
586
587 This method waits for the callback to execute, if one was provided.
588 It will also re-raise any exceptions raised by the callback. You
589 should call this to "reap" asynchronous completions and ensure that
590 any exceptions in the callbacks are handled, as an exception internal
591 to this module may have occurred.
592 """
593 with nogil:
594 rbd_aio_wait_for_complete(self.rbd_comp)
595
596 if self.exc_info:
597 raise self.exc_info[0], self.exc_info[1], self.exc_info[2]
598
599 def get_return_value(self):
600 """
601 Get the return value of an asychronous operation
602
603 The return value is set when the operation is complete.
604
605 :returns: int - return value of the operation
606 """
607 with nogil:
608 ret = rbd_aio_get_return_value(self.rbd_comp)
609 return ret
610
611 def __dealloc__(self):
612 """
613 Release a completion
614
615 This is automatically called when the completion object is freed.
616 """
617 ref.Py_XDECREF(self.buf)
618 self.buf = NULL
619 if self.rbd_comp != NULL:
620 with nogil:
621 rbd_aio_release(self.rbd_comp)
622 self.rbd_comp = NULL
623
624 cdef void _complete(self):
625 try:
626 self.__unpersist()
627 if self.oncomplete:
628 self.oncomplete(self)
629 # In the event that something raises an exception during the next 2
630 # lines of code, we will not be able to catch it, and this may result
631 # in the app not noticing a failed callback. However, this should only
632 # happen in extreme circumstances (OOM, etc.). KeyboardInterrupt
633 # should not be a problem because the callback thread from librbd
634 # ought to have SIGINT blocked.
635 except:
636 self.exc_info = sys.exc_info()
637
638 cdef __persist(self):
639 if self.oncomplete is not None and not self.persisted:
640 # Increment our own reference count to make sure the completion
641 # is not freed until the callback is called. The completion is
642 # allowed to be freed if there is no callback.
643 ref.Py_INCREF(self)
644 self.persisted = True
645
646 cdef __unpersist(self):
647 if self.persisted:
648 ref.Py_DECREF(self)
649 self.persisted = False
650
651
652 class RBD(object):
653 """
654 This class wraps librbd CRUD functions.
655 """
656 def version(self):
657 """
658 Get the version number of the ``librbd`` C library.
659
660 :returns: a tuple of ``(major, minor, extra)`` components of the
661 librbd version
662 """
663 cdef int major = 0
664 cdef int minor = 0
665 cdef int extra = 0
666 rbd_version(&major, &minor, &extra)
667 return (major, minor, extra)
668
669 def create(self, ioctx, name, size, order=None, old_format=True,
670 features=None, stripe_unit=None, stripe_count=None,
671 data_pool=None):
672 """
673 Create an rbd image.
674
675 :param ioctx: the context in which to create the image
676 :type ioctx: :class:`rados.Ioctx`
677 :param name: what the image is called
678 :type name: str
679 :param size: how big the image is in bytes
680 :type size: int
681 :param order: the image is split into (2**order) byte objects
682 :type order: int
683 :param old_format: whether to create an old-style image that
684 is accessible by old clients, but can't
685 use more advanced features like layering.
686 :type old_format: bool
687 :param features: bitmask of features to enable
688 :type features: int
689 :param stripe_unit: stripe unit in bytes (default None to let librbd decide)
690 :type stripe_unit: int
691 :param stripe_count: objects to stripe over before looping
692 :type stripe_count: int
693 :param data_pool: optional separate pool for data blocks
694 :type data_pool: str
695 :raises: :class:`ImageExists`
696 :raises: :class:`TypeError`
697 :raises: :class:`InvalidArgument`
698 :raises: :class:`FunctionNotSupported`
699 """
700 name = cstr(name, 'name')
701 cdef:
702 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
703 char *_name = name
704 uint64_t _size = size
705 int _order = 0
706 rbd_image_options_t opts
707 if order is not None:
708 _order = order
709 if old_format:
710 if (features or
711 ((stripe_unit is not None) and stripe_unit != 0) or
712 ((stripe_count is not None) and stripe_count != 0) or
713 data_pool):
714 raise InvalidArgument('format 1 images do not support feature '
715 'masks, non-default striping, nor data '
716 'pool')
717 with nogil:
718 ret = rbd_create(_ioctx, _name, _size, &_order)
719 else:
720 rbd_image_options_create(&opts)
721 try:
722 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FORMAT,
723 1 if old_format else 2)
724 if features is not None:
725 rbd_image_options_set_uint64(opts,
726 RBD_IMAGE_OPTION_FEATURES,
727 features)
728 if order is not None:
729 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER,
730 _order)
731 if stripe_unit is not None:
732 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT,
733 stripe_unit)
734 if stripe_count is not None:
735 rbd_image_options_set_uint64(opts,
736 RBD_IMAGE_OPTION_STRIPE_COUNT,
737 stripe_count)
738 if data_pool is not None:
739 rbd_image_options_set_string(opts,
740 RBD_IMAGE_OPTION_DATA_POOL,
741 data_pool)
742 with nogil:
743 ret = rbd_create4(_ioctx, _name, _size, opts)
744 finally:
745 rbd_image_options_destroy(opts)
746 if ret < 0:
747 raise make_ex(ret, 'error creating image')
748
749 def clone(self, p_ioctx, p_name, p_snapname, c_ioctx, c_name,
750 features=None, order=None, stripe_unit=None, stripe_count=None,
751 data_pool=None):
752 """
753 Clone a parent rbd snapshot into a COW sparse child.
754
755 :param p_ioctx: the parent context that represents the parent snap
756 :type ioctx: :class:`rados.Ioctx`
757 :param p_name: the parent image name
758 :type name: str
759 :param p_snapname: the parent image snapshot name
760 :type name: str
761 :param c_ioctx: the child context that represents the new clone
762 :type ioctx: :class:`rados.Ioctx`
763 :param c_name: the clone (child) name
764 :type name: str
765 :param features: bitmask of features to enable; if set, must include layering
766 :type features: int
767 :param order: the image is split into (2**order) byte objects
768 :type order: int
769 :param stripe_unit: stripe unit in bytes (default None to let librbd decide)
770 :type stripe_unit: int
771 :param stripe_count: objects to stripe over before looping
772 :type stripe_count: int
773 :param data_pool: optional separate pool for data blocks
774 :type data_pool: str
775 :raises: :class:`TypeError`
776 :raises: :class:`InvalidArgument`
777 :raises: :class:`ImageExists`
778 :raises: :class:`FunctionNotSupported`
779 :raises: :class:`ArgumentOutOfRange`
780 """
781 p_snapname = cstr(p_snapname, 'p_snapname')
782 p_name = cstr(p_name, 'p_name')
783 c_name = cstr(c_name, 'c_name')
784 cdef:
785 rados_ioctx_t _p_ioctx = convert_ioctx(p_ioctx)
786 rados_ioctx_t _c_ioctx = convert_ioctx(c_ioctx)
787 char *_p_name = p_name
788 char *_p_snapname = p_snapname
789 char *_c_name = c_name
790 rbd_image_options_t opts
791
792 rbd_image_options_create(&opts)
793 try:
794 if features is not None:
795 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FEATURES,
796 features)
797 if order is not None:
798 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER,
799 order)
800 if stripe_unit is not None:
801 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT,
802 stripe_unit)
803 if stripe_count is not None:
804 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_COUNT,
805 stripe_count)
806 if data_pool is not None:
807 rbd_image_options_set_string(opts, RBD_IMAGE_OPTION_DATA_POOL,
808 data_pool)
809 with nogil:
810 ret = rbd_clone3(_p_ioctx, _p_name, _p_snapname,
811 _c_ioctx, _c_name, opts)
812 finally:
813 rbd_image_options_destroy(opts)
814 if ret < 0:
815 raise make_ex(ret, 'error creating clone')
816
817 def list(self, ioctx):
818 """
819 List image names.
820
821 :param ioctx: determines which RADOS pool is read
822 :type ioctx: :class:`rados.Ioctx`
823 :returns: list -- a list of image names
824 """
825 cdef:
826 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
827 size_t size = 512
828 char *c_names = NULL
829 try:
830 while True:
831 c_names = <char *>realloc_chk(c_names, size)
832 with nogil:
833 ret = rbd_list(_ioctx, c_names, &size)
834 if ret >= 0:
835 break
836 elif ret != -errno.ERANGE:
837 raise make_ex(ret, 'error listing images')
838 return [decode_cstr(name) for name in c_names[:ret].split(b'\0')
839 if name]
840 finally:
841 free(c_names)
842
843 def remove(self, ioctx, name):
844 """
845 Delete an RBD image. This may take a long time, since it does
846 not return until every object that comprises the image has
847 been deleted. Note that all snapshots must be deleted before
848 the image can be removed. If there are snapshots left,
849 :class:`ImageHasSnapshots` is raised. If the image is still
850 open, or the watch from a crashed client has not expired,
851 :class:`ImageBusy` is raised.
852
853 :param ioctx: determines which RADOS pool the image is in
854 :type ioctx: :class:`rados.Ioctx`
855 :param name: the name of the image to remove
856 :type name: str
857 :raises: :class:`ImageNotFound`, :class:`ImageBusy`,
858 :class:`ImageHasSnapshots`
859 """
860 name = cstr(name, 'name')
861 cdef:
862 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
863 char *_name = name
864 with nogil:
865 ret = rbd_remove(_ioctx, _name)
866 if ret != 0:
867 raise make_ex(ret, 'error removing image')
868
869 def rename(self, ioctx, src, dest):
870 """
871 Rename an RBD image.
872
873 :param ioctx: determines which RADOS pool the image is in
874 :type ioctx: :class:`rados.Ioctx`
875 :param src: the current name of the image
876 :type src: str
877 :param dest: the new name of the image
878 :type dest: str
879 :raises: :class:`ImageNotFound`, :class:`ImageExists`
880 """
881 src = cstr(src, 'src')
882 dest = cstr(dest, 'dest')
883 cdef:
884 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
885 char *_src = src
886 char *_dest = dest
887 with nogil:
888 ret = rbd_rename(_ioctx, _src, _dest)
889 if ret != 0:
890 raise make_ex(ret, 'error renaming image')
891
892 def trash_move(self, ioctx, name, delay=0):
893 """
894 Moves an RBD image to the trash.
895 :param ioctx: determines which RADOS pool the image is in
896 :type ioctx: :class:`rados.Ioctx`
897 :param name: the name of the image to remove
898 :type name: str
899 :param delay: time delay in seconds before the image can be deleted
900 from trash
901 :type delay: int
902 :raises: :class:`ImageNotFound`
903 """
904 name = cstr(name, 'name')
905 cdef:
906 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
907 char *_name = name
908 uint64_t _delay = delay
909 with nogil:
910 ret = rbd_trash_move(_ioctx, _name, _delay)
911 if ret != 0:
912 raise make_ex(ret, 'error moving image to trash')
913
914 def trash_remove(self, ioctx, image_id, force=False):
915 """
916 Deletes an RBD image from trash. If image deferment time has not
917 expired :class:`PermissionError` is raised.
918 :param ioctx: determines which RADOS pool the image is in
919 :type ioctx: :class:`rados.Ioctx`
920 :param image_id: the id of the image to remove
921 :type image_id: str
922 :param force: force remove even if deferment time has not expired
923 :type force: bool
924 :raises: :class:`ImageNotFound`, :class:`PermissionError`
925 """
926 image_id = cstr(image_id, 'image_id')
927 cdef:
928 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
929 char *_image_id = image_id
930 int _force = force
931 with nogil:
932 ret = rbd_trash_remove(_ioctx, _image_id, _force)
933 if ret != 0:
934 raise make_ex(ret, 'error deleting image from trash')
935
936 def trash_get(self, ioctx, image_id):
937 """
938 Retrieve RBD image info from trash
939 :param ioctx: determines which RADOS pool the image is in
940 :type ioctx: :class:`rados.Ioctx`
941 :param image_id: the id of the image to restore
942 :type image_id: str
943 :returns: dict - contains the following keys:
944
945 * ``id`` (str) - image id
946
947 * ``name`` (str) - image name
948
949 * ``source`` (str) - source of deletion
950
951 * ``deletion_time`` (datetime) - time of deletion
952
953 * ``deferment_end_time`` (datetime) - time that an image is allowed
954 to be removed from trash
955
956 :raises: :class:`ImageNotFound`
957 """
958 image_id = cstr(image_id, 'image_id')
959 cdef:
960 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
961 char *_image_id = image_id
962 rbd_trash_image_info_t c_info
963 with nogil:
964 ret = rbd_trash_get(_ioctx, _image_id, &c_info)
965 if ret != 0:
966 raise make_ex(ret, 'error restoring image from trash')
967
968 __source_string = ['USER', 'MIRRORING']
969 info = {
970 'id' : decode_cstr(c_info.id),
971 'name' : decode_cstr(c_info.name),
972 'source' : __source_string[c_info.source],
973 'deletion_time' : datetime.fromtimestamp(c_info.deletion_time),
974 'deferment_end_time' : datetime.fromtimestamp(c_info.deferment_end_time)
975 }
976 rbd_trash_get_cleanup(&c_info)
977 return info
978
979 def trash_list(self, ioctx):
980 """
981 Lists all entries from trash.
982 :param ioctx: determines which RADOS pool the image is in
983 :type ioctx: :class:`rados.Ioctx`
984 :returns: :class:`TrashIterator`
985 """
986 return TrashIterator(ioctx)
987
988 def trash_restore(self, ioctx, image_id, name):
989 """
990 Restores an RBD image from trash.
991 :param ioctx: determines which RADOS pool the image is in
992 :type ioctx: :class:`rados.Ioctx`
993 :param image_id: the id of the image to restore
994 :type image_id: str
995 :param name: the new name of the restored image
996 :type name: str
997 :raises: :class:`ImageNotFound`
998 """
999 image_id = cstr(image_id, 'image_id')
1000 name = cstr(name, 'name')
1001 cdef:
1002 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1003 char *_image_id = image_id
1004 char *_name = name
1005 with nogil:
1006 ret = rbd_trash_restore(_ioctx, _image_id, _name)
1007 if ret != 0:
1008 raise make_ex(ret, 'error restoring image from trash')
1009
1010 def mirror_mode_get(self, ioctx):
1011 """
1012 Get pool mirror mode.
1013
1014 :param ioctx: determines which RADOS pool is read
1015 :type ioctx: :class:`rados.Ioctx`
1016 :returns: int - pool mirror mode
1017 """
1018 cdef:
1019 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1020 rbd_mirror_mode_t mirror_mode
1021 with nogil:
1022 ret = rbd_mirror_mode_get(_ioctx, &mirror_mode)
1023 if ret != 0:
1024 raise make_ex(ret, 'error getting mirror mode')
1025 return mirror_mode
1026
1027 def mirror_mode_set(self, ioctx, mirror_mode):
1028 """
1029 Set pool mirror mode.
1030
1031 :param ioctx: determines which RADOS pool is written
1032 :type ioctx: :class:`rados.Ioctx`
1033 :param mirror_mode: mirror mode to set
1034 :type mirror_mode: int
1035 """
1036 cdef:
1037 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1038 rbd_mirror_mode_t _mirror_mode = mirror_mode
1039 with nogil:
1040 ret = rbd_mirror_mode_set(_ioctx, _mirror_mode)
1041 if ret != 0:
1042 raise make_ex(ret, 'error setting mirror mode')
1043
1044 def mirror_peer_add(self, ioctx, cluster_name, client_name):
1045 """
1046 Add mirror peer.
1047
1048 :param ioctx: determines which RADOS pool is used
1049 :type ioctx: :class:`rados.Ioctx`
1050 :param cluster_name: mirror peer cluster name
1051 :type cluster_name: str
1052 :param client_name: mirror peer client name
1053 :type client_name: str
1054 :returns: str - peer uuid
1055 """
1056 cluster_name = cstr(cluster_name, 'cluster_name')
1057 client_name = cstr(client_name, 'client_name')
1058 cdef:
1059 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1060 char *_uuid = NULL
1061 size_t _uuid_max_length = 512
1062 char *_cluster_name = cluster_name
1063 char *_client_name = client_name
1064 try:
1065 _uuid = <char *>realloc_chk(_uuid, _uuid_max_length)
1066 ret = rbd_mirror_peer_add(_ioctx, _uuid, _uuid_max_length,
1067 _cluster_name, _client_name)
1068 if ret != 0:
1069 raise make_ex(ret, 'error adding mirror peer')
1070 return decode_cstr(_uuid)
1071 finally:
1072 free(_uuid)
1073
1074 def mirror_peer_remove(self, ioctx, uuid):
1075 """
1076 Remove mirror peer.
1077
1078 :param ioctx: determines which RADOS pool is used
1079 :type ioctx: :class:`rados.Ioctx`
1080 :param uuid: peer uuid
1081 :type uuid: str
1082 """
1083 uuid = cstr(uuid, 'uuid')
1084 cdef:
1085 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1086 char *_uuid = uuid
1087 with nogil:
1088 ret = rbd_mirror_peer_remove(_ioctx, _uuid)
1089 if ret != 0:
1090 raise make_ex(ret, 'error removing mirror peer')
1091
1092 def mirror_peer_list(self, ioctx):
1093 """
1094 Iterate over the peers of a pool.
1095
1096 :param ioctx: determines which RADOS pool is read
1097 :type ioctx: :class:`rados.Ioctx`
1098 :returns: :class:`MirrorPeerIterator`
1099 """
1100 return MirrorPeerIterator(ioctx)
1101
1102 def mirror_peer_set_client(self, ioctx, uuid, client_name):
1103 """
1104 Set mirror peer client name
1105
1106 :param ioctx: determines which RADOS pool is written
1107 :type ioctx: :class:`rados.Ioctx`
1108 :param uuid: uuid of the mirror peer
1109 :type uuid: str
1110 :param client_name: client name of the mirror peer to set
1111 :type client_name: str
1112 """
1113 uuid = cstr(uuid, 'uuid')
1114 client_name = cstr(client_name, 'client_name')
1115 cdef:
1116 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1117 char *_uuid = uuid
1118 char *_client_name = client_name
1119 with nogil:
1120 ret = rbd_mirror_peer_set_client(_ioctx, _uuid, _client_name)
1121 if ret != 0:
1122 raise make_ex(ret, 'error setting mirror peer client')
1123
1124 def mirror_peer_set_cluster(self, ioctx, uuid, cluster_name):
1125 """
1126 Set mirror peer cluster name
1127
1128 :param ioctx: determines which RADOS pool is written
1129 :type ioctx: :class:`rados.Ioctx`
1130 :param uuid: uuid of the mirror peer
1131 :type uuid: str
1132 :param cluster_name: cluster name of the mirror peer to set
1133 :type cluster_name: str
1134 """
1135 uuid = cstr(uuid, 'uuid')
1136 cluster_name = cstr(cluster_name, 'cluster_name')
1137 cdef:
1138 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1139 char *_uuid = uuid
1140 char *_cluster_name = cluster_name
1141 with nogil:
1142 ret = rbd_mirror_peer_set_cluster(_ioctx, _uuid, _cluster_name)
1143 if ret != 0:
1144 raise make_ex(ret, 'error setting mirror peer cluster')
1145
1146 def mirror_image_status_list(self, ioctx):
1147 """
1148 Iterate over the mirror image statuses of a pool.
1149
1150 :param ioctx: determines which RADOS pool is read
1151 :type ioctx: :class:`rados.Ioctx`
1152 :returns: :class:`MirrorImageStatus`
1153 """
1154 return MirrorImageStatusIterator(ioctx)
1155
1156 def mirror_image_status_summary(self, ioctx):
1157 """
1158 Get mirror image status summary of a pool.
1159
1160 :param ioctx: determines which RADOS pool is read
1161 :type ioctx: :class:`rados.Ioctx`
1162 :returns: list - a list of (state, count) tuples
1163 """
1164 cdef:
1165 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1166 rbd_mirror_image_status_state_t *states = NULL
1167 int *counts = NULL
1168 size_t maxlen = 32
1169 try:
1170 states = <rbd_mirror_image_status_state_t *>realloc_chk(states,
1171 sizeof(rbd_mirror_image_status_state_t) * maxlen)
1172 counts = <int *>realloc_chk(counts, sizeof(int) * maxlen)
1173 with nogil:
1174 ret = rbd_mirror_image_status_summary(_ioctx, states, counts,
1175 &maxlen)
1176 if ret < 0:
1177 raise make_ex(ret, 'error getting mirror image status summary')
1178 return [(states[i], counts[i]) for i in range(maxlen)]
1179 finally:
1180 free(states)
1181 free(counts)
1182
1183 cdef class MirrorPeerIterator(object):
1184 """
1185 Iterator over mirror peer info for a pool.
1186
1187 Yields a dictionary containing information about a peer.
1188
1189 Keys are:
1190
1191 * ``uuid`` (str) - uuid of the peer
1192
1193 * ``cluster_name`` (str) - cluster name of the peer
1194
1195 * ``client_name`` (str) - client name of the peer
1196 """
1197
1198 cdef:
1199 rbd_mirror_peer_t *peers
1200 int num_peers
1201
1202 def __init__(self, ioctx):
1203 cdef:
1204 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1205 self.peers = NULL
1206 self.num_peers = 10
1207 while True:
1208 self.peers = <rbd_mirror_peer_t *>realloc_chk(
1209 self.peers, self.num_peers * sizeof(rbd_mirror_peer_t))
1210 with nogil:
1211 ret = rbd_mirror_peer_list(_ioctx, self.peers, &self.num_peers)
1212 if ret < 0:
1213 if ret == -errno.ERANGE:
1214 continue
1215 self.num_peers = 0
1216 raise make_ex(ret, 'error listing peers')
1217 break
1218
1219 def __iter__(self):
1220 for i in range(self.num_peers):
1221 yield {
1222 'uuid' : decode_cstr(self.peers[i].uuid),
1223 'cluster_name' : decode_cstr(self.peers[i].cluster_name),
1224 'client_name' : decode_cstr(self.peers[i].client_name),
1225 }
1226
1227 def __dealloc__(self):
1228 if self.peers:
1229 rbd_mirror_peer_list_cleanup(self.peers, self.num_peers)
1230 free(self.peers)
1231
1232 cdef class MirrorImageStatusIterator(object):
1233 """
1234 Iterator over mirror image status for a pool.
1235
1236 Yields a dictionary containing mirror status of an image.
1237
1238 Keys are:
1239
1240 * ``name`` (str) - mirror image name
1241
1242 * `info` (dict) - mirror image info
1243
1244 * `state` (int) - mirror state
1245
1246 * `description` (str) - status description
1247
1248 * `last_update` (datetime) - last status update time
1249
1250 * ``up`` (bool) - is mirroring agent up
1251 """
1252
1253 cdef:
1254 rados_ioctx_t ioctx
1255 size_t max_read
1256 char *last_read
1257 char **image_ids
1258 rbd_mirror_image_status_t *images
1259 size_t size
1260
1261 def __init__(self, ioctx):
1262 self.ioctx = convert_ioctx(ioctx)
1263 self.max_read = 1024
1264 self.last_read = strdup("")
1265 self.image_ids = <char **>realloc_chk(NULL,
1266 sizeof(char *) * self.max_read)
1267 self.images = <rbd_mirror_image_status_t *>realloc_chk(NULL,
1268 sizeof(rbd_mirror_image_status_t) * self.max_read)
1269 self.size = 0
1270 self.get_next_chunk()
1271
1272 def __iter__(self):
1273 while self.size > 0:
1274 for i in range(self.size):
1275 yield {
1276 'name' : decode_cstr(self.images[i].name),
1277 'info' : {
1278 'global_id' : decode_cstr(self.images[i].info.global_id),
1279 'state' : self.images[i].info.state,
1280 },
1281 'state' : self.images[i].state,
1282 'description' : decode_cstr(self.images[i].description),
1283 'last_update' : datetime.fromtimestamp(self.images[i].last_update),
1284 'up' : self.images[i].up,
1285 }
1286 if self.size < self.max_read:
1287 break
1288 self.get_next_chunk()
1289
1290 def __dealloc__(self):
1291 rbd_mirror_image_status_list_cleanup(self.image_ids, self.images,
1292 self.size)
1293 if self.last_read:
1294 free(self.last_read)
1295 if self.image_ids:
1296 free(self.image_ids)
1297 if self.images:
1298 free(self.images)
1299
1300 def get_next_chunk(self):
1301 if self.size > 0:
1302 rbd_mirror_image_status_list_cleanup(self.image_ids, self.images,
1303 self.size)
1304 self.size = 0
1305 with nogil:
1306 ret = rbd_mirror_image_status_list(self.ioctx, self.last_read,
1307 self.max_read, self.image_ids,
1308 self.images, &self.size)
1309 if ret < 0:
1310 raise make_ex(ret, 'error listing mirror images status')
1311 if self.size > 0:
1312 free(self.last_read)
1313 last_read = decode_cstr(self.image_ids[self.size - 1])
1314 self.last_read = strdup(last_read)
1315 else:
1316 free(self.last_read)
1317 self.last_read = strdup("")
1318
1319 cdef int diff_iterate_cb(uint64_t offset, size_t length, int write, void *cb) \
1320 except? -9000 with gil:
1321 # Make sure that if we wound up with an exception from a previous callback,
1322 # we stop calling back (just in case librbd ever fails to bail out on the
1323 # first negative return, as older versions did)
1324 if exc.PyErr_Occurred():
1325 return -9000
1326 ret = (<object>cb)(offset, length, bool(write))
1327 if ret is None:
1328 return 0
1329 return ret
1330
1331
1332 cdef class Image(object):
1333 """
1334 This class represents an RBD image. It is used to perform I/O on
1335 the image and interact with snapshots.
1336
1337 **Note**: Any method of this class may raise :class:`ImageNotFound`
1338 if the image has been deleted.
1339 """
1340 cdef rbd_image_t image
1341 cdef bint closed
1342 cdef object name
1343 cdef object ioctx
1344 cdef rados_ioctx_t _ioctx
1345
1346 def __init__(self, ioctx, name, snapshot=None, read_only=False):
1347 """
1348 Open the image at the given snapshot.
1349 If a snapshot is specified, the image will be read-only, unless
1350 :func:`Image.set_snap` is called later.
1351
1352 If read-only mode is used, metadata for the :class:`Image`
1353 object (such as which snapshots exist) may become obsolete. See
1354 the C api for more details.
1355
1356 To clean up from opening the image, :func:`Image.close` should
1357 be called. For ease of use, this is done automatically when
1358 an :class:`Image` is used as a context manager (see :pep:`343`).
1359
1360 :param ioctx: determines which RADOS pool the image is in
1361 :type ioctx: :class:`rados.Ioctx`
1362 :param name: the name of the image
1363 :type name: str
1364 :param snapshot: which snapshot to read from
1365 :type snaphshot: str
1366 :param read_only: whether to open the image in read-only mode
1367 :type read_only: bool
1368 """
1369 name = cstr(name, 'name')
1370 snapshot = cstr(snapshot, 'snapshot', opt=True)
1371 self.closed = True
1372 self.name = name
1373 # Keep around a reference to the ioctx, so it won't get deleted
1374 self.ioctx = ioctx
1375 cdef:
1376 rados_ioctx_t _ioctx = convert_ioctx(ioctx)
1377 char *_name = name
1378 char *_snapshot = opt_str(snapshot)
1379 if read_only:
1380 with nogil:
1381 ret = rbd_open_read_only(_ioctx, _name, &self.image, _snapshot)
1382 else:
1383 with nogil:
1384 ret = rbd_open(_ioctx, _name, &self.image, _snapshot)
1385 if ret != 0:
1386 raise make_ex(ret, 'error opening image %s at snapshot %s' % (name, snapshot))
1387 self.closed = False
1388
1389 def __enter__(self):
1390 return self
1391
1392 def __exit__(self, type_, value, traceback):
1393 """
1394 Closes the image. See :func:`close`
1395 """
1396 self.close()
1397 return False
1398
1399 def __get_completion(self, oncomplete):
1400 """
1401 Constructs a completion to use with asynchronous operations
1402
1403 :param oncomplete: callback for the completion
1404
1405 :raises: :class:`Error`
1406 :returns: completion object
1407 """
1408
1409 completion_obj = Completion(self, oncomplete)
1410
1411 cdef:
1412 rbd_completion_t completion
1413 PyObject* p_completion_obj= <PyObject*>completion_obj
1414
1415 with nogil:
1416 ret = rbd_aio_create_completion(p_completion_obj, __aio_complete_cb,
1417 &completion)
1418 if ret < 0:
1419 raise make_ex(ret, "error getting a completion")
1420
1421 completion_obj.rbd_comp = completion
1422 return completion_obj
1423
1424 def close(self):
1425 """
1426 Release the resources used by this image object.
1427
1428 After this is called, this object should not be used.
1429 """
1430 if not self.closed:
1431 self.closed = True
1432 with nogil:
1433 ret = rbd_close(self.image)
1434 if ret < 0:
1435 raise make_ex(ret, 'error while closing image %s' % (
1436 self.name,))
1437
1438 def __dealloc__(self):
1439 self.close()
1440
1441 def __repr__(self):
1442 return "rbd.Image(ioctx, %r)" % self.name
1443
1444 def resize(self, size):
1445 """
1446 Change the size of the image.
1447
1448 :param size: the new size of the image
1449 :type size: int
1450 """
1451 cdef uint64_t _size = size
1452 with nogil:
1453 ret = rbd_resize(self.image, _size)
1454 if ret < 0:
1455 raise make_ex(ret, 'error resizing image %s' % (self.name,))
1456
1457 def stat(self):
1458 """
1459 Get information about the image. Currently parent pool and
1460 parent name are always -1 and ''.
1461
1462 :returns: dict - contains the following keys:
1463
1464 * ``size`` (int) - the size of the image in bytes
1465
1466 * ``obj_size`` (int) - the size of each object that comprises the
1467 image
1468
1469 * ``num_objs`` (int) - the number of objects in the image
1470
1471 * ``order`` (int) - log_2(object_size)
1472
1473 * ``block_name_prefix`` (str) - the prefix of the RADOS objects used
1474 to store the image
1475
1476 * ``parent_pool`` (int) - deprecated
1477
1478 * ``parent_name`` (str) - deprecated
1479
1480 See also :meth:`format` and :meth:`features`.
1481
1482 """
1483 cdef rbd_image_info_t info
1484 with nogil:
1485 ret = rbd_stat(self.image, &info, sizeof(info))
1486 if ret != 0:
1487 raise make_ex(ret, 'error getting info for image %s' % (self.name,))
1488 return {
1489 'size' : info.size,
1490 'obj_size' : info.obj_size,
1491 'num_objs' : info.num_objs,
1492 'order' : info.order,
1493 'block_name_prefix' : decode_cstr(info.block_name_prefix),
1494 'parent_pool' : info.parent_pool,
1495 'parent_name' : info.parent_name
1496 }
1497
1498 def id(self):
1499 """
1500 Get the RBD v2 internal image id
1501
1502 :returns: str - image id
1503 """
1504 cdef:
1505 int ret = -errno.ERANGE
1506 size_t size = 32
1507 char *image_id = NULL
1508 try:
1509 while ret == -errno.ERANGE and size <= 4096:
1510 image_id = <char *>realloc_chk(image_id, size)
1511 with nogil:
1512 ret = rbd_get_id(self.image, image_id, size)
1513 if ret == -errno.ERANGE:
1514 size *= 2
1515
1516 if ret != 0:
1517 raise make_ex(ret, 'error getting id for image %s' % (self.name,))
1518 return decode_cstr(image_id)
1519 finally:
1520 free(image_id)
1521
1522 def block_name_prefix(self):
1523 """
1524 Get the RBD block name prefix
1525
1526 :returns: str - block name prefix
1527 """
1528 cdef:
1529 int ret = -errno.ERANGE
1530 size_t size = 32
1531 char *prefix = NULL
1532 try:
1533 while ret == -errno.ERANGE and size <= 4096:
1534 prefix = <char *>realloc_chk(prefix, size)
1535 with nogil:
1536 ret = rbd_get_block_name_prefix(self.image, prefix, size)
1537 if ret == -errno.ERANGE:
1538 size *= 2
1539
1540 if ret != 0:
1541 raise make_ex(ret, 'error getting block name prefix for image %s' % (self.name,))
1542 return decode_cstr(prefix)
1543 finally:
1544 free(prefix)
1545
1546 def parent_info(self):
1547 """
1548 Get information about a cloned image's parent (if any)
1549
1550 :returns: tuple - ``(pool name, image name, snapshot name)`` components
1551 of the parent image
1552 :raises: :class:`ImageNotFound` if the image doesn't have a parent
1553 """
1554 cdef:
1555 int ret = -errno.ERANGE
1556 size_t size = 8
1557 char *pool = NULL
1558 char *name = NULL
1559 char *snapname = NULL
1560 try:
1561 while ret == -errno.ERANGE and size <= 4096:
1562 pool = <char *>realloc_chk(pool, size)
1563 name = <char *>realloc_chk(name, size)
1564 snapname = <char *>realloc_chk(snapname, size)
1565 with nogil:
1566 ret = rbd_get_parent_info2(self.image, pool, size, name,
1567 size, NULL, 0, snapname, size)
1568 if ret == -errno.ERANGE:
1569 size *= 2
1570
1571 if ret != 0:
1572 raise make_ex(ret, 'error getting parent info for image %s' % (self.name,))
1573 return (decode_cstr(pool), decode_cstr(name), decode_cstr(snapname))
1574 finally:
1575 free(pool)
1576 free(name)
1577 free(snapname)
1578
1579 def parent_id(self):
1580 """
1581 Get image id of a cloned image's parent (if any)
1582
1583 :returns: str - the parent id
1584 :raises: :class:`ImageNotFound` if the image doesn't have a parent
1585 """
1586 cdef:
1587 int ret = -errno.ERANGE
1588 size_t size = 32
1589 char *parent_id = NULL
1590 try:
1591 while ret == -errno.ERANGE and size <= 4096:
1592 parent_id = <char *>realloc_chk(parent_id, size)
1593 with nogil:
1594 ret = rbd_get_parent_info2(self.image, NULL, 0, NULL, 0,
1595 parent_id, size, NULL, 0)
1596 if ret == -errno.ERANGE:
1597 size *= 2
1598
1599 if ret != 0:
1600 raise make_ex(ret, 'error getting parent id for image %s' % (self.name,))
1601 return decode_cstr(parent_id)
1602 finally:
1603 free(parent_id)
1604
1605 def old_format(self):
1606 """
1607 Find out whether the image uses the old RBD format.
1608
1609 :returns: bool - whether the image uses the old RBD format
1610 """
1611 cdef uint8_t old
1612 with nogil:
1613 ret = rbd_get_old_format(self.image, &old)
1614 if ret != 0:
1615 raise make_ex(ret, 'error getting old_format for image %s' % (self.name))
1616 return old != 0
1617
1618 def size(self):
1619 """
1620 Get the size of the image. If open to a snapshot, returns the
1621 size of that snapshot.
1622
1623 :returns: the size of the image in bytes
1624 """
1625 cdef uint64_t image_size
1626 with nogil:
1627 ret = rbd_get_size(self.image, &image_size)
1628 if ret != 0:
1629 raise make_ex(ret, 'error getting size for image %s' % (self.name))
1630 return image_size
1631
1632 def features(self):
1633 """
1634 Gets the features bitmask of the image.
1635
1636 :returns: int - the features bitmask of the image
1637 """
1638 cdef uint64_t features
1639 with nogil:
1640 ret = rbd_get_features(self.image, &features)
1641 if ret != 0:
1642 raise make_ex(ret, 'error getting features for image %s' % (self.name))
1643 return features
1644
1645 def update_features(self, features, enabled):
1646 """
1647 Updates the features bitmask of the image by enabling/disabling
1648 a single feature. The feature must support the ability to be
1649 dynamically enabled/disabled.
1650
1651 :param features: feature bitmask to enable/disable
1652 :type features: int
1653 :param enabled: whether to enable/disable the feature
1654 :type enabled: bool
1655 :raises: :class:`InvalidArgument`
1656 """
1657 cdef:
1658 uint64_t _features = features
1659 uint8_t _enabled = bool(enabled)
1660 with nogil:
1661 ret = rbd_update_features(self.image, _features, _enabled)
1662 if ret != 0:
1663 raise make_ex(ret, 'error updating features for image %s' %
1664 (self.name))
1665
1666 def overlap(self):
1667 """
1668 Gets the number of overlapping bytes between the image and its parent
1669 image. If open to a snapshot, returns the overlap between the snapshot
1670 and the parent image.
1671
1672 :returns: int - the overlap in bytes
1673 :raises: :class:`ImageNotFound` if the image doesn't have a parent
1674 """
1675 cdef uint64_t overlap
1676 with nogil:
1677 ret = rbd_get_overlap(self.image, &overlap)
1678 if ret != 0:
1679 raise make_ex(ret, 'error getting overlap for image %s' % (self.name))
1680 return overlap
1681
1682 def flags(self):
1683 """
1684 Gets the flags bitmask of the image.
1685
1686 :returns: int - the flags bitmask of the image
1687 """
1688 cdef uint64_t flags
1689 with nogil:
1690 ret = rbd_get_flags(self.image, &flags)
1691 if ret != 0:
1692 raise make_ex(ret, 'error getting flags for image %s' % (self.name))
1693 return flags
1694
1695 def is_exclusive_lock_owner(self):
1696 """
1697 Gets the status of the image exclusive lock.
1698
1699 :returns: bool - true if the image is exclusively locked
1700 """
1701 cdef int owner
1702 with nogil:
1703 ret = rbd_is_exclusive_lock_owner(self.image, &owner)
1704 if ret != 0:
1705 raise make_ex(ret, 'error getting lock status for image %s' % (self.name))
1706 return owner == 1
1707
1708 def copy(self, dest_ioctx, dest_name, features=None, order=None,
1709 stripe_unit=None, stripe_count=None, data_pool=None):
1710 """
1711 Copy the image to another location.
1712
1713 :param dest_ioctx: determines which pool to copy into
1714 :type dest_ioctx: :class:`rados.Ioctx`
1715 :param dest_name: the name of the copy
1716 :type dest_name: str
1717 :param features: bitmask of features to enable; if set, must include layering
1718 :type features: int
1719 :param order: the image is split into (2**order) byte objects
1720 :type order: int
1721 :param stripe_unit: stripe unit in bytes (default None to let librbd decide)
1722 :type stripe_unit: int
1723 :param stripe_count: objects to stripe over before looping
1724 :type stripe_count: int
1725 :param data_pool: optional separate pool for data blocks
1726 :type data_pool: str
1727 :raises: :class:`TypeError`
1728 :raises: :class:`InvalidArgument`
1729 :raises: :class:`ImageExists`
1730 :raises: :class:`FunctionNotSupported`
1731 :raises: :class:`ArgumentOutOfRange`
1732 """
1733 dest_name = cstr(dest_name, 'dest_name')
1734 cdef:
1735 rados_ioctx_t _dest_ioctx = convert_ioctx(dest_ioctx)
1736 char *_dest_name = dest_name
1737 rbd_image_options_t opts
1738
1739 rbd_image_options_create(&opts)
1740 try:
1741 if features is not None:
1742 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FEATURES,
1743 features)
1744 if order is not None:
1745 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER,
1746 order)
1747 if stripe_unit is not None:
1748 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT,
1749 stripe_unit)
1750 if stripe_count is not None:
1751 rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_COUNT,
1752 stripe_count)
1753 if data_pool is not None:
1754 rbd_image_options_set_string(opts, RBD_IMAGE_OPTION_DATA_POOL,
1755 data_pool)
1756 with nogil:
1757 ret = rbd_copy3(self.image, _dest_ioctx, _dest_name, opts)
1758 finally:
1759 rbd_image_options_destroy(opts)
1760 if ret < 0:
1761 raise make_ex(ret, 'error copying image %s to %s' % (self.name, dest_name))
1762
1763 def list_snaps(self):
1764 """
1765 Iterate over the snapshots of an image.
1766
1767 :returns: :class:`SnapIterator`
1768 """
1769 return SnapIterator(self)
1770
1771 def create_snap(self, name):
1772 """
1773 Create a snapshot of the image.
1774
1775 :param name: the name of the snapshot
1776 :type name: str
1777 :raises: :class:`ImageExists`
1778 """
1779 name = cstr(name, 'name')
1780 cdef char *_name = name
1781 with nogil:
1782 ret = rbd_snap_create(self.image, _name)
1783 if ret != 0:
1784 raise make_ex(ret, 'error creating snapshot %s from %s' % (name, self.name))
1785
1786 def rename_snap(self, srcname, dstname):
1787 """
1788 rename a snapshot of the image.
1789
1790 :param srcname: the src name of the snapshot
1791 :type srcname: str
1792 :param dstname: the dst name of the snapshot
1793 :type dstname: str
1794 :raises: :class:`ImageExists`
1795 """
1796 srcname = cstr(srcname, 'srcname')
1797 dstname = cstr(dstname, 'dstname')
1798 cdef:
1799 char *_srcname = srcname
1800 char *_dstname = dstname
1801 with nogil:
1802 ret = rbd_snap_rename(self.image, _srcname, _dstname)
1803 if ret != 0:
1804 raise make_ex(ret, 'error renaming snapshot of %s from %s to %s' % (self.name, srcname, dstname))
1805
1806 def remove_snap(self, name):
1807 """
1808 Delete a snapshot of the image.
1809
1810 :param name: the name of the snapshot
1811 :type name: str
1812 :raises: :class:`IOError`, :class:`ImageBusy`
1813 """
1814 name = cstr(name, 'name')
1815 cdef char *_name = name
1816 with nogil:
1817 ret = rbd_snap_remove(self.image, _name)
1818 if ret != 0:
1819 raise make_ex(ret, 'error removing snapshot %s from %s' % (name, self.name))
1820
1821 def remove_snap2(self, name, flags):
1822 """
1823 Delete a snapshot of the image.
1824
1825 :param name: the name of the snapshot
1826 :param flags: the flags for removal
1827 :type name: str
1828 :raises: :class:`IOError`, :class:`ImageBusy`
1829 """
1830 name = cstr(name, 'name')
1831 cdef:
1832 char *_name = name
1833 uint32_t _flags = flags
1834 librbd_progress_fn_t prog_cb = &no_op_progress_callback
1835 with nogil:
1836 ret = rbd_snap_remove2(self.image, _name, _flags, prog_cb, NULL)
1837 if ret != 0:
1838 raise make_ex(ret, 'error removing snapshot %s from %s with flags %llx' % (name, self.name, flags))
1839
1840 def rollback_to_snap(self, name):
1841 """
1842 Revert the image to its contents at a snapshot. This is a
1843 potentially expensive operation, since it rolls back each
1844 object individually.
1845
1846 :param name: the snapshot to rollback to
1847 :type name: str
1848 :raises: :class:`IOError`
1849 """
1850 name = cstr(name, 'name')
1851 cdef char *_name = name
1852 with nogil:
1853 ret = rbd_snap_rollback(self.image, _name)
1854 if ret != 0:
1855 raise make_ex(ret, 'error rolling back image %s to snapshot %s' % (self.name, name))
1856
1857 def protect_snap(self, name):
1858 """
1859 Mark a snapshot as protected. This means it can't be deleted
1860 until it is unprotected.
1861
1862 :param name: the snapshot to protect
1863 :type name: str
1864 :raises: :class:`IOError`, :class:`ImageNotFound`
1865 """
1866 name = cstr(name, 'name')
1867 cdef char *_name = name
1868 with nogil:
1869 ret = rbd_snap_protect(self.image, _name)
1870 if ret != 0:
1871 raise make_ex(ret, 'error protecting snapshot %s@%s' % (self.name, name))
1872
1873 def unprotect_snap(self, name):
1874 """
1875 Mark a snapshot unprotected. This allows it to be deleted if
1876 it was protected.
1877
1878 :param name: the snapshot to unprotect
1879 :type name: str
1880 :raises: :class:`IOError`, :class:`ImageNotFound`
1881 """
1882 name = cstr(name, 'name')
1883 cdef char *_name = name
1884 with nogil:
1885 ret = rbd_snap_unprotect(self.image, _name)
1886 if ret != 0:
1887 raise make_ex(ret, 'error unprotecting snapshot %s@%s' % (self.name, name))
1888
1889 def is_protected_snap(self, name):
1890 """
1891 Find out whether a snapshot is protected from deletion.
1892
1893 :param name: the snapshot to check
1894 :type name: str
1895 :returns: bool - whether the snapshot is protected
1896 :raises: :class:`IOError`, :class:`ImageNotFound`
1897 """
1898 name = cstr(name, 'name')
1899 cdef:
1900 char *_name = name
1901 int is_protected
1902 with nogil:
1903 ret = rbd_snap_is_protected(self.image, _name, &is_protected)
1904 if ret != 0:
1905 raise make_ex(ret, 'error checking if snapshot %s@%s is protected' % (self.name, name))
1906 return is_protected == 1
1907
1908 def get_snap_limit(self):
1909 """
1910 Get the snapshot limit for an image.
1911 """
1912
1913 cdef:
1914 uint64_t limit
1915 with nogil:
1916 ret = rbd_snap_get_limit(self.image, &limit)
1917 if ret != 0:
1918 raise make_ex(ret, 'error getting snapshot limit for %s' % self.name)
1919 return limit
1920
1921 def set_snap_limit(self, limit):
1922 """
1923 Set the snapshot limit for an image.
1924
1925 :param limit: the new limit to set
1926 """
1927
1928 cdef:
1929 uint64_t _limit = limit
1930 with nogil:
1931 ret = rbd_snap_set_limit(self.image, _limit)
1932 if ret != 0:
1933 raise make_ex(ret, 'error setting snapshot limit for %s' % self.name)
1934 return ret
1935
1936 def get_snap_timestamp(self, snap_id):
1937 """
1938 Get the snapshot timestamp for an image.
1939 :param snap_id: the snapshot id of a snap shot
1940 """
1941 cdef:
1942 timespec timestamp
1943 uint64_t _snap_id = snap_id
1944 with nogil:
1945 ret = rbd_snap_get_timestamp(self.image, _snap_id, &timestamp)
1946 if ret != 0:
1947 raise make_ex(ret, 'error getting snapshot timestamp for image: %s, snap_id: %d' % (self.name, snap_id))
1948 return datetime.fromtimestamp(timestamp.tv_sec)
1949
1950 def remove_snap_limit(self):
1951 """
1952 Remove the snapshot limit for an image, essentially setting
1953 the limit to the maximum size allowed by the implementation.
1954 """
1955 with nogil:
1956 ret = rbd_snap_set_limit(self.image, UINT64_MAX)
1957 if ret != 0:
1958 raise make_ex(ret, 'error removing snapshot limit for %s' % self.name)
1959 return ret
1960
1961 def set_snap(self, name):
1962 """
1963 Set the snapshot to read from. Writes will raise ReadOnlyImage
1964 while a snapshot is set. Pass None to unset the snapshot
1965 (reads come from the current image) , and allow writing again.
1966
1967 :param name: the snapshot to read from, or None to unset the snapshot
1968 :type name: str or None
1969 """
1970 name = cstr(name, 'name', opt=True)
1971 cdef char *_name = opt_str(name)
1972 with nogil:
1973 ret = rbd_snap_set(self.image, _name)
1974 if ret != 0:
1975 raise make_ex(ret, 'error setting image %s to snapshot %s' % (self.name, name))
1976
1977 def read(self, offset, length, fadvise_flags=0):
1978 """
1979 Read data from the image. Raises :class:`InvalidArgument` if
1980 part of the range specified is outside the image.
1981
1982 :param offset: the offset to start reading at
1983 :type offset: int
1984 :param length: how many bytes to read
1985 :type length: int
1986 :param fadvise_flags: fadvise flags for this read
1987 :type fadvise_flags: int
1988 :returns: str - the data read
1989 :raises: :class:`InvalidArgument`, :class:`IOError`
1990 """
1991
1992 # This usage of the Python API allows us to construct a string
1993 # that librbd directly reads into, avoiding an extra copy. Although
1994 # strings are normally immutable, this usage is explicitly supported
1995 # for freshly created string objects.
1996 cdef:
1997 char *ret_buf
1998 uint64_t _offset = offset
1999 size_t _length = length
2000 int _fadvise_flags = fadvise_flags
2001 PyObject* ret_s = NULL
2002 ret_s = PyBytes_FromStringAndSize(NULL, length)
2003 try:
2004 ret_buf = PyBytes_AsString(ret_s)
2005 with nogil:
2006 ret = rbd_read2(self.image, _offset, _length, ret_buf,
2007 _fadvise_flags)
2008 if ret < 0:
2009 raise make_ex(ret, 'error reading %s %ld~%ld' % (self.name, offset, length))
2010
2011 if ret != <ssize_t>length:
2012 _PyBytes_Resize(&ret_s, ret)
2013
2014 return <object>ret_s
2015 finally:
2016 # We DECREF unconditionally: the cast to object above will have
2017 # INCREFed if necessary. This also takes care of exceptions,
2018 # including if _PyString_Resize fails (that will free the string
2019 # itself and set ret_s to NULL, hence XDECREF).
2020 ref.Py_XDECREF(ret_s)
2021
2022 def diff_iterate(self, offset, length, from_snapshot, iterate_cb,
2023 include_parent = True, whole_object = False):
2024 """
2025 Iterate over the changed extents of an image.
2026
2027 This will call iterate_cb with three arguments:
2028
2029 (offset, length, exists)
2030
2031 where the changed extent starts at offset bytes, continues for
2032 length bytes, and is full of data (if exists is True) or zeroes
2033 (if exists is False).
2034
2035 If from_snapshot is None, it is interpreted as the beginning
2036 of time and this generates all allocated extents.
2037
2038 The end version is whatever is currently selected (via set_snap)
2039 for the image.
2040
2041 iterate_cb may raise an exception, which will abort the diff and will be
2042 propagated to the caller.
2043
2044 Raises :class:`InvalidArgument` if from_snapshot is after
2045 the currently set snapshot.
2046
2047 Raises :class:`ImageNotFound` if from_snapshot is not the name
2048 of a snapshot of the image.
2049
2050 :param offset: start offset in bytes
2051 :type offset: int
2052 :param length: size of region to report on, in bytes
2053 :type length: int
2054 :param from_snapshot: starting snapshot name, or None
2055 :type from_snapshot: str or None
2056 :param iterate_cb: function to call for each extent
2057 :type iterate_cb: function acception arguments for offset,
2058 length, and exists
2059 :param include_parent: True if full history diff should include parent
2060 :type include_parent: bool
2061 :param whole_object: True if diff extents should cover whole object
2062 :type whole_object: bool
2063 :raises: :class:`InvalidArgument`, :class:`IOError`,
2064 :class:`ImageNotFound`
2065 """
2066 from_snapshot = cstr(from_snapshot, 'from_snapshot', opt=True)
2067 cdef:
2068 char *_from_snapshot = opt_str(from_snapshot)
2069 uint64_t _offset = offset, _length = length
2070 uint8_t _include_parent = include_parent
2071 uint8_t _whole_object = whole_object
2072 with nogil:
2073 ret = rbd_diff_iterate2(self.image, _from_snapshot, _offset,
2074 _length, _include_parent, _whole_object,
2075 &diff_iterate_cb, <void *>iterate_cb)
2076 if ret < 0:
2077 msg = 'error generating diff from snapshot %s' % from_snapshot
2078 raise make_ex(ret, msg)
2079
2080 def write(self, data, offset, fadvise_flags=0):
2081 """
2082 Write data to the image. Raises :class:`InvalidArgument` if
2083 part of the write would fall outside the image.
2084
2085 :param data: the data to be written
2086 :type data: bytes
2087 :param offset: where to start writing data
2088 :type offset: int
2089 :param fadvise_flags: fadvise flags for this write
2090 :type fadvise_flags: int
2091 :returns: int - the number of bytes written
2092 :raises: :class:`IncompleteWriteError`, :class:`LogicError`,
2093 :class:`InvalidArgument`, :class:`IOError`
2094 """
2095 if not isinstance(data, bytes):
2096 raise TypeError('data must be a byte string')
2097 cdef:
2098 uint64_t _offset = offset, length = len(data)
2099 char *_data = data
2100 int _fadvise_flags = fadvise_flags
2101 with nogil:
2102 ret = rbd_write2(self.image, _offset, length, _data, _fadvise_flags)
2103
2104 if ret == <ssize_t>length:
2105 return ret
2106 elif ret < 0:
2107 raise make_ex(ret, "error writing to %s" % (self.name,))
2108 elif ret < <ssize_t>length:
2109 raise IncompleteWriteError("Wrote only %ld out of %ld bytes" % (ret, length))
2110 else:
2111 raise LogicError("logic error: rbd_write(%s) \
2112 returned %d, but %d was the maximum number of bytes it could have \
2113 written." % (self.name, ret, length))
2114
2115 def discard(self, offset, length):
2116 """
2117 Trim the range from the image. It will be logically filled
2118 with zeroes.
2119 """
2120 cdef uint64_t _offset = offset, _length = length
2121 with nogil:
2122 ret = rbd_discard(self.image, _offset, _length)
2123 if ret < 0:
2124 msg = 'error discarding region %d~%d' % (offset, length)
2125 raise make_ex(ret, msg)
2126
2127 def flush(self):
2128 """
2129 Block until all writes are fully flushed if caching is enabled.
2130 """
2131 with nogil:
2132 ret = rbd_flush(self.image)
2133 if ret < 0:
2134 raise make_ex(ret, 'error flushing image')
2135
2136 def invalidate_cache(self):
2137 """
2138 Drop any cached data for the image.
2139 """
2140 with nogil:
2141 ret = rbd_invalidate_cache(self.image)
2142 if ret < 0:
2143 raise make_ex(ret, 'error invalidating cache')
2144
2145 def stripe_unit(self):
2146 """
2147 Returns the stripe unit used for the image.
2148 """
2149 cdef uint64_t stripe_unit
2150 with nogil:
2151 ret = rbd_get_stripe_unit(self.image, &stripe_unit)
2152 if ret != 0:
2153 raise make_ex(ret, 'error getting stripe unit for image %s' % (self.name))
2154 return stripe_unit
2155
2156 def stripe_count(self):
2157 """
2158 Returns the stripe count used for the image.
2159 """
2160 cdef uint64_t stripe_count
2161 with nogil:
2162 ret = rbd_get_stripe_count(self.image, &stripe_count)
2163 if ret != 0:
2164 raise make_ex(ret, 'error getting stripe count for image %s' % (self.name))
2165 return stripe_count
2166
2167 def flatten(self):
2168 """
2169 Flatten clone image (copy all blocks from parent to child)
2170 """
2171 with nogil:
2172 ret = rbd_flatten(self.image)
2173 if ret < 0:
2174 raise make_ex(ret, "error flattening %s" % self.name)
2175
2176 def rebuild_object_map(self):
2177 """
2178 Rebuilds the object map for the image HEAD or currently set snapshot
2179 """
2180 cdef librbd_progress_fn_t prog_cb = &no_op_progress_callback
2181 with nogil:
2182 ret = rbd_rebuild_object_map(self.image, prog_cb, NULL)
2183 if ret < 0:
2184 raise make_ex(ret, "error rebuilding object map %s" % self.name)
2185
2186 def list_children(self):
2187 """
2188 List children of the currently set snapshot (set via set_snap()).
2189
2190 :returns: list - a list of (pool name, image name) tuples
2191 """
2192 cdef:
2193 size_t pools_size = 512, images_size = 512
2194 char *c_pools = NULL
2195 char *c_images = NULL
2196 try:
2197 while True:
2198 c_pools = <char *>realloc_chk(c_pools, pools_size)
2199 c_images = <char *>realloc_chk(c_images, images_size)
2200 with nogil:
2201 ret = rbd_list_children(self.image, c_pools, &pools_size,
2202 c_images, &images_size)
2203 if ret >= 0:
2204 break
2205 elif ret != -errno.ERANGE:
2206 raise make_ex(ret, 'error listing images')
2207 if ret == 0:
2208 return []
2209 pools = map(decode_cstr, c_pools[:pools_size - 1].split(b'\0'))
2210 images = map(decode_cstr, c_images[:images_size - 1].split(b'\0'))
2211 return list(zip(pools, images))
2212 finally:
2213 free(c_pools)
2214 free(c_images)
2215
2216 def list_lockers(self):
2217 """
2218 List clients that have locked the image and information
2219 about the lock.
2220
2221 :returns: dict - contains the following keys:
2222
2223 * ``tag`` - the tag associated with the lock (every
2224 additional locker must use the same tag)
2225 * ``exclusive`` - boolean indicating whether the
2226 lock is exclusive or shared
2227 * ``lockers`` - a list of (client, cookie, address)
2228 tuples
2229 """
2230 cdef:
2231 size_t clients_size = 512, cookies_size = 512
2232 size_t addrs_size = 512, tag_size = 512
2233 int exclusive = 0
2234 char *c_clients = NULL
2235 char *c_cookies = NULL
2236 char *c_addrs = NULL
2237 char *c_tag = NULL
2238
2239 try:
2240 while True:
2241 c_clients = <char *>realloc_chk(c_clients, clients_size)
2242 c_cookies = <char *>realloc_chk(c_cookies, cookies_size)
2243 c_addrs = <char *>realloc_chk(c_addrs, addrs_size)
2244 c_tag = <char *>realloc_chk(c_tag, tag_size)
2245 with nogil:
2246 ret = rbd_list_lockers(self.image, &exclusive,
2247 c_tag, &tag_size,
2248 c_clients, &clients_size,
2249 c_cookies, &cookies_size,
2250 c_addrs, &addrs_size)
2251 if ret >= 0:
2252 break
2253 elif ret != -errno.ERANGE:
2254 raise make_ex(ret, 'error listing images')
2255 if ret == 0:
2256 return []
2257 clients = map(decode_cstr, c_clients[:clients_size - 1].split(b'\0'))
2258 cookies = map(decode_cstr, c_cookies[:cookies_size - 1].split(b'\0'))
2259 addrs = map(decode_cstr, c_addrs[:addrs_size - 1].split(b'\0'))
2260 return {
2261 'tag' : decode_cstr(c_tag),
2262 'exclusive' : exclusive == 1,
2263 'lockers' : list(zip(clients, cookies, addrs)),
2264 }
2265 finally:
2266 free(c_clients)
2267 free(c_cookies)
2268 free(c_addrs)
2269 free(c_tag)
2270
2271 def lock_acquire(self, lock_mode):
2272 """
2273 Acquire a managed lock on the image.
2274
2275 :param lock_mode: lock mode to set
2276 :type lock_mode: int
2277 :raises: :class:`ImageBusy` if the lock could not be acquired
2278 """
2279 cdef:
2280 rbd_lock_mode_t _lock_mode = lock_mode
2281 with nogil:
2282 ret = rbd_lock_acquire(self.image, _lock_mode)
2283 if ret < 0:
2284 raise make_ex(ret, 'error acquiring lock on image')
2285
2286 def lock_release(self):
2287 """
2288 Release a managed lock on the image that was previously acquired.
2289 """
2290 with nogil:
2291 ret = rbd_lock_release(self.image)
2292 if ret < 0:
2293 raise make_ex(ret, 'error releasing lock on image')
2294
2295 def lock_get_owners(self):
2296 """
2297 Iterate over the lock owners of an image.
2298
2299 :returns: :class:`LockOwnerIterator`
2300 """
2301 return LockOwnerIterator(self)
2302
2303 def lock_break(self, lock_mode, lock_owner):
2304 """
2305 Break the image lock held by a another client.
2306
2307 :param lock_owner: the owner of the lock to break
2308 :type lock_owner: str
2309 """
2310 lock_owner = cstr(lock_owner, 'lock_owner')
2311 cdef:
2312 rbd_lock_mode_t _lock_mode = lock_mode
2313 char *_lock_owner = lock_owner
2314 with nogil:
2315 ret = rbd_lock_break(self.image, _lock_mode, _lock_owner)
2316 if ret < 0:
2317 raise make_ex(ret, 'error breaking lock on image')
2318
2319 def lock_exclusive(self, cookie):
2320 """
2321 Take an exclusive lock on the image.
2322
2323 :raises: :class:`ImageBusy` if a different client or cookie locked it
2324 :class:`ImageExists` if the same client and cookie locked it
2325 """
2326 cookie = cstr(cookie, 'cookie')
2327 cdef char *_cookie = cookie
2328 with nogil:
2329 ret = rbd_lock_exclusive(self.image, _cookie)
2330 if ret < 0:
2331 raise make_ex(ret, 'error acquiring exclusive lock on image')
2332
2333 def lock_shared(self, cookie, tag):
2334 """
2335 Take a shared lock on the image. The tag must match
2336 that of the existing lockers, if any.
2337
2338 :raises: :class:`ImageBusy` if a different client or cookie locked it
2339 :class:`ImageExists` if the same client and cookie locked it
2340 """
2341 cookie = cstr(cookie, 'cookie')
2342 tag = cstr(tag, 'tag')
2343 cdef:
2344 char *_cookie = cookie
2345 char *_tag = tag
2346 with nogil:
2347 ret = rbd_lock_shared(self.image, _cookie, _tag)
2348 if ret < 0:
2349 raise make_ex(ret, 'error acquiring shared lock on image')
2350
2351 def unlock(self, cookie):
2352 """
2353 Release a lock on the image that was locked by this rados client.
2354 """
2355 cookie = cstr(cookie, 'cookie')
2356 cdef char *_cookie = cookie
2357 with nogil:
2358 ret = rbd_unlock(self.image, _cookie)
2359 if ret < 0:
2360 raise make_ex(ret, 'error unlocking image')
2361
2362 def break_lock(self, client, cookie):
2363 """
2364 Release a lock held by another rados client.
2365 """
2366 client = cstr(client, 'client')
2367 cookie = cstr(cookie, 'cookie')
2368 cdef:
2369 char *_client = client
2370 char *_cookie = cookie
2371 with nogil:
2372 ret = rbd_break_lock(self.image, _client, _cookie)
2373 if ret < 0:
2374 raise make_ex(ret, 'error unlocking image')
2375
2376 def mirror_image_enable(self):
2377 """
2378 Enable mirroring for the image.
2379 """
2380 with nogil:
2381 ret = rbd_mirror_image_enable(self.image)
2382 if ret < 0:
2383 raise make_ex(ret, 'error enabling mirroring for image %s'
2384 % (self.name,))
2385
2386 def mirror_image_disable(self, force):
2387 """
2388 Disable mirroring for the image.
2389
2390 :param force: force disabling
2391 :type force: bool
2392 """
2393 cdef bint c_force = force
2394 with nogil:
2395 ret = rbd_mirror_image_disable(self.image, c_force)
2396 if ret < 0:
2397 raise make_ex(ret, 'error disabling mirroring for image %s' %
2398 (self.name,))
2399
2400 def mirror_image_promote(self, force):
2401 """
2402 Promote the image to primary for mirroring.
2403
2404 :param force: force promoting
2405 :type force: bool
2406 """
2407 cdef bint c_force = force
2408 with nogil:
2409 ret = rbd_mirror_image_promote(self.image, c_force)
2410 if ret < 0:
2411 raise make_ex(ret, 'error promoting image %s to primary' %
2412 (self.name,))
2413
2414 def mirror_image_demote(self):
2415 """
2416 Demote the image to secondary for mirroring.
2417 """
2418 with nogil:
2419 ret = rbd_mirror_image_demote(self.image)
2420 if ret < 0:
2421 raise make_ex(ret, 'error demoting image %s to secondary' %
2422 (self.name,))
2423
2424 def mirror_image_resync(self):
2425 """
2426 Flag the image to resync.
2427 """
2428 with nogil:
2429 ret = rbd_mirror_image_resync(self.image)
2430 if ret < 0:
2431 raise make_ex(ret, 'error to resync image %s' % (self.name,))
2432
2433 def mirror_image_get_info(self):
2434 """
2435 Get mirror info for the image.
2436
2437 :returns: dict - contains the following keys:
2438
2439 * ``global_id`` (str) - image global id
2440
2441 * ``state`` (int) - mirror state
2442
2443 * ``primary`` (bool) - is image primary
2444 """
2445 cdef rbd_mirror_image_info_t c_info
2446 with nogil:
2447 ret = rbd_mirror_image_get_info(self.image, &c_info, sizeof(c_info))
2448 if ret != 0:
2449 raise make_ex(ret, 'error getting mirror info for image %s' %
2450 (self.name,))
2451 info = {
2452 'global_id' : decode_cstr(c_info.global_id),
2453 'state' : int(c_info.state),
2454 'primary' : c_info.primary,
2455 }
2456 free(c_info.global_id)
2457 return info
2458
2459 def mirror_image_get_status(self):
2460 """
2461 Get mirror status for the image.
2462
2463 :returns: dict - contains the following keys:
2464
2465 * ``name`` (str) - mirror image name
2466
2467 * `info` (dict) - mirror image info
2468
2469 * ``state`` (int) - status mirror state
2470
2471 * ``description`` (str) - status description
2472
2473 * ``last_update`` (datetime) - last status update time
2474
2475 * ``up`` (bool) - is mirroring agent up
2476 """
2477 cdef rbd_mirror_image_status_t c_status
2478 with nogil:
2479 ret = rbd_mirror_image_get_status(self.image, &c_status,
2480 sizeof(c_status))
2481 if ret != 0:
2482 raise make_ex(ret, 'error getting mirror status for image %s' %
2483 (self.name,))
2484 status = {
2485 'name' : decode_cstr(c_status.name),
2486 'info' : {
2487 'global_id' : decode_cstr(c_status.info.global_id),
2488 'state' : int(c_status.info.state),
2489 'primary' : c_status.info.primary,
2490 },
2491 'state' : c_status.state,
2492 'description' : decode_cstr(c_status.description),
2493 'last_update' : datetime.fromtimestamp(c_status.last_update),
2494 'up' : c_status.up,
2495 }
2496 free(c_status.name)
2497 free(c_status.info.global_id)
2498 free(c_status.description)
2499 return status
2500
2501 def aio_read(self, offset, length, oncomplete, fadvise_flags=0):
2502 """
2503 Asynchronously read data from the image
2504
2505 Raises :class:`InvalidArgument` if part of the range specified is
2506 outside the image.
2507
2508 oncomplete will be called with the returned read value as
2509 well as the completion:
2510
2511 oncomplete(completion, data_read)
2512
2513 :param offset: the offset to start reading at
2514 :type offset: int
2515 :param length: how many bytes to read
2516 :type length: int
2517 :param oncomplete: what to do when the read is complete
2518 :type oncomplete: completion
2519 :param fadvise_flags: fadvise flags for this read
2520 :type fadvise_flags: int
2521 :returns: :class:`Completion` - the completion object
2522 :raises: :class:`InvalidArgument`, :class:`IOError`
2523 """
2524
2525 cdef:
2526 char *ret_buf
2527 uint64_t _offset = offset
2528 size_t _length = length
2529 int _fadvise_flags = fadvise_flags
2530 Completion completion
2531
2532 def oncomplete_(completion_v):
2533 cdef Completion _completion_v = completion_v
2534 return_value = _completion_v.get_return_value()
2535 if return_value > 0 and return_value != length:
2536 _PyBytes_Resize(&_completion_v.buf, return_value)
2537 return oncomplete(_completion_v, <object>_completion_v.buf if return_value >= 0 else None)
2538
2539 completion = self.__get_completion(oncomplete_)
2540 completion.buf = PyBytes_FromStringAndSize(NULL, length)
2541 ret_buf = PyBytes_AsString(completion.buf)
2542 try:
2543 completion.__persist()
2544 with nogil:
2545 ret = rbd_aio_read2(self.image, _offset, _length, ret_buf,
2546 completion.rbd_comp, _fadvise_flags)
2547 if ret < 0:
2548 raise make_ex(ret, 'error reading %s %ld~%ld' %
2549 (self.name, offset, length))
2550 except:
2551 completion.__unpersist()
2552 raise
2553
2554 return completion
2555
2556 def aio_write(self, data, offset, oncomplete, fadvise_flags=0):
2557 """
2558 Asynchronously write data to the image
2559
2560 Raises :class:`InvalidArgument` if part of the write would fall outside
2561 the image.
2562
2563 oncomplete will be called with the completion:
2564
2565 oncomplete(completion)
2566
2567 :param data: the data to be written
2568 :type data: bytes
2569 :param offset: the offset to start writing at
2570 :type offset: int
2571 :param oncomplete: what to do when the write is complete
2572 :type oncomplete: completion
2573 :param fadvise_flags: fadvise flags for this write
2574 :type fadvise_flags: int
2575 :returns: :class:`Completion` - the completion object
2576 :raises: :class:`InvalidArgument`, :class:`IOError`
2577 """
2578
2579 cdef:
2580 uint64_t _offset = offset
2581 char *_data = data
2582 size_t _length = len(data)
2583 int _fadvise_flags = fadvise_flags
2584 Completion completion
2585
2586 completion = self.__get_completion(oncomplete)
2587 try:
2588 completion.__persist()
2589 with nogil:
2590 ret = rbd_aio_write2(self.image, _offset, _length, _data,
2591 completion.rbd_comp, _fadvise_flags)
2592 if ret < 0:
2593 raise make_ex(ret, 'error writing %s %ld~%ld' %
2594 (self.name, offset, _length))
2595 except:
2596 completion.__unpersist()
2597 raise
2598
2599 return completion
2600
2601 def aio_discard(self, offset, length, oncomplete):
2602 """
2603 Asynchronously trim the range from the image. It will be logically
2604 filled with zeroes.
2605 """
2606
2607 cdef:
2608 uint64_t _offset = offset
2609 size_t _length = length
2610 Completion completion
2611
2612 completion = self.__get_completion(oncomplete)
2613 try:
2614 completion.__persist()
2615 with nogil:
2616 ret = rbd_aio_discard(self.image, _offset, _length,
2617 completion.rbd_comp)
2618 if ret < 0:
2619 raise make_ex(ret, 'error discarding %s %ld~%ld' %
2620 (self.name, offset, _length))
2621 except:
2622 completion.__unpersist()
2623 raise
2624
2625 return completion
2626
2627 def aio_flush(self, oncomplete):
2628 """
2629 Asyncronously wait until all writes are fully flushed if caching is
2630 enabled.
2631 """
2632
2633 cdef Completion completion = self.__get_completion(oncomplete)
2634 try:
2635 completion.__persist()
2636 with nogil:
2637 ret = rbd_aio_flush(self.image, completion.rbd_comp)
2638 if ret < 0:
2639 raise make_ex(ret, 'error flushing')
2640 except:
2641 completion.__unpersist()
2642 raise
2643
2644 return completion
2645
2646 def metadata_get(self, key):
2647 """
2648 Get image metadata for the given key.
2649
2650 :param key: metadata key
2651 :type key: str
2652 :returns: str - image id
2653 """
2654 key = cstr(key, 'key')
2655 cdef:
2656 char *_key = key
2657 size_t size = 4096
2658 char *value = NULL
2659 int ret
2660 try:
2661 while True:
2662 value = <char *>realloc_chk(value, size)
2663 with nogil:
2664 ret = rbd_metadata_get(self.image, _key, value, &size)
2665 if ret != -errno.ERANGE:
2666 break
2667 if ret != 0:
2668 raise make_ex(ret, 'error getting metadata %s for image %s' %
2669 (self.key, self.name,))
2670 return decode_cstr(value)
2671 finally:
2672 free(value)
2673
2674 def metadata_set(self, key, value):
2675 """
2676 Set image metadata for the given key.
2677
2678 :param key: metadata key
2679 :type key: str
2680 :param value: metadata value
2681 :type value: str
2682 """
2683 key = cstr(key, 'key')
2684 value = cstr(value, 'value')
2685 cdef:
2686 char *_key = key
2687 char *_value = value
2688 with nogil:
2689 ret = rbd_metadata_set(self.image, _key, _value)
2690
2691 if ret != 0:
2692 raise make_ex(ret, 'error setting metadata %s for image %s' %
2693 (self.key, self.name,))
2694
2695
2696 def metadata_remove(self, key):
2697 """
2698 Remove image metadata for the given key.
2699
2700 :param key: metadata key
2701 :type key: str
2702 """
2703 key = cstr(key, 'key')
2704 cdef:
2705 char *_key = key
2706 with nogil:
2707 ret = rbd_metadata_remove(self.image, _key)
2708
2709 if ret != 0:
2710 raise make_ex(ret, 'error removing metadata %s for image %s' %
2711 (self.key, self.name,))
2712
2713 def metadata_list(self):
2714 """
2715 List image metadata.
2716
2717 :returns: :class:`MetadataIterator`
2718 """
2719 return MetadataIterator(self)
2720
2721 cdef class LockOwnerIterator(object):
2722 """
2723 Iterator over managed lock owners for an image
2724
2725 Yields a dictionary containing information about the image's lock
2726
2727 Keys are:
2728
2729 * ``mode`` (int) - active lock mode
2730
2731 * ``owner`` (str) - lock owner name
2732 """
2733
2734 cdef:
2735 rbd_lock_mode_t lock_mode
2736 char **lock_owners
2737 size_t num_lock_owners
2738 object image
2739
2740 def __init__(self, Image image):
2741 self.image = image
2742 self.lock_owners = NULL
2743 self.num_lock_owners = 8
2744 while True:
2745 self.lock_owners = <char**>realloc_chk(self.lock_owners,
2746 self.num_lock_owners *
2747 sizeof(char*))
2748 with nogil:
2749 ret = rbd_lock_get_owners(image.image, &self.lock_mode,
2750 self.lock_owners,
2751 &self.num_lock_owners)
2752 if ret >= 0:
2753 break
2754 elif ret != -errno.ERANGE:
2755 raise make_ex(ret, 'error listing lock owners for image %s' % (image.name,))
2756
2757 def __iter__(self):
2758 for i in range(self.num_lock_owners):
2759 yield {
2760 'mode' : int(self.lock_mode),
2761 'owner' : decode_cstr(self.lock_owners[i]),
2762 }
2763
2764 def __dealloc__(self):
2765 if self.lock_owners:
2766 rbd_lock_get_owners_cleanup(self.lock_owners, self.num_lock_owners)
2767 free(self.lock_owners)
2768
2769 cdef class MetadataIterator(object):
2770 """
2771 Iterator over metadata list for an image.
2772
2773 Yields ``(key, value)`` tuple.
2774
2775 * ``key`` (str) - metadata key
2776 * ``value`` (str) - metadata value
2777 """
2778
2779 cdef:
2780 object image_name
2781 rbd_image_t image
2782 char *last_read
2783 uint64_t max_read
2784 object next_chunk
2785
2786 def __init__(self, Image image):
2787 self.image_name = image.name
2788 self.image = image.image
2789 self.last_read = strdup("")
2790 self.max_read = 32
2791 self.get_next_chunk()
2792
2793 def __iter__(self):
2794 while len(self.next_chunk) > 0:
2795 for pair in self.next_chunk:
2796 yield pair
2797 if len(self.next_chunk) < self.max_read:
2798 break
2799 self.get_next_chunk()
2800
2801 def get_next_chunk(self):
2802 cdef:
2803 char *c_keys = NULL
2804 size_t keys_size = 4096
2805 char *c_vals = NULL
2806 size_t vals_size = 4096
2807 try:
2808 while True:
2809 c_keys = <char *>realloc_chk(c_keys, keys_size)
2810 c_vals = <char *>realloc_chk(c_vals, vals_size)
2811 with nogil:
2812 ret = rbd_metadata_list(self.image, self.last_read,
2813 self.max_read, c_keys, &keys_size,
2814 c_vals, &vals_size)
2815 if ret >= 0:
2816 break
2817 elif ret != -errno.ERANGE:
2818 raise make_ex(ret, 'error listing metadata for image %s' %
2819 (self.image_name,))
2820 keys = [decode_cstr(key) for key in
2821 c_keys[:keys_size].split(b'\0') if key]
2822 vals = [decode_cstr(val) for val in
2823 c_vals[:vals_size].split(b'\0') if val]
2824 if len(keys) > 0:
2825 free(self.last_read)
2826 self.last_read = strdup(keys[-1])
2827 self.next_chunk = zip(keys, vals)
2828 finally:
2829 free(c_keys)
2830 free(c_vals)
2831
2832 cdef class SnapIterator(object):
2833 """
2834 Iterator over snapshot info for an image.
2835
2836 Yields a dictionary containing information about a snapshot.
2837
2838 Keys are:
2839
2840 * ``id`` (int) - numeric identifier of the snapshot
2841
2842 * ``size`` (int) - size of the image at the time of snapshot (in bytes)
2843
2844 * ``name`` (str) - name of the snapshot
2845 """
2846
2847 cdef rbd_snap_info_t *snaps
2848 cdef int num_snaps
2849 cdef object image
2850
2851 def __init__(self, Image image):
2852 self.image = image
2853 self.snaps = NULL
2854 self.num_snaps = 10
2855 while True:
2856 self.snaps = <rbd_snap_info_t*>realloc_chk(self.snaps,
2857 self.num_snaps *
2858 sizeof(rbd_snap_info_t))
2859 with nogil:
2860 ret = rbd_snap_list(image.image, self.snaps, &self.num_snaps)
2861 if ret >= 0:
2862 self.num_snaps = ret
2863 break
2864 elif ret != -errno.ERANGE:
2865 raise make_ex(ret, 'error listing snapshots for image %s' % (image.name,))
2866
2867 def __iter__(self):
2868 for i in range(self.num_snaps):
2869 yield {
2870 'id' : self.snaps[i].id,
2871 'size' : self.snaps[i].size,
2872 'name' : decode_cstr(self.snaps[i].name),
2873 }
2874
2875 def __dealloc__(self):
2876 if self.snaps:
2877 rbd_snap_list_end(self.snaps)
2878 free(self.snaps)
2879
2880 cdef class TrashIterator(object):
2881 """
2882 Iterator over trash entries.
2883
2884 Yields a dictionary containing trash info of an image.
2885
2886 Keys are:
2887
2888 * `id` (str) - image id
2889
2890 * `name` (str) - image name
2891
2892 * `source` (str) - source of deletion
2893
2894 * `deletion_time` (datetime) - time of deletion
2895
2896 * `deferment_end_time` (datetime) - time that an image is allowed to be
2897 removed from trash
2898 """
2899
2900 cdef:
2901 rados_ioctx_t ioctx
2902 size_t num_entries
2903 rbd_trash_image_info_t *entries
2904
2905 def __init__(self, ioctx):
2906 self.ioctx = convert_ioctx(ioctx)
2907 self.num_entries = 1024
2908 self.entries = <rbd_trash_image_info_t *>realloc_chk(NULL,
2909 sizeof(rbd_trash_image_info_t) * self.num_entries)
2910 with nogil:
2911 ret = rbd_trash_list(self.ioctx, self.entries, &self.num_entries)
2912 if ret < 0:
2913 raise make_ex(ret, 'error listing trash entries')
2914
2915 __source_string = ['USER', 'MIRRORING']
2916
2917 def __iter__(self):
2918 for i in range(self.num_entries):
2919 yield {
2920 'id' : decode_cstr(self.entries[i].id),
2921 'name' : decode_cstr(self.entries[i].name),
2922 'source' : TrashIterator.__source_string[self.entries[i].source],
2923 'deletion_time' : datetime.fromtimestamp(self.entries[i].deletion_time),
2924 'deferment_end_time' : datetime.fromtimestamp(self.entries[i].deferment_end_time)
2925 }
2926
2927 def __dealloc__(self):
2928 rbd_trash_list_cleanup(self.entries, self.num_entries)
2929 if self.entries:
2930 free(self.entries)
2931