1
2 #include <linux/ceph/ceph_debug.h>
3
4 #include <linux/module.h>
5 #include <linux/err.h>
6 #include <linux/highmem.h>
7 #include <linux/mm.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
11 #ifdef CONFIG_BLOCK
12 #include <linux/bio.h>
13 #endif
14
15 #include <linux/ceph/libceph.h>
16 #include <linux/ceph/osd_client.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/pagelist.h>
21
22 #define OSD_OP_FRONT_LEN 4096
23 #define OSD_OPREPLY_FRONT_LEN 512
24
25 static struct kmem_cache *ceph_osd_request_cache;
26
27 static const struct ceph_connection_operations osd_con_ops;
28
29 static void __send_queued(struct ceph_osd_client *osdc);
30 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
31 static void __register_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
33 static void __unregister_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
35 static void __unregister_linger_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
37 static void __enqueue_request(struct ceph_osd_request *req);
38 static void __send_request(struct ceph_osd_client *osdc,
39 struct ceph_osd_request *req);
40
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 */
calc_layout(struct ceph_file_layout * layout,u64 off,u64 * plen,u64 * objnum,u64 * objoff,u64 * objlen)66 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
67 u64 *objnum, u64 *objoff, u64 *objlen)
68 {
69 u64 orig_len = *plen;
70 int r;
71
72 /* object extent? */
73 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
74 objoff, objlen);
75 if (r < 0)
76 return r;
77 if (*objlen < orig_len) {
78 *plen = *objlen;
79 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
81 }
82
83 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
84
85 return 0;
86 }
87
ceph_osd_data_init(struct ceph_osd_data * osd_data)88 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
89 {
90 memset(osd_data, 0, sizeof (*osd_data));
91 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
92 }
93
ceph_osd_data_pages_init(struct ceph_osd_data * osd_data,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)94 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
95 struct page **pages, u64 length, u32 alignment,
96 bool pages_from_pool, bool own_pages)
97 {
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
99 osd_data->pages = pages;
100 osd_data->length = length;
101 osd_data->alignment = alignment;
102 osd_data->pages_from_pool = pages_from_pool;
103 osd_data->own_pages = own_pages;
104 }
105
ceph_osd_data_pagelist_init(struct ceph_osd_data * osd_data,struct ceph_pagelist * pagelist)106 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
107 struct ceph_pagelist *pagelist)
108 {
109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
110 osd_data->pagelist = pagelist;
111 }
112
113 #ifdef CONFIG_BLOCK
ceph_osd_data_bio_init(struct ceph_osd_data * osd_data,struct bio * bio,size_t bio_length)114 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
115 struct bio *bio, size_t bio_length)
116 {
117 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
118 osd_data->bio = bio;
119 osd_data->bio_length = bio_length;
120 }
121 #endif /* CONFIG_BLOCK */
122
123 #define osd_req_op_data(oreq, whch, typ, fld) \
124 ({ \
125 BUG_ON(whch >= (oreq)->r_num_ops); \
126 &(oreq)->r_ops[whch].typ.fld; \
127 })
128
129 static struct ceph_osd_data *
osd_req_op_raw_data_in(struct ceph_osd_request * osd_req,unsigned int which)130 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
131 {
132 BUG_ON(which >= osd_req->r_num_ops);
133
134 return &osd_req->r_ops[which].raw_data_in;
135 }
136
137 struct ceph_osd_data *
osd_req_op_extent_osd_data(struct ceph_osd_request * osd_req,unsigned int which)138 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
139 unsigned int which)
140 {
141 return osd_req_op_data(osd_req, which, extent, osd_data);
142 }
143 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
144
145 struct ceph_osd_data *
osd_req_op_cls_response_data(struct ceph_osd_request * osd_req,unsigned int which)146 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
147 unsigned int which)
148 {
149 return osd_req_op_data(osd_req, which, cls, response_data);
150 }
151 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
152
osd_req_op_raw_data_in_pages(struct ceph_osd_request * osd_req,unsigned int which,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)153 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
154 unsigned int which, struct page **pages,
155 u64 length, u32 alignment,
156 bool pages_from_pool, bool own_pages)
157 {
158 struct ceph_osd_data *osd_data;
159
160 osd_data = osd_req_op_raw_data_in(osd_req, which);
161 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
162 pages_from_pool, own_pages);
163 }
164 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
165
osd_req_op_extent_osd_data_pages(struct ceph_osd_request * osd_req,unsigned int which,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)166 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
167 unsigned int which, struct page **pages,
168 u64 length, u32 alignment,
169 bool pages_from_pool, bool own_pages)
170 {
171 struct ceph_osd_data *osd_data;
172
173 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
174 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
175 pages_from_pool, own_pages);
176 }
177 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
178
osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_pagelist * pagelist)179 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
180 unsigned int which, struct ceph_pagelist *pagelist)
181 {
182 struct ceph_osd_data *osd_data;
183
184 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
185 ceph_osd_data_pagelist_init(osd_data, pagelist);
186 }
187 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
188
189 #ifdef CONFIG_BLOCK
osd_req_op_extent_osd_data_bio(struct ceph_osd_request * osd_req,unsigned int which,struct bio * bio,size_t bio_length)190 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
191 unsigned int which, struct bio *bio, size_t bio_length)
192 {
193 struct ceph_osd_data *osd_data;
194
195 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
196 ceph_osd_data_bio_init(osd_data, bio, bio_length);
197 }
198 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
199 #endif /* CONFIG_BLOCK */
200
osd_req_op_cls_request_info_pagelist(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_pagelist * pagelist)201 static void osd_req_op_cls_request_info_pagelist(
202 struct ceph_osd_request *osd_req,
203 unsigned int which, struct ceph_pagelist *pagelist)
204 {
205 struct ceph_osd_data *osd_data;
206
207 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
208 ceph_osd_data_pagelist_init(osd_data, pagelist);
209 }
210
osd_req_op_cls_request_data_pagelist(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_pagelist * pagelist)211 void osd_req_op_cls_request_data_pagelist(
212 struct ceph_osd_request *osd_req,
213 unsigned int which, struct ceph_pagelist *pagelist)
214 {
215 struct ceph_osd_data *osd_data;
216
217 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
218 ceph_osd_data_pagelist_init(osd_data, pagelist);
219 }
220 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
221
osd_req_op_cls_request_data_pages(struct ceph_osd_request * osd_req,unsigned int which,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)222 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
223 unsigned int which, struct page **pages, u64 length,
224 u32 alignment, bool pages_from_pool, bool own_pages)
225 {
226 struct ceph_osd_data *osd_data;
227
228 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
229 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
230 pages_from_pool, own_pages);
231 }
232 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
233
osd_req_op_cls_response_data_pages(struct ceph_osd_request * osd_req,unsigned int which,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)234 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
235 unsigned int which, struct page **pages, u64 length,
236 u32 alignment, bool pages_from_pool, bool own_pages)
237 {
238 struct ceph_osd_data *osd_data;
239
240 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
241 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
242 pages_from_pool, own_pages);
243 }
244 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
245
ceph_osd_data_length(struct ceph_osd_data * osd_data)246 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
247 {
248 switch (osd_data->type) {
249 case CEPH_OSD_DATA_TYPE_NONE:
250 return 0;
251 case CEPH_OSD_DATA_TYPE_PAGES:
252 return osd_data->length;
253 case CEPH_OSD_DATA_TYPE_PAGELIST:
254 return (u64)osd_data->pagelist->length;
255 #ifdef CONFIG_BLOCK
256 case CEPH_OSD_DATA_TYPE_BIO:
257 return (u64)osd_data->bio_length;
258 #endif /* CONFIG_BLOCK */
259 default:
260 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
261 return 0;
262 }
263 }
264
ceph_osd_data_release(struct ceph_osd_data * osd_data)265 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
266 {
267 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
268 int num_pages;
269
270 num_pages = calc_pages_for((u64)osd_data->alignment,
271 (u64)osd_data->length);
272 ceph_release_page_vector(osd_data->pages, num_pages);
273 }
274 ceph_osd_data_init(osd_data);
275 }
276
osd_req_op_data_release(struct ceph_osd_request * osd_req,unsigned int which)277 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
278 unsigned int which)
279 {
280 struct ceph_osd_req_op *op;
281
282 BUG_ON(which >= osd_req->r_num_ops);
283 op = &osd_req->r_ops[which];
284
285 switch (op->op) {
286 case CEPH_OSD_OP_READ:
287 case CEPH_OSD_OP_WRITE:
288 ceph_osd_data_release(&op->extent.osd_data);
289 break;
290 case CEPH_OSD_OP_CALL:
291 ceph_osd_data_release(&op->cls.request_info);
292 ceph_osd_data_release(&op->cls.request_data);
293 ceph_osd_data_release(&op->cls.response_data);
294 break;
295 default:
296 break;
297 }
298 }
299
300 /*
301 * requests
302 */
ceph_osdc_release_request(struct kref * kref)303 static void ceph_osdc_release_request(struct kref *kref)
304 {
305 struct ceph_osd_request *req = container_of(kref,
306 struct ceph_osd_request, r_kref);
307 unsigned int which;
308
309 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
310 req->r_request, req->r_reply);
311 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
312 WARN_ON(!list_empty(&req->r_req_lru_item));
313 WARN_ON(!list_empty(&req->r_osd_item));
314 WARN_ON(!list_empty(&req->r_linger_item));
315 WARN_ON(!list_empty(&req->r_linger_osd_item));
316 WARN_ON(req->r_osd);
317
318 if (req->r_request)
319 ceph_msg_put(req->r_request);
320 if (req->r_reply) {
321 ceph_msg_revoke_incoming(req->r_reply);
322 ceph_msg_put(req->r_reply);
323 }
324
325 for (which = 0; which < req->r_num_ops; which++)
326 osd_req_op_data_release(req, which);
327
328 ceph_put_snap_context(req->r_snapc);
329 if (req->r_mempool)
330 mempool_free(req, req->r_osdc->req_mempool);
331 else
332 kmem_cache_free(ceph_osd_request_cache, req);
333
334 }
335
ceph_osdc_get_request(struct ceph_osd_request * req)336 void ceph_osdc_get_request(struct ceph_osd_request *req)
337 {
338 dout("%s %p (was %d)\n", __func__, req,
339 atomic_read(&req->r_kref.refcount));
340 kref_get(&req->r_kref);
341 }
342 EXPORT_SYMBOL(ceph_osdc_get_request);
343
ceph_osdc_put_request(struct ceph_osd_request * req)344 void ceph_osdc_put_request(struct ceph_osd_request *req)
345 {
346 dout("%s %p (was %d)\n", __func__, req,
347 atomic_read(&req->r_kref.refcount));
348 kref_put(&req->r_kref, ceph_osdc_release_request);
349 }
350 EXPORT_SYMBOL(ceph_osdc_put_request);
351
ceph_osdc_alloc_request(struct ceph_osd_client * osdc,struct ceph_snap_context * snapc,unsigned int num_ops,bool use_mempool,gfp_t gfp_flags)352 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
353 struct ceph_snap_context *snapc,
354 unsigned int num_ops,
355 bool use_mempool,
356 gfp_t gfp_flags)
357 {
358 struct ceph_osd_request *req;
359 struct ceph_msg *msg;
360 size_t msg_size;
361
362 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
363 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
364
365 msg_size = 4 + 4 + 8 + 8 + 4+8;
366 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
367 msg_size += 1 + 8 + 4 + 4; /* pg_t */
368 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
369 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
370 msg_size += 8; /* snapid */
371 msg_size += 8; /* snap_seq */
372 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
373 msg_size += 4;
374
375 if (use_mempool) {
376 req = mempool_alloc(osdc->req_mempool, gfp_flags);
377 memset(req, 0, sizeof(*req));
378 } else {
379 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
380 }
381 if (req == NULL)
382 return NULL;
383
384 req->r_osdc = osdc;
385 req->r_mempool = use_mempool;
386 req->r_num_ops = num_ops;
387
388 kref_init(&req->r_kref);
389 init_completion(&req->r_completion);
390 init_completion(&req->r_safe_completion);
391 RB_CLEAR_NODE(&req->r_node);
392 INIT_LIST_HEAD(&req->r_unsafe_item);
393 INIT_LIST_HEAD(&req->r_linger_item);
394 INIT_LIST_HEAD(&req->r_linger_osd_item);
395 INIT_LIST_HEAD(&req->r_req_lru_item);
396 INIT_LIST_HEAD(&req->r_osd_item);
397
398 req->r_base_oloc.pool = -1;
399 req->r_target_oloc.pool = -1;
400
401 /* create reply message */
402 if (use_mempool)
403 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
404 else
405 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
406 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
407 if (!msg) {
408 ceph_osdc_put_request(req);
409 return NULL;
410 }
411 req->r_reply = msg;
412
413 /* create request message; allow space for oid */
414 if (use_mempool)
415 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
416 else
417 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
418 if (!msg) {
419 ceph_osdc_put_request(req);
420 return NULL;
421 }
422
423 memset(msg->front.iov_base, 0, msg->front.iov_len);
424
425 req->r_request = msg;
426
427 return req;
428 }
429 EXPORT_SYMBOL(ceph_osdc_alloc_request);
430
osd_req_opcode_valid(u16 opcode)431 static bool osd_req_opcode_valid(u16 opcode)
432 {
433 switch (opcode) {
434 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
435 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
436 #undef GENERATE_CASE
437 default:
438 return false;
439 }
440 }
441
442 /*
443 * This is an osd op init function for opcodes that have no data or
444 * other information associated with them. It also serves as a
445 * common init routine for all the other init functions, below.
446 */
447 static struct ceph_osd_req_op *
_osd_req_op_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode)448 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
449 u16 opcode)
450 {
451 struct ceph_osd_req_op *op;
452
453 BUG_ON(which >= osd_req->r_num_ops);
454 BUG_ON(!osd_req_opcode_valid(opcode));
455
456 op = &osd_req->r_ops[which];
457 memset(op, 0, sizeof (*op));
458 op->op = opcode;
459
460 return op;
461 }
462
osd_req_op_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode)463 void osd_req_op_init(struct ceph_osd_request *osd_req,
464 unsigned int which, u16 opcode)
465 {
466 (void)_osd_req_op_init(osd_req, which, opcode);
467 }
468 EXPORT_SYMBOL(osd_req_op_init);
469
osd_req_op_extent_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode,u64 offset,u64 length,u64 truncate_size,u32 truncate_seq)470 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
471 unsigned int which, u16 opcode,
472 u64 offset, u64 length,
473 u64 truncate_size, u32 truncate_seq)
474 {
475 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
476 size_t payload_len = 0;
477
478 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
479 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
480 opcode != CEPH_OSD_OP_TRUNCATE);
481
482 op->extent.offset = offset;
483 op->extent.length = length;
484 op->extent.truncate_size = truncate_size;
485 op->extent.truncate_seq = truncate_seq;
486 if (opcode == CEPH_OSD_OP_WRITE)
487 payload_len += length;
488
489 op->payload_len = payload_len;
490 }
491 EXPORT_SYMBOL(osd_req_op_extent_init);
492
osd_req_op_extent_update(struct ceph_osd_request * osd_req,unsigned int which,u64 length)493 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
494 unsigned int which, u64 length)
495 {
496 struct ceph_osd_req_op *op;
497 u64 previous;
498
499 BUG_ON(which >= osd_req->r_num_ops);
500 op = &osd_req->r_ops[which];
501 previous = op->extent.length;
502
503 if (length == previous)
504 return; /* Nothing to do */
505 BUG_ON(length > previous);
506
507 op->extent.length = length;
508 op->payload_len -= previous - length;
509 }
510 EXPORT_SYMBOL(osd_req_op_extent_update);
511
osd_req_op_cls_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode,const char * class,const char * method)512 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
513 u16 opcode, const char *class, const char *method)
514 {
515 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
516 struct ceph_pagelist *pagelist;
517 size_t payload_len = 0;
518 size_t size;
519
520 BUG_ON(opcode != CEPH_OSD_OP_CALL);
521
522 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
523 BUG_ON(!pagelist);
524 ceph_pagelist_init(pagelist);
525
526 op->cls.class_name = class;
527 size = strlen(class);
528 BUG_ON(size > (size_t) U8_MAX);
529 op->cls.class_len = size;
530 ceph_pagelist_append(pagelist, class, size);
531 payload_len += size;
532
533 op->cls.method_name = method;
534 size = strlen(method);
535 BUG_ON(size > (size_t) U8_MAX);
536 op->cls.method_len = size;
537 ceph_pagelist_append(pagelist, method, size);
538 payload_len += size;
539
540 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
541
542 op->cls.argc = 0; /* currently unused */
543
544 op->payload_len = payload_len;
545 }
546 EXPORT_SYMBOL(osd_req_op_cls_init);
547
osd_req_op_watch_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode,u64 cookie,u64 version,int flag)548 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
549 unsigned int which, u16 opcode,
550 u64 cookie, u64 version, int flag)
551 {
552 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
553
554 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
555
556 op->watch.cookie = cookie;
557 op->watch.ver = version;
558 if (opcode == CEPH_OSD_OP_WATCH && flag)
559 op->watch.flag = (u8)1;
560 }
561 EXPORT_SYMBOL(osd_req_op_watch_init);
562
osd_req_op_alloc_hint_init(struct ceph_osd_request * osd_req,unsigned int which,u64 expected_object_size,u64 expected_write_size)563 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
564 unsigned int which,
565 u64 expected_object_size,
566 u64 expected_write_size)
567 {
568 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
569 CEPH_OSD_OP_SETALLOCHINT);
570
571 op->alloc_hint.expected_object_size = expected_object_size;
572 op->alloc_hint.expected_write_size = expected_write_size;
573
574 /*
575 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
576 * not worth a feature bit. Set FAILOK per-op flag to make
577 * sure older osds don't trip over an unsupported opcode.
578 */
579 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
580 }
581 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
582
ceph_osdc_msg_data_add(struct ceph_msg * msg,struct ceph_osd_data * osd_data)583 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
584 struct ceph_osd_data *osd_data)
585 {
586 u64 length = ceph_osd_data_length(osd_data);
587
588 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
589 BUG_ON(length > (u64) SIZE_MAX);
590 if (length)
591 ceph_msg_data_add_pages(msg, osd_data->pages,
592 length, osd_data->alignment);
593 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
594 BUG_ON(!length);
595 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
596 #ifdef CONFIG_BLOCK
597 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
598 ceph_msg_data_add_bio(msg, osd_data->bio, length);
599 #endif
600 } else {
601 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
602 }
603 }
604
osd_req_encode_op(struct ceph_osd_request * req,struct ceph_osd_op * dst,unsigned int which)605 static u64 osd_req_encode_op(struct ceph_osd_request *req,
606 struct ceph_osd_op *dst, unsigned int which)
607 {
608 struct ceph_osd_req_op *src;
609 struct ceph_osd_data *osd_data;
610 u64 request_data_len = 0;
611 u64 data_length;
612
613 BUG_ON(which >= req->r_num_ops);
614 src = &req->r_ops[which];
615 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
616 pr_err("unrecognized osd opcode %d\n", src->op);
617
618 return 0;
619 }
620
621 switch (src->op) {
622 case CEPH_OSD_OP_STAT:
623 osd_data = &src->raw_data_in;
624 ceph_osdc_msg_data_add(req->r_reply, osd_data);
625 break;
626 case CEPH_OSD_OP_READ:
627 case CEPH_OSD_OP_WRITE:
628 case CEPH_OSD_OP_ZERO:
629 case CEPH_OSD_OP_DELETE:
630 case CEPH_OSD_OP_TRUNCATE:
631 if (src->op == CEPH_OSD_OP_WRITE)
632 request_data_len = src->extent.length;
633 dst->extent.offset = cpu_to_le64(src->extent.offset);
634 dst->extent.length = cpu_to_le64(src->extent.length);
635 dst->extent.truncate_size =
636 cpu_to_le64(src->extent.truncate_size);
637 dst->extent.truncate_seq =
638 cpu_to_le32(src->extent.truncate_seq);
639 osd_data = &src->extent.osd_data;
640 if (src->op == CEPH_OSD_OP_WRITE)
641 ceph_osdc_msg_data_add(req->r_request, osd_data);
642 else
643 ceph_osdc_msg_data_add(req->r_reply, osd_data);
644 break;
645 case CEPH_OSD_OP_CALL:
646 dst->cls.class_len = src->cls.class_len;
647 dst->cls.method_len = src->cls.method_len;
648 osd_data = &src->cls.request_info;
649 ceph_osdc_msg_data_add(req->r_request, osd_data);
650 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
651 request_data_len = osd_data->pagelist->length;
652
653 osd_data = &src->cls.request_data;
654 data_length = ceph_osd_data_length(osd_data);
655 if (data_length) {
656 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
657 dst->cls.indata_len = cpu_to_le32(data_length);
658 ceph_osdc_msg_data_add(req->r_request, osd_data);
659 src->payload_len += data_length;
660 request_data_len += data_length;
661 }
662 osd_data = &src->cls.response_data;
663 ceph_osdc_msg_data_add(req->r_reply, osd_data);
664 break;
665 case CEPH_OSD_OP_STARTSYNC:
666 break;
667 case CEPH_OSD_OP_NOTIFY_ACK:
668 case CEPH_OSD_OP_WATCH:
669 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
670 dst->watch.ver = cpu_to_le64(src->watch.ver);
671 dst->watch.flag = src->watch.flag;
672 break;
673 case CEPH_OSD_OP_SETALLOCHINT:
674 dst->alloc_hint.expected_object_size =
675 cpu_to_le64(src->alloc_hint.expected_object_size);
676 dst->alloc_hint.expected_write_size =
677 cpu_to_le64(src->alloc_hint.expected_write_size);
678 break;
679 default:
680 pr_err("unsupported osd opcode %s\n",
681 ceph_osd_op_name(src->op));
682 WARN_ON(1);
683
684 return 0;
685 }
686
687 dst->op = cpu_to_le16(src->op);
688 dst->flags = cpu_to_le32(src->flags);
689 dst->payload_len = cpu_to_le32(src->payload_len);
690
691 return request_data_len;
692 }
693
694 /*
695 * build new request AND message, calculate layout, and adjust file
696 * extent as needed.
697 *
698 * if the file was recently truncated, we include information about its
699 * old and new size so that the object can be updated appropriately. (we
700 * avoid synchronously deleting truncated objects because it's slow.)
701 *
702 * if @do_sync, include a 'startsync' command so that the osd will flush
703 * data quickly.
704 */
ceph_osdc_new_request(struct ceph_osd_client * osdc,struct ceph_file_layout * layout,struct ceph_vino vino,u64 off,u64 * plen,int num_ops,int opcode,int flags,struct ceph_snap_context * snapc,u32 truncate_seq,u64 truncate_size,bool use_mempool)705 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
706 struct ceph_file_layout *layout,
707 struct ceph_vino vino,
708 u64 off, u64 *plen, int num_ops,
709 int opcode, int flags,
710 struct ceph_snap_context *snapc,
711 u32 truncate_seq,
712 u64 truncate_size,
713 bool use_mempool)
714 {
715 struct ceph_osd_request *req;
716 u64 objnum = 0;
717 u64 objoff = 0;
718 u64 objlen = 0;
719 u32 object_size;
720 u64 object_base;
721 int r;
722
723 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
724 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
725 opcode != CEPH_OSD_OP_TRUNCATE);
726
727 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
728 GFP_NOFS);
729 if (!req)
730 return ERR_PTR(-ENOMEM);
731
732 req->r_flags = flags;
733
734 /* calculate max write size */
735 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
736 if (r < 0) {
737 ceph_osdc_put_request(req);
738 return ERR_PTR(r);
739 }
740
741 object_size = le32_to_cpu(layout->fl_object_size);
742 object_base = off - objoff;
743 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
744 if (truncate_size <= object_base) {
745 truncate_size = 0;
746 } else {
747 truncate_size -= object_base;
748 if (truncate_size > object_size)
749 truncate_size = object_size;
750 }
751 }
752
753 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
754 truncate_size, truncate_seq);
755
756 /*
757 * A second op in the ops array means the caller wants to
758 * also issue a include a 'startsync' command so that the
759 * osd will flush data quickly.
760 */
761 if (num_ops > 1)
762 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
763
764 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
765
766 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
767 "%llx.%08llx", vino.ino, objnum);
768 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
769
770 return req;
771 }
772 EXPORT_SYMBOL(ceph_osdc_new_request);
773
774 /*
775 * We keep osd requests in an rbtree, sorted by ->r_tid.
776 */
__insert_request(struct ceph_osd_client * osdc,struct ceph_osd_request * new)777 static void __insert_request(struct ceph_osd_client *osdc,
778 struct ceph_osd_request *new)
779 {
780 struct rb_node **p = &osdc->requests.rb_node;
781 struct rb_node *parent = NULL;
782 struct ceph_osd_request *req = NULL;
783
784 while (*p) {
785 parent = *p;
786 req = rb_entry(parent, struct ceph_osd_request, r_node);
787 if (new->r_tid < req->r_tid)
788 p = &(*p)->rb_left;
789 else if (new->r_tid > req->r_tid)
790 p = &(*p)->rb_right;
791 else
792 BUG();
793 }
794
795 rb_link_node(&new->r_node, parent, p);
796 rb_insert_color(&new->r_node, &osdc->requests);
797 }
798
__lookup_request(struct ceph_osd_client * osdc,u64 tid)799 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
800 u64 tid)
801 {
802 struct ceph_osd_request *req;
803 struct rb_node *n = osdc->requests.rb_node;
804
805 while (n) {
806 req = rb_entry(n, struct ceph_osd_request, r_node);
807 if (tid < req->r_tid)
808 n = n->rb_left;
809 else if (tid > req->r_tid)
810 n = n->rb_right;
811 else
812 return req;
813 }
814 return NULL;
815 }
816
817 static struct ceph_osd_request *
__lookup_request_ge(struct ceph_osd_client * osdc,u64 tid)818 __lookup_request_ge(struct ceph_osd_client *osdc,
819 u64 tid)
820 {
821 struct ceph_osd_request *req;
822 struct rb_node *n = osdc->requests.rb_node;
823
824 while (n) {
825 req = rb_entry(n, struct ceph_osd_request, r_node);
826 if (tid < req->r_tid) {
827 if (!n->rb_left)
828 return req;
829 n = n->rb_left;
830 } else if (tid > req->r_tid) {
831 n = n->rb_right;
832 } else {
833 return req;
834 }
835 }
836 return NULL;
837 }
838
__kick_linger_request(struct ceph_osd_request * req)839 static void __kick_linger_request(struct ceph_osd_request *req)
840 {
841 struct ceph_osd_client *osdc = req->r_osdc;
842 struct ceph_osd *osd = req->r_osd;
843
844 /*
845 * Linger requests need to be resent with a new tid to avoid
846 * the dup op detection logic on the OSDs. Achieve this with
847 * a re-register dance instead of open-coding.
848 */
849 ceph_osdc_get_request(req);
850 if (!list_empty(&req->r_linger_item))
851 __unregister_linger_request(osdc, req);
852 else
853 __unregister_request(osdc, req);
854 __register_request(osdc, req);
855 ceph_osdc_put_request(req);
856
857 /*
858 * Unless request has been registered as both normal and
859 * lingering, __unregister{,_linger}_request clears r_osd.
860 * However, here we need to preserve r_osd to make sure we
861 * requeue on the same OSD.
862 */
863 WARN_ON(req->r_osd || !osd);
864 req->r_osd = osd;
865
866 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
867 __enqueue_request(req);
868 }
869
870 /*
871 * Resubmit requests pending on the given osd.
872 */
__kick_osd_requests(struct ceph_osd_client * osdc,struct ceph_osd * osd)873 static void __kick_osd_requests(struct ceph_osd_client *osdc,
874 struct ceph_osd *osd)
875 {
876 struct ceph_osd_request *req, *nreq;
877 LIST_HEAD(resend);
878 LIST_HEAD(resend_linger);
879 int err;
880
881 dout("%s osd%d\n", __func__, osd->o_osd);
882 err = __reset_osd(osdc, osd);
883 if (err)
884 return;
885
886 /*
887 * Build up a list of requests to resend by traversing the
888 * osd's list of requests. Requests for a given object are
889 * sent in tid order, and that is also the order they're
890 * kept on this list. Therefore all requests that are in
891 * flight will be found first, followed by all requests that
892 * have not yet been sent. And to resend requests while
893 * preserving this order we will want to put any sent
894 * requests back on the front of the osd client's unsent
895 * list.
896 *
897 * So we build a separate ordered list of already-sent
898 * requests for the affected osd and splice it onto the
899 * front of the osd client's unsent list. Once we've seen a
900 * request that has not yet been sent we're done. Those
901 * requests are already sitting right where they belong.
902 */
903 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
904 if (!req->r_sent)
905 break;
906
907 if (!req->r_linger) {
908 dout("%s requeueing %p tid %llu\n", __func__, req,
909 req->r_tid);
910 list_move_tail(&req->r_req_lru_item, &resend);
911 req->r_flags |= CEPH_OSD_FLAG_RETRY;
912 } else {
913 list_move_tail(&req->r_req_lru_item, &resend_linger);
914 }
915 }
916 list_splice(&resend, &osdc->req_unsent);
917
918 /*
919 * Both registered and not yet registered linger requests are
920 * enqueued with a new tid on the same OSD. We add/move them
921 * to req_unsent/o_requests at the end to keep things in tid
922 * order.
923 */
924 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
925 r_linger_osd_item) {
926 WARN_ON(!list_empty(&req->r_req_lru_item));
927 __kick_linger_request(req);
928 }
929
930 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
931 __kick_linger_request(req);
932 }
933
934 /*
935 * If the osd connection drops, we need to resubmit all requests.
936 */
osd_reset(struct ceph_connection * con)937 static void osd_reset(struct ceph_connection *con)
938 {
939 struct ceph_osd *osd = con->private;
940 struct ceph_osd_client *osdc;
941
942 if (!osd)
943 return;
944 dout("osd_reset osd%d\n", osd->o_osd);
945 osdc = osd->o_osdc;
946 down_read(&osdc->map_sem);
947 mutex_lock(&osdc->request_mutex);
948 __kick_osd_requests(osdc, osd);
949 __send_queued(osdc);
950 mutex_unlock(&osdc->request_mutex);
951 up_read(&osdc->map_sem);
952 }
953
954 /*
955 * Track open sessions with osds.
956 */
create_osd(struct ceph_osd_client * osdc,int onum)957 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
958 {
959 struct ceph_osd *osd;
960
961 osd = kzalloc(sizeof(*osd), GFP_NOFS);
962 if (!osd)
963 return NULL;
964
965 atomic_set(&osd->o_ref, 1);
966 osd->o_osdc = osdc;
967 osd->o_osd = onum;
968 RB_CLEAR_NODE(&osd->o_node);
969 INIT_LIST_HEAD(&osd->o_requests);
970 INIT_LIST_HEAD(&osd->o_linger_requests);
971 INIT_LIST_HEAD(&osd->o_osd_lru);
972 osd->o_incarnation = 1;
973
974 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
975
976 INIT_LIST_HEAD(&osd->o_keepalive_item);
977 return osd;
978 }
979
get_osd(struct ceph_osd * osd)980 static struct ceph_osd *get_osd(struct ceph_osd *osd)
981 {
982 if (atomic_inc_not_zero(&osd->o_ref)) {
983 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
984 atomic_read(&osd->o_ref));
985 return osd;
986 } else {
987 dout("get_osd %p FAIL\n", osd);
988 return NULL;
989 }
990 }
991
put_osd(struct ceph_osd * osd)992 static void put_osd(struct ceph_osd *osd)
993 {
994 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
995 atomic_read(&osd->o_ref) - 1);
996 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
997 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
998
999 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
1000 kfree(osd);
1001 }
1002 }
1003
1004 /*
1005 * remove an osd from our map
1006 */
__remove_osd(struct ceph_osd_client * osdc,struct ceph_osd * osd)1007 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1008 {
1009 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1010 WARN_ON(!list_empty(&osd->o_requests));
1011 WARN_ON(!list_empty(&osd->o_linger_requests));
1012
1013 list_del_init(&osd->o_osd_lru);
1014 rb_erase(&osd->o_node, &osdc->osds);
1015 RB_CLEAR_NODE(&osd->o_node);
1016 }
1017
remove_osd(struct ceph_osd_client * osdc,struct ceph_osd * osd)1018 static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1019 {
1020 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1021
1022 if (!RB_EMPTY_NODE(&osd->o_node)) {
1023 ceph_con_close(&osd->o_con);
1024 __remove_osd(osdc, osd);
1025 put_osd(osd);
1026 }
1027 }
1028
remove_all_osds(struct ceph_osd_client * osdc)1029 static void remove_all_osds(struct ceph_osd_client *osdc)
1030 {
1031 dout("%s %p\n", __func__, osdc);
1032 mutex_lock(&osdc->request_mutex);
1033 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1034 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1035 struct ceph_osd, o_node);
1036 remove_osd(osdc, osd);
1037 }
1038 mutex_unlock(&osdc->request_mutex);
1039 }
1040
__move_osd_to_lru(struct ceph_osd_client * osdc,struct ceph_osd * osd)1041 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1042 struct ceph_osd *osd)
1043 {
1044 dout("%s %p\n", __func__, osd);
1045 BUG_ON(!list_empty(&osd->o_osd_lru));
1046
1047 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1048 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
1049 }
1050
maybe_move_osd_to_lru(struct ceph_osd_client * osdc,struct ceph_osd * osd)1051 static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1052 struct ceph_osd *osd)
1053 {
1054 dout("%s %p\n", __func__, osd);
1055
1056 if (list_empty(&osd->o_requests) &&
1057 list_empty(&osd->o_linger_requests))
1058 __move_osd_to_lru(osdc, osd);
1059 }
1060
__remove_osd_from_lru(struct ceph_osd * osd)1061 static void __remove_osd_from_lru(struct ceph_osd *osd)
1062 {
1063 dout("__remove_osd_from_lru %p\n", osd);
1064 if (!list_empty(&osd->o_osd_lru))
1065 list_del_init(&osd->o_osd_lru);
1066 }
1067
remove_old_osds(struct ceph_osd_client * osdc)1068 static void remove_old_osds(struct ceph_osd_client *osdc)
1069 {
1070 struct ceph_osd *osd, *nosd;
1071
1072 dout("__remove_old_osds %p\n", osdc);
1073 mutex_lock(&osdc->request_mutex);
1074 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1075 if (time_before(jiffies, osd->lru_ttl))
1076 break;
1077 remove_osd(osdc, osd);
1078 }
1079 mutex_unlock(&osdc->request_mutex);
1080 }
1081
1082 /*
1083 * reset osd connect
1084 */
__reset_osd(struct ceph_osd_client * osdc,struct ceph_osd * osd)1085 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1086 {
1087 struct ceph_entity_addr *peer_addr;
1088
1089 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1090 if (list_empty(&osd->o_requests) &&
1091 list_empty(&osd->o_linger_requests)) {
1092 remove_osd(osdc, osd);
1093 return -ENODEV;
1094 }
1095
1096 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1097 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1098 !ceph_con_opened(&osd->o_con)) {
1099 struct ceph_osd_request *req;
1100
1101 dout("osd addr hasn't changed and connection never opened, "
1102 "letting msgr retry\n");
1103 /* touch each r_stamp for handle_timeout()'s benfit */
1104 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1105 req->r_stamp = jiffies;
1106
1107 return -EAGAIN;
1108 }
1109
1110 ceph_con_close(&osd->o_con);
1111 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1112 osd->o_incarnation++;
1113
1114 return 0;
1115 }
1116
__insert_osd(struct ceph_osd_client * osdc,struct ceph_osd * new)1117 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1118 {
1119 struct rb_node **p = &osdc->osds.rb_node;
1120 struct rb_node *parent = NULL;
1121 struct ceph_osd *osd = NULL;
1122
1123 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1124 while (*p) {
1125 parent = *p;
1126 osd = rb_entry(parent, struct ceph_osd, o_node);
1127 if (new->o_osd < osd->o_osd)
1128 p = &(*p)->rb_left;
1129 else if (new->o_osd > osd->o_osd)
1130 p = &(*p)->rb_right;
1131 else
1132 BUG();
1133 }
1134
1135 rb_link_node(&new->o_node, parent, p);
1136 rb_insert_color(&new->o_node, &osdc->osds);
1137 }
1138
__lookup_osd(struct ceph_osd_client * osdc,int o)1139 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1140 {
1141 struct ceph_osd *osd;
1142 struct rb_node *n = osdc->osds.rb_node;
1143
1144 while (n) {
1145 osd = rb_entry(n, struct ceph_osd, o_node);
1146 if (o < osd->o_osd)
1147 n = n->rb_left;
1148 else if (o > osd->o_osd)
1149 n = n->rb_right;
1150 else
1151 return osd;
1152 }
1153 return NULL;
1154 }
1155
__schedule_osd_timeout(struct ceph_osd_client * osdc)1156 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1157 {
1158 schedule_delayed_work(&osdc->timeout_work,
1159 osdc->client->options->osd_keepalive_timeout * HZ);
1160 }
1161
__cancel_osd_timeout(struct ceph_osd_client * osdc)1162 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1163 {
1164 cancel_delayed_work(&osdc->timeout_work);
1165 }
1166
1167 /*
1168 * Register request, assign tid. If this is the first request, set up
1169 * the timeout event.
1170 */
__register_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1171 static void __register_request(struct ceph_osd_client *osdc,
1172 struct ceph_osd_request *req)
1173 {
1174 req->r_tid = ++osdc->last_tid;
1175 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1176 dout("__register_request %p tid %lld\n", req, req->r_tid);
1177 __insert_request(osdc, req);
1178 ceph_osdc_get_request(req);
1179 osdc->num_requests++;
1180 if (osdc->num_requests == 1) {
1181 dout(" first request, scheduling timeout\n");
1182 __schedule_osd_timeout(osdc);
1183 }
1184 }
1185
1186 /*
1187 * called under osdc->request_mutex
1188 */
__unregister_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1189 static void __unregister_request(struct ceph_osd_client *osdc,
1190 struct ceph_osd_request *req)
1191 {
1192 if (RB_EMPTY_NODE(&req->r_node)) {
1193 dout("__unregister_request %p tid %lld not registered\n",
1194 req, req->r_tid);
1195 return;
1196 }
1197
1198 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1199 rb_erase(&req->r_node, &osdc->requests);
1200 RB_CLEAR_NODE(&req->r_node);
1201 osdc->num_requests--;
1202
1203 if (req->r_osd) {
1204 /* make sure the original request isn't in flight. */
1205 ceph_msg_revoke(req->r_request);
1206
1207 list_del_init(&req->r_osd_item);
1208 maybe_move_osd_to_lru(osdc, req->r_osd);
1209 if (list_empty(&req->r_linger_osd_item))
1210 req->r_osd = NULL;
1211 }
1212
1213 list_del_init(&req->r_req_lru_item);
1214 ceph_osdc_put_request(req);
1215
1216 if (osdc->num_requests == 0) {
1217 dout(" no requests, canceling timeout\n");
1218 __cancel_osd_timeout(osdc);
1219 }
1220 }
1221
1222 /*
1223 * Cancel a previously queued request message
1224 */
__cancel_request(struct ceph_osd_request * req)1225 static void __cancel_request(struct ceph_osd_request *req)
1226 {
1227 if (req->r_sent && req->r_osd) {
1228 ceph_msg_revoke(req->r_request);
1229 req->r_sent = 0;
1230 }
1231 }
1232
__register_linger_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1233 static void __register_linger_request(struct ceph_osd_client *osdc,
1234 struct ceph_osd_request *req)
1235 {
1236 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1237 WARN_ON(!req->r_linger);
1238
1239 ceph_osdc_get_request(req);
1240 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1241 if (req->r_osd)
1242 list_add_tail(&req->r_linger_osd_item,
1243 &req->r_osd->o_linger_requests);
1244 }
1245
__unregister_linger_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1246 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1247 struct ceph_osd_request *req)
1248 {
1249 WARN_ON(!req->r_linger);
1250
1251 if (list_empty(&req->r_linger_item)) {
1252 dout("%s %p tid %llu not registered\n", __func__, req,
1253 req->r_tid);
1254 return;
1255 }
1256
1257 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1258 list_del_init(&req->r_linger_item);
1259
1260 if (req->r_osd) {
1261 list_del_init(&req->r_linger_osd_item);
1262 maybe_move_osd_to_lru(osdc, req->r_osd);
1263 if (list_empty(&req->r_osd_item))
1264 req->r_osd = NULL;
1265 }
1266 ceph_osdc_put_request(req);
1267 }
1268
ceph_osdc_set_request_linger(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1269 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1270 struct ceph_osd_request *req)
1271 {
1272 if (!req->r_linger) {
1273 dout("set_request_linger %p\n", req);
1274 req->r_linger = 1;
1275 }
1276 }
1277 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1278
1279 /*
1280 * Returns whether a request should be blocked from being sent
1281 * based on the current osdmap and osd_client settings.
1282 *
1283 * Caller should hold map_sem for read.
1284 */
__req_should_be_paused(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1285 static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1286 struct ceph_osd_request *req)
1287 {
1288 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1289 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1290 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1291 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1292 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1293 }
1294
1295 /*
1296 * Calculate mapping of a request to a PG. Takes tiering into account.
1297 */
__calc_request_pg(struct ceph_osdmap * osdmap,struct ceph_osd_request * req,struct ceph_pg * pg_out)1298 static int __calc_request_pg(struct ceph_osdmap *osdmap,
1299 struct ceph_osd_request *req,
1300 struct ceph_pg *pg_out)
1301 {
1302 bool need_check_tiering;
1303
1304 need_check_tiering = false;
1305 if (req->r_target_oloc.pool == -1) {
1306 req->r_target_oloc = req->r_base_oloc; /* struct */
1307 need_check_tiering = true;
1308 }
1309 if (req->r_target_oid.name_len == 0) {
1310 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1311 need_check_tiering = true;
1312 }
1313
1314 if (need_check_tiering &&
1315 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1316 struct ceph_pg_pool_info *pi;
1317
1318 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
1319 if (pi) {
1320 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1321 pi->read_tier >= 0)
1322 req->r_target_oloc.pool = pi->read_tier;
1323 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1324 pi->write_tier >= 0)
1325 req->r_target_oloc.pool = pi->write_tier;
1326 }
1327 /* !pi is caught in ceph_oloc_oid_to_pg() */
1328 }
1329
1330 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1331 &req->r_target_oid, pg_out);
1332 }
1333
__enqueue_request(struct ceph_osd_request * req)1334 static void __enqueue_request(struct ceph_osd_request *req)
1335 {
1336 struct ceph_osd_client *osdc = req->r_osdc;
1337
1338 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1339 req->r_osd ? req->r_osd->o_osd : -1);
1340
1341 if (req->r_osd) {
1342 __remove_osd_from_lru(req->r_osd);
1343 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1344 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1345 } else {
1346 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1347 }
1348 }
1349
1350 /*
1351 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1352 * (as needed), and set the request r_osd appropriately. If there is
1353 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1354 * (unsent, homeless) or leave on in-flight lru.
1355 *
1356 * Return 0 if unchanged, 1 if changed, or negative on error.
1357 *
1358 * Caller should hold map_sem for read and request_mutex.
1359 */
__map_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req,int force_resend)1360 static int __map_request(struct ceph_osd_client *osdc,
1361 struct ceph_osd_request *req, int force_resend)
1362 {
1363 struct ceph_pg pgid;
1364 int acting[CEPH_PG_MAX_SIZE];
1365 int num, o;
1366 int err;
1367 bool was_paused;
1368
1369 dout("map_request %p tid %lld\n", req, req->r_tid);
1370
1371 err = __calc_request_pg(osdc->osdmap, req, &pgid);
1372 if (err) {
1373 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1374 return err;
1375 }
1376 req->r_pgid = pgid;
1377
1378 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1379 if (num < 0)
1380 num = 0;
1381
1382 was_paused = req->r_paused;
1383 req->r_paused = __req_should_be_paused(osdc, req);
1384 if (was_paused && !req->r_paused)
1385 force_resend = 1;
1386
1387 if ((!force_resend &&
1388 req->r_osd && req->r_osd->o_osd == o &&
1389 req->r_sent >= req->r_osd->o_incarnation &&
1390 req->r_num_pg_osds == num &&
1391 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1392 (req->r_osd == NULL && o == -1) ||
1393 req->r_paused)
1394 return 0; /* no change */
1395
1396 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1397 req->r_tid, pgid.pool, pgid.seed, o,
1398 req->r_osd ? req->r_osd->o_osd : -1);
1399
1400 /* record full pg acting set */
1401 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1402 req->r_num_pg_osds = num;
1403
1404 if (req->r_osd) {
1405 __cancel_request(req);
1406 list_del_init(&req->r_osd_item);
1407 list_del_init(&req->r_linger_osd_item);
1408 req->r_osd = NULL;
1409 }
1410
1411 req->r_osd = __lookup_osd(osdc, o);
1412 if (!req->r_osd && o >= 0) {
1413 err = -ENOMEM;
1414 req->r_osd = create_osd(osdc, o);
1415 if (!req->r_osd) {
1416 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1417 goto out;
1418 }
1419
1420 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1421 __insert_osd(osdc, req->r_osd);
1422
1423 ceph_con_open(&req->r_osd->o_con,
1424 CEPH_ENTITY_TYPE_OSD, o,
1425 &osdc->osdmap->osd_addr[o]);
1426 }
1427
1428 __enqueue_request(req);
1429 err = 1; /* osd or pg changed */
1430
1431 out:
1432 return err;
1433 }
1434
1435 /*
1436 * caller should hold map_sem (for read) and request_mutex
1437 */
__send_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1438 static void __send_request(struct ceph_osd_client *osdc,
1439 struct ceph_osd_request *req)
1440 {
1441 void *p;
1442
1443 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1444 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1445 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1446
1447 /* fill in message content that changes each time we send it */
1448 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1449 put_unaligned_le32(req->r_flags, req->r_request_flags);
1450 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
1451 p = req->r_request_pgid;
1452 ceph_encode_64(&p, req->r_pgid.pool);
1453 ceph_encode_32(&p, req->r_pgid.seed);
1454 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1455 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1456 sizeof(req->r_reassert_version));
1457
1458 req->r_stamp = jiffies;
1459 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1460
1461 ceph_msg_get(req->r_request); /* send consumes a ref */
1462
1463 req->r_sent = req->r_osd->o_incarnation;
1464
1465 ceph_con_send(&req->r_osd->o_con, req->r_request);
1466 }
1467
1468 /*
1469 * Send any requests in the queue (req_unsent).
1470 */
__send_queued(struct ceph_osd_client * osdc)1471 static void __send_queued(struct ceph_osd_client *osdc)
1472 {
1473 struct ceph_osd_request *req, *tmp;
1474
1475 dout("__send_queued\n");
1476 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1477 __send_request(osdc, req);
1478 }
1479
1480 /*
1481 * Caller should hold map_sem for read and request_mutex.
1482 */
__ceph_osdc_start_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req,bool nofail)1483 static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1484 struct ceph_osd_request *req,
1485 bool nofail)
1486 {
1487 int rc;
1488
1489 __register_request(osdc, req);
1490 req->r_sent = 0;
1491 req->r_got_reply = 0;
1492 rc = __map_request(osdc, req, 0);
1493 if (rc < 0) {
1494 if (nofail) {
1495 dout("osdc_start_request failed map, "
1496 " will retry %lld\n", req->r_tid);
1497 rc = 0;
1498 } else {
1499 __unregister_request(osdc, req);
1500 }
1501 return rc;
1502 }
1503
1504 if (req->r_osd == NULL) {
1505 dout("send_request %p no up osds in pg\n", req);
1506 ceph_monc_request_next_osdmap(&osdc->client->monc);
1507 } else {
1508 __send_queued(osdc);
1509 }
1510
1511 return 0;
1512 }
1513
1514 /*
1515 * Timeout callback, called every N seconds when 1 or more osd
1516 * requests has been active for more than N seconds. When this
1517 * happens, we ping all OSDs with requests who have timed out to
1518 * ensure any communications channel reset is detected. Reset the
1519 * request timeouts another N seconds in the future as we go.
1520 * Reschedule the timeout event another N seconds in future (unless
1521 * there are no open requests).
1522 */
handle_timeout(struct work_struct * work)1523 static void handle_timeout(struct work_struct *work)
1524 {
1525 struct ceph_osd_client *osdc =
1526 container_of(work, struct ceph_osd_client, timeout_work.work);
1527 struct ceph_osd_request *req;
1528 struct ceph_osd *osd;
1529 unsigned long keepalive =
1530 osdc->client->options->osd_keepalive_timeout * HZ;
1531 struct list_head slow_osds;
1532 dout("timeout\n");
1533 down_read(&osdc->map_sem);
1534
1535 ceph_monc_request_next_osdmap(&osdc->client->monc);
1536
1537 mutex_lock(&osdc->request_mutex);
1538
1539 /*
1540 * ping osds that are a bit slow. this ensures that if there
1541 * is a break in the TCP connection we will notice, and reopen
1542 * a connection with that osd (from the fault callback).
1543 */
1544 INIT_LIST_HEAD(&slow_osds);
1545 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1546 if (time_before(jiffies, req->r_stamp + keepalive))
1547 break;
1548
1549 osd = req->r_osd;
1550 BUG_ON(!osd);
1551 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1552 req->r_tid, osd->o_osd);
1553 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1554 }
1555 while (!list_empty(&slow_osds)) {
1556 osd = list_entry(slow_osds.next, struct ceph_osd,
1557 o_keepalive_item);
1558 list_del_init(&osd->o_keepalive_item);
1559 ceph_con_keepalive(&osd->o_con);
1560 }
1561
1562 __schedule_osd_timeout(osdc);
1563 __send_queued(osdc);
1564 mutex_unlock(&osdc->request_mutex);
1565 up_read(&osdc->map_sem);
1566 }
1567
handle_osds_timeout(struct work_struct * work)1568 static void handle_osds_timeout(struct work_struct *work)
1569 {
1570 struct ceph_osd_client *osdc =
1571 container_of(work, struct ceph_osd_client,
1572 osds_timeout_work.work);
1573 unsigned long delay =
1574 osdc->client->options->osd_idle_ttl * HZ >> 2;
1575
1576 dout("osds timeout\n");
1577 down_read(&osdc->map_sem);
1578 remove_old_osds(osdc);
1579 up_read(&osdc->map_sem);
1580
1581 schedule_delayed_work(&osdc->osds_timeout_work,
1582 round_jiffies_relative(delay));
1583 }
1584
ceph_oloc_decode(void ** p,void * end,struct ceph_object_locator * oloc)1585 static int ceph_oloc_decode(void **p, void *end,
1586 struct ceph_object_locator *oloc)
1587 {
1588 u8 struct_v, struct_cv;
1589 u32 len;
1590 void *struct_end;
1591 int ret = 0;
1592
1593 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1594 struct_v = ceph_decode_8(p);
1595 struct_cv = ceph_decode_8(p);
1596 if (struct_v < 3) {
1597 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1598 struct_v, struct_cv);
1599 goto e_inval;
1600 }
1601 if (struct_cv > 6) {
1602 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1603 struct_v, struct_cv);
1604 goto e_inval;
1605 }
1606 len = ceph_decode_32(p);
1607 ceph_decode_need(p, end, len, e_inval);
1608 struct_end = *p + len;
1609
1610 oloc->pool = ceph_decode_64(p);
1611 *p += 4; /* skip preferred */
1612
1613 len = ceph_decode_32(p);
1614 if (len > 0) {
1615 pr_warn("ceph_object_locator::key is set\n");
1616 goto e_inval;
1617 }
1618
1619 if (struct_v >= 5) {
1620 len = ceph_decode_32(p);
1621 if (len > 0) {
1622 pr_warn("ceph_object_locator::nspace is set\n");
1623 goto e_inval;
1624 }
1625 }
1626
1627 if (struct_v >= 6) {
1628 s64 hash = ceph_decode_64(p);
1629 if (hash != -1) {
1630 pr_warn("ceph_object_locator::hash is set\n");
1631 goto e_inval;
1632 }
1633 }
1634
1635 /* skip the rest */
1636 *p = struct_end;
1637 out:
1638 return ret;
1639
1640 e_inval:
1641 ret = -EINVAL;
1642 goto out;
1643 }
1644
ceph_redirect_decode(void ** p,void * end,struct ceph_request_redirect * redir)1645 static int ceph_redirect_decode(void **p, void *end,
1646 struct ceph_request_redirect *redir)
1647 {
1648 u8 struct_v, struct_cv;
1649 u32 len;
1650 void *struct_end;
1651 int ret;
1652
1653 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1654 struct_v = ceph_decode_8(p);
1655 struct_cv = ceph_decode_8(p);
1656 if (struct_cv > 1) {
1657 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1658 struct_v, struct_cv);
1659 goto e_inval;
1660 }
1661 len = ceph_decode_32(p);
1662 ceph_decode_need(p, end, len, e_inval);
1663 struct_end = *p + len;
1664
1665 ret = ceph_oloc_decode(p, end, &redir->oloc);
1666 if (ret)
1667 goto out;
1668
1669 len = ceph_decode_32(p);
1670 if (len > 0) {
1671 pr_warn("ceph_request_redirect::object_name is set\n");
1672 goto e_inval;
1673 }
1674
1675 len = ceph_decode_32(p);
1676 *p += len; /* skip osd_instructions */
1677
1678 /* skip the rest */
1679 *p = struct_end;
1680 out:
1681 return ret;
1682
1683 e_inval:
1684 ret = -EINVAL;
1685 goto out;
1686 }
1687
complete_request(struct ceph_osd_request * req)1688 static void complete_request(struct ceph_osd_request *req)
1689 {
1690 complete_all(&req->r_safe_completion); /* fsync waiter */
1691 }
1692
1693 /*
1694 * handle osd op reply. either call the callback if it is specified,
1695 * or do the completion to wake up the waiting thread.
1696 */
handle_reply(struct ceph_osd_client * osdc,struct ceph_msg * msg,struct ceph_connection * con)1697 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1698 struct ceph_connection *con)
1699 {
1700 void *p, *end;
1701 struct ceph_osd_request *req;
1702 struct ceph_request_redirect redir;
1703 u64 tid;
1704 int object_len;
1705 unsigned int numops;
1706 int payload_len, flags;
1707 s32 result;
1708 s32 retry_attempt;
1709 struct ceph_pg pg;
1710 int err;
1711 u32 reassert_epoch;
1712 u64 reassert_version;
1713 u32 osdmap_epoch;
1714 int already_completed;
1715 u32 bytes;
1716 unsigned int i;
1717
1718 tid = le64_to_cpu(msg->hdr.tid);
1719 dout("handle_reply %p tid %llu\n", msg, tid);
1720
1721 p = msg->front.iov_base;
1722 end = p + msg->front.iov_len;
1723
1724 ceph_decode_need(&p, end, 4, bad);
1725 object_len = ceph_decode_32(&p);
1726 ceph_decode_need(&p, end, object_len, bad);
1727 p += object_len;
1728
1729 err = ceph_decode_pgid(&p, end, &pg);
1730 if (err)
1731 goto bad;
1732
1733 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1734 flags = ceph_decode_64(&p);
1735 result = ceph_decode_32(&p);
1736 reassert_epoch = ceph_decode_32(&p);
1737 reassert_version = ceph_decode_64(&p);
1738 osdmap_epoch = ceph_decode_32(&p);
1739
1740 /* lookup */
1741 down_read(&osdc->map_sem);
1742 mutex_lock(&osdc->request_mutex);
1743 req = __lookup_request(osdc, tid);
1744 if (req == NULL) {
1745 dout("handle_reply tid %llu dne\n", tid);
1746 goto bad_mutex;
1747 }
1748 ceph_osdc_get_request(req);
1749
1750 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1751 req, result);
1752
1753 ceph_decode_need(&p, end, 4, bad_put);
1754 numops = ceph_decode_32(&p);
1755 if (numops > CEPH_OSD_MAX_OP)
1756 goto bad_put;
1757 if (numops != req->r_num_ops)
1758 goto bad_put;
1759 payload_len = 0;
1760 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
1761 for (i = 0; i < numops; i++) {
1762 struct ceph_osd_op *op = p;
1763 int len;
1764
1765 len = le32_to_cpu(op->payload_len);
1766 req->r_reply_op_len[i] = len;
1767 dout(" op %d has %d bytes\n", i, len);
1768 payload_len += len;
1769 p += sizeof(*op);
1770 }
1771 bytes = le32_to_cpu(msg->hdr.data_len);
1772 if (payload_len != bytes) {
1773 pr_warn("sum of op payload lens %d != data_len %d\n",
1774 payload_len, bytes);
1775 goto bad_put;
1776 }
1777
1778 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
1779 retry_attempt = ceph_decode_32(&p);
1780 for (i = 0; i < numops; i++)
1781 req->r_reply_op_result[i] = ceph_decode_32(&p);
1782
1783 if (le16_to_cpu(msg->hdr.version) >= 6) {
1784 p += 8 + 4; /* skip replay_version */
1785 p += 8; /* skip user_version */
1786
1787 err = ceph_redirect_decode(&p, end, &redir);
1788 if (err)
1789 goto bad_put;
1790 } else {
1791 redir.oloc.pool = -1;
1792 }
1793
1794 if (redir.oloc.pool != -1) {
1795 dout("redirect pool %lld\n", redir.oloc.pool);
1796
1797 __unregister_request(osdc, req);
1798
1799 req->r_target_oloc = redir.oloc; /* struct */
1800
1801 /*
1802 * Start redirect requests with nofail=true. If
1803 * mapping fails, request will end up on the notarget
1804 * list, waiting for the new osdmap (which can take
1805 * a while), even though the original request mapped
1806 * successfully. In the future we might want to follow
1807 * original request's nofail setting here.
1808 */
1809 err = __ceph_osdc_start_request(osdc, req, true);
1810 BUG_ON(err);
1811
1812 goto out_unlock;
1813 }
1814
1815 already_completed = req->r_got_reply;
1816 if (!req->r_got_reply) {
1817 req->r_result = result;
1818 dout("handle_reply result %d bytes %d\n", req->r_result,
1819 bytes);
1820 if (req->r_result == 0)
1821 req->r_result = bytes;
1822
1823 /* in case this is a write and we need to replay, */
1824 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1825 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1826
1827 req->r_got_reply = 1;
1828 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1829 dout("handle_reply tid %llu dup ack\n", tid);
1830 goto out_unlock;
1831 }
1832
1833 dout("handle_reply tid %llu flags %d\n", tid, flags);
1834
1835 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1836 __register_linger_request(osdc, req);
1837
1838 /* either this is a read, or we got the safe response */
1839 if (result < 0 ||
1840 (flags & CEPH_OSD_FLAG_ONDISK) ||
1841 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1842 __unregister_request(osdc, req);
1843
1844 mutex_unlock(&osdc->request_mutex);
1845 up_read(&osdc->map_sem);
1846
1847 if (!already_completed) {
1848 if (req->r_unsafe_callback &&
1849 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1850 req->r_unsafe_callback(req, true);
1851 if (req->r_callback)
1852 req->r_callback(req, msg);
1853 else
1854 complete_all(&req->r_completion);
1855 }
1856
1857 if (flags & CEPH_OSD_FLAG_ONDISK) {
1858 if (req->r_unsafe_callback && already_completed)
1859 req->r_unsafe_callback(req, false);
1860 complete_request(req);
1861 }
1862
1863 out:
1864 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1865 ceph_osdc_put_request(req);
1866 return;
1867 out_unlock:
1868 mutex_unlock(&osdc->request_mutex);
1869 up_read(&osdc->map_sem);
1870 goto out;
1871
1872 bad_put:
1873 req->r_result = -EIO;
1874 __unregister_request(osdc, req);
1875 if (req->r_callback)
1876 req->r_callback(req, msg);
1877 else
1878 complete_all(&req->r_completion);
1879 complete_request(req);
1880 ceph_osdc_put_request(req);
1881 bad_mutex:
1882 mutex_unlock(&osdc->request_mutex);
1883 up_read(&osdc->map_sem);
1884 bad:
1885 pr_err("corrupt osd_op_reply got %d %d\n",
1886 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1887 ceph_msg_dump(msg);
1888 }
1889
reset_changed_osds(struct ceph_osd_client * osdc)1890 static void reset_changed_osds(struct ceph_osd_client *osdc)
1891 {
1892 struct rb_node *p, *n;
1893
1894 dout("%s %p\n", __func__, osdc);
1895 for (p = rb_first(&osdc->osds); p; p = n) {
1896 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1897
1898 n = rb_next(p);
1899 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1900 memcmp(&osd->o_con.peer_addr,
1901 ceph_osd_addr(osdc->osdmap,
1902 osd->o_osd),
1903 sizeof(struct ceph_entity_addr)) != 0)
1904 __reset_osd(osdc, osd);
1905 }
1906 }
1907
1908 /*
1909 * Requeue requests whose mapping to an OSD has changed. If requests map to
1910 * no osd, request a new map.
1911 *
1912 * Caller should hold map_sem for read.
1913 */
kick_requests(struct ceph_osd_client * osdc,bool force_resend,bool force_resend_writes)1914 static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1915 bool force_resend_writes)
1916 {
1917 struct ceph_osd_request *req, *nreq;
1918 struct rb_node *p;
1919 int needmap = 0;
1920 int err;
1921 bool force_resend_req;
1922
1923 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1924 force_resend_writes ? " (force resend writes)" : "");
1925 mutex_lock(&osdc->request_mutex);
1926 for (p = rb_first(&osdc->requests); p; ) {
1927 req = rb_entry(p, struct ceph_osd_request, r_node);
1928 p = rb_next(p);
1929
1930 /*
1931 * For linger requests that have not yet been
1932 * registered, move them to the linger list; they'll
1933 * be sent to the osd in the loop below. Unregister
1934 * the request before re-registering it as a linger
1935 * request to ensure the __map_request() below
1936 * will decide it needs to be sent.
1937 */
1938 if (req->r_linger && list_empty(&req->r_linger_item)) {
1939 dout("%p tid %llu restart on osd%d\n",
1940 req, req->r_tid,
1941 req->r_osd ? req->r_osd->o_osd : -1);
1942 ceph_osdc_get_request(req);
1943 __unregister_request(osdc, req);
1944 __register_linger_request(osdc, req);
1945 ceph_osdc_put_request(req);
1946 continue;
1947 }
1948
1949 force_resend_req = force_resend ||
1950 (force_resend_writes &&
1951 req->r_flags & CEPH_OSD_FLAG_WRITE);
1952 err = __map_request(osdc, req, force_resend_req);
1953 if (err < 0)
1954 continue; /* error */
1955 if (req->r_osd == NULL) {
1956 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1957 needmap++; /* request a newer map */
1958 } else if (err > 0) {
1959 if (!req->r_linger) {
1960 dout("%p tid %llu requeued on osd%d\n", req,
1961 req->r_tid,
1962 req->r_osd ? req->r_osd->o_osd : -1);
1963 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1964 }
1965 }
1966 }
1967
1968 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1969 r_linger_item) {
1970 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1971
1972 err = __map_request(osdc, req,
1973 force_resend || force_resend_writes);
1974 dout("__map_request returned %d\n", err);
1975 if (err < 0)
1976 continue; /* hrm! */
1977 if (req->r_osd == NULL || err > 0) {
1978 if (req->r_osd == NULL) {
1979 dout("lingering %p tid %llu maps to no osd\n",
1980 req, req->r_tid);
1981 /*
1982 * A homeless lingering request makes
1983 * no sense, as it's job is to keep
1984 * a particular OSD connection open.
1985 * Request a newer map and kick the
1986 * request, knowing that it won't be
1987 * resent until we actually get a map
1988 * that can tell us where to send it.
1989 */
1990 needmap++;
1991 }
1992
1993 dout("kicking lingering %p tid %llu osd%d\n", req,
1994 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
1995 __register_request(osdc, req);
1996 __unregister_linger_request(osdc, req);
1997 }
1998 }
1999 reset_changed_osds(osdc);
2000 mutex_unlock(&osdc->request_mutex);
2001
2002 if (needmap) {
2003 dout("%d requests for down osds, need new map\n", needmap);
2004 ceph_monc_request_next_osdmap(&osdc->client->monc);
2005 }
2006 }
2007
2008
2009 /*
2010 * Process updated osd map.
2011 *
2012 * The message contains any number of incremental and full maps, normally
2013 * indicating some sort of topology change in the cluster. Kick requests
2014 * off to different OSDs as needed.
2015 */
ceph_osdc_handle_map(struct ceph_osd_client * osdc,struct ceph_msg * msg)2016 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2017 {
2018 void *p, *end, *next;
2019 u32 nr_maps, maplen;
2020 u32 epoch;
2021 struct ceph_osdmap *newmap = NULL, *oldmap;
2022 int err;
2023 struct ceph_fsid fsid;
2024 bool was_full;
2025
2026 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2027 p = msg->front.iov_base;
2028 end = p + msg->front.iov_len;
2029
2030 /* verify fsid */
2031 ceph_decode_need(&p, end, sizeof(fsid), bad);
2032 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2033 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2034 return;
2035
2036 down_write(&osdc->map_sem);
2037
2038 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2039
2040 /* incremental maps */
2041 ceph_decode_32_safe(&p, end, nr_maps, bad);
2042 dout(" %d inc maps\n", nr_maps);
2043 while (nr_maps > 0) {
2044 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2045 epoch = ceph_decode_32(&p);
2046 maplen = ceph_decode_32(&p);
2047 ceph_decode_need(&p, end, maplen, bad);
2048 next = p + maplen;
2049 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2050 dout("applying incremental map %u len %d\n",
2051 epoch, maplen);
2052 newmap = osdmap_apply_incremental(&p, next,
2053 osdc->osdmap,
2054 &osdc->client->msgr);
2055 if (IS_ERR(newmap)) {
2056 err = PTR_ERR(newmap);
2057 goto bad;
2058 }
2059 BUG_ON(!newmap);
2060 if (newmap != osdc->osdmap) {
2061 ceph_osdmap_destroy(osdc->osdmap);
2062 osdc->osdmap = newmap;
2063 }
2064 was_full = was_full ||
2065 ceph_osdmap_flag(osdc->osdmap,
2066 CEPH_OSDMAP_FULL);
2067 kick_requests(osdc, 0, was_full);
2068 } else {
2069 dout("ignoring incremental map %u len %d\n",
2070 epoch, maplen);
2071 }
2072 p = next;
2073 nr_maps--;
2074 }
2075 if (newmap)
2076 goto done;
2077
2078 /* full maps */
2079 ceph_decode_32_safe(&p, end, nr_maps, bad);
2080 dout(" %d full maps\n", nr_maps);
2081 while (nr_maps) {
2082 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2083 epoch = ceph_decode_32(&p);
2084 maplen = ceph_decode_32(&p);
2085 ceph_decode_need(&p, end, maplen, bad);
2086 if (nr_maps > 1) {
2087 dout("skipping non-latest full map %u len %d\n",
2088 epoch, maplen);
2089 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2090 dout("skipping full map %u len %d, "
2091 "older than our %u\n", epoch, maplen,
2092 osdc->osdmap->epoch);
2093 } else {
2094 int skipped_map = 0;
2095
2096 dout("taking full map %u len %d\n", epoch, maplen);
2097 newmap = ceph_osdmap_decode(&p, p+maplen);
2098 if (IS_ERR(newmap)) {
2099 err = PTR_ERR(newmap);
2100 goto bad;
2101 }
2102 BUG_ON(!newmap);
2103 oldmap = osdc->osdmap;
2104 osdc->osdmap = newmap;
2105 if (oldmap) {
2106 if (oldmap->epoch + 1 < newmap->epoch)
2107 skipped_map = 1;
2108 ceph_osdmap_destroy(oldmap);
2109 }
2110 was_full = was_full ||
2111 ceph_osdmap_flag(osdc->osdmap,
2112 CEPH_OSDMAP_FULL);
2113 kick_requests(osdc, skipped_map, was_full);
2114 }
2115 p += maplen;
2116 nr_maps--;
2117 }
2118
2119 if (!osdc->osdmap)
2120 goto bad;
2121 done:
2122 downgrade_write(&osdc->map_sem);
2123 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
2124
2125 /*
2126 * subscribe to subsequent osdmap updates if full to ensure
2127 * we find out when we are no longer full and stop returning
2128 * ENOSPC.
2129 */
2130 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2131 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2132 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
2133 ceph_monc_request_next_osdmap(&osdc->client->monc);
2134
2135 mutex_lock(&osdc->request_mutex);
2136 __send_queued(osdc);
2137 mutex_unlock(&osdc->request_mutex);
2138 up_read(&osdc->map_sem);
2139 wake_up_all(&osdc->client->auth_wq);
2140 return;
2141
2142 bad:
2143 pr_err("osdc handle_map corrupt msg\n");
2144 ceph_msg_dump(msg);
2145 up_write(&osdc->map_sem);
2146 }
2147
2148 /*
2149 * watch/notify callback event infrastructure
2150 *
2151 * These callbacks are used both for watch and notify operations.
2152 */
__release_event(struct kref * kref)2153 static void __release_event(struct kref *kref)
2154 {
2155 struct ceph_osd_event *event =
2156 container_of(kref, struct ceph_osd_event, kref);
2157
2158 dout("__release_event %p\n", event);
2159 kfree(event);
2160 }
2161
get_event(struct ceph_osd_event * event)2162 static void get_event(struct ceph_osd_event *event)
2163 {
2164 kref_get(&event->kref);
2165 }
2166
ceph_osdc_put_event(struct ceph_osd_event * event)2167 void ceph_osdc_put_event(struct ceph_osd_event *event)
2168 {
2169 kref_put(&event->kref, __release_event);
2170 }
2171 EXPORT_SYMBOL(ceph_osdc_put_event);
2172
__insert_event(struct ceph_osd_client * osdc,struct ceph_osd_event * new)2173 static void __insert_event(struct ceph_osd_client *osdc,
2174 struct ceph_osd_event *new)
2175 {
2176 struct rb_node **p = &osdc->event_tree.rb_node;
2177 struct rb_node *parent = NULL;
2178 struct ceph_osd_event *event = NULL;
2179
2180 while (*p) {
2181 parent = *p;
2182 event = rb_entry(parent, struct ceph_osd_event, node);
2183 if (new->cookie < event->cookie)
2184 p = &(*p)->rb_left;
2185 else if (new->cookie > event->cookie)
2186 p = &(*p)->rb_right;
2187 else
2188 BUG();
2189 }
2190
2191 rb_link_node(&new->node, parent, p);
2192 rb_insert_color(&new->node, &osdc->event_tree);
2193 }
2194
__find_event(struct ceph_osd_client * osdc,u64 cookie)2195 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2196 u64 cookie)
2197 {
2198 struct rb_node **p = &osdc->event_tree.rb_node;
2199 struct rb_node *parent = NULL;
2200 struct ceph_osd_event *event = NULL;
2201
2202 while (*p) {
2203 parent = *p;
2204 event = rb_entry(parent, struct ceph_osd_event, node);
2205 if (cookie < event->cookie)
2206 p = &(*p)->rb_left;
2207 else if (cookie > event->cookie)
2208 p = &(*p)->rb_right;
2209 else
2210 return event;
2211 }
2212 return NULL;
2213 }
2214
__remove_event(struct ceph_osd_event * event)2215 static void __remove_event(struct ceph_osd_event *event)
2216 {
2217 struct ceph_osd_client *osdc = event->osdc;
2218
2219 if (!RB_EMPTY_NODE(&event->node)) {
2220 dout("__remove_event removed %p\n", event);
2221 rb_erase(&event->node, &osdc->event_tree);
2222 ceph_osdc_put_event(event);
2223 } else {
2224 dout("__remove_event didn't remove %p\n", event);
2225 }
2226 }
2227
ceph_osdc_create_event(struct ceph_osd_client * osdc,void (* event_cb)(u64,u64,u8,void *),void * data,struct ceph_osd_event ** pevent)2228 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2229 void (*event_cb)(u64, u64, u8, void *),
2230 void *data, struct ceph_osd_event **pevent)
2231 {
2232 struct ceph_osd_event *event;
2233
2234 event = kmalloc(sizeof(*event), GFP_NOIO);
2235 if (!event)
2236 return -ENOMEM;
2237
2238 dout("create_event %p\n", event);
2239 event->cb = event_cb;
2240 event->one_shot = 0;
2241 event->data = data;
2242 event->osdc = osdc;
2243 INIT_LIST_HEAD(&event->osd_node);
2244 RB_CLEAR_NODE(&event->node);
2245 kref_init(&event->kref); /* one ref for us */
2246 kref_get(&event->kref); /* one ref for the caller */
2247
2248 spin_lock(&osdc->event_lock);
2249 event->cookie = ++osdc->event_count;
2250 __insert_event(osdc, event);
2251 spin_unlock(&osdc->event_lock);
2252
2253 *pevent = event;
2254 return 0;
2255 }
2256 EXPORT_SYMBOL(ceph_osdc_create_event);
2257
ceph_osdc_cancel_event(struct ceph_osd_event * event)2258 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2259 {
2260 struct ceph_osd_client *osdc = event->osdc;
2261
2262 dout("cancel_event %p\n", event);
2263 spin_lock(&osdc->event_lock);
2264 __remove_event(event);
2265 spin_unlock(&osdc->event_lock);
2266 ceph_osdc_put_event(event); /* caller's */
2267 }
2268 EXPORT_SYMBOL(ceph_osdc_cancel_event);
2269
2270
do_event_work(struct work_struct * work)2271 static void do_event_work(struct work_struct *work)
2272 {
2273 struct ceph_osd_event_work *event_work =
2274 container_of(work, struct ceph_osd_event_work, work);
2275 struct ceph_osd_event *event = event_work->event;
2276 u64 ver = event_work->ver;
2277 u64 notify_id = event_work->notify_id;
2278 u8 opcode = event_work->opcode;
2279
2280 dout("do_event_work completing %p\n", event);
2281 event->cb(ver, notify_id, opcode, event->data);
2282 dout("do_event_work completed %p\n", event);
2283 ceph_osdc_put_event(event);
2284 kfree(event_work);
2285 }
2286
2287
2288 /*
2289 * Process osd watch notifications
2290 */
handle_watch_notify(struct ceph_osd_client * osdc,struct ceph_msg * msg)2291 static void handle_watch_notify(struct ceph_osd_client *osdc,
2292 struct ceph_msg *msg)
2293 {
2294 void *p, *end;
2295 u8 proto_ver;
2296 u64 cookie, ver, notify_id;
2297 u8 opcode;
2298 struct ceph_osd_event *event;
2299 struct ceph_osd_event_work *event_work;
2300
2301 p = msg->front.iov_base;
2302 end = p + msg->front.iov_len;
2303
2304 ceph_decode_8_safe(&p, end, proto_ver, bad);
2305 ceph_decode_8_safe(&p, end, opcode, bad);
2306 ceph_decode_64_safe(&p, end, cookie, bad);
2307 ceph_decode_64_safe(&p, end, ver, bad);
2308 ceph_decode_64_safe(&p, end, notify_id, bad);
2309
2310 spin_lock(&osdc->event_lock);
2311 event = __find_event(osdc, cookie);
2312 if (event) {
2313 BUG_ON(event->one_shot);
2314 get_event(event);
2315 }
2316 spin_unlock(&osdc->event_lock);
2317 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2318 cookie, ver, event);
2319 if (event) {
2320 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
2321 if (!event_work) {
2322 pr_err("couldn't allocate event_work\n");
2323 ceph_osdc_put_event(event);
2324 return;
2325 }
2326 INIT_WORK(&event_work->work, do_event_work);
2327 event_work->event = event;
2328 event_work->ver = ver;
2329 event_work->notify_id = notify_id;
2330 event_work->opcode = opcode;
2331
2332 queue_work(osdc->notify_wq, &event_work->work);
2333 }
2334
2335 return;
2336
2337 bad:
2338 pr_err("osdc handle_watch_notify corrupt msg\n");
2339 }
2340
2341 /*
2342 * build new request AND message
2343 *
2344 */
ceph_osdc_build_request(struct ceph_osd_request * req,u64 off,struct ceph_snap_context * snapc,u64 snap_id,struct timespec * mtime)2345 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2346 struct ceph_snap_context *snapc, u64 snap_id,
2347 struct timespec *mtime)
2348 {
2349 struct ceph_msg *msg = req->r_request;
2350 void *p;
2351 size_t msg_size;
2352 int flags = req->r_flags;
2353 u64 data_len;
2354 unsigned int i;
2355
2356 req->r_snapid = snap_id;
2357 req->r_snapc = ceph_get_snap_context(snapc);
2358
2359 /* encode request */
2360 msg->hdr.version = cpu_to_le16(4);
2361
2362 p = msg->front.iov_base;
2363 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2364 req->r_request_osdmap_epoch = p;
2365 p += 4;
2366 req->r_request_flags = p;
2367 p += 4;
2368 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2369 ceph_encode_timespec(p, mtime);
2370 p += sizeof(struct ceph_timespec);
2371 req->r_request_reassert_version = p;
2372 p += sizeof(struct ceph_eversion); /* will get filled in */
2373
2374 /* oloc */
2375 ceph_encode_8(&p, 4);
2376 ceph_encode_8(&p, 4);
2377 ceph_encode_32(&p, 8 + 4 + 4);
2378 req->r_request_pool = p;
2379 p += 8;
2380 ceph_encode_32(&p, -1); /* preferred */
2381 ceph_encode_32(&p, 0); /* key len */
2382
2383 ceph_encode_8(&p, 1);
2384 req->r_request_pgid = p;
2385 p += 8 + 4;
2386 ceph_encode_32(&p, -1); /* preferred */
2387
2388 /* oid */
2389 ceph_encode_32(&p, req->r_base_oid.name_len);
2390 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2391 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2392 req->r_base_oid.name, req->r_base_oid.name_len);
2393 p += req->r_base_oid.name_len;
2394
2395 /* ops--can imply data */
2396 ceph_encode_16(&p, (u16)req->r_num_ops);
2397 data_len = 0;
2398 for (i = 0; i < req->r_num_ops; i++) {
2399 data_len += osd_req_encode_op(req, p, i);
2400 p += sizeof(struct ceph_osd_op);
2401 }
2402
2403 /* snaps */
2404 ceph_encode_64(&p, req->r_snapid);
2405 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2406 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2407 if (req->r_snapc) {
2408 for (i = 0; i < snapc->num_snaps; i++) {
2409 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2410 }
2411 }
2412
2413 req->r_request_attempts = p;
2414 p += 4;
2415
2416 /* data */
2417 if (flags & CEPH_OSD_FLAG_WRITE) {
2418 u16 data_off;
2419
2420 /*
2421 * The header "data_off" is a hint to the receiver
2422 * allowing it to align received data into its
2423 * buffers such that there's no need to re-copy
2424 * it before writing it to disk (direct I/O).
2425 */
2426 data_off = (u16) (off & 0xffff);
2427 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2428 }
2429 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2430
2431 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2432 msg_size = p - msg->front.iov_base;
2433 msg->front.iov_len = msg_size;
2434 msg->hdr.front_len = cpu_to_le32(msg_size);
2435
2436 dout("build_request msg_size was %d\n", (int)msg_size);
2437 }
2438 EXPORT_SYMBOL(ceph_osdc_build_request);
2439
2440 /*
2441 * Register request, send initial attempt.
2442 */
ceph_osdc_start_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req,bool nofail)2443 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2444 struct ceph_osd_request *req,
2445 bool nofail)
2446 {
2447 int rc;
2448
2449 down_read(&osdc->map_sem);
2450 mutex_lock(&osdc->request_mutex);
2451
2452 rc = __ceph_osdc_start_request(osdc, req, nofail);
2453
2454 mutex_unlock(&osdc->request_mutex);
2455 up_read(&osdc->map_sem);
2456
2457 return rc;
2458 }
2459 EXPORT_SYMBOL(ceph_osdc_start_request);
2460
2461 /*
2462 * Unregister a registered request. The request is not completed (i.e.
2463 * no callbacks or wakeups) - higher layers are supposed to know what
2464 * they are canceling.
2465 */
ceph_osdc_cancel_request(struct ceph_osd_request * req)2466 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2467 {
2468 struct ceph_osd_client *osdc = req->r_osdc;
2469
2470 mutex_lock(&osdc->request_mutex);
2471 if (req->r_linger)
2472 __unregister_linger_request(osdc, req);
2473 __unregister_request(osdc, req);
2474 mutex_unlock(&osdc->request_mutex);
2475
2476 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2477 }
2478 EXPORT_SYMBOL(ceph_osdc_cancel_request);
2479
2480 /*
2481 * wait for a request to complete
2482 */
ceph_osdc_wait_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)2483 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2484 struct ceph_osd_request *req)
2485 {
2486 int rc;
2487
2488 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2489
2490 rc = wait_for_completion_interruptible(&req->r_completion);
2491 if (rc < 0) {
2492 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2493 ceph_osdc_cancel_request(req);
2494 complete_request(req);
2495 return rc;
2496 }
2497
2498 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2499 req->r_result);
2500 return req->r_result;
2501 }
2502 EXPORT_SYMBOL(ceph_osdc_wait_request);
2503
2504 /*
2505 * sync - wait for all in-flight requests to flush. avoid starvation.
2506 */
ceph_osdc_sync(struct ceph_osd_client * osdc)2507 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2508 {
2509 struct ceph_osd_request *req;
2510 u64 last_tid, next_tid = 0;
2511
2512 mutex_lock(&osdc->request_mutex);
2513 last_tid = osdc->last_tid;
2514 while (1) {
2515 req = __lookup_request_ge(osdc, next_tid);
2516 if (!req)
2517 break;
2518 if (req->r_tid > last_tid)
2519 break;
2520
2521 next_tid = req->r_tid + 1;
2522 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2523 continue;
2524
2525 ceph_osdc_get_request(req);
2526 mutex_unlock(&osdc->request_mutex);
2527 dout("sync waiting on tid %llu (last is %llu)\n",
2528 req->r_tid, last_tid);
2529 wait_for_completion(&req->r_safe_completion);
2530 mutex_lock(&osdc->request_mutex);
2531 ceph_osdc_put_request(req);
2532 }
2533 mutex_unlock(&osdc->request_mutex);
2534 dout("sync done (thru tid %llu)\n", last_tid);
2535 }
2536 EXPORT_SYMBOL(ceph_osdc_sync);
2537
2538 /*
2539 * Call all pending notify callbacks - for use after a watch is
2540 * unregistered, to make sure no more callbacks for it will be invoked
2541 */
ceph_osdc_flush_notifies(struct ceph_osd_client * osdc)2542 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2543 {
2544 flush_workqueue(osdc->notify_wq);
2545 }
2546 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2547
2548
2549 /*
2550 * init, shutdown
2551 */
ceph_osdc_init(struct ceph_osd_client * osdc,struct ceph_client * client)2552 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2553 {
2554 int err;
2555
2556 dout("init\n");
2557 osdc->client = client;
2558 osdc->osdmap = NULL;
2559 init_rwsem(&osdc->map_sem);
2560 init_completion(&osdc->map_waiters);
2561 osdc->last_requested_map = 0;
2562 mutex_init(&osdc->request_mutex);
2563 osdc->last_tid = 0;
2564 osdc->osds = RB_ROOT;
2565 INIT_LIST_HEAD(&osdc->osd_lru);
2566 osdc->requests = RB_ROOT;
2567 INIT_LIST_HEAD(&osdc->req_lru);
2568 INIT_LIST_HEAD(&osdc->req_unsent);
2569 INIT_LIST_HEAD(&osdc->req_notarget);
2570 INIT_LIST_HEAD(&osdc->req_linger);
2571 osdc->num_requests = 0;
2572 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2573 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2574 spin_lock_init(&osdc->event_lock);
2575 osdc->event_tree = RB_ROOT;
2576 osdc->event_count = 0;
2577
2578 schedule_delayed_work(&osdc->osds_timeout_work,
2579 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2580
2581 err = -ENOMEM;
2582 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2583 sizeof(struct ceph_osd_request));
2584 if (!osdc->req_mempool)
2585 goto out;
2586
2587 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2588 OSD_OP_FRONT_LEN, 10, true,
2589 "osd_op");
2590 if (err < 0)
2591 goto out_mempool;
2592 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2593 OSD_OPREPLY_FRONT_LEN, 10, true,
2594 "osd_op_reply");
2595 if (err < 0)
2596 goto out_msgpool;
2597
2598 err = -ENOMEM;
2599 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2600 if (!osdc->notify_wq)
2601 goto out_msgpool_reply;
2602
2603 return 0;
2604
2605 out_msgpool_reply:
2606 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2607 out_msgpool:
2608 ceph_msgpool_destroy(&osdc->msgpool_op);
2609 out_mempool:
2610 mempool_destroy(osdc->req_mempool);
2611 out:
2612 return err;
2613 }
2614
ceph_osdc_stop(struct ceph_osd_client * osdc)2615 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2616 {
2617 flush_workqueue(osdc->notify_wq);
2618 destroy_workqueue(osdc->notify_wq);
2619 cancel_delayed_work_sync(&osdc->timeout_work);
2620 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2621 if (osdc->osdmap) {
2622 ceph_osdmap_destroy(osdc->osdmap);
2623 osdc->osdmap = NULL;
2624 }
2625 remove_all_osds(osdc);
2626 mempool_destroy(osdc->req_mempool);
2627 ceph_msgpool_destroy(&osdc->msgpool_op);
2628 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2629 }
2630
2631 /*
2632 * Read some contiguous pages. If we cross a stripe boundary, shorten
2633 * *plen. Return number of bytes read, or error.
2634 */
ceph_osdc_readpages(struct ceph_osd_client * osdc,struct ceph_vino vino,struct ceph_file_layout * layout,u64 off,u64 * plen,u32 truncate_seq,u64 truncate_size,struct page ** pages,int num_pages,int page_align)2635 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2636 struct ceph_vino vino, struct ceph_file_layout *layout,
2637 u64 off, u64 *plen,
2638 u32 truncate_seq, u64 truncate_size,
2639 struct page **pages, int num_pages, int page_align)
2640 {
2641 struct ceph_osd_request *req;
2642 int rc = 0;
2643
2644 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2645 vino.snap, off, *plen);
2646 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
2647 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2648 NULL, truncate_seq, truncate_size,
2649 false);
2650 if (IS_ERR(req))
2651 return PTR_ERR(req);
2652
2653 /* it may be a short read due to an object boundary */
2654
2655 osd_req_op_extent_osd_data_pages(req, 0,
2656 pages, *plen, page_align, false, false);
2657
2658 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2659 off, *plen, *plen, page_align);
2660
2661 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2662
2663 rc = ceph_osdc_start_request(osdc, req, false);
2664 if (!rc)
2665 rc = ceph_osdc_wait_request(osdc, req);
2666
2667 ceph_osdc_put_request(req);
2668 dout("readpages result %d\n", rc);
2669 return rc;
2670 }
2671 EXPORT_SYMBOL(ceph_osdc_readpages);
2672
2673 /*
2674 * do a synchronous write on N pages
2675 */
ceph_osdc_writepages(struct ceph_osd_client * osdc,struct ceph_vino vino,struct ceph_file_layout * layout,struct ceph_snap_context * snapc,u64 off,u64 len,u32 truncate_seq,u64 truncate_size,struct timespec * mtime,struct page ** pages,int num_pages)2676 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2677 struct ceph_file_layout *layout,
2678 struct ceph_snap_context *snapc,
2679 u64 off, u64 len,
2680 u32 truncate_seq, u64 truncate_size,
2681 struct timespec *mtime,
2682 struct page **pages, int num_pages)
2683 {
2684 struct ceph_osd_request *req;
2685 int rc = 0;
2686 int page_align = off & ~PAGE_MASK;
2687
2688 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2689 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
2690 CEPH_OSD_OP_WRITE,
2691 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2692 snapc, truncate_seq, truncate_size,
2693 true);
2694 if (IS_ERR(req))
2695 return PTR_ERR(req);
2696
2697 /* it may be a short write due to an object boundary */
2698 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2699 false, false);
2700 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2701
2702 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2703
2704 rc = ceph_osdc_start_request(osdc, req, true);
2705 if (!rc)
2706 rc = ceph_osdc_wait_request(osdc, req);
2707
2708 ceph_osdc_put_request(req);
2709 if (rc == 0)
2710 rc = len;
2711 dout("writepages result %d\n", rc);
2712 return rc;
2713 }
2714 EXPORT_SYMBOL(ceph_osdc_writepages);
2715
ceph_osdc_setup(void)2716 int ceph_osdc_setup(void)
2717 {
2718 BUG_ON(ceph_osd_request_cache);
2719 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2720 sizeof (struct ceph_osd_request),
2721 __alignof__(struct ceph_osd_request),
2722 0, NULL);
2723
2724 return ceph_osd_request_cache ? 0 : -ENOMEM;
2725 }
2726 EXPORT_SYMBOL(ceph_osdc_setup);
2727
ceph_osdc_cleanup(void)2728 void ceph_osdc_cleanup(void)
2729 {
2730 BUG_ON(!ceph_osd_request_cache);
2731 kmem_cache_destroy(ceph_osd_request_cache);
2732 ceph_osd_request_cache = NULL;
2733 }
2734 EXPORT_SYMBOL(ceph_osdc_cleanup);
2735
2736 /*
2737 * handle incoming message
2738 */
dispatch(struct ceph_connection * con,struct ceph_msg * msg)2739 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2740 {
2741 struct ceph_osd *osd = con->private;
2742 struct ceph_osd_client *osdc;
2743 int type = le16_to_cpu(msg->hdr.type);
2744
2745 if (!osd)
2746 goto out;
2747 osdc = osd->o_osdc;
2748
2749 switch (type) {
2750 case CEPH_MSG_OSD_MAP:
2751 ceph_osdc_handle_map(osdc, msg);
2752 break;
2753 case CEPH_MSG_OSD_OPREPLY:
2754 handle_reply(osdc, msg, con);
2755 break;
2756 case CEPH_MSG_WATCH_NOTIFY:
2757 handle_watch_notify(osdc, msg);
2758 break;
2759
2760 default:
2761 pr_err("received unknown message type %d %s\n", type,
2762 ceph_msg_type_name(type));
2763 }
2764 out:
2765 ceph_msg_put(msg);
2766 }
2767
2768 /*
2769 * lookup and return message for incoming reply. set up reply message
2770 * pages.
2771 */
get_reply(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)2772 static struct ceph_msg *get_reply(struct ceph_connection *con,
2773 struct ceph_msg_header *hdr,
2774 int *skip)
2775 {
2776 struct ceph_osd *osd = con->private;
2777 struct ceph_osd_client *osdc = osd->o_osdc;
2778 struct ceph_msg *m;
2779 struct ceph_osd_request *req;
2780 int front_len = le32_to_cpu(hdr->front_len);
2781 int data_len = le32_to_cpu(hdr->data_len);
2782 u64 tid;
2783
2784 tid = le64_to_cpu(hdr->tid);
2785 mutex_lock(&osdc->request_mutex);
2786 req = __lookup_request(osdc, tid);
2787 if (!req) {
2788 *skip = 1;
2789 m = NULL;
2790 dout("get_reply unknown tid %llu from osd%d\n", tid,
2791 osd->o_osd);
2792 goto out;
2793 }
2794
2795 if (req->r_reply->con)
2796 dout("%s revoking msg %p from old con %p\n", __func__,
2797 req->r_reply, req->r_reply->con);
2798 ceph_msg_revoke_incoming(req->r_reply);
2799
2800 if (front_len > req->r_reply->front_alloc_len) {
2801 pr_warn("get_reply front %d > preallocated %d (%u#%llu)\n",
2802 front_len, req->r_reply->front_alloc_len,
2803 (unsigned int)con->peer_name.type,
2804 le64_to_cpu(con->peer_name.num));
2805 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2806 false);
2807 if (!m)
2808 goto out;
2809 ceph_msg_put(req->r_reply);
2810 req->r_reply = m;
2811 }
2812 m = ceph_msg_get(req->r_reply);
2813
2814 if (data_len > 0) {
2815 struct ceph_osd_data *osd_data;
2816
2817 /*
2818 * XXX This is assuming there is only one op containing
2819 * XXX page data. Probably OK for reads, but this
2820 * XXX ought to be done more generally.
2821 */
2822 osd_data = osd_req_op_extent_osd_data(req, 0);
2823 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2824 if (osd_data->pages &&
2825 unlikely(osd_data->length < data_len)) {
2826
2827 pr_warn("tid %lld reply has %d bytes we had only %llu bytes ready\n",
2828 tid, data_len, osd_data->length);
2829 *skip = 1;
2830 ceph_msg_put(m);
2831 m = NULL;
2832 goto out;
2833 }
2834 }
2835 }
2836 *skip = 0;
2837 dout("get_reply tid %lld %p\n", tid, m);
2838
2839 out:
2840 mutex_unlock(&osdc->request_mutex);
2841 return m;
2842
2843 }
2844
alloc_msg(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)2845 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2846 struct ceph_msg_header *hdr,
2847 int *skip)
2848 {
2849 struct ceph_osd *osd = con->private;
2850 int type = le16_to_cpu(hdr->type);
2851 int front = le32_to_cpu(hdr->front_len);
2852
2853 *skip = 0;
2854 switch (type) {
2855 case CEPH_MSG_OSD_MAP:
2856 case CEPH_MSG_WATCH_NOTIFY:
2857 return ceph_msg_new(type, front, GFP_NOFS, false);
2858 case CEPH_MSG_OSD_OPREPLY:
2859 return get_reply(con, hdr, skip);
2860 default:
2861 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2862 osd->o_osd);
2863 *skip = 1;
2864 return NULL;
2865 }
2866 }
2867
2868 /*
2869 * Wrappers to refcount containing ceph_osd struct
2870 */
get_osd_con(struct ceph_connection * con)2871 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2872 {
2873 struct ceph_osd *osd = con->private;
2874 if (get_osd(osd))
2875 return con;
2876 return NULL;
2877 }
2878
put_osd_con(struct ceph_connection * con)2879 static void put_osd_con(struct ceph_connection *con)
2880 {
2881 struct ceph_osd *osd = con->private;
2882 put_osd(osd);
2883 }
2884
2885 /*
2886 * authentication
2887 */
2888 /*
2889 * Note: returned pointer is the address of a structure that's
2890 * managed separately. Caller must *not* attempt to free it.
2891 */
get_authorizer(struct ceph_connection * con,int * proto,int force_new)2892 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2893 int *proto, int force_new)
2894 {
2895 struct ceph_osd *o = con->private;
2896 struct ceph_osd_client *osdc = o->o_osdc;
2897 struct ceph_auth_client *ac = osdc->client->monc.auth;
2898 struct ceph_auth_handshake *auth = &o->o_auth;
2899
2900 if (force_new && auth->authorizer) {
2901 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2902 auth->authorizer = NULL;
2903 }
2904 if (!auth->authorizer) {
2905 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2906 auth);
2907 if (ret)
2908 return ERR_PTR(ret);
2909 } else {
2910 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2911 auth);
2912 if (ret)
2913 return ERR_PTR(ret);
2914 }
2915 *proto = ac->protocol;
2916
2917 return auth;
2918 }
2919
2920
verify_authorizer_reply(struct ceph_connection * con,int len)2921 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2922 {
2923 struct ceph_osd *o = con->private;
2924 struct ceph_osd_client *osdc = o->o_osdc;
2925 struct ceph_auth_client *ac = osdc->client->monc.auth;
2926
2927 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2928 }
2929
invalidate_authorizer(struct ceph_connection * con)2930 static int invalidate_authorizer(struct ceph_connection *con)
2931 {
2932 struct ceph_osd *o = con->private;
2933 struct ceph_osd_client *osdc = o->o_osdc;
2934 struct ceph_auth_client *ac = osdc->client->monc.auth;
2935
2936 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2937 return ceph_monc_validate_auth(&osdc->client->monc);
2938 }
2939
2940 static const struct ceph_connection_operations osd_con_ops = {
2941 .get = get_osd_con,
2942 .put = put_osd_con,
2943 .dispatch = dispatch,
2944 .get_authorizer = get_authorizer,
2945 .verify_authorizer_reply = verify_authorizer_reply,
2946 .invalidate_authorizer = invalidate_authorizer,
2947 .alloc_msg = alloc_msg,
2948 .fault = osd_reset,
2949 };
2950