Commit | Line | Data |
---|---|---|
e2a58ee5 | 1 | |
602adf40 YS |
2 | /* |
3 | rbd.c -- Export ceph rados objects as a Linux block device | |
4 | ||
5 | ||
6 | based on drivers/block/osdblk.c: | |
7 | ||
8 | Copyright 2009 Red Hat, Inc. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; see the file COPYING. If not, write to | |
21 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | ||
23 | ||
24 | ||
dfc5606d | 25 | For usage instructions, please refer to: |
602adf40 | 26 | |
dfc5606d | 27 | Documentation/ABI/testing/sysfs-bus-rbd |
602adf40 YS |
28 | |
29 | */ | |
30 | ||
31 | #include <linux/ceph/libceph.h> | |
32 | #include <linux/ceph/osd_client.h> | |
33 | #include <linux/ceph/mon_client.h> | |
ed95b21a | 34 | #include <linux/ceph/cls_lock_client.h> |
43df3d35 | 35 | #include <linux/ceph/striper.h> |
602adf40 | 36 | #include <linux/ceph/decode.h> |
82995cc6 | 37 | #include <linux/fs_parser.h> |
30d1cff8 | 38 | #include <linux/bsearch.h> |
602adf40 YS |
39 | |
40 | #include <linux/kernel.h> | |
41 | #include <linux/device.h> | |
42 | #include <linux/module.h> | |
7ad18afa | 43 | #include <linux/blk-mq.h> |
602adf40 YS |
44 | #include <linux/fs.h> |
45 | #include <linux/blkdev.h> | |
1c2a9dfe | 46 | #include <linux/slab.h> |
f8a22fc2 | 47 | #include <linux/idr.h> |
bc1ecc65 | 48 | #include <linux/workqueue.h> |
602adf40 YS |
49 | |
50 | #include "rbd_types.h" | |
51 | ||
aafb230e AE |
52 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
53 | ||
a2acd00e AE |
54 | /* |
55 | * Increment the given counter and return its updated value. | |
56 | * If the counter is already 0 it will not be incremented. | |
57 | * If the counter is already at its maximum value returns | |
58 | * -EINVAL without updating it. | |
59 | */ | |
60 | static int atomic_inc_return_safe(atomic_t *v) | |
61 | { | |
62 | unsigned int counter; | |
63 | ||
bfc18e38 | 64 | counter = (unsigned int)atomic_fetch_add_unless(v, 1, 0); |
a2acd00e AE |
65 | if (counter <= (unsigned int)INT_MAX) |
66 | return (int)counter; | |
67 | ||
68 | atomic_dec(v); | |
69 | ||
70 | return -EINVAL; | |
71 | } | |
72 | ||
73 | /* Decrement the counter. Return the resulting value, or -EINVAL */ | |
74 | static int atomic_dec_return_safe(atomic_t *v) | |
75 | { | |
76 | int counter; | |
77 | ||
78 | counter = atomic_dec_return(v); | |
79 | if (counter >= 0) | |
80 | return counter; | |
81 | ||
82 | atomic_inc(v); | |
83 | ||
84 | return -EINVAL; | |
85 | } | |
86 | ||
f0f8cef5 | 87 | #define RBD_DRV_NAME "rbd" |
602adf40 | 88 | |
7e513d43 ID |
89 | #define RBD_MINORS_PER_MAJOR 256 |
90 | #define RBD_SINGLE_MAJOR_PART_SHIFT 4 | |
602adf40 | 91 | |
6d69bb53 ID |
92 | #define RBD_MAX_PARENT_CHAIN_LEN 16 |
93 | ||
d4b125e9 AE |
94 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
95 | #define RBD_MAX_SNAP_NAME_LEN \ | |
96 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) | |
97 | ||
35d489f9 | 98 | #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ |
602adf40 YS |
99 | |
100 | #define RBD_SNAP_HEAD_NAME "-" | |
101 | ||
9682fc6d AE |
102 | #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ |
103 | ||
9e15b77d AE |
104 | /* This allows a single page to hold an image name sent by OSD */ |
105 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) | |
1e130199 | 106 | #define RBD_IMAGE_ID_LEN_MAX 64 |
9e15b77d | 107 | |
1e130199 | 108 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
589d30e0 | 109 | |
ed95b21a | 110 | #define RBD_NOTIFY_TIMEOUT 5 /* seconds */ |
99d16943 ID |
111 | #define RBD_RETRY_DELAY msecs_to_jiffies(1000) |
112 | ||
d889140c AE |
113 | /* Feature bits */ |
114 | ||
8767b293 ID |
115 | #define RBD_FEATURE_LAYERING (1ULL<<0) |
116 | #define RBD_FEATURE_STRIPINGV2 (1ULL<<1) | |
117 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2) | |
22e8bd51 ID |
118 | #define RBD_FEATURE_OBJECT_MAP (1ULL<<3) |
119 | #define RBD_FEATURE_FAST_DIFF (1ULL<<4) | |
b9f6d447 | 120 | #define RBD_FEATURE_DEEP_FLATTEN (1ULL<<5) |
8767b293 | 121 | #define RBD_FEATURE_DATA_POOL (1ULL<<7) |
e573427a | 122 | #define RBD_FEATURE_OPERATIONS (1ULL<<8) |
8767b293 | 123 | |
ed95b21a ID |
124 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
125 | RBD_FEATURE_STRIPINGV2 | \ | |
7e97332e | 126 | RBD_FEATURE_EXCLUSIVE_LOCK | \ |
22e8bd51 ID |
127 | RBD_FEATURE_OBJECT_MAP | \ |
128 | RBD_FEATURE_FAST_DIFF | \ | |
b9f6d447 | 129 | RBD_FEATURE_DEEP_FLATTEN | \ |
e573427a ID |
130 | RBD_FEATURE_DATA_POOL | \ |
131 | RBD_FEATURE_OPERATIONS) | |
d889140c AE |
132 | |
133 | /* Features supported by this (client software) implementation. */ | |
134 | ||
770eba6e | 135 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
d889140c | 136 | |
81a89793 AE |
137 | /* |
138 | * An RBD device name will be "rbd#", where the "rbd" comes from | |
139 | * RBD_DRV_NAME above, and # is a unique integer identifier. | |
81a89793 | 140 | */ |
602adf40 YS |
141 | #define DEV_NAME_LEN 32 |
142 | ||
143 | /* | |
144 | * block device image metadata (in-memory version) | |
145 | */ | |
146 | struct rbd_image_header { | |
f35a4dee | 147 | /* These six fields never change for a given rbd image */ |
849b4260 | 148 | char *object_prefix; |
602adf40 | 149 | __u8 obj_order; |
f35a4dee AE |
150 | u64 stripe_unit; |
151 | u64 stripe_count; | |
7e97332e | 152 | s64 data_pool_id; |
f35a4dee | 153 | u64 features; /* Might be changeable someday? */ |
602adf40 | 154 | |
f84344f3 AE |
155 | /* The remaining fields need to be updated occasionally */ |
156 | u64 image_size; | |
157 | struct ceph_snap_context *snapc; | |
f35a4dee AE |
158 | char *snap_names; /* format 1 only */ |
159 | u64 *snap_sizes; /* format 1 only */ | |
59c2be1e YS |
160 | }; |
161 | ||
0d7dbfce AE |
162 | /* |
163 | * An rbd image specification. | |
164 | * | |
165 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely | |
c66c6e0c AE |
166 | * identify an image. Each rbd_dev structure includes a pointer to |
167 | * an rbd_spec structure that encapsulates this identity. | |
168 | * | |
169 | * Each of the id's in an rbd_spec has an associated name. For a | |
170 | * user-mapped image, the names are supplied and the id's associated | |
171 | * with them are looked up. For a layered image, a parent image is | |
172 | * defined by the tuple, and the names are looked up. | |
173 | * | |
174 | * An rbd_dev structure contains a parent_spec pointer which is | |
175 | * non-null if the image it represents is a child in a layered | |
176 | * image. This pointer will refer to the rbd_spec structure used | |
177 | * by the parent rbd_dev for its own identity (i.e., the structure | |
178 | * is shared between the parent and child). | |
179 | * | |
180 | * Since these structures are populated once, during the discovery | |
181 | * phase of image construction, they are effectively immutable so | |
182 | * we make no effort to synchronize access to them. | |
183 | * | |
184 | * Note that code herein does not assume the image name is known (it | |
185 | * could be a null pointer). | |
0d7dbfce AE |
186 | */ |
187 | struct rbd_spec { | |
188 | u64 pool_id; | |
ecb4dc22 | 189 | const char *pool_name; |
b26c047b | 190 | const char *pool_ns; /* NULL if default, never "" */ |
0d7dbfce | 191 | |
ecb4dc22 AE |
192 | const char *image_id; |
193 | const char *image_name; | |
0d7dbfce AE |
194 | |
195 | u64 snap_id; | |
ecb4dc22 | 196 | const char *snap_name; |
0d7dbfce AE |
197 | |
198 | struct kref kref; | |
199 | }; | |
200 | ||
602adf40 | 201 | /* |
f0f8cef5 | 202 | * an instance of the client. multiple devices may share an rbd client. |
602adf40 YS |
203 | */ |
204 | struct rbd_client { | |
205 | struct ceph_client *client; | |
206 | struct kref kref; | |
207 | struct list_head node; | |
208 | }; | |
209 | ||
0192ce2e ID |
210 | struct pending_result { |
211 | int result; /* first nonzero result */ | |
212 | int num_pending; | |
213 | }; | |
214 | ||
bf0d5f50 | 215 | struct rbd_img_request; |
bf0d5f50 | 216 | |
9969ebc5 | 217 | enum obj_request_type { |
a1fbb5e7 | 218 | OBJ_REQUEST_NODATA = 1, |
5359a17d | 219 | OBJ_REQUEST_BIO, /* pointer into provided bio (list) */ |
7e07efb1 | 220 | OBJ_REQUEST_BVECS, /* pointer into provided bio_vec array */ |
afb97888 | 221 | OBJ_REQUEST_OWN_BVECS, /* private bio_vec array, doesn't own pages */ |
9969ebc5 | 222 | }; |
bf0d5f50 | 223 | |
6d2940c8 | 224 | enum obj_operation_type { |
a1fbb5e7 | 225 | OBJ_OP_READ = 1, |
6d2940c8 | 226 | OBJ_OP_WRITE, |
90e98c52 | 227 | OBJ_OP_DISCARD, |
6484cbe9 | 228 | OBJ_OP_ZEROOUT, |
6d2940c8 GZ |
229 | }; |
230 | ||
0ad5d953 ID |
231 | #define RBD_OBJ_FLAG_DELETION (1U << 0) |
232 | #define RBD_OBJ_FLAG_COPYUP_ENABLED (1U << 1) | |
793333a3 | 233 | #define RBD_OBJ_FLAG_COPYUP_ZEROS (1U << 2) |
22e8bd51 ID |
234 | #define RBD_OBJ_FLAG_MAY_EXIST (1U << 3) |
235 | #define RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT (1U << 4) | |
0ad5d953 | 236 | |
a9b67e69 | 237 | enum rbd_obj_read_state { |
85b5e6d1 ID |
238 | RBD_OBJ_READ_START = 1, |
239 | RBD_OBJ_READ_OBJECT, | |
a9b67e69 ID |
240 | RBD_OBJ_READ_PARENT, |
241 | }; | |
242 | ||
3da691bf ID |
243 | /* |
244 | * Writes go through the following state machine to deal with | |
245 | * layering: | |
246 | * | |
89a59c1c ID |
247 | * . . . . . RBD_OBJ_WRITE_GUARD. . . . . . . . . . . . . . |
248 | * . | . | |
249 | * . v . | |
250 | * . RBD_OBJ_WRITE_READ_FROM_PARENT. . . . | |
251 | * . | . . | |
252 | * . v v (deep-copyup . | |
253 | * (image . RBD_OBJ_WRITE_COPYUP_EMPTY_SNAPC . not needed) . | |
254 | * flattened) v | . . | |
255 | * . v . . | |
256 | * . . . .RBD_OBJ_WRITE_COPYUP_OPS. . . . . (copyup . | |
257 | * | not needed) v | |
258 | * v . | |
259 | * done . . . . . . . . . . . . . . . . . . | |
260 | * ^ | |
261 | * | | |
262 | * RBD_OBJ_WRITE_FLAT | |
3da691bf ID |
263 | * |
264 | * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether | |
89a59c1c ID |
265 | * assert_exists guard is needed or not (in some cases it's not needed |
266 | * even if there is a parent). | |
3da691bf ID |
267 | */ |
268 | enum rbd_obj_write_state { | |
85b5e6d1 | 269 | RBD_OBJ_WRITE_START = 1, |
22e8bd51 | 270 | RBD_OBJ_WRITE_PRE_OBJECT_MAP, |
85b5e6d1 | 271 | RBD_OBJ_WRITE_OBJECT, |
793333a3 ID |
272 | __RBD_OBJ_WRITE_COPYUP, |
273 | RBD_OBJ_WRITE_COPYUP, | |
22e8bd51 | 274 | RBD_OBJ_WRITE_POST_OBJECT_MAP, |
793333a3 ID |
275 | }; |
276 | ||
277 | enum rbd_obj_copyup_state { | |
278 | RBD_OBJ_COPYUP_START = 1, | |
279 | RBD_OBJ_COPYUP_READ_PARENT, | |
22e8bd51 ID |
280 | __RBD_OBJ_COPYUP_OBJECT_MAPS, |
281 | RBD_OBJ_COPYUP_OBJECT_MAPS, | |
793333a3 ID |
282 | __RBD_OBJ_COPYUP_WRITE_OBJECT, |
283 | RBD_OBJ_COPYUP_WRITE_OBJECT, | |
926f9b3f AE |
284 | }; |
285 | ||
bf0d5f50 | 286 | struct rbd_obj_request { |
43df3d35 | 287 | struct ceph_object_extent ex; |
0ad5d953 | 288 | unsigned int flags; /* RBD_OBJ_FLAG_* */ |
c5b5ef6c | 289 | union { |
a9b67e69 | 290 | enum rbd_obj_read_state read_state; /* for reads */ |
3da691bf | 291 | enum rbd_obj_write_state write_state; /* for writes */ |
c5b5ef6c | 292 | }; |
bf0d5f50 | 293 | |
51c3509e | 294 | struct rbd_img_request *img_request; |
86bd7998 ID |
295 | struct ceph_file_extent *img_extents; |
296 | u32 num_img_extents; | |
bf0d5f50 | 297 | |
788e2df3 | 298 | union { |
5359a17d | 299 | struct ceph_bio_iter bio_pos; |
788e2df3 | 300 | struct { |
7e07efb1 ID |
301 | struct ceph_bvec_iter bvec_pos; |
302 | u32 bvec_count; | |
afb97888 | 303 | u32 bvec_idx; |
788e2df3 AE |
304 | }; |
305 | }; | |
793333a3 ID |
306 | |
307 | enum rbd_obj_copyup_state copyup_state; | |
7e07efb1 ID |
308 | struct bio_vec *copyup_bvecs; |
309 | u32 copyup_bvec_count; | |
bf0d5f50 | 310 | |
bcbab1db | 311 | struct list_head osd_reqs; /* w/ r_private_item */ |
bf0d5f50 | 312 | |
85b5e6d1 | 313 | struct mutex state_mutex; |
793333a3 | 314 | struct pending_result pending; |
bf0d5f50 AE |
315 | struct kref kref; |
316 | }; | |
317 | ||
0c425248 | 318 | enum img_req_flags { |
9849e986 | 319 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
d0b2e944 | 320 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
0c425248 AE |
321 | }; |
322 | ||
0192ce2e ID |
323 | enum rbd_img_state { |
324 | RBD_IMG_START = 1, | |
637cd060 | 325 | RBD_IMG_EXCLUSIVE_LOCK, |
0192ce2e ID |
326 | __RBD_IMG_OBJECT_REQUESTS, |
327 | RBD_IMG_OBJECT_REQUESTS, | |
328 | }; | |
329 | ||
bf0d5f50 | 330 | struct rbd_img_request { |
bf0d5f50 | 331 | struct rbd_device *rbd_dev; |
9bb0248d | 332 | enum obj_operation_type op_type; |
ecc633ca | 333 | enum obj_request_type data_type; |
0c425248 | 334 | unsigned long flags; |
0192ce2e | 335 | enum rbd_img_state state; |
bf0d5f50 | 336 | union { |
9849e986 | 337 | u64 snap_id; /* for reads */ |
bf0d5f50 | 338 | struct ceph_snap_context *snapc; /* for writes */ |
9849e986 AE |
339 | }; |
340 | union { | |
341 | struct request *rq; /* block request */ | |
342 | struct rbd_obj_request *obj_request; /* obj req initiator */ | |
bf0d5f50 | 343 | }; |
bf0d5f50 | 344 | |
e1fddc8f | 345 | struct list_head lock_item; |
43df3d35 | 346 | struct list_head object_extents; /* obj_req.ex structs */ |
bf0d5f50 | 347 | |
0192ce2e ID |
348 | struct mutex state_mutex; |
349 | struct pending_result pending; | |
350 | struct work_struct work; | |
351 | int work_result; | |
bf0d5f50 AE |
352 | struct kref kref; |
353 | }; | |
354 | ||
355 | #define for_each_obj_request(ireq, oreq) \ | |
43df3d35 | 356 | list_for_each_entry(oreq, &(ireq)->object_extents, ex.oe_item) |
bf0d5f50 | 357 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
43df3d35 | 358 | list_for_each_entry_safe(oreq, n, &(ireq)->object_extents, ex.oe_item) |
bf0d5f50 | 359 | |
99d16943 ID |
360 | enum rbd_watch_state { |
361 | RBD_WATCH_STATE_UNREGISTERED, | |
362 | RBD_WATCH_STATE_REGISTERED, | |
363 | RBD_WATCH_STATE_ERROR, | |
364 | }; | |
365 | ||
ed95b21a ID |
366 | enum rbd_lock_state { |
367 | RBD_LOCK_STATE_UNLOCKED, | |
368 | RBD_LOCK_STATE_LOCKED, | |
369 | RBD_LOCK_STATE_RELEASING, | |
370 | }; | |
371 | ||
372 | /* WatchNotify::ClientId */ | |
373 | struct rbd_client_id { | |
374 | u64 gid; | |
375 | u64 handle; | |
376 | }; | |
377 | ||
f84344f3 | 378 | struct rbd_mapping { |
99c1f08f | 379 | u64 size; |
f84344f3 AE |
380 | }; |
381 | ||
602adf40 YS |
382 | /* |
383 | * a single device | |
384 | */ | |
385 | struct rbd_device { | |
de71a297 | 386 | int dev_id; /* blkdev unique id */ |
602adf40 YS |
387 | |
388 | int major; /* blkdev assigned major */ | |
dd82fff1 | 389 | int minor; |
602adf40 | 390 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
602adf40 | 391 | |
a30b71b9 | 392 | u32 image_format; /* Either 1 or 2 */ |
602adf40 YS |
393 | struct rbd_client *rbd_client; |
394 | ||
395 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ | |
396 | ||
b82d167b | 397 | spinlock_t lock; /* queue, flags, open_count */ |
602adf40 YS |
398 | |
399 | struct rbd_image_header header; | |
b82d167b | 400 | unsigned long flags; /* possibly lock protected */ |
0d7dbfce | 401 | struct rbd_spec *spec; |
d147543d | 402 | struct rbd_options *opts; |
0d6d1e9c | 403 | char *config_info; /* add{,_single_major} string */ |
602adf40 | 404 | |
c41d13a3 | 405 | struct ceph_object_id header_oid; |
922dab61 | 406 | struct ceph_object_locator header_oloc; |
971f839a | 407 | |
1643dfa4 | 408 | struct ceph_file_layout layout; /* used for all rbd requests */ |
0903e875 | 409 | |
99d16943 ID |
410 | struct mutex watch_mutex; |
411 | enum rbd_watch_state watch_state; | |
922dab61 | 412 | struct ceph_osd_linger_request *watch_handle; |
99d16943 ID |
413 | u64 watch_cookie; |
414 | struct delayed_work watch_dwork; | |
59c2be1e | 415 | |
ed95b21a ID |
416 | struct rw_semaphore lock_rwsem; |
417 | enum rbd_lock_state lock_state; | |
cbbfb0ff | 418 | char lock_cookie[32]; |
ed95b21a ID |
419 | struct rbd_client_id owner_cid; |
420 | struct work_struct acquired_lock_work; | |
421 | struct work_struct released_lock_work; | |
422 | struct delayed_work lock_dwork; | |
423 | struct work_struct unlock_work; | |
e1fddc8f | 424 | spinlock_t lock_lists_lock; |
637cd060 | 425 | struct list_head acquiring_list; |
e1fddc8f | 426 | struct list_head running_list; |
637cd060 ID |
427 | struct completion acquire_wait; |
428 | int acquire_err; | |
e1fddc8f | 429 | struct completion releasing_wait; |
ed95b21a | 430 | |
22e8bd51 ID |
431 | spinlock_t object_map_lock; |
432 | u8 *object_map; | |
433 | u64 object_map_size; /* in objects */ | |
434 | u64 object_map_flags; | |
ed95b21a | 435 | |
1643dfa4 | 436 | struct workqueue_struct *task_wq; |
59c2be1e | 437 | |
86b00e0d AE |
438 | struct rbd_spec *parent_spec; |
439 | u64 parent_overlap; | |
a2acd00e | 440 | atomic_t parent_ref; |
2f82ee54 | 441 | struct rbd_device *parent; |
86b00e0d | 442 | |
7ad18afa CH |
443 | /* Block layer tags. */ |
444 | struct blk_mq_tag_set tag_set; | |
445 | ||
c666601a JD |
446 | /* protects updating the header */ |
447 | struct rw_semaphore header_rwsem; | |
f84344f3 AE |
448 | |
449 | struct rbd_mapping mapping; | |
602adf40 YS |
450 | |
451 | struct list_head node; | |
dfc5606d | 452 | |
dfc5606d YS |
453 | /* sysfs related */ |
454 | struct device dev; | |
b82d167b | 455 | unsigned long open_count; /* protected by lock */ |
dfc5606d YS |
456 | }; |
457 | ||
b82d167b | 458 | /* |
87c0fded ID |
459 | * Flag bits for rbd_dev->flags: |
460 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected | |
461 | * by rbd_dev->lock | |
b82d167b | 462 | */ |
6d292906 | 463 | enum rbd_dev_flags { |
686238b7 | 464 | RBD_DEV_FLAG_EXISTS, /* rbd_dev_device_setup() ran */ |
b82d167b | 465 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
39258aa2 | 466 | RBD_DEV_FLAG_READONLY, /* -o ro or snapshot */ |
6d292906 AE |
467 | }; |
468 | ||
cfbf6377 | 469 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
e124a82f | 470 | |
602adf40 | 471 | static LIST_HEAD(rbd_dev_list); /* devices */ |
e124a82f AE |
472 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
473 | ||
432b8587 AE |
474 | static LIST_HEAD(rbd_client_list); /* clients */ |
475 | static DEFINE_SPINLOCK(rbd_client_list_lock); | |
602adf40 | 476 | |
78c2a44a AE |
477 | /* Slab caches for frequently-allocated structures */ |
478 | ||
1c2a9dfe | 479 | static struct kmem_cache *rbd_img_request_cache; |
868311b1 | 480 | static struct kmem_cache *rbd_obj_request_cache; |
1c2a9dfe | 481 | |
9b60e70b | 482 | static int rbd_major; |
f8a22fc2 ID |
483 | static DEFINE_IDA(rbd_dev_id_ida); |
484 | ||
f5ee37bd ID |
485 | static struct workqueue_struct *rbd_wq; |
486 | ||
89a59c1c ID |
487 | static struct ceph_snap_context rbd_empty_snapc = { |
488 | .nref = REFCOUNT_INIT(1), | |
489 | }; | |
490 | ||
9b60e70b | 491 | /* |
3cfa3b16 | 492 | * single-major requires >= 0.75 version of userspace rbd utility. |
9b60e70b | 493 | */ |
3cfa3b16 | 494 | static bool single_major = true; |
5657a819 | 495 | module_param(single_major, bool, 0444); |
3cfa3b16 | 496 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: true)"); |
9b60e70b | 497 | |
7e9586ba GKH |
498 | static ssize_t add_store(struct bus_type *bus, const char *buf, size_t count); |
499 | static ssize_t remove_store(struct bus_type *bus, const char *buf, | |
500 | size_t count); | |
501 | static ssize_t add_single_major_store(struct bus_type *bus, const char *buf, | |
502 | size_t count); | |
503 | static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf, | |
504 | size_t count); | |
6d69bb53 | 505 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth); |
f0f8cef5 | 506 | |
9b60e70b ID |
507 | static int rbd_dev_id_to_minor(int dev_id) |
508 | { | |
7e513d43 | 509 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
9b60e70b ID |
510 | } |
511 | ||
512 | static int minor_to_rbd_dev_id(int minor) | |
513 | { | |
7e513d43 | 514 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
9b60e70b ID |
515 | } |
516 | ||
39258aa2 ID |
517 | static bool rbd_is_ro(struct rbd_device *rbd_dev) |
518 | { | |
519 | return test_bit(RBD_DEV_FLAG_READONLY, &rbd_dev->flags); | |
520 | } | |
521 | ||
f3c0e459 ID |
522 | static bool rbd_is_snap(struct rbd_device *rbd_dev) |
523 | { | |
524 | return rbd_dev->spec->snap_id != CEPH_NOSNAP; | |
525 | } | |
526 | ||
ed95b21a ID |
527 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
528 | { | |
637cd060 ID |
529 | lockdep_assert_held(&rbd_dev->lock_rwsem); |
530 | ||
ed95b21a ID |
531 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
532 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; | |
533 | } | |
534 | ||
535 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) | |
536 | { | |
537 | bool is_lock_owner; | |
538 | ||
539 | down_read(&rbd_dev->lock_rwsem); | |
540 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); | |
541 | up_read(&rbd_dev->lock_rwsem); | |
542 | return is_lock_owner; | |
543 | } | |
544 | ||
7e9586ba | 545 | static ssize_t supported_features_show(struct bus_type *bus, char *buf) |
8767b293 ID |
546 | { |
547 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); | |
548 | } | |
549 | ||
7e9586ba GKH |
550 | static BUS_ATTR_WO(add); |
551 | static BUS_ATTR_WO(remove); | |
552 | static BUS_ATTR_WO(add_single_major); | |
553 | static BUS_ATTR_WO(remove_single_major); | |
554 | static BUS_ATTR_RO(supported_features); | |
b15a21dd GKH |
555 | |
556 | static struct attribute *rbd_bus_attrs[] = { | |
557 | &bus_attr_add.attr, | |
558 | &bus_attr_remove.attr, | |
9b60e70b ID |
559 | &bus_attr_add_single_major.attr, |
560 | &bus_attr_remove_single_major.attr, | |
8767b293 | 561 | &bus_attr_supported_features.attr, |
b15a21dd | 562 | NULL, |
f0f8cef5 | 563 | }; |
92c76dc0 ID |
564 | |
565 | static umode_t rbd_bus_is_visible(struct kobject *kobj, | |
566 | struct attribute *attr, int index) | |
567 | { | |
9b60e70b ID |
568 | if (!single_major && |
569 | (attr == &bus_attr_add_single_major.attr || | |
570 | attr == &bus_attr_remove_single_major.attr)) | |
571 | return 0; | |
572 | ||
92c76dc0 ID |
573 | return attr->mode; |
574 | } | |
575 | ||
576 | static const struct attribute_group rbd_bus_group = { | |
577 | .attrs = rbd_bus_attrs, | |
578 | .is_visible = rbd_bus_is_visible, | |
579 | }; | |
580 | __ATTRIBUTE_GROUPS(rbd_bus); | |
f0f8cef5 AE |
581 | |
582 | static struct bus_type rbd_bus_type = { | |
583 | .name = "rbd", | |
b15a21dd | 584 | .bus_groups = rbd_bus_groups, |
f0f8cef5 AE |
585 | }; |
586 | ||
587 | static void rbd_root_dev_release(struct device *dev) | |
588 | { | |
589 | } | |
590 | ||
591 | static struct device rbd_root_dev = { | |
592 | .init_name = "rbd", | |
593 | .release = rbd_root_dev_release, | |
594 | }; | |
595 | ||
06ecc6cb AE |
596 | static __printf(2, 3) |
597 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) | |
598 | { | |
599 | struct va_format vaf; | |
600 | va_list args; | |
601 | ||
602 | va_start(args, fmt); | |
603 | vaf.fmt = fmt; | |
604 | vaf.va = &args; | |
605 | ||
606 | if (!rbd_dev) | |
607 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); | |
608 | else if (rbd_dev->disk) | |
609 | printk(KERN_WARNING "%s: %s: %pV\n", | |
610 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); | |
611 | else if (rbd_dev->spec && rbd_dev->spec->image_name) | |
612 | printk(KERN_WARNING "%s: image %s: %pV\n", | |
613 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); | |
614 | else if (rbd_dev->spec && rbd_dev->spec->image_id) | |
615 | printk(KERN_WARNING "%s: id %s: %pV\n", | |
616 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); | |
617 | else /* punt */ | |
618 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", | |
619 | RBD_DRV_NAME, rbd_dev, &vaf); | |
620 | va_end(args); | |
621 | } | |
622 | ||
aafb230e AE |
623 | #ifdef RBD_DEBUG |
624 | #define rbd_assert(expr) \ | |
625 | if (unlikely(!(expr))) { \ | |
626 | printk(KERN_ERR "\nAssertion failure in %s() " \ | |
627 | "at line %d:\n\n" \ | |
628 | "\trbd_assert(%s);\n\n", \ | |
629 | __func__, __LINE__, #expr); \ | |
630 | BUG(); \ | |
631 | } | |
632 | #else /* !RBD_DEBUG */ | |
633 | # define rbd_assert(expr) ((void) 0) | |
634 | #endif /* !RBD_DEBUG */ | |
dfc5606d | 635 | |
05a46afd | 636 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
8b3e1a56 | 637 | |
cc4a38bd | 638 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
2df3fac7 | 639 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
a720ae09 | 640 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
e8f59b59 | 641 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
54cac61f AE |
642 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
643 | u64 snap_id); | |
2ad3d716 AE |
644 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
645 | u8 *order, u64 *snap_size); | |
22e8bd51 | 646 | static int rbd_dev_v2_get_flags(struct rbd_device *rbd_dev); |
59c2be1e | 647 | |
54ab3b24 | 648 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req, int result); |
0192ce2e ID |
649 | static void rbd_img_handle_request(struct rbd_img_request *img_req, int result); |
650 | ||
651 | /* | |
652 | * Return true if nothing else is pending. | |
653 | */ | |
654 | static bool pending_result_dec(struct pending_result *pending, int *result) | |
655 | { | |
656 | rbd_assert(pending->num_pending > 0); | |
657 | ||
658 | if (*result && !pending->result) | |
659 | pending->result = *result; | |
660 | if (--pending->num_pending) | |
661 | return false; | |
662 | ||
663 | *result = pending->result; | |
664 | return true; | |
665 | } | |
59c2be1e | 666 | |
602adf40 YS |
667 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
668 | { | |
f0f8cef5 | 669 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
b82d167b | 670 | bool removing = false; |
602adf40 | 671 | |
a14ea269 | 672 | spin_lock_irq(&rbd_dev->lock); |
b82d167b AE |
673 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
674 | removing = true; | |
675 | else | |
676 | rbd_dev->open_count++; | |
a14ea269 | 677 | spin_unlock_irq(&rbd_dev->lock); |
b82d167b AE |
678 | if (removing) |
679 | return -ENOENT; | |
680 | ||
c3e946ce | 681 | (void) get_device(&rbd_dev->dev); |
340c7a2b | 682 | |
602adf40 YS |
683 | return 0; |
684 | } | |
685 | ||
db2a144b | 686 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
dfc5606d YS |
687 | { |
688 | struct rbd_device *rbd_dev = disk->private_data; | |
b82d167b AE |
689 | unsigned long open_count_before; |
690 | ||
a14ea269 | 691 | spin_lock_irq(&rbd_dev->lock); |
b82d167b | 692 | open_count_before = rbd_dev->open_count--; |
a14ea269 | 693 | spin_unlock_irq(&rbd_dev->lock); |
b82d167b | 694 | rbd_assert(open_count_before > 0); |
dfc5606d | 695 | |
c3e946ce | 696 | put_device(&rbd_dev->dev); |
dfc5606d YS |
697 | } |
698 | ||
131fd9f6 GZ |
699 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
700 | { | |
1de797bb | 701 | int ro; |
131fd9f6 | 702 | |
1de797bb | 703 | if (get_user(ro, (int __user *)arg)) |
131fd9f6 GZ |
704 | return -EFAULT; |
705 | ||
c1b62057 ID |
706 | /* |
707 | * Both images mapped read-only and snapshots can't be marked | |
708 | * read-write. | |
709 | */ | |
710 | if (!ro) { | |
711 | if (rbd_is_ro(rbd_dev)) | |
712 | return -EROFS; | |
713 | ||
714 | rbd_assert(!rbd_is_snap(rbd_dev)); | |
715 | } | |
131fd9f6 | 716 | |
1de797bb ID |
717 | /* Let blkdev_roset() handle it */ |
718 | return -ENOTTY; | |
131fd9f6 GZ |
719 | } |
720 | ||
721 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, | |
722 | unsigned int cmd, unsigned long arg) | |
723 | { | |
724 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; | |
1de797bb | 725 | int ret; |
131fd9f6 | 726 | |
131fd9f6 GZ |
727 | switch (cmd) { |
728 | case BLKROSET: | |
729 | ret = rbd_ioctl_set_ro(rbd_dev, arg); | |
730 | break; | |
731 | default: | |
732 | ret = -ENOTTY; | |
733 | } | |
734 | ||
131fd9f6 GZ |
735 | return ret; |
736 | } | |
737 | ||
738 | #ifdef CONFIG_COMPAT | |
739 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
740 | unsigned int cmd, unsigned long arg) | |
741 | { | |
742 | return rbd_ioctl(bdev, mode, cmd, arg); | |
743 | } | |
744 | #endif /* CONFIG_COMPAT */ | |
745 | ||
602adf40 YS |
746 | static const struct block_device_operations rbd_bd_ops = { |
747 | .owner = THIS_MODULE, | |
748 | .open = rbd_open, | |
dfc5606d | 749 | .release = rbd_release, |
131fd9f6 GZ |
750 | .ioctl = rbd_ioctl, |
751 | #ifdef CONFIG_COMPAT | |
752 | .compat_ioctl = rbd_compat_ioctl, | |
753 | #endif | |
602adf40 YS |
754 | }; |
755 | ||
756 | /* | |
7262cfca | 757 | * Initialize an rbd client instance. Success or not, this function |
cfbf6377 | 758 | * consumes ceph_opts. Caller holds client_mutex. |
602adf40 | 759 | */ |
f8c38929 | 760 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
602adf40 YS |
761 | { |
762 | struct rbd_client *rbdc; | |
763 | int ret = -ENOMEM; | |
764 | ||
37206ee5 | 765 | dout("%s:\n", __func__); |
602adf40 YS |
766 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
767 | if (!rbdc) | |
768 | goto out_opt; | |
769 | ||
770 | kref_init(&rbdc->kref); | |
771 | INIT_LIST_HEAD(&rbdc->node); | |
772 | ||
74da4a0f | 773 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
602adf40 | 774 | if (IS_ERR(rbdc->client)) |
08f75463 | 775 | goto out_rbdc; |
43ae4701 | 776 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
602adf40 YS |
777 | |
778 | ret = ceph_open_session(rbdc->client); | |
779 | if (ret < 0) | |
08f75463 | 780 | goto out_client; |
602adf40 | 781 | |
432b8587 | 782 | spin_lock(&rbd_client_list_lock); |
602adf40 | 783 | list_add_tail(&rbdc->node, &rbd_client_list); |
432b8587 | 784 | spin_unlock(&rbd_client_list_lock); |
602adf40 | 785 | |
37206ee5 | 786 | dout("%s: rbdc %p\n", __func__, rbdc); |
bc534d86 | 787 | |
602adf40 | 788 | return rbdc; |
08f75463 | 789 | out_client: |
602adf40 | 790 | ceph_destroy_client(rbdc->client); |
08f75463 | 791 | out_rbdc: |
602adf40 YS |
792 | kfree(rbdc); |
793 | out_opt: | |
43ae4701 AE |
794 | if (ceph_opts) |
795 | ceph_destroy_options(ceph_opts); | |
37206ee5 AE |
796 | dout("%s: error %d\n", __func__, ret); |
797 | ||
28f259b7 | 798 | return ERR_PTR(ret); |
602adf40 YS |
799 | } |
800 | ||
2f82ee54 AE |
801 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
802 | { | |
803 | kref_get(&rbdc->kref); | |
804 | ||
805 | return rbdc; | |
806 | } | |
807 | ||
602adf40 | 808 | /* |
1f7ba331 AE |
809 | * Find a ceph client with specific addr and configuration. If |
810 | * found, bump its reference count. | |
602adf40 | 811 | */ |
1f7ba331 | 812 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
602adf40 YS |
813 | { |
814 | struct rbd_client *client_node; | |
1f7ba331 | 815 | bool found = false; |
602adf40 | 816 | |
43ae4701 | 817 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
602adf40 YS |
818 | return NULL; |
819 | ||
1f7ba331 AE |
820 | spin_lock(&rbd_client_list_lock); |
821 | list_for_each_entry(client_node, &rbd_client_list, node) { | |
822 | if (!ceph_compare_options(ceph_opts, client_node->client)) { | |
2f82ee54 AE |
823 | __rbd_get_client(client_node); |
824 | ||
1f7ba331 AE |
825 | found = true; |
826 | break; | |
827 | } | |
828 | } | |
829 | spin_unlock(&rbd_client_list_lock); | |
830 | ||
831 | return found ? client_node : NULL; | |
602adf40 YS |
832 | } |
833 | ||
59c2be1e | 834 | /* |
210c104c | 835 | * (Per device) rbd map options |
59c2be1e YS |
836 | */ |
837 | enum { | |
b5584180 | 838 | Opt_queue_depth, |
0c93e1b7 | 839 | Opt_alloc_size, |
34f55d0b | 840 | Opt_lock_timeout, |
59c2be1e | 841 | /* int args above */ |
b26c047b | 842 | Opt_pool_ns, |
59c2be1e | 843 | /* string args above */ |
cc0538b6 AE |
844 | Opt_read_only, |
845 | Opt_read_write, | |
80de1912 | 846 | Opt_lock_on_read, |
e010dd0a | 847 | Opt_exclusive, |
d9360540 | 848 | Opt_notrim, |
59c2be1e YS |
849 | }; |
850 | ||
82995cc6 DH |
851 | static const struct fs_parameter_spec rbd_param_specs[] = { |
852 | fsparam_u32 ("alloc_size", Opt_alloc_size), | |
853 | fsparam_flag ("exclusive", Opt_exclusive), | |
854 | fsparam_flag ("lock_on_read", Opt_lock_on_read), | |
855 | fsparam_u32 ("lock_timeout", Opt_lock_timeout), | |
856 | fsparam_flag ("notrim", Opt_notrim), | |
857 | fsparam_string ("_pool_ns", Opt_pool_ns), | |
858 | fsparam_u32 ("queue_depth", Opt_queue_depth), | |
859 | fsparam_flag ("read_only", Opt_read_only), | |
860 | fsparam_flag ("read_write", Opt_read_write), | |
861 | fsparam_flag ("ro", Opt_read_only), | |
862 | fsparam_flag ("rw", Opt_read_write), | |
863 | {} | |
864 | }; | |
865 | ||
866 | static const struct fs_parameter_description rbd_parameters = { | |
867 | .name = "rbd", | |
868 | .specs = rbd_param_specs, | |
59c2be1e YS |
869 | }; |
870 | ||
98571b5a | 871 | struct rbd_options { |
b5584180 | 872 | int queue_depth; |
0c93e1b7 | 873 | int alloc_size; |
34f55d0b | 874 | unsigned long lock_timeout; |
98571b5a | 875 | bool read_only; |
80de1912 | 876 | bool lock_on_read; |
e010dd0a | 877 | bool exclusive; |
d9360540 | 878 | bool trim; |
98571b5a AE |
879 | }; |
880 | ||
b5584180 | 881 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
0c93e1b7 | 882 | #define RBD_ALLOC_SIZE_DEFAULT (64 * 1024) |
34f55d0b | 883 | #define RBD_LOCK_TIMEOUT_DEFAULT 0 /* no timeout */ |
98571b5a | 884 | #define RBD_READ_ONLY_DEFAULT false |
80de1912 | 885 | #define RBD_LOCK_ON_READ_DEFAULT false |
e010dd0a | 886 | #define RBD_EXCLUSIVE_DEFAULT false |
d9360540 | 887 | #define RBD_TRIM_DEFAULT true |
98571b5a | 888 | |
82995cc6 | 889 | struct rbd_parse_opts_ctx { |
c300156b | 890 | struct rbd_spec *spec; |
82995cc6 | 891 | struct ceph_options *copts; |
c300156b ID |
892 | struct rbd_options *opts; |
893 | }; | |
894 | ||
6d2940c8 GZ |
895 | static char* obj_op_name(enum obj_operation_type op_type) |
896 | { | |
897 | switch (op_type) { | |
898 | case OBJ_OP_READ: | |
899 | return "read"; | |
900 | case OBJ_OP_WRITE: | |
901 | return "write"; | |
90e98c52 GZ |
902 | case OBJ_OP_DISCARD: |
903 | return "discard"; | |
6484cbe9 ID |
904 | case OBJ_OP_ZEROOUT: |
905 | return "zeroout"; | |
6d2940c8 GZ |
906 | default: |
907 | return "???"; | |
908 | } | |
909 | } | |
910 | ||
602adf40 YS |
911 | /* |
912 | * Destroy ceph client | |
d23a4b3f | 913 | * |
432b8587 | 914 | * Caller must hold rbd_client_list_lock. |
602adf40 YS |
915 | */ |
916 | static void rbd_client_release(struct kref *kref) | |
917 | { | |
918 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); | |
919 | ||
37206ee5 | 920 | dout("%s: rbdc %p\n", __func__, rbdc); |
cd9d9f5d | 921 | spin_lock(&rbd_client_list_lock); |
602adf40 | 922 | list_del(&rbdc->node); |
cd9d9f5d | 923 | spin_unlock(&rbd_client_list_lock); |
602adf40 YS |
924 | |
925 | ceph_destroy_client(rbdc->client); | |
926 | kfree(rbdc); | |
927 | } | |
928 | ||
929 | /* | |
930 | * Drop reference to ceph client node. If it's not referenced anymore, release | |
931 | * it. | |
932 | */ | |
9d3997fd | 933 | static void rbd_put_client(struct rbd_client *rbdc) |
602adf40 | 934 | { |
c53d5893 AE |
935 | if (rbdc) |
936 | kref_put(&rbdc->kref, rbd_client_release); | |
602adf40 YS |
937 | } |
938 | ||
5feb0d8d ID |
939 | /* |
940 | * Get a ceph client with specific addr and configuration, if one does | |
941 | * not exist create it. Either way, ceph_opts is consumed by this | |
942 | * function. | |
943 | */ | |
944 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) | |
945 | { | |
946 | struct rbd_client *rbdc; | |
dd435855 | 947 | int ret; |
5feb0d8d | 948 | |
a32e4143 | 949 | mutex_lock(&client_mutex); |
5feb0d8d | 950 | rbdc = rbd_client_find(ceph_opts); |
dd435855 | 951 | if (rbdc) { |
5feb0d8d | 952 | ceph_destroy_options(ceph_opts); |
dd435855 ID |
953 | |
954 | /* | |
955 | * Using an existing client. Make sure ->pg_pools is up to | |
956 | * date before we look up the pool id in do_rbd_add(). | |
957 | */ | |
9d4a227f ID |
958 | ret = ceph_wait_for_latest_osdmap(rbdc->client, |
959 | rbdc->client->options->mount_timeout); | |
dd435855 ID |
960 | if (ret) { |
961 | rbd_warn(NULL, "failed to get latest osdmap: %d", ret); | |
962 | rbd_put_client(rbdc); | |
963 | rbdc = ERR_PTR(ret); | |
964 | } | |
965 | } else { | |
5feb0d8d | 966 | rbdc = rbd_client_create(ceph_opts); |
dd435855 | 967 | } |
5feb0d8d ID |
968 | mutex_unlock(&client_mutex); |
969 | ||
970 | return rbdc; | |
971 | } | |
972 | ||
a30b71b9 AE |
973 | static bool rbd_image_format_valid(u32 image_format) |
974 | { | |
975 | return image_format == 1 || image_format == 2; | |
976 | } | |
977 | ||
8e94af8e AE |
978 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
979 | { | |
103a150f AE |
980 | size_t size; |
981 | u32 snap_count; | |
982 | ||
983 | /* The header has to start with the magic rbd header text */ | |
984 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) | |
985 | return false; | |
986 | ||
db2388b6 AE |
987 | /* The bio layer requires at least sector-sized I/O */ |
988 | ||
989 | if (ondisk->options.order < SECTOR_SHIFT) | |
990 | return false; | |
991 | ||
992 | /* If we use u64 in a few spots we may be able to loosen this */ | |
993 | ||
994 | if (ondisk->options.order > 8 * sizeof (int) - 1) | |
995 | return false; | |
996 | ||
103a150f AE |
997 | /* |
998 | * The size of a snapshot header has to fit in a size_t, and | |
999 | * that limits the number of snapshots. | |
1000 | */ | |
1001 | snap_count = le32_to_cpu(ondisk->snap_count); | |
1002 | size = SIZE_MAX - sizeof (struct ceph_snap_context); | |
1003 | if (snap_count > size / sizeof (__le64)) | |
1004 | return false; | |
1005 | ||
1006 | /* | |
1007 | * Not only that, but the size of the entire the snapshot | |
1008 | * header must also be representable in a size_t. | |
1009 | */ | |
1010 | size -= snap_count * sizeof (__le64); | |
1011 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) | |
1012 | return false; | |
1013 | ||
1014 | return true; | |
8e94af8e AE |
1015 | } |
1016 | ||
5bc3fb17 ID |
1017 | /* |
1018 | * returns the size of an object in the image | |
1019 | */ | |
1020 | static u32 rbd_obj_bytes(struct rbd_image_header *header) | |
1021 | { | |
1022 | return 1U << header->obj_order; | |
1023 | } | |
1024 | ||
263423f8 ID |
1025 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
1026 | { | |
1027 | if (rbd_dev->header.stripe_unit == 0 || | |
1028 | rbd_dev->header.stripe_count == 0) { | |
1029 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); | |
1030 | rbd_dev->header.stripe_count = 1; | |
1031 | } | |
1032 | ||
1033 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; | |
1034 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; | |
1035 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); | |
7e97332e ID |
1036 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
1037 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; | |
263423f8 ID |
1038 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
1039 | } | |
1040 | ||
602adf40 | 1041 | /* |
bb23e37a AE |
1042 | * Fill an rbd image header with information from the given format 1 |
1043 | * on-disk header. | |
602adf40 | 1044 | */ |
662518b1 | 1045 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
4156d998 | 1046 | struct rbd_image_header_ondisk *ondisk) |
602adf40 | 1047 | { |
662518b1 | 1048 | struct rbd_image_header *header = &rbd_dev->header; |
bb23e37a AE |
1049 | bool first_time = header->object_prefix == NULL; |
1050 | struct ceph_snap_context *snapc; | |
1051 | char *object_prefix = NULL; | |
1052 | char *snap_names = NULL; | |
1053 | u64 *snap_sizes = NULL; | |
ccece235 | 1054 | u32 snap_count; |
bb23e37a | 1055 | int ret = -ENOMEM; |
621901d6 | 1056 | u32 i; |
602adf40 | 1057 | |
bb23e37a | 1058 | /* Allocate this now to avoid having to handle failure below */ |
6a52325f | 1059 | |
bb23e37a | 1060 | if (first_time) { |
848d796c ID |
1061 | object_prefix = kstrndup(ondisk->object_prefix, |
1062 | sizeof(ondisk->object_prefix), | |
1063 | GFP_KERNEL); | |
bb23e37a AE |
1064 | if (!object_prefix) |
1065 | return -ENOMEM; | |
bb23e37a | 1066 | } |
00f1f36f | 1067 | |
bb23e37a | 1068 | /* Allocate the snapshot context and fill it in */ |
00f1f36f | 1069 | |
bb23e37a AE |
1070 | snap_count = le32_to_cpu(ondisk->snap_count); |
1071 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); | |
1072 | if (!snapc) | |
1073 | goto out_err; | |
1074 | snapc->seq = le64_to_cpu(ondisk->snap_seq); | |
602adf40 | 1075 | if (snap_count) { |
bb23e37a | 1076 | struct rbd_image_snap_ondisk *snaps; |
f785cc1d AE |
1077 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
1078 | ||
bb23e37a | 1079 | /* We'll keep a copy of the snapshot names... */ |
621901d6 | 1080 | |
bb23e37a AE |
1081 | if (snap_names_len > (u64)SIZE_MAX) |
1082 | goto out_2big; | |
1083 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); | |
1084 | if (!snap_names) | |
6a52325f AE |
1085 | goto out_err; |
1086 | ||
bb23e37a | 1087 | /* ...as well as the array of their sizes. */ |
88a25a5f ME |
1088 | snap_sizes = kmalloc_array(snap_count, |
1089 | sizeof(*header->snap_sizes), | |
1090 | GFP_KERNEL); | |
bb23e37a | 1091 | if (!snap_sizes) |
6a52325f | 1092 | goto out_err; |
bb23e37a | 1093 | |
f785cc1d | 1094 | /* |
bb23e37a AE |
1095 | * Copy the names, and fill in each snapshot's id |
1096 | * and size. | |
1097 | * | |
99a41ebc | 1098 | * Note that rbd_dev_v1_header_info() guarantees the |
bb23e37a | 1099 | * ondisk buffer we're working with has |
f785cc1d AE |
1100 | * snap_names_len bytes beyond the end of the |
1101 | * snapshot id array, this memcpy() is safe. | |
1102 | */ | |
bb23e37a AE |
1103 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
1104 | snaps = ondisk->snaps; | |
1105 | for (i = 0; i < snap_count; i++) { | |
1106 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); | |
1107 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); | |
1108 | } | |
602adf40 | 1109 | } |
6a52325f | 1110 | |
bb23e37a | 1111 | /* We won't fail any more, fill in the header */ |
621901d6 | 1112 | |
bb23e37a AE |
1113 | if (first_time) { |
1114 | header->object_prefix = object_prefix; | |
1115 | header->obj_order = ondisk->options.order; | |
263423f8 | 1116 | rbd_init_layout(rbd_dev); |
602adf40 | 1117 | } else { |
662518b1 AE |
1118 | ceph_put_snap_context(header->snapc); |
1119 | kfree(header->snap_names); | |
1120 | kfree(header->snap_sizes); | |
602adf40 | 1121 | } |
849b4260 | 1122 | |
bb23e37a | 1123 | /* The remaining fields always get updated (when we refresh) */ |
621901d6 | 1124 | |
f84344f3 | 1125 | header->image_size = le64_to_cpu(ondisk->image_size); |
bb23e37a AE |
1126 | header->snapc = snapc; |
1127 | header->snap_names = snap_names; | |
1128 | header->snap_sizes = snap_sizes; | |
468521c1 | 1129 | |
602adf40 | 1130 | return 0; |
bb23e37a AE |
1131 | out_2big: |
1132 | ret = -EIO; | |
6a52325f | 1133 | out_err: |
bb23e37a AE |
1134 | kfree(snap_sizes); |
1135 | kfree(snap_names); | |
1136 | ceph_put_snap_context(snapc); | |
1137 | kfree(object_prefix); | |
ccece235 | 1138 | |
bb23e37a | 1139 | return ret; |
602adf40 YS |
1140 | } |
1141 | ||
9682fc6d AE |
1142 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
1143 | { | |
1144 | const char *snap_name; | |
1145 | ||
1146 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); | |
1147 | ||
1148 | /* Skip over names until we find the one we are looking for */ | |
1149 | ||
1150 | snap_name = rbd_dev->header.snap_names; | |
1151 | while (which--) | |
1152 | snap_name += strlen(snap_name) + 1; | |
1153 | ||
1154 | return kstrdup(snap_name, GFP_KERNEL); | |
1155 | } | |
1156 | ||
30d1cff8 AE |
1157 | /* |
1158 | * Snapshot id comparison function for use with qsort()/bsearch(). | |
1159 | * Note that result is for snapshots in *descending* order. | |
1160 | */ | |
1161 | static int snapid_compare_reverse(const void *s1, const void *s2) | |
1162 | { | |
1163 | u64 snap_id1 = *(u64 *)s1; | |
1164 | u64 snap_id2 = *(u64 *)s2; | |
1165 | ||
1166 | if (snap_id1 < snap_id2) | |
1167 | return 1; | |
1168 | return snap_id1 == snap_id2 ? 0 : -1; | |
1169 | } | |
1170 | ||
1171 | /* | |
1172 | * Search a snapshot context to see if the given snapshot id is | |
1173 | * present. | |
1174 | * | |
1175 | * Returns the position of the snapshot id in the array if it's found, | |
1176 | * or BAD_SNAP_INDEX otherwise. | |
1177 | * | |
1178 | * Note: The snapshot array is in kept sorted (by the osd) in | |
1179 | * reverse order, highest snapshot id first. | |
1180 | */ | |
9682fc6d AE |
1181 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
1182 | { | |
1183 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; | |
30d1cff8 | 1184 | u64 *found; |
9682fc6d | 1185 | |
30d1cff8 AE |
1186 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
1187 | sizeof (snap_id), snapid_compare_reverse); | |
9682fc6d | 1188 | |
30d1cff8 | 1189 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
9682fc6d AE |
1190 | } |
1191 | ||
2ad3d716 AE |
1192 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
1193 | u64 snap_id) | |
9e15b77d | 1194 | { |
54cac61f | 1195 | u32 which; |
da6a6b63 | 1196 | const char *snap_name; |
9e15b77d | 1197 | |
54cac61f AE |
1198 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
1199 | if (which == BAD_SNAP_INDEX) | |
da6a6b63 | 1200 | return ERR_PTR(-ENOENT); |
54cac61f | 1201 | |
da6a6b63 JD |
1202 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
1203 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); | |
54cac61f AE |
1204 | } |
1205 | ||
1206 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) | |
1207 | { | |
9e15b77d AE |
1208 | if (snap_id == CEPH_NOSNAP) |
1209 | return RBD_SNAP_HEAD_NAME; | |
1210 | ||
54cac61f AE |
1211 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
1212 | if (rbd_dev->image_format == 1) | |
1213 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); | |
9e15b77d | 1214 | |
54cac61f | 1215 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
9e15b77d AE |
1216 | } |
1217 | ||
2ad3d716 AE |
1218 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
1219 | u64 *snap_size) | |
602adf40 | 1220 | { |
2ad3d716 AE |
1221 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
1222 | if (snap_id == CEPH_NOSNAP) { | |
1223 | *snap_size = rbd_dev->header.image_size; | |
1224 | } else if (rbd_dev->image_format == 1) { | |
1225 | u32 which; | |
602adf40 | 1226 | |
2ad3d716 AE |
1227 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
1228 | if (which == BAD_SNAP_INDEX) | |
1229 | return -ENOENT; | |
e86924a8 | 1230 | |
2ad3d716 AE |
1231 | *snap_size = rbd_dev->header.snap_sizes[which]; |
1232 | } else { | |
1233 | u64 size = 0; | |
1234 | int ret; | |
1235 | ||
1236 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); | |
1237 | if (ret) | |
1238 | return ret; | |
1239 | ||
1240 | *snap_size = size; | |
1241 | } | |
1242 | return 0; | |
602adf40 YS |
1243 | } |
1244 | ||
2ad3d716 AE |
1245 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
1246 | { | |
8f4b7d98 | 1247 | u64 snap_id = rbd_dev->spec->snap_id; |
2ad3d716 | 1248 | u64 size = 0; |
2ad3d716 AE |
1249 | int ret; |
1250 | ||
2ad3d716 | 1251 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
2ad3d716 AE |
1252 | if (ret) |
1253 | return ret; | |
1254 | ||
1255 | rbd_dev->mapping.size = size; | |
8b0241f8 | 1256 | return 0; |
602adf40 YS |
1257 | } |
1258 | ||
d1cf5788 AE |
1259 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
1260 | { | |
1261 | rbd_dev->mapping.size = 0; | |
200a6a8b AE |
1262 | } |
1263 | ||
5359a17d | 1264 | static void zero_bvec(struct bio_vec *bv) |
602adf40 | 1265 | { |
602adf40 | 1266 | void *buf; |
5359a17d | 1267 | unsigned long flags; |
602adf40 | 1268 | |
5359a17d ID |
1269 | buf = bvec_kmap_irq(bv, &flags); |
1270 | memset(buf, 0, bv->bv_len); | |
1271 | flush_dcache_page(bv->bv_page); | |
1272 | bvec_kunmap_irq(buf, &flags); | |
602adf40 YS |
1273 | } |
1274 | ||
5359a17d | 1275 | static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes) |
b9434c5b | 1276 | { |
5359a17d | 1277 | struct ceph_bio_iter it = *bio_pos; |
b9434c5b | 1278 | |
5359a17d ID |
1279 | ceph_bio_iter_advance(&it, off); |
1280 | ceph_bio_iter_advance_step(&it, bytes, ({ | |
1281 | zero_bvec(&bv); | |
1282 | })); | |
b9434c5b AE |
1283 | } |
1284 | ||
7e07efb1 | 1285 | static void zero_bvecs(struct ceph_bvec_iter *bvec_pos, u32 off, u32 bytes) |
602adf40 | 1286 | { |
7e07efb1 | 1287 | struct ceph_bvec_iter it = *bvec_pos; |
602adf40 | 1288 | |
7e07efb1 ID |
1289 | ceph_bvec_iter_advance(&it, off); |
1290 | ceph_bvec_iter_advance_step(&it, bytes, ({ | |
1291 | zero_bvec(&bv); | |
1292 | })); | |
f7760dad AE |
1293 | } |
1294 | ||
1295 | /* | |
3da691bf | 1296 | * Zero a range in @obj_req data buffer defined by a bio (list) or |
afb97888 | 1297 | * (private) bio_vec array. |
f7760dad | 1298 | * |
3da691bf | 1299 | * @off is relative to the start of the data buffer. |
926f9b3f | 1300 | */ |
3da691bf ID |
1301 | static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off, |
1302 | u32 bytes) | |
926f9b3f | 1303 | { |
54ab3b24 ID |
1304 | dout("%s %p data buf %u~%u\n", __func__, obj_req, off, bytes); |
1305 | ||
ecc633ca | 1306 | switch (obj_req->img_request->data_type) { |
3da691bf ID |
1307 | case OBJ_REQUEST_BIO: |
1308 | zero_bios(&obj_req->bio_pos, off, bytes); | |
1309 | break; | |
1310 | case OBJ_REQUEST_BVECS: | |
afb97888 | 1311 | case OBJ_REQUEST_OWN_BVECS: |
3da691bf ID |
1312 | zero_bvecs(&obj_req->bvec_pos, off, bytes); |
1313 | break; | |
1314 | default: | |
16809372 | 1315 | BUG(); |
6365d33a AE |
1316 | } |
1317 | } | |
1318 | ||
bf0d5f50 AE |
1319 | static void rbd_obj_request_destroy(struct kref *kref); |
1320 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) | |
1321 | { | |
1322 | rbd_assert(obj_request != NULL); | |
37206ee5 | 1323 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
2c935bc5 | 1324 | kref_read(&obj_request->kref)); |
bf0d5f50 AE |
1325 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
1326 | } | |
1327 | ||
bf0d5f50 AE |
1328 | static void rbd_img_request_destroy(struct kref *kref); |
1329 | static void rbd_img_request_put(struct rbd_img_request *img_request) | |
1330 | { | |
1331 | rbd_assert(img_request != NULL); | |
37206ee5 | 1332 | dout("%s: img %p (was %d)\n", __func__, img_request, |
2c935bc5 | 1333 | kref_read(&img_request->kref)); |
e93aca0a | 1334 | kref_put(&img_request->kref, rbd_img_request_destroy); |
bf0d5f50 AE |
1335 | } |
1336 | ||
1337 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, | |
1338 | struct rbd_obj_request *obj_request) | |
1339 | { | |
25dcf954 AE |
1340 | rbd_assert(obj_request->img_request == NULL); |
1341 | ||
b155e86c | 1342 | /* Image request now owns object's original reference */ |
bf0d5f50 | 1343 | obj_request->img_request = img_request; |
15961b44 | 1344 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
bf0d5f50 AE |
1345 | } |
1346 | ||
1347 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, | |
1348 | struct rbd_obj_request *obj_request) | |
1349 | { | |
15961b44 | 1350 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
43df3d35 | 1351 | list_del(&obj_request->ex.oe_item); |
bf0d5f50 | 1352 | rbd_assert(obj_request->img_request == img_request); |
bf0d5f50 AE |
1353 | rbd_obj_request_put(obj_request); |
1354 | } | |
1355 | ||
a086a1b8 | 1356 | static void rbd_osd_submit(struct ceph_osd_request *osd_req) |
bf0d5f50 | 1357 | { |
a086a1b8 | 1358 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
980917fc | 1359 | |
a086a1b8 ID |
1360 | dout("%s osd_req %p for obj_req %p objno %llu %llu~%llu\n", |
1361 | __func__, osd_req, obj_req, obj_req->ex.oe_objno, | |
1362 | obj_req->ex.oe_off, obj_req->ex.oe_len); | |
980917fc | 1363 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
bf0d5f50 AE |
1364 | } |
1365 | ||
0c425248 AE |
1366 | /* |
1367 | * The default/initial value for all image request flags is 0. Each | |
1368 | * is conditionally set to 1 at image request initialization time | |
1369 | * and currently never change thereafter. | |
1370 | */ | |
d0b2e944 AE |
1371 | static void img_request_layered_set(struct rbd_img_request *img_request) |
1372 | { | |
1373 | set_bit(IMG_REQ_LAYERED, &img_request->flags); | |
1374 | smp_mb(); | |
1375 | } | |
1376 | ||
a2acd00e AE |
1377 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
1378 | { | |
1379 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); | |
1380 | smp_mb(); | |
1381 | } | |
1382 | ||
d0b2e944 AE |
1383 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
1384 | { | |
1385 | smp_mb(); | |
1386 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; | |
1387 | } | |
1388 | ||
3da691bf | 1389 | static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req) |
6e2a4505 | 1390 | { |
3da691bf | 1391 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
b9434c5b | 1392 | |
43df3d35 ID |
1393 | return !obj_req->ex.oe_off && |
1394 | obj_req->ex.oe_len == rbd_dev->layout.object_size; | |
6e2a4505 AE |
1395 | } |
1396 | ||
3da691bf | 1397 | static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req) |
bf0d5f50 | 1398 | { |
3da691bf | 1399 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
bf0d5f50 | 1400 | |
43df3d35 | 1401 | return obj_req->ex.oe_off + obj_req->ex.oe_len == |
3da691bf | 1402 | rbd_dev->layout.object_size; |
0dcc685e ID |
1403 | } |
1404 | ||
13488d53 ID |
1405 | /* |
1406 | * Must be called after rbd_obj_calc_img_extents(). | |
1407 | */ | |
1408 | static bool rbd_obj_copyup_enabled(struct rbd_obj_request *obj_req) | |
1409 | { | |
1410 | if (!obj_req->num_img_extents || | |
9b17eb2c ID |
1411 | (rbd_obj_is_entire(obj_req) && |
1412 | !obj_req->img_request->snapc->num_snaps)) | |
13488d53 ID |
1413 | return false; |
1414 | ||
1415 | return true; | |
1416 | } | |
1417 | ||
86bd7998 | 1418 | static u64 rbd_obj_img_extents_bytes(struct rbd_obj_request *obj_req) |
bf0d5f50 | 1419 | { |
86bd7998 ID |
1420 | return ceph_file_extents_bytes(obj_req->img_extents, |
1421 | obj_req->num_img_extents); | |
bf0d5f50 AE |
1422 | } |
1423 | ||
3da691bf | 1424 | static bool rbd_img_is_write(struct rbd_img_request *img_req) |
bf0d5f50 | 1425 | { |
9bb0248d | 1426 | switch (img_req->op_type) { |
3da691bf ID |
1427 | case OBJ_OP_READ: |
1428 | return false; | |
1429 | case OBJ_OP_WRITE: | |
1430 | case OBJ_OP_DISCARD: | |
6484cbe9 | 1431 | case OBJ_OP_ZEROOUT: |
3da691bf ID |
1432 | return true; |
1433 | default: | |
c6244b3b | 1434 | BUG(); |
3da691bf | 1435 | } |
90e98c52 GZ |
1436 | } |
1437 | ||
85e084fe | 1438 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
bf0d5f50 | 1439 | { |
3da691bf | 1440 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
54ab3b24 | 1441 | int result; |
bf0d5f50 | 1442 | |
3da691bf ID |
1443 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, |
1444 | osd_req->r_result, obj_req); | |
bf0d5f50 | 1445 | |
54ab3b24 ID |
1446 | /* |
1447 | * Writes aren't allowed to return a data payload. In some | |
1448 | * guarded write cases (e.g. stat + zero on an empty object) | |
1449 | * a stat response makes it through, but we don't care. | |
1450 | */ | |
1451 | if (osd_req->r_result > 0 && rbd_img_is_write(obj_req->img_request)) | |
1452 | result = 0; | |
3da691bf | 1453 | else |
54ab3b24 | 1454 | result = osd_req->r_result; |
bf0d5f50 | 1455 | |
54ab3b24 | 1456 | rbd_obj_handle_request(obj_req, result); |
bf0d5f50 AE |
1457 | } |
1458 | ||
bcbab1db | 1459 | static void rbd_osd_format_read(struct ceph_osd_request *osd_req) |
430c28c3 | 1460 | { |
bcbab1db | 1461 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
430c28c3 | 1462 | |
a162b308 | 1463 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
7c84883a | 1464 | osd_req->r_snapid = obj_request->img_request->snap_id; |
9d4df01f AE |
1465 | } |
1466 | ||
bcbab1db | 1467 | static void rbd_osd_format_write(struct ceph_osd_request *osd_req) |
9d4df01f | 1468 | { |
bcbab1db | 1469 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
9d4df01f | 1470 | |
a162b308 | 1471 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE; |
fac02ddf | 1472 | ktime_get_real_ts64(&osd_req->r_mtime); |
43df3d35 | 1473 | osd_req->r_data_offset = obj_request->ex.oe_off; |
430c28c3 AE |
1474 | } |
1475 | ||
bc81207e | 1476 | static struct ceph_osd_request * |
bcbab1db ID |
1477 | __rbd_obj_add_osd_request(struct rbd_obj_request *obj_req, |
1478 | struct ceph_snap_context *snapc, int num_ops) | |
bc81207e | 1479 | { |
e28eded5 | 1480 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
bc81207e ID |
1481 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
1482 | struct ceph_osd_request *req; | |
a90bb0c1 ID |
1483 | const char *name_format = rbd_dev->image_format == 1 ? |
1484 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; | |
bcbab1db | 1485 | int ret; |
bc81207e | 1486 | |
e28eded5 | 1487 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
bc81207e | 1488 | if (!req) |
bcbab1db | 1489 | return ERR_PTR(-ENOMEM); |
bc81207e | 1490 | |
bcbab1db | 1491 | list_add_tail(&req->r_private_item, &obj_req->osd_reqs); |
bc81207e | 1492 | req->r_callback = rbd_osd_req_callback; |
a162b308 | 1493 | req->r_priv = obj_req; |
bc81207e | 1494 | |
b26c047b ID |
1495 | /* |
1496 | * Data objects may be stored in a separate pool, but always in | |
1497 | * the same namespace in that pool as the header in its pool. | |
1498 | */ | |
1499 | ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc); | |
bc81207e | 1500 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
b26c047b | 1501 | |
bcbab1db ID |
1502 | ret = ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
1503 | rbd_dev->header.object_prefix, | |
1504 | obj_req->ex.oe_objno); | |
1505 | if (ret) | |
1506 | return ERR_PTR(ret); | |
bc81207e | 1507 | |
bc81207e | 1508 | return req; |
bc81207e ID |
1509 | } |
1510 | ||
e28eded5 | 1511 | static struct ceph_osd_request * |
bcbab1db | 1512 | rbd_obj_add_osd_request(struct rbd_obj_request *obj_req, int num_ops) |
bf0d5f50 | 1513 | { |
bcbab1db ID |
1514 | return __rbd_obj_add_osd_request(obj_req, obj_req->img_request->snapc, |
1515 | num_ops); | |
bf0d5f50 AE |
1516 | } |
1517 | ||
ecc633ca | 1518 | static struct rbd_obj_request *rbd_obj_request_create(void) |
bf0d5f50 AE |
1519 | { |
1520 | struct rbd_obj_request *obj_request; | |
bf0d5f50 | 1521 | |
5a60e876 | 1522 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
6c696d85 | 1523 | if (!obj_request) |
f907ad55 | 1524 | return NULL; |
f907ad55 | 1525 | |
43df3d35 | 1526 | ceph_object_extent_init(&obj_request->ex); |
bcbab1db | 1527 | INIT_LIST_HEAD(&obj_request->osd_reqs); |
85b5e6d1 | 1528 | mutex_init(&obj_request->state_mutex); |
bf0d5f50 AE |
1529 | kref_init(&obj_request->kref); |
1530 | ||
67e2b652 | 1531 | dout("%s %p\n", __func__, obj_request); |
bf0d5f50 AE |
1532 | return obj_request; |
1533 | } | |
1534 | ||
1535 | static void rbd_obj_request_destroy(struct kref *kref) | |
1536 | { | |
1537 | struct rbd_obj_request *obj_request; | |
bcbab1db | 1538 | struct ceph_osd_request *osd_req; |
7e07efb1 | 1539 | u32 i; |
bf0d5f50 AE |
1540 | |
1541 | obj_request = container_of(kref, struct rbd_obj_request, kref); | |
1542 | ||
37206ee5 AE |
1543 | dout("%s: obj %p\n", __func__, obj_request); |
1544 | ||
bcbab1db ID |
1545 | while (!list_empty(&obj_request->osd_reqs)) { |
1546 | osd_req = list_first_entry(&obj_request->osd_reqs, | |
1547 | struct ceph_osd_request, r_private_item); | |
1548 | list_del_init(&osd_req->r_private_item); | |
1549 | ceph_osdc_put_request(osd_req); | |
1550 | } | |
bf0d5f50 | 1551 | |
ecc633ca | 1552 | switch (obj_request->img_request->data_type) { |
9969ebc5 | 1553 | case OBJ_REQUEST_NODATA: |
bf0d5f50 | 1554 | case OBJ_REQUEST_BIO: |
7e07efb1 | 1555 | case OBJ_REQUEST_BVECS: |
5359a17d | 1556 | break; /* Nothing to do */ |
afb97888 ID |
1557 | case OBJ_REQUEST_OWN_BVECS: |
1558 | kfree(obj_request->bvec_pos.bvecs); | |
788e2df3 | 1559 | break; |
7e07efb1 | 1560 | default: |
16809372 | 1561 | BUG(); |
bf0d5f50 AE |
1562 | } |
1563 | ||
86bd7998 | 1564 | kfree(obj_request->img_extents); |
7e07efb1 ID |
1565 | if (obj_request->copyup_bvecs) { |
1566 | for (i = 0; i < obj_request->copyup_bvec_count; i++) { | |
1567 | if (obj_request->copyup_bvecs[i].bv_page) | |
1568 | __free_page(obj_request->copyup_bvecs[i].bv_page); | |
1569 | } | |
1570 | kfree(obj_request->copyup_bvecs); | |
bf0d5f50 AE |
1571 | } |
1572 | ||
868311b1 | 1573 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
bf0d5f50 AE |
1574 | } |
1575 | ||
fb65d228 AE |
1576 | /* It's OK to call this for a device with no parent */ |
1577 | ||
1578 | static void rbd_spec_put(struct rbd_spec *spec); | |
1579 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) | |
1580 | { | |
1581 | rbd_dev_remove_parent(rbd_dev); | |
1582 | rbd_spec_put(rbd_dev->parent_spec); | |
1583 | rbd_dev->parent_spec = NULL; | |
1584 | rbd_dev->parent_overlap = 0; | |
1585 | } | |
1586 | ||
a2acd00e AE |
1587 | /* |
1588 | * Parent image reference counting is used to determine when an | |
1589 | * image's parent fields can be safely torn down--after there are no | |
1590 | * more in-flight requests to the parent image. When the last | |
1591 | * reference is dropped, cleaning them up is safe. | |
1592 | */ | |
1593 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) | |
1594 | { | |
1595 | int counter; | |
1596 | ||
1597 | if (!rbd_dev->parent_spec) | |
1598 | return; | |
1599 | ||
1600 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); | |
1601 | if (counter > 0) | |
1602 | return; | |
1603 | ||
1604 | /* Last reference; clean up parent data structures */ | |
1605 | ||
1606 | if (!counter) | |
1607 | rbd_dev_unparent(rbd_dev); | |
1608 | else | |
9584d508 | 1609 | rbd_warn(rbd_dev, "parent reference underflow"); |
a2acd00e AE |
1610 | } |
1611 | ||
1612 | /* | |
1613 | * If an image has a non-zero parent overlap, get a reference to its | |
1614 | * parent. | |
1615 | * | |
1616 | * Returns true if the rbd device has a parent with a non-zero | |
1617 | * overlap and a reference for it was successfully taken, or | |
1618 | * false otherwise. | |
1619 | */ | |
1620 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) | |
1621 | { | |
ae43e9d0 | 1622 | int counter = 0; |
a2acd00e AE |
1623 | |
1624 | if (!rbd_dev->parent_spec) | |
1625 | return false; | |
1626 | ||
ae43e9d0 ID |
1627 | down_read(&rbd_dev->header_rwsem); |
1628 | if (rbd_dev->parent_overlap) | |
1629 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); | |
1630 | up_read(&rbd_dev->header_rwsem); | |
a2acd00e AE |
1631 | |
1632 | if (counter < 0) | |
9584d508 | 1633 | rbd_warn(rbd_dev, "parent reference overflow"); |
a2acd00e | 1634 | |
ae43e9d0 | 1635 | return counter > 0; |
a2acd00e AE |
1636 | } |
1637 | ||
bf0d5f50 AE |
1638 | /* |
1639 | * Caller is responsible for filling in the list of object requests | |
1640 | * that comprises the image request, and the Linux request pointer | |
1641 | * (if there is one). | |
1642 | */ | |
cc344fa1 AE |
1643 | static struct rbd_img_request *rbd_img_request_create( |
1644 | struct rbd_device *rbd_dev, | |
6d2940c8 | 1645 | enum obj_operation_type op_type, |
4e752f0a | 1646 | struct ceph_snap_context *snapc) |
bf0d5f50 AE |
1647 | { |
1648 | struct rbd_img_request *img_request; | |
bf0d5f50 | 1649 | |
a0c5895b | 1650 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
bf0d5f50 AE |
1651 | if (!img_request) |
1652 | return NULL; | |
1653 | ||
bf0d5f50 | 1654 | img_request->rbd_dev = rbd_dev; |
9bb0248d | 1655 | img_request->op_type = op_type; |
9bb0248d | 1656 | if (!rbd_img_is_write(img_request)) |
bf0d5f50 | 1657 | img_request->snap_id = rbd_dev->spec->snap_id; |
9bb0248d ID |
1658 | else |
1659 | img_request->snapc = snapc; | |
1660 | ||
a2acd00e | 1661 | if (rbd_dev_parent_get(rbd_dev)) |
d0b2e944 | 1662 | img_request_layered_set(img_request); |
a0c5895b | 1663 | |
e1fddc8f | 1664 | INIT_LIST_HEAD(&img_request->lock_item); |
43df3d35 | 1665 | INIT_LIST_HEAD(&img_request->object_extents); |
0192ce2e | 1666 | mutex_init(&img_request->state_mutex); |
bf0d5f50 AE |
1667 | kref_init(&img_request->kref); |
1668 | ||
bf0d5f50 AE |
1669 | return img_request; |
1670 | } | |
1671 | ||
1672 | static void rbd_img_request_destroy(struct kref *kref) | |
1673 | { | |
1674 | struct rbd_img_request *img_request; | |
1675 | struct rbd_obj_request *obj_request; | |
1676 | struct rbd_obj_request *next_obj_request; | |
1677 | ||
1678 | img_request = container_of(kref, struct rbd_img_request, kref); | |
1679 | ||
37206ee5 AE |
1680 | dout("%s: img %p\n", __func__, img_request); |
1681 | ||
e1fddc8f | 1682 | WARN_ON(!list_empty(&img_request->lock_item)); |
bf0d5f50 AE |
1683 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
1684 | rbd_img_obj_request_del(img_request, obj_request); | |
1685 | ||
a2acd00e AE |
1686 | if (img_request_layered_test(img_request)) { |
1687 | img_request_layered_clear(img_request); | |
1688 | rbd_dev_parent_put(img_request->rbd_dev); | |
1689 | } | |
1690 | ||
9bb0248d | 1691 | if (rbd_img_is_write(img_request)) |
812164f8 | 1692 | ceph_put_snap_context(img_request->snapc); |
bf0d5f50 | 1693 | |
1c2a9dfe | 1694 | kmem_cache_free(rbd_img_request_cache, img_request); |
bf0d5f50 AE |
1695 | } |
1696 | ||
22e8bd51 ID |
1697 | #define BITS_PER_OBJ 2 |
1698 | #define OBJS_PER_BYTE (BITS_PER_BYTE / BITS_PER_OBJ) | |
1699 | #define OBJ_MASK ((1 << BITS_PER_OBJ) - 1) | |
e93f3152 | 1700 | |
22e8bd51 ID |
1701 | static void __rbd_object_map_index(struct rbd_device *rbd_dev, u64 objno, |
1702 | u64 *index, u8 *shift) | |
1703 | { | |
1704 | u32 off; | |
e93f3152 | 1705 | |
22e8bd51 ID |
1706 | rbd_assert(objno < rbd_dev->object_map_size); |
1707 | *index = div_u64_rem(objno, OBJS_PER_BYTE, &off); | |
1708 | *shift = (OBJS_PER_BYTE - off - 1) * BITS_PER_OBJ; | |
1709 | } | |
e93f3152 | 1710 | |
22e8bd51 ID |
1711 | static u8 __rbd_object_map_get(struct rbd_device *rbd_dev, u64 objno) |
1712 | { | |
1713 | u64 index; | |
1714 | u8 shift; | |
e93f3152 | 1715 | |
22e8bd51 ID |
1716 | lockdep_assert_held(&rbd_dev->object_map_lock); |
1717 | __rbd_object_map_index(rbd_dev, objno, &index, &shift); | |
1718 | return (rbd_dev->object_map[index] >> shift) & OBJ_MASK; | |
e93f3152 AE |
1719 | } |
1720 | ||
22e8bd51 | 1721 | static void __rbd_object_map_set(struct rbd_device *rbd_dev, u64 objno, u8 val) |
e93f3152 | 1722 | { |
22e8bd51 ID |
1723 | u64 index; |
1724 | u8 shift; | |
1725 | u8 *p; | |
e93f3152 | 1726 | |
22e8bd51 ID |
1727 | lockdep_assert_held(&rbd_dev->object_map_lock); |
1728 | rbd_assert(!(val & ~OBJ_MASK)); | |
e93f3152 | 1729 | |
22e8bd51 ID |
1730 | __rbd_object_map_index(rbd_dev, objno, &index, &shift); |
1731 | p = &rbd_dev->object_map[index]; | |
1732 | *p = (*p & ~(OBJ_MASK << shift)) | (val << shift); | |
e93f3152 AE |
1733 | } |
1734 | ||
22e8bd51 | 1735 | static u8 rbd_object_map_get(struct rbd_device *rbd_dev, u64 objno) |
1217857f | 1736 | { |
22e8bd51 ID |
1737 | u8 state; |
1738 | ||
1739 | spin_lock(&rbd_dev->object_map_lock); | |
1740 | state = __rbd_object_map_get(rbd_dev, objno); | |
1741 | spin_unlock(&rbd_dev->object_map_lock); | |
1742 | return state; | |
3da691bf | 1743 | } |
1217857f | 1744 | |
22e8bd51 | 1745 | static bool use_object_map(struct rbd_device *rbd_dev) |
3da691bf | 1746 | { |
3fe69921 ID |
1747 | /* |
1748 | * An image mapped read-only can't use the object map -- it isn't | |
1749 | * loaded because the header lock isn't acquired. Someone else can | |
1750 | * write to the image and update the object map behind our back. | |
1751 | * | |
1752 | * A snapshot can't be written to, so using the object map is always | |
1753 | * safe. | |
1754 | */ | |
1755 | if (!rbd_is_snap(rbd_dev) && rbd_is_ro(rbd_dev)) | |
1756 | return false; | |
1757 | ||
22e8bd51 ID |
1758 | return ((rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP) && |
1759 | !(rbd_dev->object_map_flags & RBD_FLAG_OBJECT_MAP_INVALID)); | |
3da691bf ID |
1760 | } |
1761 | ||
22e8bd51 | 1762 | static bool rbd_object_map_may_exist(struct rbd_device *rbd_dev, u64 objno) |
3da691bf | 1763 | { |
22e8bd51 | 1764 | u8 state; |
8b3e1a56 | 1765 | |
22e8bd51 ID |
1766 | /* fall back to default logic if object map is disabled or invalid */ |
1767 | if (!use_object_map(rbd_dev)) | |
1768 | return true; | |
3da691bf | 1769 | |
22e8bd51 ID |
1770 | state = rbd_object_map_get(rbd_dev, objno); |
1771 | return state != OBJECT_NONEXISTENT; | |
1217857f AE |
1772 | } |
1773 | ||
22e8bd51 ID |
1774 | static void rbd_object_map_name(struct rbd_device *rbd_dev, u64 snap_id, |
1775 | struct ceph_object_id *oid) | |
13488d53 | 1776 | { |
22e8bd51 ID |
1777 | if (snap_id == CEPH_NOSNAP) |
1778 | ceph_oid_printf(oid, "%s%s", RBD_OBJECT_MAP_PREFIX, | |
1779 | rbd_dev->spec->image_id); | |
1780 | else | |
1781 | ceph_oid_printf(oid, "%s%s.%016llx", RBD_OBJECT_MAP_PREFIX, | |
1782 | rbd_dev->spec->image_id, snap_id); | |
13488d53 ID |
1783 | } |
1784 | ||
22e8bd51 | 1785 | static int rbd_object_map_lock(struct rbd_device *rbd_dev) |
2169238d | 1786 | { |
22e8bd51 ID |
1787 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
1788 | CEPH_DEFINE_OID_ONSTACK(oid); | |
1789 | u8 lock_type; | |
1790 | char *lock_tag; | |
1791 | struct ceph_locker *lockers; | |
1792 | u32 num_lockers; | |
1793 | bool broke_lock = false; | |
1794 | int ret; | |
2169238d | 1795 | |
22e8bd51 | 1796 | rbd_object_map_name(rbd_dev, CEPH_NOSNAP, &oid); |
2169238d | 1797 | |
22e8bd51 ID |
1798 | again: |
1799 | ret = ceph_cls_lock(osdc, &oid, &rbd_dev->header_oloc, RBD_LOCK_NAME, | |
1800 | CEPH_CLS_LOCK_EXCLUSIVE, "", "", "", 0); | |
1801 | if (ret != -EBUSY || broke_lock) { | |
1802 | if (ret == -EEXIST) | |
1803 | ret = 0; /* already locked by myself */ | |
1804 | if (ret) | |
1805 | rbd_warn(rbd_dev, "failed to lock object map: %d", ret); | |
1806 | return ret; | |
1807 | } | |
2169238d | 1808 | |
22e8bd51 ID |
1809 | ret = ceph_cls_lock_info(osdc, &oid, &rbd_dev->header_oloc, |
1810 | RBD_LOCK_NAME, &lock_type, &lock_tag, | |
1811 | &lockers, &num_lockers); | |
1812 | if (ret) { | |
1813 | if (ret == -ENOENT) | |
1814 | goto again; | |
3da691bf | 1815 | |
22e8bd51 | 1816 | rbd_warn(rbd_dev, "failed to get object map lockers: %d", ret); |
86bd7998 | 1817 | return ret; |
22e8bd51 | 1818 | } |
86bd7998 | 1819 | |
22e8bd51 ID |
1820 | kfree(lock_tag); |
1821 | if (num_lockers == 0) | |
1822 | goto again; | |
2169238d | 1823 | |
22e8bd51 ID |
1824 | rbd_warn(rbd_dev, "breaking object map lock owned by %s%llu", |
1825 | ENTITY_NAME(lockers[0].id.name)); | |
2169238d | 1826 | |
22e8bd51 ID |
1827 | ret = ceph_cls_break_lock(osdc, &oid, &rbd_dev->header_oloc, |
1828 | RBD_LOCK_NAME, lockers[0].id.cookie, | |
1829 | &lockers[0].id.name); | |
1830 | ceph_free_lockers(lockers, num_lockers); | |
1831 | if (ret) { | |
1832 | if (ret == -ENOENT) | |
1833 | goto again; | |
13488d53 | 1834 | |
22e8bd51 ID |
1835 | rbd_warn(rbd_dev, "failed to break object map lock: %d", ret); |
1836 | return ret; | |
3da691bf ID |
1837 | } |
1838 | ||
22e8bd51 ID |
1839 | broke_lock = true; |
1840 | goto again; | |
2169238d AE |
1841 | } |
1842 | ||
22e8bd51 | 1843 | static void rbd_object_map_unlock(struct rbd_device *rbd_dev) |
6484cbe9 | 1844 | { |
22e8bd51 ID |
1845 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
1846 | CEPH_DEFINE_OID_ONSTACK(oid); | |
1847 | int ret; | |
1848 | ||
1849 | rbd_object_map_name(rbd_dev, CEPH_NOSNAP, &oid); | |
1850 | ||
1851 | ret = ceph_cls_unlock(osdc, &oid, &rbd_dev->header_oloc, RBD_LOCK_NAME, | |
1852 | ""); | |
1853 | if (ret && ret != -ENOENT) | |
1854 | rbd_warn(rbd_dev, "failed to unlock object map: %d", ret); | |
6484cbe9 ID |
1855 | } |
1856 | ||
22e8bd51 | 1857 | static int decode_object_map_header(void **p, void *end, u64 *object_map_size) |
6484cbe9 | 1858 | { |
22e8bd51 ID |
1859 | u8 struct_v; |
1860 | u32 struct_len; | |
1861 | u32 header_len; | |
1862 | void *header_end; | |
6484cbe9 ID |
1863 | int ret; |
1864 | ||
22e8bd51 ID |
1865 | ceph_decode_32_safe(p, end, header_len, e_inval); |
1866 | header_end = *p + header_len; | |
0c93e1b7 | 1867 | |
22e8bd51 ID |
1868 | ret = ceph_start_decoding(p, end, 1, "BitVector header", &struct_v, |
1869 | &struct_len); | |
6484cbe9 ID |
1870 | if (ret) |
1871 | return ret; | |
1872 | ||
22e8bd51 | 1873 | ceph_decode_64_safe(p, end, *object_map_size, e_inval); |
6484cbe9 | 1874 | |
22e8bd51 | 1875 | *p = header_end; |
6484cbe9 | 1876 | return 0; |
22e8bd51 ID |
1877 | |
1878 | e_inval: | |
1879 | return -EINVAL; | |
6484cbe9 ID |
1880 | } |
1881 | ||
22e8bd51 | 1882 | static int __rbd_object_map_load(struct rbd_device *rbd_dev) |
13488d53 | 1883 | { |
22e8bd51 ID |
1884 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
1885 | CEPH_DEFINE_OID_ONSTACK(oid); | |
1886 | struct page **pages; | |
1887 | void *p, *end; | |
1888 | size_t reply_len; | |
1889 | u64 num_objects; | |
1890 | u64 object_map_bytes; | |
1891 | u64 object_map_size; | |
1892 | int num_pages; | |
1893 | int ret; | |
13488d53 | 1894 | |
22e8bd51 | 1895 | rbd_assert(!rbd_dev->object_map && !rbd_dev->object_map_size); |
13488d53 | 1896 | |
22e8bd51 ID |
1897 | num_objects = ceph_get_num_objects(&rbd_dev->layout, |
1898 | rbd_dev->mapping.size); | |
1899 | object_map_bytes = DIV_ROUND_UP_ULL(num_objects * BITS_PER_OBJ, | |
1900 | BITS_PER_BYTE); | |
1901 | num_pages = calc_pages_for(0, object_map_bytes) + 1; | |
1902 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); | |
1903 | if (IS_ERR(pages)) | |
1904 | return PTR_ERR(pages); | |
13488d53 | 1905 | |
22e8bd51 ID |
1906 | reply_len = num_pages * PAGE_SIZE; |
1907 | rbd_object_map_name(rbd_dev, rbd_dev->spec->snap_id, &oid); | |
1908 | ret = ceph_osdc_call(osdc, &oid, &rbd_dev->header_oloc, | |
1909 | "rbd", "object_map_load", CEPH_OSD_FLAG_READ, | |
1910 | NULL, 0, pages, &reply_len); | |
1911 | if (ret) | |
1912 | goto out; | |
3b434a2a | 1913 | |
22e8bd51 ID |
1914 | p = page_address(pages[0]); |
1915 | end = p + min(reply_len, (size_t)PAGE_SIZE); | |
1916 | ret = decode_object_map_header(&p, end, &object_map_size); | |
1917 | if (ret) | |
1918 | goto out; | |
1919 | ||
1920 | if (object_map_size != num_objects) { | |
1921 | rbd_warn(rbd_dev, "object map size mismatch: %llu vs %llu", | |
1922 | object_map_size, num_objects); | |
1923 | ret = -EINVAL; | |
1924 | goto out; | |
3b434a2a JD |
1925 | } |
1926 | ||
22e8bd51 ID |
1927 | if (offset_in_page(p) + object_map_bytes > reply_len) { |
1928 | ret = -EINVAL; | |
1929 | goto out; | |
1930 | } | |
1931 | ||
1932 | rbd_dev->object_map = kvmalloc(object_map_bytes, GFP_KERNEL); | |
1933 | if (!rbd_dev->object_map) { | |
1934 | ret = -ENOMEM; | |
1935 | goto out; | |
1936 | } | |
1937 | ||
1938 | rbd_dev->object_map_size = object_map_size; | |
1939 | ceph_copy_from_page_vector(pages, rbd_dev->object_map, | |
1940 | offset_in_page(p), object_map_bytes); | |
1941 | ||
1942 | out: | |
1943 | ceph_release_page_vector(pages, num_pages); | |
1944 | return ret; | |
1945 | } | |
3da691bf | 1946 | |
22e8bd51 ID |
1947 | static void rbd_object_map_free(struct rbd_device *rbd_dev) |
1948 | { | |
1949 | kvfree(rbd_dev->object_map); | |
1950 | rbd_dev->object_map = NULL; | |
1951 | rbd_dev->object_map_size = 0; | |
3b434a2a JD |
1952 | } |
1953 | ||
22e8bd51 | 1954 | static int rbd_object_map_load(struct rbd_device *rbd_dev) |
bf0d5f50 | 1955 | { |
3da691bf | 1956 | int ret; |
37206ee5 | 1957 | |
22e8bd51 | 1958 | ret = __rbd_object_map_load(rbd_dev); |
86bd7998 ID |
1959 | if (ret) |
1960 | return ret; | |
f1a4739f | 1961 | |
22e8bd51 ID |
1962 | ret = rbd_dev_v2_get_flags(rbd_dev); |
1963 | if (ret) { | |
1964 | rbd_object_map_free(rbd_dev); | |
1965 | return ret; | |
1966 | } | |
1967 | ||
1968 | if (rbd_dev->object_map_flags & RBD_FLAG_OBJECT_MAP_INVALID) | |
1969 | rbd_warn(rbd_dev, "object map is invalid"); | |
1970 | ||
1971 | return 0; | |
1972 | } | |
1973 | ||
1974 | static int rbd_object_map_open(struct rbd_device *rbd_dev) | |
1975 | { | |
1976 | int ret; | |
1977 | ||
1978 | ret = rbd_object_map_lock(rbd_dev); | |
1979 | if (ret) | |
1980 | return ret; | |
1981 | ||
1982 | ret = rbd_object_map_load(rbd_dev); | |
1983 | if (ret) { | |
1984 | rbd_object_map_unlock(rbd_dev); | |
1985 | return ret; | |
1986 | } | |
1987 | ||
1988 | return 0; | |
1989 | } | |
1990 | ||
1991 | static void rbd_object_map_close(struct rbd_device *rbd_dev) | |
1992 | { | |
1993 | rbd_object_map_free(rbd_dev); | |
1994 | rbd_object_map_unlock(rbd_dev); | |
1995 | } | |
1996 | ||
1997 | /* | |
1998 | * This function needs snap_id (or more precisely just something to | |
1999 | * distinguish between HEAD and snapshot object maps), new_state and | |
2000 | * current_state that were passed to rbd_object_map_update(). | |
2001 | * | |
2002 | * To avoid allocating and stashing a context we piggyback on the OSD | |
2003 | * request. A HEAD update has two ops (assert_locked). For new_state | |
2004 | * and current_state we decode our own object_map_update op, encoded in | |
2005 | * rbd_cls_object_map_update(). | |
2006 | */ | |
2007 | static int rbd_object_map_update_finish(struct rbd_obj_request *obj_req, | |
2008 | struct ceph_osd_request *osd_req) | |
2009 | { | |
2010 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; | |
2011 | struct ceph_osd_data *osd_data; | |
2012 | u64 objno; | |
633739b2 | 2013 | u8 state, new_state, uninitialized_var(current_state); |
22e8bd51 ID |
2014 | bool has_current_state; |
2015 | void *p; | |
2016 | ||
2017 | if (osd_req->r_result) | |
2018 | return osd_req->r_result; | |
2019 | ||
2020 | /* | |
2021 | * Nothing to do for a snapshot object map. | |
2022 | */ | |
2023 | if (osd_req->r_num_ops == 1) | |
2024 | return 0; | |
2025 | ||
2026 | /* | |
2027 | * Update in-memory HEAD object map. | |
2028 | */ | |
2029 | rbd_assert(osd_req->r_num_ops == 2); | |
2030 | osd_data = osd_req_op_data(osd_req, 1, cls, request_data); | |
2031 | rbd_assert(osd_data->type == CEPH_OSD_DATA_TYPE_PAGES); | |
2032 | ||
2033 | p = page_address(osd_data->pages[0]); | |
2034 | objno = ceph_decode_64(&p); | |
2035 | rbd_assert(objno == obj_req->ex.oe_objno); | |
2036 | rbd_assert(ceph_decode_64(&p) == objno + 1); | |
2037 | new_state = ceph_decode_8(&p); | |
2038 | has_current_state = ceph_decode_8(&p); | |
2039 | if (has_current_state) | |
2040 | current_state = ceph_decode_8(&p); | |
2041 | ||
2042 | spin_lock(&rbd_dev->object_map_lock); | |
2043 | state = __rbd_object_map_get(rbd_dev, objno); | |
2044 | if (!has_current_state || current_state == state || | |
2045 | (current_state == OBJECT_EXISTS && state == OBJECT_EXISTS_CLEAN)) | |
2046 | __rbd_object_map_set(rbd_dev, objno, new_state); | |
2047 | spin_unlock(&rbd_dev->object_map_lock); | |
2048 | ||
2049 | return 0; | |
2050 | } | |
2051 | ||
2052 | static void rbd_object_map_callback(struct ceph_osd_request *osd_req) | |
2053 | { | |
2054 | struct rbd_obj_request *obj_req = osd_req->r_priv; | |
2055 | int result; | |
2056 | ||
2057 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, | |
2058 | osd_req->r_result, obj_req); | |
2059 | ||
2060 | result = rbd_object_map_update_finish(obj_req, osd_req); | |
2061 | rbd_obj_handle_request(obj_req, result); | |
2062 | } | |
2063 | ||
2064 | static bool update_needed(struct rbd_device *rbd_dev, u64 objno, u8 new_state) | |
2065 | { | |
2066 | u8 state = rbd_object_map_get(rbd_dev, objno); | |
bf0d5f50 | 2067 | |
22e8bd51 ID |
2068 | if (state == new_state || |
2069 | (new_state == OBJECT_PENDING && state == OBJECT_NONEXISTENT) || | |
2070 | (new_state == OBJECT_NONEXISTENT && state != OBJECT_PENDING)) | |
2071 | return false; | |
2072 | ||
2073 | return true; | |
2074 | } | |
2075 | ||
2076 | static int rbd_cls_object_map_update(struct ceph_osd_request *req, | |
2077 | int which, u64 objno, u8 new_state, | |
2078 | const u8 *current_state) | |
2079 | { | |
2080 | struct page **pages; | |
2081 | void *p, *start; | |
2082 | int ret; | |
2083 | ||
2084 | ret = osd_req_op_cls_init(req, which, "rbd", "object_map_update"); | |
2085 | if (ret) | |
2086 | return ret; | |
2087 | ||
2088 | pages = ceph_alloc_page_vector(1, GFP_NOIO); | |
2089 | if (IS_ERR(pages)) | |
2090 | return PTR_ERR(pages); | |
2091 | ||
2092 | p = start = page_address(pages[0]); | |
2093 | ceph_encode_64(&p, objno); | |
2094 | ceph_encode_64(&p, objno + 1); | |
2095 | ceph_encode_8(&p, new_state); | |
2096 | if (current_state) { | |
2097 | ceph_encode_8(&p, 1); | |
2098 | ceph_encode_8(&p, *current_state); | |
2099 | } else { | |
2100 | ceph_encode_8(&p, 0); | |
2101 | } | |
2102 | ||
2103 | osd_req_op_cls_request_data_pages(req, which, pages, p - start, 0, | |
2104 | false, true); | |
2105 | return 0; | |
2106 | } | |
2107 | ||
2108 | /* | |
2109 | * Return: | |
2110 | * 0 - object map update sent | |
2111 | * 1 - object map update isn't needed | |
2112 | * <0 - error | |
2113 | */ | |
2114 | static int rbd_object_map_update(struct rbd_obj_request *obj_req, u64 snap_id, | |
2115 | u8 new_state, const u8 *current_state) | |
2116 | { | |
2117 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; | |
2118 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
2119 | struct ceph_osd_request *req; | |
2120 | int num_ops = 1; | |
2121 | int which = 0; | |
2122 | int ret; | |
2123 | ||
2124 | if (snap_id == CEPH_NOSNAP) { | |
2125 | if (!update_needed(rbd_dev, obj_req->ex.oe_objno, new_state)) | |
2126 | return 1; | |
2127 | ||
2128 | num_ops++; /* assert_locked */ | |
2129 | } | |
2130 | ||
2131 | req = ceph_osdc_alloc_request(osdc, NULL, num_ops, false, GFP_NOIO); | |
2132 | if (!req) | |
2133 | return -ENOMEM; | |
2134 | ||
2135 | list_add_tail(&req->r_private_item, &obj_req->osd_reqs); | |
2136 | req->r_callback = rbd_object_map_callback; | |
2137 | req->r_priv = obj_req; | |
2138 | ||
2139 | rbd_object_map_name(rbd_dev, snap_id, &req->r_base_oid); | |
2140 | ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc); | |
2141 | req->r_flags = CEPH_OSD_FLAG_WRITE; | |
2142 | ktime_get_real_ts64(&req->r_mtime); | |
2143 | ||
2144 | if (snap_id == CEPH_NOSNAP) { | |
2145 | /* | |
2146 | * Protect against possible race conditions during lock | |
2147 | * ownership transitions. | |
2148 | */ | |
2149 | ret = ceph_cls_assert_locked(req, which++, RBD_LOCK_NAME, | |
2150 | CEPH_CLS_LOCK_EXCLUSIVE, "", ""); | |
3da691bf ID |
2151 | if (ret) |
2152 | return ret; | |
22e8bd51 ID |
2153 | } |
2154 | ||
2155 | ret = rbd_cls_object_map_update(req, which, obj_req->ex.oe_objno, | |
2156 | new_state, current_state); | |
2157 | if (ret) | |
2158 | return ret; | |
2159 | ||
2160 | ret = ceph_osdc_alloc_messages(req, GFP_NOIO); | |
2161 | if (ret) | |
2162 | return ret; | |
13488d53 | 2163 | |
22e8bd51 ID |
2164 | ceph_osdc_start_request(osdc, req, false); |
2165 | return 0; | |
2166 | } | |
2167 | ||
86bd7998 ID |
2168 | static void prune_extents(struct ceph_file_extent *img_extents, |
2169 | u32 *num_img_extents, u64 overlap) | |
e93f3152 | 2170 | { |
86bd7998 | 2171 | u32 cnt = *num_img_extents; |
e93f3152 | 2172 | |
86bd7998 ID |
2173 | /* drop extents completely beyond the overlap */ |
2174 | while (cnt && img_extents[cnt - 1].fe_off >= overlap) | |
2175 | cnt--; | |
e93f3152 | 2176 | |
86bd7998 ID |
2177 | if (cnt) { |
2178 | struct ceph_file_extent *ex = &img_extents[cnt - 1]; | |
e93f3152 | 2179 | |
86bd7998 ID |
2180 | /* trim final overlapping extent */ |
2181 | if (ex->fe_off + ex->fe_len > overlap) | |
2182 | ex->fe_len = overlap - ex->fe_off; | |
2183 | } | |
e93f3152 | 2184 | |
86bd7998 | 2185 | *num_img_extents = cnt; |
e93f3152 AE |
2186 | } |
2187 | ||
86bd7998 ID |
2188 | /* |
2189 | * Determine the byte range(s) covered by either just the object extent | |
2190 | * or the entire object in the parent image. | |
2191 | */ | |
2192 | static int rbd_obj_calc_img_extents(struct rbd_obj_request *obj_req, | |
2193 | bool entire) | |
e93f3152 | 2194 | { |
86bd7998 ID |
2195 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
2196 | int ret; | |
e93f3152 | 2197 | |
86bd7998 ID |
2198 | if (!rbd_dev->parent_overlap) |
2199 | return 0; | |
e93f3152 | 2200 | |
86bd7998 ID |
2201 | ret = ceph_extent_to_file(&rbd_dev->layout, obj_req->ex.oe_objno, |
2202 | entire ? 0 : obj_req->ex.oe_off, | |
2203 | entire ? rbd_dev->layout.object_size : | |
2204 | obj_req->ex.oe_len, | |
2205 | &obj_req->img_extents, | |
2206 | &obj_req->num_img_extents); | |
2207 | if (ret) | |
2208 | return ret; | |
e93f3152 | 2209 | |
86bd7998 ID |
2210 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
2211 | rbd_dev->parent_overlap); | |
2212 | return 0; | |
e93f3152 AE |
2213 | } |
2214 | ||
bcbab1db | 2215 | static void rbd_osd_setup_data(struct ceph_osd_request *osd_req, int which) |
1217857f | 2216 | { |
bcbab1db ID |
2217 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
2218 | ||
ecc633ca | 2219 | switch (obj_req->img_request->data_type) { |
3da691bf | 2220 | case OBJ_REQUEST_BIO: |
bcbab1db | 2221 | osd_req_op_extent_osd_data_bio(osd_req, which, |
3da691bf | 2222 | &obj_req->bio_pos, |
43df3d35 | 2223 | obj_req->ex.oe_len); |
3da691bf ID |
2224 | break; |
2225 | case OBJ_REQUEST_BVECS: | |
afb97888 | 2226 | case OBJ_REQUEST_OWN_BVECS: |
3da691bf | 2227 | rbd_assert(obj_req->bvec_pos.iter.bi_size == |
43df3d35 | 2228 | obj_req->ex.oe_len); |
afb97888 | 2229 | rbd_assert(obj_req->bvec_idx == obj_req->bvec_count); |
bcbab1db | 2230 | osd_req_op_extent_osd_data_bvec_pos(osd_req, which, |
3da691bf ID |
2231 | &obj_req->bvec_pos); |
2232 | break; | |
2233 | default: | |
16809372 | 2234 | BUG(); |
1217857f | 2235 | } |
3da691bf | 2236 | } |
1217857f | 2237 | |
bcbab1db | 2238 | static int rbd_osd_setup_stat(struct ceph_osd_request *osd_req, int which) |
3da691bf ID |
2239 | { |
2240 | struct page **pages; | |
8b3e1a56 | 2241 | |
3da691bf ID |
2242 | /* |
2243 | * The response data for a STAT call consists of: | |
2244 | * le64 length; | |
2245 | * struct { | |
2246 | * le32 tv_sec; | |
2247 | * le32 tv_nsec; | |
2248 | * } mtime; | |
2249 | */ | |
2250 | pages = ceph_alloc_page_vector(1, GFP_NOIO); | |
2251 | if (IS_ERR(pages)) | |
2252 | return PTR_ERR(pages); | |
2253 | ||
bcbab1db ID |
2254 | osd_req_op_init(osd_req, which, CEPH_OSD_OP_STAT, 0); |
2255 | osd_req_op_raw_data_in_pages(osd_req, which, pages, | |
3da691bf ID |
2256 | 8 + sizeof(struct ceph_timespec), |
2257 | 0, false, true); | |
2258 | return 0; | |
1217857f AE |
2259 | } |
2260 | ||
b5ae8cbc ID |
2261 | static int rbd_osd_setup_copyup(struct ceph_osd_request *osd_req, int which, |
2262 | u32 bytes) | |
2263 | { | |
2264 | struct rbd_obj_request *obj_req = osd_req->r_priv; | |
2265 | int ret; | |
2266 | ||
2267 | ret = osd_req_op_cls_init(osd_req, which, "rbd", "copyup"); | |
2268 | if (ret) | |
2269 | return ret; | |
2270 | ||
2271 | osd_req_op_cls_request_data_bvecs(osd_req, which, obj_req->copyup_bvecs, | |
2272 | obj_req->copyup_bvec_count, bytes); | |
2273 | return 0; | |
2274 | } | |
2275 | ||
ea9b743c ID |
2276 | static int rbd_obj_init_read(struct rbd_obj_request *obj_req) |
2277 | { | |
2278 | obj_req->read_state = RBD_OBJ_READ_START; | |
2279 | return 0; | |
2280 | } | |
2281 | ||
bcbab1db ID |
2282 | static void __rbd_osd_setup_write_ops(struct ceph_osd_request *osd_req, |
2283 | int which) | |
2169238d | 2284 | { |
bcbab1db | 2285 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
3da691bf ID |
2286 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
2287 | u16 opcode; | |
2169238d | 2288 | |
8b5bec5c ID |
2289 | if (!use_object_map(rbd_dev) || |
2290 | !(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST)) { | |
2291 | osd_req_op_alloc_hint_init(osd_req, which++, | |
2292 | rbd_dev->layout.object_size, | |
2293 | rbd_dev->layout.object_size); | |
2294 | } | |
2169238d | 2295 | |
3da691bf ID |
2296 | if (rbd_obj_is_entire(obj_req)) |
2297 | opcode = CEPH_OSD_OP_WRITEFULL; | |
2298 | else | |
2299 | opcode = CEPH_OSD_OP_WRITE; | |
2169238d | 2300 | |
bcbab1db | 2301 | osd_req_op_extent_init(osd_req, which, opcode, |
43df3d35 | 2302 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
bcbab1db | 2303 | rbd_osd_setup_data(osd_req, which); |
3da691bf | 2304 | } |
2169238d | 2305 | |
ea9b743c | 2306 | static int rbd_obj_init_write(struct rbd_obj_request *obj_req) |
3da691bf | 2307 | { |
3da691bf ID |
2308 | int ret; |
2309 | ||
86bd7998 ID |
2310 | /* reverse map the entire object onto the parent */ |
2311 | ret = rbd_obj_calc_img_extents(obj_req, true); | |
2312 | if (ret) | |
2313 | return ret; | |
2314 | ||
0ad5d953 ID |
2315 | if (rbd_obj_copyup_enabled(obj_req)) |
2316 | obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ENABLED; | |
2317 | ||
85b5e6d1 | 2318 | obj_req->write_state = RBD_OBJ_WRITE_START; |
3da691bf | 2319 | return 0; |
2169238d AE |
2320 | } |
2321 | ||
6484cbe9 ID |
2322 | static u16 truncate_or_zero_opcode(struct rbd_obj_request *obj_req) |
2323 | { | |
2324 | return rbd_obj_is_tail(obj_req) ? CEPH_OSD_OP_TRUNCATE : | |
2325 | CEPH_OSD_OP_ZERO; | |
2326 | } | |
2327 | ||
27bbd911 ID |
2328 | static void __rbd_osd_setup_discard_ops(struct ceph_osd_request *osd_req, |
2329 | int which) | |
2330 | { | |
2331 | struct rbd_obj_request *obj_req = osd_req->r_priv; | |
2332 | ||
2333 | if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents) { | |
2334 | rbd_assert(obj_req->flags & RBD_OBJ_FLAG_DELETION); | |
2335 | osd_req_op_init(osd_req, which, CEPH_OSD_OP_DELETE, 0); | |
13488d53 | 2336 | } else { |
27bbd911 ID |
2337 | osd_req_op_extent_init(osd_req, which, |
2338 | truncate_or_zero_opcode(obj_req), | |
2339 | obj_req->ex.oe_off, obj_req->ex.oe_len, | |
2340 | 0, 0); | |
2341 | } | |
2342 | } | |
2343 | ||
ea9b743c | 2344 | static int rbd_obj_init_discard(struct rbd_obj_request *obj_req) |
6484cbe9 | 2345 | { |
0c93e1b7 | 2346 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
27bbd911 | 2347 | u64 off, next_off; |
6484cbe9 ID |
2348 | int ret; |
2349 | ||
0c93e1b7 ID |
2350 | /* |
2351 | * Align the range to alloc_size boundary and punt on discards | |
2352 | * that are too small to free up any space. | |
2353 | * | |
2354 | * alloc_size == object_size && is_tail() is a special case for | |
2355 | * filestore with filestore_punch_hole = false, needed to allow | |
2356 | * truncate (in addition to delete). | |
2357 | */ | |
2358 | if (rbd_dev->opts->alloc_size != rbd_dev->layout.object_size || | |
2359 | !rbd_obj_is_tail(obj_req)) { | |
27bbd911 ID |
2360 | off = round_up(obj_req->ex.oe_off, rbd_dev->opts->alloc_size); |
2361 | next_off = round_down(obj_req->ex.oe_off + obj_req->ex.oe_len, | |
2362 | rbd_dev->opts->alloc_size); | |
0c93e1b7 ID |
2363 | if (off >= next_off) |
2364 | return 1; | |
27bbd911 ID |
2365 | |
2366 | dout("%s %p %llu~%llu -> %llu~%llu\n", __func__, | |
2367 | obj_req, obj_req->ex.oe_off, obj_req->ex.oe_len, | |
2368 | off, next_off - off); | |
2369 | obj_req->ex.oe_off = off; | |
2370 | obj_req->ex.oe_len = next_off - off; | |
0c93e1b7 ID |
2371 | } |
2372 | ||
6484cbe9 ID |
2373 | /* reverse map the entire object onto the parent */ |
2374 | ret = rbd_obj_calc_img_extents(obj_req, true); | |
2375 | if (ret) | |
2376 | return ret; | |
2377 | ||
22e8bd51 | 2378 | obj_req->flags |= RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT; |
0ad5d953 ID |
2379 | if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents) |
2380 | obj_req->flags |= RBD_OBJ_FLAG_DELETION; | |
2381 | ||
85b5e6d1 | 2382 | obj_req->write_state = RBD_OBJ_WRITE_START; |
6484cbe9 ID |
2383 | return 0; |
2384 | } | |
2385 | ||
bcbab1db ID |
2386 | static void __rbd_osd_setup_zeroout_ops(struct ceph_osd_request *osd_req, |
2387 | int which) | |
3da691bf | 2388 | { |
bcbab1db | 2389 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
3b434a2a JD |
2390 | u16 opcode; |
2391 | ||
3da691bf | 2392 | if (rbd_obj_is_entire(obj_req)) { |
86bd7998 | 2393 | if (obj_req->num_img_extents) { |
0ad5d953 | 2394 | if (!(obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED)) |
bcbab1db | 2395 | osd_req_op_init(osd_req, which++, |
9b17eb2c | 2396 | CEPH_OSD_OP_CREATE, 0); |
3b434a2a JD |
2397 | opcode = CEPH_OSD_OP_TRUNCATE; |
2398 | } else { | |
0ad5d953 | 2399 | rbd_assert(obj_req->flags & RBD_OBJ_FLAG_DELETION); |
bcbab1db | 2400 | osd_req_op_init(osd_req, which++, |
3da691bf ID |
2401 | CEPH_OSD_OP_DELETE, 0); |
2402 | opcode = 0; | |
3b434a2a | 2403 | } |
3b434a2a | 2404 | } else { |
6484cbe9 | 2405 | opcode = truncate_or_zero_opcode(obj_req); |
3b434a2a JD |
2406 | } |
2407 | ||
3da691bf | 2408 | if (opcode) |
bcbab1db | 2409 | osd_req_op_extent_init(osd_req, which, opcode, |
43df3d35 | 2410 | obj_req->ex.oe_off, obj_req->ex.oe_len, |
3da691bf | 2411 | 0, 0); |
3b434a2a JD |
2412 | } |
2413 | ||
ea9b743c | 2414 | static int rbd_obj_init_zeroout(struct rbd_obj_request *obj_req) |
bf0d5f50 | 2415 | { |
3da691bf | 2416 | int ret; |
37206ee5 | 2417 | |
86bd7998 ID |
2418 | /* reverse map the entire object onto the parent */ |
2419 | ret = rbd_obj_calc_img_extents(obj_req, true); | |
2420 | if (ret) | |
2421 | return ret; | |
f1a4739f | 2422 | |
0ad5d953 ID |
2423 | if (rbd_obj_copyup_enabled(obj_req)) |
2424 | obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ENABLED; | |
2425 | if (!obj_req->num_img_extents) { | |
22e8bd51 | 2426 | obj_req->flags |= RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT; |
0ad5d953 ID |
2427 | if (rbd_obj_is_entire(obj_req)) |
2428 | obj_req->flags |= RBD_OBJ_FLAG_DELETION; | |
3da691bf | 2429 | } |
3b434a2a | 2430 | |
a086a1b8 | 2431 | obj_req->write_state = RBD_OBJ_WRITE_START; |
3da691bf ID |
2432 | return 0; |
2433 | } | |
9d4df01f | 2434 | |
a086a1b8 ID |
2435 | static int count_write_ops(struct rbd_obj_request *obj_req) |
2436 | { | |
8b5bec5c ID |
2437 | struct rbd_img_request *img_req = obj_req->img_request; |
2438 | ||
2439 | switch (img_req->op_type) { | |
a086a1b8 | 2440 | case OBJ_OP_WRITE: |
8b5bec5c ID |
2441 | if (!use_object_map(img_req->rbd_dev) || |
2442 | !(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST)) | |
2443 | return 2; /* setallochint + write/writefull */ | |
2444 | ||
2445 | return 1; /* write/writefull */ | |
a086a1b8 ID |
2446 | case OBJ_OP_DISCARD: |
2447 | return 1; /* delete/truncate/zero */ | |
2448 | case OBJ_OP_ZEROOUT: | |
2449 | if (rbd_obj_is_entire(obj_req) && obj_req->num_img_extents && | |
2450 | !(obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED)) | |
2451 | return 2; /* create + truncate */ | |
bf0d5f50 | 2452 | |
a086a1b8 ID |
2453 | return 1; /* delete/truncate/zero */ |
2454 | default: | |
2455 | BUG(); | |
3da691bf | 2456 | } |
a086a1b8 | 2457 | } |
3b434a2a | 2458 | |
a086a1b8 ID |
2459 | static void rbd_osd_setup_write_ops(struct ceph_osd_request *osd_req, |
2460 | int which) | |
2461 | { | |
2462 | struct rbd_obj_request *obj_req = osd_req->r_priv; | |
2463 | ||
2464 | switch (obj_req->img_request->op_type) { | |
2465 | case OBJ_OP_WRITE: | |
2466 | __rbd_osd_setup_write_ops(osd_req, which); | |
2467 | break; | |
2468 | case OBJ_OP_DISCARD: | |
2469 | __rbd_osd_setup_discard_ops(osd_req, which); | |
2470 | break; | |
2471 | case OBJ_OP_ZEROOUT: | |
2472 | __rbd_osd_setup_zeroout_ops(osd_req, which); | |
2473 | break; | |
2474 | default: | |
2475 | BUG(); | |
2476 | } | |
3da691bf | 2477 | } |
9d4df01f | 2478 | |
3da691bf | 2479 | /* |
a086a1b8 ID |
2480 | * Prune the list of object requests (adjust offset and/or length, drop |
2481 | * redundant requests). Prepare object request state machines and image | |
2482 | * request state machine for execution. | |
3da691bf ID |
2483 | */ |
2484 | static int __rbd_img_fill_request(struct rbd_img_request *img_req) | |
2485 | { | |
0c93e1b7 | 2486 | struct rbd_obj_request *obj_req, *next_obj_req; |
3da691bf | 2487 | int ret; |
430c28c3 | 2488 | |
0c93e1b7 | 2489 | for_each_obj_request_safe(img_req, obj_req, next_obj_req) { |
9bb0248d | 2490 | switch (img_req->op_type) { |
3da691bf | 2491 | case OBJ_OP_READ: |
ea9b743c | 2492 | ret = rbd_obj_init_read(obj_req); |
3da691bf ID |
2493 | break; |
2494 | case OBJ_OP_WRITE: | |
ea9b743c | 2495 | ret = rbd_obj_init_write(obj_req); |
3da691bf ID |
2496 | break; |
2497 | case OBJ_OP_DISCARD: | |
ea9b743c | 2498 | ret = rbd_obj_init_discard(obj_req); |
3da691bf | 2499 | break; |
6484cbe9 | 2500 | case OBJ_OP_ZEROOUT: |
ea9b743c | 2501 | ret = rbd_obj_init_zeroout(obj_req); |
6484cbe9 | 2502 | break; |
3da691bf | 2503 | default: |
16809372 | 2504 | BUG(); |
3da691bf | 2505 | } |
0c93e1b7 | 2506 | if (ret < 0) |
3da691bf | 2507 | return ret; |
0c93e1b7 | 2508 | if (ret > 0) { |
0c93e1b7 ID |
2509 | rbd_img_obj_request_del(img_req, obj_req); |
2510 | continue; | |
2511 | } | |
bf0d5f50 AE |
2512 | } |
2513 | ||
0192ce2e | 2514 | img_req->state = RBD_IMG_START; |
bf0d5f50 | 2515 | return 0; |
3da691bf | 2516 | } |
bf0d5f50 | 2517 | |
5a237819 ID |
2518 | union rbd_img_fill_iter { |
2519 | struct ceph_bio_iter bio_iter; | |
2520 | struct ceph_bvec_iter bvec_iter; | |
2521 | }; | |
bf0d5f50 | 2522 | |
5a237819 ID |
2523 | struct rbd_img_fill_ctx { |
2524 | enum obj_request_type pos_type; | |
2525 | union rbd_img_fill_iter *pos; | |
2526 | union rbd_img_fill_iter iter; | |
2527 | ceph_object_extent_fn_t set_pos_fn; | |
afb97888 ID |
2528 | ceph_object_extent_fn_t count_fn; |
2529 | ceph_object_extent_fn_t copy_fn; | |
5a237819 | 2530 | }; |
bf0d5f50 | 2531 | |
5a237819 | 2532 | static struct ceph_object_extent *alloc_object_extent(void *arg) |
0eefd470 | 2533 | { |
5a237819 ID |
2534 | struct rbd_img_request *img_req = arg; |
2535 | struct rbd_obj_request *obj_req; | |
0eefd470 | 2536 | |
5a237819 ID |
2537 | obj_req = rbd_obj_request_create(); |
2538 | if (!obj_req) | |
2539 | return NULL; | |
2761713d | 2540 | |
5a237819 ID |
2541 | rbd_img_obj_request_add(img_req, obj_req); |
2542 | return &obj_req->ex; | |
2543 | } | |
0eefd470 | 2544 | |
afb97888 ID |
2545 | /* |
2546 | * While su != os && sc == 1 is technically not fancy (it's the same | |
2547 | * layout as su == os && sc == 1), we can't use the nocopy path for it | |
2548 | * because ->set_pos_fn() should be called only once per object. | |
2549 | * ceph_file_to_extents() invokes action_fn once per stripe unit, so | |
2550 | * treat su != os && sc == 1 as fancy. | |
2551 | */ | |
2552 | static bool rbd_layout_is_fancy(struct ceph_file_layout *l) | |
2553 | { | |
2554 | return l->stripe_unit != l->object_size; | |
2555 | } | |
0eefd470 | 2556 | |
afb97888 ID |
2557 | static int rbd_img_fill_request_nocopy(struct rbd_img_request *img_req, |
2558 | struct ceph_file_extent *img_extents, | |
2559 | u32 num_img_extents, | |
2560 | struct rbd_img_fill_ctx *fctx) | |
2561 | { | |
2562 | u32 i; | |
2563 | int ret; | |
2564 | ||
2565 | img_req->data_type = fctx->pos_type; | |
0eefd470 AE |
2566 | |
2567 | /* | |
afb97888 ID |
2568 | * Create object requests and set each object request's starting |
2569 | * position in the provided bio (list) or bio_vec array. | |
0eefd470 | 2570 | */ |
afb97888 ID |
2571 | fctx->iter = *fctx->pos; |
2572 | for (i = 0; i < num_img_extents; i++) { | |
2573 | ret = ceph_file_to_extents(&img_req->rbd_dev->layout, | |
2574 | img_extents[i].fe_off, | |
2575 | img_extents[i].fe_len, | |
2576 | &img_req->object_extents, | |
2577 | alloc_object_extent, img_req, | |
2578 | fctx->set_pos_fn, &fctx->iter); | |
2579 | if (ret) | |
2580 | return ret; | |
2581 | } | |
0eefd470 | 2582 | |
afb97888 | 2583 | return __rbd_img_fill_request(img_req); |
0eefd470 AE |
2584 | } |
2585 | ||
5a237819 ID |
2586 | /* |
2587 | * Map a list of image extents to a list of object extents, create the | |
2588 | * corresponding object requests (normally each to a different object, | |
2589 | * but not always) and add them to @img_req. For each object request, | |
afb97888 | 2590 | * set up its data descriptor to point to the corresponding chunk(s) of |
5a237819 ID |
2591 | * @fctx->pos data buffer. |
2592 | * | |
afb97888 ID |
2593 | * Because ceph_file_to_extents() will merge adjacent object extents |
2594 | * together, each object request's data descriptor may point to multiple | |
2595 | * different chunks of @fctx->pos data buffer. | |
2596 | * | |
5a237819 ID |
2597 | * @fctx->pos data buffer is assumed to be large enough. |
2598 | */ | |
2599 | static int rbd_img_fill_request(struct rbd_img_request *img_req, | |
2600 | struct ceph_file_extent *img_extents, | |
2601 | u32 num_img_extents, | |
2602 | struct rbd_img_fill_ctx *fctx) | |
3d7efd18 | 2603 | { |
afb97888 ID |
2604 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
2605 | struct rbd_obj_request *obj_req; | |
5a237819 ID |
2606 | u32 i; |
2607 | int ret; | |
2608 | ||
afb97888 ID |
2609 | if (fctx->pos_type == OBJ_REQUEST_NODATA || |
2610 | !rbd_layout_is_fancy(&rbd_dev->layout)) | |
2611 | return rbd_img_fill_request_nocopy(img_req, img_extents, | |
2612 | num_img_extents, fctx); | |
3d7efd18 | 2613 | |
afb97888 | 2614 | img_req->data_type = OBJ_REQUEST_OWN_BVECS; |
0eefd470 | 2615 | |
bbea1c1a | 2616 | /* |
afb97888 ID |
2617 | * Create object requests and determine ->bvec_count for each object |
2618 | * request. Note that ->bvec_count sum over all object requests may | |
2619 | * be greater than the number of bio_vecs in the provided bio (list) | |
2620 | * or bio_vec array because when mapped, those bio_vecs can straddle | |
2621 | * stripe unit boundaries. | |
bbea1c1a | 2622 | */ |
5a237819 ID |
2623 | fctx->iter = *fctx->pos; |
2624 | for (i = 0; i < num_img_extents; i++) { | |
afb97888 | 2625 | ret = ceph_file_to_extents(&rbd_dev->layout, |
5a237819 ID |
2626 | img_extents[i].fe_off, |
2627 | img_extents[i].fe_len, | |
2628 | &img_req->object_extents, | |
2629 | alloc_object_extent, img_req, | |
afb97888 ID |
2630 | fctx->count_fn, &fctx->iter); |
2631 | if (ret) | |
2632 | return ret; | |
bbea1c1a | 2633 | } |
0eefd470 | 2634 | |
afb97888 ID |
2635 | for_each_obj_request(img_req, obj_req) { |
2636 | obj_req->bvec_pos.bvecs = kmalloc_array(obj_req->bvec_count, | |
2637 | sizeof(*obj_req->bvec_pos.bvecs), | |
2638 | GFP_NOIO); | |
2639 | if (!obj_req->bvec_pos.bvecs) | |
2640 | return -ENOMEM; | |
2641 | } | |
0eefd470 | 2642 | |
8785b1d4 | 2643 | /* |
afb97888 ID |
2644 | * Fill in each object request's private bio_vec array, splitting and |
2645 | * rearranging the provided bio_vecs in stripe unit chunks as needed. | |
8785b1d4 | 2646 | */ |
afb97888 ID |
2647 | fctx->iter = *fctx->pos; |
2648 | for (i = 0; i < num_img_extents; i++) { | |
2649 | ret = ceph_iterate_extents(&rbd_dev->layout, | |
2650 | img_extents[i].fe_off, | |
2651 | img_extents[i].fe_len, | |
2652 | &img_req->object_extents, | |
2653 | fctx->copy_fn, &fctx->iter); | |
5a237819 ID |
2654 | if (ret) |
2655 | return ret; | |
2656 | } | |
3d7efd18 | 2657 | |
5a237819 ID |
2658 | return __rbd_img_fill_request(img_req); |
2659 | } | |
2660 | ||
2661 | static int rbd_img_fill_nodata(struct rbd_img_request *img_req, | |
2662 | u64 off, u64 len) | |
2663 | { | |
2664 | struct ceph_file_extent ex = { off, len }; | |
2665 | union rbd_img_fill_iter dummy; | |
2666 | struct rbd_img_fill_ctx fctx = { | |
2667 | .pos_type = OBJ_REQUEST_NODATA, | |
2668 | .pos = &dummy, | |
2669 | }; | |
2670 | ||
2671 | return rbd_img_fill_request(img_req, &ex, 1, &fctx); | |
2672 | } | |
2673 | ||
2674 | static void set_bio_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) | |
2675 | { | |
2676 | struct rbd_obj_request *obj_req = | |
2677 | container_of(ex, struct rbd_obj_request, ex); | |
2678 | struct ceph_bio_iter *it = arg; | |
3d7efd18 | 2679 | |
5a237819 ID |
2680 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
2681 | obj_req->bio_pos = *it; | |
2682 | ceph_bio_iter_advance(it, bytes); | |
2683 | } | |
3d7efd18 | 2684 | |
afb97888 ID |
2685 | static void count_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
2686 | { | |
2687 | struct rbd_obj_request *obj_req = | |
2688 | container_of(ex, struct rbd_obj_request, ex); | |
2689 | struct ceph_bio_iter *it = arg; | |
0eefd470 | 2690 | |
afb97888 ID |
2691 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
2692 | ceph_bio_iter_advance_step(it, bytes, ({ | |
2693 | obj_req->bvec_count++; | |
2694 | })); | |
0eefd470 | 2695 | |
afb97888 | 2696 | } |
0eefd470 | 2697 | |
afb97888 ID |
2698 | static void copy_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
2699 | { | |
2700 | struct rbd_obj_request *obj_req = | |
2701 | container_of(ex, struct rbd_obj_request, ex); | |
2702 | struct ceph_bio_iter *it = arg; | |
0eefd470 | 2703 | |
afb97888 ID |
2704 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
2705 | ceph_bio_iter_advance_step(it, bytes, ({ | |
2706 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; | |
2707 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; | |
2708 | })); | |
3d7efd18 AE |
2709 | } |
2710 | ||
5a237819 ID |
2711 | static int __rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
2712 | struct ceph_file_extent *img_extents, | |
2713 | u32 num_img_extents, | |
2714 | struct ceph_bio_iter *bio_pos) | |
2715 | { | |
2716 | struct rbd_img_fill_ctx fctx = { | |
2717 | .pos_type = OBJ_REQUEST_BIO, | |
2718 | .pos = (union rbd_img_fill_iter *)bio_pos, | |
2719 | .set_pos_fn = set_bio_pos, | |
afb97888 ID |
2720 | .count_fn = count_bio_bvecs, |
2721 | .copy_fn = copy_bio_bvecs, | |
5a237819 | 2722 | }; |
3d7efd18 | 2723 | |
5a237819 ID |
2724 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
2725 | &fctx); | |
2726 | } | |
3d7efd18 | 2727 | |
5a237819 ID |
2728 | static int rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
2729 | u64 off, u64 len, struct bio *bio) | |
2730 | { | |
2731 | struct ceph_file_extent ex = { off, len }; | |
2732 | struct ceph_bio_iter it = { .bio = bio, .iter = bio->bi_iter }; | |
3d7efd18 | 2733 | |
5a237819 ID |
2734 | return __rbd_img_fill_from_bio(img_req, &ex, 1, &it); |
2735 | } | |
a9e8ba2c | 2736 | |
5a237819 ID |
2737 | static void set_bvec_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
2738 | { | |
2739 | struct rbd_obj_request *obj_req = | |
2740 | container_of(ex, struct rbd_obj_request, ex); | |
2741 | struct ceph_bvec_iter *it = arg; | |
3d7efd18 | 2742 | |
5a237819 ID |
2743 | obj_req->bvec_pos = *it; |
2744 | ceph_bvec_iter_shorten(&obj_req->bvec_pos, bytes); | |
2745 | ceph_bvec_iter_advance(it, bytes); | |
2746 | } | |
3d7efd18 | 2747 | |
afb97888 ID |
2748 | static void count_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
2749 | { | |
2750 | struct rbd_obj_request *obj_req = | |
2751 | container_of(ex, struct rbd_obj_request, ex); | |
2752 | struct ceph_bvec_iter *it = arg; | |
058aa991 | 2753 | |
afb97888 ID |
2754 | ceph_bvec_iter_advance_step(it, bytes, ({ |
2755 | obj_req->bvec_count++; | |
2756 | })); | |
2757 | } | |
058aa991 | 2758 | |
afb97888 ID |
2759 | static void copy_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
2760 | { | |
2761 | struct rbd_obj_request *obj_req = | |
2762 | container_of(ex, struct rbd_obj_request, ex); | |
2763 | struct ceph_bvec_iter *it = arg; | |
3d7efd18 | 2764 | |
afb97888 ID |
2765 | ceph_bvec_iter_advance_step(it, bytes, ({ |
2766 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; | |
2767 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; | |
2768 | })); | |
3d7efd18 AE |
2769 | } |
2770 | ||
5a237819 ID |
2771 | static int __rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
2772 | struct ceph_file_extent *img_extents, | |
2773 | u32 num_img_extents, | |
2774 | struct ceph_bvec_iter *bvec_pos) | |
c5b5ef6c | 2775 | { |
5a237819 ID |
2776 | struct rbd_img_fill_ctx fctx = { |
2777 | .pos_type = OBJ_REQUEST_BVECS, | |
2778 | .pos = (union rbd_img_fill_iter *)bvec_pos, | |
2779 | .set_pos_fn = set_bvec_pos, | |
afb97888 ID |
2780 | .count_fn = count_bvecs, |
2781 | .copy_fn = copy_bvecs, | |
5a237819 | 2782 | }; |
c5b5ef6c | 2783 | |
5a237819 ID |
2784 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
2785 | &fctx); | |
2786 | } | |
c5b5ef6c | 2787 | |
5a237819 ID |
2788 | static int rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
2789 | struct ceph_file_extent *img_extents, | |
2790 | u32 num_img_extents, | |
2791 | struct bio_vec *bvecs) | |
2792 | { | |
2793 | struct ceph_bvec_iter it = { | |
2794 | .bvecs = bvecs, | |
2795 | .iter = { .bi_size = ceph_file_extents_bytes(img_extents, | |
2796 | num_img_extents) }, | |
2797 | }; | |
c5b5ef6c | 2798 | |
5a237819 ID |
2799 | return __rbd_img_fill_from_bvecs(img_req, img_extents, num_img_extents, |
2800 | &it); | |
2801 | } | |
c5b5ef6c | 2802 | |
0192ce2e | 2803 | static void rbd_img_handle_request_work(struct work_struct *work) |
bf0d5f50 | 2804 | { |
0192ce2e ID |
2805 | struct rbd_img_request *img_req = |
2806 | container_of(work, struct rbd_img_request, work); | |
c5b5ef6c | 2807 | |
0192ce2e ID |
2808 | rbd_img_handle_request(img_req, img_req->work_result); |
2809 | } | |
c2e82414 | 2810 | |
0192ce2e ID |
2811 | static void rbd_img_schedule(struct rbd_img_request *img_req, int result) |
2812 | { | |
2813 | INIT_WORK(&img_req->work, rbd_img_handle_request_work); | |
2814 | img_req->work_result = result; | |
2815 | queue_work(rbd_wq, &img_req->work); | |
c5b5ef6c | 2816 | } |
c2e82414 | 2817 | |
22e8bd51 ID |
2818 | static bool rbd_obj_may_exist(struct rbd_obj_request *obj_req) |
2819 | { | |
2820 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; | |
2821 | ||
2822 | if (rbd_object_map_may_exist(rbd_dev, obj_req->ex.oe_objno)) { | |
2823 | obj_req->flags |= RBD_OBJ_FLAG_MAY_EXIST; | |
2824 | return true; | |
2825 | } | |
2826 | ||
2827 | dout("%s %p objno %llu assuming dne\n", __func__, obj_req, | |
2828 | obj_req->ex.oe_objno); | |
2829 | return false; | |
2830 | } | |
2831 | ||
85b5e6d1 ID |
2832 | static int rbd_obj_read_object(struct rbd_obj_request *obj_req) |
2833 | { | |
a086a1b8 ID |
2834 | struct ceph_osd_request *osd_req; |
2835 | int ret; | |
2836 | ||
2837 | osd_req = __rbd_obj_add_osd_request(obj_req, NULL, 1); | |
2838 | if (IS_ERR(osd_req)) | |
2839 | return PTR_ERR(osd_req); | |
2840 | ||
2841 | osd_req_op_extent_init(osd_req, 0, CEPH_OSD_OP_READ, | |
2842 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); | |
2843 | rbd_osd_setup_data(osd_req, 0); | |
2844 | rbd_osd_format_read(osd_req); | |
2845 | ||
2846 | ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO); | |
2847 | if (ret) | |
2848 | return ret; | |
2849 | ||
2850 | rbd_osd_submit(osd_req); | |
85b5e6d1 | 2851 | return 0; |
c5b5ef6c AE |
2852 | } |
2853 | ||
86bd7998 | 2854 | static int rbd_obj_read_from_parent(struct rbd_obj_request *obj_req) |
c5b5ef6c | 2855 | { |
3da691bf ID |
2856 | struct rbd_img_request *img_req = obj_req->img_request; |
2857 | struct rbd_img_request *child_img_req; | |
c5b5ef6c AE |
2858 | int ret; |
2859 | ||
e93aca0a ID |
2860 | child_img_req = rbd_img_request_create(img_req->rbd_dev->parent, |
2861 | OBJ_OP_READ, NULL); | |
3da691bf | 2862 | if (!child_img_req) |
710214e3 ID |
2863 | return -ENOMEM; |
2864 | ||
e93aca0a ID |
2865 | __set_bit(IMG_REQ_CHILD, &child_img_req->flags); |
2866 | child_img_req->obj_request = obj_req; | |
a90bb0c1 | 2867 | |
21ed05a8 ID |
2868 | dout("%s child_img_req %p for obj_req %p\n", __func__, child_img_req, |
2869 | obj_req); | |
2870 | ||
3da691bf | 2871 | if (!rbd_img_is_write(img_req)) { |
ecc633ca | 2872 | switch (img_req->data_type) { |
3da691bf | 2873 | case OBJ_REQUEST_BIO: |
5a237819 ID |
2874 | ret = __rbd_img_fill_from_bio(child_img_req, |
2875 | obj_req->img_extents, | |
2876 | obj_req->num_img_extents, | |
2877 | &obj_req->bio_pos); | |
3da691bf ID |
2878 | break; |
2879 | case OBJ_REQUEST_BVECS: | |
afb97888 | 2880 | case OBJ_REQUEST_OWN_BVECS: |
5a237819 ID |
2881 | ret = __rbd_img_fill_from_bvecs(child_img_req, |
2882 | obj_req->img_extents, | |
2883 | obj_req->num_img_extents, | |
2884 | &obj_req->bvec_pos); | |
3da691bf ID |
2885 | break; |
2886 | default: | |
d342a15b | 2887 | BUG(); |
3da691bf ID |
2888 | } |
2889 | } else { | |
5a237819 ID |
2890 | ret = rbd_img_fill_from_bvecs(child_img_req, |
2891 | obj_req->img_extents, | |
2892 | obj_req->num_img_extents, | |
2893 | obj_req->copyup_bvecs); | |
3da691bf ID |
2894 | } |
2895 | if (ret) { | |
2896 | rbd_img_request_put(child_img_req); | |
2897 | return ret; | |
2898 | } | |
2899 | ||
0192ce2e ID |
2900 | /* avoid parent chain recursion */ |
2901 | rbd_img_schedule(child_img_req, 0); | |
3da691bf ID |
2902 | return 0; |
2903 | } | |
2904 | ||
85b5e6d1 | 2905 | static bool rbd_obj_advance_read(struct rbd_obj_request *obj_req, int *result) |
3da691bf ID |
2906 | { |
2907 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; | |
2908 | int ret; | |
2909 | ||
22e8bd51 | 2910 | again: |
a9b67e69 | 2911 | switch (obj_req->read_state) { |
85b5e6d1 ID |
2912 | case RBD_OBJ_READ_START: |
2913 | rbd_assert(!*result); | |
2914 | ||
22e8bd51 ID |
2915 | if (!rbd_obj_may_exist(obj_req)) { |
2916 | *result = -ENOENT; | |
2917 | obj_req->read_state = RBD_OBJ_READ_OBJECT; | |
2918 | goto again; | |
2919 | } | |
2920 | ||
85b5e6d1 | 2921 | ret = rbd_obj_read_object(obj_req); |
3da691bf | 2922 | if (ret) { |
85b5e6d1 | 2923 | *result = ret; |
3da691bf ID |
2924 | return true; |
2925 | } | |
85b5e6d1 ID |
2926 | obj_req->read_state = RBD_OBJ_READ_OBJECT; |
2927 | return false; | |
a9b67e69 ID |
2928 | case RBD_OBJ_READ_OBJECT: |
2929 | if (*result == -ENOENT && rbd_dev->parent_overlap) { | |
2930 | /* reverse map this object extent onto the parent */ | |
2931 | ret = rbd_obj_calc_img_extents(obj_req, false); | |
86bd7998 | 2932 | if (ret) { |
54ab3b24 | 2933 | *result = ret; |
86bd7998 ID |
2934 | return true; |
2935 | } | |
a9b67e69 ID |
2936 | if (obj_req->num_img_extents) { |
2937 | ret = rbd_obj_read_from_parent(obj_req); | |
2938 | if (ret) { | |
2939 | *result = ret; | |
2940 | return true; | |
2941 | } | |
2942 | obj_req->read_state = RBD_OBJ_READ_PARENT; | |
2943 | return false; | |
2944 | } | |
86bd7998 | 2945 | } |
710214e3 | 2946 | |
a9b67e69 ID |
2947 | /* |
2948 | * -ENOENT means a hole in the image -- zero-fill the entire | |
2949 | * length of the request. A short read also implies zero-fill | |
2950 | * to the end of the request. | |
2951 | */ | |
2952 | if (*result == -ENOENT) { | |
2953 | rbd_obj_zero_range(obj_req, 0, obj_req->ex.oe_len); | |
2954 | *result = 0; | |
2955 | } else if (*result >= 0) { | |
2956 | if (*result < obj_req->ex.oe_len) | |
2957 | rbd_obj_zero_range(obj_req, *result, | |
2958 | obj_req->ex.oe_len - *result); | |
2959 | else | |
2960 | rbd_assert(*result == obj_req->ex.oe_len); | |
2961 | *result = 0; | |
2962 | } | |
2963 | return true; | |
2964 | case RBD_OBJ_READ_PARENT: | |
d435c9a7 ID |
2965 | /* |
2966 | * The parent image is read only up to the overlap -- zero-fill | |
2967 | * from the overlap to the end of the request. | |
2968 | */ | |
2969 | if (!*result) { | |
2970 | u32 obj_overlap = rbd_obj_img_extents_bytes(obj_req); | |
2971 | ||
2972 | if (obj_overlap < obj_req->ex.oe_len) | |
2973 | rbd_obj_zero_range(obj_req, obj_overlap, | |
2974 | obj_req->ex.oe_len - obj_overlap); | |
2975 | } | |
a9b67e69 ID |
2976 | return true; |
2977 | default: | |
2978 | BUG(); | |
710214e3 | 2979 | } |
3da691bf | 2980 | } |
c5b5ef6c | 2981 | |
22e8bd51 ID |
2982 | static bool rbd_obj_write_is_noop(struct rbd_obj_request *obj_req) |
2983 | { | |
2984 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; | |
2985 | ||
2986 | if (rbd_object_map_may_exist(rbd_dev, obj_req->ex.oe_objno)) | |
2987 | obj_req->flags |= RBD_OBJ_FLAG_MAY_EXIST; | |
2988 | ||
2989 | if (!(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST) && | |
2990 | (obj_req->flags & RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT)) { | |
2991 | dout("%s %p noop for nonexistent\n", __func__, obj_req); | |
2992 | return true; | |
2993 | } | |
2994 | ||
2995 | return false; | |
2996 | } | |
2997 | ||
2998 | /* | |
2999 | * Return: | |
3000 | * 0 - object map update sent | |
3001 | * 1 - object map update isn't needed | |
3002 | * <0 - error | |
3003 | */ | |
3004 | static int rbd_obj_write_pre_object_map(struct rbd_obj_request *obj_req) | |
3005 | { | |
3006 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; | |
3007 | u8 new_state; | |
3008 | ||
3009 | if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)) | |
3010 | return 1; | |
3011 | ||
3012 | if (obj_req->flags & RBD_OBJ_FLAG_DELETION) | |
3013 | new_state = OBJECT_PENDING; | |
3014 | else | |
3015 | new_state = OBJECT_EXISTS; | |
3016 | ||
3017 | return rbd_object_map_update(obj_req, CEPH_NOSNAP, new_state, NULL); | |
3018 | } | |
3019 | ||
85b5e6d1 ID |
3020 | static int rbd_obj_write_object(struct rbd_obj_request *obj_req) |
3021 | { | |
a086a1b8 ID |
3022 | struct ceph_osd_request *osd_req; |
3023 | int num_ops = count_write_ops(obj_req); | |
3024 | int which = 0; | |
3025 | int ret; | |
710214e3 | 3026 | |
a086a1b8 ID |
3027 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) |
3028 | num_ops++; /* stat */ | |
3029 | ||
3030 | osd_req = rbd_obj_add_osd_request(obj_req, num_ops); | |
3031 | if (IS_ERR(osd_req)) | |
3032 | return PTR_ERR(osd_req); | |
3033 | ||
3034 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) { | |
3035 | ret = rbd_osd_setup_stat(osd_req, which++); | |
3036 | if (ret) | |
3037 | return ret; | |
710214e3 | 3038 | } |
c5b5ef6c | 3039 | |
a086a1b8 ID |
3040 | rbd_osd_setup_write_ops(osd_req, which); |
3041 | rbd_osd_format_write(osd_req); | |
3042 | ||
3043 | ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO); | |
3044 | if (ret) | |
3045 | return ret; | |
3046 | ||
3047 | rbd_osd_submit(osd_req); | |
85b5e6d1 | 3048 | return 0; |
3da691bf | 3049 | } |
c5b5ef6c | 3050 | |
3da691bf ID |
3051 | /* |
3052 | * copyup_bvecs pages are never highmem pages | |
3053 | */ | |
3054 | static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes) | |
3055 | { | |
3056 | struct ceph_bvec_iter it = { | |
3057 | .bvecs = bvecs, | |
3058 | .iter = { .bi_size = bytes }, | |
3059 | }; | |
c5b5ef6c | 3060 | |
3da691bf ID |
3061 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
3062 | if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0, | |
3063 | bv.bv_len)) | |
3064 | return false; | |
3065 | })); | |
3066 | return true; | |
c5b5ef6c AE |
3067 | } |
3068 | ||
3a482501 ID |
3069 | #define MODS_ONLY U32_MAX |
3070 | ||
793333a3 ID |
3071 | static int rbd_obj_copyup_empty_snapc(struct rbd_obj_request *obj_req, |
3072 | u32 bytes) | |
b454e36d | 3073 | { |
bcbab1db | 3074 | struct ceph_osd_request *osd_req; |
fe943d50 | 3075 | int ret; |
70d045f6 | 3076 | |
3da691bf | 3077 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
89a59c1c | 3078 | rbd_assert(bytes > 0 && bytes != MODS_ONLY); |
70d045f6 | 3079 | |
bcbab1db ID |
3080 | osd_req = __rbd_obj_add_osd_request(obj_req, &rbd_empty_snapc, 1); |
3081 | if (IS_ERR(osd_req)) | |
3082 | return PTR_ERR(osd_req); | |
b454e36d | 3083 | |
b5ae8cbc | 3084 | ret = rbd_osd_setup_copyup(osd_req, 0, bytes); |
fe943d50 CX |
3085 | if (ret) |
3086 | return ret; | |
3087 | ||
bcbab1db | 3088 | rbd_osd_format_write(osd_req); |
3da691bf | 3089 | |
bcbab1db | 3090 | ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO); |
89a59c1c ID |
3091 | if (ret) |
3092 | return ret; | |
3093 | ||
a086a1b8 | 3094 | rbd_osd_submit(osd_req); |
89a59c1c ID |
3095 | return 0; |
3096 | } | |
3097 | ||
793333a3 ID |
3098 | static int rbd_obj_copyup_current_snapc(struct rbd_obj_request *obj_req, |
3099 | u32 bytes) | |
b454e36d | 3100 | { |
bcbab1db | 3101 | struct ceph_osd_request *osd_req; |
a086a1b8 ID |
3102 | int num_ops = count_write_ops(obj_req); |
3103 | int which = 0; | |
fe943d50 | 3104 | int ret; |
70d045f6 | 3105 | |
3da691bf | 3106 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
70d045f6 | 3107 | |
a086a1b8 ID |
3108 | if (bytes != MODS_ONLY) |
3109 | num_ops++; /* copyup */ | |
13488d53 | 3110 | |
a086a1b8 | 3111 | osd_req = rbd_obj_add_osd_request(obj_req, num_ops); |
bcbab1db ID |
3112 | if (IS_ERR(osd_req)) |
3113 | return PTR_ERR(osd_req); | |
b454e36d | 3114 | |
3a482501 | 3115 | if (bytes != MODS_ONLY) { |
b5ae8cbc | 3116 | ret = rbd_osd_setup_copyup(osd_req, which++, bytes); |
3a482501 ID |
3117 | if (ret) |
3118 | return ret; | |
3da691bf | 3119 | } |
3da691bf | 3120 | |
a086a1b8 ID |
3121 | rbd_osd_setup_write_ops(osd_req, which); |
3122 | rbd_osd_format_write(osd_req); | |
70d045f6 | 3123 | |
bcbab1db | 3124 | ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO); |
26f887e0 ID |
3125 | if (ret) |
3126 | return ret; | |
3127 | ||
a086a1b8 | 3128 | rbd_osd_submit(osd_req); |
3da691bf | 3129 | return 0; |
70d045f6 ID |
3130 | } |
3131 | ||
7e07efb1 | 3132 | static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap) |
70d045f6 | 3133 | { |
7e07efb1 | 3134 | u32 i; |
b454e36d | 3135 | |
7e07efb1 ID |
3136 | rbd_assert(!obj_req->copyup_bvecs); |
3137 | obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap); | |
3138 | obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count, | |
3139 | sizeof(*obj_req->copyup_bvecs), | |
3140 | GFP_NOIO); | |
3141 | if (!obj_req->copyup_bvecs) | |
3142 | return -ENOMEM; | |
b454e36d | 3143 | |
7e07efb1 ID |
3144 | for (i = 0; i < obj_req->copyup_bvec_count; i++) { |
3145 | unsigned int len = min(obj_overlap, (u64)PAGE_SIZE); | |
3146 | ||
3147 | obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO); | |
3148 | if (!obj_req->copyup_bvecs[i].bv_page) | |
3149 | return -ENOMEM; | |
3d7efd18 | 3150 | |
7e07efb1 ID |
3151 | obj_req->copyup_bvecs[i].bv_offset = 0; |
3152 | obj_req->copyup_bvecs[i].bv_len = len; | |
3153 | obj_overlap -= len; | |
3154 | } | |
b454e36d | 3155 | |
7e07efb1 ID |
3156 | rbd_assert(!obj_overlap); |
3157 | return 0; | |
b454e36d AE |
3158 | } |
3159 | ||
0ad5d953 ID |
3160 | /* |
3161 | * The target object doesn't exist. Read the data for the entire | |
3162 | * target object up to the overlap point (if any) from the parent, | |
3163 | * so we can use it for a copyup. | |
3164 | */ | |
793333a3 | 3165 | static int rbd_obj_copyup_read_parent(struct rbd_obj_request *obj_req) |
bf0d5f50 | 3166 | { |
3da691bf | 3167 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
3da691bf | 3168 | int ret; |
bf0d5f50 | 3169 | |
86bd7998 ID |
3170 | rbd_assert(obj_req->num_img_extents); |
3171 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, | |
3172 | rbd_dev->parent_overlap); | |
3173 | if (!obj_req->num_img_extents) { | |
3da691bf ID |
3174 | /* |
3175 | * The overlap has become 0 (most likely because the | |
3a482501 ID |
3176 | * image has been flattened). Re-submit the original write |
3177 | * request -- pass MODS_ONLY since the copyup isn't needed | |
3178 | * anymore. | |
3da691bf | 3179 | */ |
793333a3 | 3180 | return rbd_obj_copyup_current_snapc(obj_req, MODS_ONLY); |
bf0d5f50 AE |
3181 | } |
3182 | ||
86bd7998 | 3183 | ret = setup_copyup_bvecs(obj_req, rbd_obj_img_extents_bytes(obj_req)); |
3da691bf ID |
3184 | if (ret) |
3185 | return ret; | |
3186 | ||
86bd7998 | 3187 | return rbd_obj_read_from_parent(obj_req); |
bf0d5f50 | 3188 | } |
8b3e1a56 | 3189 | |
22e8bd51 ID |
3190 | static void rbd_obj_copyup_object_maps(struct rbd_obj_request *obj_req) |
3191 | { | |
3192 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; | |
3193 | struct ceph_snap_context *snapc = obj_req->img_request->snapc; | |
3194 | u8 new_state; | |
3195 | u32 i; | |
3196 | int ret; | |
3197 | ||
3198 | rbd_assert(!obj_req->pending.result && !obj_req->pending.num_pending); | |
3199 | ||
3200 | if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)) | |
3201 | return; | |
3202 | ||
3203 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ZEROS) | |
3204 | return; | |
3205 | ||
3206 | for (i = 0; i < snapc->num_snaps; i++) { | |
3207 | if ((rbd_dev->header.features & RBD_FEATURE_FAST_DIFF) && | |
3208 | i + 1 < snapc->num_snaps) | |
3209 | new_state = OBJECT_EXISTS_CLEAN; | |
3210 | else | |
3211 | new_state = OBJECT_EXISTS; | |
3212 | ||
3213 | ret = rbd_object_map_update(obj_req, snapc->snaps[i], | |
3214 | new_state, NULL); | |
3215 | if (ret < 0) { | |
3216 | obj_req->pending.result = ret; | |
3217 | return; | |
3218 | } | |
3219 | ||
3220 | rbd_assert(!ret); | |
3221 | obj_req->pending.num_pending++; | |
3222 | } | |
3223 | } | |
3224 | ||
793333a3 ID |
3225 | static void rbd_obj_copyup_write_object(struct rbd_obj_request *obj_req) |
3226 | { | |
3227 | u32 bytes = rbd_obj_img_extents_bytes(obj_req); | |
3228 | int ret; | |
3229 | ||
3230 | rbd_assert(!obj_req->pending.result && !obj_req->pending.num_pending); | |
3231 | ||
3232 | /* | |
3233 | * Only send non-zero copyup data to save some I/O and network | |
3234 | * bandwidth -- zero copyup data is equivalent to the object not | |
3235 | * existing. | |
3236 | */ | |
3237 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ZEROS) | |
3238 | bytes = 0; | |
3239 | ||
3240 | if (obj_req->img_request->snapc->num_snaps && bytes > 0) { | |
3241 | /* | |
3242 | * Send a copyup request with an empty snapshot context to | |
3243 | * deep-copyup the object through all existing snapshots. | |
3244 | * A second request with the current snapshot context will be | |
3245 | * sent for the actual modification. | |
3246 | */ | |
3247 | ret = rbd_obj_copyup_empty_snapc(obj_req, bytes); | |
3248 | if (ret) { | |
3249 | obj_req->pending.result = ret; | |
3250 | return; | |
3251 | } | |
3252 | ||
3253 | obj_req->pending.num_pending++; | |
3254 | bytes = MODS_ONLY; | |
3255 | } | |
3256 | ||
3257 | ret = rbd_obj_copyup_current_snapc(obj_req, bytes); | |
3258 | if (ret) { | |
3259 | obj_req->pending.result = ret; | |
3260 | return; | |
3261 | } | |
3262 | ||
3263 | obj_req->pending.num_pending++; | |
3264 | } | |
3265 | ||
3266 | static bool rbd_obj_advance_copyup(struct rbd_obj_request *obj_req, int *result) | |
3267 | { | |
22e8bd51 | 3268 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
793333a3 ID |
3269 | int ret; |
3270 | ||
3271 | again: | |
3272 | switch (obj_req->copyup_state) { | |
3273 | case RBD_OBJ_COPYUP_START: | |
3274 | rbd_assert(!*result); | |
3275 | ||
3276 | ret = rbd_obj_copyup_read_parent(obj_req); | |
3277 | if (ret) { | |
3278 | *result = ret; | |
3279 | return true; | |
3280 | } | |
3281 | if (obj_req->num_img_extents) | |
3282 | obj_req->copyup_state = RBD_OBJ_COPYUP_READ_PARENT; | |
3283 | else | |
3284 | obj_req->copyup_state = RBD_OBJ_COPYUP_WRITE_OBJECT; | |
3285 | return false; | |
3286 | case RBD_OBJ_COPYUP_READ_PARENT: | |
3287 | if (*result) | |
3288 | return true; | |
3289 | ||
3290 | if (is_zero_bvecs(obj_req->copyup_bvecs, | |
3291 | rbd_obj_img_extents_bytes(obj_req))) { | |
3292 | dout("%s %p detected zeros\n", __func__, obj_req); | |
3293 | obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ZEROS; | |
3294 | } | |
3295 | ||
22e8bd51 ID |
3296 | rbd_obj_copyup_object_maps(obj_req); |
3297 | if (!obj_req->pending.num_pending) { | |
3298 | *result = obj_req->pending.result; | |
3299 | obj_req->copyup_state = RBD_OBJ_COPYUP_OBJECT_MAPS; | |
3300 | goto again; | |
3301 | } | |
3302 | obj_req->copyup_state = __RBD_OBJ_COPYUP_OBJECT_MAPS; | |
3303 | return false; | |
3304 | case __RBD_OBJ_COPYUP_OBJECT_MAPS: | |
3305 | if (!pending_result_dec(&obj_req->pending, result)) | |
3306 | return false; | |
3307 | /* fall through */ | |
3308 | case RBD_OBJ_COPYUP_OBJECT_MAPS: | |
3309 | if (*result) { | |
3310 | rbd_warn(rbd_dev, "snap object map update failed: %d", | |
3311 | *result); | |
3312 | return true; | |
3313 | } | |
3314 | ||
793333a3 ID |
3315 | rbd_obj_copyup_write_object(obj_req); |
3316 | if (!obj_req->pending.num_pending) { | |
3317 | *result = obj_req->pending.result; | |
3318 | obj_req->copyup_state = RBD_OBJ_COPYUP_WRITE_OBJECT; | |
3319 | goto again; | |
3320 | } | |
3321 | obj_req->copyup_state = __RBD_OBJ_COPYUP_WRITE_OBJECT; | |
3322 | return false; | |
3323 | case __RBD_OBJ_COPYUP_WRITE_OBJECT: | |
3324 | if (!pending_result_dec(&obj_req->pending, result)) | |
3325 | return false; | |
3326 | /* fall through */ | |
3327 | case RBD_OBJ_COPYUP_WRITE_OBJECT: | |
3328 | return true; | |
3329 | default: | |
3330 | BUG(); | |
3331 | } | |
3332 | } | |
3333 | ||
22e8bd51 ID |
3334 | /* |
3335 | * Return: | |
3336 | * 0 - object map update sent | |
3337 | * 1 - object map update isn't needed | |
3338 | * <0 - error | |
3339 | */ | |
3340 | static int rbd_obj_write_post_object_map(struct rbd_obj_request *obj_req) | |
3341 | { | |
3342 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; | |
3343 | u8 current_state = OBJECT_PENDING; | |
3344 | ||
3345 | if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)) | |
3346 | return 1; | |
3347 | ||
3348 | if (!(obj_req->flags & RBD_OBJ_FLAG_DELETION)) | |
3349 | return 1; | |
3350 | ||
3351 | return rbd_object_map_update(obj_req, CEPH_NOSNAP, OBJECT_NONEXISTENT, | |
3352 | ¤t_state); | |
3353 | } | |
3354 | ||
85b5e6d1 | 3355 | static bool rbd_obj_advance_write(struct rbd_obj_request *obj_req, int *result) |
8b3e1a56 | 3356 | { |
793333a3 | 3357 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
3da691bf | 3358 | int ret; |
8b3e1a56 | 3359 | |
793333a3 | 3360 | again: |
3da691bf | 3361 | switch (obj_req->write_state) { |
85b5e6d1 ID |
3362 | case RBD_OBJ_WRITE_START: |
3363 | rbd_assert(!*result); | |
3364 | ||
22e8bd51 ID |
3365 | if (rbd_obj_write_is_noop(obj_req)) |
3366 | return true; | |
3367 | ||
3368 | ret = rbd_obj_write_pre_object_map(obj_req); | |
3369 | if (ret < 0) { | |
3370 | *result = ret; | |
3371 | return true; | |
3372 | } | |
3373 | obj_req->write_state = RBD_OBJ_WRITE_PRE_OBJECT_MAP; | |
3374 | if (ret > 0) | |
3375 | goto again; | |
3376 | return false; | |
3377 | case RBD_OBJ_WRITE_PRE_OBJECT_MAP: | |
3378 | if (*result) { | |
3379 | rbd_warn(rbd_dev, "pre object map update failed: %d", | |
3380 | *result); | |
3381 | return true; | |
3382 | } | |
85b5e6d1 ID |
3383 | ret = rbd_obj_write_object(obj_req); |
3384 | if (ret) { | |
3385 | *result = ret; | |
3386 | return true; | |
3387 | } | |
3388 | obj_req->write_state = RBD_OBJ_WRITE_OBJECT; | |
3389 | return false; | |
0ad5d953 | 3390 | case RBD_OBJ_WRITE_OBJECT: |
54ab3b24 | 3391 | if (*result == -ENOENT) { |
0ad5d953 | 3392 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) { |
793333a3 ID |
3393 | *result = 0; |
3394 | obj_req->copyup_state = RBD_OBJ_COPYUP_START; | |
3395 | obj_req->write_state = __RBD_OBJ_WRITE_COPYUP; | |
3396 | goto again; | |
0ad5d953 | 3397 | } |
3da691bf | 3398 | /* |
0ad5d953 ID |
3399 | * On a non-existent object: |
3400 | * delete - -ENOENT, truncate/zero - 0 | |
3da691bf | 3401 | */ |
0ad5d953 ID |
3402 | if (obj_req->flags & RBD_OBJ_FLAG_DELETION) |
3403 | *result = 0; | |
3da691bf | 3404 | } |
a9b67e69 | 3405 | if (*result) |
3a482501 | 3406 | return true; |
8b3e1a56 | 3407 | |
793333a3 ID |
3408 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP; |
3409 | goto again; | |
3410 | case __RBD_OBJ_WRITE_COPYUP: | |
3411 | if (!rbd_obj_advance_copyup(obj_req, result)) | |
3412 | return false; | |
3413 | /* fall through */ | |
3414 | case RBD_OBJ_WRITE_COPYUP: | |
22e8bd51 | 3415 | if (*result) { |
793333a3 | 3416 | rbd_warn(rbd_dev, "copyup failed: %d", *result); |
22e8bd51 ID |
3417 | return true; |
3418 | } | |
3419 | ret = rbd_obj_write_post_object_map(obj_req); | |
3420 | if (ret < 0) { | |
3421 | *result = ret; | |
3422 | return true; | |
3423 | } | |
3424 | obj_req->write_state = RBD_OBJ_WRITE_POST_OBJECT_MAP; | |
3425 | if (ret > 0) | |
3426 | goto again; | |
3427 | return false; | |
3428 | case RBD_OBJ_WRITE_POST_OBJECT_MAP: | |
3429 | if (*result) | |
3430 | rbd_warn(rbd_dev, "post object map update failed: %d", | |
3431 | *result); | |
793333a3 | 3432 | return true; |
3da691bf | 3433 | default: |
c6244b3b | 3434 | BUG(); |
3da691bf ID |
3435 | } |
3436 | } | |
02c74fba | 3437 | |
3da691bf | 3438 | /* |
0ad5d953 | 3439 | * Return true if @obj_req is completed. |
3da691bf | 3440 | */ |
54ab3b24 ID |
3441 | static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req, |
3442 | int *result) | |
3da691bf | 3443 | { |
0ad5d953 | 3444 | struct rbd_img_request *img_req = obj_req->img_request; |
0192ce2e | 3445 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
0ad5d953 ID |
3446 | bool done; |
3447 | ||
85b5e6d1 | 3448 | mutex_lock(&obj_req->state_mutex); |
0ad5d953 | 3449 | if (!rbd_img_is_write(img_req)) |
85b5e6d1 | 3450 | done = rbd_obj_advance_read(obj_req, result); |
0ad5d953 | 3451 | else |
85b5e6d1 ID |
3452 | done = rbd_obj_advance_write(obj_req, result); |
3453 | mutex_unlock(&obj_req->state_mutex); | |
0ad5d953 | 3454 | |
0192ce2e ID |
3455 | if (done && *result) { |
3456 | rbd_assert(*result < 0); | |
3457 | rbd_warn(rbd_dev, "%s at objno %llu %llu~%llu result %d", | |
3458 | obj_op_name(img_req->op_type), obj_req->ex.oe_objno, | |
3459 | obj_req->ex.oe_off, obj_req->ex.oe_len, *result); | |
3460 | } | |
0ad5d953 | 3461 | return done; |
3da691bf | 3462 | } |
02c74fba | 3463 | |
0192ce2e ID |
3464 | /* |
3465 | * This is open-coded in rbd_img_handle_request() to avoid parent chain | |
3466 | * recursion. | |
3467 | */ | |
3468 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req, int result) | |
3469 | { | |
3470 | if (__rbd_obj_handle_request(obj_req, &result)) | |
3471 | rbd_img_handle_request(obj_req->img_request, result); | |
3472 | } | |
3473 | ||
e1fddc8f ID |
3474 | static bool need_exclusive_lock(struct rbd_img_request *img_req) |
3475 | { | |
3476 | struct rbd_device *rbd_dev = img_req->rbd_dev; | |
3477 | ||
3478 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) | |
3479 | return false; | |
3480 | ||
3fe69921 | 3481 | if (rbd_is_ro(rbd_dev)) |
e1fddc8f ID |
3482 | return false; |
3483 | ||
3484 | rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); | |
22e8bd51 ID |
3485 | if (rbd_dev->opts->lock_on_read || |
3486 | (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)) | |
e1fddc8f ID |
3487 | return true; |
3488 | ||
3489 | return rbd_img_is_write(img_req); | |
3490 | } | |
3491 | ||
637cd060 | 3492 | static bool rbd_lock_add_request(struct rbd_img_request *img_req) |
e1fddc8f ID |
3493 | { |
3494 | struct rbd_device *rbd_dev = img_req->rbd_dev; | |
637cd060 | 3495 | bool locked; |
e1fddc8f ID |
3496 | |
3497 | lockdep_assert_held(&rbd_dev->lock_rwsem); | |
637cd060 | 3498 | locked = rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED; |
e1fddc8f ID |
3499 | spin_lock(&rbd_dev->lock_lists_lock); |
3500 | rbd_assert(list_empty(&img_req->lock_item)); | |
637cd060 ID |
3501 | if (!locked) |
3502 | list_add_tail(&img_req->lock_item, &rbd_dev->acquiring_list); | |
3503 | else | |
3504 | list_add_tail(&img_req->lock_item, &rbd_dev->running_list); | |
e1fddc8f | 3505 | spin_unlock(&rbd_dev->lock_lists_lock); |
637cd060 | 3506 | return locked; |
e1fddc8f ID |
3507 | } |
3508 | ||
3509 | static void rbd_lock_del_request(struct rbd_img_request *img_req) | |
3510 | { | |
3511 | struct rbd_device *rbd_dev = img_req->rbd_dev; | |
3512 | bool need_wakeup; | |
3513 | ||
3514 | lockdep_assert_held(&rbd_dev->lock_rwsem); | |
3515 | spin_lock(&rbd_dev->lock_lists_lock); | |
3516 | rbd_assert(!list_empty(&img_req->lock_item)); | |
3517 | list_del_init(&img_req->lock_item); | |
3518 | need_wakeup = (rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING && | |
3519 | list_empty(&rbd_dev->running_list)); | |
3520 | spin_unlock(&rbd_dev->lock_lists_lock); | |
3521 | if (need_wakeup) | |
3522 | complete(&rbd_dev->releasing_wait); | |
3523 | } | |
3524 | ||
637cd060 ID |
3525 | static int rbd_img_exclusive_lock(struct rbd_img_request *img_req) |
3526 | { | |
3527 | struct rbd_device *rbd_dev = img_req->rbd_dev; | |
3528 | ||
3529 | if (!need_exclusive_lock(img_req)) | |
3530 | return 1; | |
3531 | ||
3532 | if (rbd_lock_add_request(img_req)) | |
3533 | return 1; | |
3534 | ||
3535 | if (rbd_dev->opts->exclusive) { | |
3536 | WARN_ON(1); /* lock got released? */ | |
3537 | return -EROFS; | |
3538 | } | |
3539 | ||
3540 | /* | |
3541 | * Note the use of mod_delayed_work() in rbd_acquire_lock() | |
3542 | * and cancel_delayed_work() in wake_lock_waiters(). | |
3543 | */ | |
3544 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); | |
3545 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); | |
3546 | return 0; | |
3547 | } | |
3548 | ||
0192ce2e | 3549 | static void rbd_img_object_requests(struct rbd_img_request *img_req) |
7114edac | 3550 | { |
0192ce2e | 3551 | struct rbd_obj_request *obj_req; |
7114edac | 3552 | |
0192ce2e ID |
3553 | rbd_assert(!img_req->pending.result && !img_req->pending.num_pending); |
3554 | ||
3555 | for_each_obj_request(img_req, obj_req) { | |
3556 | int result = 0; | |
a9e8ba2c | 3557 | |
0192ce2e ID |
3558 | if (__rbd_obj_handle_request(obj_req, &result)) { |
3559 | if (result) { | |
3560 | img_req->pending.result = result; | |
3561 | return; | |
3562 | } | |
3563 | } else { | |
3564 | img_req->pending.num_pending++; | |
3565 | } | |
3566 | } | |
8b3e1a56 AE |
3567 | } |
3568 | ||
0192ce2e | 3569 | static bool rbd_img_advance(struct rbd_img_request *img_req, int *result) |
8b3e1a56 | 3570 | { |
637cd060 | 3571 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
3da691bf | 3572 | int ret; |
8b3e1a56 | 3573 | |
0192ce2e ID |
3574 | again: |
3575 | switch (img_req->state) { | |
3576 | case RBD_IMG_START: | |
3577 | rbd_assert(!*result); | |
8b3e1a56 | 3578 | |
637cd060 ID |
3579 | ret = rbd_img_exclusive_lock(img_req); |
3580 | if (ret < 0) { | |
3581 | *result = ret; | |
3da691bf ID |
3582 | return true; |
3583 | } | |
637cd060 ID |
3584 | img_req->state = RBD_IMG_EXCLUSIVE_LOCK; |
3585 | if (ret > 0) | |
3586 | goto again; | |
3da691bf | 3587 | return false; |
637cd060 ID |
3588 | case RBD_IMG_EXCLUSIVE_LOCK: |
3589 | if (*result) | |
89a59c1c ID |
3590 | return true; |
3591 | ||
637cd060 ID |
3592 | rbd_assert(!need_exclusive_lock(img_req) || |
3593 | __rbd_is_lock_owner(rbd_dev)); | |
3594 | ||
0192ce2e ID |
3595 | rbd_img_object_requests(img_req); |
3596 | if (!img_req->pending.num_pending) { | |
3597 | *result = img_req->pending.result; | |
3598 | img_req->state = RBD_IMG_OBJECT_REQUESTS; | |
3599 | goto again; | |
3da691bf | 3600 | } |
0192ce2e | 3601 | img_req->state = __RBD_IMG_OBJECT_REQUESTS; |
3da691bf | 3602 | return false; |
0192ce2e ID |
3603 | case __RBD_IMG_OBJECT_REQUESTS: |
3604 | if (!pending_result_dec(&img_req->pending, result)) | |
3605 | return false; | |
3606 | /* fall through */ | |
3607 | case RBD_IMG_OBJECT_REQUESTS: | |
3608 | return true; | |
3da691bf | 3609 | default: |
c6244b3b | 3610 | BUG(); |
3da691bf ID |
3611 | } |
3612 | } | |
02c74fba | 3613 | |
3da691bf | 3614 | /* |
0192ce2e | 3615 | * Return true if @img_req is completed. |
3da691bf | 3616 | */ |
0192ce2e ID |
3617 | static bool __rbd_img_handle_request(struct rbd_img_request *img_req, |
3618 | int *result) | |
7114edac | 3619 | { |
0192ce2e ID |
3620 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
3621 | bool done; | |
7114edac | 3622 | |
e1fddc8f ID |
3623 | if (need_exclusive_lock(img_req)) { |
3624 | down_read(&rbd_dev->lock_rwsem); | |
3625 | mutex_lock(&img_req->state_mutex); | |
3626 | done = rbd_img_advance(img_req, result); | |
3627 | if (done) | |
3628 | rbd_lock_del_request(img_req); | |
3629 | mutex_unlock(&img_req->state_mutex); | |
3630 | up_read(&rbd_dev->lock_rwsem); | |
3631 | } else { | |
3632 | mutex_lock(&img_req->state_mutex); | |
3633 | done = rbd_img_advance(img_req, result); | |
3634 | mutex_unlock(&img_req->state_mutex); | |
02c74fba | 3635 | } |
a9e8ba2c | 3636 | |
0192ce2e ID |
3637 | if (done && *result) { |
3638 | rbd_assert(*result < 0); | |
3639 | rbd_warn(rbd_dev, "%s%s result %d", | |
3640 | test_bit(IMG_REQ_CHILD, &img_req->flags) ? "child " : "", | |
3641 | obj_op_name(img_req->op_type), *result); | |
7114edac | 3642 | } |
0192ce2e | 3643 | return done; |
7114edac | 3644 | } |
a9e8ba2c | 3645 | |
0192ce2e | 3646 | static void rbd_img_handle_request(struct rbd_img_request *img_req, int result) |
3da691bf | 3647 | { |
7114edac | 3648 | again: |
0192ce2e | 3649 | if (!__rbd_img_handle_request(img_req, &result)) |
7114edac | 3650 | return; |
8b3e1a56 | 3651 | |
7114edac | 3652 | if (test_bit(IMG_REQ_CHILD, &img_req->flags)) { |
0192ce2e ID |
3653 | struct rbd_obj_request *obj_req = img_req->obj_request; |
3654 | ||
54ab3b24 | 3655 | rbd_img_request_put(img_req); |
0192ce2e ID |
3656 | if (__rbd_obj_handle_request(obj_req, &result)) { |
3657 | img_req = obj_req->img_request; | |
3658 | goto again; | |
3659 | } | |
3660 | } else { | |
3661 | struct request *rq = img_req->rq; | |
3662 | ||
3663 | rbd_img_request_put(img_req); | |
3664 | blk_mq_end_request(rq, errno_to_blk_status(result)); | |
7114edac | 3665 | } |
8b3e1a56 | 3666 | } |
bf0d5f50 | 3667 | |
ed95b21a | 3668 | static const struct rbd_client_id rbd_empty_cid; |
b8d70035 | 3669 | |
ed95b21a ID |
3670 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
3671 | const struct rbd_client_id *rhs) | |
3672 | { | |
3673 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; | |
3674 | } | |
3675 | ||
3676 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) | |
3677 | { | |
3678 | struct rbd_client_id cid; | |
3679 | ||
3680 | mutex_lock(&rbd_dev->watch_mutex); | |
3681 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); | |
3682 | cid.handle = rbd_dev->watch_cookie; | |
3683 | mutex_unlock(&rbd_dev->watch_mutex); | |
3684 | return cid; | |
3685 | } | |
3686 | ||
3687 | /* | |
3688 | * lock_rwsem must be held for write | |
3689 | */ | |
3690 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, | |
3691 | const struct rbd_client_id *cid) | |
3692 | { | |
3693 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, | |
3694 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, | |
3695 | cid->gid, cid->handle); | |
3696 | rbd_dev->owner_cid = *cid; /* struct */ | |
3697 | } | |
3698 | ||
3699 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) | |
3700 | { | |
3701 | mutex_lock(&rbd_dev->watch_mutex); | |
3702 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); | |
3703 | mutex_unlock(&rbd_dev->watch_mutex); | |
3704 | } | |
3705 | ||
edd8ca80 FM |
3706 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
3707 | { | |
3708 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); | |
3709 | ||
a2b1da09 | 3710 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
edd8ca80 FM |
3711 | strcpy(rbd_dev->lock_cookie, cookie); |
3712 | rbd_set_owner_cid(rbd_dev, &cid); | |
3713 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); | |
3714 | } | |
3715 | ||
ed95b21a ID |
3716 | /* |
3717 | * lock_rwsem must be held for write | |
3718 | */ | |
3719 | static int rbd_lock(struct rbd_device *rbd_dev) | |
b8d70035 | 3720 | { |
922dab61 | 3721 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
ed95b21a | 3722 | char cookie[32]; |
e627db08 | 3723 | int ret; |
b8d70035 | 3724 | |
cbbfb0ff ID |
3725 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
3726 | rbd_dev->lock_cookie[0] != '\0'); | |
52bb1f9b | 3727 | |
ed95b21a ID |
3728 | format_lock_cookie(rbd_dev, cookie); |
3729 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, | |
3730 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, | |
3731 | RBD_LOCK_TAG, "", 0); | |
e627db08 | 3732 | if (ret) |
ed95b21a | 3733 | return ret; |
b8d70035 | 3734 | |
edd8ca80 | 3735 | __rbd_lock(rbd_dev, cookie); |
ed95b21a | 3736 | return 0; |
b8d70035 AE |
3737 | } |
3738 | ||
ed95b21a ID |
3739 | /* |
3740 | * lock_rwsem must be held for write | |
3741 | */ | |
bbead745 | 3742 | static void rbd_unlock(struct rbd_device *rbd_dev) |
bb040aa0 | 3743 | { |
922dab61 | 3744 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
bb040aa0 ID |
3745 | int ret; |
3746 | ||
cbbfb0ff ID |
3747 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
3748 | rbd_dev->lock_cookie[0] == '\0'); | |
bb040aa0 | 3749 | |
ed95b21a | 3750 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
cbbfb0ff | 3751 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
bbead745 | 3752 | if (ret && ret != -ENOENT) |
637cd060 | 3753 | rbd_warn(rbd_dev, "failed to unlock header: %d", ret); |
bb040aa0 | 3754 | |
bbead745 ID |
3755 | /* treat errors as the image is unlocked */ |
3756 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; | |
cbbfb0ff | 3757 | rbd_dev->lock_cookie[0] = '\0'; |
ed95b21a ID |
3758 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
3759 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); | |
bb040aa0 ID |
3760 | } |
3761 | ||
ed95b21a ID |
3762 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
3763 | enum rbd_notify_op notify_op, | |
3764 | struct page ***preply_pages, | |
3765 | size_t *preply_len) | |
9969ebc5 AE |
3766 | { |
3767 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
ed95b21a | 3768 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
08a79102 KS |
3769 | char buf[4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN]; |
3770 | int buf_size = sizeof(buf); | |
ed95b21a | 3771 | void *p = buf; |
9969ebc5 | 3772 | |
ed95b21a | 3773 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
9969ebc5 | 3774 | |
ed95b21a ID |
3775 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
3776 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); | |
3777 | ceph_encode_32(&p, notify_op); | |
3778 | ceph_encode_64(&p, cid.gid); | |
3779 | ceph_encode_64(&p, cid.handle); | |
8eb87565 | 3780 | |
ed95b21a ID |
3781 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
3782 | &rbd_dev->header_oloc, buf, buf_size, | |
3783 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); | |
b30a01f2 ID |
3784 | } |
3785 | ||
ed95b21a ID |
3786 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
3787 | enum rbd_notify_op notify_op) | |
b30a01f2 | 3788 | { |
ed95b21a ID |
3789 | struct page **reply_pages; |
3790 | size_t reply_len; | |
b30a01f2 | 3791 | |
ed95b21a ID |
3792 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
3793 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); | |
3794 | } | |
b30a01f2 | 3795 | |
ed95b21a ID |
3796 | static void rbd_notify_acquired_lock(struct work_struct *work) |
3797 | { | |
3798 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, | |
3799 | acquired_lock_work); | |
76756a51 | 3800 | |
ed95b21a | 3801 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
c525f036 ID |
3802 | } |
3803 | ||
ed95b21a | 3804 | static void rbd_notify_released_lock(struct work_struct *work) |
c525f036 | 3805 | { |
ed95b21a ID |
3806 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
3807 | released_lock_work); | |
811c6688 | 3808 | |
ed95b21a | 3809 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
fca27065 ID |
3810 | } |
3811 | ||
ed95b21a | 3812 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
36be9a76 | 3813 | { |
ed95b21a ID |
3814 | struct page **reply_pages; |
3815 | size_t reply_len; | |
3816 | bool lock_owner_responded = false; | |
36be9a76 AE |
3817 | int ret; |
3818 | ||
ed95b21a | 3819 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
36be9a76 | 3820 | |
ed95b21a ID |
3821 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
3822 | &reply_pages, &reply_len); | |
3823 | if (ret && ret != -ETIMEDOUT) { | |
3824 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); | |
36be9a76 | 3825 | goto out; |
ed95b21a | 3826 | } |
36be9a76 | 3827 | |
ed95b21a ID |
3828 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
3829 | void *p = page_address(reply_pages[0]); | |
3830 | void *const end = p + reply_len; | |
3831 | u32 n; | |
36be9a76 | 3832 | |
ed95b21a ID |
3833 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
3834 | while (n--) { | |
3835 | u8 struct_v; | |
3836 | u32 len; | |
36be9a76 | 3837 | |
ed95b21a ID |
3838 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
3839 | p += 8 + 8; /* skip gid and cookie */ | |
04017e29 | 3840 | |
ed95b21a ID |
3841 | ceph_decode_32_safe(&p, end, len, e_inval); |
3842 | if (!len) | |
3843 | continue; | |
3844 | ||
3845 | if (lock_owner_responded) { | |
3846 | rbd_warn(rbd_dev, | |
3847 | "duplicate lock owners detected"); | |
3848 | ret = -EIO; | |
3849 | goto out; | |
3850 | } | |
3851 | ||
3852 | lock_owner_responded = true; | |
3853 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", | |
3854 | &struct_v, &len); | |
3855 | if (ret) { | |
3856 | rbd_warn(rbd_dev, | |
3857 | "failed to decode ResponseMessage: %d", | |
3858 | ret); | |
3859 | goto e_inval; | |
3860 | } | |
3861 | ||
3862 | ret = ceph_decode_32(&p); | |
3863 | } | |
3864 | } | |
3865 | ||
3866 | if (!lock_owner_responded) { | |
3867 | rbd_warn(rbd_dev, "no lock owners detected"); | |
3868 | ret = -ETIMEDOUT; | |
3869 | } | |
3870 | ||
3871 | out: | |
3872 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); | |
3873 | return ret; | |
3874 | ||
3875 | e_inval: | |
3876 | ret = -EINVAL; | |
3877 | goto out; | |
3878 | } | |
3879 | ||
637cd060 ID |
3880 | /* |
3881 | * Either image request state machine(s) or rbd_add_acquire_lock() | |
3882 | * (i.e. "rbd map"). | |
3883 | */ | |
3884 | static void wake_lock_waiters(struct rbd_device *rbd_dev, int result) | |
ed95b21a | 3885 | { |
637cd060 ID |
3886 | struct rbd_img_request *img_req; |
3887 | ||
3888 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); | |
d9b9c893 | 3889 | lockdep_assert_held_write(&rbd_dev->lock_rwsem); |
ed95b21a ID |
3890 | |
3891 | cancel_delayed_work(&rbd_dev->lock_dwork); | |
637cd060 ID |
3892 | if (!completion_done(&rbd_dev->acquire_wait)) { |
3893 | rbd_assert(list_empty(&rbd_dev->acquiring_list) && | |
3894 | list_empty(&rbd_dev->running_list)); | |
3895 | rbd_dev->acquire_err = result; | |
3896 | complete_all(&rbd_dev->acquire_wait); | |
3897 | return; | |
3898 | } | |
3899 | ||
3900 | list_for_each_entry(img_req, &rbd_dev->acquiring_list, lock_item) { | |
3901 | mutex_lock(&img_req->state_mutex); | |
3902 | rbd_assert(img_req->state == RBD_IMG_EXCLUSIVE_LOCK); | |
3903 | rbd_img_schedule(img_req, result); | |
3904 | mutex_unlock(&img_req->state_mutex); | |
3905 | } | |
3906 | ||
3907 | list_splice_tail_init(&rbd_dev->acquiring_list, &rbd_dev->running_list); | |
ed95b21a ID |
3908 | } |
3909 | ||
3910 | static int get_lock_owner_info(struct rbd_device *rbd_dev, | |
3911 | struct ceph_locker **lockers, u32 *num_lockers) | |
3912 | { | |
3913 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
3914 | u8 lock_type; | |
3915 | char *lock_tag; | |
3916 | int ret; | |
3917 | ||
3918 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
3919 | ||
3920 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, | |
3921 | &rbd_dev->header_oloc, RBD_LOCK_NAME, | |
3922 | &lock_type, &lock_tag, lockers, num_lockers); | |
3923 | if (ret) | |
3924 | return ret; | |
3925 | ||
3926 | if (*num_lockers == 0) { | |
3927 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); | |
3928 | goto out; | |
3929 | } | |
3930 | ||
3931 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { | |
3932 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", | |
3933 | lock_tag); | |
3934 | ret = -EBUSY; | |
3935 | goto out; | |
3936 | } | |
3937 | ||
3938 | if (lock_type == CEPH_CLS_LOCK_SHARED) { | |
3939 | rbd_warn(rbd_dev, "shared lock type detected"); | |
3940 | ret = -EBUSY; | |
3941 | goto out; | |
3942 | } | |
3943 | ||
3944 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, | |
3945 | strlen(RBD_LOCK_COOKIE_PREFIX))) { | |
3946 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", | |
3947 | (*lockers)[0].id.cookie); | |
3948 | ret = -EBUSY; | |
3949 | goto out; | |
3950 | } | |
3951 | ||
3952 | out: | |
3953 | kfree(lock_tag); | |
3954 | return ret; | |
3955 | } | |
3956 | ||
3957 | static int find_watcher(struct rbd_device *rbd_dev, | |
3958 | const struct ceph_locker *locker) | |
3959 | { | |
3960 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
3961 | struct ceph_watch_item *watchers; | |
3962 | u32 num_watchers; | |
3963 | u64 cookie; | |
3964 | int i; | |
3965 | int ret; | |
3966 | ||
3967 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, | |
3968 | &rbd_dev->header_oloc, &watchers, | |
3969 | &num_watchers); | |
3970 | if (ret) | |
3971 | return ret; | |
3972 | ||
3973 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); | |
3974 | for (i = 0; i < num_watchers; i++) { | |
3975 | if (!memcmp(&watchers[i].addr, &locker->info.addr, | |
3976 | sizeof(locker->info.addr)) && | |
3977 | watchers[i].cookie == cookie) { | |
3978 | struct rbd_client_id cid = { | |
3979 | .gid = le64_to_cpu(watchers[i].name.num), | |
3980 | .handle = cookie, | |
3981 | }; | |
3982 | ||
3983 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, | |
3984 | rbd_dev, cid.gid, cid.handle); | |
3985 | rbd_set_owner_cid(rbd_dev, &cid); | |
3986 | ret = 1; | |
3987 | goto out; | |
3988 | } | |
3989 | } | |
3990 | ||
3991 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); | |
3992 | ret = 0; | |
3993 | out: | |
3994 | kfree(watchers); | |
3995 | return ret; | |
3996 | } | |
3997 | ||
3998 | /* | |
3999 | * lock_rwsem must be held for write | |
4000 | */ | |
4001 | static int rbd_try_lock(struct rbd_device *rbd_dev) | |
4002 | { | |
4003 | struct ceph_client *client = rbd_dev->rbd_client->client; | |
4004 | struct ceph_locker *lockers; | |
4005 | u32 num_lockers; | |
4006 | int ret; | |
4007 | ||
4008 | for (;;) { | |
4009 | ret = rbd_lock(rbd_dev); | |
4010 | if (ret != -EBUSY) | |
4011 | return ret; | |
4012 | ||
4013 | /* determine if the current lock holder is still alive */ | |
4014 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); | |
4015 | if (ret) | |
4016 | return ret; | |
4017 | ||
4018 | if (num_lockers == 0) | |
4019 | goto again; | |
4020 | ||
4021 | ret = find_watcher(rbd_dev, lockers); | |
637cd060 ID |
4022 | if (ret) |
4023 | goto out; /* request lock or error */ | |
ed95b21a | 4024 | |
22e8bd51 | 4025 | rbd_warn(rbd_dev, "breaking header lock owned by %s%llu", |
ed95b21a ID |
4026 | ENTITY_NAME(lockers[0].id.name)); |
4027 | ||
4028 | ret = ceph_monc_blacklist_add(&client->monc, | |
4029 | &lockers[0].info.addr); | |
4030 | if (ret) { | |
4031 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", | |
4032 | ENTITY_NAME(lockers[0].id.name), ret); | |
4033 | goto out; | |
4034 | } | |
4035 | ||
4036 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, | |
4037 | &rbd_dev->header_oloc, RBD_LOCK_NAME, | |
4038 | lockers[0].id.cookie, | |
4039 | &lockers[0].id.name); | |
4040 | if (ret && ret != -ENOENT) | |
4041 | goto out; | |
4042 | ||
4043 | again: | |
4044 | ceph_free_lockers(lockers, num_lockers); | |
4045 | } | |
4046 | ||
4047 | out: | |
4048 | ceph_free_lockers(lockers, num_lockers); | |
4049 | return ret; | |
4050 | } | |
4051 | ||
22e8bd51 ID |
4052 | static int rbd_post_acquire_action(struct rbd_device *rbd_dev) |
4053 | { | |
4054 | int ret; | |
4055 | ||
4056 | if (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP) { | |
4057 | ret = rbd_object_map_open(rbd_dev); | |
4058 | if (ret) | |
4059 | return ret; | |
4060 | } | |
4061 | ||
4062 | return 0; | |
4063 | } | |
4064 | ||
ed95b21a | 4065 | /* |
637cd060 ID |
4066 | * Return: |
4067 | * 0 - lock acquired | |
4068 | * 1 - caller should call rbd_request_lock() | |
4069 | * <0 - error | |
ed95b21a | 4070 | */ |
637cd060 | 4071 | static int rbd_try_acquire_lock(struct rbd_device *rbd_dev) |
ed95b21a | 4072 | { |
637cd060 | 4073 | int ret; |
ed95b21a ID |
4074 | |
4075 | down_read(&rbd_dev->lock_rwsem); | |
4076 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, | |
4077 | rbd_dev->lock_state); | |
4078 | if (__rbd_is_lock_owner(rbd_dev)) { | |
ed95b21a | 4079 | up_read(&rbd_dev->lock_rwsem); |
637cd060 | 4080 | return 0; |
ed95b21a ID |
4081 | } |
4082 | ||
4083 | up_read(&rbd_dev->lock_rwsem); | |
4084 | down_write(&rbd_dev->lock_rwsem); | |
4085 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, | |
4086 | rbd_dev->lock_state); | |
637cd060 ID |
4087 | if (__rbd_is_lock_owner(rbd_dev)) { |
4088 | up_write(&rbd_dev->lock_rwsem); | |
4089 | return 0; | |
ed95b21a ID |
4090 | } |
4091 | ||
637cd060 ID |
4092 | ret = rbd_try_lock(rbd_dev); |
4093 | if (ret < 0) { | |
4094 | rbd_warn(rbd_dev, "failed to lock header: %d", ret); | |
4095 | if (ret == -EBLACKLISTED) | |
4096 | goto out; | |
4097 | ||
4098 | ret = 1; /* request lock anyway */ | |
4099 | } | |
4100 | if (ret > 0) { | |
4101 | up_write(&rbd_dev->lock_rwsem); | |
4102 | return ret; | |
4103 | } | |
4104 | ||
4105 | rbd_assert(rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED); | |
4106 | rbd_assert(list_empty(&rbd_dev->running_list)); | |
4107 | ||
22e8bd51 ID |
4108 | ret = rbd_post_acquire_action(rbd_dev); |
4109 | if (ret) { | |
4110 | rbd_warn(rbd_dev, "post-acquire action failed: %d", ret); | |
4111 | /* | |
4112 | * Can't stay in RBD_LOCK_STATE_LOCKED because | |
4113 | * rbd_lock_add_request() would let the request through, | |
4114 | * assuming that e.g. object map is locked and loaded. | |
4115 | */ | |
4116 | rbd_unlock(rbd_dev); | |
ed95b21a ID |
4117 | } |
4118 | ||
637cd060 ID |
4119 | out: |
4120 | wake_lock_waiters(rbd_dev, ret); | |
ed95b21a | 4121 | up_write(&rbd_dev->lock_rwsem); |
637cd060 | 4122 | return ret; |
ed95b21a ID |
4123 | } |
4124 | ||
4125 | static void rbd_acquire_lock(struct work_struct *work) | |
4126 | { | |
4127 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), | |
4128 | struct rbd_device, lock_dwork); | |
637cd060 | 4129 | int ret; |
ed95b21a ID |
4130 | |
4131 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
4132 | again: | |
637cd060 ID |
4133 | ret = rbd_try_acquire_lock(rbd_dev); |
4134 | if (ret <= 0) { | |
4135 | dout("%s rbd_dev %p ret %d - done\n", __func__, rbd_dev, ret); | |
ed95b21a ID |
4136 | return; |
4137 | } | |
4138 | ||
4139 | ret = rbd_request_lock(rbd_dev); | |
4140 | if (ret == -ETIMEDOUT) { | |
4141 | goto again; /* treat this as a dead client */ | |
e010dd0a ID |
4142 | } else if (ret == -EROFS) { |
4143 | rbd_warn(rbd_dev, "peer will not release lock"); | |
637cd060 ID |
4144 | down_write(&rbd_dev->lock_rwsem); |
4145 | wake_lock_waiters(rbd_dev, ret); | |
4146 | up_write(&rbd_dev->lock_rwsem); | |
ed95b21a ID |
4147 | } else if (ret < 0) { |
4148 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); | |
4149 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, | |
4150 | RBD_RETRY_DELAY); | |
4151 | } else { | |
4152 | /* | |
4153 | * lock owner acked, but resend if we don't see them | |
4154 | * release the lock | |
4155 | */ | |
6b0a8774 | 4156 | dout("%s rbd_dev %p requeuing lock_dwork\n", __func__, |
ed95b21a ID |
4157 | rbd_dev); |
4158 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, | |
4159 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); | |
4160 | } | |
4161 | } | |
4162 | ||
a2b1da09 | 4163 | static bool rbd_quiesce_lock(struct rbd_device *rbd_dev) |
ed95b21a | 4164 | { |
e1fddc8f ID |
4165 | bool need_wait; |
4166 | ||
a2b1da09 | 4167 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
d9b9c893 | 4168 | lockdep_assert_held_write(&rbd_dev->lock_rwsem); |
a2b1da09 | 4169 | |
ed95b21a ID |
4170 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
4171 | return false; | |
4172 | ||
52bb1f9b | 4173 | /* |
ed95b21a | 4174 | * Ensure that all in-flight IO is flushed. |
52bb1f9b | 4175 | */ |
e1fddc8f ID |
4176 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
4177 | rbd_assert(!completion_done(&rbd_dev->releasing_wait)); | |
4178 | need_wait = !list_empty(&rbd_dev->running_list); | |
4179 | downgrade_write(&rbd_dev->lock_rwsem); | |
4180 | if (need_wait) | |
4181 | wait_for_completion(&rbd_dev->releasing_wait); | |
ed95b21a ID |
4182 | up_read(&rbd_dev->lock_rwsem); |
4183 | ||
4184 | down_write(&rbd_dev->lock_rwsem); | |
ed95b21a ID |
4185 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
4186 | return false; | |
4187 | ||
e1fddc8f | 4188 | rbd_assert(list_empty(&rbd_dev->running_list)); |
a2b1da09 ID |
4189 | return true; |
4190 | } | |
4191 | ||
22e8bd51 ID |
4192 | static void rbd_pre_release_action(struct rbd_device *rbd_dev) |
4193 | { | |
4194 | if (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP) | |
4195 | rbd_object_map_close(rbd_dev); | |
4196 | } | |
4197 | ||
e1fddc8f ID |
4198 | static void __rbd_release_lock(struct rbd_device *rbd_dev) |
4199 | { | |
4200 | rbd_assert(list_empty(&rbd_dev->running_list)); | |
4201 | ||
22e8bd51 | 4202 | rbd_pre_release_action(rbd_dev); |
bbead745 | 4203 | rbd_unlock(rbd_dev); |
e1fddc8f ID |
4204 | } |
4205 | ||
a2b1da09 ID |
4206 | /* |
4207 | * lock_rwsem must be held for write | |
4208 | */ | |
4209 | static void rbd_release_lock(struct rbd_device *rbd_dev) | |
4210 | { | |
4211 | if (!rbd_quiesce_lock(rbd_dev)) | |
4212 | return; | |
4213 | ||
e1fddc8f | 4214 | __rbd_release_lock(rbd_dev); |
a2b1da09 | 4215 | |
bbead745 ID |
4216 | /* |
4217 | * Give others a chance to grab the lock - we would re-acquire | |
637cd060 ID |
4218 | * almost immediately if we got new IO while draining the running |
4219 | * list otherwise. We need to ack our own notifications, so this | |
4220 | * lock_dwork will be requeued from rbd_handle_released_lock() by | |
4221 | * way of maybe_kick_acquire(). | |
bbead745 ID |
4222 | */ |
4223 | cancel_delayed_work(&rbd_dev->lock_dwork); | |
ed95b21a ID |
4224 | } |
4225 | ||
4226 | static void rbd_release_lock_work(struct work_struct *work) | |
4227 | { | |
4228 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, | |
4229 | unlock_work); | |
4230 | ||
4231 | down_write(&rbd_dev->lock_rwsem); | |
4232 | rbd_release_lock(rbd_dev); | |
4233 | up_write(&rbd_dev->lock_rwsem); | |
4234 | } | |
4235 | ||
637cd060 ID |
4236 | static void maybe_kick_acquire(struct rbd_device *rbd_dev) |
4237 | { | |
4238 | bool have_requests; | |
4239 | ||
4240 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
4241 | if (__rbd_is_lock_owner(rbd_dev)) | |
4242 | return; | |
4243 | ||
4244 | spin_lock(&rbd_dev->lock_lists_lock); | |
4245 | have_requests = !list_empty(&rbd_dev->acquiring_list); | |
4246 | spin_unlock(&rbd_dev->lock_lists_lock); | |
4247 | if (have_requests || delayed_work_pending(&rbd_dev->lock_dwork)) { | |
4248 | dout("%s rbd_dev %p kicking lock_dwork\n", __func__, rbd_dev); | |
4249 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); | |
4250 | } | |
4251 | } | |
4252 | ||
ed95b21a ID |
4253 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
4254 | void **p) | |
4255 | { | |
4256 | struct rbd_client_id cid = { 0 }; | |
4257 | ||
4258 | if (struct_v >= 2) { | |
4259 | cid.gid = ceph_decode_64(p); | |
4260 | cid.handle = ceph_decode_64(p); | |
4261 | } | |
4262 | ||
4263 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, | |
4264 | cid.handle); | |
4265 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { | |
4266 | down_write(&rbd_dev->lock_rwsem); | |
4267 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { | |
4268 | /* | |
4269 | * we already know that the remote client is | |
4270 | * the owner | |
4271 | */ | |
4272 | up_write(&rbd_dev->lock_rwsem); | |
4273 | return; | |
4274 | } | |
4275 | ||
4276 | rbd_set_owner_cid(rbd_dev, &cid); | |
4277 | downgrade_write(&rbd_dev->lock_rwsem); | |
4278 | } else { | |
4279 | down_read(&rbd_dev->lock_rwsem); | |
4280 | } | |
4281 | ||
637cd060 | 4282 | maybe_kick_acquire(rbd_dev); |
ed95b21a ID |
4283 | up_read(&rbd_dev->lock_rwsem); |
4284 | } | |
4285 | ||
4286 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, | |
4287 | void **p) | |
4288 | { | |
4289 | struct rbd_client_id cid = { 0 }; | |
4290 | ||
4291 | if (struct_v >= 2) { | |
4292 | cid.gid = ceph_decode_64(p); | |
4293 | cid.handle = ceph_decode_64(p); | |
4294 | } | |
4295 | ||
4296 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, | |
4297 | cid.handle); | |
4298 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { | |
4299 | down_write(&rbd_dev->lock_rwsem); | |
4300 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { | |
4301 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", | |
4302 | __func__, rbd_dev, cid.gid, cid.handle, | |
4303 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); | |
4304 | up_write(&rbd_dev->lock_rwsem); | |
4305 | return; | |
4306 | } | |
4307 | ||
4308 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); | |
4309 | downgrade_write(&rbd_dev->lock_rwsem); | |
4310 | } else { | |
4311 | down_read(&rbd_dev->lock_rwsem); | |
4312 | } | |
4313 | ||
637cd060 | 4314 | maybe_kick_acquire(rbd_dev); |
ed95b21a ID |
4315 | up_read(&rbd_dev->lock_rwsem); |
4316 | } | |
4317 | ||
3b77faa0 ID |
4318 | /* |
4319 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no | |
4320 | * ResponseMessage is needed. | |
4321 | */ | |
4322 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, | |
4323 | void **p) | |
ed95b21a ID |
4324 | { |
4325 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); | |
4326 | struct rbd_client_id cid = { 0 }; | |
3b77faa0 | 4327 | int result = 1; |
ed95b21a ID |
4328 | |
4329 | if (struct_v >= 2) { | |
4330 | cid.gid = ceph_decode_64(p); | |
4331 | cid.handle = ceph_decode_64(p); | |
4332 | } | |
4333 | ||
4334 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, | |
4335 | cid.handle); | |
4336 | if (rbd_cid_equal(&cid, &my_cid)) | |
3b77faa0 | 4337 | return result; |
ed95b21a ID |
4338 | |
4339 | down_read(&rbd_dev->lock_rwsem); | |
3b77faa0 ID |
4340 | if (__rbd_is_lock_owner(rbd_dev)) { |
4341 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && | |
4342 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) | |
4343 | goto out_unlock; | |
4344 | ||
4345 | /* | |
4346 | * encode ResponseMessage(0) so the peer can detect | |
4347 | * a missing owner | |
4348 | */ | |
4349 | result = 0; | |
4350 | ||
4351 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { | |
e010dd0a ID |
4352 | if (!rbd_dev->opts->exclusive) { |
4353 | dout("%s rbd_dev %p queueing unlock_work\n", | |
4354 | __func__, rbd_dev); | |
4355 | queue_work(rbd_dev->task_wq, | |
4356 | &rbd_dev->unlock_work); | |
4357 | } else { | |
4358 | /* refuse to release the lock */ | |
4359 | result = -EROFS; | |
4360 | } | |
ed95b21a ID |
4361 | } |
4362 | } | |
3b77faa0 ID |
4363 | |
4364 | out_unlock: | |
ed95b21a | 4365 | up_read(&rbd_dev->lock_rwsem); |
3b77faa0 | 4366 | return result; |
ed95b21a ID |
4367 | } |
4368 | ||
4369 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, | |
4370 | u64 notify_id, u64 cookie, s32 *result) | |
4371 | { | |
4372 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
08a79102 KS |
4373 | char buf[4 + CEPH_ENCODING_START_BLK_LEN]; |
4374 | int buf_size = sizeof(buf); | |
ed95b21a ID |
4375 | int ret; |
4376 | ||
4377 | if (result) { | |
4378 | void *p = buf; | |
4379 | ||
4380 | /* encode ResponseMessage */ | |
4381 | ceph_start_encoding(&p, 1, 1, | |
4382 | buf_size - CEPH_ENCODING_START_BLK_LEN); | |
4383 | ceph_encode_32(&p, *result); | |
4384 | } else { | |
4385 | buf_size = 0; | |
4386 | } | |
b8d70035 | 4387 | |
922dab61 ID |
4388 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
4389 | &rbd_dev->header_oloc, notify_id, cookie, | |
ed95b21a | 4390 | buf, buf_size); |
52bb1f9b | 4391 | if (ret) |
ed95b21a ID |
4392 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
4393 | } | |
4394 | ||
4395 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, | |
4396 | u64 cookie) | |
4397 | { | |
4398 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
4399 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); | |
4400 | } | |
4401 | ||
4402 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, | |
4403 | u64 notify_id, u64 cookie, s32 result) | |
4404 | { | |
4405 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); | |
4406 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); | |
4407 | } | |
4408 | ||
4409 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, | |
4410 | u64 notifier_id, void *data, size_t data_len) | |
4411 | { | |
4412 | struct rbd_device *rbd_dev = arg; | |
4413 | void *p = data; | |
4414 | void *const end = p + data_len; | |
d4c2269b | 4415 | u8 struct_v = 0; |
ed95b21a ID |
4416 | u32 len; |
4417 | u32 notify_op; | |
4418 | int ret; | |
4419 | ||
4420 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", | |
4421 | __func__, rbd_dev, cookie, notify_id, data_len); | |
4422 | if (data_len) { | |
4423 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", | |
4424 | &struct_v, &len); | |
4425 | if (ret) { | |
4426 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", | |
4427 | ret); | |
4428 | return; | |
4429 | } | |
4430 | ||
4431 | notify_op = ceph_decode_32(&p); | |
4432 | } else { | |
4433 | /* legacy notification for header updates */ | |
4434 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; | |
4435 | len = 0; | |
4436 | } | |
4437 | ||
4438 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); | |
4439 | switch (notify_op) { | |
4440 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: | |
4441 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); | |
4442 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
4443 | break; | |
4444 | case RBD_NOTIFY_OP_RELEASED_LOCK: | |
4445 | rbd_handle_released_lock(rbd_dev, struct_v, &p); | |
4446 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
4447 | break; | |
4448 | case RBD_NOTIFY_OP_REQUEST_LOCK: | |
3b77faa0 ID |
4449 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
4450 | if (ret <= 0) | |
ed95b21a | 4451 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
3b77faa0 | 4452 | cookie, ret); |
ed95b21a ID |
4453 | else |
4454 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
4455 | break; | |
4456 | case RBD_NOTIFY_OP_HEADER_UPDATE: | |
4457 | ret = rbd_dev_refresh(rbd_dev); | |
4458 | if (ret) | |
4459 | rbd_warn(rbd_dev, "refresh failed: %d", ret); | |
4460 | ||
4461 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
4462 | break; | |
4463 | default: | |
4464 | if (rbd_is_lock_owner(rbd_dev)) | |
4465 | rbd_acknowledge_notify_result(rbd_dev, notify_id, | |
4466 | cookie, -EOPNOTSUPP); | |
4467 | else | |
4468 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
4469 | break; | |
4470 | } | |
b8d70035 AE |
4471 | } |
4472 | ||
99d16943 ID |
4473 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
4474 | ||
922dab61 | 4475 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
bb040aa0 | 4476 | { |
922dab61 | 4477 | struct rbd_device *rbd_dev = arg; |
bb040aa0 | 4478 | |
922dab61 | 4479 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
bb040aa0 | 4480 | |
ed95b21a ID |
4481 | down_write(&rbd_dev->lock_rwsem); |
4482 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); | |
4483 | up_write(&rbd_dev->lock_rwsem); | |
4484 | ||
99d16943 ID |
4485 | mutex_lock(&rbd_dev->watch_mutex); |
4486 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { | |
4487 | __rbd_unregister_watch(rbd_dev); | |
4488 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; | |
bb040aa0 | 4489 | |
99d16943 | 4490 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
bb040aa0 | 4491 | } |
99d16943 | 4492 | mutex_unlock(&rbd_dev->watch_mutex); |
bb040aa0 ID |
4493 | } |
4494 | ||
9969ebc5 | 4495 | /* |
99d16943 | 4496 | * watch_mutex must be locked |
9969ebc5 | 4497 | */ |
99d16943 | 4498 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
9969ebc5 AE |
4499 | { |
4500 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
922dab61 | 4501 | struct ceph_osd_linger_request *handle; |
9969ebc5 | 4502 | |
922dab61 | 4503 | rbd_assert(!rbd_dev->watch_handle); |
99d16943 | 4504 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
9969ebc5 | 4505 | |
922dab61 ID |
4506 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
4507 | &rbd_dev->header_oloc, rbd_watch_cb, | |
4508 | rbd_watch_errcb, rbd_dev); | |
4509 | if (IS_ERR(handle)) | |
4510 | return PTR_ERR(handle); | |
8eb87565 | 4511 | |
922dab61 | 4512 | rbd_dev->watch_handle = handle; |
b30a01f2 | 4513 | return 0; |
b30a01f2 ID |
4514 | } |
4515 | ||
99d16943 ID |
4516 | /* |
4517 | * watch_mutex must be locked | |
4518 | */ | |
4519 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) | |
b30a01f2 | 4520 | { |
922dab61 ID |
4521 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
4522 | int ret; | |
b30a01f2 | 4523 | |
99d16943 ID |
4524 | rbd_assert(rbd_dev->watch_handle); |
4525 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
b30a01f2 | 4526 | |
922dab61 ID |
4527 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
4528 | if (ret) | |
4529 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); | |
76756a51 | 4530 | |
922dab61 | 4531 | rbd_dev->watch_handle = NULL; |
c525f036 ID |
4532 | } |
4533 | ||
99d16943 ID |
4534 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
4535 | { | |
4536 | int ret; | |
4537 | ||
4538 | mutex_lock(&rbd_dev->watch_mutex); | |
4539 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); | |
4540 | ret = __rbd_register_watch(rbd_dev); | |
4541 | if (ret) | |
4542 | goto out; | |
4543 | ||
4544 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; | |
4545 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; | |
4546 | ||
4547 | out: | |
4548 | mutex_unlock(&rbd_dev->watch_mutex); | |
4549 | return ret; | |
4550 | } | |
4551 | ||
4552 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) | |
c525f036 | 4553 | { |
99d16943 ID |
4554 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
4555 | ||
ed95b21a ID |
4556 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
4557 | cancel_work_sync(&rbd_dev->released_lock_work); | |
4558 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); | |
4559 | cancel_work_sync(&rbd_dev->unlock_work); | |
99d16943 ID |
4560 | } |
4561 | ||
4562 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) | |
4563 | { | |
4564 | cancel_tasks_sync(rbd_dev); | |
4565 | ||
4566 | mutex_lock(&rbd_dev->watch_mutex); | |
4567 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) | |
4568 | __rbd_unregister_watch(rbd_dev); | |
4569 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; | |
4570 | mutex_unlock(&rbd_dev->watch_mutex); | |
811c6688 | 4571 | |
23edca86 | 4572 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
811c6688 | 4573 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
fca27065 ID |
4574 | } |
4575 | ||
14bb211d ID |
4576 | /* |
4577 | * lock_rwsem must be held for write | |
4578 | */ | |
4579 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) | |
4580 | { | |
4581 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
4582 | char cookie[32]; | |
4583 | int ret; | |
4584 | ||
a2b1da09 ID |
4585 | if (!rbd_quiesce_lock(rbd_dev)) |
4586 | return; | |
14bb211d ID |
4587 | |
4588 | format_lock_cookie(rbd_dev, cookie); | |
4589 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, | |
4590 | &rbd_dev->header_oloc, RBD_LOCK_NAME, | |
4591 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, | |
4592 | RBD_LOCK_TAG, cookie); | |
4593 | if (ret) { | |
4594 | if (ret != -EOPNOTSUPP) | |
4595 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", | |
4596 | ret); | |
4597 | ||
4598 | /* | |
4599 | * Lock cookie cannot be updated on older OSDs, so do | |
4600 | * a manual release and queue an acquire. | |
4601 | */ | |
e1fddc8f | 4602 | __rbd_release_lock(rbd_dev); |
a2b1da09 | 4603 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
14bb211d | 4604 | } else { |
edd8ca80 | 4605 | __rbd_lock(rbd_dev, cookie); |
637cd060 | 4606 | wake_lock_waiters(rbd_dev, 0); |
14bb211d ID |
4607 | } |
4608 | } | |
4609 | ||
99d16943 ID |
4610 | static void rbd_reregister_watch(struct work_struct *work) |
4611 | { | |
4612 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), | |
4613 | struct rbd_device, watch_dwork); | |
4614 | int ret; | |
4615 | ||
4616 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
4617 | ||
4618 | mutex_lock(&rbd_dev->watch_mutex); | |
87c0fded ID |
4619 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
4620 | mutex_unlock(&rbd_dev->watch_mutex); | |
14bb211d | 4621 | return; |
87c0fded | 4622 | } |
99d16943 ID |
4623 | |
4624 | ret = __rbd_register_watch(rbd_dev); | |
4625 | if (ret) { | |
4626 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); | |
637cd060 | 4627 | if (ret != -EBLACKLISTED && ret != -ENOENT) { |
99d16943 ID |
4628 | queue_delayed_work(rbd_dev->task_wq, |
4629 | &rbd_dev->watch_dwork, | |
4630 | RBD_RETRY_DELAY); | |
637cd060 ID |
4631 | mutex_unlock(&rbd_dev->watch_mutex); |
4632 | return; | |
87c0fded | 4633 | } |
637cd060 | 4634 | |
87c0fded | 4635 | mutex_unlock(&rbd_dev->watch_mutex); |
637cd060 ID |
4636 | down_write(&rbd_dev->lock_rwsem); |
4637 | wake_lock_waiters(rbd_dev, ret); | |
4638 | up_write(&rbd_dev->lock_rwsem); | |
14bb211d | 4639 | return; |
99d16943 ID |
4640 | } |
4641 | ||
4642 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; | |
4643 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; | |
4644 | mutex_unlock(&rbd_dev->watch_mutex); | |
4645 | ||
14bb211d ID |
4646 | down_write(&rbd_dev->lock_rwsem); |
4647 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) | |
4648 | rbd_reacquire_lock(rbd_dev); | |
4649 | up_write(&rbd_dev->lock_rwsem); | |
4650 | ||
99d16943 ID |
4651 | ret = rbd_dev_refresh(rbd_dev); |
4652 | if (ret) | |
f6870cc9 | 4653 | rbd_warn(rbd_dev, "reregistration refresh failed: %d", ret); |
99d16943 ID |
4654 | } |
4655 | ||
36be9a76 | 4656 | /* |
f40eb349 AE |
4657 | * Synchronous osd object method call. Returns the number of bytes |
4658 | * returned in the outbound buffer, or a negative error code. | |
36be9a76 AE |
4659 | */ |
4660 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, | |
ecd4a68a ID |
4661 | struct ceph_object_id *oid, |
4662 | struct ceph_object_locator *oloc, | |
36be9a76 | 4663 | const char *method_name, |
4157976b | 4664 | const void *outbound, |
36be9a76 | 4665 | size_t outbound_size, |
4157976b | 4666 | void *inbound, |
e2a58ee5 | 4667 | size_t inbound_size) |
36be9a76 | 4668 | { |
ecd4a68a ID |
4669 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
4670 | struct page *req_page = NULL; | |
4671 | struct page *reply_page; | |
36be9a76 AE |
4672 | int ret; |
4673 | ||
4674 | /* | |
6010a451 AE |
4675 | * Method calls are ultimately read operations. The result |
4676 | * should placed into the inbound buffer provided. They | |
4677 | * also supply outbound data--parameters for the object | |
4678 | * method. Currently if this is present it will be a | |
4679 | * snapshot id. | |
36be9a76 | 4680 | */ |
ecd4a68a ID |
4681 | if (outbound) { |
4682 | if (outbound_size > PAGE_SIZE) | |
4683 | return -E2BIG; | |
36be9a76 | 4684 | |
ecd4a68a ID |
4685 | req_page = alloc_page(GFP_KERNEL); |
4686 | if (!req_page) | |
4687 | return -ENOMEM; | |
04017e29 | 4688 | |
ecd4a68a | 4689 | memcpy(page_address(req_page), outbound, outbound_size); |
04017e29 | 4690 | } |
36be9a76 | 4691 | |
ecd4a68a ID |
4692 | reply_page = alloc_page(GFP_KERNEL); |
4693 | if (!reply_page) { | |
4694 | if (req_page) | |
4695 | __free_page(req_page); | |
4696 | return -ENOMEM; | |
4697 | } | |
57385b51 | 4698 | |
ecd4a68a ID |
4699 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
4700 | CEPH_OSD_FLAG_READ, req_page, outbound_size, | |
68ada915 | 4701 | &reply_page, &inbound_size); |
ecd4a68a ID |
4702 | if (!ret) { |
4703 | memcpy(inbound, page_address(reply_page), inbound_size); | |
4704 | ret = inbound_size; | |
4705 | } | |
36be9a76 | 4706 | |
ecd4a68a ID |
4707 | if (req_page) |
4708 | __free_page(req_page); | |
4709 | __free_page(reply_page); | |
36be9a76 AE |
4710 | return ret; |
4711 | } | |
4712 | ||
7ad18afa | 4713 | static void rbd_queue_workfn(struct work_struct *work) |
bf0d5f50 | 4714 | { |
7ad18afa CH |
4715 | struct request *rq = blk_mq_rq_from_pdu(work); |
4716 | struct rbd_device *rbd_dev = rq->q->queuedata; | |
bc1ecc65 | 4717 | struct rbd_img_request *img_request; |
4e752f0a | 4718 | struct ceph_snap_context *snapc = NULL; |
bc1ecc65 ID |
4719 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
4720 | u64 length = blk_rq_bytes(rq); | |
6d2940c8 | 4721 | enum obj_operation_type op_type; |
4e752f0a | 4722 | u64 mapping_size; |
bf0d5f50 AE |
4723 | int result; |
4724 | ||
aebf526b CH |
4725 | switch (req_op(rq)) { |
4726 | case REQ_OP_DISCARD: | |
90e98c52 | 4727 | op_type = OBJ_OP_DISCARD; |
aebf526b | 4728 | break; |
6484cbe9 ID |
4729 | case REQ_OP_WRITE_ZEROES: |
4730 | op_type = OBJ_OP_ZEROOUT; | |
4731 | break; | |
aebf526b | 4732 | case REQ_OP_WRITE: |
6d2940c8 | 4733 | op_type = OBJ_OP_WRITE; |
aebf526b CH |
4734 | break; |
4735 | case REQ_OP_READ: | |
6d2940c8 | 4736 | op_type = OBJ_OP_READ; |
aebf526b CH |
4737 | break; |
4738 | default: | |
4739 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); | |
4740 | result = -EIO; | |
4741 | goto err; | |
4742 | } | |
6d2940c8 | 4743 | |
bc1ecc65 | 4744 | /* Ignore/skip any zero-length requests */ |
bf0d5f50 | 4745 | |
bc1ecc65 ID |
4746 | if (!length) { |
4747 | dout("%s: zero-length request\n", __func__); | |
4748 | result = 0; | |
4749 | goto err_rq; | |
4750 | } | |
bf0d5f50 | 4751 | |
b948ad78 ID |
4752 | if (op_type != OBJ_OP_READ) { |
4753 | if (rbd_is_ro(rbd_dev)) { | |
4754 | rbd_warn(rbd_dev, "%s on read-only mapping", | |
4755 | obj_op_name(op_type)); | |
4756 | result = -EIO; | |
4757 | goto err; | |
4758 | } | |
4759 | rbd_assert(!rbd_is_snap(rbd_dev)); | |
b91a7bdc | 4760 | } |
4dda41d3 | 4761 | |
bc1ecc65 ID |
4762 | if (offset && length > U64_MAX - offset + 1) { |
4763 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, | |
4764 | length); | |
4765 | result = -EINVAL; | |
4766 | goto err_rq; /* Shouldn't happen */ | |
4767 | } | |
4dda41d3 | 4768 | |
7ad18afa CH |
4769 | blk_mq_start_request(rq); |
4770 | ||
4e752f0a JD |
4771 | down_read(&rbd_dev->header_rwsem); |
4772 | mapping_size = rbd_dev->mapping.size; | |
6d2940c8 | 4773 | if (op_type != OBJ_OP_READ) { |
4e752f0a JD |
4774 | snapc = rbd_dev->header.snapc; |
4775 | ceph_get_snap_context(snapc); | |
4776 | } | |
4777 | up_read(&rbd_dev->header_rwsem); | |
4778 | ||
4779 | if (offset + length > mapping_size) { | |
bc1ecc65 | 4780 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
4e752f0a | 4781 | length, mapping_size); |
bc1ecc65 ID |
4782 | result = -EIO; |
4783 | goto err_rq; | |
4784 | } | |
bf0d5f50 | 4785 | |
dfd9875f | 4786 | img_request = rbd_img_request_create(rbd_dev, op_type, snapc); |
bc1ecc65 ID |
4787 | if (!img_request) { |
4788 | result = -ENOMEM; | |
637cd060 | 4789 | goto err_rq; |
bc1ecc65 ID |
4790 | } |
4791 | img_request->rq = rq; | |
70b16db8 | 4792 | snapc = NULL; /* img_request consumes a ref */ |
bf0d5f50 | 4793 | |
21ed05a8 ID |
4794 | dout("%s rbd_dev %p img_req %p %s %llu~%llu\n", __func__, rbd_dev, |
4795 | img_request, obj_op_name(op_type), offset, length); | |
4796 | ||
6484cbe9 | 4797 | if (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_ZEROOUT) |
5a237819 | 4798 | result = rbd_img_fill_nodata(img_request, offset, length); |
90e98c52 | 4799 | else |
5a237819 ID |
4800 | result = rbd_img_fill_from_bio(img_request, offset, length, |
4801 | rq->bio); | |
0192ce2e | 4802 | if (result) |
bc1ecc65 | 4803 | goto err_img_request; |
bf0d5f50 | 4804 | |
e1fddc8f | 4805 | rbd_img_handle_request(img_request, 0); |
bc1ecc65 | 4806 | return; |
bf0d5f50 | 4807 | |
bc1ecc65 ID |
4808 | err_img_request: |
4809 | rbd_img_request_put(img_request); | |
4810 | err_rq: | |
4811 | if (result) | |
4812 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", | |
6d2940c8 | 4813 | obj_op_name(op_type), length, offset, result); |
e96a650a | 4814 | ceph_put_snap_context(snapc); |
7ad18afa | 4815 | err: |
2a842aca | 4816 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
bc1ecc65 | 4817 | } |
bf0d5f50 | 4818 | |
fc17b653 | 4819 | static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
7ad18afa | 4820 | const struct blk_mq_queue_data *bd) |
bc1ecc65 | 4821 | { |
7ad18afa CH |
4822 | struct request *rq = bd->rq; |
4823 | struct work_struct *work = blk_mq_rq_to_pdu(rq); | |
bf0d5f50 | 4824 | |
7ad18afa | 4825 | queue_work(rbd_wq, work); |
fc17b653 | 4826 | return BLK_STS_OK; |
bf0d5f50 AE |
4827 | } |
4828 | ||
602adf40 YS |
4829 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
4830 | { | |
5769ed0c ID |
4831 | blk_cleanup_queue(rbd_dev->disk->queue); |
4832 | blk_mq_free_tag_set(&rbd_dev->tag_set); | |
4833 | put_disk(rbd_dev->disk); | |
a0cab924 | 4834 | rbd_dev->disk = NULL; |
602adf40 YS |
4835 | } |
4836 | ||
788e2df3 | 4837 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
fe5478e0 ID |
4838 | struct ceph_object_id *oid, |
4839 | struct ceph_object_locator *oloc, | |
4840 | void *buf, int buf_len) | |
788e2df3 AE |
4841 | |
4842 | { | |
fe5478e0 ID |
4843 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
4844 | struct ceph_osd_request *req; | |
4845 | struct page **pages; | |
4846 | int num_pages = calc_pages_for(0, buf_len); | |
788e2df3 AE |
4847 | int ret; |
4848 | ||
fe5478e0 ID |
4849 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
4850 | if (!req) | |
4851 | return -ENOMEM; | |
788e2df3 | 4852 | |
fe5478e0 ID |
4853 | ceph_oid_copy(&req->r_base_oid, oid); |
4854 | ceph_oloc_copy(&req->r_base_oloc, oloc); | |
4855 | req->r_flags = CEPH_OSD_FLAG_READ; | |
430c28c3 | 4856 | |
fe5478e0 ID |
4857 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
4858 | if (IS_ERR(pages)) { | |
4859 | ret = PTR_ERR(pages); | |
4860 | goto out_req; | |
4861 | } | |
1ceae7ef | 4862 | |
fe5478e0 ID |
4863 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
4864 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, | |
4865 | true); | |
4866 | ||
26f887e0 ID |
4867 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
4868 | if (ret) | |
4869 | goto out_req; | |
4870 | ||
fe5478e0 ID |
4871 | ceph_osdc_start_request(osdc, req, false); |
4872 | ret = ceph_osdc_wait_request(osdc, req); | |
4873 | if (ret >= 0) | |
4874 | ceph_copy_from_page_vector(pages, buf, 0, ret); | |
788e2df3 | 4875 | |
fe5478e0 ID |
4876 | out_req: |
4877 | ceph_osdc_put_request(req); | |
788e2df3 AE |
4878 | return ret; |
4879 | } | |
4880 | ||
602adf40 | 4881 | /* |
662518b1 AE |
4882 | * Read the complete header for the given rbd device. On successful |
4883 | * return, the rbd_dev->header field will contain up-to-date | |
4884 | * information about the image. | |
602adf40 | 4885 | */ |
99a41ebc | 4886 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
602adf40 | 4887 | { |
4156d998 | 4888 | struct rbd_image_header_ondisk *ondisk = NULL; |
50f7c4c9 | 4889 | u32 snap_count = 0; |
4156d998 AE |
4890 | u64 names_size = 0; |
4891 | u32 want_count; | |
4892 | int ret; | |
602adf40 | 4893 | |
00f1f36f | 4894 | /* |
4156d998 AE |
4895 | * The complete header will include an array of its 64-bit |
4896 | * snapshot ids, followed by the names of those snapshots as | |
4897 | * a contiguous block of NUL-terminated strings. Note that | |
4898 | * the number of snapshots could change by the time we read | |
4899 | * it in, in which case we re-read it. | |
00f1f36f | 4900 | */ |
4156d998 AE |
4901 | do { |
4902 | size_t size; | |
4903 | ||
4904 | kfree(ondisk); | |
4905 | ||
4906 | size = sizeof (*ondisk); | |
4907 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); | |
4908 | size += names_size; | |
4909 | ondisk = kmalloc(size, GFP_KERNEL); | |
4910 | if (!ondisk) | |
662518b1 | 4911 | return -ENOMEM; |
4156d998 | 4912 | |
fe5478e0 ID |
4913 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
4914 | &rbd_dev->header_oloc, ondisk, size); | |
4156d998 | 4915 | if (ret < 0) |
662518b1 | 4916 | goto out; |
c0cd10db | 4917 | if ((size_t)ret < size) { |
4156d998 | 4918 | ret = -ENXIO; |
06ecc6cb AE |
4919 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
4920 | size, ret); | |
662518b1 | 4921 | goto out; |
4156d998 AE |
4922 | } |
4923 | if (!rbd_dev_ondisk_valid(ondisk)) { | |
4924 | ret = -ENXIO; | |
06ecc6cb | 4925 | rbd_warn(rbd_dev, "invalid header"); |
662518b1 | 4926 | goto out; |
81e759fb | 4927 | } |
602adf40 | 4928 | |
4156d998 AE |
4929 | names_size = le64_to_cpu(ondisk->snap_names_len); |
4930 | want_count = snap_count; | |
4931 | snap_count = le32_to_cpu(ondisk->snap_count); | |
4932 | } while (snap_count != want_count); | |
00f1f36f | 4933 | |
662518b1 AE |
4934 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
4935 | out: | |
4156d998 AE |
4936 | kfree(ondisk); |
4937 | ||
4938 | return ret; | |
602adf40 YS |
4939 | } |
4940 | ||
9875201e JD |
4941 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
4942 | { | |
4943 | sector_t size; | |
9875201e JD |
4944 | |
4945 | /* | |
811c6688 ID |
4946 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
4947 | * try to update its size. If REMOVING is set, updating size | |
4948 | * is just useless work since the device can't be opened. | |
9875201e | 4949 | */ |
811c6688 ID |
4950 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
4951 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { | |
9875201e JD |
4952 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
4953 | dout("setting size to %llu sectors", (unsigned long long)size); | |
4954 | set_capacity(rbd_dev->disk, size); | |
4955 | revalidate_disk(rbd_dev->disk); | |
4956 | } | |
4957 | } | |
4958 | ||
cc4a38bd | 4959 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
1fe5e993 | 4960 | { |
e627db08 | 4961 | u64 mapping_size; |
1fe5e993 AE |
4962 | int ret; |
4963 | ||
cfbf6377 | 4964 | down_write(&rbd_dev->header_rwsem); |
3b5cf2a2 | 4965 | mapping_size = rbd_dev->mapping.size; |
a720ae09 ID |
4966 | |
4967 | ret = rbd_dev_header_info(rbd_dev); | |
52bb1f9b | 4968 | if (ret) |
73e39e4d | 4969 | goto out; |
15228ede | 4970 | |
e8f59b59 ID |
4971 | /* |
4972 | * If there is a parent, see if it has disappeared due to the | |
4973 | * mapped image getting flattened. | |
4974 | */ | |
4975 | if (rbd_dev->parent) { | |
4976 | ret = rbd_dev_v2_parent_info(rbd_dev); | |
4977 | if (ret) | |
73e39e4d | 4978 | goto out; |
e8f59b59 ID |
4979 | } |
4980 | ||
686238b7 ID |
4981 | rbd_assert(!rbd_is_snap(rbd_dev)); |
4982 | rbd_dev->mapping.size = rbd_dev->header.image_size; | |
15228ede | 4983 | |
73e39e4d | 4984 | out: |
cfbf6377 | 4985 | up_write(&rbd_dev->header_rwsem); |
73e39e4d | 4986 | if (!ret && mapping_size != rbd_dev->mapping.size) |
9875201e | 4987 | rbd_dev_update_size(rbd_dev); |
1fe5e993 | 4988 | |
73e39e4d | 4989 | return ret; |
1fe5e993 AE |
4990 | } |
4991 | ||
d6296d39 CH |
4992 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
4993 | unsigned int hctx_idx, unsigned int numa_node) | |
7ad18afa CH |
4994 | { |
4995 | struct work_struct *work = blk_mq_rq_to_pdu(rq); | |
4996 | ||
4997 | INIT_WORK(work, rbd_queue_workfn); | |
4998 | return 0; | |
4999 | } | |
5000 | ||
f363b089 | 5001 | static const struct blk_mq_ops rbd_mq_ops = { |
7ad18afa | 5002 | .queue_rq = rbd_queue_rq, |
7ad18afa CH |
5003 | .init_request = rbd_init_request, |
5004 | }; | |
5005 | ||
602adf40 YS |
5006 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
5007 | { | |
5008 | struct gendisk *disk; | |
5009 | struct request_queue *q; | |
420efbdf ID |
5010 | unsigned int objset_bytes = |
5011 | rbd_dev->layout.object_size * rbd_dev->layout.stripe_count; | |
7ad18afa | 5012 | int err; |
602adf40 | 5013 | |
602adf40 | 5014 | /* create gendisk info */ |
7e513d43 ID |
5015 | disk = alloc_disk(single_major ? |
5016 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : | |
5017 | RBD_MINORS_PER_MAJOR); | |
602adf40 | 5018 | if (!disk) |
1fcdb8aa | 5019 | return -ENOMEM; |
602adf40 | 5020 | |
f0f8cef5 | 5021 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
de71a297 | 5022 | rbd_dev->dev_id); |
602adf40 | 5023 | disk->major = rbd_dev->major; |
dd82fff1 | 5024 | disk->first_minor = rbd_dev->minor; |
7e513d43 ID |
5025 | if (single_major) |
5026 | disk->flags |= GENHD_FL_EXT_DEVT; | |
602adf40 YS |
5027 | disk->fops = &rbd_bd_ops; |
5028 | disk->private_data = rbd_dev; | |
5029 | ||
7ad18afa CH |
5030 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
5031 | rbd_dev->tag_set.ops = &rbd_mq_ops; | |
b5584180 | 5032 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
7ad18afa | 5033 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
56d18f62 | 5034 | rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
7ad18afa CH |
5035 | rbd_dev->tag_set.nr_hw_queues = 1; |
5036 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); | |
5037 | ||
5038 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); | |
5039 | if (err) | |
602adf40 | 5040 | goto out_disk; |
029bcbd8 | 5041 | |
7ad18afa CH |
5042 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
5043 | if (IS_ERR(q)) { | |
5044 | err = PTR_ERR(q); | |
5045 | goto out_tag_set; | |
5046 | } | |
5047 | ||
8b904b5b | 5048 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
d8a2c89c | 5049 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
593a9e7b | 5050 | |
420efbdf | 5051 | blk_queue_max_hw_sectors(q, objset_bytes >> SECTOR_SHIFT); |
0d9fde4f | 5052 | q->limits.max_sectors = queue_max_hw_sectors(q); |
21acdf45 | 5053 | blk_queue_max_segments(q, USHRT_MAX); |
24f1df60 | 5054 | blk_queue_max_segment_size(q, UINT_MAX); |
16d80c54 ID |
5055 | blk_queue_io_min(q, rbd_dev->opts->alloc_size); |
5056 | blk_queue_io_opt(q, rbd_dev->opts->alloc_size); | |
029bcbd8 | 5057 | |
d9360540 ID |
5058 | if (rbd_dev->opts->trim) { |
5059 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); | |
16d80c54 | 5060 | q->limits.discard_granularity = rbd_dev->opts->alloc_size; |
d9360540 ID |
5061 | blk_queue_max_discard_sectors(q, objset_bytes >> SECTOR_SHIFT); |
5062 | blk_queue_max_write_zeroes_sectors(q, objset_bytes >> SECTOR_SHIFT); | |
5063 | } | |
90e98c52 | 5064 | |
bae818ee | 5065 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
dc3b17cc | 5066 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
bae818ee | 5067 | |
5769ed0c ID |
5068 | /* |
5069 | * disk_release() expects a queue ref from add_disk() and will | |
5070 | * put it. Hold an extra ref until add_disk() is called. | |
5071 | */ | |
5072 | WARN_ON(!blk_get_queue(q)); | |
602adf40 | 5073 | disk->queue = q; |
602adf40 YS |
5074 | q->queuedata = rbd_dev; |
5075 | ||
5076 | rbd_dev->disk = disk; | |
602adf40 | 5077 | |
602adf40 | 5078 | return 0; |
7ad18afa CH |
5079 | out_tag_set: |
5080 | blk_mq_free_tag_set(&rbd_dev->tag_set); | |
602adf40 YS |
5081 | out_disk: |
5082 | put_disk(disk); | |
7ad18afa | 5083 | return err; |
602adf40 YS |
5084 | } |
5085 | ||
dfc5606d YS |
5086 | /* |
5087 | sysfs | |
5088 | */ | |
5089 | ||
593a9e7b AE |
5090 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
5091 | { | |
5092 | return container_of(dev, struct rbd_device, dev); | |
5093 | } | |
5094 | ||
dfc5606d YS |
5095 | static ssize_t rbd_size_show(struct device *dev, |
5096 | struct device_attribute *attr, char *buf) | |
5097 | { | |
593a9e7b | 5098 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
a51aa0c0 | 5099 | |
fc71d833 AE |
5100 | return sprintf(buf, "%llu\n", |
5101 | (unsigned long long)rbd_dev->mapping.size); | |
dfc5606d YS |
5102 | } |
5103 | ||
34b13184 AE |
5104 | static ssize_t rbd_features_show(struct device *dev, |
5105 | struct device_attribute *attr, char *buf) | |
5106 | { | |
5107 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5108 | ||
fa58bcad | 5109 | return sprintf(buf, "0x%016llx\n", rbd_dev->header.features); |
34b13184 AE |
5110 | } |
5111 | ||
dfc5606d YS |
5112 | static ssize_t rbd_major_show(struct device *dev, |
5113 | struct device_attribute *attr, char *buf) | |
5114 | { | |
593a9e7b | 5115 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
602adf40 | 5116 | |
fc71d833 AE |
5117 | if (rbd_dev->major) |
5118 | return sprintf(buf, "%d\n", rbd_dev->major); | |
5119 | ||
5120 | return sprintf(buf, "(none)\n"); | |
dd82fff1 ID |
5121 | } |
5122 | ||
5123 | static ssize_t rbd_minor_show(struct device *dev, | |
5124 | struct device_attribute *attr, char *buf) | |
5125 | { | |
5126 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
fc71d833 | 5127 | |
dd82fff1 | 5128 | return sprintf(buf, "%d\n", rbd_dev->minor); |
dfc5606d YS |
5129 | } |
5130 | ||
005a07bf ID |
5131 | static ssize_t rbd_client_addr_show(struct device *dev, |
5132 | struct device_attribute *attr, char *buf) | |
5133 | { | |
5134 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5135 | struct ceph_entity_addr *client_addr = | |
5136 | ceph_client_addr(rbd_dev->rbd_client->client); | |
5137 | ||
5138 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, | |
5139 | le32_to_cpu(client_addr->nonce)); | |
5140 | } | |
5141 | ||
dfc5606d YS |
5142 | static ssize_t rbd_client_id_show(struct device *dev, |
5143 | struct device_attribute *attr, char *buf) | |
602adf40 | 5144 | { |
593a9e7b | 5145 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 5146 | |
1dbb4399 | 5147 | return sprintf(buf, "client%lld\n", |
033268a5 | 5148 | ceph_client_gid(rbd_dev->rbd_client->client)); |
602adf40 YS |
5149 | } |
5150 | ||
267fb90b MC |
5151 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
5152 | struct device_attribute *attr, char *buf) | |
5153 | { | |
5154 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5155 | ||
5156 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); | |
5157 | } | |
5158 | ||
0d6d1e9c MC |
5159 | static ssize_t rbd_config_info_show(struct device *dev, |
5160 | struct device_attribute *attr, char *buf) | |
5161 | { | |
5162 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5163 | ||
5164 | return sprintf(buf, "%s\n", rbd_dev->config_info); | |
602adf40 YS |
5165 | } |
5166 | ||
dfc5606d YS |
5167 | static ssize_t rbd_pool_show(struct device *dev, |
5168 | struct device_attribute *attr, char *buf) | |
602adf40 | 5169 | { |
593a9e7b | 5170 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 5171 | |
0d7dbfce | 5172 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
dfc5606d YS |
5173 | } |
5174 | ||
9bb2f334 AE |
5175 | static ssize_t rbd_pool_id_show(struct device *dev, |
5176 | struct device_attribute *attr, char *buf) | |
5177 | { | |
5178 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5179 | ||
0d7dbfce | 5180 | return sprintf(buf, "%llu\n", |
fc71d833 | 5181 | (unsigned long long) rbd_dev->spec->pool_id); |
9bb2f334 AE |
5182 | } |
5183 | ||
b26c047b ID |
5184 | static ssize_t rbd_pool_ns_show(struct device *dev, |
5185 | struct device_attribute *attr, char *buf) | |
5186 | { | |
5187 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5188 | ||
5189 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_ns ?: ""); | |
5190 | } | |
5191 | ||
dfc5606d YS |
5192 | static ssize_t rbd_name_show(struct device *dev, |
5193 | struct device_attribute *attr, char *buf) | |
5194 | { | |
593a9e7b | 5195 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 5196 | |
a92ffdf8 AE |
5197 | if (rbd_dev->spec->image_name) |
5198 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); | |
5199 | ||
5200 | return sprintf(buf, "(unknown)\n"); | |
dfc5606d YS |
5201 | } |
5202 | ||
589d30e0 AE |
5203 | static ssize_t rbd_image_id_show(struct device *dev, |
5204 | struct device_attribute *attr, char *buf) | |
5205 | { | |
5206 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5207 | ||
0d7dbfce | 5208 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
589d30e0 AE |
5209 | } |
5210 | ||
34b13184 AE |
5211 | /* |
5212 | * Shows the name of the currently-mapped snapshot (or | |
5213 | * RBD_SNAP_HEAD_NAME for the base image). | |
5214 | */ | |
dfc5606d YS |
5215 | static ssize_t rbd_snap_show(struct device *dev, |
5216 | struct device_attribute *attr, | |
5217 | char *buf) | |
5218 | { | |
593a9e7b | 5219 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 5220 | |
0d7dbfce | 5221 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
dfc5606d YS |
5222 | } |
5223 | ||
92a58671 MC |
5224 | static ssize_t rbd_snap_id_show(struct device *dev, |
5225 | struct device_attribute *attr, char *buf) | |
5226 | { | |
5227 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5228 | ||
5229 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); | |
5230 | } | |
5231 | ||
86b00e0d | 5232 | /* |
ff96128f ID |
5233 | * For a v2 image, shows the chain of parent images, separated by empty |
5234 | * lines. For v1 images or if there is no parent, shows "(no parent | |
5235 | * image)". | |
86b00e0d AE |
5236 | */ |
5237 | static ssize_t rbd_parent_show(struct device *dev, | |
ff96128f ID |
5238 | struct device_attribute *attr, |
5239 | char *buf) | |
86b00e0d AE |
5240 | { |
5241 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
ff96128f | 5242 | ssize_t count = 0; |
86b00e0d | 5243 | |
ff96128f | 5244 | if (!rbd_dev->parent) |
86b00e0d AE |
5245 | return sprintf(buf, "(no parent image)\n"); |
5246 | ||
ff96128f ID |
5247 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
5248 | struct rbd_spec *spec = rbd_dev->parent_spec; | |
5249 | ||
5250 | count += sprintf(&buf[count], "%s" | |
5251 | "pool_id %llu\npool_name %s\n" | |
e92c0eaf | 5252 | "pool_ns %s\n" |
ff96128f ID |
5253 | "image_id %s\nimage_name %s\n" |
5254 | "snap_id %llu\nsnap_name %s\n" | |
5255 | "overlap %llu\n", | |
5256 | !count ? "" : "\n", /* first? */ | |
5257 | spec->pool_id, spec->pool_name, | |
e92c0eaf | 5258 | spec->pool_ns ?: "", |
ff96128f ID |
5259 | spec->image_id, spec->image_name ?: "(unknown)", |
5260 | spec->snap_id, spec->snap_name, | |
5261 | rbd_dev->parent_overlap); | |
5262 | } | |
5263 | ||
5264 | return count; | |
86b00e0d AE |
5265 | } |
5266 | ||
dfc5606d YS |
5267 | static ssize_t rbd_image_refresh(struct device *dev, |
5268 | struct device_attribute *attr, | |
5269 | const char *buf, | |
5270 | size_t size) | |
5271 | { | |
593a9e7b | 5272 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
b813623a | 5273 | int ret; |
602adf40 | 5274 | |
cc4a38bd | 5275 | ret = rbd_dev_refresh(rbd_dev); |
e627db08 | 5276 | if (ret) |
52bb1f9b | 5277 | return ret; |
b813623a | 5278 | |
52bb1f9b | 5279 | return size; |
dfc5606d | 5280 | } |
602adf40 | 5281 | |
5657a819 JP |
5282 | static DEVICE_ATTR(size, 0444, rbd_size_show, NULL); |
5283 | static DEVICE_ATTR(features, 0444, rbd_features_show, NULL); | |
5284 | static DEVICE_ATTR(major, 0444, rbd_major_show, NULL); | |
5285 | static DEVICE_ATTR(minor, 0444, rbd_minor_show, NULL); | |
5286 | static DEVICE_ATTR(client_addr, 0444, rbd_client_addr_show, NULL); | |
5287 | static DEVICE_ATTR(client_id, 0444, rbd_client_id_show, NULL); | |
5288 | static DEVICE_ATTR(cluster_fsid, 0444, rbd_cluster_fsid_show, NULL); | |
5289 | static DEVICE_ATTR(config_info, 0400, rbd_config_info_show, NULL); | |
5290 | static DEVICE_ATTR(pool, 0444, rbd_pool_show, NULL); | |
5291 | static DEVICE_ATTR(pool_id, 0444, rbd_pool_id_show, NULL); | |
b26c047b | 5292 | static DEVICE_ATTR(pool_ns, 0444, rbd_pool_ns_show, NULL); |
5657a819 JP |
5293 | static DEVICE_ATTR(name, 0444, rbd_name_show, NULL); |
5294 | static DEVICE_ATTR(image_id, 0444, rbd_image_id_show, NULL); | |
5295 | static DEVICE_ATTR(refresh, 0200, NULL, rbd_image_refresh); | |
5296 | static DEVICE_ATTR(current_snap, 0444, rbd_snap_show, NULL); | |
5297 | static DEVICE_ATTR(snap_id, 0444, rbd_snap_id_show, NULL); | |
5298 | static DEVICE_ATTR(parent, 0444, rbd_parent_show, NULL); | |
dfc5606d YS |
5299 | |
5300 | static struct attribute *rbd_attrs[] = { | |
5301 | &dev_attr_size.attr, | |
34b13184 | 5302 | &dev_attr_features.attr, |
dfc5606d | 5303 | &dev_attr_major.attr, |
dd82fff1 | 5304 | &dev_attr_minor.attr, |
005a07bf | 5305 | &dev_attr_client_addr.attr, |
dfc5606d | 5306 | &dev_attr_client_id.attr, |
267fb90b | 5307 | &dev_attr_cluster_fsid.attr, |
0d6d1e9c | 5308 | &dev_attr_config_info.attr, |
dfc5606d | 5309 | &dev_attr_pool.attr, |
9bb2f334 | 5310 | &dev_attr_pool_id.attr, |
b26c047b | 5311 | &dev_attr_pool_ns.attr, |
dfc5606d | 5312 | &dev_attr_name.attr, |
589d30e0 | 5313 | &dev_attr_image_id.attr, |
dfc5606d | 5314 | &dev_attr_current_snap.attr, |
92a58671 | 5315 | &dev_attr_snap_id.attr, |
86b00e0d | 5316 | &dev_attr_parent.attr, |
dfc5606d | 5317 | &dev_attr_refresh.attr, |
dfc5606d YS |
5318 | NULL |
5319 | }; | |
5320 | ||
5321 | static struct attribute_group rbd_attr_group = { | |
5322 | .attrs = rbd_attrs, | |
5323 | }; | |
5324 | ||
5325 | static const struct attribute_group *rbd_attr_groups[] = { | |
5326 | &rbd_attr_group, | |
5327 | NULL | |
5328 | }; | |
5329 | ||
6cac4695 | 5330 | static void rbd_dev_release(struct device *dev); |
dfc5606d | 5331 | |
b9942bc9 | 5332 | static const struct device_type rbd_device_type = { |
dfc5606d YS |
5333 | .name = "rbd", |
5334 | .groups = rbd_attr_groups, | |
6cac4695 | 5335 | .release = rbd_dev_release, |
dfc5606d YS |
5336 | }; |
5337 | ||
8b8fb99c AE |
5338 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
5339 | { | |
5340 | kref_get(&spec->kref); | |
5341 | ||
5342 | return spec; | |
5343 | } | |
5344 | ||
5345 | static void rbd_spec_free(struct kref *kref); | |
5346 | static void rbd_spec_put(struct rbd_spec *spec) | |
5347 | { | |
5348 | if (spec) | |
5349 | kref_put(&spec->kref, rbd_spec_free); | |
5350 | } | |
5351 | ||
5352 | static struct rbd_spec *rbd_spec_alloc(void) | |
5353 | { | |
5354 | struct rbd_spec *spec; | |
5355 | ||
5356 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); | |
5357 | if (!spec) | |
5358 | return NULL; | |
04077599 ID |
5359 | |
5360 | spec->pool_id = CEPH_NOPOOL; | |
5361 | spec->snap_id = CEPH_NOSNAP; | |
8b8fb99c AE |
5362 | kref_init(&spec->kref); |
5363 | ||
8b8fb99c AE |
5364 | return spec; |
5365 | } | |
5366 | ||
5367 | static void rbd_spec_free(struct kref *kref) | |
5368 | { | |
5369 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); | |
5370 | ||
5371 | kfree(spec->pool_name); | |
b26c047b | 5372 | kfree(spec->pool_ns); |
8b8fb99c AE |
5373 | kfree(spec->image_id); |
5374 | kfree(spec->image_name); | |
5375 | kfree(spec->snap_name); | |
5376 | kfree(spec); | |
5377 | } | |
5378 | ||
1643dfa4 | 5379 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
dd5ac32d | 5380 | { |
99d16943 | 5381 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
ed95b21a | 5382 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
dd5ac32d | 5383 | |
c41d13a3 | 5384 | ceph_oid_destroy(&rbd_dev->header_oid); |
6b6dddbe | 5385 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
0d6d1e9c | 5386 | kfree(rbd_dev->config_info); |
c41d13a3 | 5387 | |
dd5ac32d ID |
5388 | rbd_put_client(rbd_dev->rbd_client); |
5389 | rbd_spec_put(rbd_dev->spec); | |
5390 | kfree(rbd_dev->opts); | |
5391 | kfree(rbd_dev); | |
1643dfa4 ID |
5392 | } |
5393 | ||
5394 | static void rbd_dev_release(struct device *dev) | |
5395 | { | |
5396 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
5397 | bool need_put = !!rbd_dev->opts; | |
5398 | ||
5399 | if (need_put) { | |
5400 | destroy_workqueue(rbd_dev->task_wq); | |
5401 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); | |
5402 | } | |
5403 | ||
5404 | rbd_dev_free(rbd_dev); | |
dd5ac32d ID |
5405 | |
5406 | /* | |
5407 | * This is racy, but way better than putting module outside of | |
5408 | * the release callback. The race window is pretty small, so | |
5409 | * doing something similar to dm (dm-builtin.c) is overkill. | |
5410 | */ | |
5411 | if (need_put) | |
5412 | module_put(THIS_MODULE); | |
5413 | } | |
5414 | ||
1643dfa4 ID |
5415 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
5416 | struct rbd_spec *spec) | |
c53d5893 AE |
5417 | { |
5418 | struct rbd_device *rbd_dev; | |
5419 | ||
1643dfa4 | 5420 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
c53d5893 AE |
5421 | if (!rbd_dev) |
5422 | return NULL; | |
5423 | ||
5424 | spin_lock_init(&rbd_dev->lock); | |
5425 | INIT_LIST_HEAD(&rbd_dev->node); | |
c53d5893 AE |
5426 | init_rwsem(&rbd_dev->header_rwsem); |
5427 | ||
7e97332e | 5428 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
c41d13a3 | 5429 | ceph_oid_init(&rbd_dev->header_oid); |
431a02cd | 5430 | rbd_dev->header_oloc.pool = spec->pool_id; |
b26c047b ID |
5431 | if (spec->pool_ns) { |
5432 | WARN_ON(!*spec->pool_ns); | |
5433 | rbd_dev->header_oloc.pool_ns = | |
5434 | ceph_find_or_create_string(spec->pool_ns, | |
5435 | strlen(spec->pool_ns)); | |
5436 | } | |
c41d13a3 | 5437 | |
99d16943 ID |
5438 | mutex_init(&rbd_dev->watch_mutex); |
5439 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; | |
5440 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); | |
5441 | ||
ed95b21a ID |
5442 | init_rwsem(&rbd_dev->lock_rwsem); |
5443 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; | |
5444 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); | |
5445 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); | |
5446 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); | |
5447 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); | |
e1fddc8f | 5448 | spin_lock_init(&rbd_dev->lock_lists_lock); |
637cd060 | 5449 | INIT_LIST_HEAD(&rbd_dev->acquiring_list); |
e1fddc8f | 5450 | INIT_LIST_HEAD(&rbd_dev->running_list); |
637cd060 | 5451 | init_completion(&rbd_dev->acquire_wait); |
e1fddc8f | 5452 | init_completion(&rbd_dev->releasing_wait); |
ed95b21a | 5453 | |
22e8bd51 | 5454 | spin_lock_init(&rbd_dev->object_map_lock); |
ed95b21a | 5455 | |
dd5ac32d ID |
5456 | rbd_dev->dev.bus = &rbd_bus_type; |
5457 | rbd_dev->dev.type = &rbd_device_type; | |
5458 | rbd_dev->dev.parent = &rbd_root_dev; | |
dd5ac32d ID |
5459 | device_initialize(&rbd_dev->dev); |
5460 | ||
c53d5893 | 5461 | rbd_dev->rbd_client = rbdc; |
d147543d | 5462 | rbd_dev->spec = spec; |
0903e875 | 5463 | |
1643dfa4 ID |
5464 | return rbd_dev; |
5465 | } | |
5466 | ||
5467 | /* | |
5468 | * Create a mapping rbd_dev. | |
5469 | */ | |
5470 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, | |
5471 | struct rbd_spec *spec, | |
5472 | struct rbd_options *opts) | |
5473 | { | |
5474 | struct rbd_device *rbd_dev; | |
5475 | ||
5476 | rbd_dev = __rbd_dev_create(rbdc, spec); | |
5477 | if (!rbd_dev) | |
5478 | return NULL; | |
5479 | ||
5480 | rbd_dev->opts = opts; | |
5481 | ||
5482 | /* get an id and fill in device name */ | |
5483 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, | |
5484 | minor_to_rbd_dev_id(1 << MINORBITS), | |
5485 | GFP_KERNEL); | |
5486 | if (rbd_dev->dev_id < 0) | |
5487 | goto fail_rbd_dev; | |
5488 | ||
5489 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); | |
5490 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, | |
5491 | rbd_dev->name); | |
5492 | if (!rbd_dev->task_wq) | |
5493 | goto fail_dev_id; | |
dd5ac32d | 5494 | |
1643dfa4 ID |
5495 | /* we have a ref from do_rbd_add() */ |
5496 | __module_get(THIS_MODULE); | |
dd5ac32d | 5497 | |
1643dfa4 | 5498 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
c53d5893 | 5499 | return rbd_dev; |
1643dfa4 ID |
5500 | |
5501 | fail_dev_id: | |
5502 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); | |
5503 | fail_rbd_dev: | |
5504 | rbd_dev_free(rbd_dev); | |
5505 | return NULL; | |
c53d5893 AE |
5506 | } |
5507 | ||
5508 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) | |
5509 | { | |
dd5ac32d ID |
5510 | if (rbd_dev) |
5511 | put_device(&rbd_dev->dev); | |
c53d5893 AE |
5512 | } |
5513 | ||
9d475de5 AE |
5514 | /* |
5515 | * Get the size and object order for an image snapshot, or if | |
5516 | * snap_id is CEPH_NOSNAP, gets this information for the base | |
5517 | * image. | |
5518 | */ | |
5519 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, | |
5520 | u8 *order, u64 *snap_size) | |
5521 | { | |
5522 | __le64 snapid = cpu_to_le64(snap_id); | |
5523 | int ret; | |
5524 | struct { | |
5525 | u8 order; | |
5526 | __le64 size; | |
5527 | } __attribute__ ((packed)) size_buf = { 0 }; | |
5528 | ||
ecd4a68a ID |
5529 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
5530 | &rbd_dev->header_oloc, "get_size", | |
5531 | &snapid, sizeof(snapid), | |
5532 | &size_buf, sizeof(size_buf)); | |
36be9a76 | 5533 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
9d475de5 AE |
5534 | if (ret < 0) |
5535 | return ret; | |
57385b51 AE |
5536 | if (ret < sizeof (size_buf)) |
5537 | return -ERANGE; | |
9d475de5 | 5538 | |
c3545579 | 5539 | if (order) { |
c86f86e9 | 5540 | *order = size_buf.order; |
c3545579 JD |
5541 | dout(" order %u", (unsigned int)*order); |
5542 | } | |
9d475de5 AE |
5543 | *snap_size = le64_to_cpu(size_buf.size); |
5544 | ||
c3545579 JD |
5545 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
5546 | (unsigned long long)snap_id, | |
57385b51 | 5547 | (unsigned long long)*snap_size); |
9d475de5 AE |
5548 | |
5549 | return 0; | |
5550 | } | |
5551 | ||
5552 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) | |
5553 | { | |
5554 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, | |
5555 | &rbd_dev->header.obj_order, | |
5556 | &rbd_dev->header.image_size); | |
5557 | } | |
5558 | ||
1e130199 AE |
5559 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
5560 | { | |
5435d206 | 5561 | size_t size; |
1e130199 AE |
5562 | void *reply_buf; |
5563 | int ret; | |
5564 | void *p; | |
5565 | ||
5435d206 DY |
5566 | /* Response will be an encoded string, which includes a length */ |
5567 | size = sizeof(__le32) + RBD_OBJ_PREFIX_LEN_MAX; | |
5568 | reply_buf = kzalloc(size, GFP_KERNEL); | |
1e130199 AE |
5569 | if (!reply_buf) |
5570 | return -ENOMEM; | |
5571 | ||
ecd4a68a ID |
5572 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
5573 | &rbd_dev->header_oloc, "get_object_prefix", | |
5435d206 | 5574 | NULL, 0, reply_buf, size); |
36be9a76 | 5575 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
1e130199 AE |
5576 | if (ret < 0) |
5577 | goto out; | |
5578 | ||
5579 | p = reply_buf; | |
5580 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, | |
57385b51 AE |
5581 | p + ret, NULL, GFP_NOIO); |
5582 | ret = 0; | |
1e130199 AE |
5583 | |
5584 | if (IS_ERR(rbd_dev->header.object_prefix)) { | |
5585 | ret = PTR_ERR(rbd_dev->header.object_prefix); | |
5586 | rbd_dev->header.object_prefix = NULL; | |
5587 | } else { | |
5588 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); | |
5589 | } | |
1e130199 AE |
5590 | out: |
5591 | kfree(reply_buf); | |
5592 | ||
5593 | return ret; | |
5594 | } | |
5595 | ||
b1b5402a | 5596 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
196e2d6d | 5597 | bool read_only, u64 *snap_features) |
b1b5402a | 5598 | { |
196e2d6d ID |
5599 | struct { |
5600 | __le64 snap_id; | |
5601 | u8 read_only; | |
5602 | } features_in; | |
b1b5402a AE |
5603 | struct { |
5604 | __le64 features; | |
5605 | __le64 incompat; | |
4157976b | 5606 | } __attribute__ ((packed)) features_buf = { 0 }; |
d3767f0f | 5607 | u64 unsup; |
b1b5402a AE |
5608 | int ret; |
5609 | ||
196e2d6d ID |
5610 | features_in.snap_id = cpu_to_le64(snap_id); |
5611 | features_in.read_only = read_only; | |
5612 | ||
ecd4a68a ID |
5613 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
5614 | &rbd_dev->header_oloc, "get_features", | |
196e2d6d | 5615 | &features_in, sizeof(features_in), |
ecd4a68a | 5616 | &features_buf, sizeof(features_buf)); |
36be9a76 | 5617 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
b1b5402a AE |
5618 | if (ret < 0) |
5619 | return ret; | |
57385b51 AE |
5620 | if (ret < sizeof (features_buf)) |
5621 | return -ERANGE; | |
d889140c | 5622 | |
d3767f0f ID |
5623 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
5624 | if (unsup) { | |
5625 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", | |
5626 | unsup); | |
b8f5c6ed | 5627 | return -ENXIO; |
d3767f0f | 5628 | } |
d889140c | 5629 | |
b1b5402a AE |
5630 | *snap_features = le64_to_cpu(features_buf.features); |
5631 | ||
5632 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", | |
57385b51 AE |
5633 | (unsigned long long)snap_id, |
5634 | (unsigned long long)*snap_features, | |
5635 | (unsigned long long)le64_to_cpu(features_buf.incompat)); | |
b1b5402a AE |
5636 | |
5637 | return 0; | |
5638 | } | |
5639 | ||
5640 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) | |
5641 | { | |
5642 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, | |
196e2d6d ID |
5643 | rbd_is_ro(rbd_dev), |
5644 | &rbd_dev->header.features); | |
b1b5402a AE |
5645 | } |
5646 | ||
22e8bd51 ID |
5647 | /* |
5648 | * These are generic image flags, but since they are used only for | |
5649 | * object map, store them in rbd_dev->object_map_flags. | |
5650 | * | |
5651 | * For the same reason, this function is called only on object map | |
5652 | * (re)load and not on header refresh. | |
5653 | */ | |
5654 | static int rbd_dev_v2_get_flags(struct rbd_device *rbd_dev) | |
5655 | { | |
5656 | __le64 snapid = cpu_to_le64(rbd_dev->spec->snap_id); | |
5657 | __le64 flags; | |
5658 | int ret; | |
5659 | ||
5660 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, | |
5661 | &rbd_dev->header_oloc, "get_flags", | |
5662 | &snapid, sizeof(snapid), | |
5663 | &flags, sizeof(flags)); | |
5664 | if (ret < 0) | |
5665 | return ret; | |
5666 | if (ret < sizeof(flags)) | |
5667 | return -EBADMSG; | |
5668 | ||
5669 | rbd_dev->object_map_flags = le64_to_cpu(flags); | |
5670 | return 0; | |
5671 | } | |
5672 | ||
eb3b2d6b ID |
5673 | struct parent_image_info { |
5674 | u64 pool_id; | |
e92c0eaf | 5675 | const char *pool_ns; |
eb3b2d6b ID |
5676 | const char *image_id; |
5677 | u64 snap_id; | |
5678 | ||
e92c0eaf | 5679 | bool has_overlap; |
eb3b2d6b ID |
5680 | u64 overlap; |
5681 | }; | |
5682 | ||
e92c0eaf ID |
5683 | /* |
5684 | * The caller is responsible for @pii. | |
5685 | */ | |
5686 | static int decode_parent_image_spec(void **p, void *end, | |
5687 | struct parent_image_info *pii) | |
5688 | { | |
5689 | u8 struct_v; | |
5690 | u32 struct_len; | |
5691 | int ret; | |
5692 | ||
5693 | ret = ceph_start_decoding(p, end, 1, "ParentImageSpec", | |
5694 | &struct_v, &struct_len); | |
5695 | if (ret) | |
5696 | return ret; | |
5697 | ||
5698 | ceph_decode_64_safe(p, end, pii->pool_id, e_inval); | |
5699 | pii->pool_ns = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL); | |
5700 | if (IS_ERR(pii->pool_ns)) { | |
5701 | ret = PTR_ERR(pii->pool_ns); | |
5702 | pii->pool_ns = NULL; | |
5703 | return ret; | |
5704 | } | |
5705 | pii->image_id = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL); | |
5706 | if (IS_ERR(pii->image_id)) { | |
5707 | ret = PTR_ERR(pii->image_id); | |
5708 | pii->image_id = NULL; | |
5709 | return ret; | |
5710 | } | |
5711 | ceph_decode_64_safe(p, end, pii->snap_id, e_inval); | |
5712 | return 0; | |
5713 | ||
5714 | e_inval: | |
5715 | return -EINVAL; | |
5716 | } | |
5717 | ||
5718 | static int __get_parent_info(struct rbd_device *rbd_dev, | |
5719 | struct page *req_page, | |
5720 | struct page *reply_page, | |
5721 | struct parent_image_info *pii) | |
5722 | { | |
5723 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
5724 | size_t reply_len = PAGE_SIZE; | |
5725 | void *p, *end; | |
5726 | int ret; | |
5727 | ||
5728 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, | |
5729 | "rbd", "parent_get", CEPH_OSD_FLAG_READ, | |
68ada915 | 5730 | req_page, sizeof(u64), &reply_page, &reply_len); |
e92c0eaf ID |
5731 | if (ret) |
5732 | return ret == -EOPNOTSUPP ? 1 : ret; | |
5733 | ||
5734 | p = page_address(reply_page); | |
5735 | end = p + reply_len; | |
5736 | ret = decode_parent_image_spec(&p, end, pii); | |
5737 | if (ret) | |
5738 | return ret; | |
5739 | ||
5740 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, | |
5741 | "rbd", "parent_overlap_get", CEPH_OSD_FLAG_READ, | |
68ada915 | 5742 | req_page, sizeof(u64), &reply_page, &reply_len); |
e92c0eaf ID |
5743 | if (ret) |
5744 | return ret; | |
5745 | ||
5746 | p = page_address(reply_page); | |
5747 | end = p + reply_len; | |
5748 | ceph_decode_8_safe(&p, end, pii->has_overlap, e_inval); | |
5749 | if (pii->has_overlap) | |
5750 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); | |
5751 | ||
5752 | return 0; | |
5753 | ||
5754 | e_inval: | |
5755 | return -EINVAL; | |
5756 | } | |
5757 | ||
eb3b2d6b ID |
5758 | /* |
5759 | * The caller is responsible for @pii. | |
5760 | */ | |
5761 | static int __get_parent_info_legacy(struct rbd_device *rbd_dev, | |
5762 | struct page *req_page, | |
5763 | struct page *reply_page, | |
5764 | struct parent_image_info *pii) | |
5765 | { | |
5766 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
5767 | size_t reply_len = PAGE_SIZE; | |
5768 | void *p, *end; | |
5769 | int ret; | |
5770 | ||
5771 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, | |
5772 | "rbd", "get_parent", CEPH_OSD_FLAG_READ, | |
68ada915 | 5773 | req_page, sizeof(u64), &reply_page, &reply_len); |
eb3b2d6b ID |
5774 | if (ret) |
5775 | return ret; | |
5776 | ||
5777 | p = page_address(reply_page); | |
5778 | end = p + reply_len; | |
5779 | ceph_decode_64_safe(&p, end, pii->pool_id, e_inval); | |
5780 | pii->image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); | |
5781 | if (IS_ERR(pii->image_id)) { | |
5782 | ret = PTR_ERR(pii->image_id); | |
5783 | pii->image_id = NULL; | |
5784 | return ret; | |
5785 | } | |
5786 | ceph_decode_64_safe(&p, end, pii->snap_id, e_inval); | |
e92c0eaf | 5787 | pii->has_overlap = true; |
eb3b2d6b ID |
5788 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); |
5789 | ||
5790 | return 0; | |
5791 | ||
5792 | e_inval: | |
5793 | return -EINVAL; | |
5794 | } | |
5795 | ||
5796 | static int get_parent_info(struct rbd_device *rbd_dev, | |
5797 | struct parent_image_info *pii) | |
5798 | { | |
5799 | struct page *req_page, *reply_page; | |
5800 | void *p; | |
5801 | int ret; | |
5802 | ||
5803 | req_page = alloc_page(GFP_KERNEL); | |
5804 | if (!req_page) | |
5805 | return -ENOMEM; | |
5806 | ||
5807 | reply_page = alloc_page(GFP_KERNEL); | |
5808 | if (!reply_page) { | |
5809 | __free_page(req_page); | |
5810 | return -ENOMEM; | |
5811 | } | |
5812 | ||
5813 | p = page_address(req_page); | |
5814 | ceph_encode_64(&p, rbd_dev->spec->snap_id); | |
e92c0eaf ID |
5815 | ret = __get_parent_info(rbd_dev, req_page, reply_page, pii); |
5816 | if (ret > 0) | |
5817 | ret = __get_parent_info_legacy(rbd_dev, req_page, reply_page, | |
5818 | pii); | |
eb3b2d6b ID |
5819 | |
5820 | __free_page(req_page); | |
5821 | __free_page(reply_page); | |
5822 | return ret; | |
5823 | } | |
5824 | ||
86b00e0d AE |
5825 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
5826 | { | |
5827 | struct rbd_spec *parent_spec; | |
eb3b2d6b | 5828 | struct parent_image_info pii = { 0 }; |
86b00e0d AE |
5829 | int ret; |
5830 | ||
5831 | parent_spec = rbd_spec_alloc(); | |
5832 | if (!parent_spec) | |
5833 | return -ENOMEM; | |
5834 | ||
eb3b2d6b ID |
5835 | ret = get_parent_info(rbd_dev, &pii); |
5836 | if (ret) | |
86b00e0d | 5837 | goto out_err; |
86b00e0d | 5838 | |
e92c0eaf ID |
5839 | dout("%s pool_id %llu pool_ns %s image_id %s snap_id %llu has_overlap %d overlap %llu\n", |
5840 | __func__, pii.pool_id, pii.pool_ns, pii.image_id, pii.snap_id, | |
5841 | pii.has_overlap, pii.overlap); | |
86b00e0d | 5842 | |
e92c0eaf | 5843 | if (pii.pool_id == CEPH_NOPOOL || !pii.has_overlap) { |
392a9dad AE |
5844 | /* |
5845 | * Either the parent never existed, or we have | |
5846 | * record of it but the image got flattened so it no | |
5847 | * longer has a parent. When the parent of a | |
5848 | * layered image disappears we immediately set the | |
5849 | * overlap to 0. The effect of this is that all new | |
5850 | * requests will be treated as if the image had no | |
5851 | * parent. | |
e92c0eaf ID |
5852 | * |
5853 | * If !pii.has_overlap, the parent image spec is not | |
5854 | * applicable. It's there to avoid duplication in each | |
5855 | * snapshot record. | |
392a9dad AE |
5856 | */ |
5857 | if (rbd_dev->parent_overlap) { | |
5858 | rbd_dev->parent_overlap = 0; | |
392a9dad AE |
5859 | rbd_dev_parent_put(rbd_dev); |
5860 | pr_info("%s: clone image has been flattened\n", | |
5861 | rbd_dev->disk->disk_name); | |
5862 | } | |
5863 | ||
86b00e0d | 5864 | goto out; /* No parent? No problem. */ |
392a9dad | 5865 | } |
86b00e0d | 5866 | |
0903e875 AE |
5867 | /* The ceph file layout needs to fit pool id in 32 bits */ |
5868 | ||
5869 | ret = -EIO; | |
eb3b2d6b | 5870 | if (pii.pool_id > (u64)U32_MAX) { |
9584d508 | 5871 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
eb3b2d6b | 5872 | (unsigned long long)pii.pool_id, U32_MAX); |
86b00e0d AE |
5873 | goto out_err; |
5874 | } | |
86b00e0d | 5875 | |
3b5cf2a2 AE |
5876 | /* |
5877 | * The parent won't change (except when the clone is | |
5878 | * flattened, already handled that). So we only need to | |
5879 | * record the parent spec we have not already done so. | |
5880 | */ | |
5881 | if (!rbd_dev->parent_spec) { | |
eb3b2d6b | 5882 | parent_spec->pool_id = pii.pool_id; |
e92c0eaf ID |
5883 | if (pii.pool_ns && *pii.pool_ns) { |
5884 | parent_spec->pool_ns = pii.pool_ns; | |
5885 | pii.pool_ns = NULL; | |
5886 | } | |
eb3b2d6b ID |
5887 | parent_spec->image_id = pii.image_id; |
5888 | pii.image_id = NULL; | |
5889 | parent_spec->snap_id = pii.snap_id; | |
b26c047b | 5890 | |
70cf49cf AE |
5891 | rbd_dev->parent_spec = parent_spec; |
5892 | parent_spec = NULL; /* rbd_dev now owns this */ | |
3b5cf2a2 AE |
5893 | } |
5894 | ||
5895 | /* | |
cf32bd9c ID |
5896 | * We always update the parent overlap. If it's zero we issue |
5897 | * a warning, as we will proceed as if there was no parent. | |
3b5cf2a2 | 5898 | */ |
eb3b2d6b | 5899 | if (!pii.overlap) { |
3b5cf2a2 | 5900 | if (parent_spec) { |
cf32bd9c ID |
5901 | /* refresh, careful to warn just once */ |
5902 | if (rbd_dev->parent_overlap) | |
5903 | rbd_warn(rbd_dev, | |
5904 | "clone now standalone (overlap became 0)"); | |
3b5cf2a2 | 5905 | } else { |
cf32bd9c ID |
5906 | /* initial probe */ |
5907 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); | |
3b5cf2a2 | 5908 | } |
70cf49cf | 5909 | } |
eb3b2d6b | 5910 | rbd_dev->parent_overlap = pii.overlap; |
cf32bd9c | 5911 | |
86b00e0d AE |
5912 | out: |
5913 | ret = 0; | |
5914 | out_err: | |
e92c0eaf | 5915 | kfree(pii.pool_ns); |
eb3b2d6b | 5916 | kfree(pii.image_id); |
86b00e0d | 5917 | rbd_spec_put(parent_spec); |
86b00e0d AE |
5918 | return ret; |
5919 | } | |
5920 | ||
cc070d59 AE |
5921 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
5922 | { | |
5923 | struct { | |
5924 | __le64 stripe_unit; | |
5925 | __le64 stripe_count; | |
5926 | } __attribute__ ((packed)) striping_info_buf = { 0 }; | |
5927 | size_t size = sizeof (striping_info_buf); | |
5928 | void *p; | |
cc070d59 AE |
5929 | int ret; |
5930 | ||
ecd4a68a ID |
5931 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
5932 | &rbd_dev->header_oloc, "get_stripe_unit_count", | |
5933 | NULL, 0, &striping_info_buf, size); | |
cc070d59 AE |
5934 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
5935 | if (ret < 0) | |
5936 | return ret; | |
5937 | if (ret < size) | |
5938 | return -ERANGE; | |
5939 | ||
cc070d59 | 5940 | p = &striping_info_buf; |
b1331852 ID |
5941 | rbd_dev->header.stripe_unit = ceph_decode_64(&p); |
5942 | rbd_dev->header.stripe_count = ceph_decode_64(&p); | |
cc070d59 AE |
5943 | return 0; |
5944 | } | |
5945 | ||
7e97332e ID |
5946 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
5947 | { | |
5948 | __le64 data_pool_id; | |
5949 | int ret; | |
5950 | ||
5951 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, | |
5952 | &rbd_dev->header_oloc, "get_data_pool", | |
5953 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); | |
5954 | if (ret < 0) | |
5955 | return ret; | |
5956 | if (ret < sizeof(data_pool_id)) | |
5957 | return -EBADMSG; | |
5958 | ||
5959 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); | |
5960 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); | |
5961 | return 0; | |
5962 | } | |
5963 | ||
9e15b77d AE |
5964 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
5965 | { | |
ecd4a68a | 5966 | CEPH_DEFINE_OID_ONSTACK(oid); |
9e15b77d AE |
5967 | size_t image_id_size; |
5968 | char *image_id; | |
5969 | void *p; | |
5970 | void *end; | |
5971 | size_t size; | |
5972 | void *reply_buf = NULL; | |
5973 | size_t len = 0; | |
5974 | char *image_name = NULL; | |
5975 | int ret; | |
5976 | ||
5977 | rbd_assert(!rbd_dev->spec->image_name); | |
5978 | ||
69e7a02f AE |
5979 | len = strlen(rbd_dev->spec->image_id); |
5980 | image_id_size = sizeof (__le32) + len; | |
9e15b77d AE |
5981 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
5982 | if (!image_id) | |
5983 | return NULL; | |
5984 | ||
5985 | p = image_id; | |
4157976b | 5986 | end = image_id + image_id_size; |
57385b51 | 5987 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
9e15b77d AE |
5988 | |
5989 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; | |
5990 | reply_buf = kmalloc(size, GFP_KERNEL); | |
5991 | if (!reply_buf) | |
5992 | goto out; | |
5993 | ||
ecd4a68a ID |
5994 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
5995 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, | |
5996 | "dir_get_name", image_id, image_id_size, | |
5997 | reply_buf, size); | |
9e15b77d AE |
5998 | if (ret < 0) |
5999 | goto out; | |
6000 | p = reply_buf; | |
f40eb349 AE |
6001 | end = reply_buf + ret; |
6002 | ||
9e15b77d AE |
6003 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
6004 | if (IS_ERR(image_name)) | |
6005 | image_name = NULL; | |
6006 | else | |
6007 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); | |
6008 | out: | |
6009 | kfree(reply_buf); | |
6010 | kfree(image_id); | |
6011 | ||
6012 | return image_name; | |
6013 | } | |
6014 | ||
2ad3d716 AE |
6015 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
6016 | { | |
6017 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; | |
6018 | const char *snap_name; | |
6019 | u32 which = 0; | |
6020 | ||
6021 | /* Skip over names until we find the one we are looking for */ | |
6022 | ||
6023 | snap_name = rbd_dev->header.snap_names; | |
6024 | while (which < snapc->num_snaps) { | |
6025 | if (!strcmp(name, snap_name)) | |
6026 | return snapc->snaps[which]; | |
6027 | snap_name += strlen(snap_name) + 1; | |
6028 | which++; | |
6029 | } | |
6030 | return CEPH_NOSNAP; | |
6031 | } | |
6032 | ||
6033 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) | |
6034 | { | |
6035 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; | |
6036 | u32 which; | |
6037 | bool found = false; | |
6038 | u64 snap_id; | |
6039 | ||
6040 | for (which = 0; !found && which < snapc->num_snaps; which++) { | |
6041 | const char *snap_name; | |
6042 | ||
6043 | snap_id = snapc->snaps[which]; | |
6044 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); | |
efadc98a JD |
6045 | if (IS_ERR(snap_name)) { |
6046 | /* ignore no-longer existing snapshots */ | |
6047 | if (PTR_ERR(snap_name) == -ENOENT) | |
6048 | continue; | |
6049 | else | |
6050 | break; | |
6051 | } | |
2ad3d716 AE |
6052 | found = !strcmp(name, snap_name); |
6053 | kfree(snap_name); | |
6054 | } | |
6055 | return found ? snap_id : CEPH_NOSNAP; | |
6056 | } | |
6057 | ||
6058 | /* | |
6059 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if | |
6060 | * no snapshot by that name is found, or if an error occurs. | |
6061 | */ | |
6062 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) | |
6063 | { | |
6064 | if (rbd_dev->image_format == 1) | |
6065 | return rbd_v1_snap_id_by_name(rbd_dev, name); | |
6066 | ||
6067 | return rbd_v2_snap_id_by_name(rbd_dev, name); | |
6068 | } | |
6069 | ||
9e15b77d | 6070 | /* |
04077599 ID |
6071 | * An image being mapped will have everything but the snap id. |
6072 | */ | |
6073 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) | |
6074 | { | |
6075 | struct rbd_spec *spec = rbd_dev->spec; | |
6076 | ||
6077 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); | |
6078 | rbd_assert(spec->image_id && spec->image_name); | |
6079 | rbd_assert(spec->snap_name); | |
6080 | ||
6081 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { | |
6082 | u64 snap_id; | |
6083 | ||
6084 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); | |
6085 | if (snap_id == CEPH_NOSNAP) | |
6086 | return -ENOENT; | |
6087 | ||
6088 | spec->snap_id = snap_id; | |
6089 | } else { | |
6090 | spec->snap_id = CEPH_NOSNAP; | |
6091 | } | |
6092 | ||
6093 | return 0; | |
6094 | } | |
6095 | ||
6096 | /* | |
6097 | * A parent image will have all ids but none of the names. | |
e1d4213f | 6098 | * |
04077599 ID |
6099 | * All names in an rbd spec are dynamically allocated. It's OK if we |
6100 | * can't figure out the name for an image id. | |
9e15b77d | 6101 | */ |
04077599 | 6102 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
9e15b77d | 6103 | { |
2e9f7f1c AE |
6104 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
6105 | struct rbd_spec *spec = rbd_dev->spec; | |
6106 | const char *pool_name; | |
6107 | const char *image_name; | |
6108 | const char *snap_name; | |
9e15b77d AE |
6109 | int ret; |
6110 | ||
04077599 ID |
6111 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
6112 | rbd_assert(spec->image_id); | |
6113 | rbd_assert(spec->snap_id != CEPH_NOSNAP); | |
9e15b77d | 6114 | |
2e9f7f1c | 6115 | /* Get the pool name; we have to make our own copy of this */ |
9e15b77d | 6116 | |
2e9f7f1c AE |
6117 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
6118 | if (!pool_name) { | |
6119 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); | |
935dc89f AE |
6120 | return -EIO; |
6121 | } | |
2e9f7f1c AE |
6122 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
6123 | if (!pool_name) | |
9e15b77d AE |
6124 | return -ENOMEM; |
6125 | ||
6126 | /* Fetch the image name; tolerate failure here */ | |
6127 | ||
2e9f7f1c AE |
6128 | image_name = rbd_dev_image_name(rbd_dev); |
6129 | if (!image_name) | |
06ecc6cb | 6130 | rbd_warn(rbd_dev, "unable to get image name"); |
9e15b77d | 6131 | |
04077599 | 6132 | /* Fetch the snapshot name */ |
9e15b77d | 6133 | |
2e9f7f1c | 6134 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
da6a6b63 JD |
6135 | if (IS_ERR(snap_name)) { |
6136 | ret = PTR_ERR(snap_name); | |
9e15b77d | 6137 | goto out_err; |
2e9f7f1c AE |
6138 | } |
6139 | ||
6140 | spec->pool_name = pool_name; | |
6141 | spec->image_name = image_name; | |
6142 | spec->snap_name = snap_name; | |
9e15b77d AE |
6143 | |
6144 | return 0; | |
04077599 | 6145 | |
9e15b77d | 6146 | out_err: |
2e9f7f1c AE |
6147 | kfree(image_name); |
6148 | kfree(pool_name); | |
9e15b77d AE |
6149 | return ret; |
6150 | } | |
6151 | ||
cc4a38bd | 6152 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
35d489f9 AE |
6153 | { |
6154 | size_t size; | |
6155 | int ret; | |
6156 | void *reply_buf; | |
6157 | void *p; | |
6158 | void *end; | |
6159 | u64 seq; | |
6160 | u32 snap_count; | |
6161 | struct ceph_snap_context *snapc; | |
6162 | u32 i; | |
6163 | ||
6164 | /* | |
6165 | * We'll need room for the seq value (maximum snapshot id), | |
6166 | * snapshot count, and array of that many snapshot ids. | |
6167 | * For now we have a fixed upper limit on the number we're | |
6168 | * prepared to receive. | |
6169 | */ | |
6170 | size = sizeof (__le64) + sizeof (__le32) + | |
6171 | RBD_MAX_SNAP_COUNT * sizeof (__le64); | |
6172 | reply_buf = kzalloc(size, GFP_KERNEL); | |
6173 | if (!reply_buf) | |
6174 | return -ENOMEM; | |
6175 | ||
ecd4a68a ID |
6176 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
6177 | &rbd_dev->header_oloc, "get_snapcontext", | |
6178 | NULL, 0, reply_buf, size); | |
36be9a76 | 6179 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
35d489f9 AE |
6180 | if (ret < 0) |
6181 | goto out; | |
6182 | ||
35d489f9 | 6183 | p = reply_buf; |
57385b51 AE |
6184 | end = reply_buf + ret; |
6185 | ret = -ERANGE; | |
35d489f9 AE |
6186 | ceph_decode_64_safe(&p, end, seq, out); |
6187 | ceph_decode_32_safe(&p, end, snap_count, out); | |
6188 | ||
6189 | /* | |
6190 | * Make sure the reported number of snapshot ids wouldn't go | |
6191 | * beyond the end of our buffer. But before checking that, | |
6192 | * make sure the computed size of the snapshot context we | |
6193 | * allocate is representable in a size_t. | |
6194 | */ | |
6195 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) | |
6196 | / sizeof (u64)) { | |
6197 | ret = -EINVAL; | |
6198 | goto out; | |
6199 | } | |
6200 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) | |
6201 | goto out; | |
468521c1 | 6202 | ret = 0; |
35d489f9 | 6203 | |
812164f8 | 6204 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
35d489f9 AE |
6205 | if (!snapc) { |
6206 | ret = -ENOMEM; | |
6207 | goto out; | |
6208 | } | |
35d489f9 | 6209 | snapc->seq = seq; |
35d489f9 AE |
6210 | for (i = 0; i < snap_count; i++) |
6211 | snapc->snaps[i] = ceph_decode_64(&p); | |
6212 | ||
49ece554 | 6213 | ceph_put_snap_context(rbd_dev->header.snapc); |
35d489f9 AE |
6214 | rbd_dev->header.snapc = snapc; |
6215 | ||
6216 | dout(" snap context seq = %llu, snap_count = %u\n", | |
57385b51 | 6217 | (unsigned long long)seq, (unsigned int)snap_count); |
35d489f9 AE |
6218 | out: |
6219 | kfree(reply_buf); | |
6220 | ||
57385b51 | 6221 | return ret; |
35d489f9 AE |
6222 | } |
6223 | ||
54cac61f AE |
6224 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
6225 | u64 snap_id) | |
b8b1e2db AE |
6226 | { |
6227 | size_t size; | |
6228 | void *reply_buf; | |
54cac61f | 6229 | __le64 snapid; |
b8b1e2db AE |
6230 | int ret; |
6231 | void *p; | |
6232 | void *end; | |
b8b1e2db AE |
6233 | char *snap_name; |
6234 | ||
6235 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; | |
6236 | reply_buf = kmalloc(size, GFP_KERNEL); | |
6237 | if (!reply_buf) | |
6238 | return ERR_PTR(-ENOMEM); | |
6239 | ||
54cac61f | 6240 | snapid = cpu_to_le64(snap_id); |
ecd4a68a ID |
6241 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
6242 | &rbd_dev->header_oloc, "get_snapshot_name", | |
6243 | &snapid, sizeof(snapid), reply_buf, size); | |
36be9a76 | 6244 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
f40eb349 AE |
6245 | if (ret < 0) { |
6246 | snap_name = ERR_PTR(ret); | |
b8b1e2db | 6247 | goto out; |
f40eb349 | 6248 | } |
b8b1e2db AE |
6249 | |
6250 | p = reply_buf; | |
f40eb349 | 6251 | end = reply_buf + ret; |
e5c35534 | 6252 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
f40eb349 | 6253 | if (IS_ERR(snap_name)) |
b8b1e2db | 6254 | goto out; |
b8b1e2db | 6255 | |
f40eb349 | 6256 | dout(" snap_id 0x%016llx snap_name = %s\n", |
54cac61f | 6257 | (unsigned long long)snap_id, snap_name); |
b8b1e2db AE |
6258 | out: |
6259 | kfree(reply_buf); | |
6260 | ||
f40eb349 | 6261 | return snap_name; |
b8b1e2db AE |
6262 | } |
6263 | ||
2df3fac7 | 6264 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
117973fb | 6265 | { |
2df3fac7 | 6266 | bool first_time = rbd_dev->header.object_prefix == NULL; |
117973fb | 6267 | int ret; |
117973fb | 6268 | |
1617e40c JD |
6269 | ret = rbd_dev_v2_image_size(rbd_dev); |
6270 | if (ret) | |
cfbf6377 | 6271 | return ret; |
1617e40c | 6272 | |
2df3fac7 AE |
6273 | if (first_time) { |
6274 | ret = rbd_dev_v2_header_onetime(rbd_dev); | |
6275 | if (ret) | |
cfbf6377 | 6276 | return ret; |
2df3fac7 AE |
6277 | } |
6278 | ||
cc4a38bd | 6279 | ret = rbd_dev_v2_snap_context(rbd_dev); |
d194cd1d ID |
6280 | if (ret && first_time) { |
6281 | kfree(rbd_dev->header.object_prefix); | |
6282 | rbd_dev->header.object_prefix = NULL; | |
6283 | } | |
117973fb AE |
6284 | |
6285 | return ret; | |
6286 | } | |
6287 | ||
a720ae09 ID |
6288 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
6289 | { | |
6290 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); | |
6291 | ||
6292 | if (rbd_dev->image_format == 1) | |
6293 | return rbd_dev_v1_header_info(rbd_dev); | |
6294 | ||
6295 | return rbd_dev_v2_header_info(rbd_dev); | |
6296 | } | |
6297 | ||
e28fff26 AE |
6298 | /* |
6299 | * Skips over white space at *buf, and updates *buf to point to the | |
6300 | * first found non-space character (if any). Returns the length of | |
593a9e7b AE |
6301 | * the token (string of non-white space characters) found. Note |
6302 | * that *buf must be terminated with '\0'. | |
e28fff26 AE |
6303 | */ |
6304 | static inline size_t next_token(const char **buf) | |
6305 | { | |
6306 | /* | |
6307 | * These are the characters that produce nonzero for | |
6308 | * isspace() in the "C" and "POSIX" locales. | |
6309 | */ | |
6310 | const char *spaces = " \f\n\r\t\v"; | |
6311 | ||
6312 | *buf += strspn(*buf, spaces); /* Find start of token */ | |
6313 | ||
6314 | return strcspn(*buf, spaces); /* Return token length */ | |
6315 | } | |
6316 | ||
ea3352f4 AE |
6317 | /* |
6318 | * Finds the next token in *buf, dynamically allocates a buffer big | |
6319 | * enough to hold a copy of it, and copies the token into the new | |
6320 | * buffer. The copy is guaranteed to be terminated with '\0'. Note | |
6321 | * that a duplicate buffer is created even for a zero-length token. | |
6322 | * | |
6323 | * Returns a pointer to the newly-allocated duplicate, or a null | |
6324 | * pointer if memory for the duplicate was not available. If | |
6325 | * the lenp argument is a non-null pointer, the length of the token | |
6326 | * (not including the '\0') is returned in *lenp. | |
6327 | * | |
6328 | * If successful, the *buf pointer will be updated to point beyond | |
6329 | * the end of the found token. | |
6330 | * | |
6331 | * Note: uses GFP_KERNEL for allocation. | |
6332 | */ | |
6333 | static inline char *dup_token(const char **buf, size_t *lenp) | |
6334 | { | |
6335 | char *dup; | |
6336 | size_t len; | |
6337 | ||
6338 | len = next_token(buf); | |
4caf35f9 | 6339 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
ea3352f4 AE |
6340 | if (!dup) |
6341 | return NULL; | |
ea3352f4 AE |
6342 | *(dup + len) = '\0'; |
6343 | *buf += len; | |
6344 | ||
6345 | if (lenp) | |
6346 | *lenp = len; | |
6347 | ||
6348 | return dup; | |
6349 | } | |
6350 | ||
82995cc6 DH |
6351 | static int rbd_parse_param(struct fs_parameter *param, |
6352 | struct rbd_parse_opts_ctx *pctx) | |
6353 | { | |
6354 | struct rbd_options *opt = pctx->opts; | |
6355 | struct fs_parse_result result; | |
6356 | int token, ret; | |
6357 | ||
6358 | ret = ceph_parse_param(param, pctx->copts, NULL); | |
6359 | if (ret != -ENOPARAM) | |
6360 | return ret; | |
6361 | ||
6362 | token = fs_parse(NULL, &rbd_parameters, param, &result); | |
6363 | dout("%s fs_parse '%s' token %d\n", __func__, param->key, token); | |
6364 | if (token < 0) { | |
6365 | if (token == -ENOPARAM) { | |
6366 | return invalf(NULL, "rbd: Unknown parameter '%s'", | |
6367 | param->key); | |
6368 | } | |
6369 | return token; | |
6370 | } | |
6371 | ||
6372 | switch (token) { | |
6373 | case Opt_queue_depth: | |
6374 | if (result.uint_32 < 1) | |
6375 | goto out_of_range; | |
6376 | opt->queue_depth = result.uint_32; | |
6377 | break; | |
6378 | case Opt_alloc_size: | |
6379 | if (result.uint_32 < SECTOR_SIZE) | |
6380 | goto out_of_range; | |
6381 | if (!is_power_of_2(result.uint_32)) { | |
6382 | return invalf(NULL, "rbd: alloc_size must be a power of 2"); | |
6383 | } | |
6384 | opt->alloc_size = result.uint_32; | |
6385 | break; | |
6386 | case Opt_lock_timeout: | |
6387 | /* 0 is "wait forever" (i.e. infinite timeout) */ | |
6388 | if (result.uint_32 > INT_MAX / 1000) | |
6389 | goto out_of_range; | |
6390 | opt->lock_timeout = msecs_to_jiffies(result.uint_32 * 1000); | |
6391 | break; | |
6392 | case Opt_pool_ns: | |
6393 | kfree(pctx->spec->pool_ns); | |
6394 | pctx->spec->pool_ns = param->string; | |
6395 | param->string = NULL; | |
6396 | break; | |
6397 | case Opt_read_only: | |
6398 | opt->read_only = true; | |
6399 | break; | |
6400 | case Opt_read_write: | |
6401 | opt->read_only = false; | |
6402 | break; | |
6403 | case Opt_lock_on_read: | |
6404 | opt->lock_on_read = true; | |
6405 | break; | |
6406 | case Opt_exclusive: | |
6407 | opt->exclusive = true; | |
6408 | break; | |
6409 | case Opt_notrim: | |
6410 | opt->trim = false; | |
6411 | break; | |
6412 | default: | |
6413 | BUG(); | |
6414 | } | |
6415 | ||
6416 | return 0; | |
6417 | ||
6418 | out_of_range: | |
6419 | return invalf(NULL, "rbd: %s out of range", param->key); | |
6420 | } | |
6421 | ||
6422 | /* | |
6423 | * This duplicates most of generic_parse_monolithic(), untying it from | |
6424 | * fs_context and skipping standard superblock and security options. | |
6425 | */ | |
6426 | static int rbd_parse_options(char *options, struct rbd_parse_opts_ctx *pctx) | |
6427 | { | |
6428 | char *key; | |
6429 | int ret = 0; | |
6430 | ||
6431 | dout("%s '%s'\n", __func__, options); | |
6432 | while ((key = strsep(&options, ",")) != NULL) { | |
6433 | if (*key) { | |
6434 | struct fs_parameter param = { | |
6435 | .key = key, | |
6436 | .type = fs_value_is_string, | |
6437 | }; | |
6438 | char *value = strchr(key, '='); | |
6439 | size_t v_len = 0; | |
6440 | ||
6441 | if (value) { | |
6442 | if (value == key) | |
6443 | continue; | |
6444 | *value++ = 0; | |
6445 | v_len = strlen(value); | |
6446 | } | |
6447 | ||
6448 | ||
6449 | if (v_len > 0) { | |
6450 | param.string = kmemdup_nul(value, v_len, | |
6451 | GFP_KERNEL); | |
6452 | if (!param.string) | |
6453 | return -ENOMEM; | |
6454 | } | |
6455 | param.size = v_len; | |
6456 | ||
6457 | ret = rbd_parse_param(¶m, pctx); | |
6458 | kfree(param.string); | |
6459 | if (ret) | |
6460 | break; | |
6461 | } | |
6462 | } | |
6463 | ||
6464 | return ret; | |
6465 | } | |
6466 | ||
a725f65e | 6467 | /* |
859c31df AE |
6468 | * Parse the options provided for an "rbd add" (i.e., rbd image |
6469 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, | |
6470 | * and the data written is passed here via a NUL-terminated buffer. | |
6471 | * Returns 0 if successful or an error code otherwise. | |
d22f76e7 | 6472 | * |
859c31df AE |
6473 | * The information extracted from these options is recorded in |
6474 | * the other parameters which return dynamically-allocated | |
6475 | * structures: | |
6476 | * ceph_opts | |
6477 | * The address of a pointer that will refer to a ceph options | |
6478 | * structure. Caller must release the returned pointer using | |
6479 | * ceph_destroy_options() when it is no longer needed. | |
6480 | * rbd_opts | |
6481 | * Address of an rbd options pointer. Fully initialized by | |
6482 | * this function; caller must release with kfree(). | |
6483 | * spec | |
6484 | * Address of an rbd image specification pointer. Fully | |
6485 | * initialized by this function based on parsed options. | |
6486 | * Caller must release with rbd_spec_put(). | |
6487 | * | |
6488 | * The options passed take this form: | |
6489 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] | |
6490 | * where: | |
6491 | * <mon_addrs> | |
6492 | * A comma-separated list of one or more monitor addresses. | |
6493 | * A monitor address is an ip address, optionally followed | |
6494 | * by a port number (separated by a colon). | |
6495 | * I.e.: ip1[:port1][,ip2[:port2]...] | |
6496 | * <options> | |
6497 | * A comma-separated list of ceph and/or rbd options. | |
6498 | * <pool_name> | |
6499 | * The name of the rados pool containing the rbd image. | |
6500 | * <image_name> | |
6501 | * The name of the image in that pool to map. | |
6502 | * <snap_id> | |
6503 | * An optional snapshot id. If provided, the mapping will | |
6504 | * present data from the image at the time that snapshot was | |
6505 | * created. The image head is used if no snapshot id is | |
6506 | * provided. Snapshot mappings are always read-only. | |
a725f65e | 6507 | */ |
859c31df | 6508 | static int rbd_add_parse_args(const char *buf, |
dc79b113 | 6509 | struct ceph_options **ceph_opts, |
859c31df AE |
6510 | struct rbd_options **opts, |
6511 | struct rbd_spec **rbd_spec) | |
e28fff26 | 6512 | { |
d22f76e7 | 6513 | size_t len; |
859c31df | 6514 | char *options; |
0ddebc0c | 6515 | const char *mon_addrs; |
ecb4dc22 | 6516 | char *snap_name; |
0ddebc0c | 6517 | size_t mon_addrs_size; |
82995cc6 | 6518 | struct rbd_parse_opts_ctx pctx = { 0 }; |
dc79b113 | 6519 | int ret; |
e28fff26 AE |
6520 | |
6521 | /* The first four tokens are required */ | |
6522 | ||
7ef3214a | 6523 | len = next_token(&buf); |
4fb5d671 AE |
6524 | if (!len) { |
6525 | rbd_warn(NULL, "no monitor address(es) provided"); | |
6526 | return -EINVAL; | |
6527 | } | |
0ddebc0c | 6528 | mon_addrs = buf; |
82995cc6 | 6529 | mon_addrs_size = len; |
7ef3214a | 6530 | buf += len; |
a725f65e | 6531 | |
dc79b113 | 6532 | ret = -EINVAL; |
f28e565a AE |
6533 | options = dup_token(&buf, NULL); |
6534 | if (!options) | |
dc79b113 | 6535 | return -ENOMEM; |
4fb5d671 AE |
6536 | if (!*options) { |
6537 | rbd_warn(NULL, "no options provided"); | |
6538 | goto out_err; | |
6539 | } | |
e28fff26 | 6540 | |
c300156b ID |
6541 | pctx.spec = rbd_spec_alloc(); |
6542 | if (!pctx.spec) | |
f28e565a | 6543 | goto out_mem; |
859c31df | 6544 | |
c300156b ID |
6545 | pctx.spec->pool_name = dup_token(&buf, NULL); |
6546 | if (!pctx.spec->pool_name) | |
859c31df | 6547 | goto out_mem; |
c300156b | 6548 | if (!*pctx.spec->pool_name) { |
4fb5d671 AE |
6549 | rbd_warn(NULL, "no pool name provided"); |
6550 | goto out_err; | |
6551 | } | |
e28fff26 | 6552 | |
c300156b ID |
6553 | pctx.spec->image_name = dup_token(&buf, NULL); |
6554 | if (!pctx.spec->image_name) | |
f28e565a | 6555 | goto out_mem; |
c300156b | 6556 | if (!*pctx.spec->image_name) { |
4fb5d671 AE |
6557 | rbd_warn(NULL, "no image name provided"); |
6558 | goto out_err; | |
6559 | } | |
d4b125e9 | 6560 | |
f28e565a AE |
6561 | /* |
6562 | * Snapshot name is optional; default is to use "-" | |
6563 | * (indicating the head/no snapshot). | |
6564 | */ | |
3feeb894 | 6565 | len = next_token(&buf); |
820a5f3e | 6566 | if (!len) { |
3feeb894 AE |
6567 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
6568 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; | |
f28e565a | 6569 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
dc79b113 | 6570 | ret = -ENAMETOOLONG; |
f28e565a | 6571 | goto out_err; |
849b4260 | 6572 | } |
ecb4dc22 AE |
6573 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
6574 | if (!snap_name) | |
f28e565a | 6575 | goto out_mem; |
ecb4dc22 | 6576 | *(snap_name + len) = '\0'; |
c300156b | 6577 | pctx.spec->snap_name = snap_name; |
e5c35534 | 6578 | |
82995cc6 DH |
6579 | pctx.copts = ceph_alloc_options(); |
6580 | if (!pctx.copts) | |
6581 | goto out_mem; | |
6582 | ||
0ddebc0c | 6583 | /* Initialize all rbd options to the defaults */ |
e28fff26 | 6584 | |
c300156b ID |
6585 | pctx.opts = kzalloc(sizeof(*pctx.opts), GFP_KERNEL); |
6586 | if (!pctx.opts) | |
4e9afeba AE |
6587 | goto out_mem; |
6588 | ||
c300156b ID |
6589 | pctx.opts->read_only = RBD_READ_ONLY_DEFAULT; |
6590 | pctx.opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; | |
0c93e1b7 | 6591 | pctx.opts->alloc_size = RBD_ALLOC_SIZE_DEFAULT; |
c300156b ID |
6592 | pctx.opts->lock_timeout = RBD_LOCK_TIMEOUT_DEFAULT; |
6593 | pctx.opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; | |
6594 | pctx.opts->exclusive = RBD_EXCLUSIVE_DEFAULT; | |
6595 | pctx.opts->trim = RBD_TRIM_DEFAULT; | |
d22f76e7 | 6596 | |
82995cc6 DH |
6597 | ret = ceph_parse_mon_ips(mon_addrs, mon_addrs_size, pctx.copts, NULL); |
6598 | if (ret) | |
dc79b113 | 6599 | goto out_err; |
859c31df | 6600 | |
82995cc6 DH |
6601 | ret = rbd_parse_options(options, &pctx); |
6602 | if (ret) | |
6603 | goto out_err; | |
6604 | ||
6605 | *ceph_opts = pctx.copts; | |
c300156b ID |
6606 | *opts = pctx.opts; |
6607 | *rbd_spec = pctx.spec; | |
82995cc6 | 6608 | kfree(options); |
dc79b113 | 6609 | return 0; |
82995cc6 | 6610 | |
f28e565a | 6611 | out_mem: |
dc79b113 | 6612 | ret = -ENOMEM; |
d22f76e7 | 6613 | out_err: |
c300156b | 6614 | kfree(pctx.opts); |
82995cc6 | 6615 | ceph_destroy_options(pctx.copts); |
c300156b | 6616 | rbd_spec_put(pctx.spec); |
f28e565a | 6617 | kfree(options); |
dc79b113 | 6618 | return ret; |
a725f65e AE |
6619 | } |
6620 | ||
e010dd0a ID |
6621 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
6622 | { | |
6623 | down_write(&rbd_dev->lock_rwsem); | |
6624 | if (__rbd_is_lock_owner(rbd_dev)) | |
e1fddc8f | 6625 | __rbd_release_lock(rbd_dev); |
e010dd0a ID |
6626 | up_write(&rbd_dev->lock_rwsem); |
6627 | } | |
6628 | ||
637cd060 ID |
6629 | /* |
6630 | * If the wait is interrupted, an error is returned even if the lock | |
6631 | * was successfully acquired. rbd_dev_image_unlock() will release it | |
6632 | * if needed. | |
6633 | */ | |
e010dd0a ID |
6634 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
6635 | { | |
637cd060 | 6636 | long ret; |
2f18d466 | 6637 | |
e010dd0a | 6638 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
637cd060 ID |
6639 | if (!rbd_dev->opts->exclusive && !rbd_dev->opts->lock_on_read) |
6640 | return 0; | |
6641 | ||
e010dd0a ID |
6642 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
6643 | return -EINVAL; | |
6644 | } | |
6645 | ||
3fe69921 | 6646 | if (rbd_is_ro(rbd_dev)) |
637cd060 ID |
6647 | return 0; |
6648 | ||
6649 | rbd_assert(!rbd_is_lock_owner(rbd_dev)); | |
6650 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); | |
6651 | ret = wait_for_completion_killable_timeout(&rbd_dev->acquire_wait, | |
6652 | ceph_timeout_jiffies(rbd_dev->opts->lock_timeout)); | |
25e6be21 | 6653 | if (ret > 0) { |
637cd060 | 6654 | ret = rbd_dev->acquire_err; |
25e6be21 DY |
6655 | } else { |
6656 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); | |
6657 | if (!ret) | |
6658 | ret = -ETIMEDOUT; | |
6659 | } | |
637cd060 | 6660 | |
2f18d466 | 6661 | if (ret) { |
637cd060 ID |
6662 | rbd_warn(rbd_dev, "failed to acquire exclusive lock: %ld", ret); |
6663 | return ret; | |
e010dd0a ID |
6664 | } |
6665 | ||
637cd060 ID |
6666 | /* |
6667 | * The lock may have been released by now, unless automatic lock | |
6668 | * transitions are disabled. | |
6669 | */ | |
6670 | rbd_assert(!rbd_dev->opts->exclusive || rbd_is_lock_owner(rbd_dev)); | |
e010dd0a ID |
6671 | return 0; |
6672 | } | |
6673 | ||
589d30e0 AE |
6674 | /* |
6675 | * An rbd format 2 image has a unique identifier, distinct from the | |
6676 | * name given to it by the user. Internally, that identifier is | |
6677 | * what's used to specify the names of objects related to the image. | |
6678 | * | |
6679 | * A special "rbd id" object is used to map an rbd image name to its | |
6680 | * id. If that object doesn't exist, then there is no v2 rbd image | |
6681 | * with the supplied name. | |
6682 | * | |
6683 | * This function will record the given rbd_dev's image_id field if | |
6684 | * it can be determined, and in that case will return 0. If any | |
6685 | * errors occur a negative errno will be returned and the rbd_dev's | |
6686 | * image_id field will be unchanged (and should be NULL). | |
6687 | */ | |
6688 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) | |
6689 | { | |
6690 | int ret; | |
6691 | size_t size; | |
ecd4a68a | 6692 | CEPH_DEFINE_OID_ONSTACK(oid); |
589d30e0 | 6693 | void *response; |
c0fba368 | 6694 | char *image_id; |
2f82ee54 | 6695 | |
2c0d0a10 AE |
6696 | /* |
6697 | * When probing a parent image, the image id is already | |
6698 | * known (and the image name likely is not). There's no | |
c0fba368 AE |
6699 | * need to fetch the image id again in this case. We |
6700 | * do still need to set the image format though. | |
2c0d0a10 | 6701 | */ |
c0fba368 AE |
6702 | if (rbd_dev->spec->image_id) { |
6703 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; | |
6704 | ||
2c0d0a10 | 6705 | return 0; |
c0fba368 | 6706 | } |
2c0d0a10 | 6707 | |
589d30e0 AE |
6708 | /* |
6709 | * First, see if the format 2 image id file exists, and if | |
6710 | * so, get the image's persistent id from it. | |
6711 | */ | |
ecd4a68a ID |
6712 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
6713 | rbd_dev->spec->image_name); | |
6714 | if (ret) | |
6715 | return ret; | |
6716 | ||
6717 | dout("rbd id object name is %s\n", oid.name); | |
589d30e0 AE |
6718 | |
6719 | /* Response will be an encoded string, which includes a length */ | |
589d30e0 AE |
6720 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
6721 | response = kzalloc(size, GFP_NOIO); | |
6722 | if (!response) { | |
6723 | ret = -ENOMEM; | |
6724 | goto out; | |
6725 | } | |
6726 | ||
c0fba368 AE |
6727 | /* If it doesn't exist we'll assume it's a format 1 image */ |
6728 | ||
ecd4a68a ID |
6729 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
6730 | "get_id", NULL, 0, | |
5435d206 | 6731 | response, size); |
36be9a76 | 6732 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
c0fba368 AE |
6733 | if (ret == -ENOENT) { |
6734 | image_id = kstrdup("", GFP_KERNEL); | |
6735 | ret = image_id ? 0 : -ENOMEM; | |
6736 | if (!ret) | |
6737 | rbd_dev->image_format = 1; | |
7dd440c9 | 6738 | } else if (ret >= 0) { |
c0fba368 AE |
6739 | void *p = response; |
6740 | ||
6741 | image_id = ceph_extract_encoded_string(&p, p + ret, | |
979ed480 | 6742 | NULL, GFP_NOIO); |
461f758a | 6743 | ret = PTR_ERR_OR_ZERO(image_id); |
c0fba368 AE |
6744 | if (!ret) |
6745 | rbd_dev->image_format = 2; | |
c0fba368 AE |
6746 | } |
6747 | ||
6748 | if (!ret) { | |
6749 | rbd_dev->spec->image_id = image_id; | |
6750 | dout("image_id is %s\n", image_id); | |
589d30e0 AE |
6751 | } |
6752 | out: | |
6753 | kfree(response); | |
ecd4a68a | 6754 | ceph_oid_destroy(&oid); |
589d30e0 AE |
6755 | return ret; |
6756 | } | |
6757 | ||
3abef3b3 AE |
6758 | /* |
6759 | * Undo whatever state changes are made by v1 or v2 header info | |
6760 | * call. | |
6761 | */ | |
6fd48b3b AE |
6762 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
6763 | { | |
6764 | struct rbd_image_header *header; | |
6765 | ||
e69b8d41 | 6766 | rbd_dev_parent_put(rbd_dev); |
22e8bd51 | 6767 | rbd_object_map_free(rbd_dev); |
da5ef6be | 6768 | rbd_dev_mapping_clear(rbd_dev); |
6fd48b3b AE |
6769 | |
6770 | /* Free dynamic fields from the header, then zero it out */ | |
6771 | ||
6772 | header = &rbd_dev->header; | |
812164f8 | 6773 | ceph_put_snap_context(header->snapc); |
6fd48b3b AE |
6774 | kfree(header->snap_sizes); |
6775 | kfree(header->snap_names); | |
6776 | kfree(header->object_prefix); | |
6777 | memset(header, 0, sizeof (*header)); | |
6778 | } | |
6779 | ||
2df3fac7 | 6780 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
a30b71b9 AE |
6781 | { |
6782 | int ret; | |
a30b71b9 | 6783 | |
1e130199 | 6784 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
57385b51 | 6785 | if (ret) |
b1b5402a AE |
6786 | goto out_err; |
6787 | ||
2df3fac7 AE |
6788 | /* |
6789 | * Get the and check features for the image. Currently the | |
6790 | * features are assumed to never change. | |
6791 | */ | |
b1b5402a | 6792 | ret = rbd_dev_v2_features(rbd_dev); |
57385b51 | 6793 | if (ret) |
9d475de5 | 6794 | goto out_err; |
35d489f9 | 6795 | |
cc070d59 AE |
6796 | /* If the image supports fancy striping, get its parameters */ |
6797 | ||
6798 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { | |
6799 | ret = rbd_dev_v2_striping_info(rbd_dev); | |
6800 | if (ret < 0) | |
6801 | goto out_err; | |
6802 | } | |
a30b71b9 | 6803 | |
7e97332e ID |
6804 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
6805 | ret = rbd_dev_v2_data_pool(rbd_dev); | |
6806 | if (ret) | |
6807 | goto out_err; | |
6808 | } | |
6809 | ||
263423f8 | 6810 | rbd_init_layout(rbd_dev); |
35152979 | 6811 | return 0; |
263423f8 | 6812 | |
9d475de5 | 6813 | out_err: |
642a2537 | 6814 | rbd_dev->header.features = 0; |
1e130199 AE |
6815 | kfree(rbd_dev->header.object_prefix); |
6816 | rbd_dev->header.object_prefix = NULL; | |
9d475de5 | 6817 | return ret; |
a30b71b9 AE |
6818 | } |
6819 | ||
6d69bb53 ID |
6820 | /* |
6821 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> | |
6822 | * rbd_dev_image_probe() recursion depth, which means it's also the | |
6823 | * length of the already discovered part of the parent chain. | |
6824 | */ | |
6825 | static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth) | |
83a06263 | 6826 | { |
2f82ee54 | 6827 | struct rbd_device *parent = NULL; |
124afba2 AE |
6828 | int ret; |
6829 | ||
6830 | if (!rbd_dev->parent_spec) | |
6831 | return 0; | |
124afba2 | 6832 | |
6d69bb53 ID |
6833 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
6834 | pr_info("parent chain is too long (%d)\n", depth); | |
6835 | ret = -EINVAL; | |
6836 | goto out_err; | |
6837 | } | |
6838 | ||
1643dfa4 | 6839 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
1f2c6651 ID |
6840 | if (!parent) { |
6841 | ret = -ENOMEM; | |
124afba2 | 6842 | goto out_err; |
1f2c6651 ID |
6843 | } |
6844 | ||
6845 | /* | |
6846 | * Images related by parent/child relationships always share | |
6847 | * rbd_client and spec/parent_spec, so bump their refcounts. | |
6848 | */ | |
6849 | __rbd_get_client(rbd_dev->rbd_client); | |
6850 | rbd_spec_get(rbd_dev->parent_spec); | |
124afba2 | 6851 | |
39258aa2 ID |
6852 | __set_bit(RBD_DEV_FLAG_READONLY, &parent->flags); |
6853 | ||
6d69bb53 | 6854 | ret = rbd_dev_image_probe(parent, depth); |
124afba2 AE |
6855 | if (ret < 0) |
6856 | goto out_err; | |
1f2c6651 | 6857 | |
124afba2 | 6858 | rbd_dev->parent = parent; |
a2acd00e | 6859 | atomic_set(&rbd_dev->parent_ref, 1); |
124afba2 | 6860 | return 0; |
1f2c6651 | 6861 | |
124afba2 | 6862 | out_err: |
1f2c6651 | 6863 | rbd_dev_unparent(rbd_dev); |
1761b229 | 6864 | rbd_dev_destroy(parent); |
124afba2 AE |
6865 | return ret; |
6866 | } | |
6867 | ||
5769ed0c ID |
6868 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
6869 | { | |
6870 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); | |
5769ed0c ID |
6871 | rbd_free_disk(rbd_dev); |
6872 | if (!single_major) | |
6873 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
6874 | } | |
6875 | ||
811c6688 ID |
6876 | /* |
6877 | * rbd_dev->header_rwsem must be locked for write and will be unlocked | |
6878 | * upon return. | |
6879 | */ | |
200a6a8b | 6880 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
124afba2 | 6881 | { |
83a06263 | 6882 | int ret; |
d1cf5788 | 6883 | |
9b60e70b | 6884 | /* Record our major and minor device numbers. */ |
83a06263 | 6885 | |
9b60e70b ID |
6886 | if (!single_major) { |
6887 | ret = register_blkdev(0, rbd_dev->name); | |
6888 | if (ret < 0) | |
1643dfa4 | 6889 | goto err_out_unlock; |
9b60e70b ID |
6890 | |
6891 | rbd_dev->major = ret; | |
6892 | rbd_dev->minor = 0; | |
6893 | } else { | |
6894 | rbd_dev->major = rbd_major; | |
6895 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); | |
6896 | } | |
83a06263 AE |
6897 | |
6898 | /* Set up the blkdev mapping. */ | |
6899 | ||
6900 | ret = rbd_init_disk(rbd_dev); | |
6901 | if (ret) | |
6902 | goto err_out_blkdev; | |
6903 | ||
f35a4dee | 6904 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
39258aa2 | 6905 | set_disk_ro(rbd_dev->disk, rbd_is_ro(rbd_dev)); |
f35a4dee | 6906 | |
5769ed0c | 6907 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
f35a4dee | 6908 | if (ret) |
da5ef6be | 6909 | goto err_out_disk; |
83a06263 | 6910 | |
129b79d4 | 6911 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
811c6688 | 6912 | up_write(&rbd_dev->header_rwsem); |
5769ed0c | 6913 | return 0; |
2f82ee54 | 6914 | |
83a06263 AE |
6915 | err_out_disk: |
6916 | rbd_free_disk(rbd_dev); | |
6917 | err_out_blkdev: | |
9b60e70b ID |
6918 | if (!single_major) |
6919 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
811c6688 ID |
6920 | err_out_unlock: |
6921 | up_write(&rbd_dev->header_rwsem); | |
83a06263 AE |
6922 | return ret; |
6923 | } | |
6924 | ||
332bb12d AE |
6925 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
6926 | { | |
6927 | struct rbd_spec *spec = rbd_dev->spec; | |
c41d13a3 | 6928 | int ret; |
332bb12d AE |
6929 | |
6930 | /* Record the header object name for this rbd image. */ | |
6931 | ||
6932 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); | |
332bb12d | 6933 | if (rbd_dev->image_format == 1) |
c41d13a3 ID |
6934 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
6935 | spec->image_name, RBD_SUFFIX); | |
332bb12d | 6936 | else |
c41d13a3 ID |
6937 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
6938 | RBD_HEADER_PREFIX, spec->image_id); | |
332bb12d | 6939 | |
c41d13a3 | 6940 | return ret; |
332bb12d AE |
6941 | } |
6942 | ||
b9ef2b88 ID |
6943 | static void rbd_print_dne(struct rbd_device *rbd_dev, bool is_snap) |
6944 | { | |
6945 | if (!is_snap) { | |
6946 | pr_info("image %s/%s%s%s does not exist\n", | |
6947 | rbd_dev->spec->pool_name, | |
6948 | rbd_dev->spec->pool_ns ?: "", | |
6949 | rbd_dev->spec->pool_ns ? "/" : "", | |
6950 | rbd_dev->spec->image_name); | |
6951 | } else { | |
6952 | pr_info("snap %s/%s%s%s@%s does not exist\n", | |
6953 | rbd_dev->spec->pool_name, | |
6954 | rbd_dev->spec->pool_ns ?: "", | |
6955 | rbd_dev->spec->pool_ns ? "/" : "", | |
6956 | rbd_dev->spec->image_name, | |
6957 | rbd_dev->spec->snap_name); | |
6958 | } | |
6959 | } | |
6960 | ||
200a6a8b AE |
6961 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
6962 | { | |
6fd48b3b | 6963 | rbd_dev_unprobe(rbd_dev); |
fd22aef8 ID |
6964 | if (rbd_dev->opts) |
6965 | rbd_unregister_watch(rbd_dev); | |
6fd48b3b AE |
6966 | rbd_dev->image_format = 0; |
6967 | kfree(rbd_dev->spec->image_id); | |
6968 | rbd_dev->spec->image_id = NULL; | |
200a6a8b AE |
6969 | } |
6970 | ||
a30b71b9 AE |
6971 | /* |
6972 | * Probe for the existence of the header object for the given rbd | |
1f3ef788 AE |
6973 | * device. If this image is the one being mapped (i.e., not a |
6974 | * parent), initiate a watch on its header object before using that | |
6975 | * object to get detailed information about the rbd image. | |
a30b71b9 | 6976 | */ |
6d69bb53 | 6977 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth) |
a30b71b9 | 6978 | { |
b9ef2b88 | 6979 | bool need_watch = !rbd_is_ro(rbd_dev); |
a30b71b9 AE |
6980 | int ret; |
6981 | ||
6982 | /* | |
3abef3b3 AE |
6983 | * Get the id from the image id object. Unless there's an |
6984 | * error, rbd_dev->spec->image_id will be filled in with | |
6985 | * a dynamically-allocated string, and rbd_dev->image_format | |
6986 | * will be set to either 1 or 2. | |
a30b71b9 AE |
6987 | */ |
6988 | ret = rbd_dev_image_id(rbd_dev); | |
6989 | if (ret) | |
c0fba368 | 6990 | return ret; |
c0fba368 | 6991 | |
332bb12d AE |
6992 | ret = rbd_dev_header_name(rbd_dev); |
6993 | if (ret) | |
6994 | goto err_out_format; | |
6995 | ||
b9ef2b88 | 6996 | if (need_watch) { |
99d16943 | 6997 | ret = rbd_register_watch(rbd_dev); |
1fe48023 ID |
6998 | if (ret) { |
6999 | if (ret == -ENOENT) | |
b9ef2b88 | 7000 | rbd_print_dne(rbd_dev, false); |
c41d13a3 | 7001 | goto err_out_format; |
1fe48023 | 7002 | } |
1f3ef788 | 7003 | } |
b644de2b | 7004 | |
a720ae09 | 7005 | ret = rbd_dev_header_info(rbd_dev); |
b9ef2b88 ID |
7006 | if (ret) { |
7007 | if (ret == -ENOENT && !need_watch) | |
7008 | rbd_print_dne(rbd_dev, false); | |
b644de2b | 7009 | goto err_out_watch; |
b9ef2b88 | 7010 | } |
83a06263 | 7011 | |
04077599 ID |
7012 | /* |
7013 | * If this image is the one being mapped, we have pool name and | |
7014 | * id, image name and id, and snap name - need to fill snap id. | |
7015 | * Otherwise this is a parent image, identified by pool, image | |
7016 | * and snap ids - need to fill in names for those ids. | |
7017 | */ | |
6d69bb53 | 7018 | if (!depth) |
04077599 ID |
7019 | ret = rbd_spec_fill_snap_id(rbd_dev); |
7020 | else | |
7021 | ret = rbd_spec_fill_names(rbd_dev); | |
1fe48023 ID |
7022 | if (ret) { |
7023 | if (ret == -ENOENT) | |
b9ef2b88 | 7024 | rbd_print_dne(rbd_dev, true); |
33dca39f | 7025 | goto err_out_probe; |
1fe48023 | 7026 | } |
9bb81c9b | 7027 | |
da5ef6be ID |
7028 | ret = rbd_dev_mapping_set(rbd_dev); |
7029 | if (ret) | |
7030 | goto err_out_probe; | |
7031 | ||
f3c0e459 | 7032 | if (rbd_is_snap(rbd_dev) && |
22e8bd51 ID |
7033 | (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)) { |
7034 | ret = rbd_object_map_load(rbd_dev); | |
7035 | if (ret) | |
7036 | goto err_out_probe; | |
7037 | } | |
7038 | ||
e8f59b59 ID |
7039 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
7040 | ret = rbd_dev_v2_parent_info(rbd_dev); | |
7041 | if (ret) | |
7042 | goto err_out_probe; | |
e8f59b59 ID |
7043 | } |
7044 | ||
6d69bb53 | 7045 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
30d60ba2 AE |
7046 | if (ret) |
7047 | goto err_out_probe; | |
7048 | ||
7049 | dout("discovered format %u image, header name is %s\n", | |
c41d13a3 | 7050 | rbd_dev->image_format, rbd_dev->header_oid.name); |
30d60ba2 | 7051 | return 0; |
e8f59b59 | 7052 | |
6fd48b3b AE |
7053 | err_out_probe: |
7054 | rbd_dev_unprobe(rbd_dev); | |
b644de2b | 7055 | err_out_watch: |
b9ef2b88 | 7056 | if (need_watch) |
99d16943 | 7057 | rbd_unregister_watch(rbd_dev); |
332bb12d AE |
7058 | err_out_format: |
7059 | rbd_dev->image_format = 0; | |
5655c4d9 AE |
7060 | kfree(rbd_dev->spec->image_id); |
7061 | rbd_dev->spec->image_id = NULL; | |
a30b71b9 AE |
7062 | return ret; |
7063 | } | |
7064 | ||
9b60e70b ID |
7065 | static ssize_t do_rbd_add(struct bus_type *bus, |
7066 | const char *buf, | |
7067 | size_t count) | |
602adf40 | 7068 | { |
cb8627c7 | 7069 | struct rbd_device *rbd_dev = NULL; |
dc79b113 | 7070 | struct ceph_options *ceph_opts = NULL; |
4e9afeba | 7071 | struct rbd_options *rbd_opts = NULL; |
859c31df | 7072 | struct rbd_spec *spec = NULL; |
9d3997fd | 7073 | struct rbd_client *rbdc; |
b51c83c2 | 7074 | int rc; |
602adf40 YS |
7075 | |
7076 | if (!try_module_get(THIS_MODULE)) | |
7077 | return -ENODEV; | |
7078 | ||
602adf40 | 7079 | /* parse add command */ |
859c31df | 7080 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
dc79b113 | 7081 | if (rc < 0) |
dd5ac32d | 7082 | goto out; |
78cea76e | 7083 | |
9d3997fd AE |
7084 | rbdc = rbd_get_client(ceph_opts); |
7085 | if (IS_ERR(rbdc)) { | |
7086 | rc = PTR_ERR(rbdc); | |
0ddebc0c | 7087 | goto err_out_args; |
9d3997fd | 7088 | } |
602adf40 | 7089 | |
602adf40 | 7090 | /* pick the pool */ |
dd435855 | 7091 | rc = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, spec->pool_name); |
1fe48023 ID |
7092 | if (rc < 0) { |
7093 | if (rc == -ENOENT) | |
7094 | pr_info("pool %s does not exist\n", spec->pool_name); | |
602adf40 | 7095 | goto err_out_client; |
1fe48023 | 7096 | } |
c0cd10db | 7097 | spec->pool_id = (u64)rc; |
859c31df | 7098 | |
d147543d | 7099 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
b51c83c2 ID |
7100 | if (!rbd_dev) { |
7101 | rc = -ENOMEM; | |
bd4ba655 | 7102 | goto err_out_client; |
b51c83c2 | 7103 | } |
c53d5893 AE |
7104 | rbdc = NULL; /* rbd_dev now owns this */ |
7105 | spec = NULL; /* rbd_dev now owns this */ | |
d147543d | 7106 | rbd_opts = NULL; /* rbd_dev now owns this */ |
602adf40 | 7107 | |
39258aa2 ID |
7108 | /* if we are mapping a snapshot it will be a read-only mapping */ |
7109 | if (rbd_dev->opts->read_only || | |
7110 | strcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME)) | |
7111 | __set_bit(RBD_DEV_FLAG_READONLY, &rbd_dev->flags); | |
7112 | ||
0d6d1e9c MC |
7113 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
7114 | if (!rbd_dev->config_info) { | |
7115 | rc = -ENOMEM; | |
7116 | goto err_out_rbd_dev; | |
7117 | } | |
7118 | ||
811c6688 | 7119 | down_write(&rbd_dev->header_rwsem); |
6d69bb53 | 7120 | rc = rbd_dev_image_probe(rbd_dev, 0); |
0d6d1e9c MC |
7121 | if (rc < 0) { |
7122 | up_write(&rbd_dev->header_rwsem); | |
c53d5893 | 7123 | goto err_out_rbd_dev; |
0d6d1e9c | 7124 | } |
05fd6f6f | 7125 | |
0c93e1b7 ID |
7126 | if (rbd_dev->opts->alloc_size > rbd_dev->layout.object_size) { |
7127 | rbd_warn(rbd_dev, "alloc_size adjusted to %u", | |
7128 | rbd_dev->layout.object_size); | |
7129 | rbd_dev->opts->alloc_size = rbd_dev->layout.object_size; | |
7130 | } | |
7131 | ||
b536f69a | 7132 | rc = rbd_dev_device_setup(rbd_dev); |
fd22aef8 | 7133 | if (rc) |
8b679ec5 | 7134 | goto err_out_image_probe; |
3abef3b3 | 7135 | |
637cd060 ID |
7136 | rc = rbd_add_acquire_lock(rbd_dev); |
7137 | if (rc) | |
7138 | goto err_out_image_lock; | |
3abef3b3 | 7139 | |
5769ed0c ID |
7140 | /* Everything's ready. Announce the disk to the world. */ |
7141 | ||
7142 | rc = device_add(&rbd_dev->dev); | |
7143 | if (rc) | |
e010dd0a | 7144 | goto err_out_image_lock; |
5769ed0c ID |
7145 | |
7146 | add_disk(rbd_dev->disk); | |
7147 | /* see rbd_init_disk() */ | |
7148 | blk_put_queue(rbd_dev->disk->queue); | |
7149 | ||
7150 | spin_lock(&rbd_dev_list_lock); | |
7151 | list_add_tail(&rbd_dev->node, &rbd_dev_list); | |
7152 | spin_unlock(&rbd_dev_list_lock); | |
7153 | ||
7154 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, | |
7155 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, | |
7156 | rbd_dev->header.features); | |
dd5ac32d ID |
7157 | rc = count; |
7158 | out: | |
7159 | module_put(THIS_MODULE); | |
7160 | return rc; | |
b536f69a | 7161 | |
e010dd0a ID |
7162 | err_out_image_lock: |
7163 | rbd_dev_image_unlock(rbd_dev); | |
5769ed0c | 7164 | rbd_dev_device_release(rbd_dev); |
8b679ec5 ID |
7165 | err_out_image_probe: |
7166 | rbd_dev_image_release(rbd_dev); | |
c53d5893 AE |
7167 | err_out_rbd_dev: |
7168 | rbd_dev_destroy(rbd_dev); | |
bd4ba655 | 7169 | err_out_client: |
9d3997fd | 7170 | rbd_put_client(rbdc); |
0ddebc0c | 7171 | err_out_args: |
859c31df | 7172 | rbd_spec_put(spec); |
d147543d | 7173 | kfree(rbd_opts); |
dd5ac32d | 7174 | goto out; |
602adf40 YS |
7175 | } |
7176 | ||
7e9586ba | 7177 | static ssize_t add_store(struct bus_type *bus, const char *buf, size_t count) |
9b60e70b ID |
7178 | { |
7179 | if (single_major) | |
7180 | return -EINVAL; | |
7181 | ||
7182 | return do_rbd_add(bus, buf, count); | |
7183 | } | |
7184 | ||
7e9586ba GKH |
7185 | static ssize_t add_single_major_store(struct bus_type *bus, const char *buf, |
7186 | size_t count) | |
9b60e70b ID |
7187 | { |
7188 | return do_rbd_add(bus, buf, count); | |
7189 | } | |
7190 | ||
05a46afd AE |
7191 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
7192 | { | |
ad945fc1 | 7193 | while (rbd_dev->parent) { |
05a46afd AE |
7194 | struct rbd_device *first = rbd_dev; |
7195 | struct rbd_device *second = first->parent; | |
7196 | struct rbd_device *third; | |
7197 | ||
7198 | /* | |
7199 | * Follow to the parent with no grandparent and | |
7200 | * remove it. | |
7201 | */ | |
7202 | while (second && (third = second->parent)) { | |
7203 | first = second; | |
7204 | second = third; | |
7205 | } | |
ad945fc1 | 7206 | rbd_assert(second); |
8ad42cd0 | 7207 | rbd_dev_image_release(second); |
8b679ec5 | 7208 | rbd_dev_destroy(second); |
ad945fc1 AE |
7209 | first->parent = NULL; |
7210 | first->parent_overlap = 0; | |
7211 | ||
7212 | rbd_assert(first->parent_spec); | |
05a46afd AE |
7213 | rbd_spec_put(first->parent_spec); |
7214 | first->parent_spec = NULL; | |
05a46afd AE |
7215 | } |
7216 | } | |
7217 | ||
9b60e70b ID |
7218 | static ssize_t do_rbd_remove(struct bus_type *bus, |
7219 | const char *buf, | |
7220 | size_t count) | |
602adf40 YS |
7221 | { |
7222 | struct rbd_device *rbd_dev = NULL; | |
751cc0e3 AE |
7223 | struct list_head *tmp; |
7224 | int dev_id; | |
0276dca6 | 7225 | char opt_buf[6]; |
0276dca6 | 7226 | bool force = false; |
0d8189e1 | 7227 | int ret; |
602adf40 | 7228 | |
0276dca6 MC |
7229 | dev_id = -1; |
7230 | opt_buf[0] = '\0'; | |
7231 | sscanf(buf, "%d %5s", &dev_id, opt_buf); | |
7232 | if (dev_id < 0) { | |
7233 | pr_err("dev_id out of range\n"); | |
602adf40 | 7234 | return -EINVAL; |
0276dca6 MC |
7235 | } |
7236 | if (opt_buf[0] != '\0') { | |
7237 | if (!strcmp(opt_buf, "force")) { | |
7238 | force = true; | |
7239 | } else { | |
7240 | pr_err("bad remove option at '%s'\n", opt_buf); | |
7241 | return -EINVAL; | |
7242 | } | |
7243 | } | |
602adf40 | 7244 | |
751cc0e3 AE |
7245 | ret = -ENOENT; |
7246 | spin_lock(&rbd_dev_list_lock); | |
7247 | list_for_each(tmp, &rbd_dev_list) { | |
7248 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
7249 | if (rbd_dev->dev_id == dev_id) { | |
7250 | ret = 0; | |
7251 | break; | |
7252 | } | |
42382b70 | 7253 | } |
751cc0e3 AE |
7254 | if (!ret) { |
7255 | spin_lock_irq(&rbd_dev->lock); | |
0276dca6 | 7256 | if (rbd_dev->open_count && !force) |
751cc0e3 | 7257 | ret = -EBUSY; |
85f5a4d6 ID |
7258 | else if (test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
7259 | &rbd_dev->flags)) | |
7260 | ret = -EINPROGRESS; | |
751cc0e3 AE |
7261 | spin_unlock_irq(&rbd_dev->lock); |
7262 | } | |
7263 | spin_unlock(&rbd_dev_list_lock); | |
85f5a4d6 | 7264 | if (ret) |
1ba0f1e7 | 7265 | return ret; |
751cc0e3 | 7266 | |
0276dca6 MC |
7267 | if (force) { |
7268 | /* | |
7269 | * Prevent new IO from being queued and wait for existing | |
7270 | * IO to complete/fail. | |
7271 | */ | |
7272 | blk_mq_freeze_queue(rbd_dev->disk->queue); | |
7273 | blk_set_queue_dying(rbd_dev->disk->queue); | |
7274 | } | |
7275 | ||
5769ed0c ID |
7276 | del_gendisk(rbd_dev->disk); |
7277 | spin_lock(&rbd_dev_list_lock); | |
7278 | list_del_init(&rbd_dev->node); | |
7279 | spin_unlock(&rbd_dev_list_lock); | |
7280 | device_del(&rbd_dev->dev); | |
fca27065 | 7281 | |
e010dd0a | 7282 | rbd_dev_image_unlock(rbd_dev); |
dd5ac32d | 7283 | rbd_dev_device_release(rbd_dev); |
8ad42cd0 | 7284 | rbd_dev_image_release(rbd_dev); |
8b679ec5 | 7285 | rbd_dev_destroy(rbd_dev); |
1ba0f1e7 | 7286 | return count; |
602adf40 YS |
7287 | } |
7288 | ||
7e9586ba | 7289 | static ssize_t remove_store(struct bus_type *bus, const char *buf, size_t count) |
9b60e70b ID |
7290 | { |
7291 | if (single_major) | |
7292 | return -EINVAL; | |
7293 | ||
7294 | return do_rbd_remove(bus, buf, count); | |
7295 | } | |
7296 | ||
7e9586ba GKH |
7297 | static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf, |
7298 | size_t count) | |
9b60e70b ID |
7299 | { |
7300 | return do_rbd_remove(bus, buf, count); | |
7301 | } | |
7302 | ||
602adf40 YS |
7303 | /* |
7304 | * create control files in sysfs | |
dfc5606d | 7305 | * /sys/bus/rbd/... |
602adf40 | 7306 | */ |
7d8dc534 | 7307 | static int __init rbd_sysfs_init(void) |
602adf40 | 7308 | { |
dfc5606d | 7309 | int ret; |
602adf40 | 7310 | |
fed4c143 | 7311 | ret = device_register(&rbd_root_dev); |
21079786 | 7312 | if (ret < 0) |
dfc5606d | 7313 | return ret; |
602adf40 | 7314 | |
fed4c143 AE |
7315 | ret = bus_register(&rbd_bus_type); |
7316 | if (ret < 0) | |
7317 | device_unregister(&rbd_root_dev); | |
602adf40 | 7318 | |
602adf40 YS |
7319 | return ret; |
7320 | } | |
7321 | ||
7d8dc534 | 7322 | static void __exit rbd_sysfs_cleanup(void) |
602adf40 | 7323 | { |
dfc5606d | 7324 | bus_unregister(&rbd_bus_type); |
fed4c143 | 7325 | device_unregister(&rbd_root_dev); |
602adf40 YS |
7326 | } |
7327 | ||
7d8dc534 | 7328 | static int __init rbd_slab_init(void) |
1c2a9dfe AE |
7329 | { |
7330 | rbd_assert(!rbd_img_request_cache); | |
03d94406 | 7331 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
868311b1 AE |
7332 | if (!rbd_img_request_cache) |
7333 | return -ENOMEM; | |
7334 | ||
7335 | rbd_assert(!rbd_obj_request_cache); | |
03d94406 | 7336 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
78c2a44a AE |
7337 | if (!rbd_obj_request_cache) |
7338 | goto out_err; | |
7339 | ||
6c696d85 | 7340 | return 0; |
1c2a9dfe | 7341 | |
6c696d85 | 7342 | out_err: |
868311b1 AE |
7343 | kmem_cache_destroy(rbd_img_request_cache); |
7344 | rbd_img_request_cache = NULL; | |
1c2a9dfe AE |
7345 | return -ENOMEM; |
7346 | } | |
7347 | ||
7348 | static void rbd_slab_exit(void) | |
7349 | { | |
868311b1 AE |
7350 | rbd_assert(rbd_obj_request_cache); |
7351 | kmem_cache_destroy(rbd_obj_request_cache); | |
7352 | rbd_obj_request_cache = NULL; | |
7353 | ||
1c2a9dfe AE |
7354 | rbd_assert(rbd_img_request_cache); |
7355 | kmem_cache_destroy(rbd_img_request_cache); | |
7356 | rbd_img_request_cache = NULL; | |
7357 | } | |
7358 | ||
cc344fa1 | 7359 | static int __init rbd_init(void) |
602adf40 YS |
7360 | { |
7361 | int rc; | |
7362 | ||
1e32d34c AE |
7363 | if (!libceph_compatible(NULL)) { |
7364 | rbd_warn(NULL, "libceph incompatibility (quitting)"); | |
1e32d34c AE |
7365 | return -EINVAL; |
7366 | } | |
e1b4d96d | 7367 | |
1c2a9dfe | 7368 | rc = rbd_slab_init(); |
602adf40 YS |
7369 | if (rc) |
7370 | return rc; | |
e1b4d96d | 7371 | |
f5ee37bd ID |
7372 | /* |
7373 | * The number of active work items is limited by the number of | |
f77303bd | 7374 | * rbd devices * queue depth, so leave @max_active at default. |
f5ee37bd ID |
7375 | */ |
7376 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); | |
7377 | if (!rbd_wq) { | |
7378 | rc = -ENOMEM; | |
7379 | goto err_out_slab; | |
7380 | } | |
7381 | ||
9b60e70b ID |
7382 | if (single_major) { |
7383 | rbd_major = register_blkdev(0, RBD_DRV_NAME); | |
7384 | if (rbd_major < 0) { | |
7385 | rc = rbd_major; | |
f5ee37bd | 7386 | goto err_out_wq; |
9b60e70b ID |
7387 | } |
7388 | } | |
7389 | ||
1c2a9dfe AE |
7390 | rc = rbd_sysfs_init(); |
7391 | if (rc) | |
9b60e70b ID |
7392 | goto err_out_blkdev; |
7393 | ||
7394 | if (single_major) | |
7395 | pr_info("loaded (major %d)\n", rbd_major); | |
7396 | else | |
7397 | pr_info("loaded\n"); | |
1c2a9dfe | 7398 | |
e1b4d96d ID |
7399 | return 0; |
7400 | ||
9b60e70b ID |
7401 | err_out_blkdev: |
7402 | if (single_major) | |
7403 | unregister_blkdev(rbd_major, RBD_DRV_NAME); | |
f5ee37bd ID |
7404 | err_out_wq: |
7405 | destroy_workqueue(rbd_wq); | |
e1b4d96d ID |
7406 | err_out_slab: |
7407 | rbd_slab_exit(); | |
1c2a9dfe | 7408 | return rc; |
602adf40 YS |
7409 | } |
7410 | ||
cc344fa1 | 7411 | static void __exit rbd_exit(void) |
602adf40 | 7412 | { |
ffe312cf | 7413 | ida_destroy(&rbd_dev_id_ida); |
602adf40 | 7414 | rbd_sysfs_cleanup(); |
9b60e70b ID |
7415 | if (single_major) |
7416 | unregister_blkdev(rbd_major, RBD_DRV_NAME); | |
f5ee37bd | 7417 | destroy_workqueue(rbd_wq); |
1c2a9dfe | 7418 | rbd_slab_exit(); |
602adf40 YS |
7419 | } |
7420 | ||
7421 | module_init(rbd_init); | |
7422 | module_exit(rbd_exit); | |
7423 | ||
d552c619 | 7424 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
602adf40 YS |
7425 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
7426 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); | |
602adf40 YS |
7427 | /* following authorship retained from original osdblk.c */ |
7428 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); | |
7429 | ||
90da258b | 7430 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
602adf40 | 7431 | MODULE_LICENSE("GPL"); |