Commit | Line | Data |
---|---|---|
3d14c5d2 | 1 | #include <linux/ceph/ceph_debug.h> |
f24e9980 | 2 | |
3d14c5d2 | 3 | #include <linux/module.h> |
f24e9980 SW |
4 | #include <linux/err.h> |
5 | #include <linux/highmem.h> | |
6 | #include <linux/mm.h> | |
7 | #include <linux/pagemap.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/uaccess.h> | |
68b4476b YS |
10 | #ifdef CONFIG_BLOCK |
11 | #include <linux/bio.h> | |
12 | #endif | |
f24e9980 | 13 | |
3d14c5d2 YS |
14 | #include <linux/ceph/libceph.h> |
15 | #include <linux/ceph/osd_client.h> | |
16 | #include <linux/ceph/messenger.h> | |
17 | #include <linux/ceph/decode.h> | |
18 | #include <linux/ceph/auth.h> | |
19 | #include <linux/ceph/pagelist.h> | |
f24e9980 | 20 | |
c16e7869 SW |
21 | #define OSD_OP_FRONT_LEN 4096 |
22 | #define OSD_OPREPLY_FRONT_LEN 512 | |
0d59ab81 | 23 | |
9e32789f | 24 | static const struct ceph_connection_operations osd_con_ops; |
f24e9980 | 25 | |
f9d25199 | 26 | static void __send_queued(struct ceph_osd_client *osdc); |
6f6c7006 | 27 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd); |
a40c4f10 YS |
28 | static void __register_request(struct ceph_osd_client *osdc, |
29 | struct ceph_osd_request *req); | |
30 | static void __unregister_linger_request(struct ceph_osd_client *osdc, | |
31 | struct ceph_osd_request *req); | |
56e925b6 SW |
32 | static void __send_request(struct ceph_osd_client *osdc, |
33 | struct ceph_osd_request *req); | |
f24e9980 | 34 | |
68b4476b YS |
35 | static int op_has_extent(int op) |
36 | { | |
37 | return (op == CEPH_OSD_OP_READ || | |
38 | op == CEPH_OSD_OP_WRITE); | |
39 | } | |
40 | ||
f24e9980 SW |
41 | /* |
42 | * Implement client access to distributed object storage cluster. | |
43 | * | |
44 | * All data objects are stored within a cluster/cloud of OSDs, or | |
45 | * "object storage devices." (Note that Ceph OSDs have _nothing_ to | |
46 | * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply | |
47 | * remote daemons serving up and coordinating consistent and safe | |
48 | * access to storage. | |
49 | * | |
50 | * Cluster membership and the mapping of data objects onto storage devices | |
51 | * are described by the osd map. | |
52 | * | |
53 | * We keep track of pending OSD requests (read, write), resubmit | |
54 | * requests to different OSDs when the cluster topology/data layout | |
55 | * change, or retry the affected requests when the communications | |
56 | * channel with an OSD is reset. | |
57 | */ | |
58 | ||
59 | /* | |
60 | * calculate the mapping of a file extent onto an object, and fill out the | |
61 | * request accordingly. shorten extent as necessary if it crosses an | |
62 | * object boundary. | |
63 | * | |
64 | * fill osd op in request message. | |
65 | */ | |
e75b45cf | 66 | static int calc_layout(struct ceph_vino vino, |
d63b77f4 SW |
67 | struct ceph_file_layout *layout, |
68 | u64 off, u64 *plen, | |
69 | struct ceph_osd_request *req, | |
70 | struct ceph_osd_req_op *op) | |
f24e9980 | 71 | { |
60e56f13 AE |
72 | u64 orig_len = *plen; |
73 | u64 bno = 0; | |
74 | u64 objoff = 0; | |
75 | u64 objlen = 0; | |
d63b77f4 | 76 | int r; |
f24e9980 | 77 | |
60e56f13 AE |
78 | /* object extent? */ |
79 | r = ceph_calc_file_object_mapping(layout, off, orig_len, &bno, | |
80 | &objoff, &objlen); | |
d63b77f4 SW |
81 | if (r < 0) |
82 | return r; | |
60e56f13 AE |
83 | if (objlen < orig_len) { |
84 | *plen = objlen; | |
85 | dout(" skipping last %llu, final file extent %llu~%llu\n", | |
86 | orig_len - *plen, off, *plen); | |
87 | } | |
88 | ||
89 | if (op_has_extent(op->op)) { | |
90 | u32 osize = le32_to_cpu(layout->fl_object_size); | |
91 | op->extent.offset = objoff; | |
92 | op->extent.length = objlen; | |
93 | if (op->extent.truncate_size <= off - objoff) { | |
94 | op->extent.truncate_size = 0; | |
95 | } else { | |
96 | op->extent.truncate_size -= off - objoff; | |
97 | if (op->extent.truncate_size > osize) | |
98 | op->extent.truncate_size = osize; | |
99 | } | |
100 | } | |
101 | req->r_num_pages = calc_pages_for(off, *plen); | |
102 | req->r_page_alignment = off & ~PAGE_MASK; | |
103 | if (op->op == CEPH_OSD_OP_WRITE) | |
104 | op->payload_len = *plen; | |
105 | ||
106 | dout("calc_layout bno=%llx %llu~%llu (%d pages)\n", | |
107 | bno, objoff, objlen, req->r_num_pages); | |
f24e9980 | 108 | |
2dab036b | 109 | snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno); |
f24e9980 | 110 | req->r_oid_len = strlen(req->r_oid); |
d63b77f4 SW |
111 | |
112 | return r; | |
f24e9980 SW |
113 | } |
114 | ||
f24e9980 SW |
115 | /* |
116 | * requests | |
117 | */ | |
415e49a9 | 118 | void ceph_osdc_release_request(struct kref *kref) |
f24e9980 | 119 | { |
415e49a9 SW |
120 | struct ceph_osd_request *req = container_of(kref, |
121 | struct ceph_osd_request, | |
122 | r_kref); | |
123 | ||
124 | if (req->r_request) | |
125 | ceph_msg_put(req->r_request); | |
0d59ab81 | 126 | if (req->r_con_filling_msg) { |
9cbb1d72 AE |
127 | dout("%s revoking msg %p from con %p\n", __func__, |
128 | req->r_reply, req->r_con_filling_msg); | |
8921d114 | 129 | ceph_msg_revoke_incoming(req->r_reply); |
0d47766f | 130 | req->r_con_filling_msg->ops->put(req->r_con_filling_msg); |
9cbb1d72 | 131 | req->r_con_filling_msg = NULL; |
350b1c32 | 132 | } |
ab8cb34a AE |
133 | if (req->r_reply) |
134 | ceph_msg_put(req->r_reply); | |
415e49a9 SW |
135 | if (req->r_own_pages) |
136 | ceph_release_page_vector(req->r_pages, | |
137 | req->r_num_pages); | |
138 | ceph_put_snap_context(req->r_snapc); | |
c885837f | 139 | ceph_pagelist_release(&req->r_trail); |
415e49a9 SW |
140 | if (req->r_mempool) |
141 | mempool_free(req, req->r_osdc->req_mempool); | |
142 | else | |
143 | kfree(req); | |
f24e9980 | 144 | } |
3d14c5d2 | 145 | EXPORT_SYMBOL(ceph_osdc_release_request); |
68b4476b | 146 | |
3499e8a5 | 147 | struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc, |
f24e9980 | 148 | struct ceph_snap_context *snapc, |
ae7ca4a3 | 149 | unsigned int num_op, |
3499e8a5 | 150 | bool use_mempool, |
54a54007 | 151 | gfp_t gfp_flags) |
f24e9980 SW |
152 | { |
153 | struct ceph_osd_request *req; | |
154 | struct ceph_msg *msg; | |
68b4476b | 155 | size_t msg_size = sizeof(struct ceph_osd_request_head); |
3499e8a5 | 156 | |
68b4476b | 157 | msg_size += num_op*sizeof(struct ceph_osd_op); |
f24e9980 SW |
158 | |
159 | if (use_mempool) { | |
3499e8a5 | 160 | req = mempool_alloc(osdc->req_mempool, gfp_flags); |
f24e9980 SW |
161 | memset(req, 0, sizeof(*req)); |
162 | } else { | |
3499e8a5 | 163 | req = kzalloc(sizeof(*req), gfp_flags); |
f24e9980 SW |
164 | } |
165 | if (req == NULL) | |
a79832f2 | 166 | return NULL; |
f24e9980 | 167 | |
f24e9980 SW |
168 | req->r_osdc = osdc; |
169 | req->r_mempool = use_mempool; | |
68b4476b | 170 | |
415e49a9 | 171 | kref_init(&req->r_kref); |
f24e9980 SW |
172 | init_completion(&req->r_completion); |
173 | init_completion(&req->r_safe_completion); | |
a978fa20 | 174 | RB_CLEAR_NODE(&req->r_node); |
f24e9980 | 175 | INIT_LIST_HEAD(&req->r_unsafe_item); |
a40c4f10 YS |
176 | INIT_LIST_HEAD(&req->r_linger_item); |
177 | INIT_LIST_HEAD(&req->r_linger_osd); | |
935b639a | 178 | INIT_LIST_HEAD(&req->r_req_lru_item); |
cd43045c SW |
179 | INIT_LIST_HEAD(&req->r_osd_item); |
180 | ||
c16e7869 SW |
181 | /* create reply message */ |
182 | if (use_mempool) | |
183 | msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0); | |
184 | else | |
185 | msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, | |
b61c2763 | 186 | OSD_OPREPLY_FRONT_LEN, gfp_flags, true); |
a79832f2 | 187 | if (!msg) { |
c16e7869 | 188 | ceph_osdc_put_request(req); |
a79832f2 | 189 | return NULL; |
c16e7869 SW |
190 | } |
191 | req->r_reply = msg; | |
192 | ||
c885837f | 193 | ceph_pagelist_init(&req->r_trail); |
d50b409f | 194 | |
c16e7869 | 195 | /* create request message; allow space for oid */ |
224736d9 | 196 | msg_size += MAX_OBJ_NAME_SIZE; |
f24e9980 SW |
197 | if (snapc) |
198 | msg_size += sizeof(u64) * snapc->num_snaps; | |
199 | if (use_mempool) | |
8f3bc053 | 200 | msg = ceph_msgpool_get(&osdc->msgpool_op, 0); |
f24e9980 | 201 | else |
b61c2763 | 202 | msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true); |
a79832f2 | 203 | if (!msg) { |
f24e9980 | 204 | ceph_osdc_put_request(req); |
a79832f2 | 205 | return NULL; |
f24e9980 | 206 | } |
68b4476b | 207 | |
f24e9980 | 208 | memset(msg->front.iov_base, 0, msg->front.iov_len); |
3499e8a5 YS |
209 | |
210 | req->r_request = msg; | |
3499e8a5 YS |
211 | |
212 | return req; | |
213 | } | |
3d14c5d2 | 214 | EXPORT_SYMBOL(ceph_osdc_alloc_request); |
3499e8a5 | 215 | |
68b4476b YS |
216 | static void osd_req_encode_op(struct ceph_osd_request *req, |
217 | struct ceph_osd_op *dst, | |
218 | struct ceph_osd_req_op *src) | |
219 | { | |
220 | dst->op = cpu_to_le16(src->op); | |
221 | ||
065a68f9 | 222 | switch (src->op) { |
fbfab539 AE |
223 | case CEPH_OSD_OP_STAT: |
224 | break; | |
68b4476b YS |
225 | case CEPH_OSD_OP_READ: |
226 | case CEPH_OSD_OP_WRITE: | |
227 | dst->extent.offset = | |
228 | cpu_to_le64(src->extent.offset); | |
229 | dst->extent.length = | |
230 | cpu_to_le64(src->extent.length); | |
231 | dst->extent.truncate_size = | |
232 | cpu_to_le64(src->extent.truncate_size); | |
233 | dst->extent.truncate_seq = | |
234 | cpu_to_le32(src->extent.truncate_seq); | |
235 | break; | |
ae1533b6 | 236 | case CEPH_OSD_OP_CALL: |
ae1533b6 YS |
237 | dst->cls.class_len = src->cls.class_len; |
238 | dst->cls.method_len = src->cls.method_len; | |
239 | dst->cls.indata_len = cpu_to_le32(src->cls.indata_len); | |
240 | ||
c885837f | 241 | ceph_pagelist_append(&req->r_trail, src->cls.class_name, |
ae1533b6 | 242 | src->cls.class_len); |
c885837f | 243 | ceph_pagelist_append(&req->r_trail, src->cls.method_name, |
ae1533b6 | 244 | src->cls.method_len); |
c885837f | 245 | ceph_pagelist_append(&req->r_trail, src->cls.indata, |
ae1533b6 YS |
246 | src->cls.indata_len); |
247 | break; | |
68b4476b YS |
248 | case CEPH_OSD_OP_STARTSYNC: |
249 | break; | |
a40c4f10 YS |
250 | case CEPH_OSD_OP_NOTIFY_ACK: |
251 | case CEPH_OSD_OP_WATCH: | |
252 | dst->watch.cookie = cpu_to_le64(src->watch.cookie); | |
253 | dst->watch.ver = cpu_to_le64(src->watch.ver); | |
254 | dst->watch.flag = src->watch.flag; | |
255 | break; | |
68b4476b YS |
256 | default: |
257 | pr_err("unrecognized osd opcode %d\n", dst->op); | |
258 | WARN_ON(1); | |
259 | break; | |
4c46459c AE |
260 | case CEPH_OSD_OP_MAPEXT: |
261 | case CEPH_OSD_OP_MASKTRUNC: | |
262 | case CEPH_OSD_OP_SPARSE_READ: | |
a9f36c3e | 263 | case CEPH_OSD_OP_NOTIFY: |
4c46459c AE |
264 | case CEPH_OSD_OP_ASSERT_VER: |
265 | case CEPH_OSD_OP_WRITEFULL: | |
266 | case CEPH_OSD_OP_TRUNCATE: | |
267 | case CEPH_OSD_OP_ZERO: | |
268 | case CEPH_OSD_OP_DELETE: | |
269 | case CEPH_OSD_OP_APPEND: | |
270 | case CEPH_OSD_OP_SETTRUNC: | |
271 | case CEPH_OSD_OP_TRIMTRUNC: | |
272 | case CEPH_OSD_OP_TMAPUP: | |
273 | case CEPH_OSD_OP_TMAPPUT: | |
274 | case CEPH_OSD_OP_TMAPGET: | |
275 | case CEPH_OSD_OP_CREATE: | |
a9f36c3e | 276 | case CEPH_OSD_OP_ROLLBACK: |
4c46459c AE |
277 | case CEPH_OSD_OP_OMAPGETKEYS: |
278 | case CEPH_OSD_OP_OMAPGETVALS: | |
279 | case CEPH_OSD_OP_OMAPGETHEADER: | |
280 | case CEPH_OSD_OP_OMAPGETVALSBYKEYS: | |
281 | case CEPH_OSD_OP_MODE_RD: | |
282 | case CEPH_OSD_OP_OMAPSETVALS: | |
283 | case CEPH_OSD_OP_OMAPSETHEADER: | |
284 | case CEPH_OSD_OP_OMAPCLEAR: | |
285 | case CEPH_OSD_OP_OMAPRMKEYS: | |
286 | case CEPH_OSD_OP_OMAP_CMP: | |
287 | case CEPH_OSD_OP_CLONERANGE: | |
288 | case CEPH_OSD_OP_ASSERT_SRC_VERSION: | |
289 | case CEPH_OSD_OP_SRC_CMPXATTR: | |
a9f36c3e | 290 | case CEPH_OSD_OP_GETXATTR: |
4c46459c | 291 | case CEPH_OSD_OP_GETXATTRS: |
a9f36c3e AE |
292 | case CEPH_OSD_OP_CMPXATTR: |
293 | case CEPH_OSD_OP_SETXATTR: | |
4c46459c AE |
294 | case CEPH_OSD_OP_SETXATTRS: |
295 | case CEPH_OSD_OP_RESETXATTRS: | |
296 | case CEPH_OSD_OP_RMXATTR: | |
297 | case CEPH_OSD_OP_PULL: | |
298 | case CEPH_OSD_OP_PUSH: | |
299 | case CEPH_OSD_OP_BALANCEREADS: | |
300 | case CEPH_OSD_OP_UNBALANCEREADS: | |
301 | case CEPH_OSD_OP_SCRUB: | |
302 | case CEPH_OSD_OP_SCRUB_RESERVE: | |
303 | case CEPH_OSD_OP_SCRUB_UNRESERVE: | |
304 | case CEPH_OSD_OP_SCRUB_STOP: | |
305 | case CEPH_OSD_OP_SCRUB_MAP: | |
306 | case CEPH_OSD_OP_WRLOCK: | |
307 | case CEPH_OSD_OP_WRUNLOCK: | |
308 | case CEPH_OSD_OP_RDLOCK: | |
309 | case CEPH_OSD_OP_RDUNLOCK: | |
310 | case CEPH_OSD_OP_UPLOCK: | |
311 | case CEPH_OSD_OP_DNLOCK: | |
312 | case CEPH_OSD_OP_PGLS: | |
313 | case CEPH_OSD_OP_PGLS_FILTER: | |
314 | pr_err("unsupported osd opcode %s\n", | |
315 | ceph_osd_op_name(dst->op)); | |
316 | WARN_ON(1); | |
317 | break; | |
68b4476b YS |
318 | } |
319 | dst->payload_len = cpu_to_le32(src->payload_len); | |
320 | } | |
321 | ||
3499e8a5 YS |
322 | /* |
323 | * build new request AND message | |
324 | * | |
325 | */ | |
326 | void ceph_osdc_build_request(struct ceph_osd_request *req, | |
ae7ca4a3 | 327 | u64 off, u64 len, unsigned int num_op, |
68b4476b | 328 | struct ceph_osd_req_op *src_ops, |
4d6b250b | 329 | struct ceph_snap_context *snapc, u64 snap_id, |
af77f26c | 330 | struct timespec *mtime) |
3499e8a5 YS |
331 | { |
332 | struct ceph_msg *msg = req->r_request; | |
333 | struct ceph_osd_request_head *head; | |
68b4476b | 334 | struct ceph_osd_req_op *src_op; |
3499e8a5 YS |
335 | struct ceph_osd_op *op; |
336 | void *p; | |
3499e8a5 | 337 | size_t msg_size = sizeof(*head) + num_op*sizeof(*op); |
3499e8a5 | 338 | int flags = req->r_flags; |
f44246e3 | 339 | u64 data_len; |
68b4476b | 340 | int i; |
3499e8a5 | 341 | |
d178a9e7 AE |
342 | WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0); |
343 | ||
f24e9980 | 344 | head = msg->front.iov_base; |
4d6b250b | 345 | head->snapid = cpu_to_le64(snap_id); |
f24e9980 SW |
346 | op = (void *)(head + 1); |
347 | p = (void *)(op + num_op); | |
348 | ||
f24e9980 SW |
349 | req->r_snapc = ceph_get_snap_context(snapc); |
350 | ||
351 | head->client_inc = cpu_to_le32(1); /* always, for now. */ | |
352 | head->flags = cpu_to_le32(flags); | |
353 | if (flags & CEPH_OSD_FLAG_WRITE) | |
354 | ceph_encode_timespec(&head->mtime, mtime); | |
ae7ca4a3 | 355 | BUG_ON(num_op > (unsigned int) ((u16) -1)); |
f24e9980 | 356 | head->num_ops = cpu_to_le16(num_op); |
f24e9980 | 357 | |
f24e9980 | 358 | /* fill in oid */ |
af77f26c AE |
359 | head->object_len = cpu_to_le32(req->r_oid_len); |
360 | memcpy(p, req->r_oid, req->r_oid_len); | |
361 | p += req->r_oid_len; | |
f24e9980 | 362 | |
68b4476b | 363 | src_op = src_ops; |
ae7ca4a3 AE |
364 | while (num_op--) |
365 | osd_req_encode_op(req, op++, src_op++); | |
68b4476b | 366 | |
f24e9980 SW |
367 | if (snapc) { |
368 | head->snap_seq = cpu_to_le64(snapc->seq); | |
369 | head->num_snaps = cpu_to_le32(snapc->num_snaps); | |
370 | for (i = 0; i < snapc->num_snaps; i++) { | |
371 | put_unaligned_le64(snapc->snaps[i], p); | |
372 | p += sizeof(u64); | |
373 | } | |
374 | } | |
375 | ||
f44246e3 | 376 | data_len = req->r_trail.length; |
68b4476b YS |
377 | if (flags & CEPH_OSD_FLAG_WRITE) { |
378 | req->r_request->hdr.data_off = cpu_to_le16(off); | |
f44246e3 | 379 | data_len += len; |
68b4476b | 380 | } |
f44246e3 | 381 | req->r_request->hdr.data_len = cpu_to_le32(data_len); |
c5c6b19d SW |
382 | req->r_request->page_alignment = req->r_page_alignment; |
383 | ||
f24e9980 | 384 | BUG_ON(p > msg->front.iov_base + msg->front.iov_len); |
6f863e71 SW |
385 | msg_size = p - msg->front.iov_base; |
386 | msg->front.iov_len = msg_size; | |
387 | msg->hdr.front_len = cpu_to_le32(msg_size); | |
3499e8a5 YS |
388 | return; |
389 | } | |
3d14c5d2 | 390 | EXPORT_SYMBOL(ceph_osdc_build_request); |
3499e8a5 YS |
391 | |
392 | /* | |
393 | * build new request AND message, calculate layout, and adjust file | |
394 | * extent as needed. | |
395 | * | |
396 | * if the file was recently truncated, we include information about its | |
397 | * old and new size so that the object can be updated appropriately. (we | |
398 | * avoid synchronously deleting truncated objects because it's slow.) | |
399 | * | |
400 | * if @do_sync, include a 'startsync' command so that the osd will flush | |
401 | * data quickly. | |
402 | */ | |
403 | struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, | |
404 | struct ceph_file_layout *layout, | |
405 | struct ceph_vino vino, | |
406 | u64 off, u64 *plen, | |
407 | int opcode, int flags, | |
408 | struct ceph_snap_context *snapc, | |
409 | int do_sync, | |
410 | u32 truncate_seq, | |
411 | u64 truncate_size, | |
412 | struct timespec *mtime, | |
a3bea47e | 413 | bool use_mempool, |
b7495fc2 | 414 | int page_align) |
3499e8a5 | 415 | { |
ae7ca4a3 | 416 | struct ceph_osd_req_op ops[2]; |
68b4476b | 417 | struct ceph_osd_request *req; |
ae7ca4a3 | 418 | unsigned int num_op = 1; |
6816282d | 419 | int r; |
68b4476b | 420 | |
ae7ca4a3 AE |
421 | memset(&ops, 0, sizeof ops); |
422 | ||
68b4476b YS |
423 | ops[0].op = opcode; |
424 | ops[0].extent.truncate_seq = truncate_seq; | |
425 | ops[0].extent.truncate_size = truncate_size; | |
68b4476b YS |
426 | |
427 | if (do_sync) { | |
428 | ops[1].op = CEPH_OSD_OP_STARTSYNC; | |
ae7ca4a3 AE |
429 | num_op++; |
430 | } | |
68b4476b | 431 | |
ae7ca4a3 AE |
432 | req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool, |
433 | GFP_NOFS); | |
4ad12621 | 434 | if (!req) |
6816282d | 435 | return ERR_PTR(-ENOMEM); |
d178a9e7 | 436 | req->r_flags = flags; |
3499e8a5 YS |
437 | |
438 | /* calculate max write size */ | |
e75b45cf | 439 | r = calc_layout(vino, layout, off, plen, req, ops); |
6816282d SW |
440 | if (r < 0) |
441 | return ERR_PTR(r); | |
3499e8a5 YS |
442 | req->r_file_layout = *layout; /* keep a copy */ |
443 | ||
9bb0ce2b SW |
444 | /* in case it differs from natural (file) alignment that |
445 | calc_layout filled in for us */ | |
446 | req->r_num_pages = calc_pages_for(page_align, *plen); | |
b7495fc2 SW |
447 | req->r_page_alignment = page_align; |
448 | ||
ae7ca4a3 AE |
449 | ceph_osdc_build_request(req, off, *plen, num_op, ops, |
450 | snapc, vino.snap, mtime); | |
3499e8a5 | 451 | |
f24e9980 SW |
452 | return req; |
453 | } | |
3d14c5d2 | 454 | EXPORT_SYMBOL(ceph_osdc_new_request); |
f24e9980 SW |
455 | |
456 | /* | |
457 | * We keep osd requests in an rbtree, sorted by ->r_tid. | |
458 | */ | |
459 | static void __insert_request(struct ceph_osd_client *osdc, | |
460 | struct ceph_osd_request *new) | |
461 | { | |
462 | struct rb_node **p = &osdc->requests.rb_node; | |
463 | struct rb_node *parent = NULL; | |
464 | struct ceph_osd_request *req = NULL; | |
465 | ||
466 | while (*p) { | |
467 | parent = *p; | |
468 | req = rb_entry(parent, struct ceph_osd_request, r_node); | |
469 | if (new->r_tid < req->r_tid) | |
470 | p = &(*p)->rb_left; | |
471 | else if (new->r_tid > req->r_tid) | |
472 | p = &(*p)->rb_right; | |
473 | else | |
474 | BUG(); | |
475 | } | |
476 | ||
477 | rb_link_node(&new->r_node, parent, p); | |
478 | rb_insert_color(&new->r_node, &osdc->requests); | |
479 | } | |
480 | ||
481 | static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc, | |
482 | u64 tid) | |
483 | { | |
484 | struct ceph_osd_request *req; | |
485 | struct rb_node *n = osdc->requests.rb_node; | |
486 | ||
487 | while (n) { | |
488 | req = rb_entry(n, struct ceph_osd_request, r_node); | |
489 | if (tid < req->r_tid) | |
490 | n = n->rb_left; | |
491 | else if (tid > req->r_tid) | |
492 | n = n->rb_right; | |
493 | else | |
494 | return req; | |
495 | } | |
496 | return NULL; | |
497 | } | |
498 | ||
499 | static struct ceph_osd_request * | |
500 | __lookup_request_ge(struct ceph_osd_client *osdc, | |
501 | u64 tid) | |
502 | { | |
503 | struct ceph_osd_request *req; | |
504 | struct rb_node *n = osdc->requests.rb_node; | |
505 | ||
506 | while (n) { | |
507 | req = rb_entry(n, struct ceph_osd_request, r_node); | |
508 | if (tid < req->r_tid) { | |
509 | if (!n->rb_left) | |
510 | return req; | |
511 | n = n->rb_left; | |
512 | } else if (tid > req->r_tid) { | |
513 | n = n->rb_right; | |
514 | } else { | |
515 | return req; | |
516 | } | |
517 | } | |
518 | return NULL; | |
519 | } | |
520 | ||
6f6c7006 SW |
521 | /* |
522 | * Resubmit requests pending on the given osd. | |
523 | */ | |
524 | static void __kick_osd_requests(struct ceph_osd_client *osdc, | |
525 | struct ceph_osd *osd) | |
526 | { | |
a40c4f10 | 527 | struct ceph_osd_request *req, *nreq; |
6f6c7006 SW |
528 | int err; |
529 | ||
530 | dout("__kick_osd_requests osd%d\n", osd->o_osd); | |
531 | err = __reset_osd(osdc, osd); | |
685a7555 | 532 | if (err) |
6f6c7006 SW |
533 | return; |
534 | ||
535 | list_for_each_entry(req, &osd->o_requests, r_osd_item) { | |
536 | list_move(&req->r_req_lru_item, &osdc->req_unsent); | |
537 | dout("requeued %p tid %llu osd%d\n", req, req->r_tid, | |
538 | osd->o_osd); | |
a40c4f10 YS |
539 | if (!req->r_linger) |
540 | req->r_flags |= CEPH_OSD_FLAG_RETRY; | |
541 | } | |
542 | ||
543 | list_for_each_entry_safe(req, nreq, &osd->o_linger_requests, | |
544 | r_linger_osd) { | |
77f38e0e SW |
545 | /* |
546 | * reregister request prior to unregistering linger so | |
547 | * that r_osd is preserved. | |
548 | */ | |
549 | BUG_ON(!list_empty(&req->r_req_lru_item)); | |
a40c4f10 | 550 | __register_request(osdc, req); |
77f38e0e SW |
551 | list_add(&req->r_req_lru_item, &osdc->req_unsent); |
552 | list_add(&req->r_osd_item, &req->r_osd->o_requests); | |
553 | __unregister_linger_request(osdc, req); | |
a40c4f10 YS |
554 | dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid, |
555 | osd->o_osd); | |
6f6c7006 SW |
556 | } |
557 | } | |
558 | ||
f24e9980 | 559 | /* |
81b024e7 | 560 | * If the osd connection drops, we need to resubmit all requests. |
f24e9980 SW |
561 | */ |
562 | static void osd_reset(struct ceph_connection *con) | |
563 | { | |
564 | struct ceph_osd *osd = con->private; | |
565 | struct ceph_osd_client *osdc; | |
566 | ||
567 | if (!osd) | |
568 | return; | |
569 | dout("osd_reset osd%d\n", osd->o_osd); | |
570 | osdc = osd->o_osdc; | |
f24e9980 | 571 | down_read(&osdc->map_sem); |
83aff95e SW |
572 | mutex_lock(&osdc->request_mutex); |
573 | __kick_osd_requests(osdc, osd); | |
f9d25199 | 574 | __send_queued(osdc); |
83aff95e | 575 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
576 | up_read(&osdc->map_sem); |
577 | } | |
578 | ||
579 | /* | |
580 | * Track open sessions with osds. | |
581 | */ | |
e10006f8 | 582 | static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum) |
f24e9980 SW |
583 | { |
584 | struct ceph_osd *osd; | |
585 | ||
586 | osd = kzalloc(sizeof(*osd), GFP_NOFS); | |
587 | if (!osd) | |
588 | return NULL; | |
589 | ||
590 | atomic_set(&osd->o_ref, 1); | |
591 | osd->o_osdc = osdc; | |
e10006f8 | 592 | osd->o_osd = onum; |
f407731d | 593 | RB_CLEAR_NODE(&osd->o_node); |
f24e9980 | 594 | INIT_LIST_HEAD(&osd->o_requests); |
a40c4f10 | 595 | INIT_LIST_HEAD(&osd->o_linger_requests); |
f5a2041b | 596 | INIT_LIST_HEAD(&osd->o_osd_lru); |
f24e9980 SW |
597 | osd->o_incarnation = 1; |
598 | ||
b7a9e5dd | 599 | ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr); |
4e7a5dcd | 600 | |
422d2cb8 | 601 | INIT_LIST_HEAD(&osd->o_keepalive_item); |
f24e9980 SW |
602 | return osd; |
603 | } | |
604 | ||
605 | static struct ceph_osd *get_osd(struct ceph_osd *osd) | |
606 | { | |
607 | if (atomic_inc_not_zero(&osd->o_ref)) { | |
608 | dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1, | |
609 | atomic_read(&osd->o_ref)); | |
610 | return osd; | |
611 | } else { | |
612 | dout("get_osd %p FAIL\n", osd); | |
613 | return NULL; | |
614 | } | |
615 | } | |
616 | ||
617 | static void put_osd(struct ceph_osd *osd) | |
618 | { | |
619 | dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref), | |
620 | atomic_read(&osd->o_ref) - 1); | |
a255651d | 621 | if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) { |
79494d1b SW |
622 | struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth; |
623 | ||
a255651d | 624 | if (ac->ops && ac->ops->destroy_authorizer) |
6c4a1915 | 625 | ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer); |
f24e9980 | 626 | kfree(osd); |
79494d1b | 627 | } |
f24e9980 SW |
628 | } |
629 | ||
630 | /* | |
631 | * remove an osd from our map | |
632 | */ | |
f5a2041b | 633 | static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) |
f24e9980 | 634 | { |
f5a2041b | 635 | dout("__remove_osd %p\n", osd); |
f24e9980 SW |
636 | BUG_ON(!list_empty(&osd->o_requests)); |
637 | rb_erase(&osd->o_node, &osdc->osds); | |
f5a2041b | 638 | list_del_init(&osd->o_osd_lru); |
f24e9980 SW |
639 | ceph_con_close(&osd->o_con); |
640 | put_osd(osd); | |
641 | } | |
642 | ||
aca420bc SW |
643 | static void remove_all_osds(struct ceph_osd_client *osdc) |
644 | { | |
048a9d2d | 645 | dout("%s %p\n", __func__, osdc); |
aca420bc SW |
646 | mutex_lock(&osdc->request_mutex); |
647 | while (!RB_EMPTY_ROOT(&osdc->osds)) { | |
648 | struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds), | |
649 | struct ceph_osd, o_node); | |
650 | __remove_osd(osdc, osd); | |
651 | } | |
652 | mutex_unlock(&osdc->request_mutex); | |
653 | } | |
654 | ||
f5a2041b YS |
655 | static void __move_osd_to_lru(struct ceph_osd_client *osdc, |
656 | struct ceph_osd *osd) | |
657 | { | |
658 | dout("__move_osd_to_lru %p\n", osd); | |
659 | BUG_ON(!list_empty(&osd->o_osd_lru)); | |
660 | list_add_tail(&osd->o_osd_lru, &osdc->osd_lru); | |
3d14c5d2 | 661 | osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ; |
f5a2041b YS |
662 | } |
663 | ||
664 | static void __remove_osd_from_lru(struct ceph_osd *osd) | |
665 | { | |
666 | dout("__remove_osd_from_lru %p\n", osd); | |
667 | if (!list_empty(&osd->o_osd_lru)) | |
668 | list_del_init(&osd->o_osd_lru); | |
669 | } | |
670 | ||
aca420bc | 671 | static void remove_old_osds(struct ceph_osd_client *osdc) |
f5a2041b YS |
672 | { |
673 | struct ceph_osd *osd, *nosd; | |
674 | ||
675 | dout("__remove_old_osds %p\n", osdc); | |
676 | mutex_lock(&osdc->request_mutex); | |
677 | list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) { | |
aca420bc | 678 | if (time_before(jiffies, osd->lru_ttl)) |
f5a2041b YS |
679 | break; |
680 | __remove_osd(osdc, osd); | |
681 | } | |
682 | mutex_unlock(&osdc->request_mutex); | |
683 | } | |
684 | ||
f24e9980 SW |
685 | /* |
686 | * reset osd connect | |
687 | */ | |
f5a2041b | 688 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) |
f24e9980 | 689 | { |
c3acb181 | 690 | struct ceph_entity_addr *peer_addr; |
f24e9980 | 691 | |
f5a2041b | 692 | dout("__reset_osd %p osd%d\n", osd, osd->o_osd); |
a40c4f10 YS |
693 | if (list_empty(&osd->o_requests) && |
694 | list_empty(&osd->o_linger_requests)) { | |
f5a2041b | 695 | __remove_osd(osdc, osd); |
c3acb181 AE |
696 | |
697 | return -ENODEV; | |
698 | } | |
699 | ||
700 | peer_addr = &osdc->osdmap->osd_addr[osd->o_osd]; | |
701 | if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) && | |
702 | !ceph_con_opened(&osd->o_con)) { | |
703 | struct ceph_osd_request *req; | |
704 | ||
87b315a5 SW |
705 | dout(" osd addr hasn't changed and connection never opened," |
706 | " letting msgr retry"); | |
707 | /* touch each r_stamp for handle_timeout()'s benfit */ | |
708 | list_for_each_entry(req, &osd->o_requests, r_osd_item) | |
709 | req->r_stamp = jiffies; | |
c3acb181 AE |
710 | |
711 | return -EAGAIN; | |
f24e9980 | 712 | } |
c3acb181 AE |
713 | |
714 | ceph_con_close(&osd->o_con); | |
715 | ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr); | |
716 | osd->o_incarnation++; | |
717 | ||
718 | return 0; | |
f24e9980 SW |
719 | } |
720 | ||
721 | static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new) | |
722 | { | |
723 | struct rb_node **p = &osdc->osds.rb_node; | |
724 | struct rb_node *parent = NULL; | |
725 | struct ceph_osd *osd = NULL; | |
726 | ||
aca420bc | 727 | dout("__insert_osd %p osd%d\n", new, new->o_osd); |
f24e9980 SW |
728 | while (*p) { |
729 | parent = *p; | |
730 | osd = rb_entry(parent, struct ceph_osd, o_node); | |
731 | if (new->o_osd < osd->o_osd) | |
732 | p = &(*p)->rb_left; | |
733 | else if (new->o_osd > osd->o_osd) | |
734 | p = &(*p)->rb_right; | |
735 | else | |
736 | BUG(); | |
737 | } | |
738 | ||
739 | rb_link_node(&new->o_node, parent, p); | |
740 | rb_insert_color(&new->o_node, &osdc->osds); | |
741 | } | |
742 | ||
743 | static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o) | |
744 | { | |
745 | struct ceph_osd *osd; | |
746 | struct rb_node *n = osdc->osds.rb_node; | |
747 | ||
748 | while (n) { | |
749 | osd = rb_entry(n, struct ceph_osd, o_node); | |
750 | if (o < osd->o_osd) | |
751 | n = n->rb_left; | |
752 | else if (o > osd->o_osd) | |
753 | n = n->rb_right; | |
754 | else | |
755 | return osd; | |
756 | } | |
757 | return NULL; | |
758 | } | |
759 | ||
422d2cb8 YS |
760 | static void __schedule_osd_timeout(struct ceph_osd_client *osdc) |
761 | { | |
762 | schedule_delayed_work(&osdc->timeout_work, | |
3d14c5d2 | 763 | osdc->client->options->osd_keepalive_timeout * HZ); |
422d2cb8 YS |
764 | } |
765 | ||
766 | static void __cancel_osd_timeout(struct ceph_osd_client *osdc) | |
767 | { | |
768 | cancel_delayed_work(&osdc->timeout_work); | |
769 | } | |
f24e9980 SW |
770 | |
771 | /* | |
772 | * Register request, assign tid. If this is the first request, set up | |
773 | * the timeout event. | |
774 | */ | |
a40c4f10 YS |
775 | static void __register_request(struct ceph_osd_client *osdc, |
776 | struct ceph_osd_request *req) | |
f24e9980 | 777 | { |
f24e9980 | 778 | req->r_tid = ++osdc->last_tid; |
6df058c0 | 779 | req->r_request->hdr.tid = cpu_to_le64(req->r_tid); |
77f38e0e | 780 | dout("__register_request %p tid %lld\n", req, req->r_tid); |
f24e9980 SW |
781 | __insert_request(osdc, req); |
782 | ceph_osdc_get_request(req); | |
783 | osdc->num_requests++; | |
f24e9980 | 784 | if (osdc->num_requests == 1) { |
422d2cb8 YS |
785 | dout(" first request, scheduling timeout\n"); |
786 | __schedule_osd_timeout(osdc); | |
f24e9980 | 787 | } |
a40c4f10 YS |
788 | } |
789 | ||
790 | static void register_request(struct ceph_osd_client *osdc, | |
791 | struct ceph_osd_request *req) | |
792 | { | |
793 | mutex_lock(&osdc->request_mutex); | |
794 | __register_request(osdc, req); | |
f24e9980 SW |
795 | mutex_unlock(&osdc->request_mutex); |
796 | } | |
797 | ||
798 | /* | |
799 | * called under osdc->request_mutex | |
800 | */ | |
801 | static void __unregister_request(struct ceph_osd_client *osdc, | |
802 | struct ceph_osd_request *req) | |
803 | { | |
35f9f8a0 SW |
804 | if (RB_EMPTY_NODE(&req->r_node)) { |
805 | dout("__unregister_request %p tid %lld not registered\n", | |
806 | req, req->r_tid); | |
807 | return; | |
808 | } | |
809 | ||
f24e9980 SW |
810 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); |
811 | rb_erase(&req->r_node, &osdc->requests); | |
812 | osdc->num_requests--; | |
813 | ||
0ba6478d SW |
814 | if (req->r_osd) { |
815 | /* make sure the original request isn't in flight. */ | |
6740a845 | 816 | ceph_msg_revoke(req->r_request); |
0ba6478d SW |
817 | |
818 | list_del_init(&req->r_osd_item); | |
a40c4f10 YS |
819 | if (list_empty(&req->r_osd->o_requests) && |
820 | list_empty(&req->r_osd->o_linger_requests)) { | |
821 | dout("moving osd to %p lru\n", req->r_osd); | |
f5a2041b | 822 | __move_osd_to_lru(osdc, req->r_osd); |
a40c4f10 | 823 | } |
fbdb9190 | 824 | if (list_empty(&req->r_linger_item)) |
a40c4f10 | 825 | req->r_osd = NULL; |
0ba6478d | 826 | } |
f24e9980 | 827 | |
7d5f2481 | 828 | list_del_init(&req->r_req_lru_item); |
f24e9980 SW |
829 | ceph_osdc_put_request(req); |
830 | ||
422d2cb8 YS |
831 | if (osdc->num_requests == 0) { |
832 | dout(" no requests, canceling timeout\n"); | |
833 | __cancel_osd_timeout(osdc); | |
f24e9980 SW |
834 | } |
835 | } | |
836 | ||
837 | /* | |
838 | * Cancel a previously queued request message | |
839 | */ | |
840 | static void __cancel_request(struct ceph_osd_request *req) | |
841 | { | |
6bc18876 | 842 | if (req->r_sent && req->r_osd) { |
6740a845 | 843 | ceph_msg_revoke(req->r_request); |
f24e9980 SW |
844 | req->r_sent = 0; |
845 | } | |
846 | } | |
847 | ||
a40c4f10 YS |
848 | static void __register_linger_request(struct ceph_osd_client *osdc, |
849 | struct ceph_osd_request *req) | |
850 | { | |
851 | dout("__register_linger_request %p\n", req); | |
852 | list_add_tail(&req->r_linger_item, &osdc->req_linger); | |
6194ea89 SW |
853 | if (req->r_osd) |
854 | list_add_tail(&req->r_linger_osd, | |
855 | &req->r_osd->o_linger_requests); | |
a40c4f10 YS |
856 | } |
857 | ||
858 | static void __unregister_linger_request(struct ceph_osd_client *osdc, | |
859 | struct ceph_osd_request *req) | |
860 | { | |
861 | dout("__unregister_linger_request %p\n", req); | |
61c74035 | 862 | list_del_init(&req->r_linger_item); |
a40c4f10 | 863 | if (req->r_osd) { |
a40c4f10 YS |
864 | list_del_init(&req->r_linger_osd); |
865 | ||
866 | if (list_empty(&req->r_osd->o_requests) && | |
867 | list_empty(&req->r_osd->o_linger_requests)) { | |
868 | dout("moving osd to %p lru\n", req->r_osd); | |
869 | __move_osd_to_lru(osdc, req->r_osd); | |
870 | } | |
fbdb9190 SW |
871 | if (list_empty(&req->r_osd_item)) |
872 | req->r_osd = NULL; | |
a40c4f10 YS |
873 | } |
874 | } | |
875 | ||
876 | void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc, | |
877 | struct ceph_osd_request *req) | |
878 | { | |
879 | mutex_lock(&osdc->request_mutex); | |
880 | if (req->r_linger) { | |
881 | __unregister_linger_request(osdc, req); | |
882 | ceph_osdc_put_request(req); | |
883 | } | |
884 | mutex_unlock(&osdc->request_mutex); | |
885 | } | |
886 | EXPORT_SYMBOL(ceph_osdc_unregister_linger_request); | |
887 | ||
888 | void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc, | |
889 | struct ceph_osd_request *req) | |
890 | { | |
891 | if (!req->r_linger) { | |
892 | dout("set_request_linger %p\n", req); | |
893 | req->r_linger = 1; | |
894 | /* | |
895 | * caller is now responsible for calling | |
896 | * unregister_linger_request | |
897 | */ | |
898 | ceph_osdc_get_request(req); | |
899 | } | |
900 | } | |
901 | EXPORT_SYMBOL(ceph_osdc_set_request_linger); | |
902 | ||
f24e9980 SW |
903 | /* |
904 | * Pick an osd (the first 'up' osd in the pg), allocate the osd struct | |
905 | * (as needed), and set the request r_osd appropriately. If there is | |
25985edc | 906 | * no up osd, set r_osd to NULL. Move the request to the appropriate list |
6f6c7006 | 907 | * (unsent, homeless) or leave on in-flight lru. |
f24e9980 SW |
908 | * |
909 | * Return 0 if unchanged, 1 if changed, or negative on error. | |
910 | * | |
911 | * Caller should hold map_sem for read and request_mutex. | |
912 | */ | |
6f6c7006 | 913 | static int __map_request(struct ceph_osd_client *osdc, |
38d6453c | 914 | struct ceph_osd_request *req, int force_resend) |
f24e9980 SW |
915 | { |
916 | struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base; | |
12979354 | 917 | struct ceph_pg_v1 pgid; |
d85b7056 SW |
918 | int acting[CEPH_PG_MAX_SIZE]; |
919 | int o = -1, num = 0; | |
f24e9980 | 920 | int err; |
f24e9980 | 921 | |
6f6c7006 | 922 | dout("map_request %p tid %lld\n", req, req->r_tid); |
f24e9980 SW |
923 | err = ceph_calc_object_layout(&reqhead->layout, req->r_oid, |
924 | &req->r_file_layout, osdc->osdmap); | |
6f6c7006 SW |
925 | if (err) { |
926 | list_move(&req->r_req_lru_item, &osdc->req_notarget); | |
f24e9980 | 927 | return err; |
6f6c7006 | 928 | } |
51042122 | 929 | pgid = reqhead->layout.ol_pgid; |
7740a42f SW |
930 | req->r_pgid = pgid; |
931 | ||
d85b7056 SW |
932 | err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting); |
933 | if (err > 0) { | |
934 | o = acting[0]; | |
935 | num = err; | |
936 | } | |
f24e9980 | 937 | |
38d6453c SW |
938 | if ((!force_resend && |
939 | req->r_osd && req->r_osd->o_osd == o && | |
d85b7056 SW |
940 | req->r_sent >= req->r_osd->o_incarnation && |
941 | req->r_num_pg_osds == num && | |
942 | memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) || | |
f24e9980 SW |
943 | (req->r_osd == NULL && o == -1)) |
944 | return 0; /* no change */ | |
945 | ||
6f6c7006 | 946 | dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n", |
51042122 | 947 | req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o, |
f24e9980 SW |
948 | req->r_osd ? req->r_osd->o_osd : -1); |
949 | ||
d85b7056 SW |
950 | /* record full pg acting set */ |
951 | memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num); | |
952 | req->r_num_pg_osds = num; | |
953 | ||
f24e9980 SW |
954 | if (req->r_osd) { |
955 | __cancel_request(req); | |
956 | list_del_init(&req->r_osd_item); | |
f24e9980 SW |
957 | req->r_osd = NULL; |
958 | } | |
959 | ||
960 | req->r_osd = __lookup_osd(osdc, o); | |
961 | if (!req->r_osd && o >= 0) { | |
c99eb1c7 | 962 | err = -ENOMEM; |
e10006f8 | 963 | req->r_osd = create_osd(osdc, o); |
6f6c7006 SW |
964 | if (!req->r_osd) { |
965 | list_move(&req->r_req_lru_item, &osdc->req_notarget); | |
c99eb1c7 | 966 | goto out; |
6f6c7006 | 967 | } |
f24e9980 | 968 | |
6f6c7006 | 969 | dout("map_request osd %p is osd%d\n", req->r_osd, o); |
f24e9980 SW |
970 | __insert_osd(osdc, req->r_osd); |
971 | ||
b7a9e5dd SW |
972 | ceph_con_open(&req->r_osd->o_con, |
973 | CEPH_ENTITY_TYPE_OSD, o, | |
974 | &osdc->osdmap->osd_addr[o]); | |
f24e9980 SW |
975 | } |
976 | ||
f5a2041b YS |
977 | if (req->r_osd) { |
978 | __remove_osd_from_lru(req->r_osd); | |
f24e9980 | 979 | list_add(&req->r_osd_item, &req->r_osd->o_requests); |
6f6c7006 SW |
980 | list_move(&req->r_req_lru_item, &osdc->req_unsent); |
981 | } else { | |
982 | list_move(&req->r_req_lru_item, &osdc->req_notarget); | |
f5a2041b | 983 | } |
d85b7056 | 984 | err = 1; /* osd or pg changed */ |
f24e9980 SW |
985 | |
986 | out: | |
f24e9980 SW |
987 | return err; |
988 | } | |
989 | ||
990 | /* | |
991 | * caller should hold map_sem (for read) and request_mutex | |
992 | */ | |
56e925b6 SW |
993 | static void __send_request(struct ceph_osd_client *osdc, |
994 | struct ceph_osd_request *req) | |
f24e9980 SW |
995 | { |
996 | struct ceph_osd_request_head *reqhead; | |
f24e9980 SW |
997 | |
998 | dout("send_request %p tid %llu to osd%d flags %d\n", | |
999 | req, req->r_tid, req->r_osd->o_osd, req->r_flags); | |
1000 | ||
1001 | reqhead = req->r_request->front.iov_base; | |
1002 | reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch); | |
1003 | reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */ | |
1004 | reqhead->reassert_version = req->r_reassert_version; | |
1005 | ||
3dd72fc0 | 1006 | req->r_stamp = jiffies; |
07a27e22 | 1007 | list_move_tail(&req->r_req_lru_item, &osdc->req_lru); |
f24e9980 SW |
1008 | |
1009 | ceph_msg_get(req->r_request); /* send consumes a ref */ | |
1010 | ceph_con_send(&req->r_osd->o_con, req->r_request); | |
1011 | req->r_sent = req->r_osd->o_incarnation; | |
f24e9980 SW |
1012 | } |
1013 | ||
6f6c7006 SW |
1014 | /* |
1015 | * Send any requests in the queue (req_unsent). | |
1016 | */ | |
f9d25199 | 1017 | static void __send_queued(struct ceph_osd_client *osdc) |
6f6c7006 SW |
1018 | { |
1019 | struct ceph_osd_request *req, *tmp; | |
1020 | ||
f9d25199 AE |
1021 | dout("__send_queued\n"); |
1022 | list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) | |
6f6c7006 | 1023 | __send_request(osdc, req); |
6f6c7006 SW |
1024 | } |
1025 | ||
f24e9980 SW |
1026 | /* |
1027 | * Timeout callback, called every N seconds when 1 or more osd | |
1028 | * requests has been active for more than N seconds. When this | |
1029 | * happens, we ping all OSDs with requests who have timed out to | |
1030 | * ensure any communications channel reset is detected. Reset the | |
1031 | * request timeouts another N seconds in the future as we go. | |
1032 | * Reschedule the timeout event another N seconds in future (unless | |
1033 | * there are no open requests). | |
1034 | */ | |
1035 | static void handle_timeout(struct work_struct *work) | |
1036 | { | |
1037 | struct ceph_osd_client *osdc = | |
1038 | container_of(work, struct ceph_osd_client, timeout_work.work); | |
83aff95e | 1039 | struct ceph_osd_request *req; |
f24e9980 | 1040 | struct ceph_osd *osd; |
422d2cb8 | 1041 | unsigned long keepalive = |
3d14c5d2 | 1042 | osdc->client->options->osd_keepalive_timeout * HZ; |
422d2cb8 | 1043 | struct list_head slow_osds; |
f24e9980 SW |
1044 | dout("timeout\n"); |
1045 | down_read(&osdc->map_sem); | |
1046 | ||
1047 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1048 | ||
1049 | mutex_lock(&osdc->request_mutex); | |
f24e9980 | 1050 | |
422d2cb8 YS |
1051 | /* |
1052 | * ping osds that are a bit slow. this ensures that if there | |
1053 | * is a break in the TCP connection we will notice, and reopen | |
1054 | * a connection with that osd (from the fault callback). | |
1055 | */ | |
1056 | INIT_LIST_HEAD(&slow_osds); | |
1057 | list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) { | |
3dd72fc0 | 1058 | if (time_before(jiffies, req->r_stamp + keepalive)) |
422d2cb8 YS |
1059 | break; |
1060 | ||
1061 | osd = req->r_osd; | |
1062 | BUG_ON(!osd); | |
1063 | dout(" tid %llu is slow, will send keepalive on osd%d\n", | |
f24e9980 | 1064 | req->r_tid, osd->o_osd); |
422d2cb8 YS |
1065 | list_move_tail(&osd->o_keepalive_item, &slow_osds); |
1066 | } | |
1067 | while (!list_empty(&slow_osds)) { | |
1068 | osd = list_entry(slow_osds.next, struct ceph_osd, | |
1069 | o_keepalive_item); | |
1070 | list_del_init(&osd->o_keepalive_item); | |
f24e9980 SW |
1071 | ceph_con_keepalive(&osd->o_con); |
1072 | } | |
1073 | ||
422d2cb8 | 1074 | __schedule_osd_timeout(osdc); |
f9d25199 | 1075 | __send_queued(osdc); |
f24e9980 | 1076 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
1077 | up_read(&osdc->map_sem); |
1078 | } | |
1079 | ||
f5a2041b YS |
1080 | static void handle_osds_timeout(struct work_struct *work) |
1081 | { | |
1082 | struct ceph_osd_client *osdc = | |
1083 | container_of(work, struct ceph_osd_client, | |
1084 | osds_timeout_work.work); | |
1085 | unsigned long delay = | |
3d14c5d2 | 1086 | osdc->client->options->osd_idle_ttl * HZ >> 2; |
f5a2041b YS |
1087 | |
1088 | dout("osds timeout\n"); | |
1089 | down_read(&osdc->map_sem); | |
aca420bc | 1090 | remove_old_osds(osdc); |
f5a2041b YS |
1091 | up_read(&osdc->map_sem); |
1092 | ||
1093 | schedule_delayed_work(&osdc->osds_timeout_work, | |
1094 | round_jiffies_relative(delay)); | |
1095 | } | |
1096 | ||
25845472 SW |
1097 | static void complete_request(struct ceph_osd_request *req) |
1098 | { | |
1099 | if (req->r_safe_callback) | |
1100 | req->r_safe_callback(req, NULL); | |
1101 | complete_all(&req->r_safe_completion); /* fsync waiter */ | |
1102 | } | |
1103 | ||
f24e9980 SW |
1104 | /* |
1105 | * handle osd op reply. either call the callback if it is specified, | |
1106 | * or do the completion to wake up the waiting thread. | |
1107 | */ | |
350b1c32 SW |
1108 | static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg, |
1109 | struct ceph_connection *con) | |
f24e9980 SW |
1110 | { |
1111 | struct ceph_osd_reply_head *rhead = msg->front.iov_base; | |
1112 | struct ceph_osd_request *req; | |
1113 | u64 tid; | |
1114 | int numops, object_len, flags; | |
0ceed5db | 1115 | s32 result; |
f24e9980 | 1116 | |
6df058c0 | 1117 | tid = le64_to_cpu(msg->hdr.tid); |
f24e9980 SW |
1118 | if (msg->front.iov_len < sizeof(*rhead)) |
1119 | goto bad; | |
f24e9980 SW |
1120 | numops = le32_to_cpu(rhead->num_ops); |
1121 | object_len = le32_to_cpu(rhead->object_len); | |
0ceed5db | 1122 | result = le32_to_cpu(rhead->result); |
f24e9980 SW |
1123 | if (msg->front.iov_len != sizeof(*rhead) + object_len + |
1124 | numops * sizeof(struct ceph_osd_op)) | |
1125 | goto bad; | |
0ceed5db | 1126 | dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result); |
f24e9980 SW |
1127 | /* lookup */ |
1128 | mutex_lock(&osdc->request_mutex); | |
1129 | req = __lookup_request(osdc, tid); | |
1130 | if (req == NULL) { | |
1131 | dout("handle_reply tid %llu dne\n", tid); | |
1132 | mutex_unlock(&osdc->request_mutex); | |
1133 | return; | |
1134 | } | |
1135 | ceph_osdc_get_request(req); | |
1136 | flags = le32_to_cpu(rhead->flags); | |
1137 | ||
350b1c32 | 1138 | /* |
0d59ab81 | 1139 | * if this connection filled our message, drop our reference now, to |
350b1c32 SW |
1140 | * avoid a (safe but slower) revoke later. |
1141 | */ | |
0d59ab81 | 1142 | if (req->r_con_filling_msg == con && req->r_reply == msg) { |
c16e7869 | 1143 | dout(" dropping con_filling_msg ref %p\n", con); |
0d59ab81 | 1144 | req->r_con_filling_msg = NULL; |
0d47766f | 1145 | con->ops->put(con); |
350b1c32 SW |
1146 | } |
1147 | ||
f24e9980 | 1148 | if (!req->r_got_reply) { |
95c96174 | 1149 | unsigned int bytes; |
f24e9980 SW |
1150 | |
1151 | req->r_result = le32_to_cpu(rhead->result); | |
1152 | bytes = le32_to_cpu(msg->hdr.data_len); | |
1153 | dout("handle_reply result %d bytes %d\n", req->r_result, | |
1154 | bytes); | |
1155 | if (req->r_result == 0) | |
1156 | req->r_result = bytes; | |
1157 | ||
1158 | /* in case this is a write and we need to replay, */ | |
1159 | req->r_reassert_version = rhead->reassert_version; | |
1160 | ||
1161 | req->r_got_reply = 1; | |
1162 | } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) { | |
1163 | dout("handle_reply tid %llu dup ack\n", tid); | |
34b43a56 | 1164 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
1165 | goto done; |
1166 | } | |
1167 | ||
1168 | dout("handle_reply tid %llu flags %d\n", tid, flags); | |
1169 | ||
a40c4f10 YS |
1170 | if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK)) |
1171 | __register_linger_request(osdc, req); | |
1172 | ||
f24e9980 | 1173 | /* either this is a read, or we got the safe response */ |
0ceed5db SW |
1174 | if (result < 0 || |
1175 | (flags & CEPH_OSD_FLAG_ONDISK) || | |
f24e9980 SW |
1176 | ((flags & CEPH_OSD_FLAG_WRITE) == 0)) |
1177 | __unregister_request(osdc, req); | |
1178 | ||
1179 | mutex_unlock(&osdc->request_mutex); | |
1180 | ||
1181 | if (req->r_callback) | |
1182 | req->r_callback(req, msg); | |
1183 | else | |
03066f23 | 1184 | complete_all(&req->r_completion); |
f24e9980 | 1185 | |
25845472 SW |
1186 | if (flags & CEPH_OSD_FLAG_ONDISK) |
1187 | complete_request(req); | |
f24e9980 SW |
1188 | |
1189 | done: | |
a40c4f10 | 1190 | dout("req=%p req->r_linger=%d\n", req, req->r_linger); |
f24e9980 SW |
1191 | ceph_osdc_put_request(req); |
1192 | return; | |
1193 | ||
1194 | bad: | |
1195 | pr_err("corrupt osd_op_reply got %d %d expected %d\n", | |
1196 | (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len), | |
1197 | (int)sizeof(*rhead)); | |
9ec7cab1 | 1198 | ceph_msg_dump(msg); |
f24e9980 SW |
1199 | } |
1200 | ||
6f6c7006 | 1201 | static void reset_changed_osds(struct ceph_osd_client *osdc) |
f24e9980 | 1202 | { |
f24e9980 | 1203 | struct rb_node *p, *n; |
f24e9980 | 1204 | |
6f6c7006 SW |
1205 | for (p = rb_first(&osdc->osds); p; p = n) { |
1206 | struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node); | |
f24e9980 | 1207 | |
6f6c7006 SW |
1208 | n = rb_next(p); |
1209 | if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || | |
1210 | memcmp(&osd->o_con.peer_addr, | |
1211 | ceph_osd_addr(osdc->osdmap, | |
1212 | osd->o_osd), | |
1213 | sizeof(struct ceph_entity_addr)) != 0) | |
1214 | __reset_osd(osdc, osd); | |
f24e9980 | 1215 | } |
422d2cb8 YS |
1216 | } |
1217 | ||
1218 | /* | |
6f6c7006 SW |
1219 | * Requeue requests whose mapping to an OSD has changed. If requests map to |
1220 | * no osd, request a new map. | |
422d2cb8 | 1221 | * |
e6d50f67 | 1222 | * Caller should hold map_sem for read. |
422d2cb8 | 1223 | */ |
38d6453c | 1224 | static void kick_requests(struct ceph_osd_client *osdc, int force_resend) |
422d2cb8 | 1225 | { |
a40c4f10 | 1226 | struct ceph_osd_request *req, *nreq; |
6f6c7006 SW |
1227 | struct rb_node *p; |
1228 | int needmap = 0; | |
1229 | int err; | |
422d2cb8 | 1230 | |
38d6453c | 1231 | dout("kick_requests %s\n", force_resend ? " (force resend)" : ""); |
422d2cb8 | 1232 | mutex_lock(&osdc->request_mutex); |
6194ea89 | 1233 | for (p = rb_first(&osdc->requests); p; ) { |
6f6c7006 | 1234 | req = rb_entry(p, struct ceph_osd_request, r_node); |
6194ea89 | 1235 | p = rb_next(p); |
ab60b16d AE |
1236 | |
1237 | /* | |
1238 | * For linger requests that have not yet been | |
1239 | * registered, move them to the linger list; they'll | |
1240 | * be sent to the osd in the loop below. Unregister | |
1241 | * the request before re-registering it as a linger | |
1242 | * request to ensure the __map_request() below | |
1243 | * will decide it needs to be sent. | |
1244 | */ | |
1245 | if (req->r_linger && list_empty(&req->r_linger_item)) { | |
1246 | dout("%p tid %llu restart on osd%d\n", | |
1247 | req, req->r_tid, | |
1248 | req->r_osd ? req->r_osd->o_osd : -1); | |
1249 | __unregister_request(osdc, req); | |
1250 | __register_linger_request(osdc, req); | |
1251 | continue; | |
1252 | } | |
1253 | ||
38d6453c | 1254 | err = __map_request(osdc, req, force_resend); |
6f6c7006 SW |
1255 | if (err < 0) |
1256 | continue; /* error */ | |
1257 | if (req->r_osd == NULL) { | |
1258 | dout("%p tid %llu maps to no osd\n", req, req->r_tid); | |
1259 | needmap++; /* request a newer map */ | |
1260 | } else if (err > 0) { | |
6194ea89 SW |
1261 | if (!req->r_linger) { |
1262 | dout("%p tid %llu requeued on osd%d\n", req, | |
1263 | req->r_tid, | |
1264 | req->r_osd ? req->r_osd->o_osd : -1); | |
a40c4f10 | 1265 | req->r_flags |= CEPH_OSD_FLAG_RETRY; |
6194ea89 SW |
1266 | } |
1267 | } | |
a40c4f10 YS |
1268 | } |
1269 | ||
1270 | list_for_each_entry_safe(req, nreq, &osdc->req_linger, | |
1271 | r_linger_item) { | |
1272 | dout("linger req=%p req->r_osd=%p\n", req, req->r_osd); | |
1273 | ||
38d6453c | 1274 | err = __map_request(osdc, req, force_resend); |
ab60b16d | 1275 | dout("__map_request returned %d\n", err); |
a40c4f10 YS |
1276 | if (err == 0) |
1277 | continue; /* no change and no osd was specified */ | |
1278 | if (err < 0) | |
1279 | continue; /* hrm! */ | |
1280 | if (req->r_osd == NULL) { | |
1281 | dout("tid %llu maps to no valid osd\n", req->r_tid); | |
1282 | needmap++; /* request a newer map */ | |
1283 | continue; | |
6f6c7006 | 1284 | } |
a40c4f10 YS |
1285 | |
1286 | dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid, | |
1287 | req->r_osd ? req->r_osd->o_osd : -1); | |
a40c4f10 | 1288 | __register_request(osdc, req); |
c89ce05e | 1289 | __unregister_linger_request(osdc, req); |
6f6c7006 | 1290 | } |
f24e9980 SW |
1291 | mutex_unlock(&osdc->request_mutex); |
1292 | ||
1293 | if (needmap) { | |
1294 | dout("%d requests for down osds, need new map\n", needmap); | |
1295 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1296 | } | |
e6d50f67 | 1297 | reset_changed_osds(osdc); |
422d2cb8 | 1298 | } |
6f6c7006 SW |
1299 | |
1300 | ||
f24e9980 SW |
1301 | /* |
1302 | * Process updated osd map. | |
1303 | * | |
1304 | * The message contains any number of incremental and full maps, normally | |
1305 | * indicating some sort of topology change in the cluster. Kick requests | |
1306 | * off to different OSDs as needed. | |
1307 | */ | |
1308 | void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) | |
1309 | { | |
1310 | void *p, *end, *next; | |
1311 | u32 nr_maps, maplen; | |
1312 | u32 epoch; | |
1313 | struct ceph_osdmap *newmap = NULL, *oldmap; | |
1314 | int err; | |
1315 | struct ceph_fsid fsid; | |
1316 | ||
1317 | dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0); | |
1318 | p = msg->front.iov_base; | |
1319 | end = p + msg->front.iov_len; | |
1320 | ||
1321 | /* verify fsid */ | |
1322 | ceph_decode_need(&p, end, sizeof(fsid), bad); | |
1323 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | |
0743304d SW |
1324 | if (ceph_check_fsid(osdc->client, &fsid) < 0) |
1325 | return; | |
f24e9980 SW |
1326 | |
1327 | down_write(&osdc->map_sem); | |
1328 | ||
1329 | /* incremental maps */ | |
1330 | ceph_decode_32_safe(&p, end, nr_maps, bad); | |
1331 | dout(" %d inc maps\n", nr_maps); | |
1332 | while (nr_maps > 0) { | |
1333 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | |
c89136ea SW |
1334 | epoch = ceph_decode_32(&p); |
1335 | maplen = ceph_decode_32(&p); | |
f24e9980 SW |
1336 | ceph_decode_need(&p, end, maplen, bad); |
1337 | next = p + maplen; | |
1338 | if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) { | |
1339 | dout("applying incremental map %u len %d\n", | |
1340 | epoch, maplen); | |
1341 | newmap = osdmap_apply_incremental(&p, next, | |
1342 | osdc->osdmap, | |
15d9882c | 1343 | &osdc->client->msgr); |
f24e9980 SW |
1344 | if (IS_ERR(newmap)) { |
1345 | err = PTR_ERR(newmap); | |
1346 | goto bad; | |
1347 | } | |
30dc6381 | 1348 | BUG_ON(!newmap); |
f24e9980 SW |
1349 | if (newmap != osdc->osdmap) { |
1350 | ceph_osdmap_destroy(osdc->osdmap); | |
1351 | osdc->osdmap = newmap; | |
1352 | } | |
38d6453c | 1353 | kick_requests(osdc, 0); |
f24e9980 SW |
1354 | } else { |
1355 | dout("ignoring incremental map %u len %d\n", | |
1356 | epoch, maplen); | |
1357 | } | |
1358 | p = next; | |
1359 | nr_maps--; | |
1360 | } | |
1361 | if (newmap) | |
1362 | goto done; | |
1363 | ||
1364 | /* full maps */ | |
1365 | ceph_decode_32_safe(&p, end, nr_maps, bad); | |
1366 | dout(" %d full maps\n", nr_maps); | |
1367 | while (nr_maps) { | |
1368 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | |
c89136ea SW |
1369 | epoch = ceph_decode_32(&p); |
1370 | maplen = ceph_decode_32(&p); | |
f24e9980 SW |
1371 | ceph_decode_need(&p, end, maplen, bad); |
1372 | if (nr_maps > 1) { | |
1373 | dout("skipping non-latest full map %u len %d\n", | |
1374 | epoch, maplen); | |
1375 | } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) { | |
1376 | dout("skipping full map %u len %d, " | |
1377 | "older than our %u\n", epoch, maplen, | |
1378 | osdc->osdmap->epoch); | |
1379 | } else { | |
38d6453c SW |
1380 | int skipped_map = 0; |
1381 | ||
f24e9980 SW |
1382 | dout("taking full map %u len %d\n", epoch, maplen); |
1383 | newmap = osdmap_decode(&p, p+maplen); | |
1384 | if (IS_ERR(newmap)) { | |
1385 | err = PTR_ERR(newmap); | |
1386 | goto bad; | |
1387 | } | |
30dc6381 | 1388 | BUG_ON(!newmap); |
f24e9980 SW |
1389 | oldmap = osdc->osdmap; |
1390 | osdc->osdmap = newmap; | |
38d6453c SW |
1391 | if (oldmap) { |
1392 | if (oldmap->epoch + 1 < newmap->epoch) | |
1393 | skipped_map = 1; | |
f24e9980 | 1394 | ceph_osdmap_destroy(oldmap); |
38d6453c SW |
1395 | } |
1396 | kick_requests(osdc, skipped_map); | |
f24e9980 SW |
1397 | } |
1398 | p += maplen; | |
1399 | nr_maps--; | |
1400 | } | |
1401 | ||
1402 | done: | |
1403 | downgrade_write(&osdc->map_sem); | |
1404 | ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch); | |
cd634fb6 SW |
1405 | |
1406 | /* | |
1407 | * subscribe to subsequent osdmap updates if full to ensure | |
1408 | * we find out when we are no longer full and stop returning | |
1409 | * ENOSPC. | |
1410 | */ | |
1411 | if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) | |
1412 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1413 | ||
f9d25199 AE |
1414 | mutex_lock(&osdc->request_mutex); |
1415 | __send_queued(osdc); | |
1416 | mutex_unlock(&osdc->request_mutex); | |
f24e9980 | 1417 | up_read(&osdc->map_sem); |
03066f23 | 1418 | wake_up_all(&osdc->client->auth_wq); |
f24e9980 SW |
1419 | return; |
1420 | ||
1421 | bad: | |
1422 | pr_err("osdc handle_map corrupt msg\n"); | |
9ec7cab1 | 1423 | ceph_msg_dump(msg); |
f24e9980 SW |
1424 | up_write(&osdc->map_sem); |
1425 | return; | |
1426 | } | |
1427 | ||
a40c4f10 YS |
1428 | /* |
1429 | * watch/notify callback event infrastructure | |
1430 | * | |
1431 | * These callbacks are used both for watch and notify operations. | |
1432 | */ | |
1433 | static void __release_event(struct kref *kref) | |
1434 | { | |
1435 | struct ceph_osd_event *event = | |
1436 | container_of(kref, struct ceph_osd_event, kref); | |
1437 | ||
1438 | dout("__release_event %p\n", event); | |
1439 | kfree(event); | |
1440 | } | |
1441 | ||
1442 | static void get_event(struct ceph_osd_event *event) | |
1443 | { | |
1444 | kref_get(&event->kref); | |
1445 | } | |
1446 | ||
1447 | void ceph_osdc_put_event(struct ceph_osd_event *event) | |
1448 | { | |
1449 | kref_put(&event->kref, __release_event); | |
1450 | } | |
1451 | EXPORT_SYMBOL(ceph_osdc_put_event); | |
1452 | ||
1453 | static void __insert_event(struct ceph_osd_client *osdc, | |
1454 | struct ceph_osd_event *new) | |
1455 | { | |
1456 | struct rb_node **p = &osdc->event_tree.rb_node; | |
1457 | struct rb_node *parent = NULL; | |
1458 | struct ceph_osd_event *event = NULL; | |
1459 | ||
1460 | while (*p) { | |
1461 | parent = *p; | |
1462 | event = rb_entry(parent, struct ceph_osd_event, node); | |
1463 | if (new->cookie < event->cookie) | |
1464 | p = &(*p)->rb_left; | |
1465 | else if (new->cookie > event->cookie) | |
1466 | p = &(*p)->rb_right; | |
1467 | else | |
1468 | BUG(); | |
1469 | } | |
1470 | ||
1471 | rb_link_node(&new->node, parent, p); | |
1472 | rb_insert_color(&new->node, &osdc->event_tree); | |
1473 | } | |
1474 | ||
1475 | static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc, | |
1476 | u64 cookie) | |
1477 | { | |
1478 | struct rb_node **p = &osdc->event_tree.rb_node; | |
1479 | struct rb_node *parent = NULL; | |
1480 | struct ceph_osd_event *event = NULL; | |
1481 | ||
1482 | while (*p) { | |
1483 | parent = *p; | |
1484 | event = rb_entry(parent, struct ceph_osd_event, node); | |
1485 | if (cookie < event->cookie) | |
1486 | p = &(*p)->rb_left; | |
1487 | else if (cookie > event->cookie) | |
1488 | p = &(*p)->rb_right; | |
1489 | else | |
1490 | return event; | |
1491 | } | |
1492 | return NULL; | |
1493 | } | |
1494 | ||
1495 | static void __remove_event(struct ceph_osd_event *event) | |
1496 | { | |
1497 | struct ceph_osd_client *osdc = event->osdc; | |
1498 | ||
1499 | if (!RB_EMPTY_NODE(&event->node)) { | |
1500 | dout("__remove_event removed %p\n", event); | |
1501 | rb_erase(&event->node, &osdc->event_tree); | |
1502 | ceph_osdc_put_event(event); | |
1503 | } else { | |
1504 | dout("__remove_event didn't remove %p\n", event); | |
1505 | } | |
1506 | } | |
1507 | ||
1508 | int ceph_osdc_create_event(struct ceph_osd_client *osdc, | |
1509 | void (*event_cb)(u64, u64, u8, void *), | |
3c663bbd | 1510 | void *data, struct ceph_osd_event **pevent) |
a40c4f10 YS |
1511 | { |
1512 | struct ceph_osd_event *event; | |
1513 | ||
1514 | event = kmalloc(sizeof(*event), GFP_NOIO); | |
1515 | if (!event) | |
1516 | return -ENOMEM; | |
1517 | ||
1518 | dout("create_event %p\n", event); | |
1519 | event->cb = event_cb; | |
3c663bbd | 1520 | event->one_shot = 0; |
a40c4f10 YS |
1521 | event->data = data; |
1522 | event->osdc = osdc; | |
1523 | INIT_LIST_HEAD(&event->osd_node); | |
3ee5234d | 1524 | RB_CLEAR_NODE(&event->node); |
a40c4f10 YS |
1525 | kref_init(&event->kref); /* one ref for us */ |
1526 | kref_get(&event->kref); /* one ref for the caller */ | |
a40c4f10 YS |
1527 | |
1528 | spin_lock(&osdc->event_lock); | |
1529 | event->cookie = ++osdc->event_count; | |
1530 | __insert_event(osdc, event); | |
1531 | spin_unlock(&osdc->event_lock); | |
1532 | ||
1533 | *pevent = event; | |
1534 | return 0; | |
1535 | } | |
1536 | EXPORT_SYMBOL(ceph_osdc_create_event); | |
1537 | ||
1538 | void ceph_osdc_cancel_event(struct ceph_osd_event *event) | |
1539 | { | |
1540 | struct ceph_osd_client *osdc = event->osdc; | |
1541 | ||
1542 | dout("cancel_event %p\n", event); | |
1543 | spin_lock(&osdc->event_lock); | |
1544 | __remove_event(event); | |
1545 | spin_unlock(&osdc->event_lock); | |
1546 | ceph_osdc_put_event(event); /* caller's */ | |
1547 | } | |
1548 | EXPORT_SYMBOL(ceph_osdc_cancel_event); | |
1549 | ||
1550 | ||
1551 | static void do_event_work(struct work_struct *work) | |
1552 | { | |
1553 | struct ceph_osd_event_work *event_work = | |
1554 | container_of(work, struct ceph_osd_event_work, work); | |
1555 | struct ceph_osd_event *event = event_work->event; | |
1556 | u64 ver = event_work->ver; | |
1557 | u64 notify_id = event_work->notify_id; | |
1558 | u8 opcode = event_work->opcode; | |
1559 | ||
1560 | dout("do_event_work completing %p\n", event); | |
1561 | event->cb(ver, notify_id, opcode, event->data); | |
a40c4f10 YS |
1562 | dout("do_event_work completed %p\n", event); |
1563 | ceph_osdc_put_event(event); | |
1564 | kfree(event_work); | |
1565 | } | |
1566 | ||
1567 | ||
1568 | /* | |
1569 | * Process osd watch notifications | |
1570 | */ | |
3c663bbd AE |
1571 | static void handle_watch_notify(struct ceph_osd_client *osdc, |
1572 | struct ceph_msg *msg) | |
a40c4f10 YS |
1573 | { |
1574 | void *p, *end; | |
1575 | u8 proto_ver; | |
1576 | u64 cookie, ver, notify_id; | |
1577 | u8 opcode; | |
1578 | struct ceph_osd_event *event; | |
1579 | struct ceph_osd_event_work *event_work; | |
1580 | ||
1581 | p = msg->front.iov_base; | |
1582 | end = p + msg->front.iov_len; | |
1583 | ||
1584 | ceph_decode_8_safe(&p, end, proto_ver, bad); | |
1585 | ceph_decode_8_safe(&p, end, opcode, bad); | |
1586 | ceph_decode_64_safe(&p, end, cookie, bad); | |
1587 | ceph_decode_64_safe(&p, end, ver, bad); | |
1588 | ceph_decode_64_safe(&p, end, notify_id, bad); | |
1589 | ||
1590 | spin_lock(&osdc->event_lock); | |
1591 | event = __find_event(osdc, cookie); | |
1592 | if (event) { | |
3c663bbd | 1593 | BUG_ON(event->one_shot); |
a40c4f10 | 1594 | get_event(event); |
a40c4f10 YS |
1595 | } |
1596 | spin_unlock(&osdc->event_lock); | |
1597 | dout("handle_watch_notify cookie %lld ver %lld event %p\n", | |
1598 | cookie, ver, event); | |
1599 | if (event) { | |
1600 | event_work = kmalloc(sizeof(*event_work), GFP_NOIO); | |
a40c4f10 YS |
1601 | if (!event_work) { |
1602 | dout("ERROR: could not allocate event_work\n"); | |
1603 | goto done_err; | |
1604 | } | |
6b0ae409 | 1605 | INIT_WORK(&event_work->work, do_event_work); |
a40c4f10 YS |
1606 | event_work->event = event; |
1607 | event_work->ver = ver; | |
1608 | event_work->notify_id = notify_id; | |
1609 | event_work->opcode = opcode; | |
1610 | if (!queue_work(osdc->notify_wq, &event_work->work)) { | |
1611 | dout("WARNING: failed to queue notify event work\n"); | |
1612 | goto done_err; | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | return; | |
1617 | ||
1618 | done_err: | |
a40c4f10 YS |
1619 | ceph_osdc_put_event(event); |
1620 | return; | |
1621 | ||
1622 | bad: | |
1623 | pr_err("osdc handle_watch_notify corrupt msg\n"); | |
1624 | return; | |
1625 | } | |
1626 | ||
f24e9980 SW |
1627 | /* |
1628 | * Register request, send initial attempt. | |
1629 | */ | |
1630 | int ceph_osdc_start_request(struct ceph_osd_client *osdc, | |
1631 | struct ceph_osd_request *req, | |
1632 | bool nofail) | |
1633 | { | |
c1ea8823 | 1634 | int rc = 0; |
f24e9980 SW |
1635 | |
1636 | req->r_request->pages = req->r_pages; | |
1637 | req->r_request->nr_pages = req->r_num_pages; | |
68b4476b YS |
1638 | #ifdef CONFIG_BLOCK |
1639 | req->r_request->bio = req->r_bio; | |
1640 | #endif | |
c885837f | 1641 | req->r_request->trail = &req->r_trail; |
f24e9980 SW |
1642 | |
1643 | register_request(osdc, req); | |
1644 | ||
1645 | down_read(&osdc->map_sem); | |
1646 | mutex_lock(&osdc->request_mutex); | |
c1ea8823 SW |
1647 | /* |
1648 | * a racing kick_requests() may have sent the message for us | |
1649 | * while we dropped request_mutex above, so only send now if | |
1650 | * the request still han't been touched yet. | |
1651 | */ | |
1652 | if (req->r_sent == 0) { | |
38d6453c | 1653 | rc = __map_request(osdc, req, 0); |
9d6fcb08 SW |
1654 | if (rc < 0) { |
1655 | if (nofail) { | |
1656 | dout("osdc_start_request failed map, " | |
1657 | " will retry %lld\n", req->r_tid); | |
1658 | rc = 0; | |
1659 | } | |
234af26f | 1660 | goto out_unlock; |
9d6fcb08 | 1661 | } |
6f6c7006 SW |
1662 | if (req->r_osd == NULL) { |
1663 | dout("send_request %p no up osds in pg\n", req); | |
1664 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1665 | } else { | |
56e925b6 | 1666 | __send_request(osdc, req); |
f24e9980 | 1667 | } |
56e925b6 | 1668 | rc = 0; |
f24e9980 | 1669 | } |
234af26f DC |
1670 | |
1671 | out_unlock: | |
f24e9980 SW |
1672 | mutex_unlock(&osdc->request_mutex); |
1673 | up_read(&osdc->map_sem); | |
1674 | return rc; | |
1675 | } | |
3d14c5d2 | 1676 | EXPORT_SYMBOL(ceph_osdc_start_request); |
f24e9980 SW |
1677 | |
1678 | /* | |
1679 | * wait for a request to complete | |
1680 | */ | |
1681 | int ceph_osdc_wait_request(struct ceph_osd_client *osdc, | |
1682 | struct ceph_osd_request *req) | |
1683 | { | |
1684 | int rc; | |
1685 | ||
1686 | rc = wait_for_completion_interruptible(&req->r_completion); | |
1687 | if (rc < 0) { | |
1688 | mutex_lock(&osdc->request_mutex); | |
1689 | __cancel_request(req); | |
529cfcc4 | 1690 | __unregister_request(osdc, req); |
f24e9980 | 1691 | mutex_unlock(&osdc->request_mutex); |
25845472 | 1692 | complete_request(req); |
529cfcc4 | 1693 | dout("wait_request tid %llu canceled/timed out\n", req->r_tid); |
f24e9980 SW |
1694 | return rc; |
1695 | } | |
1696 | ||
1697 | dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result); | |
1698 | return req->r_result; | |
1699 | } | |
3d14c5d2 | 1700 | EXPORT_SYMBOL(ceph_osdc_wait_request); |
f24e9980 SW |
1701 | |
1702 | /* | |
1703 | * sync - wait for all in-flight requests to flush. avoid starvation. | |
1704 | */ | |
1705 | void ceph_osdc_sync(struct ceph_osd_client *osdc) | |
1706 | { | |
1707 | struct ceph_osd_request *req; | |
1708 | u64 last_tid, next_tid = 0; | |
1709 | ||
1710 | mutex_lock(&osdc->request_mutex); | |
1711 | last_tid = osdc->last_tid; | |
1712 | while (1) { | |
1713 | req = __lookup_request_ge(osdc, next_tid); | |
1714 | if (!req) | |
1715 | break; | |
1716 | if (req->r_tid > last_tid) | |
1717 | break; | |
1718 | ||
1719 | next_tid = req->r_tid + 1; | |
1720 | if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0) | |
1721 | continue; | |
1722 | ||
1723 | ceph_osdc_get_request(req); | |
1724 | mutex_unlock(&osdc->request_mutex); | |
1725 | dout("sync waiting on tid %llu (last is %llu)\n", | |
1726 | req->r_tid, last_tid); | |
1727 | wait_for_completion(&req->r_safe_completion); | |
1728 | mutex_lock(&osdc->request_mutex); | |
1729 | ceph_osdc_put_request(req); | |
1730 | } | |
1731 | mutex_unlock(&osdc->request_mutex); | |
1732 | dout("sync done (thru tid %llu)\n", last_tid); | |
1733 | } | |
3d14c5d2 | 1734 | EXPORT_SYMBOL(ceph_osdc_sync); |
f24e9980 SW |
1735 | |
1736 | /* | |
1737 | * init, shutdown | |
1738 | */ | |
1739 | int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) | |
1740 | { | |
1741 | int err; | |
1742 | ||
1743 | dout("init\n"); | |
1744 | osdc->client = client; | |
1745 | osdc->osdmap = NULL; | |
1746 | init_rwsem(&osdc->map_sem); | |
1747 | init_completion(&osdc->map_waiters); | |
1748 | osdc->last_requested_map = 0; | |
1749 | mutex_init(&osdc->request_mutex); | |
f24e9980 SW |
1750 | osdc->last_tid = 0; |
1751 | osdc->osds = RB_ROOT; | |
f5a2041b | 1752 | INIT_LIST_HEAD(&osdc->osd_lru); |
f24e9980 | 1753 | osdc->requests = RB_ROOT; |
422d2cb8 | 1754 | INIT_LIST_HEAD(&osdc->req_lru); |
6f6c7006 SW |
1755 | INIT_LIST_HEAD(&osdc->req_unsent); |
1756 | INIT_LIST_HEAD(&osdc->req_notarget); | |
a40c4f10 | 1757 | INIT_LIST_HEAD(&osdc->req_linger); |
f24e9980 SW |
1758 | osdc->num_requests = 0; |
1759 | INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); | |
f5a2041b | 1760 | INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); |
a40c4f10 YS |
1761 | spin_lock_init(&osdc->event_lock); |
1762 | osdc->event_tree = RB_ROOT; | |
1763 | osdc->event_count = 0; | |
f5a2041b YS |
1764 | |
1765 | schedule_delayed_work(&osdc->osds_timeout_work, | |
3d14c5d2 | 1766 | round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ)); |
f24e9980 | 1767 | |
5f44f142 | 1768 | err = -ENOMEM; |
f24e9980 SW |
1769 | osdc->req_mempool = mempool_create_kmalloc_pool(10, |
1770 | sizeof(struct ceph_osd_request)); | |
1771 | if (!osdc->req_mempool) | |
5f44f142 | 1772 | goto out; |
f24e9980 | 1773 | |
d50b409f SW |
1774 | err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP, |
1775 | OSD_OP_FRONT_LEN, 10, true, | |
4f48280e | 1776 | "osd_op"); |
f24e9980 | 1777 | if (err < 0) |
5f44f142 | 1778 | goto out_mempool; |
d50b409f | 1779 | err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY, |
4f48280e SW |
1780 | OSD_OPREPLY_FRONT_LEN, 10, true, |
1781 | "osd_op_reply"); | |
c16e7869 SW |
1782 | if (err < 0) |
1783 | goto out_msgpool; | |
a40c4f10 YS |
1784 | |
1785 | osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify"); | |
1786 | if (IS_ERR(osdc->notify_wq)) { | |
1787 | err = PTR_ERR(osdc->notify_wq); | |
1788 | osdc->notify_wq = NULL; | |
1789 | goto out_msgpool; | |
1790 | } | |
f24e9980 | 1791 | return 0; |
5f44f142 | 1792 | |
c16e7869 SW |
1793 | out_msgpool: |
1794 | ceph_msgpool_destroy(&osdc->msgpool_op); | |
5f44f142 SW |
1795 | out_mempool: |
1796 | mempool_destroy(osdc->req_mempool); | |
1797 | out: | |
1798 | return err; | |
f24e9980 SW |
1799 | } |
1800 | ||
1801 | void ceph_osdc_stop(struct ceph_osd_client *osdc) | |
1802 | { | |
a40c4f10 YS |
1803 | flush_workqueue(osdc->notify_wq); |
1804 | destroy_workqueue(osdc->notify_wq); | |
f24e9980 | 1805 | cancel_delayed_work_sync(&osdc->timeout_work); |
f5a2041b | 1806 | cancel_delayed_work_sync(&osdc->osds_timeout_work); |
f24e9980 SW |
1807 | if (osdc->osdmap) { |
1808 | ceph_osdmap_destroy(osdc->osdmap); | |
1809 | osdc->osdmap = NULL; | |
1810 | } | |
aca420bc | 1811 | remove_all_osds(osdc); |
f24e9980 SW |
1812 | mempool_destroy(osdc->req_mempool); |
1813 | ceph_msgpool_destroy(&osdc->msgpool_op); | |
c16e7869 | 1814 | ceph_msgpool_destroy(&osdc->msgpool_op_reply); |
f24e9980 SW |
1815 | } |
1816 | ||
1817 | /* | |
1818 | * Read some contiguous pages. If we cross a stripe boundary, shorten | |
1819 | * *plen. Return number of bytes read, or error. | |
1820 | */ | |
1821 | int ceph_osdc_readpages(struct ceph_osd_client *osdc, | |
1822 | struct ceph_vino vino, struct ceph_file_layout *layout, | |
1823 | u64 off, u64 *plen, | |
1824 | u32 truncate_seq, u64 truncate_size, | |
b7495fc2 | 1825 | struct page **pages, int num_pages, int page_align) |
f24e9980 SW |
1826 | { |
1827 | struct ceph_osd_request *req; | |
1828 | int rc = 0; | |
1829 | ||
1830 | dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino, | |
1831 | vino.snap, off, *plen); | |
1832 | req = ceph_osdc_new_request(osdc, layout, vino, off, plen, | |
1833 | CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, | |
1834 | NULL, 0, truncate_seq, truncate_size, NULL, | |
a3bea47e | 1835 | false, page_align); |
6816282d SW |
1836 | if (IS_ERR(req)) |
1837 | return PTR_ERR(req); | |
f24e9980 SW |
1838 | |
1839 | /* it may be a short read due to an object boundary */ | |
1840 | req->r_pages = pages; | |
f24e9980 | 1841 | |
b7495fc2 SW |
1842 | dout("readpages final extent is %llu~%llu (%d pages align %d)\n", |
1843 | off, *plen, req->r_num_pages, page_align); | |
f24e9980 SW |
1844 | |
1845 | rc = ceph_osdc_start_request(osdc, req, false); | |
1846 | if (!rc) | |
1847 | rc = ceph_osdc_wait_request(osdc, req); | |
1848 | ||
1849 | ceph_osdc_put_request(req); | |
1850 | dout("readpages result %d\n", rc); | |
1851 | return rc; | |
1852 | } | |
3d14c5d2 | 1853 | EXPORT_SYMBOL(ceph_osdc_readpages); |
f24e9980 SW |
1854 | |
1855 | /* | |
1856 | * do a synchronous write on N pages | |
1857 | */ | |
1858 | int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino, | |
1859 | struct ceph_file_layout *layout, | |
1860 | struct ceph_snap_context *snapc, | |
1861 | u64 off, u64 len, | |
1862 | u32 truncate_seq, u64 truncate_size, | |
1863 | struct timespec *mtime, | |
24808826 | 1864 | struct page **pages, int num_pages) |
f24e9980 SW |
1865 | { |
1866 | struct ceph_osd_request *req; | |
1867 | int rc = 0; | |
b7495fc2 | 1868 | int page_align = off & ~PAGE_MASK; |
f24e9980 SW |
1869 | |
1870 | BUG_ON(vino.snap != CEPH_NOSNAP); | |
1871 | req = ceph_osdc_new_request(osdc, layout, vino, off, &len, | |
1872 | CEPH_OSD_OP_WRITE, | |
24808826 | 1873 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, |
fbf8685f | 1874 | snapc, 0, |
f24e9980 | 1875 | truncate_seq, truncate_size, mtime, |
a3bea47e | 1876 | true, page_align); |
6816282d SW |
1877 | if (IS_ERR(req)) |
1878 | return PTR_ERR(req); | |
f24e9980 SW |
1879 | |
1880 | /* it may be a short write due to an object boundary */ | |
1881 | req->r_pages = pages; | |
f24e9980 SW |
1882 | dout("writepages %llu~%llu (%d pages)\n", off, len, |
1883 | req->r_num_pages); | |
1884 | ||
87f979d3 | 1885 | rc = ceph_osdc_start_request(osdc, req, true); |
f24e9980 SW |
1886 | if (!rc) |
1887 | rc = ceph_osdc_wait_request(osdc, req); | |
1888 | ||
1889 | ceph_osdc_put_request(req); | |
1890 | if (rc == 0) | |
1891 | rc = len; | |
1892 | dout("writepages result %d\n", rc); | |
1893 | return rc; | |
1894 | } | |
3d14c5d2 | 1895 | EXPORT_SYMBOL(ceph_osdc_writepages); |
f24e9980 SW |
1896 | |
1897 | /* | |
1898 | * handle incoming message | |
1899 | */ | |
1900 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | |
1901 | { | |
1902 | struct ceph_osd *osd = con->private; | |
32c895e7 | 1903 | struct ceph_osd_client *osdc; |
f24e9980 SW |
1904 | int type = le16_to_cpu(msg->hdr.type); |
1905 | ||
1906 | if (!osd) | |
4a32f93d | 1907 | goto out; |
32c895e7 | 1908 | osdc = osd->o_osdc; |
f24e9980 SW |
1909 | |
1910 | switch (type) { | |
1911 | case CEPH_MSG_OSD_MAP: | |
1912 | ceph_osdc_handle_map(osdc, msg); | |
1913 | break; | |
1914 | case CEPH_MSG_OSD_OPREPLY: | |
350b1c32 | 1915 | handle_reply(osdc, msg, con); |
f24e9980 | 1916 | break; |
a40c4f10 YS |
1917 | case CEPH_MSG_WATCH_NOTIFY: |
1918 | handle_watch_notify(osdc, msg); | |
1919 | break; | |
f24e9980 SW |
1920 | |
1921 | default: | |
1922 | pr_err("received unknown message type %d %s\n", type, | |
1923 | ceph_msg_type_name(type)); | |
1924 | } | |
4a32f93d | 1925 | out: |
f24e9980 SW |
1926 | ceph_msg_put(msg); |
1927 | } | |
1928 | ||
5b3a4db3 | 1929 | /* |
21b667f6 SW |
1930 | * lookup and return message for incoming reply. set up reply message |
1931 | * pages. | |
5b3a4db3 SW |
1932 | */ |
1933 | static struct ceph_msg *get_reply(struct ceph_connection *con, | |
2450418c YS |
1934 | struct ceph_msg_header *hdr, |
1935 | int *skip) | |
f24e9980 SW |
1936 | { |
1937 | struct ceph_osd *osd = con->private; | |
1938 | struct ceph_osd_client *osdc = osd->o_osdc; | |
2450418c | 1939 | struct ceph_msg *m; |
0547a9b3 | 1940 | struct ceph_osd_request *req; |
5b3a4db3 SW |
1941 | int front = le32_to_cpu(hdr->front_len); |
1942 | int data_len = le32_to_cpu(hdr->data_len); | |
0547a9b3 | 1943 | u64 tid; |
f24e9980 | 1944 | |
0547a9b3 YS |
1945 | tid = le64_to_cpu(hdr->tid); |
1946 | mutex_lock(&osdc->request_mutex); | |
1947 | req = __lookup_request(osdc, tid); | |
1948 | if (!req) { | |
1949 | *skip = 1; | |
1950 | m = NULL; | |
756a16a5 SW |
1951 | dout("get_reply unknown tid %llu from osd%d\n", tid, |
1952 | osd->o_osd); | |
0547a9b3 YS |
1953 | goto out; |
1954 | } | |
c16e7869 SW |
1955 | |
1956 | if (req->r_con_filling_msg) { | |
8921d114 | 1957 | dout("%s revoking msg %p from old con %p\n", __func__, |
c16e7869 | 1958 | req->r_reply, req->r_con_filling_msg); |
8921d114 | 1959 | ceph_msg_revoke_incoming(req->r_reply); |
0d47766f | 1960 | req->r_con_filling_msg->ops->put(req->r_con_filling_msg); |
6f46cb29 | 1961 | req->r_con_filling_msg = NULL; |
0547a9b3 YS |
1962 | } |
1963 | ||
c16e7869 SW |
1964 | if (front > req->r_reply->front.iov_len) { |
1965 | pr_warning("get_reply front %d > preallocated %d\n", | |
1966 | front, (int)req->r_reply->front.iov_len); | |
b61c2763 | 1967 | m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false); |
a79832f2 | 1968 | if (!m) |
c16e7869 SW |
1969 | goto out; |
1970 | ceph_msg_put(req->r_reply); | |
1971 | req->r_reply = m; | |
1972 | } | |
1973 | m = ceph_msg_get(req->r_reply); | |
1974 | ||
0547a9b3 | 1975 | if (data_len > 0) { |
b7495fc2 | 1976 | int want = calc_pages_for(req->r_page_alignment, data_len); |
21b667f6 | 1977 | |
9cbb1d72 | 1978 | if (req->r_pages && unlikely(req->r_num_pages < want)) { |
9bb0ce2b SW |
1979 | pr_warning("tid %lld reply has %d bytes %d pages, we" |
1980 | " had only %d pages ready\n", tid, data_len, | |
1981 | want, req->r_num_pages); | |
0547a9b3 YS |
1982 | *skip = 1; |
1983 | ceph_msg_put(m); | |
a79832f2 | 1984 | m = NULL; |
21b667f6 | 1985 | goto out; |
0547a9b3 | 1986 | } |
21b667f6 SW |
1987 | m->pages = req->r_pages; |
1988 | m->nr_pages = req->r_num_pages; | |
c5c6b19d | 1989 | m->page_alignment = req->r_page_alignment; |
68b4476b YS |
1990 | #ifdef CONFIG_BLOCK |
1991 | m->bio = req->r_bio; | |
1992 | #endif | |
0547a9b3 | 1993 | } |
5b3a4db3 | 1994 | *skip = 0; |
0d47766f | 1995 | req->r_con_filling_msg = con->ops->get(con); |
c16e7869 | 1996 | dout("get_reply tid %lld %p\n", tid, m); |
0547a9b3 YS |
1997 | |
1998 | out: | |
1999 | mutex_unlock(&osdc->request_mutex); | |
2450418c | 2000 | return m; |
5b3a4db3 SW |
2001 | |
2002 | } | |
2003 | ||
2004 | static struct ceph_msg *alloc_msg(struct ceph_connection *con, | |
2005 | struct ceph_msg_header *hdr, | |
2006 | int *skip) | |
2007 | { | |
2008 | struct ceph_osd *osd = con->private; | |
2009 | int type = le16_to_cpu(hdr->type); | |
2010 | int front = le32_to_cpu(hdr->front_len); | |
2011 | ||
1c20f2d2 | 2012 | *skip = 0; |
5b3a4db3 SW |
2013 | switch (type) { |
2014 | case CEPH_MSG_OSD_MAP: | |
a40c4f10 | 2015 | case CEPH_MSG_WATCH_NOTIFY: |
b61c2763 | 2016 | return ceph_msg_new(type, front, GFP_NOFS, false); |
5b3a4db3 SW |
2017 | case CEPH_MSG_OSD_OPREPLY: |
2018 | return get_reply(con, hdr, skip); | |
2019 | default: | |
2020 | pr_info("alloc_msg unexpected msg type %d from osd%d\n", type, | |
2021 | osd->o_osd); | |
2022 | *skip = 1; | |
2023 | return NULL; | |
2024 | } | |
f24e9980 SW |
2025 | } |
2026 | ||
2027 | /* | |
2028 | * Wrappers to refcount containing ceph_osd struct | |
2029 | */ | |
2030 | static struct ceph_connection *get_osd_con(struct ceph_connection *con) | |
2031 | { | |
2032 | struct ceph_osd *osd = con->private; | |
2033 | if (get_osd(osd)) | |
2034 | return con; | |
2035 | return NULL; | |
2036 | } | |
2037 | ||
2038 | static void put_osd_con(struct ceph_connection *con) | |
2039 | { | |
2040 | struct ceph_osd *osd = con->private; | |
2041 | put_osd(osd); | |
2042 | } | |
2043 | ||
4e7a5dcd SW |
2044 | /* |
2045 | * authentication | |
2046 | */ | |
a3530df3 AE |
2047 | /* |
2048 | * Note: returned pointer is the address of a structure that's | |
2049 | * managed separately. Caller must *not* attempt to free it. | |
2050 | */ | |
2051 | static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, | |
8f43fb53 | 2052 | int *proto, int force_new) |
4e7a5dcd SW |
2053 | { |
2054 | struct ceph_osd *o = con->private; | |
2055 | struct ceph_osd_client *osdc = o->o_osdc; | |
2056 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
74f1869f | 2057 | struct ceph_auth_handshake *auth = &o->o_auth; |
4e7a5dcd | 2058 | |
74f1869f | 2059 | if (force_new && auth->authorizer) { |
a255651d AE |
2060 | if (ac->ops && ac->ops->destroy_authorizer) |
2061 | ac->ops->destroy_authorizer(ac, auth->authorizer); | |
74f1869f AE |
2062 | auth->authorizer = NULL; |
2063 | } | |
a255651d | 2064 | if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) { |
a3530df3 AE |
2065 | int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD, |
2066 | auth); | |
4e7a5dcd | 2067 | if (ret) |
a3530df3 | 2068 | return ERR_PTR(ret); |
4e7a5dcd | 2069 | } |
4e7a5dcd | 2070 | *proto = ac->protocol; |
74f1869f | 2071 | |
a3530df3 | 2072 | return auth; |
4e7a5dcd SW |
2073 | } |
2074 | ||
2075 | ||
2076 | static int verify_authorizer_reply(struct ceph_connection *con, int len) | |
2077 | { | |
2078 | struct ceph_osd *o = con->private; | |
2079 | struct ceph_osd_client *osdc = o->o_osdc; | |
2080 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
2081 | ||
a255651d AE |
2082 | /* |
2083 | * XXX If ac->ops or ac->ops->verify_authorizer_reply is null, | |
2084 | * XXX which do we do: succeed or fail? | |
2085 | */ | |
6c4a1915 | 2086 | return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len); |
4e7a5dcd SW |
2087 | } |
2088 | ||
9bd2e6f8 SW |
2089 | static int invalidate_authorizer(struct ceph_connection *con) |
2090 | { | |
2091 | struct ceph_osd *o = con->private; | |
2092 | struct ceph_osd_client *osdc = o->o_osdc; | |
2093 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
2094 | ||
a255651d | 2095 | if (ac->ops && ac->ops->invalidate_authorizer) |
9bd2e6f8 SW |
2096 | ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); |
2097 | ||
2098 | return ceph_monc_validate_auth(&osdc->client->monc); | |
2099 | } | |
4e7a5dcd | 2100 | |
9e32789f | 2101 | static const struct ceph_connection_operations osd_con_ops = { |
f24e9980 SW |
2102 | .get = get_osd_con, |
2103 | .put = put_osd_con, | |
2104 | .dispatch = dispatch, | |
4e7a5dcd SW |
2105 | .get_authorizer = get_authorizer, |
2106 | .verify_authorizer_reply = verify_authorizer_reply, | |
9bd2e6f8 | 2107 | .invalidate_authorizer = invalidate_authorizer, |
f24e9980 | 2108 | .alloc_msg = alloc_msg, |
81b024e7 | 2109 | .fault = osd_reset, |
f24e9980 | 2110 | }; |