1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/ceph/ceph_debug.h>
4
5 #include <linux/module.h>
6 #include <linux/err.h>
7 #include <linux/highmem.h>
8 #include <linux/mm.h>
9 #include <linux/pagemap.h>
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
12 #ifdef CONFIG_BLOCK
13 #include <linux/bio.h>
14 #endif
15
16 #include <linux/ceph/ceph_features.h>
17 #include <linux/ceph/libceph.h>
18 #include <linux/ceph/osd_client.h>
19 #include <linux/ceph/messenger.h>
20 #include <linux/ceph/decode.h>
21 #include <linux/ceph/auth.h>
22 #include <linux/ceph/pagelist.h>
23 #include <linux/ceph/striper.h>
24
25 #define OSD_OPREPLY_FRONT_LEN 512
26
27 static struct kmem_cache *ceph_osd_request_cache;
28
29 static const struct ceph_connection_operations osd_con_ops;
30
31 /*
32 * Implement client access to distributed object storage cluster.
33 *
34 * All data objects are stored within a cluster/cloud of OSDs, or
35 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
36 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
37 * remote daemons serving up and coordinating consistent and safe
38 * access to storage.
39 *
40 * Cluster membership and the mapping of data objects onto storage devices
41 * are described by the osd map.
42 *
43 * We keep track of pending OSD requests (read, write), resubmit
44 * requests to different OSDs when the cluster topology/data layout
45 * change, or retry the affected requests when the communications
46 * channel with an OSD is reset.
47 */
48
49 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
50 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
51 static void link_linger(struct ceph_osd *osd,
52 struct ceph_osd_linger_request *lreq);
53 static void unlink_linger(struct ceph_osd *osd,
54 struct ceph_osd_linger_request *lreq);
55 static void clear_backoffs(struct ceph_osd *osd);
56
57 #if 1
rwsem_is_wrlocked(struct rw_semaphore * sem)58 static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
59 {
60 bool wrlocked = true;
61
62 if (unlikely(down_read_trylock(sem))) {
63 wrlocked = false;
64 up_read(sem);
65 }
66
67 return wrlocked;
68 }
verify_osdc_locked(struct ceph_osd_client * osdc)69 static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
70 {
71 WARN_ON(!rwsem_is_locked(&osdc->lock));
72 }
verify_osdc_wrlocked(struct ceph_osd_client * osdc)73 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
74 {
75 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
76 }
verify_osd_locked(struct ceph_osd * osd)77 static inline void verify_osd_locked(struct ceph_osd *osd)
78 {
79 struct ceph_osd_client *osdc = osd->o_osdc;
80
81 WARN_ON(!(mutex_is_locked(&osd->lock) &&
82 rwsem_is_locked(&osdc->lock)) &&
83 !rwsem_is_wrlocked(&osdc->lock));
84 }
verify_lreq_locked(struct ceph_osd_linger_request * lreq)85 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86 {
87 WARN_ON(!mutex_is_locked(&lreq->lock));
88 }
89 #else
verify_osdc_locked(struct ceph_osd_client * osdc)90 static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
verify_osdc_wrlocked(struct ceph_osd_client * osdc)91 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
verify_osd_locked(struct ceph_osd * osd)92 static inline void verify_osd_locked(struct ceph_osd *osd) { }
verify_lreq_locked(struct ceph_osd_linger_request * lreq)93 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
94 #endif
95
96 /*
97 * calculate the mapping of a file extent onto an object, and fill out the
98 * request accordingly. shorten extent as necessary if it crosses an
99 * object boundary.
100 *
101 * fill osd op in request message.
102 */
calc_layout(struct ceph_file_layout * layout,u64 off,u64 * plen,u64 * objnum,u64 * objoff,u64 * objlen)103 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104 u64 *objnum, u64 *objoff, u64 *objlen)
105 {
106 u64 orig_len = *plen;
107 u32 xlen;
108
109 /* object extent? */
110 ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111 objoff, &xlen);
112 *objlen = xlen;
113 if (*objlen < orig_len) {
114 *plen = *objlen;
115 dout(" skipping last %llu, final file extent %llu~%llu\n",
116 orig_len - *plen, off, *plen);
117 }
118
119 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
120 return 0;
121 }
122
ceph_osd_data_init(struct ceph_osd_data * osd_data)123 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124 {
125 memset(osd_data, 0, sizeof (*osd_data));
126 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127 }
128
129 /*
130 * Consumes @pages if @own_pages is true.
131 */
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)132 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
133 struct page **pages, u64 length, u32 alignment,
134 bool pages_from_pool, bool own_pages)
135 {
136 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
137 osd_data->pages = pages;
138 osd_data->length = length;
139 osd_data->alignment = alignment;
140 osd_data->pages_from_pool = pages_from_pool;
141 osd_data->own_pages = own_pages;
142 }
143
144 /*
145 * Consumes a ref on @pagelist.
146 */
ceph_osd_data_pagelist_init(struct ceph_osd_data * osd_data,struct ceph_pagelist * pagelist)147 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
148 struct ceph_pagelist *pagelist)
149 {
150 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
151 osd_data->pagelist = pagelist;
152 }
153
154 #ifdef CONFIG_BLOCK
ceph_osd_data_bio_init(struct ceph_osd_data * osd_data,struct ceph_bio_iter * bio_pos,u32 bio_length)155 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
156 struct ceph_bio_iter *bio_pos,
157 u32 bio_length)
158 {
159 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
160 osd_data->bio_pos = *bio_pos;
161 osd_data->bio_length = bio_length;
162 }
163 #endif /* CONFIG_BLOCK */
164
ceph_osd_data_bvecs_init(struct ceph_osd_data * osd_data,struct ceph_bvec_iter * bvec_pos,u32 num_bvecs)165 static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
166 struct ceph_bvec_iter *bvec_pos,
167 u32 num_bvecs)
168 {
169 osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
170 osd_data->bvec_pos = *bvec_pos;
171 osd_data->num_bvecs = num_bvecs;
172 }
173
ceph_osd_iter_init(struct ceph_osd_data * osd_data,struct iov_iter * iter)174 static void ceph_osd_iter_init(struct ceph_osd_data *osd_data,
175 struct iov_iter *iter)
176 {
177 osd_data->type = CEPH_OSD_DATA_TYPE_ITER;
178 osd_data->iter = *iter;
179 }
180
181 static struct ceph_osd_data *
osd_req_op_raw_data_in(struct ceph_osd_request * osd_req,unsigned int which)182 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
183 {
184 BUG_ON(which >= osd_req->r_num_ops);
185
186 return &osd_req->r_ops[which].raw_data_in;
187 }
188
189 struct ceph_osd_data *
osd_req_op_extent_osd_data(struct ceph_osd_request * osd_req,unsigned int which)190 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
191 unsigned int which)
192 {
193 return osd_req_op_data(osd_req, which, extent, osd_data);
194 }
195 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
196
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)197 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
198 unsigned int which, struct page **pages,
199 u64 length, u32 alignment,
200 bool pages_from_pool, bool own_pages)
201 {
202 struct ceph_osd_data *osd_data;
203
204 osd_data = osd_req_op_raw_data_in(osd_req, which);
205 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
206 pages_from_pool, own_pages);
207 }
208 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
209
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)210 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
211 unsigned int which, struct page **pages,
212 u64 length, u32 alignment,
213 bool pages_from_pool, bool own_pages)
214 {
215 struct ceph_osd_data *osd_data;
216
217 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
218 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
219 pages_from_pool, own_pages);
220 }
221 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
222
223 #ifdef CONFIG_BLOCK
osd_req_op_extent_osd_data_bio(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_bio_iter * bio_pos,u32 bio_length)224 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
225 unsigned int which,
226 struct ceph_bio_iter *bio_pos,
227 u32 bio_length)
228 {
229 struct ceph_osd_data *osd_data;
230
231 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
232 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
233 }
234 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
235 #endif /* CONFIG_BLOCK */
236
osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request * osd_req,unsigned int which,struct bio_vec * bvecs,u32 num_bvecs,u32 bytes)237 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
238 unsigned int which,
239 struct bio_vec *bvecs, u32 num_bvecs,
240 u32 bytes)
241 {
242 struct ceph_osd_data *osd_data;
243 struct ceph_bvec_iter it = {
244 .bvecs = bvecs,
245 .iter = { .bi_size = bytes },
246 };
247
248 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
249 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
250 }
251 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
252
osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_bvec_iter * bvec_pos)253 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
254 unsigned int which,
255 struct ceph_bvec_iter *bvec_pos)
256 {
257 struct ceph_osd_data *osd_data;
258
259 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
260 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
261 }
262 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
263
264 /**
265 * osd_req_op_extent_osd_iter - Set up an operation with an iterator buffer
266 * @osd_req: The request to set up
267 * @which: Index of the operation in which to set the iter
268 * @iter: The buffer iterator
269 */
osd_req_op_extent_osd_iter(struct ceph_osd_request * osd_req,unsigned int which,struct iov_iter * iter)270 void osd_req_op_extent_osd_iter(struct ceph_osd_request *osd_req,
271 unsigned int which, struct iov_iter *iter)
272 {
273 struct ceph_osd_data *osd_data;
274
275 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
276 ceph_osd_iter_init(osd_data, iter);
277 }
278 EXPORT_SYMBOL(osd_req_op_extent_osd_iter);
279
osd_req_op_cls_request_info_pagelist(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_pagelist * pagelist)280 static void osd_req_op_cls_request_info_pagelist(
281 struct ceph_osd_request *osd_req,
282 unsigned int which, struct ceph_pagelist *pagelist)
283 {
284 struct ceph_osd_data *osd_data;
285
286 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
287 ceph_osd_data_pagelist_init(osd_data, pagelist);
288 }
289
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)290 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
291 unsigned int which, struct page **pages, u64 length,
292 u32 alignment, bool pages_from_pool, bool own_pages)
293 {
294 struct ceph_osd_data *osd_data;
295
296 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
297 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
298 pages_from_pool, own_pages);
299 osd_req->r_ops[which].cls.indata_len += length;
300 osd_req->r_ops[which].indata_len += length;
301 }
302 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
303
osd_req_op_cls_request_data_bvecs(struct ceph_osd_request * osd_req,unsigned int which,struct bio_vec * bvecs,u32 num_bvecs,u32 bytes)304 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
305 unsigned int which,
306 struct bio_vec *bvecs, u32 num_bvecs,
307 u32 bytes)
308 {
309 struct ceph_osd_data *osd_data;
310 struct ceph_bvec_iter it = {
311 .bvecs = bvecs,
312 .iter = { .bi_size = bytes },
313 };
314
315 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
316 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
317 osd_req->r_ops[which].cls.indata_len += bytes;
318 osd_req->r_ops[which].indata_len += bytes;
319 }
320 EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
321
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)322 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
323 unsigned int which, struct page **pages, u64 length,
324 u32 alignment, bool pages_from_pool, bool own_pages)
325 {
326 struct ceph_osd_data *osd_data;
327
328 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
329 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
330 pages_from_pool, own_pages);
331 }
332 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
333
ceph_osd_data_length(struct ceph_osd_data * osd_data)334 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
335 {
336 switch (osd_data->type) {
337 case CEPH_OSD_DATA_TYPE_NONE:
338 return 0;
339 case CEPH_OSD_DATA_TYPE_PAGES:
340 return osd_data->length;
341 case CEPH_OSD_DATA_TYPE_PAGELIST:
342 return (u64)osd_data->pagelist->length;
343 #ifdef CONFIG_BLOCK
344 case CEPH_OSD_DATA_TYPE_BIO:
345 return (u64)osd_data->bio_length;
346 #endif /* CONFIG_BLOCK */
347 case CEPH_OSD_DATA_TYPE_BVECS:
348 return osd_data->bvec_pos.iter.bi_size;
349 case CEPH_OSD_DATA_TYPE_ITER:
350 return iov_iter_count(&osd_data->iter);
351 default:
352 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
353 return 0;
354 }
355 }
356
ceph_osd_data_release(struct ceph_osd_data * osd_data)357 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
358 {
359 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
360 int num_pages;
361
362 num_pages = calc_pages_for((u64)osd_data->alignment,
363 (u64)osd_data->length);
364 ceph_release_page_vector(osd_data->pages, num_pages);
365 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
366 ceph_pagelist_release(osd_data->pagelist);
367 }
368 ceph_osd_data_init(osd_data);
369 }
370
osd_req_op_data_release(struct ceph_osd_request * osd_req,unsigned int which)371 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
372 unsigned int which)
373 {
374 struct ceph_osd_req_op *op;
375
376 BUG_ON(which >= osd_req->r_num_ops);
377 op = &osd_req->r_ops[which];
378
379 switch (op->op) {
380 case CEPH_OSD_OP_READ:
381 case CEPH_OSD_OP_SPARSE_READ:
382 case CEPH_OSD_OP_WRITE:
383 case CEPH_OSD_OP_WRITEFULL:
384 kfree(op->extent.sparse_ext);
385 ceph_osd_data_release(&op->extent.osd_data);
386 break;
387 case CEPH_OSD_OP_CALL:
388 ceph_osd_data_release(&op->cls.request_info);
389 ceph_osd_data_release(&op->cls.request_data);
390 ceph_osd_data_release(&op->cls.response_data);
391 break;
392 case CEPH_OSD_OP_SETXATTR:
393 case CEPH_OSD_OP_CMPXATTR:
394 ceph_osd_data_release(&op->xattr.osd_data);
395 break;
396 case CEPH_OSD_OP_STAT:
397 ceph_osd_data_release(&op->raw_data_in);
398 break;
399 case CEPH_OSD_OP_NOTIFY_ACK:
400 ceph_osd_data_release(&op->notify_ack.request_data);
401 break;
402 case CEPH_OSD_OP_NOTIFY:
403 ceph_osd_data_release(&op->notify.request_data);
404 ceph_osd_data_release(&op->notify.response_data);
405 break;
406 case CEPH_OSD_OP_LIST_WATCHERS:
407 ceph_osd_data_release(&op->list_watchers.response_data);
408 break;
409 case CEPH_OSD_OP_COPY_FROM2:
410 ceph_osd_data_release(&op->copy_from.osd_data);
411 break;
412 default:
413 break;
414 }
415 }
416
417 /*
418 * Assumes @t is zero-initialized.
419 */
target_init(struct ceph_osd_request_target * t)420 static void target_init(struct ceph_osd_request_target *t)
421 {
422 ceph_oid_init(&t->base_oid);
423 ceph_oloc_init(&t->base_oloc);
424 ceph_oid_init(&t->target_oid);
425 ceph_oloc_init(&t->target_oloc);
426
427 ceph_osds_init(&t->acting);
428 ceph_osds_init(&t->up);
429 t->size = -1;
430 t->min_size = -1;
431
432 t->osd = CEPH_HOMELESS_OSD;
433 }
434
target_copy(struct ceph_osd_request_target * dest,const struct ceph_osd_request_target * src)435 static void target_copy(struct ceph_osd_request_target *dest,
436 const struct ceph_osd_request_target *src)
437 {
438 ceph_oid_copy(&dest->base_oid, &src->base_oid);
439 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
440 ceph_oid_copy(&dest->target_oid, &src->target_oid);
441 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
442
443 dest->pgid = src->pgid; /* struct */
444 dest->spgid = src->spgid; /* struct */
445 dest->pg_num = src->pg_num;
446 dest->pg_num_mask = src->pg_num_mask;
447 ceph_osds_copy(&dest->acting, &src->acting);
448 ceph_osds_copy(&dest->up, &src->up);
449 dest->size = src->size;
450 dest->min_size = src->min_size;
451 dest->sort_bitwise = src->sort_bitwise;
452 dest->recovery_deletes = src->recovery_deletes;
453
454 dest->flags = src->flags;
455 dest->used_replica = src->used_replica;
456 dest->paused = src->paused;
457
458 dest->epoch = src->epoch;
459 dest->last_force_resend = src->last_force_resend;
460
461 dest->osd = src->osd;
462 }
463
target_destroy(struct ceph_osd_request_target * t)464 static void target_destroy(struct ceph_osd_request_target *t)
465 {
466 ceph_oid_destroy(&t->base_oid);
467 ceph_oloc_destroy(&t->base_oloc);
468 ceph_oid_destroy(&t->target_oid);
469 ceph_oloc_destroy(&t->target_oloc);
470 }
471
472 /*
473 * requests
474 */
request_release_checks(struct ceph_osd_request * req)475 static void request_release_checks(struct ceph_osd_request *req)
476 {
477 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
478 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
479 WARN_ON(!list_empty(&req->r_private_item));
480 WARN_ON(req->r_osd);
481 }
482
ceph_osdc_release_request(struct kref * kref)483 static void ceph_osdc_release_request(struct kref *kref)
484 {
485 struct ceph_osd_request *req = container_of(kref,
486 struct ceph_osd_request, r_kref);
487 unsigned int which;
488
489 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
490 req->r_request, req->r_reply);
491 request_release_checks(req);
492
493 if (req->r_request)
494 ceph_msg_put(req->r_request);
495 if (req->r_reply)
496 ceph_msg_put(req->r_reply);
497
498 for (which = 0; which < req->r_num_ops; which++)
499 osd_req_op_data_release(req, which);
500
501 target_destroy(&req->r_t);
502 ceph_put_snap_context(req->r_snapc);
503
504 if (req->r_mempool)
505 mempool_free(req, req->r_osdc->req_mempool);
506 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
507 kmem_cache_free(ceph_osd_request_cache, req);
508 else
509 kfree(req);
510 }
511
ceph_osdc_get_request(struct ceph_osd_request * req)512 void ceph_osdc_get_request(struct ceph_osd_request *req)
513 {
514 dout("%s %p (was %d)\n", __func__, req,
515 kref_read(&req->r_kref));
516 kref_get(&req->r_kref);
517 }
518 EXPORT_SYMBOL(ceph_osdc_get_request);
519
ceph_osdc_put_request(struct ceph_osd_request * req)520 void ceph_osdc_put_request(struct ceph_osd_request *req)
521 {
522 if (req) {
523 dout("%s %p (was %d)\n", __func__, req,
524 kref_read(&req->r_kref));
525 kref_put(&req->r_kref, ceph_osdc_release_request);
526 }
527 }
528 EXPORT_SYMBOL(ceph_osdc_put_request);
529
request_init(struct ceph_osd_request * req)530 static void request_init(struct ceph_osd_request *req)
531 {
532 /* req only, each op is zeroed in osd_req_op_init() */
533 memset(req, 0, sizeof(*req));
534
535 kref_init(&req->r_kref);
536 init_completion(&req->r_completion);
537 RB_CLEAR_NODE(&req->r_node);
538 RB_CLEAR_NODE(&req->r_mc_node);
539 INIT_LIST_HEAD(&req->r_private_item);
540
541 target_init(&req->r_t);
542 }
543
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)544 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
545 struct ceph_snap_context *snapc,
546 unsigned int num_ops,
547 bool use_mempool,
548 gfp_t gfp_flags)
549 {
550 struct ceph_osd_request *req;
551
552 if (use_mempool) {
553 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
554 req = mempool_alloc(osdc->req_mempool, gfp_flags);
555 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
556 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
557 } else {
558 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
559 req = kmalloc_flex(*req, r_ops, num_ops, gfp_flags);
560 }
561 if (unlikely(!req))
562 return NULL;
563
564 request_init(req);
565 req->r_osdc = osdc;
566 req->r_mempool = use_mempool;
567 req->r_num_ops = num_ops;
568 req->r_snapid = CEPH_NOSNAP;
569 req->r_snapc = ceph_get_snap_context(snapc);
570
571 dout("%s req %p\n", __func__, req);
572 return req;
573 }
574 EXPORT_SYMBOL(ceph_osdc_alloc_request);
575
ceph_oloc_encoding_size(const struct ceph_object_locator * oloc)576 static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
577 {
578 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
579 }
580
__ceph_osdc_alloc_messages(struct ceph_osd_request * req,gfp_t gfp,int num_request_data_items,int num_reply_data_items)581 static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
582 int num_request_data_items,
583 int num_reply_data_items)
584 {
585 struct ceph_osd_client *osdc = req->r_osdc;
586 struct ceph_msg *msg;
587 int msg_size;
588
589 WARN_ON(req->r_request || req->r_reply);
590 WARN_ON(ceph_oid_empty(&req->r_base_oid));
591 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
592
593 /* create request message */
594 msg_size = CEPH_ENCODING_START_BLK_LEN +
595 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
596 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
597 msg_size += CEPH_ENCODING_START_BLK_LEN +
598 sizeof(struct ceph_osd_reqid); /* reqid */
599 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
600 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
601 msg_size += CEPH_ENCODING_START_BLK_LEN +
602 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
603 msg_size += 4 + req->r_base_oid.name_len; /* oid */
604 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
605 msg_size += 8; /* snapid */
606 msg_size += 8; /* snap_seq */
607 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
608 msg_size += 4 + 8; /* retry_attempt, features */
609
610 if (req->r_mempool)
611 msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
612 num_request_data_items);
613 else
614 msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
615 num_request_data_items, gfp, true);
616 if (!msg)
617 return -ENOMEM;
618
619 memset(msg->front.iov_base, 0, msg->front.iov_len);
620 req->r_request = msg;
621
622 /* create reply message */
623 msg_size = OSD_OPREPLY_FRONT_LEN;
624 msg_size += req->r_base_oid.name_len;
625 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
626
627 if (req->r_mempool)
628 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
629 num_reply_data_items);
630 else
631 msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
632 num_reply_data_items, gfp, true);
633 if (!msg)
634 return -ENOMEM;
635
636 req->r_reply = msg;
637
638 return 0;
639 }
640
osd_req_opcode_valid(u16 opcode)641 static bool osd_req_opcode_valid(u16 opcode)
642 {
643 switch (opcode) {
644 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
645 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
646 #undef GENERATE_CASE
647 default:
648 return false;
649 }
650 }
651
get_num_data_items(struct ceph_osd_request * req,int * num_request_data_items,int * num_reply_data_items)652 static void get_num_data_items(struct ceph_osd_request *req,
653 int *num_request_data_items,
654 int *num_reply_data_items)
655 {
656 struct ceph_osd_req_op *op;
657
658 *num_request_data_items = 0;
659 *num_reply_data_items = 0;
660
661 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
662 switch (op->op) {
663 /* request */
664 case CEPH_OSD_OP_WRITE:
665 case CEPH_OSD_OP_WRITEFULL:
666 case CEPH_OSD_OP_SETXATTR:
667 case CEPH_OSD_OP_CMPXATTR:
668 case CEPH_OSD_OP_NOTIFY_ACK:
669 case CEPH_OSD_OP_COPY_FROM2:
670 *num_request_data_items += 1;
671 break;
672
673 /* reply */
674 case CEPH_OSD_OP_STAT:
675 case CEPH_OSD_OP_READ:
676 case CEPH_OSD_OP_SPARSE_READ:
677 case CEPH_OSD_OP_LIST_WATCHERS:
678 *num_reply_data_items += 1;
679 break;
680
681 /* both */
682 case CEPH_OSD_OP_NOTIFY:
683 *num_request_data_items += 1;
684 *num_reply_data_items += 1;
685 break;
686 case CEPH_OSD_OP_CALL:
687 *num_request_data_items += 2;
688 *num_reply_data_items += 1;
689 break;
690
691 default:
692 WARN_ON(!osd_req_opcode_valid(op->op));
693 break;
694 }
695 }
696 }
697
698 /*
699 * oid, oloc and OSD op opcode(s) must be filled in before this function
700 * is called.
701 */
ceph_osdc_alloc_messages(struct ceph_osd_request * req,gfp_t gfp)702 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
703 {
704 int num_request_data_items, num_reply_data_items;
705
706 get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
707 return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
708 num_reply_data_items);
709 }
710 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
711
712 /*
713 * This is an osd op init function for opcodes that have no data or
714 * other information associated with them. It also serves as a
715 * common init routine for all the other init functions, below.
716 */
717 struct ceph_osd_req_op *
osd_req_op_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode,u32 flags)718 osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
719 u16 opcode, u32 flags)
720 {
721 struct ceph_osd_req_op *op;
722
723 BUG_ON(which >= osd_req->r_num_ops);
724 BUG_ON(!osd_req_opcode_valid(opcode));
725
726 op = &osd_req->r_ops[which];
727 memset(op, 0, sizeof (*op));
728 op->op = opcode;
729 op->flags = flags;
730
731 return op;
732 }
733 EXPORT_SYMBOL(osd_req_op_init);
734
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)735 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
736 unsigned int which, u16 opcode,
737 u64 offset, u64 length,
738 u64 truncate_size, u32 truncate_seq)
739 {
740 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
741 opcode, 0);
742 size_t payload_len = 0;
743
744 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
745 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
746 opcode != CEPH_OSD_OP_TRUNCATE && opcode != CEPH_OSD_OP_SPARSE_READ);
747
748 op->extent.offset = offset;
749 op->extent.length = length;
750 op->extent.truncate_size = truncate_size;
751 op->extent.truncate_seq = truncate_seq;
752 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
753 payload_len += length;
754
755 op->indata_len = payload_len;
756 }
757 EXPORT_SYMBOL(osd_req_op_extent_init);
758
osd_req_op_extent_update(struct ceph_osd_request * osd_req,unsigned int which,u64 length)759 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
760 unsigned int which, u64 length)
761 {
762 struct ceph_osd_req_op *op;
763 u64 previous;
764
765 BUG_ON(which >= osd_req->r_num_ops);
766 op = &osd_req->r_ops[which];
767 previous = op->extent.length;
768
769 if (length == previous)
770 return; /* Nothing to do */
771 BUG_ON(length > previous);
772
773 op->extent.length = length;
774 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
775 op->indata_len -= previous - length;
776 }
777 EXPORT_SYMBOL(osd_req_op_extent_update);
778
osd_req_op_extent_dup_last(struct ceph_osd_request * osd_req,unsigned int which,u64 offset_inc)779 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
780 unsigned int which, u64 offset_inc)
781 {
782 struct ceph_osd_req_op *op, *prev_op;
783
784 BUG_ON(which + 1 >= osd_req->r_num_ops);
785
786 prev_op = &osd_req->r_ops[which];
787 op = osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
788 /* dup previous one */
789 op->indata_len = prev_op->indata_len;
790 op->outdata_len = prev_op->outdata_len;
791 op->extent = prev_op->extent;
792 /* adjust offset */
793 op->extent.offset += offset_inc;
794 op->extent.length -= offset_inc;
795
796 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
797 op->indata_len -= offset_inc;
798 }
799 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
800
osd_req_op_cls_init(struct ceph_osd_request * osd_req,unsigned int which,const char * class,const char * method)801 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
802 const char *class, const char *method)
803 {
804 struct ceph_osd_req_op *op;
805 struct ceph_pagelist *pagelist;
806 size_t payload_len = 0;
807 size_t size;
808 int ret;
809
810 op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
811
812 pagelist = ceph_pagelist_alloc(GFP_NOFS);
813 if (!pagelist)
814 return -ENOMEM;
815
816 op->cls.class_name = class;
817 size = strlen(class);
818 BUG_ON(size > (size_t) U8_MAX);
819 op->cls.class_len = size;
820 ret = ceph_pagelist_append(pagelist, class, size);
821 if (ret)
822 goto err_pagelist_free;
823 payload_len += size;
824
825 op->cls.method_name = method;
826 size = strlen(method);
827 BUG_ON(size > (size_t) U8_MAX);
828 op->cls.method_len = size;
829 ret = ceph_pagelist_append(pagelist, method, size);
830 if (ret)
831 goto err_pagelist_free;
832 payload_len += size;
833
834 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
835 op->indata_len = payload_len;
836 return 0;
837
838 err_pagelist_free:
839 ceph_pagelist_release(pagelist);
840 return ret;
841 }
842 EXPORT_SYMBOL(osd_req_op_cls_init);
843
osd_req_op_xattr_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode,const char * name,const void * value,size_t size,u8 cmp_op,u8 cmp_mode)844 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
845 u16 opcode, const char *name, const void *value,
846 size_t size, u8 cmp_op, u8 cmp_mode)
847 {
848 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
849 opcode, 0);
850 struct ceph_pagelist *pagelist;
851 size_t payload_len;
852 int ret;
853
854 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
855
856 pagelist = ceph_pagelist_alloc(GFP_NOFS);
857 if (!pagelist)
858 return -ENOMEM;
859
860 payload_len = strlen(name);
861 op->xattr.name_len = payload_len;
862 ret = ceph_pagelist_append(pagelist, name, payload_len);
863 if (ret)
864 goto err_pagelist_free;
865
866 op->xattr.value_len = size;
867 ret = ceph_pagelist_append(pagelist, value, size);
868 if (ret)
869 goto err_pagelist_free;
870 payload_len += size;
871
872 op->xattr.cmp_op = cmp_op;
873 op->xattr.cmp_mode = cmp_mode;
874
875 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
876 op->indata_len = payload_len;
877 return 0;
878
879 err_pagelist_free:
880 ceph_pagelist_release(pagelist);
881 return ret;
882 }
883 EXPORT_SYMBOL(osd_req_op_xattr_init);
884
885 /*
886 * @watch_opcode: CEPH_OSD_WATCH_OP_*
887 */
osd_req_op_watch_init(struct ceph_osd_request * req,int which,u8 watch_opcode,u64 cookie,u32 gen)888 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
889 u8 watch_opcode, u64 cookie, u32 gen)
890 {
891 struct ceph_osd_req_op *op;
892
893 op = osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
894 op->watch.cookie = cookie;
895 op->watch.op = watch_opcode;
896 op->watch.gen = gen;
897 }
898
899 /*
900 * prot_ver, timeout and notify payload (may be empty) should already be
901 * encoded in @request_pl
902 */
osd_req_op_notify_init(struct ceph_osd_request * req,int which,u64 cookie,struct ceph_pagelist * request_pl)903 static void osd_req_op_notify_init(struct ceph_osd_request *req, int which,
904 u64 cookie, struct ceph_pagelist *request_pl)
905 {
906 struct ceph_osd_req_op *op;
907
908 op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
909 op->notify.cookie = cookie;
910
911 ceph_osd_data_pagelist_init(&op->notify.request_data, request_pl);
912 op->indata_len = request_pl->length;
913 }
914
915 /*
916 * @flags: CEPH_OSD_OP_ALLOC_HINT_FLAG_*
917 */
osd_req_op_alloc_hint_init(struct ceph_osd_request * osd_req,unsigned int which,u64 expected_object_size,u64 expected_write_size,u32 flags)918 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
919 unsigned int which,
920 u64 expected_object_size,
921 u64 expected_write_size,
922 u32 flags)
923 {
924 struct ceph_osd_req_op *op;
925
926 op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_SETALLOCHINT, 0);
927 op->alloc_hint.expected_object_size = expected_object_size;
928 op->alloc_hint.expected_write_size = expected_write_size;
929 op->alloc_hint.flags = flags;
930
931 /*
932 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
933 * not worth a feature bit. Set FAILOK per-op flag to make
934 * sure older osds don't trip over an unsupported opcode.
935 */
936 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
937 }
938 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
939
ceph_osdc_msg_data_add(struct ceph_msg * msg,struct ceph_osd_data * osd_data)940 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
941 struct ceph_osd_data *osd_data)
942 {
943 u64 length = ceph_osd_data_length(osd_data);
944
945 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
946 BUG_ON(length > (u64) SIZE_MAX);
947 if (length)
948 ceph_msg_data_add_pages(msg, osd_data->pages,
949 length, osd_data->alignment, false);
950 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
951 BUG_ON(!length);
952 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
953 #ifdef CONFIG_BLOCK
954 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
955 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
956 #endif
957 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
958 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
959 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_ITER) {
960 ceph_msg_data_add_iter(msg, &osd_data->iter);
961 } else {
962 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
963 }
964 }
965
osd_req_encode_op(struct ceph_osd_op * dst,const struct ceph_osd_req_op * src)966 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
967 const struct ceph_osd_req_op *src)
968 {
969 switch (src->op) {
970 case CEPH_OSD_OP_STAT:
971 break;
972 case CEPH_OSD_OP_READ:
973 case CEPH_OSD_OP_SPARSE_READ:
974 case CEPH_OSD_OP_WRITE:
975 case CEPH_OSD_OP_WRITEFULL:
976 case CEPH_OSD_OP_ZERO:
977 case CEPH_OSD_OP_TRUNCATE:
978 dst->extent.offset = cpu_to_le64(src->extent.offset);
979 dst->extent.length = cpu_to_le64(src->extent.length);
980 dst->extent.truncate_size =
981 cpu_to_le64(src->extent.truncate_size);
982 dst->extent.truncate_seq =
983 cpu_to_le32(src->extent.truncate_seq);
984 break;
985 case CEPH_OSD_OP_CALL:
986 dst->cls.class_len = src->cls.class_len;
987 dst->cls.method_len = src->cls.method_len;
988 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
989 break;
990 case CEPH_OSD_OP_WATCH:
991 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
992 dst->watch.ver = cpu_to_le64(0);
993 dst->watch.op = src->watch.op;
994 dst->watch.gen = cpu_to_le32(src->watch.gen);
995 break;
996 case CEPH_OSD_OP_NOTIFY_ACK:
997 break;
998 case CEPH_OSD_OP_NOTIFY:
999 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
1000 break;
1001 case CEPH_OSD_OP_LIST_WATCHERS:
1002 break;
1003 case CEPH_OSD_OP_SETALLOCHINT:
1004 dst->alloc_hint.expected_object_size =
1005 cpu_to_le64(src->alloc_hint.expected_object_size);
1006 dst->alloc_hint.expected_write_size =
1007 cpu_to_le64(src->alloc_hint.expected_write_size);
1008 dst->alloc_hint.flags = cpu_to_le32(src->alloc_hint.flags);
1009 break;
1010 case CEPH_OSD_OP_SETXATTR:
1011 case CEPH_OSD_OP_CMPXATTR:
1012 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1013 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1014 dst->xattr.cmp_op = src->xattr.cmp_op;
1015 dst->xattr.cmp_mode = src->xattr.cmp_mode;
1016 break;
1017 case CEPH_OSD_OP_CREATE:
1018 case CEPH_OSD_OP_DELETE:
1019 break;
1020 case CEPH_OSD_OP_COPY_FROM2:
1021 dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
1022 dst->copy_from.src_version =
1023 cpu_to_le64(src->copy_from.src_version);
1024 dst->copy_from.flags = src->copy_from.flags;
1025 dst->copy_from.src_fadvise_flags =
1026 cpu_to_le32(src->copy_from.src_fadvise_flags);
1027 break;
1028 case CEPH_OSD_OP_ASSERT_VER:
1029 dst->assert_ver.unused = cpu_to_le64(0);
1030 dst->assert_ver.ver = cpu_to_le64(src->assert_ver.ver);
1031 break;
1032 default:
1033 pr_err("unsupported osd opcode %s\n",
1034 ceph_osd_op_name(src->op));
1035 WARN_ON(1);
1036
1037 return 0;
1038 }
1039
1040 dst->op = cpu_to_le16(src->op);
1041 dst->flags = cpu_to_le32(src->flags);
1042 dst->payload_len = cpu_to_le32(src->indata_len);
1043
1044 return src->indata_len;
1045 }
1046
1047 /*
1048 * build new request AND message, calculate layout, and adjust file
1049 * extent as needed.
1050 *
1051 * if the file was recently truncated, we include information about its
1052 * old and new size so that the object can be updated appropriately. (we
1053 * avoid synchronously deleting truncated objects because it's slow.)
1054 */
ceph_osdc_new_request(struct ceph_osd_client * osdc,struct ceph_file_layout * layout,struct ceph_vino vino,u64 off,u64 * plen,unsigned int which,int num_ops,int opcode,int flags,struct ceph_snap_context * snapc,u32 truncate_seq,u64 truncate_size,bool use_mempool)1055 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
1056 struct ceph_file_layout *layout,
1057 struct ceph_vino vino,
1058 u64 off, u64 *plen,
1059 unsigned int which, int num_ops,
1060 int opcode, int flags,
1061 struct ceph_snap_context *snapc,
1062 u32 truncate_seq,
1063 u64 truncate_size,
1064 bool use_mempool)
1065 {
1066 struct ceph_osd_request *req;
1067 u64 objnum = 0;
1068 u64 objoff = 0;
1069 u64 objlen = 0;
1070 int r;
1071
1072 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1073 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1074 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE &&
1075 opcode != CEPH_OSD_OP_SPARSE_READ);
1076
1077 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1078 GFP_NOFS);
1079 if (!req) {
1080 r = -ENOMEM;
1081 goto fail;
1082 }
1083
1084 /* calculate max write size */
1085 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
1086 if (r)
1087 goto fail;
1088
1089 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1090 osd_req_op_init(req, which, opcode, 0);
1091 } else {
1092 u32 object_size = layout->object_size;
1093 u32 object_base = off - objoff;
1094 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1095 if (truncate_size <= object_base) {
1096 truncate_size = 0;
1097 } else {
1098 truncate_size -= object_base;
1099 if (truncate_size > object_size)
1100 truncate_size = object_size;
1101 }
1102 }
1103 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1104 truncate_size, truncate_seq);
1105 }
1106
1107 req->r_base_oloc.pool = layout->pool_id;
1108 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1109 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1110 req->r_flags = flags | osdc->client->options->read_from_replica;
1111
1112 req->r_snapid = vino.snap;
1113 if (flags & CEPH_OSD_FLAG_WRITE)
1114 req->r_data_offset = off;
1115
1116 if (num_ops > 1) {
1117 int num_req_ops, num_rep_ops;
1118
1119 /*
1120 * If this is a multi-op write request, assume that we'll need
1121 * request ops. If it's a multi-op read then assume we'll need
1122 * reply ops. Anything else and call it -EINVAL.
1123 */
1124 if (flags & CEPH_OSD_FLAG_WRITE) {
1125 num_req_ops = num_ops;
1126 num_rep_ops = 0;
1127 } else if (flags & CEPH_OSD_FLAG_READ) {
1128 num_req_ops = 0;
1129 num_rep_ops = num_ops;
1130 } else {
1131 r = -EINVAL;
1132 goto fail;
1133 }
1134
1135 r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_req_ops,
1136 num_rep_ops);
1137 } else {
1138 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1139 }
1140 if (r)
1141 goto fail;
1142
1143 return req;
1144
1145 fail:
1146 ceph_osdc_put_request(req);
1147 return ERR_PTR(r);
1148 }
1149 EXPORT_SYMBOL(ceph_osdc_new_request);
1150
__ceph_alloc_sparse_ext_map(struct ceph_osd_req_op * op,int cnt)1151 int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt)
1152 {
1153 WARN_ON(op->op != CEPH_OSD_OP_SPARSE_READ);
1154
1155 op->extent.sparse_ext_cnt = cnt;
1156 op->extent.sparse_ext = kmalloc_objs(*op->extent.sparse_ext, cnt,
1157 GFP_NOFS);
1158 if (!op->extent.sparse_ext)
1159 return -ENOMEM;
1160 return 0;
1161 }
1162 EXPORT_SYMBOL(__ceph_alloc_sparse_ext_map);
1163
1164 /*
1165 * We keep osd requests in an rbtree, sorted by ->r_tid.
1166 */
DEFINE_RB_FUNCS(request,struct ceph_osd_request,r_tid,r_node)1167 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
1168 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
1169
1170 /*
1171 * Call @fn on each OSD request as long as @fn returns 0.
1172 */
1173 static void for_each_request(struct ceph_osd_client *osdc,
1174 int (*fn)(struct ceph_osd_request *req, void *arg),
1175 void *arg)
1176 {
1177 struct rb_node *n, *p;
1178
1179 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1180 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1181
1182 for (p = rb_first(&osd->o_requests); p; ) {
1183 struct ceph_osd_request *req =
1184 rb_entry(p, struct ceph_osd_request, r_node);
1185
1186 p = rb_next(p);
1187 if (fn(req, arg))
1188 return;
1189 }
1190 }
1191
1192 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1193 struct ceph_osd_request *req =
1194 rb_entry(p, struct ceph_osd_request, r_node);
1195
1196 p = rb_next(p);
1197 if (fn(req, arg))
1198 return;
1199 }
1200 }
1201
osd_homeless(struct ceph_osd * osd)1202 static bool osd_homeless(struct ceph_osd *osd)
1203 {
1204 return osd->o_osd == CEPH_HOMELESS_OSD;
1205 }
1206
osd_registered(struct ceph_osd * osd)1207 static bool osd_registered(struct ceph_osd *osd)
1208 {
1209 verify_osdc_locked(osd->o_osdc);
1210
1211 return !RB_EMPTY_NODE(&osd->o_node);
1212 }
1213
1214 /*
1215 * Assumes @osd is zero-initialized.
1216 */
osd_init(struct ceph_osd * osd)1217 static void osd_init(struct ceph_osd *osd)
1218 {
1219 refcount_set(&osd->o_ref, 1);
1220 RB_CLEAR_NODE(&osd->o_node);
1221 spin_lock_init(&osd->o_requests_lock);
1222 osd->o_requests = RB_ROOT;
1223 osd->o_linger_requests = RB_ROOT;
1224 osd->o_backoff_mappings = RB_ROOT;
1225 osd->o_backoffs_by_id = RB_ROOT;
1226 INIT_LIST_HEAD(&osd->o_osd_lru);
1227 INIT_LIST_HEAD(&osd->o_keepalive_item);
1228 osd->o_incarnation = 1;
1229 mutex_init(&osd->lock);
1230 }
1231
ceph_init_sparse_read(struct ceph_sparse_read * sr)1232 static void ceph_init_sparse_read(struct ceph_sparse_read *sr)
1233 {
1234 kfree(sr->sr_extent);
1235 memset(sr, '\0', sizeof(*sr));
1236 sr->sr_state = CEPH_SPARSE_READ_HDR;
1237 }
1238
osd_cleanup(struct ceph_osd * osd)1239 static void osd_cleanup(struct ceph_osd *osd)
1240 {
1241 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1242 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1243 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1244 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1245 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1246 WARN_ON(!list_empty(&osd->o_osd_lru));
1247 WARN_ON(!list_empty(&osd->o_keepalive_item));
1248
1249 ceph_init_sparse_read(&osd->o_sparse_read);
1250
1251 if (osd->o_auth.authorizer) {
1252 WARN_ON(osd_homeless(osd));
1253 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1254 }
1255 }
1256
1257 /*
1258 * Track open sessions with osds.
1259 */
create_osd(struct ceph_osd_client * osdc,int onum)1260 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1261 {
1262 struct ceph_osd *osd;
1263
1264 WARN_ON(onum == CEPH_HOMELESS_OSD);
1265
1266 osd = kzalloc_obj(*osd, GFP_NOIO | __GFP_NOFAIL);
1267 osd_init(osd);
1268 osd->o_osdc = osdc;
1269 osd->o_osd = onum;
1270 osd->o_sparse_op_idx = -1;
1271
1272 ceph_init_sparse_read(&osd->o_sparse_read);
1273
1274 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1275
1276 return osd;
1277 }
1278
get_osd(struct ceph_osd * osd)1279 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1280 {
1281 if (refcount_inc_not_zero(&osd->o_ref)) {
1282 dout("get_osd %p -> %d\n", osd, refcount_read(&osd->o_ref));
1283 return osd;
1284 } else {
1285 dout("get_osd %p FAIL\n", osd);
1286 return NULL;
1287 }
1288 }
1289
put_osd(struct ceph_osd * osd)1290 static void put_osd(struct ceph_osd *osd)
1291 {
1292 dout("put_osd %p -> %d\n", osd, refcount_read(&osd->o_ref) - 1);
1293 if (refcount_dec_and_test(&osd->o_ref)) {
1294 osd_cleanup(osd);
1295 kfree(osd);
1296 }
1297 }
1298
DEFINE_RB_FUNCS(osd,struct ceph_osd,o_osd,o_node)1299 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1300
1301 static void __move_osd_to_lru(struct ceph_osd *osd)
1302 {
1303 struct ceph_osd_client *osdc = osd->o_osdc;
1304
1305 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1306 BUG_ON(!list_empty(&osd->o_osd_lru));
1307
1308 spin_lock(&osdc->osd_lru_lock);
1309 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1310 spin_unlock(&osdc->osd_lru_lock);
1311
1312 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1313 }
1314
maybe_move_osd_to_lru(struct ceph_osd * osd)1315 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1316 {
1317 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1318 RB_EMPTY_ROOT(&osd->o_linger_requests))
1319 __move_osd_to_lru(osd);
1320 }
1321
__remove_osd_from_lru(struct ceph_osd * osd)1322 static void __remove_osd_from_lru(struct ceph_osd *osd)
1323 {
1324 struct ceph_osd_client *osdc = osd->o_osdc;
1325
1326 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1327
1328 spin_lock(&osdc->osd_lru_lock);
1329 if (!list_empty(&osd->o_osd_lru))
1330 list_del_init(&osd->o_osd_lru);
1331 spin_unlock(&osdc->osd_lru_lock);
1332 }
1333
1334 /*
1335 * Close the connection and assign any leftover requests to the
1336 * homeless session.
1337 */
close_osd(struct ceph_osd * osd)1338 static void close_osd(struct ceph_osd *osd)
1339 {
1340 struct ceph_osd_client *osdc = osd->o_osdc;
1341 struct rb_node *n;
1342
1343 verify_osdc_wrlocked(osdc);
1344 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1345
1346 ceph_con_close(&osd->o_con);
1347
1348 for (n = rb_first(&osd->o_requests); n; ) {
1349 struct ceph_osd_request *req =
1350 rb_entry(n, struct ceph_osd_request, r_node);
1351
1352 n = rb_next(n); /* unlink_request() */
1353
1354 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1355 unlink_request(osd, req);
1356 link_request(&osdc->homeless_osd, req);
1357 }
1358 for (n = rb_first(&osd->o_linger_requests); n; ) {
1359 struct ceph_osd_linger_request *lreq =
1360 rb_entry(n, struct ceph_osd_linger_request, node);
1361
1362 n = rb_next(n); /* unlink_linger() */
1363
1364 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1365 lreq->linger_id);
1366 unlink_linger(osd, lreq);
1367 link_linger(&osdc->homeless_osd, lreq);
1368 }
1369 clear_backoffs(osd);
1370
1371 __remove_osd_from_lru(osd);
1372 erase_osd(&osdc->osds, osd);
1373 put_osd(osd);
1374 }
1375
1376 /*
1377 * reset osd connect
1378 */
reopen_osd(struct ceph_osd * osd)1379 static int reopen_osd(struct ceph_osd *osd)
1380 {
1381 struct ceph_entity_addr *peer_addr;
1382
1383 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1384
1385 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1386 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1387 close_osd(osd);
1388 return -ENODEV;
1389 }
1390
1391 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1392 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1393 !ceph_con_opened(&osd->o_con)) {
1394 struct rb_node *n;
1395
1396 dout("osd addr hasn't changed and connection never opened, "
1397 "letting msgr retry\n");
1398 /* touch each r_stamp for handle_timeout()'s benfit */
1399 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1400 struct ceph_osd_request *req =
1401 rb_entry(n, struct ceph_osd_request, r_node);
1402 req->r_stamp = jiffies;
1403 }
1404
1405 return -EAGAIN;
1406 }
1407
1408 ceph_con_close(&osd->o_con);
1409 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1410 osd->o_incarnation++;
1411
1412 return 0;
1413 }
1414
lookup_create_osd(struct ceph_osd_client * osdc,int o,bool wrlocked)1415 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1416 bool wrlocked)
1417 {
1418 struct ceph_osd *osd;
1419
1420 if (wrlocked)
1421 verify_osdc_wrlocked(osdc);
1422 else
1423 verify_osdc_locked(osdc);
1424
1425 if (o != CEPH_HOMELESS_OSD)
1426 osd = lookup_osd(&osdc->osds, o);
1427 else
1428 osd = &osdc->homeless_osd;
1429 if (!osd) {
1430 if (!wrlocked)
1431 return ERR_PTR(-EAGAIN);
1432
1433 osd = create_osd(osdc, o);
1434 insert_osd(&osdc->osds, osd);
1435 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1436 &osdc->osdmap->osd_addr[osd->o_osd]);
1437 }
1438
1439 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1440 return osd;
1441 }
1442
1443 /*
1444 * Create request <-> OSD session relation.
1445 *
1446 * @req has to be assigned a tid, @osd may be homeless.
1447 */
link_request(struct ceph_osd * osd,struct ceph_osd_request * req)1448 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1449 {
1450 verify_osd_locked(osd);
1451 WARN_ON(!req->r_tid || req->r_osd);
1452 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1453 req, req->r_tid);
1454
1455 if (!osd_homeless(osd))
1456 __remove_osd_from_lru(osd);
1457 else
1458 atomic_inc(&osd->o_osdc->num_homeless);
1459
1460 get_osd(osd);
1461 spin_lock(&osd->o_requests_lock);
1462 insert_request(&osd->o_requests, req);
1463 spin_unlock(&osd->o_requests_lock);
1464 req->r_osd = osd;
1465 }
1466
unlink_request(struct ceph_osd * osd,struct ceph_osd_request * req)1467 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1468 {
1469 verify_osd_locked(osd);
1470 WARN_ON(req->r_osd != osd);
1471 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1472 req, req->r_tid);
1473
1474 req->r_osd = NULL;
1475 spin_lock(&osd->o_requests_lock);
1476 erase_request(&osd->o_requests, req);
1477 spin_unlock(&osd->o_requests_lock);
1478 put_osd(osd);
1479
1480 if (!osd_homeless(osd))
1481 maybe_move_osd_to_lru(osd);
1482 else
1483 atomic_dec(&osd->o_osdc->num_homeless);
1484 }
1485
__pool_full(struct ceph_pg_pool_info * pi)1486 static bool __pool_full(struct ceph_pg_pool_info *pi)
1487 {
1488 return pi->flags & CEPH_POOL_FLAG_FULL;
1489 }
1490
have_pool_full(struct ceph_osd_client * osdc)1491 static bool have_pool_full(struct ceph_osd_client *osdc)
1492 {
1493 struct rb_node *n;
1494
1495 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1496 struct ceph_pg_pool_info *pi =
1497 rb_entry(n, struct ceph_pg_pool_info, node);
1498
1499 if (__pool_full(pi))
1500 return true;
1501 }
1502
1503 return false;
1504 }
1505
pool_full(struct ceph_osd_client * osdc,s64 pool_id)1506 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1507 {
1508 struct ceph_pg_pool_info *pi;
1509
1510 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1511 if (!pi)
1512 return false;
1513
1514 return __pool_full(pi);
1515 }
1516
1517 /*
1518 * Returns whether a request should be blocked from being sent
1519 * based on the current osdmap and osd_client settings.
1520 */
target_should_be_paused(struct ceph_osd_client * osdc,const struct ceph_osd_request_target * t,struct ceph_pg_pool_info * pi)1521 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1522 const struct ceph_osd_request_target *t,
1523 struct ceph_pg_pool_info *pi)
1524 {
1525 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1526 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1527 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1528 __pool_full(pi);
1529
1530 WARN_ON(pi->id != t->target_oloc.pool);
1531 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1532 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1533 (osdc->osdmap->epoch < osdc->epoch_barrier);
1534 }
1535
pick_random_replica(const struct ceph_osds * acting)1536 static int pick_random_replica(const struct ceph_osds *acting)
1537 {
1538 int i = get_random_u32_below(acting->size);
1539
1540 dout("%s picked osd%d, primary osd%d\n", __func__,
1541 acting->osds[i], acting->primary);
1542 return i;
1543 }
1544
1545 /*
1546 * Picks the closest replica based on client's location given by
1547 * crush_location option. Prefers the primary if the locality is
1548 * the same.
1549 */
pick_closest_replica(struct ceph_osd_client * osdc,const struct ceph_osds * acting)1550 static int pick_closest_replica(struct ceph_osd_client *osdc,
1551 const struct ceph_osds *acting)
1552 {
1553 struct ceph_options *opt = osdc->client->options;
1554 int best_i, best_locality;
1555 int i = 0, locality;
1556
1557 do {
1558 locality = ceph_get_crush_locality(osdc->osdmap,
1559 acting->osds[i],
1560 &opt->crush_locs);
1561 if (i == 0 ||
1562 (locality >= 0 && best_locality < 0) ||
1563 (locality >= 0 && best_locality >= 0 &&
1564 locality < best_locality)) {
1565 best_i = i;
1566 best_locality = locality;
1567 }
1568 } while (++i < acting->size);
1569
1570 dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
1571 acting->osds[best_i], best_locality, acting->primary);
1572 return best_i;
1573 }
1574
1575 enum calc_target_result {
1576 CALC_TARGET_NO_ACTION = 0,
1577 CALC_TARGET_NEED_RESEND,
1578 CALC_TARGET_POOL_DNE,
1579 };
1580
calc_target(struct ceph_osd_client * osdc,struct ceph_osd_request_target * t,bool any_change)1581 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1582 struct ceph_osd_request_target *t,
1583 bool any_change)
1584 {
1585 struct ceph_pg_pool_info *pi;
1586 struct ceph_pg pgid, last_pgid;
1587 struct ceph_osds up, acting;
1588 bool should_be_paused;
1589 bool is_read = t->flags & CEPH_OSD_FLAG_READ;
1590 bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
1591 bool force_resend = false;
1592 bool unpaused = false;
1593 bool legacy_change = false;
1594 bool split = false;
1595 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1596 bool recovery_deletes = ceph_osdmap_flag(osdc,
1597 CEPH_OSDMAP_RECOVERY_DELETES);
1598 enum calc_target_result ct_res;
1599
1600 t->epoch = osdc->osdmap->epoch;
1601 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1602 if (!pi) {
1603 t->osd = CEPH_HOMELESS_OSD;
1604 ct_res = CALC_TARGET_POOL_DNE;
1605 goto out;
1606 }
1607
1608 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1609 if (t->last_force_resend < pi->last_force_request_resend) {
1610 t->last_force_resend = pi->last_force_request_resend;
1611 force_resend = true;
1612 } else if (t->last_force_resend == 0) {
1613 force_resend = true;
1614 }
1615 }
1616
1617 /* apply tiering */
1618 ceph_oid_copy(&t->target_oid, &t->base_oid);
1619 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1620 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1621 if (is_read && pi->read_tier >= 0)
1622 t->target_oloc.pool = pi->read_tier;
1623 if (is_write && pi->write_tier >= 0)
1624 t->target_oloc.pool = pi->write_tier;
1625
1626 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1627 if (!pi) {
1628 t->osd = CEPH_HOMELESS_OSD;
1629 ct_res = CALC_TARGET_POOL_DNE;
1630 goto out;
1631 }
1632 }
1633
1634 __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
1635 last_pgid.pool = pgid.pool;
1636 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1637
1638 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1639 if (any_change &&
1640 ceph_is_new_interval(&t->acting,
1641 &acting,
1642 &t->up,
1643 &up,
1644 t->size,
1645 pi->size,
1646 t->min_size,
1647 pi->min_size,
1648 t->pg_num,
1649 pi->pg_num,
1650 t->sort_bitwise,
1651 sort_bitwise,
1652 t->recovery_deletes,
1653 recovery_deletes,
1654 &last_pgid))
1655 force_resend = true;
1656
1657 should_be_paused = target_should_be_paused(osdc, t, pi);
1658 if (t->paused && !should_be_paused) {
1659 unpaused = true;
1660 }
1661 if (t->paused != should_be_paused) {
1662 dout("%s t %p paused %d -> %d\n", __func__, t, t->paused,
1663 should_be_paused);
1664 t->paused = should_be_paused;
1665 }
1666
1667 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1668 ceph_osds_changed(&t->acting, &acting,
1669 t->used_replica || any_change);
1670 if (t->pg_num)
1671 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1672
1673 if (legacy_change || force_resend || split) {
1674 t->pgid = pgid; /* struct */
1675 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1676 ceph_osds_copy(&t->acting, &acting);
1677 ceph_osds_copy(&t->up, &up);
1678 t->size = pi->size;
1679 t->min_size = pi->min_size;
1680 t->pg_num = pi->pg_num;
1681 t->pg_num_mask = pi->pg_num_mask;
1682 t->sort_bitwise = sort_bitwise;
1683 t->recovery_deletes = recovery_deletes;
1684
1685 if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
1686 CEPH_OSD_FLAG_LOCALIZE_READS)) &&
1687 !is_write && pi->type == CEPH_POOL_TYPE_REP &&
1688 acting.size > 1) {
1689 int pos;
1690
1691 WARN_ON(!is_read || acting.osds[0] != acting.primary);
1692 if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
1693 pos = pick_random_replica(&acting);
1694 } else {
1695 pos = pick_closest_replica(osdc, &acting);
1696 }
1697 t->osd = acting.osds[pos];
1698 t->used_replica = pos > 0;
1699 } else {
1700 t->osd = acting.primary;
1701 t->used_replica = false;
1702 }
1703 }
1704
1705 if (unpaused || legacy_change || force_resend || split)
1706 ct_res = CALC_TARGET_NEED_RESEND;
1707 else
1708 ct_res = CALC_TARGET_NO_ACTION;
1709
1710 out:
1711 dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1712 legacy_change, force_resend, split, ct_res, t->osd);
1713 return ct_res;
1714 }
1715
alloc_spg_mapping(void)1716 static struct ceph_spg_mapping *alloc_spg_mapping(void)
1717 {
1718 struct ceph_spg_mapping *spg;
1719
1720 spg = kmalloc_obj(*spg, GFP_NOIO);
1721 if (!spg)
1722 return NULL;
1723
1724 RB_CLEAR_NODE(&spg->node);
1725 spg->backoffs = RB_ROOT;
1726 return spg;
1727 }
1728
free_spg_mapping(struct ceph_spg_mapping * spg)1729 static void free_spg_mapping(struct ceph_spg_mapping *spg)
1730 {
1731 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1732 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1733
1734 kfree(spg);
1735 }
1736
1737 /*
1738 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1739 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1740 * defined only within a specific spgid; it does not pass anything to
1741 * children on split, or to another primary.
1742 */
DEFINE_RB_FUNCS2(spg_mapping,struct ceph_spg_mapping,spgid,ceph_spg_compare,RB_BYPTR,const struct ceph_spg *,node)1743 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1744 RB_BYPTR, const struct ceph_spg *, node)
1745
1746 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1747 {
1748 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1749 }
1750
hoid_get_effective_key(const struct ceph_hobject_id * hoid,void ** pkey,size_t * pkey_len)1751 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1752 void **pkey, size_t *pkey_len)
1753 {
1754 if (hoid->key_len) {
1755 *pkey = hoid->key;
1756 *pkey_len = hoid->key_len;
1757 } else {
1758 *pkey = hoid->oid;
1759 *pkey_len = hoid->oid_len;
1760 }
1761 }
1762
compare_names(const void * name1,size_t name1_len,const void * name2,size_t name2_len)1763 static int compare_names(const void *name1, size_t name1_len,
1764 const void *name2, size_t name2_len)
1765 {
1766 int ret;
1767
1768 ret = memcmp(name1, name2, min(name1_len, name2_len));
1769 if (!ret) {
1770 if (name1_len < name2_len)
1771 ret = -1;
1772 else if (name1_len > name2_len)
1773 ret = 1;
1774 }
1775 return ret;
1776 }
1777
hoid_compare(const struct ceph_hobject_id * lhs,const struct ceph_hobject_id * rhs)1778 static int hoid_compare(const struct ceph_hobject_id *lhs,
1779 const struct ceph_hobject_id *rhs)
1780 {
1781 void *effective_key1, *effective_key2;
1782 size_t effective_key1_len, effective_key2_len;
1783 int ret;
1784
1785 if (lhs->is_max < rhs->is_max)
1786 return -1;
1787 if (lhs->is_max > rhs->is_max)
1788 return 1;
1789
1790 if (lhs->pool < rhs->pool)
1791 return -1;
1792 if (lhs->pool > rhs->pool)
1793 return 1;
1794
1795 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1796 return -1;
1797 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1798 return 1;
1799
1800 ret = compare_names(lhs->nspace, lhs->nspace_len,
1801 rhs->nspace, rhs->nspace_len);
1802 if (ret)
1803 return ret;
1804
1805 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1806 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1807 ret = compare_names(effective_key1, effective_key1_len,
1808 effective_key2, effective_key2_len);
1809 if (ret)
1810 return ret;
1811
1812 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1813 if (ret)
1814 return ret;
1815
1816 if (lhs->snapid < rhs->snapid)
1817 return -1;
1818 if (lhs->snapid > rhs->snapid)
1819 return 1;
1820
1821 return 0;
1822 }
1823
1824 /*
1825 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1826 * compat stuff here.
1827 *
1828 * Assumes @hoid is zero-initialized.
1829 */
decode_hoid(void ** p,void * end,struct ceph_hobject_id * hoid)1830 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1831 {
1832 u8 struct_v;
1833 u32 struct_len;
1834 int ret;
1835
1836 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1837 &struct_len);
1838 if (ret)
1839 return ret;
1840
1841 if (struct_v < 4) {
1842 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1843 goto e_inval;
1844 }
1845
1846 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1847 GFP_NOIO);
1848 if (IS_ERR(hoid->key)) {
1849 ret = PTR_ERR(hoid->key);
1850 hoid->key = NULL;
1851 return ret;
1852 }
1853
1854 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1855 GFP_NOIO);
1856 if (IS_ERR(hoid->oid)) {
1857 ret = PTR_ERR(hoid->oid);
1858 hoid->oid = NULL;
1859 return ret;
1860 }
1861
1862 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1863 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1864 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1865
1866 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1867 GFP_NOIO);
1868 if (IS_ERR(hoid->nspace)) {
1869 ret = PTR_ERR(hoid->nspace);
1870 hoid->nspace = NULL;
1871 return ret;
1872 }
1873
1874 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1875
1876 ceph_hoid_build_hash_cache(hoid);
1877 return 0;
1878
1879 e_inval:
1880 return -EINVAL;
1881 }
1882
hoid_encoding_size(const struct ceph_hobject_id * hoid)1883 static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1884 {
1885 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1886 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1887 }
1888
encode_hoid(void ** p,void * end,const struct ceph_hobject_id * hoid)1889 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1890 {
1891 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1892 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1893 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1894 ceph_encode_64(p, hoid->snapid);
1895 ceph_encode_32(p, hoid->hash);
1896 ceph_encode_8(p, hoid->is_max);
1897 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1898 ceph_encode_64(p, hoid->pool);
1899 }
1900
free_hoid(struct ceph_hobject_id * hoid)1901 static void free_hoid(struct ceph_hobject_id *hoid)
1902 {
1903 if (hoid) {
1904 kfree(hoid->key);
1905 kfree(hoid->oid);
1906 kfree(hoid->nspace);
1907 kfree(hoid);
1908 }
1909 }
1910
alloc_backoff(void)1911 static struct ceph_osd_backoff *alloc_backoff(void)
1912 {
1913 struct ceph_osd_backoff *backoff;
1914
1915 backoff = kzalloc_obj(*backoff, GFP_NOIO);
1916 if (!backoff)
1917 return NULL;
1918
1919 RB_CLEAR_NODE(&backoff->spg_node);
1920 RB_CLEAR_NODE(&backoff->id_node);
1921 return backoff;
1922 }
1923
free_backoff(struct ceph_osd_backoff * backoff)1924 static void free_backoff(struct ceph_osd_backoff *backoff)
1925 {
1926 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1927 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1928
1929 free_hoid(backoff->begin);
1930 free_hoid(backoff->end);
1931 kfree(backoff);
1932 }
1933
1934 /*
1935 * Within a specific spgid, backoffs are managed by ->begin hoid.
1936 */
1937 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1938 RB_BYVAL, spg_node);
1939
lookup_containing_backoff(struct rb_root * root,const struct ceph_hobject_id * hoid)1940 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1941 const struct ceph_hobject_id *hoid)
1942 {
1943 struct rb_node *n = root->rb_node;
1944
1945 while (n) {
1946 struct ceph_osd_backoff *cur =
1947 rb_entry(n, struct ceph_osd_backoff, spg_node);
1948 int cmp;
1949
1950 cmp = hoid_compare(hoid, cur->begin);
1951 if (cmp < 0) {
1952 n = n->rb_left;
1953 } else if (cmp > 0) {
1954 if (hoid_compare(hoid, cur->end) < 0)
1955 return cur;
1956
1957 n = n->rb_right;
1958 } else {
1959 return cur;
1960 }
1961 }
1962
1963 return NULL;
1964 }
1965
1966 /*
1967 * Each backoff has a unique id within its OSD session.
1968 */
DEFINE_RB_FUNCS(backoff_by_id,struct ceph_osd_backoff,id,id_node)1969 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1970
1971 static void clear_backoffs(struct ceph_osd *osd)
1972 {
1973 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1974 struct ceph_spg_mapping *spg =
1975 rb_entry(rb_first(&osd->o_backoff_mappings),
1976 struct ceph_spg_mapping, node);
1977
1978 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1979 struct ceph_osd_backoff *backoff =
1980 rb_entry(rb_first(&spg->backoffs),
1981 struct ceph_osd_backoff, spg_node);
1982
1983 erase_backoff(&spg->backoffs, backoff);
1984 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1985 free_backoff(backoff);
1986 }
1987 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1988 free_spg_mapping(spg);
1989 }
1990 }
1991
1992 /*
1993 * Set up a temporary, non-owning view into @t.
1994 */
hoid_fill_from_target(struct ceph_hobject_id * hoid,const struct ceph_osd_request_target * t)1995 static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1996 const struct ceph_osd_request_target *t)
1997 {
1998 hoid->key = NULL;
1999 hoid->key_len = 0;
2000 hoid->oid = t->target_oid.name;
2001 hoid->oid_len = t->target_oid.name_len;
2002 hoid->snapid = CEPH_NOSNAP;
2003 hoid->hash = t->pgid.seed;
2004 hoid->is_max = false;
2005 if (t->target_oloc.pool_ns) {
2006 hoid->nspace = t->target_oloc.pool_ns->str;
2007 hoid->nspace_len = t->target_oloc.pool_ns->len;
2008 } else {
2009 hoid->nspace = NULL;
2010 hoid->nspace_len = 0;
2011 }
2012 hoid->pool = t->target_oloc.pool;
2013 ceph_hoid_build_hash_cache(hoid);
2014 }
2015
should_plug_request(struct ceph_osd_request * req)2016 static bool should_plug_request(struct ceph_osd_request *req)
2017 {
2018 struct ceph_osd *osd = req->r_osd;
2019 struct ceph_spg_mapping *spg;
2020 struct ceph_osd_backoff *backoff;
2021 struct ceph_hobject_id hoid;
2022
2023 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
2024 if (!spg)
2025 return false;
2026
2027 hoid_fill_from_target(&hoid, &req->r_t);
2028 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
2029 if (!backoff)
2030 return false;
2031
2032 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
2033 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
2034 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
2035 return true;
2036 }
2037
2038 /*
2039 * Keep get_num_data_items() in sync with this function.
2040 */
setup_request_data(struct ceph_osd_request * req)2041 static void setup_request_data(struct ceph_osd_request *req)
2042 {
2043 struct ceph_msg *request_msg = req->r_request;
2044 struct ceph_msg *reply_msg = req->r_reply;
2045 struct ceph_osd_req_op *op;
2046
2047 if (req->r_request->num_data_items || req->r_reply->num_data_items)
2048 return;
2049
2050 WARN_ON(request_msg->data_length || reply_msg->data_length);
2051 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
2052 switch (op->op) {
2053 /* request */
2054 case CEPH_OSD_OP_WRITE:
2055 case CEPH_OSD_OP_WRITEFULL:
2056 WARN_ON(op->indata_len != op->extent.length);
2057 ceph_osdc_msg_data_add(request_msg,
2058 &op->extent.osd_data);
2059 break;
2060 case CEPH_OSD_OP_SETXATTR:
2061 case CEPH_OSD_OP_CMPXATTR:
2062 WARN_ON(op->indata_len != op->xattr.name_len +
2063 op->xattr.value_len);
2064 ceph_osdc_msg_data_add(request_msg,
2065 &op->xattr.osd_data);
2066 break;
2067 case CEPH_OSD_OP_NOTIFY_ACK:
2068 ceph_osdc_msg_data_add(request_msg,
2069 &op->notify_ack.request_data);
2070 break;
2071 case CEPH_OSD_OP_COPY_FROM2:
2072 ceph_osdc_msg_data_add(request_msg,
2073 &op->copy_from.osd_data);
2074 break;
2075
2076 /* reply */
2077 case CEPH_OSD_OP_STAT:
2078 ceph_osdc_msg_data_add(reply_msg,
2079 &op->raw_data_in);
2080 break;
2081 case CEPH_OSD_OP_READ:
2082 case CEPH_OSD_OP_SPARSE_READ:
2083 ceph_osdc_msg_data_add(reply_msg,
2084 &op->extent.osd_data);
2085 break;
2086 case CEPH_OSD_OP_LIST_WATCHERS:
2087 ceph_osdc_msg_data_add(reply_msg,
2088 &op->list_watchers.response_data);
2089 break;
2090
2091 /* both */
2092 case CEPH_OSD_OP_CALL:
2093 WARN_ON(op->indata_len != op->cls.class_len +
2094 op->cls.method_len +
2095 op->cls.indata_len);
2096 ceph_osdc_msg_data_add(request_msg,
2097 &op->cls.request_info);
2098 /* optional, can be NONE */
2099 ceph_osdc_msg_data_add(request_msg,
2100 &op->cls.request_data);
2101 /* optional, can be NONE */
2102 ceph_osdc_msg_data_add(reply_msg,
2103 &op->cls.response_data);
2104 break;
2105 case CEPH_OSD_OP_NOTIFY:
2106 ceph_osdc_msg_data_add(request_msg,
2107 &op->notify.request_data);
2108 ceph_osdc_msg_data_add(reply_msg,
2109 &op->notify.response_data);
2110 break;
2111 }
2112 }
2113 }
2114
encode_pgid(void ** p,const struct ceph_pg * pgid)2115 static void encode_pgid(void **p, const struct ceph_pg *pgid)
2116 {
2117 ceph_encode_8(p, 1);
2118 ceph_encode_64(p, pgid->pool);
2119 ceph_encode_32(p, pgid->seed);
2120 ceph_encode_32(p, -1); /* preferred */
2121 }
2122
encode_spgid(void ** p,const struct ceph_spg * spgid)2123 static void encode_spgid(void **p, const struct ceph_spg *spgid)
2124 {
2125 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
2126 encode_pgid(p, &spgid->pgid);
2127 ceph_encode_8(p, spgid->shard);
2128 }
2129
encode_oloc(void ** p,void * end,const struct ceph_object_locator * oloc)2130 static void encode_oloc(void **p, void *end,
2131 const struct ceph_object_locator *oloc)
2132 {
2133 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
2134 ceph_encode_64(p, oloc->pool);
2135 ceph_encode_32(p, -1); /* preferred */
2136 ceph_encode_32(p, 0); /* key len */
2137 if (oloc->pool_ns)
2138 ceph_encode_string(p, end, oloc->pool_ns->str,
2139 oloc->pool_ns->len);
2140 else
2141 ceph_encode_32(p, 0);
2142 }
2143
encode_request_partial(struct ceph_osd_request * req,struct ceph_msg * msg)2144 static void encode_request_partial(struct ceph_osd_request *req,
2145 struct ceph_msg *msg)
2146 {
2147 void *p = msg->front.iov_base;
2148 void *const end = p + msg->front_alloc_len;
2149 u32 data_len = 0;
2150 int i;
2151
2152 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2153 /* snapshots aren't writeable */
2154 WARN_ON(req->r_snapid != CEPH_NOSNAP);
2155 } else {
2156 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2157 req->r_data_offset || req->r_snapc);
2158 }
2159
2160 setup_request_data(req);
2161
2162 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
2163 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2164 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2165 ceph_encode_32(&p, req->r_flags);
2166
2167 /* reqid */
2168 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
2169 memset(p, 0, sizeof(struct ceph_osd_reqid));
2170 p += sizeof(struct ceph_osd_reqid);
2171
2172 /* trace */
2173 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
2174 p += sizeof(struct ceph_blkin_trace_info);
2175
2176 ceph_encode_32(&p, 0); /* client_inc, always 0 */
2177 ceph_encode_timespec64(p, &req->r_mtime);
2178 p += sizeof(struct ceph_timespec);
2179
2180 encode_oloc(&p, end, &req->r_t.target_oloc);
2181 ceph_encode_string(&p, end, req->r_t.target_oid.name,
2182 req->r_t.target_oid.name_len);
2183
2184 /* ops, can imply data */
2185 ceph_encode_16(&p, req->r_num_ops);
2186 for (i = 0; i < req->r_num_ops; i++) {
2187 data_len += osd_req_encode_op(p, &req->r_ops[i]);
2188 p += sizeof(struct ceph_osd_op);
2189 }
2190
2191 ceph_encode_64(&p, req->r_snapid); /* snapid */
2192 if (req->r_snapc) {
2193 ceph_encode_64(&p, req->r_snapc->seq);
2194 ceph_encode_32(&p, req->r_snapc->num_snaps);
2195 for (i = 0; i < req->r_snapc->num_snaps; i++)
2196 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2197 } else {
2198 ceph_encode_64(&p, 0); /* snap_seq */
2199 ceph_encode_32(&p, 0); /* snaps len */
2200 }
2201
2202 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2203 BUG_ON(p > end - 8); /* space for features */
2204
2205 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2206 /* front_len is finalized in encode_request_finish() */
2207 msg->front.iov_len = p - msg->front.iov_base;
2208 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2209 msg->hdr.data_len = cpu_to_le32(data_len);
2210 /*
2211 * The header "data_off" is a hint to the receiver allowing it
2212 * to align received data into its buffers such that there's no
2213 * need to re-copy it before writing it to disk (direct I/O).
2214 */
2215 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2216
2217 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2218 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2219 }
2220
encode_request_finish(struct ceph_msg * msg)2221 static void encode_request_finish(struct ceph_msg *msg)
2222 {
2223 void *p = msg->front.iov_base;
2224 void *const partial_end = p + msg->front.iov_len;
2225 void *const end = p + msg->front_alloc_len;
2226
2227 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2228 /* luminous OSD -- encode features and be done */
2229 p = partial_end;
2230 ceph_encode_64(&p, msg->con->peer_features);
2231 } else {
2232 struct {
2233 char spgid[CEPH_ENCODING_START_BLK_LEN +
2234 CEPH_PGID_ENCODING_LEN + 1];
2235 __le32 hash;
2236 __le32 epoch;
2237 __le32 flags;
2238 char reqid[CEPH_ENCODING_START_BLK_LEN +
2239 sizeof(struct ceph_osd_reqid)];
2240 char trace[sizeof(struct ceph_blkin_trace_info)];
2241 __le32 client_inc;
2242 struct ceph_timespec mtime;
2243 } __packed head;
2244 struct ceph_pg pgid;
2245 void *oloc, *oid, *tail;
2246 int oloc_len, oid_len, tail_len;
2247 int len;
2248
2249 /*
2250 * Pre-luminous OSD -- reencode v8 into v4 using @head
2251 * as a temporary buffer. Encode the raw PG; the rest
2252 * is just a matter of moving oloc, oid and tail blobs
2253 * around.
2254 */
2255 memcpy(&head, p, sizeof(head));
2256 p += sizeof(head);
2257
2258 oloc = p;
2259 p += CEPH_ENCODING_START_BLK_LEN;
2260 pgid.pool = ceph_decode_64(&p);
2261 p += 4 + 4; /* preferred, key len */
2262 len = ceph_decode_32(&p);
2263 p += len; /* nspace */
2264 oloc_len = p - oloc;
2265
2266 oid = p;
2267 len = ceph_decode_32(&p);
2268 p += len;
2269 oid_len = p - oid;
2270
2271 tail = p;
2272 tail_len = partial_end - p;
2273
2274 p = msg->front.iov_base;
2275 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2276 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2277 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2278 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2279
2280 /* reassert_version */
2281 memset(p, 0, sizeof(struct ceph_eversion));
2282 p += sizeof(struct ceph_eversion);
2283
2284 BUG_ON(p >= oloc);
2285 memmove(p, oloc, oloc_len);
2286 p += oloc_len;
2287
2288 pgid.seed = le32_to_cpu(head.hash);
2289 encode_pgid(&p, &pgid); /* raw pg */
2290
2291 BUG_ON(p >= oid);
2292 memmove(p, oid, oid_len);
2293 p += oid_len;
2294
2295 /* tail -- ops, snapid, snapc, retry_attempt */
2296 BUG_ON(p >= tail);
2297 memmove(p, tail, tail_len);
2298 p += tail_len;
2299
2300 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2301 }
2302
2303 BUG_ON(p > end);
2304 msg->front.iov_len = p - msg->front.iov_base;
2305 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2306
2307 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2308 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2309 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2310 le16_to_cpu(msg->hdr.version));
2311 }
2312
2313 /*
2314 * @req has to be assigned a tid and registered.
2315 */
send_request(struct ceph_osd_request * req)2316 static void send_request(struct ceph_osd_request *req)
2317 {
2318 struct ceph_osd *osd = req->r_osd;
2319
2320 verify_osd_locked(osd);
2321 WARN_ON(osd->o_osd != req->r_t.osd);
2322
2323 /* backoff? */
2324 if (should_plug_request(req))
2325 return;
2326
2327 /*
2328 * We may have a previously queued request message hanging
2329 * around. Cancel it to avoid corrupting the msgr.
2330 */
2331 if (req->r_sent)
2332 ceph_msg_revoke(req->r_request);
2333
2334 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2335 if (req->r_attempts)
2336 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2337 else
2338 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2339
2340 encode_request_partial(req, req->r_request);
2341
2342 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
2343 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2344 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
2345 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2346 req->r_attempts);
2347
2348 req->r_t.paused = false;
2349 req->r_stamp = jiffies;
2350 req->r_attempts++;
2351
2352 req->r_sent = osd->o_incarnation;
2353 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2354 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
2355 }
2356
maybe_request_map(struct ceph_osd_client * osdc)2357 static void maybe_request_map(struct ceph_osd_client *osdc)
2358 {
2359 bool continuous = false;
2360
2361 verify_osdc_locked(osdc);
2362 WARN_ON(!osdc->osdmap->epoch);
2363
2364 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2365 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2366 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2367 dout("%s osdc %p continuous\n", __func__, osdc);
2368 continuous = true;
2369 } else {
2370 dout("%s osdc %p onetime\n", __func__, osdc);
2371 }
2372
2373 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2374 osdc->osdmap->epoch + 1, continuous))
2375 ceph_monc_renew_subs(&osdc->client->monc);
2376 }
2377
2378 static void complete_request(struct ceph_osd_request *req, int err);
2379 static void send_map_check(struct ceph_osd_request *req);
2380
__submit_request(struct ceph_osd_request * req,bool wrlocked)2381 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
2382 {
2383 struct ceph_osd_client *osdc = req->r_osdc;
2384 struct ceph_osd *osd;
2385 enum calc_target_result ct_res;
2386 int err = 0;
2387 bool need_send = false;
2388 bool promoted = false;
2389
2390 WARN_ON(req->r_tid);
2391 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2392
2393 again:
2394 ct_res = calc_target(osdc, &req->r_t, false);
2395 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2396 goto promote;
2397
2398 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2399 if (IS_ERR(osd)) {
2400 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2401 goto promote;
2402 }
2403
2404 if (osdc->abort_err) {
2405 dout("req %p abort_err %d\n", req, osdc->abort_err);
2406 err = osdc->abort_err;
2407 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2408 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2409 osdc->epoch_barrier);
2410 req->r_t.paused = true;
2411 maybe_request_map(osdc);
2412 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2413 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2414 dout("req %p pausewr\n", req);
2415 req->r_t.paused = true;
2416 maybe_request_map(osdc);
2417 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2418 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2419 dout("req %p pauserd\n", req);
2420 req->r_t.paused = true;
2421 maybe_request_map(osdc);
2422 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2423 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2424 CEPH_OSD_FLAG_FULL_FORCE)) &&
2425 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2426 pool_full(osdc, req->r_t.base_oloc.pool))) {
2427 dout("req %p full/pool_full\n", req);
2428 if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
2429 err = -ENOSPC;
2430 } else {
2431 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL))
2432 pr_warn_ratelimited("cluster is full (osdmap FULL)\n");
2433 else
2434 pr_warn_ratelimited("pool %lld is full or reached quota\n",
2435 req->r_t.base_oloc.pool);
2436 req->r_t.paused = true;
2437 maybe_request_map(osdc);
2438 }
2439 } else if (!osd_homeless(osd)) {
2440 need_send = true;
2441 } else {
2442 maybe_request_map(osdc);
2443 }
2444
2445 mutex_lock(&osd->lock);
2446 /*
2447 * Assign the tid atomically with send_request() to protect
2448 * multiple writes to the same object from racing with each
2449 * other, resulting in out of order ops on the OSDs.
2450 */
2451 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2452 link_request(osd, req);
2453 if (need_send)
2454 send_request(req);
2455 else if (err)
2456 complete_request(req, err);
2457 mutex_unlock(&osd->lock);
2458
2459 if (!err && ct_res == CALC_TARGET_POOL_DNE)
2460 send_map_check(req);
2461
2462 if (promoted)
2463 downgrade_write(&osdc->lock);
2464 return;
2465
2466 promote:
2467 up_read(&osdc->lock);
2468 down_write(&osdc->lock);
2469 wrlocked = true;
2470 promoted = true;
2471 goto again;
2472 }
2473
account_request(struct ceph_osd_request * req)2474 static void account_request(struct ceph_osd_request *req)
2475 {
2476 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2477 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
2478
2479 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
2480 atomic_inc(&req->r_osdc->num_requests);
2481
2482 req->r_start_stamp = jiffies;
2483 req->r_start_latency = ktime_get();
2484 }
2485
submit_request(struct ceph_osd_request * req,bool wrlocked)2486 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2487 {
2488 ceph_osdc_get_request(req);
2489 account_request(req);
2490 __submit_request(req, wrlocked);
2491 }
2492
finish_request(struct ceph_osd_request * req)2493 static void finish_request(struct ceph_osd_request *req)
2494 {
2495 struct ceph_osd_client *osdc = req->r_osdc;
2496
2497 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
2498 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2499
2500 req->r_end_latency = ktime_get();
2501
2502 if (req->r_osd) {
2503 ceph_init_sparse_read(&req->r_osd->o_sparse_read);
2504 unlink_request(req->r_osd, req);
2505 }
2506 atomic_dec(&osdc->num_requests);
2507
2508 /*
2509 * If an OSD has failed or returned and a request has been sent
2510 * twice, it's possible to get a reply and end up here while the
2511 * request message is queued for delivery. We will ignore the
2512 * reply, so not a big deal, but better to try and catch it.
2513 */
2514 ceph_msg_revoke(req->r_request);
2515 ceph_msg_revoke_incoming(req->r_reply);
2516 }
2517
__complete_request(struct ceph_osd_request * req)2518 static void __complete_request(struct ceph_osd_request *req)
2519 {
2520 dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2521 req->r_tid, req->r_callback, req->r_result);
2522
2523 if (req->r_callback)
2524 req->r_callback(req);
2525 complete_all(&req->r_completion);
2526 ceph_osdc_put_request(req);
2527 }
2528
complete_request_workfn(struct work_struct * work)2529 static void complete_request_workfn(struct work_struct *work)
2530 {
2531 struct ceph_osd_request *req =
2532 container_of(work, struct ceph_osd_request, r_complete_work);
2533
2534 __complete_request(req);
2535 }
2536
2537 /*
2538 * This is open-coded in handle_reply().
2539 */
complete_request(struct ceph_osd_request * req,int err)2540 static void complete_request(struct ceph_osd_request *req, int err)
2541 {
2542 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2543
2544 req->r_result = err;
2545 finish_request(req);
2546
2547 INIT_WORK(&req->r_complete_work, complete_request_workfn);
2548 queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
2549 }
2550
cancel_map_check(struct ceph_osd_request * req)2551 static void cancel_map_check(struct ceph_osd_request *req)
2552 {
2553 struct ceph_osd_client *osdc = req->r_osdc;
2554 struct ceph_osd_request *lookup_req;
2555
2556 verify_osdc_wrlocked(osdc);
2557
2558 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2559 if (!lookup_req)
2560 return;
2561
2562 WARN_ON(lookup_req != req);
2563 erase_request_mc(&osdc->map_checks, req);
2564 ceph_osdc_put_request(req);
2565 }
2566
cancel_request(struct ceph_osd_request * req)2567 static void cancel_request(struct ceph_osd_request *req)
2568 {
2569 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2570
2571 cancel_map_check(req);
2572 finish_request(req);
2573 complete_all(&req->r_completion);
2574 ceph_osdc_put_request(req);
2575 }
2576
abort_request(struct ceph_osd_request * req,int err)2577 static void abort_request(struct ceph_osd_request *req, int err)
2578 {
2579 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2580
2581 cancel_map_check(req);
2582 complete_request(req, err);
2583 }
2584
abort_fn(struct ceph_osd_request * req,void * arg)2585 static int abort_fn(struct ceph_osd_request *req, void *arg)
2586 {
2587 int err = *(int *)arg;
2588
2589 abort_request(req, err);
2590 return 0; /* continue iteration */
2591 }
2592
2593 /*
2594 * Abort all in-flight requests with @err and arrange for all future
2595 * requests to be failed immediately.
2596 */
ceph_osdc_abort_requests(struct ceph_osd_client * osdc,int err)2597 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2598 {
2599 dout("%s osdc %p err %d\n", __func__, osdc, err);
2600 down_write(&osdc->lock);
2601 for_each_request(osdc, abort_fn, &err);
2602 osdc->abort_err = err;
2603 up_write(&osdc->lock);
2604 }
2605 EXPORT_SYMBOL(ceph_osdc_abort_requests);
2606
ceph_osdc_clear_abort_err(struct ceph_osd_client * osdc)2607 void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
2608 {
2609 down_write(&osdc->lock);
2610 osdc->abort_err = 0;
2611 up_write(&osdc->lock);
2612 }
2613 EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
2614
update_epoch_barrier(struct ceph_osd_client * osdc,u32 eb)2615 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2616 {
2617 if (likely(eb > osdc->epoch_barrier)) {
2618 dout("updating epoch_barrier from %u to %u\n",
2619 osdc->epoch_barrier, eb);
2620 osdc->epoch_barrier = eb;
2621 /* Request map if we're not to the barrier yet */
2622 if (eb > osdc->osdmap->epoch)
2623 maybe_request_map(osdc);
2624 }
2625 }
2626
ceph_osdc_update_epoch_barrier(struct ceph_osd_client * osdc,u32 eb)2627 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2628 {
2629 down_read(&osdc->lock);
2630 if (unlikely(eb > osdc->epoch_barrier)) {
2631 up_read(&osdc->lock);
2632 down_write(&osdc->lock);
2633 update_epoch_barrier(osdc, eb);
2634 up_write(&osdc->lock);
2635 } else {
2636 up_read(&osdc->lock);
2637 }
2638 }
2639 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2640
2641 /*
2642 * We can end up releasing caps as a result of abort_request().
2643 * In that case, we probably want to ensure that the cap release message
2644 * has an updated epoch barrier in it, so set the epoch barrier prior to
2645 * aborting the first request.
2646 */
abort_on_full_fn(struct ceph_osd_request * req,void * arg)2647 static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2648 {
2649 struct ceph_osd_client *osdc = req->r_osdc;
2650 bool *victims = arg;
2651
2652 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2653 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2654 pool_full(osdc, req->r_t.base_oloc.pool))) {
2655 if (!*victims) {
2656 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2657 *victims = true;
2658 }
2659 abort_request(req, -ENOSPC);
2660 }
2661
2662 return 0; /* continue iteration */
2663 }
2664
2665 /*
2666 * Drop all pending requests that are stalled waiting on a full condition to
2667 * clear, and complete them with ENOSPC as the return code. Set the
2668 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2669 * cancelled.
2670 */
ceph_osdc_abort_on_full(struct ceph_osd_client * osdc)2671 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2672 {
2673 bool victims = false;
2674
2675 if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2676 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
2677 for_each_request(osdc, abort_on_full_fn, &victims);
2678 }
2679
check_pool_dne(struct ceph_osd_request * req)2680 static void check_pool_dne(struct ceph_osd_request *req)
2681 {
2682 struct ceph_osd_client *osdc = req->r_osdc;
2683 struct ceph_osdmap *map = osdc->osdmap;
2684
2685 verify_osdc_wrlocked(osdc);
2686 WARN_ON(!map->epoch);
2687
2688 if (req->r_attempts) {
2689 /*
2690 * We sent a request earlier, which means that
2691 * previously the pool existed, and now it does not
2692 * (i.e., it was deleted).
2693 */
2694 req->r_map_dne_bound = map->epoch;
2695 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2696 req->r_tid);
2697 } else {
2698 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2699 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2700 }
2701
2702 if (req->r_map_dne_bound) {
2703 if (map->epoch >= req->r_map_dne_bound) {
2704 /* we had a new enough map */
2705 pr_info_ratelimited("tid %llu pool does not exist\n",
2706 req->r_tid);
2707 complete_request(req, -ENOENT);
2708 }
2709 } else {
2710 send_map_check(req);
2711 }
2712 }
2713
map_check_cb(struct ceph_mon_generic_request * greq)2714 static void map_check_cb(struct ceph_mon_generic_request *greq)
2715 {
2716 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2717 struct ceph_osd_request *req;
2718 u64 tid = greq->private_data;
2719
2720 WARN_ON(greq->result || !greq->u.newest);
2721
2722 down_write(&osdc->lock);
2723 req = lookup_request_mc(&osdc->map_checks, tid);
2724 if (!req) {
2725 dout("%s tid %llu dne\n", __func__, tid);
2726 goto out_unlock;
2727 }
2728
2729 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2730 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2731 if (!req->r_map_dne_bound)
2732 req->r_map_dne_bound = greq->u.newest;
2733 erase_request_mc(&osdc->map_checks, req);
2734 check_pool_dne(req);
2735
2736 ceph_osdc_put_request(req);
2737 out_unlock:
2738 up_write(&osdc->lock);
2739 }
2740
send_map_check(struct ceph_osd_request * req)2741 static void send_map_check(struct ceph_osd_request *req)
2742 {
2743 struct ceph_osd_client *osdc = req->r_osdc;
2744 struct ceph_osd_request *lookup_req;
2745 int ret;
2746
2747 verify_osdc_wrlocked(osdc);
2748
2749 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2750 if (lookup_req) {
2751 WARN_ON(lookup_req != req);
2752 return;
2753 }
2754
2755 ceph_osdc_get_request(req);
2756 insert_request_mc(&osdc->map_checks, req);
2757 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2758 map_check_cb, req->r_tid);
2759 WARN_ON(ret);
2760 }
2761
2762 /*
2763 * lingering requests, watch/notify v2 infrastructure
2764 */
linger_release(struct kref * kref)2765 static void linger_release(struct kref *kref)
2766 {
2767 struct ceph_osd_linger_request *lreq =
2768 container_of(kref, struct ceph_osd_linger_request, kref);
2769
2770 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2771 lreq->reg_req, lreq->ping_req);
2772 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2773 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2774 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2775 WARN_ON(!list_empty(&lreq->scan_item));
2776 WARN_ON(!list_empty(&lreq->pending_lworks));
2777 WARN_ON(lreq->osd);
2778
2779 if (lreq->request_pl)
2780 ceph_pagelist_release(lreq->request_pl);
2781 if (lreq->notify_id_pages)
2782 ceph_release_page_vector(lreq->notify_id_pages, 1);
2783
2784 ceph_osdc_put_request(lreq->reg_req);
2785 ceph_osdc_put_request(lreq->ping_req);
2786 target_destroy(&lreq->t);
2787 kfree(lreq);
2788 }
2789
linger_put(struct ceph_osd_linger_request * lreq)2790 static void linger_put(struct ceph_osd_linger_request *lreq)
2791 {
2792 if (lreq)
2793 kref_put(&lreq->kref, linger_release);
2794 }
2795
2796 static struct ceph_osd_linger_request *
linger_get(struct ceph_osd_linger_request * lreq)2797 linger_get(struct ceph_osd_linger_request *lreq)
2798 {
2799 kref_get(&lreq->kref);
2800 return lreq;
2801 }
2802
2803 static struct ceph_osd_linger_request *
linger_alloc(struct ceph_osd_client * osdc)2804 linger_alloc(struct ceph_osd_client *osdc)
2805 {
2806 struct ceph_osd_linger_request *lreq;
2807
2808 lreq = kzalloc_obj(*lreq, GFP_NOIO);
2809 if (!lreq)
2810 return NULL;
2811
2812 kref_init(&lreq->kref);
2813 mutex_init(&lreq->lock);
2814 RB_CLEAR_NODE(&lreq->node);
2815 RB_CLEAR_NODE(&lreq->osdc_node);
2816 RB_CLEAR_NODE(&lreq->mc_node);
2817 INIT_LIST_HEAD(&lreq->scan_item);
2818 INIT_LIST_HEAD(&lreq->pending_lworks);
2819 init_completion(&lreq->reg_commit_wait);
2820 init_completion(&lreq->notify_finish_wait);
2821
2822 lreq->osdc = osdc;
2823 target_init(&lreq->t);
2824
2825 dout("%s lreq %p\n", __func__, lreq);
2826 return lreq;
2827 }
2828
DEFINE_RB_INSDEL_FUNCS(linger,struct ceph_osd_linger_request,linger_id,node)2829 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2830 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2831 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2832
2833 /*
2834 * Create linger request <-> OSD session relation.
2835 *
2836 * @lreq has to be registered, @osd may be homeless.
2837 */
2838 static void link_linger(struct ceph_osd *osd,
2839 struct ceph_osd_linger_request *lreq)
2840 {
2841 verify_osd_locked(osd);
2842 WARN_ON(!lreq->linger_id || lreq->osd);
2843 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2844 osd->o_osd, lreq, lreq->linger_id);
2845
2846 if (!osd_homeless(osd))
2847 __remove_osd_from_lru(osd);
2848 else
2849 atomic_inc(&osd->o_osdc->num_homeless);
2850
2851 get_osd(osd);
2852 insert_linger(&osd->o_linger_requests, lreq);
2853 lreq->osd = osd;
2854 }
2855
unlink_linger(struct ceph_osd * osd,struct ceph_osd_linger_request * lreq)2856 static void unlink_linger(struct ceph_osd *osd,
2857 struct ceph_osd_linger_request *lreq)
2858 {
2859 verify_osd_locked(osd);
2860 WARN_ON(lreq->osd != osd);
2861 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2862 osd->o_osd, lreq, lreq->linger_id);
2863
2864 lreq->osd = NULL;
2865 erase_linger(&osd->o_linger_requests, lreq);
2866 put_osd(osd);
2867
2868 if (!osd_homeless(osd))
2869 maybe_move_osd_to_lru(osd);
2870 else
2871 atomic_dec(&osd->o_osdc->num_homeless);
2872 }
2873
__linger_registered(struct ceph_osd_linger_request * lreq)2874 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2875 {
2876 verify_osdc_locked(lreq->osdc);
2877
2878 return !RB_EMPTY_NODE(&lreq->osdc_node);
2879 }
2880
linger_registered(struct ceph_osd_linger_request * lreq)2881 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2882 {
2883 struct ceph_osd_client *osdc = lreq->osdc;
2884 bool registered;
2885
2886 down_read(&osdc->lock);
2887 registered = __linger_registered(lreq);
2888 up_read(&osdc->lock);
2889
2890 return registered;
2891 }
2892
linger_register(struct ceph_osd_linger_request * lreq)2893 static void linger_register(struct ceph_osd_linger_request *lreq)
2894 {
2895 struct ceph_osd_client *osdc = lreq->osdc;
2896
2897 verify_osdc_wrlocked(osdc);
2898 WARN_ON(lreq->linger_id);
2899
2900 linger_get(lreq);
2901 lreq->linger_id = ++osdc->last_linger_id;
2902 insert_linger_osdc(&osdc->linger_requests, lreq);
2903 }
2904
linger_unregister(struct ceph_osd_linger_request * lreq)2905 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2906 {
2907 struct ceph_osd_client *osdc = lreq->osdc;
2908
2909 verify_osdc_wrlocked(osdc);
2910
2911 erase_linger_osdc(&osdc->linger_requests, lreq);
2912 linger_put(lreq);
2913 }
2914
cancel_linger_request(struct ceph_osd_request * req)2915 static void cancel_linger_request(struct ceph_osd_request *req)
2916 {
2917 struct ceph_osd_linger_request *lreq = req->r_priv;
2918
2919 WARN_ON(!req->r_linger);
2920 cancel_request(req);
2921 linger_put(lreq);
2922 }
2923
2924 struct linger_work {
2925 struct work_struct work;
2926 struct ceph_osd_linger_request *lreq;
2927 struct list_head pending_item;
2928 unsigned long queued_stamp;
2929
2930 union {
2931 struct {
2932 u64 notify_id;
2933 u64 notifier_id;
2934 void *payload; /* points into @msg front */
2935 size_t payload_len;
2936
2937 struct ceph_msg *msg; /* for ceph_msg_put() */
2938 } notify;
2939 struct {
2940 int err;
2941 } error;
2942 };
2943 };
2944
lwork_alloc(struct ceph_osd_linger_request * lreq,work_func_t workfn)2945 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2946 work_func_t workfn)
2947 {
2948 struct linger_work *lwork;
2949
2950 lwork = kzalloc_obj(*lwork, GFP_NOIO);
2951 if (!lwork)
2952 return NULL;
2953
2954 INIT_WORK(&lwork->work, workfn);
2955 INIT_LIST_HEAD(&lwork->pending_item);
2956 lwork->lreq = linger_get(lreq);
2957
2958 return lwork;
2959 }
2960
lwork_free(struct linger_work * lwork)2961 static void lwork_free(struct linger_work *lwork)
2962 {
2963 struct ceph_osd_linger_request *lreq = lwork->lreq;
2964
2965 mutex_lock(&lreq->lock);
2966 list_del(&lwork->pending_item);
2967 mutex_unlock(&lreq->lock);
2968
2969 linger_put(lreq);
2970 kfree(lwork);
2971 }
2972
lwork_queue(struct linger_work * lwork)2973 static void lwork_queue(struct linger_work *lwork)
2974 {
2975 struct ceph_osd_linger_request *lreq = lwork->lreq;
2976 struct ceph_osd_client *osdc = lreq->osdc;
2977
2978 verify_lreq_locked(lreq);
2979 WARN_ON(!list_empty(&lwork->pending_item));
2980
2981 lwork->queued_stamp = jiffies;
2982 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2983 queue_work(osdc->notify_wq, &lwork->work);
2984 }
2985
do_watch_notify(struct work_struct * w)2986 static void do_watch_notify(struct work_struct *w)
2987 {
2988 struct linger_work *lwork = container_of(w, struct linger_work, work);
2989 struct ceph_osd_linger_request *lreq = lwork->lreq;
2990
2991 if (!linger_registered(lreq)) {
2992 dout("%s lreq %p not registered\n", __func__, lreq);
2993 goto out;
2994 }
2995
2996 WARN_ON(!lreq->is_watch);
2997 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2998 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2999 lwork->notify.payload_len);
3000 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
3001 lwork->notify.notifier_id, lwork->notify.payload,
3002 lwork->notify.payload_len);
3003
3004 out:
3005 ceph_msg_put(lwork->notify.msg);
3006 lwork_free(lwork);
3007 }
3008
do_watch_error(struct work_struct * w)3009 static void do_watch_error(struct work_struct *w)
3010 {
3011 struct linger_work *lwork = container_of(w, struct linger_work, work);
3012 struct ceph_osd_linger_request *lreq = lwork->lreq;
3013
3014 if (!linger_registered(lreq)) {
3015 dout("%s lreq %p not registered\n", __func__, lreq);
3016 goto out;
3017 }
3018
3019 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
3020 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
3021
3022 out:
3023 lwork_free(lwork);
3024 }
3025
queue_watch_error(struct ceph_osd_linger_request * lreq)3026 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
3027 {
3028 struct linger_work *lwork;
3029
3030 lwork = lwork_alloc(lreq, do_watch_error);
3031 if (!lwork) {
3032 pr_err("failed to allocate error-lwork\n");
3033 return;
3034 }
3035
3036 lwork->error.err = lreq->last_error;
3037 lwork_queue(lwork);
3038 }
3039
linger_reg_commit_complete(struct ceph_osd_linger_request * lreq,int result)3040 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
3041 int result)
3042 {
3043 if (!completion_done(&lreq->reg_commit_wait)) {
3044 lreq->reg_commit_error = (result <= 0 ? result : 0);
3045 complete_all(&lreq->reg_commit_wait);
3046 }
3047 }
3048
linger_commit_cb(struct ceph_osd_request * req)3049 static void linger_commit_cb(struct ceph_osd_request *req)
3050 {
3051 struct ceph_osd_linger_request *lreq = req->r_priv;
3052
3053 mutex_lock(&lreq->lock);
3054 if (req != lreq->reg_req) {
3055 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
3056 __func__, lreq, lreq->linger_id, req, lreq->reg_req);
3057 goto out;
3058 }
3059
3060 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
3061 lreq->linger_id, req->r_result);
3062 linger_reg_commit_complete(lreq, req->r_result);
3063 lreq->committed = true;
3064
3065 if (!lreq->is_watch) {
3066 struct ceph_osd_data *osd_data =
3067 osd_req_op_data(req, 0, notify, response_data);
3068 void *p = page_address(osd_data->pages[0]);
3069
3070 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
3071 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
3072
3073 /* make note of the notify_id */
3074 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
3075 lreq->notify_id = ceph_decode_64(&p);
3076 dout("lreq %p notify_id %llu\n", lreq,
3077 lreq->notify_id);
3078 } else {
3079 dout("lreq %p no notify_id\n", lreq);
3080 }
3081 }
3082
3083 out:
3084 mutex_unlock(&lreq->lock);
3085 linger_put(lreq);
3086 }
3087
normalize_watch_error(int err)3088 static int normalize_watch_error(int err)
3089 {
3090 /*
3091 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
3092 * notification and a failure to reconnect because we raced with
3093 * the delete appear the same to the user.
3094 */
3095 if (err == -ENOENT)
3096 err = -ENOTCONN;
3097
3098 return err;
3099 }
3100
linger_reconnect_cb(struct ceph_osd_request * req)3101 static void linger_reconnect_cb(struct ceph_osd_request *req)
3102 {
3103 struct ceph_osd_linger_request *lreq = req->r_priv;
3104
3105 mutex_lock(&lreq->lock);
3106 if (req != lreq->reg_req) {
3107 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
3108 __func__, lreq, lreq->linger_id, req, lreq->reg_req);
3109 goto out;
3110 }
3111
3112 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
3113 lreq, lreq->linger_id, req->r_result, lreq->last_error);
3114 if (req->r_result < 0) {
3115 if (!lreq->last_error) {
3116 lreq->last_error = normalize_watch_error(req->r_result);
3117 queue_watch_error(lreq);
3118 }
3119 }
3120
3121 out:
3122 mutex_unlock(&lreq->lock);
3123 linger_put(lreq);
3124 }
3125
send_linger(struct ceph_osd_linger_request * lreq)3126 static void send_linger(struct ceph_osd_linger_request *lreq)
3127 {
3128 struct ceph_osd_client *osdc = lreq->osdc;
3129 struct ceph_osd_request *req;
3130 int ret;
3131
3132 verify_osdc_wrlocked(osdc);
3133 mutex_lock(&lreq->lock);
3134 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3135
3136 if (lreq->reg_req) {
3137 if (lreq->reg_req->r_osd)
3138 cancel_linger_request(lreq->reg_req);
3139 ceph_osdc_put_request(lreq->reg_req);
3140 }
3141
3142 req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
3143 BUG_ON(!req);
3144
3145 target_copy(&req->r_t, &lreq->t);
3146 req->r_mtime = lreq->mtime;
3147
3148 if (lreq->is_watch && lreq->committed) {
3149 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT,
3150 lreq->linger_id, ++lreq->register_gen);
3151 dout("lreq %p reconnect register_gen %u\n", lreq,
3152 req->r_ops[0].watch.gen);
3153 req->r_callback = linger_reconnect_cb;
3154 } else {
3155 if (lreq->is_watch) {
3156 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH,
3157 lreq->linger_id, 0);
3158 } else {
3159 lreq->notify_id = 0;
3160
3161 refcount_inc(&lreq->request_pl->refcnt);
3162 osd_req_op_notify_init(req, 0, lreq->linger_id,
3163 lreq->request_pl);
3164 ceph_osd_data_pages_init(
3165 osd_req_op_data(req, 0, notify, response_data),
3166 lreq->notify_id_pages, PAGE_SIZE, 0, false, false);
3167 }
3168 dout("lreq %p register\n", lreq);
3169 req->r_callback = linger_commit_cb;
3170 }
3171
3172 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3173 BUG_ON(ret);
3174
3175 req->r_priv = linger_get(lreq);
3176 req->r_linger = true;
3177 lreq->reg_req = req;
3178 mutex_unlock(&lreq->lock);
3179
3180 submit_request(req, true);
3181 }
3182
linger_ping_cb(struct ceph_osd_request * req)3183 static void linger_ping_cb(struct ceph_osd_request *req)
3184 {
3185 struct ceph_osd_linger_request *lreq = req->r_priv;
3186
3187 mutex_lock(&lreq->lock);
3188 if (req != lreq->ping_req) {
3189 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
3190 __func__, lreq, lreq->linger_id, req, lreq->ping_req);
3191 goto out;
3192 }
3193
3194 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3195 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3196 lreq->last_error);
3197 if (lreq->register_gen == req->r_ops[0].watch.gen) {
3198 if (!req->r_result) {
3199 lreq->watch_valid_thru = lreq->ping_sent;
3200 } else if (!lreq->last_error) {
3201 lreq->last_error = normalize_watch_error(req->r_result);
3202 queue_watch_error(lreq);
3203 }
3204 } else {
3205 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3206 lreq->register_gen, req->r_ops[0].watch.gen);
3207 }
3208
3209 out:
3210 mutex_unlock(&lreq->lock);
3211 linger_put(lreq);
3212 }
3213
send_linger_ping(struct ceph_osd_linger_request * lreq)3214 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3215 {
3216 struct ceph_osd_client *osdc = lreq->osdc;
3217 struct ceph_osd_request *req;
3218 int ret;
3219
3220 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3221 dout("%s PAUSERD\n", __func__);
3222 return;
3223 }
3224
3225 lreq->ping_sent = jiffies;
3226 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3227 __func__, lreq, lreq->linger_id, lreq->ping_sent,
3228 lreq->register_gen);
3229
3230 if (lreq->ping_req) {
3231 if (lreq->ping_req->r_osd)
3232 cancel_linger_request(lreq->ping_req);
3233 ceph_osdc_put_request(lreq->ping_req);
3234 }
3235
3236 req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
3237 BUG_ON(!req);
3238
3239 target_copy(&req->r_t, &lreq->t);
3240 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id,
3241 lreq->register_gen);
3242 req->r_callback = linger_ping_cb;
3243
3244 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3245 BUG_ON(ret);
3246
3247 req->r_priv = linger_get(lreq);
3248 req->r_linger = true;
3249 lreq->ping_req = req;
3250
3251 ceph_osdc_get_request(req);
3252 account_request(req);
3253 req->r_tid = atomic64_inc_return(&osdc->last_tid);
3254 link_request(lreq->osd, req);
3255 send_request(req);
3256 }
3257
linger_submit(struct ceph_osd_linger_request * lreq)3258 static void linger_submit(struct ceph_osd_linger_request *lreq)
3259 {
3260 struct ceph_osd_client *osdc = lreq->osdc;
3261 struct ceph_osd *osd;
3262
3263 down_write(&osdc->lock);
3264 linger_register(lreq);
3265
3266 calc_target(osdc, &lreq->t, false);
3267 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3268 link_linger(osd, lreq);
3269
3270 send_linger(lreq);
3271 up_write(&osdc->lock);
3272 }
3273
cancel_linger_map_check(struct ceph_osd_linger_request * lreq)3274 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3275 {
3276 struct ceph_osd_client *osdc = lreq->osdc;
3277 struct ceph_osd_linger_request *lookup_lreq;
3278
3279 verify_osdc_wrlocked(osdc);
3280
3281 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3282 lreq->linger_id);
3283 if (!lookup_lreq)
3284 return;
3285
3286 WARN_ON(lookup_lreq != lreq);
3287 erase_linger_mc(&osdc->linger_map_checks, lreq);
3288 linger_put(lreq);
3289 }
3290
3291 /*
3292 * @lreq has to be both registered and linked.
3293 */
__linger_cancel(struct ceph_osd_linger_request * lreq)3294 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3295 {
3296 if (lreq->ping_req && lreq->ping_req->r_osd)
3297 cancel_linger_request(lreq->ping_req);
3298 if (lreq->reg_req && lreq->reg_req->r_osd)
3299 cancel_linger_request(lreq->reg_req);
3300 cancel_linger_map_check(lreq);
3301 unlink_linger(lreq->osd, lreq);
3302 linger_unregister(lreq);
3303 }
3304
linger_cancel(struct ceph_osd_linger_request * lreq)3305 static void linger_cancel(struct ceph_osd_linger_request *lreq)
3306 {
3307 struct ceph_osd_client *osdc = lreq->osdc;
3308
3309 down_write(&osdc->lock);
3310 if (__linger_registered(lreq))
3311 __linger_cancel(lreq);
3312 up_write(&osdc->lock);
3313 }
3314
3315 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3316
check_linger_pool_dne(struct ceph_osd_linger_request * lreq)3317 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3318 {
3319 struct ceph_osd_client *osdc = lreq->osdc;
3320 struct ceph_osdmap *map = osdc->osdmap;
3321
3322 verify_osdc_wrlocked(osdc);
3323 WARN_ON(!map->epoch);
3324
3325 if (lreq->register_gen) {
3326 lreq->map_dne_bound = map->epoch;
3327 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3328 lreq, lreq->linger_id);
3329 } else {
3330 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3331 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3332 map->epoch);
3333 }
3334
3335 if (lreq->map_dne_bound) {
3336 if (map->epoch >= lreq->map_dne_bound) {
3337 /* we had a new enough map */
3338 pr_info("linger_id %llu pool does not exist\n",
3339 lreq->linger_id);
3340 linger_reg_commit_complete(lreq, -ENOENT);
3341 __linger_cancel(lreq);
3342 }
3343 } else {
3344 send_linger_map_check(lreq);
3345 }
3346 }
3347
linger_map_check_cb(struct ceph_mon_generic_request * greq)3348 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3349 {
3350 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3351 struct ceph_osd_linger_request *lreq;
3352 u64 linger_id = greq->private_data;
3353
3354 WARN_ON(greq->result || !greq->u.newest);
3355
3356 down_write(&osdc->lock);
3357 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3358 if (!lreq) {
3359 dout("%s linger_id %llu dne\n", __func__, linger_id);
3360 goto out_unlock;
3361 }
3362
3363 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3364 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3365 greq->u.newest);
3366 if (!lreq->map_dne_bound)
3367 lreq->map_dne_bound = greq->u.newest;
3368 erase_linger_mc(&osdc->linger_map_checks, lreq);
3369 check_linger_pool_dne(lreq);
3370
3371 linger_put(lreq);
3372 out_unlock:
3373 up_write(&osdc->lock);
3374 }
3375
send_linger_map_check(struct ceph_osd_linger_request * lreq)3376 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3377 {
3378 struct ceph_osd_client *osdc = lreq->osdc;
3379 struct ceph_osd_linger_request *lookup_lreq;
3380 int ret;
3381
3382 verify_osdc_wrlocked(osdc);
3383
3384 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3385 lreq->linger_id);
3386 if (lookup_lreq) {
3387 WARN_ON(lookup_lreq != lreq);
3388 return;
3389 }
3390
3391 linger_get(lreq);
3392 insert_linger_mc(&osdc->linger_map_checks, lreq);
3393 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3394 linger_map_check_cb, lreq->linger_id);
3395 WARN_ON(ret);
3396 }
3397
linger_reg_commit_wait(struct ceph_osd_linger_request * lreq)3398 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3399 {
3400 int ret;
3401
3402 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3403 ret = wait_for_completion_killable(&lreq->reg_commit_wait);
3404 return ret ?: lreq->reg_commit_error;
3405 }
3406
linger_notify_finish_wait(struct ceph_osd_linger_request * lreq,unsigned long timeout)3407 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq,
3408 unsigned long timeout)
3409 {
3410 long left;
3411
3412 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3413 left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait,
3414 ceph_timeout_jiffies(timeout));
3415 if (left <= 0)
3416 left = left ?: -ETIMEDOUT;
3417 else
3418 left = lreq->notify_finish_error; /* completed */
3419
3420 return left;
3421 }
3422
3423 /*
3424 * Timeout callback, called every N seconds. When 1 or more OSD
3425 * requests has been active for more than N seconds, we send a keepalive
3426 * (tag + timestamp) to its OSD to ensure any communications channel
3427 * reset is detected.
3428 */
handle_timeout(struct work_struct * work)3429 static void handle_timeout(struct work_struct *work)
3430 {
3431 struct ceph_osd_client *osdc =
3432 container_of(work, struct ceph_osd_client, timeout_work.work);
3433 struct ceph_options *opts = osdc->client->options;
3434 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3435 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3436 LIST_HEAD(slow_osds);
3437 struct rb_node *n, *p;
3438
3439 dout("%s osdc %p\n", __func__, osdc);
3440 down_write(&osdc->lock);
3441
3442 /*
3443 * ping osds that are a bit slow. this ensures that if there
3444 * is a break in the TCP connection we will notice, and reopen
3445 * a connection with that osd (from the fault callback).
3446 */
3447 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3448 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3449 bool found = false;
3450
3451 for (p = rb_first(&osd->o_requests); p; ) {
3452 struct ceph_osd_request *req =
3453 rb_entry(p, struct ceph_osd_request, r_node);
3454
3455 p = rb_next(p); /* abort_request() */
3456
3457 if (time_before(req->r_stamp, cutoff)) {
3458 dout(" req %p tid %llu on osd%d is laggy\n",
3459 req, req->r_tid, osd->o_osd);
3460 found = true;
3461 }
3462 if (opts->osd_request_timeout &&
3463 time_before(req->r_start_stamp, expiry_cutoff)) {
3464 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3465 req->r_tid, osd->o_osd);
3466 abort_request(req, -ETIMEDOUT);
3467 }
3468 }
3469 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3470 struct ceph_osd_linger_request *lreq =
3471 rb_entry(p, struct ceph_osd_linger_request, node);
3472
3473 dout(" lreq %p linger_id %llu is served by osd%d\n",
3474 lreq, lreq->linger_id, osd->o_osd);
3475 found = true;
3476
3477 mutex_lock(&lreq->lock);
3478 if (lreq->is_watch && lreq->committed && !lreq->last_error)
3479 send_linger_ping(lreq);
3480 mutex_unlock(&lreq->lock);
3481 }
3482
3483 if (found)
3484 list_move_tail(&osd->o_keepalive_item, &slow_osds);
3485 }
3486
3487 if (opts->osd_request_timeout) {
3488 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3489 struct ceph_osd_request *req =
3490 rb_entry(p, struct ceph_osd_request, r_node);
3491
3492 p = rb_next(p); /* abort_request() */
3493
3494 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3495 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3496 req->r_tid, osdc->homeless_osd.o_osd);
3497 abort_request(req, -ETIMEDOUT);
3498 }
3499 }
3500 }
3501
3502 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3503 maybe_request_map(osdc);
3504
3505 while (!list_empty(&slow_osds)) {
3506 struct ceph_osd *osd = list_first_entry(&slow_osds,
3507 struct ceph_osd,
3508 o_keepalive_item);
3509 list_del_init(&osd->o_keepalive_item);
3510 ceph_con_keepalive(&osd->o_con);
3511 }
3512
3513 up_write(&osdc->lock);
3514 schedule_delayed_work(&osdc->timeout_work,
3515 osdc->client->options->osd_keepalive_timeout);
3516 }
3517
handle_osds_timeout(struct work_struct * work)3518 static void handle_osds_timeout(struct work_struct *work)
3519 {
3520 struct ceph_osd_client *osdc =
3521 container_of(work, struct ceph_osd_client,
3522 osds_timeout_work.work);
3523 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3524 struct ceph_osd *osd, *nosd;
3525
3526 dout("%s osdc %p\n", __func__, osdc);
3527 down_write(&osdc->lock);
3528 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3529 if (time_before(jiffies, osd->lru_ttl))
3530 break;
3531
3532 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3533 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3534 close_osd(osd);
3535 }
3536
3537 up_write(&osdc->lock);
3538 schedule_delayed_work(&osdc->osds_timeout_work,
3539 round_jiffies_relative(delay));
3540 }
3541
ceph_oloc_decode(void ** p,void * end,struct ceph_object_locator * oloc)3542 static int ceph_oloc_decode(void **p, void *end,
3543 struct ceph_object_locator *oloc)
3544 {
3545 u8 struct_v, struct_cv;
3546 u32 len;
3547 void *struct_end;
3548 int ret = 0;
3549
3550 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3551 struct_v = ceph_decode_8(p);
3552 struct_cv = ceph_decode_8(p);
3553 if (struct_v < 3) {
3554 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3555 struct_v, struct_cv);
3556 goto e_inval;
3557 }
3558 if (struct_cv > 6) {
3559 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3560 struct_v, struct_cv);
3561 goto e_inval;
3562 }
3563 len = ceph_decode_32(p);
3564 ceph_decode_need(p, end, len, e_inval);
3565 struct_end = *p + len;
3566
3567 oloc->pool = ceph_decode_64(p);
3568 *p += 4; /* skip preferred */
3569
3570 len = ceph_decode_32(p);
3571 if (len > 0) {
3572 pr_warn("ceph_object_locator::key is set\n");
3573 goto e_inval;
3574 }
3575
3576 if (struct_v >= 5) {
3577 bool changed = false;
3578
3579 len = ceph_decode_32(p);
3580 if (len > 0) {
3581 ceph_decode_need(p, end, len, e_inval);
3582 if (!oloc->pool_ns ||
3583 ceph_compare_string(oloc->pool_ns, *p, len))
3584 changed = true;
3585 *p += len;
3586 } else {
3587 if (oloc->pool_ns)
3588 changed = true;
3589 }
3590 if (changed) {
3591 /* redirect changes namespace */
3592 pr_warn("ceph_object_locator::nspace is changed\n");
3593 goto e_inval;
3594 }
3595 }
3596
3597 if (struct_v >= 6) {
3598 s64 hash = ceph_decode_64(p);
3599 if (hash != -1) {
3600 pr_warn("ceph_object_locator::hash is set\n");
3601 goto e_inval;
3602 }
3603 }
3604
3605 /* skip the rest */
3606 *p = struct_end;
3607 out:
3608 return ret;
3609
3610 e_inval:
3611 ret = -EINVAL;
3612 goto out;
3613 }
3614
ceph_redirect_decode(void ** p,void * end,struct ceph_request_redirect * redir)3615 static int ceph_redirect_decode(void **p, void *end,
3616 struct ceph_request_redirect *redir)
3617 {
3618 u8 struct_v, struct_cv;
3619 u32 len;
3620 void *struct_end;
3621 int ret;
3622
3623 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3624 struct_v = ceph_decode_8(p);
3625 struct_cv = ceph_decode_8(p);
3626 if (struct_cv > 1) {
3627 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3628 struct_v, struct_cv);
3629 goto e_inval;
3630 }
3631 len = ceph_decode_32(p);
3632 ceph_decode_need(p, end, len, e_inval);
3633 struct_end = *p + len;
3634
3635 ret = ceph_oloc_decode(p, end, &redir->oloc);
3636 if (ret)
3637 goto out;
3638
3639 len = ceph_decode_32(p);
3640 if (len > 0) {
3641 pr_warn("ceph_request_redirect::object_name is set\n");
3642 goto e_inval;
3643 }
3644
3645 /* skip the rest */
3646 *p = struct_end;
3647 out:
3648 return ret;
3649
3650 e_inval:
3651 ret = -EINVAL;
3652 goto out;
3653 }
3654
3655 struct MOSDOpReply {
3656 struct ceph_pg pgid;
3657 u64 flags;
3658 int result;
3659 u32 epoch;
3660 int num_ops;
3661 u32 outdata_len[CEPH_OSD_MAX_OPS];
3662 s32 rval[CEPH_OSD_MAX_OPS];
3663 int retry_attempt;
3664 struct ceph_eversion replay_version;
3665 u64 user_version;
3666 struct ceph_request_redirect redirect;
3667 };
3668
decode_MOSDOpReply(const struct ceph_msg * msg,struct MOSDOpReply * m)3669 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3670 {
3671 void *p = msg->front.iov_base;
3672 void *const end = p + msg->front.iov_len;
3673 u16 version = le16_to_cpu(msg->hdr.version);
3674 struct ceph_eversion bad_replay_version;
3675 u8 decode_redir;
3676 u32 len;
3677 int ret;
3678 int i;
3679
3680 ceph_decode_32_safe(&p, end, len, e_inval);
3681 ceph_decode_need(&p, end, len, e_inval);
3682 p += len; /* skip oid */
3683
3684 ret = ceph_decode_pgid(&p, end, &m->pgid);
3685 if (ret)
3686 return ret;
3687
3688 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3689 ceph_decode_32_safe(&p, end, m->result, e_inval);
3690 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3691 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3692 p += sizeof(bad_replay_version);
3693 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
3694
3695 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3696 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3697 goto e_inval;
3698
3699 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3700 e_inval);
3701 for (i = 0; i < m->num_ops; i++) {
3702 struct ceph_osd_op *op = p;
3703
3704 m->outdata_len[i] = le32_to_cpu(op->payload_len);
3705 p += sizeof(*op);
3706 }
3707
3708 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3709 for (i = 0; i < m->num_ops; i++)
3710 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3711
3712 if (version >= 5) {
3713 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3714 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3715 p += sizeof(m->replay_version);
3716 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3717 } else {
3718 m->replay_version = bad_replay_version; /* struct */
3719 m->user_version = le64_to_cpu(m->replay_version.version);
3720 }
3721
3722 if (version >= 6) {
3723 if (version >= 7)
3724 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3725 else
3726 decode_redir = 1;
3727 } else {
3728 decode_redir = 0;
3729 }
3730
3731 if (decode_redir) {
3732 ret = ceph_redirect_decode(&p, end, &m->redirect);
3733 if (ret)
3734 return ret;
3735 } else {
3736 ceph_oloc_init(&m->redirect.oloc);
3737 }
3738
3739 return 0;
3740
3741 e_inval:
3742 return -EINVAL;
3743 }
3744
3745 /*
3746 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3747 * specified.
3748 */
handle_reply(struct ceph_osd * osd,struct ceph_msg * msg)3749 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3750 {
3751 struct ceph_osd_client *osdc = osd->o_osdc;
3752 struct ceph_osd_request *req;
3753 struct MOSDOpReply m;
3754 u64 tid = le64_to_cpu(msg->hdr.tid);
3755 u32 data_len = 0;
3756 int ret;
3757 int i;
3758
3759 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3760
3761 down_read(&osdc->lock);
3762 if (!osd_registered(osd)) {
3763 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3764 goto out_unlock_osdc;
3765 }
3766 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3767
3768 mutex_lock(&osd->lock);
3769 req = lookup_request(&osd->o_requests, tid);
3770 if (!req) {
3771 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3772 goto out_unlock_session;
3773 }
3774
3775 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3776 ret = decode_MOSDOpReply(msg, &m);
3777 m.redirect.oloc.pool_ns = NULL;
3778 if (ret) {
3779 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3780 req->r_tid, ret);
3781 ceph_msg_dump(msg);
3782 goto fail_request;
3783 }
3784 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3785 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3786 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3787 le64_to_cpu(m.replay_version.version), m.user_version);
3788
3789 if (m.retry_attempt >= 0) {
3790 if (m.retry_attempt != req->r_attempts - 1) {
3791 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3792 req, req->r_tid, m.retry_attempt,
3793 req->r_attempts - 1);
3794 goto out_unlock_session;
3795 }
3796 } else {
3797 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3798 }
3799
3800 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3801 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3802 m.redirect.oloc.pool);
3803 unlink_request(osd, req);
3804 mutex_unlock(&osd->lock);
3805
3806 /*
3807 * Not ceph_oloc_copy() - changing pool_ns is not
3808 * supported.
3809 */
3810 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3811 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
3812 CEPH_OSD_FLAG_IGNORE_OVERLAY |
3813 CEPH_OSD_FLAG_IGNORE_CACHE;
3814 req->r_tid = 0;
3815 __submit_request(req, false);
3816 goto out_unlock_osdc;
3817 }
3818
3819 if (m.result == -EAGAIN) {
3820 dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
3821 unlink_request(osd, req);
3822 mutex_unlock(&osd->lock);
3823
3824 /*
3825 * The object is missing on the replica or not (yet)
3826 * readable. Clear pgid to force a resend to the primary
3827 * via legacy_change.
3828 */
3829 req->r_t.pgid.pool = 0;
3830 req->r_t.pgid.seed = 0;
3831 WARN_ON(!req->r_t.used_replica);
3832 req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
3833 CEPH_OSD_FLAG_LOCALIZE_READS);
3834 req->r_tid = 0;
3835 __submit_request(req, false);
3836 goto out_unlock_osdc;
3837 }
3838
3839 if (m.num_ops != req->r_num_ops) {
3840 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3841 req->r_num_ops, req->r_tid);
3842 goto fail_request;
3843 }
3844 for (i = 0; i < req->r_num_ops; i++) {
3845 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3846 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3847 req->r_ops[i].rval = m.rval[i];
3848 req->r_ops[i].outdata_len = m.outdata_len[i];
3849 data_len += m.outdata_len[i];
3850 }
3851 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3852 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3853 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3854 goto fail_request;
3855 }
3856 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3857 req, req->r_tid, m.result, data_len);
3858
3859 /*
3860 * Since we only ever request ONDISK, we should only ever get
3861 * one (type of) reply back.
3862 */
3863 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3864 req->r_version = m.user_version;
3865 req->r_result = m.result ?: data_len;
3866 finish_request(req);
3867 mutex_unlock(&osd->lock);
3868 up_read(&osdc->lock);
3869
3870 __complete_request(req);
3871 return;
3872
3873 fail_request:
3874 complete_request(req, -EIO);
3875 out_unlock_session:
3876 mutex_unlock(&osd->lock);
3877 out_unlock_osdc:
3878 up_read(&osdc->lock);
3879 }
3880
set_pool_was_full(struct ceph_osd_client * osdc)3881 static void set_pool_was_full(struct ceph_osd_client *osdc)
3882 {
3883 struct rb_node *n;
3884
3885 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3886 struct ceph_pg_pool_info *pi =
3887 rb_entry(n, struct ceph_pg_pool_info, node);
3888
3889 pi->was_full = __pool_full(pi);
3890 }
3891 }
3892
pool_cleared_full(struct ceph_osd_client * osdc,s64 pool_id)3893 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3894 {
3895 struct ceph_pg_pool_info *pi;
3896
3897 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3898 if (!pi)
3899 return false;
3900
3901 return pi->was_full && !__pool_full(pi);
3902 }
3903
3904 static enum calc_target_result
recalc_linger_target(struct ceph_osd_linger_request * lreq)3905 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3906 {
3907 struct ceph_osd_client *osdc = lreq->osdc;
3908 enum calc_target_result ct_res;
3909
3910 ct_res = calc_target(osdc, &lreq->t, true);
3911 if (ct_res == CALC_TARGET_NEED_RESEND) {
3912 struct ceph_osd *osd;
3913
3914 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3915 if (osd != lreq->osd) {
3916 unlink_linger(lreq->osd, lreq);
3917 link_linger(osd, lreq);
3918 }
3919 }
3920
3921 return ct_res;
3922 }
3923
3924 /*
3925 * Requeue requests whose mapping to an OSD has changed.
3926 */
scan_requests(struct ceph_osd * osd,bool force_resend,bool cleared_full,bool check_pool_cleared_full,struct rb_root * need_resend,struct list_head * need_resend_linger)3927 static void scan_requests(struct ceph_osd *osd,
3928 bool force_resend,
3929 bool cleared_full,
3930 bool check_pool_cleared_full,
3931 struct rb_root *need_resend,
3932 struct list_head *need_resend_linger)
3933 {
3934 struct ceph_osd_client *osdc = osd->o_osdc;
3935 struct rb_node *n;
3936 bool force_resend_writes;
3937
3938 for (n = rb_first(&osd->o_linger_requests); n; ) {
3939 struct ceph_osd_linger_request *lreq =
3940 rb_entry(n, struct ceph_osd_linger_request, node);
3941 enum calc_target_result ct_res;
3942
3943 n = rb_next(n); /* recalc_linger_target() */
3944
3945 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3946 lreq->linger_id);
3947 ct_res = recalc_linger_target(lreq);
3948 switch (ct_res) {
3949 case CALC_TARGET_NO_ACTION:
3950 force_resend_writes = cleared_full ||
3951 (check_pool_cleared_full &&
3952 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3953 if (!force_resend && !force_resend_writes)
3954 break;
3955
3956 fallthrough;
3957 case CALC_TARGET_NEED_RESEND:
3958 cancel_linger_map_check(lreq);
3959 /*
3960 * scan_requests() for the previous epoch(s)
3961 * may have already added it to the list, since
3962 * it's not unlinked here.
3963 */
3964 if (list_empty(&lreq->scan_item))
3965 list_add_tail(&lreq->scan_item, need_resend_linger);
3966 break;
3967 case CALC_TARGET_POOL_DNE:
3968 list_del_init(&lreq->scan_item);
3969 check_linger_pool_dne(lreq);
3970 break;
3971 }
3972 }
3973
3974 for (n = rb_first(&osd->o_requests); n; ) {
3975 struct ceph_osd_request *req =
3976 rb_entry(n, struct ceph_osd_request, r_node);
3977 enum calc_target_result ct_res;
3978
3979 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3980
3981 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3982 ct_res = calc_target(osdc, &req->r_t, false);
3983 switch (ct_res) {
3984 case CALC_TARGET_NO_ACTION:
3985 force_resend_writes = cleared_full ||
3986 (check_pool_cleared_full &&
3987 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3988 if (!force_resend &&
3989 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3990 !force_resend_writes))
3991 break;
3992
3993 fallthrough;
3994 case CALC_TARGET_NEED_RESEND:
3995 cancel_map_check(req);
3996 unlink_request(osd, req);
3997 insert_request(need_resend, req);
3998 break;
3999 case CALC_TARGET_POOL_DNE:
4000 check_pool_dne(req);
4001 break;
4002 }
4003 }
4004 }
4005
handle_one_map(struct ceph_osd_client * osdc,void * p,void * end,bool incremental,struct rb_root * need_resend,struct list_head * need_resend_linger)4006 static int handle_one_map(struct ceph_osd_client *osdc,
4007 void *p, void *end, bool incremental,
4008 struct rb_root *need_resend,
4009 struct list_head *need_resend_linger)
4010 {
4011 struct ceph_osdmap *newmap;
4012 struct rb_node *n;
4013 bool skipped_map = false;
4014 bool was_full;
4015
4016 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
4017 set_pool_was_full(osdc);
4018
4019 if (incremental)
4020 newmap = osdmap_apply_incremental(&p, end,
4021 ceph_msgr2(osdc->client),
4022 osdc->osdmap);
4023 else
4024 newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client));
4025 if (IS_ERR(newmap))
4026 return PTR_ERR(newmap);
4027
4028 if (newmap != osdc->osdmap) {
4029 /*
4030 * Preserve ->was_full before destroying the old map.
4031 * For pools that weren't in the old map, ->was_full
4032 * should be false.
4033 */
4034 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
4035 struct ceph_pg_pool_info *pi =
4036 rb_entry(n, struct ceph_pg_pool_info, node);
4037 struct ceph_pg_pool_info *old_pi;
4038
4039 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
4040 if (old_pi)
4041 pi->was_full = old_pi->was_full;
4042 else
4043 WARN_ON(pi->was_full);
4044 }
4045
4046 if (osdc->osdmap->epoch &&
4047 osdc->osdmap->epoch + 1 < newmap->epoch) {
4048 WARN_ON(incremental);
4049 skipped_map = true;
4050 }
4051
4052 ceph_osdmap_destroy(osdc->osdmap);
4053 osdc->osdmap = newmap;
4054 }
4055
4056 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
4057 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
4058 need_resend, need_resend_linger);
4059
4060 for (n = rb_first(&osdc->osds); n; ) {
4061 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4062
4063 n = rb_next(n); /* close_osd() */
4064
4065 scan_requests(osd, skipped_map, was_full, true, need_resend,
4066 need_resend_linger);
4067 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
4068 memcmp(&osd->o_con.peer_addr,
4069 ceph_osd_addr(osdc->osdmap, osd->o_osd),
4070 sizeof(struct ceph_entity_addr)))
4071 close_osd(osd);
4072 }
4073
4074 return 0;
4075 }
4076
kick_requests(struct ceph_osd_client * osdc,struct rb_root * need_resend,struct list_head * need_resend_linger)4077 static void kick_requests(struct ceph_osd_client *osdc,
4078 struct rb_root *need_resend,
4079 struct list_head *need_resend_linger)
4080 {
4081 struct ceph_osd_linger_request *lreq, *nlreq;
4082 enum calc_target_result ct_res;
4083 struct rb_node *n;
4084
4085 /* make sure need_resend targets reflect latest map */
4086 for (n = rb_first(need_resend); n; ) {
4087 struct ceph_osd_request *req =
4088 rb_entry(n, struct ceph_osd_request, r_node);
4089
4090 n = rb_next(n);
4091
4092 if (req->r_t.epoch < osdc->osdmap->epoch) {
4093 ct_res = calc_target(osdc, &req->r_t, false);
4094 if (ct_res == CALC_TARGET_POOL_DNE) {
4095 erase_request(need_resend, req);
4096 check_pool_dne(req);
4097 }
4098 }
4099 }
4100
4101 for (n = rb_first(need_resend); n; ) {
4102 struct ceph_osd_request *req =
4103 rb_entry(n, struct ceph_osd_request, r_node);
4104 struct ceph_osd *osd;
4105
4106 n = rb_next(n);
4107 erase_request(need_resend, req); /* before link_request() */
4108
4109 osd = lookup_create_osd(osdc, req->r_t.osd, true);
4110 link_request(osd, req);
4111 if (!req->r_linger) {
4112 if (!osd_homeless(osd) && !req->r_t.paused)
4113 send_request(req);
4114 } else {
4115 cancel_linger_request(req);
4116 }
4117 }
4118
4119 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
4120 if (!osd_homeless(lreq->osd))
4121 send_linger(lreq);
4122
4123 list_del_init(&lreq->scan_item);
4124 }
4125 }
4126
4127 /*
4128 * Process updated osd map.
4129 *
4130 * The message contains any number of incremental and full maps, normally
4131 * indicating some sort of topology change in the cluster. Kick requests
4132 * off to different OSDs as needed.
4133 */
ceph_osdc_handle_map(struct ceph_osd_client * osdc,struct ceph_msg * msg)4134 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
4135 {
4136 void *p = msg->front.iov_base;
4137 void *const end = p + msg->front.iov_len;
4138 u32 nr_maps, maplen;
4139 u32 epoch;
4140 struct ceph_fsid fsid;
4141 struct rb_root need_resend = RB_ROOT;
4142 LIST_HEAD(need_resend_linger);
4143 bool handled_incremental = false;
4144 bool was_pauserd, was_pausewr;
4145 bool pauserd, pausewr;
4146 int err;
4147
4148 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
4149 down_write(&osdc->lock);
4150
4151 /* verify fsid */
4152 ceph_decode_need(&p, end, sizeof(fsid), bad);
4153 ceph_decode_copy(&p, &fsid, sizeof(fsid));
4154 if (ceph_check_fsid(osdc->client, &fsid) < 0)
4155 goto bad;
4156
4157 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4158 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4159 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
4160 have_pool_full(osdc);
4161
4162 /* incremental maps */
4163 ceph_decode_32_safe(&p, end, nr_maps, bad);
4164 dout(" %d inc maps\n", nr_maps);
4165 while (nr_maps > 0) {
4166 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
4167 epoch = ceph_decode_32(&p);
4168 maplen = ceph_decode_32(&p);
4169 ceph_decode_need(&p, end, maplen, bad);
4170 if (osdc->osdmap->epoch &&
4171 osdc->osdmap->epoch + 1 == epoch) {
4172 dout("applying incremental map %u len %d\n",
4173 epoch, maplen);
4174 err = handle_one_map(osdc, p, p + maplen, true,
4175 &need_resend, &need_resend_linger);
4176 if (err)
4177 goto bad;
4178 handled_incremental = true;
4179 } else {
4180 dout("ignoring incremental map %u len %d\n",
4181 epoch, maplen);
4182 }
4183 p += maplen;
4184 nr_maps--;
4185 }
4186 if (handled_incremental)
4187 goto done;
4188
4189 /* full maps */
4190 ceph_decode_32_safe(&p, end, nr_maps, bad);
4191 dout(" %d full maps\n", nr_maps);
4192 while (nr_maps) {
4193 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
4194 epoch = ceph_decode_32(&p);
4195 maplen = ceph_decode_32(&p);
4196 ceph_decode_need(&p, end, maplen, bad);
4197 if (nr_maps > 1) {
4198 dout("skipping non-latest full map %u len %d\n",
4199 epoch, maplen);
4200 } else if (osdc->osdmap->epoch >= epoch) {
4201 dout("skipping full map %u len %d, "
4202 "older than our %u\n", epoch, maplen,
4203 osdc->osdmap->epoch);
4204 } else {
4205 dout("taking full map %u len %d\n", epoch, maplen);
4206 err = handle_one_map(osdc, p, p + maplen, false,
4207 &need_resend, &need_resend_linger);
4208 if (err)
4209 goto bad;
4210 }
4211 p += maplen;
4212 nr_maps--;
4213 }
4214
4215 done:
4216 /*
4217 * subscribe to subsequent osdmap updates if full to ensure
4218 * we find out when we are no longer full and stop returning
4219 * ENOSPC.
4220 */
4221 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4222 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4223 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
4224 have_pool_full(osdc);
4225 if (was_pauserd || was_pausewr || pauserd || pausewr ||
4226 osdc->osdmap->epoch < osdc->epoch_barrier)
4227 maybe_request_map(osdc);
4228
4229 kick_requests(osdc, &need_resend, &need_resend_linger);
4230
4231 ceph_osdc_abort_on_full(osdc);
4232 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
4233 osdc->osdmap->epoch);
4234 up_write(&osdc->lock);
4235 wake_up_all(&osdc->client->auth_wq);
4236 return;
4237
4238 bad:
4239 pr_err("osdc handle_map corrupt msg\n");
4240 ceph_msg_dump(msg);
4241 up_write(&osdc->lock);
4242 }
4243
4244 /*
4245 * Resubmit requests pending on the given osd.
4246 */
kick_osd_requests(struct ceph_osd * osd)4247 static void kick_osd_requests(struct ceph_osd *osd)
4248 {
4249 struct rb_node *n;
4250
4251 clear_backoffs(osd);
4252
4253 for (n = rb_first(&osd->o_requests); n; ) {
4254 struct ceph_osd_request *req =
4255 rb_entry(n, struct ceph_osd_request, r_node);
4256
4257 n = rb_next(n); /* cancel_linger_request() */
4258
4259 if (!req->r_linger) {
4260 if (!req->r_t.paused)
4261 send_request(req);
4262 } else {
4263 cancel_linger_request(req);
4264 }
4265 }
4266 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4267 struct ceph_osd_linger_request *lreq =
4268 rb_entry(n, struct ceph_osd_linger_request, node);
4269
4270 send_linger(lreq);
4271 }
4272 }
4273
4274 /*
4275 * If the osd connection drops, we need to resubmit all requests.
4276 */
osd_fault(struct ceph_connection * con)4277 static void osd_fault(struct ceph_connection *con)
4278 {
4279 struct ceph_osd *osd = con->private;
4280 struct ceph_osd_client *osdc = osd->o_osdc;
4281
4282 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4283
4284 down_write(&osdc->lock);
4285 if (!osd_registered(osd)) {
4286 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4287 goto out_unlock;
4288 }
4289
4290 osd->o_sparse_op_idx = -1;
4291 ceph_init_sparse_read(&osd->o_sparse_read);
4292
4293 if (!reopen_osd(osd))
4294 kick_osd_requests(osd);
4295 maybe_request_map(osdc);
4296
4297 out_unlock:
4298 up_write(&osdc->lock);
4299 }
4300
4301 struct MOSDBackoff {
4302 struct ceph_spg spgid;
4303 u32 map_epoch;
4304 u8 op;
4305 u64 id;
4306 struct ceph_hobject_id *begin;
4307 struct ceph_hobject_id *end;
4308 };
4309
decode_MOSDBackoff(const struct ceph_msg * msg,struct MOSDBackoff * m)4310 static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4311 {
4312 void *p = msg->front.iov_base;
4313 void *const end = p + msg->front.iov_len;
4314 u8 struct_v;
4315 u32 struct_len;
4316 int ret;
4317
4318 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4319 if (ret)
4320 return ret;
4321
4322 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4323 if (ret)
4324 return ret;
4325
4326 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4327 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4328 ceph_decode_8_safe(&p, end, m->op, e_inval);
4329 ceph_decode_64_safe(&p, end, m->id, e_inval);
4330
4331 m->begin = kzalloc_obj(*m->begin, GFP_NOIO);
4332 if (!m->begin)
4333 return -ENOMEM;
4334
4335 ret = decode_hoid(&p, end, m->begin);
4336 if (ret) {
4337 free_hoid(m->begin);
4338 return ret;
4339 }
4340
4341 m->end = kzalloc_obj(*m->end, GFP_NOIO);
4342 if (!m->end) {
4343 free_hoid(m->begin);
4344 return -ENOMEM;
4345 }
4346
4347 ret = decode_hoid(&p, end, m->end);
4348 if (ret) {
4349 free_hoid(m->begin);
4350 free_hoid(m->end);
4351 return ret;
4352 }
4353
4354 return 0;
4355
4356 e_inval:
4357 return -EINVAL;
4358 }
4359
create_backoff_message(const struct ceph_osd_backoff * backoff,u32 map_epoch)4360 static struct ceph_msg *create_backoff_message(
4361 const struct ceph_osd_backoff *backoff,
4362 u32 map_epoch)
4363 {
4364 struct ceph_msg *msg;
4365 void *p, *end;
4366 int msg_size;
4367
4368 msg_size = CEPH_ENCODING_START_BLK_LEN +
4369 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4370 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4371 msg_size += CEPH_ENCODING_START_BLK_LEN +
4372 hoid_encoding_size(backoff->begin);
4373 msg_size += CEPH_ENCODING_START_BLK_LEN +
4374 hoid_encoding_size(backoff->end);
4375
4376 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4377 if (!msg)
4378 return NULL;
4379
4380 p = msg->front.iov_base;
4381 end = p + msg->front_alloc_len;
4382
4383 encode_spgid(&p, &backoff->spgid);
4384 ceph_encode_32(&p, map_epoch);
4385 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4386 ceph_encode_64(&p, backoff->id);
4387 encode_hoid(&p, end, backoff->begin);
4388 encode_hoid(&p, end, backoff->end);
4389 BUG_ON(p != end);
4390
4391 msg->front.iov_len = p - msg->front.iov_base;
4392 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4393 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4394
4395 return msg;
4396 }
4397
handle_backoff_block(struct ceph_osd * osd,struct MOSDBackoff * m)4398 static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4399 {
4400 struct ceph_spg_mapping *spg;
4401 struct ceph_osd_backoff *backoff;
4402 struct ceph_msg *msg;
4403
4404 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4405 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4406
4407 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4408 if (!spg) {
4409 spg = alloc_spg_mapping();
4410 if (!spg) {
4411 pr_err("%s failed to allocate spg\n", __func__);
4412 return;
4413 }
4414 spg->spgid = m->spgid; /* struct */
4415 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4416 }
4417
4418 backoff = alloc_backoff();
4419 if (!backoff) {
4420 pr_err("%s failed to allocate backoff\n", __func__);
4421 return;
4422 }
4423 backoff->spgid = m->spgid; /* struct */
4424 backoff->id = m->id;
4425 backoff->begin = m->begin;
4426 m->begin = NULL; /* backoff now owns this */
4427 backoff->end = m->end;
4428 m->end = NULL; /* ditto */
4429
4430 insert_backoff(&spg->backoffs, backoff);
4431 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4432
4433 /*
4434 * Ack with original backoff's epoch so that the OSD can
4435 * discard this if there was a PG split.
4436 */
4437 msg = create_backoff_message(backoff, m->map_epoch);
4438 if (!msg) {
4439 pr_err("%s failed to allocate msg\n", __func__);
4440 return;
4441 }
4442 ceph_con_send(&osd->o_con, msg);
4443 }
4444
target_contained_by(const struct ceph_osd_request_target * t,const struct ceph_hobject_id * begin,const struct ceph_hobject_id * end)4445 static bool target_contained_by(const struct ceph_osd_request_target *t,
4446 const struct ceph_hobject_id *begin,
4447 const struct ceph_hobject_id *end)
4448 {
4449 struct ceph_hobject_id hoid;
4450 int cmp;
4451
4452 hoid_fill_from_target(&hoid, t);
4453 cmp = hoid_compare(&hoid, begin);
4454 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4455 }
4456
handle_backoff_unblock(struct ceph_osd * osd,const struct MOSDBackoff * m)4457 static void handle_backoff_unblock(struct ceph_osd *osd,
4458 const struct MOSDBackoff *m)
4459 {
4460 struct ceph_spg_mapping *spg;
4461 struct ceph_osd_backoff *backoff;
4462 struct rb_node *n;
4463
4464 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4465 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4466
4467 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4468 if (!backoff) {
4469 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4470 __func__, osd->o_osd, m->spgid.pgid.pool,
4471 m->spgid.pgid.seed, m->spgid.shard, m->id);
4472 return;
4473 }
4474
4475 if (hoid_compare(backoff->begin, m->begin) &&
4476 hoid_compare(backoff->end, m->end)) {
4477 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4478 __func__, osd->o_osd, m->spgid.pgid.pool,
4479 m->spgid.pgid.seed, m->spgid.shard, m->id);
4480 /* unblock it anyway... */
4481 }
4482
4483 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4484 BUG_ON(!spg);
4485
4486 erase_backoff(&spg->backoffs, backoff);
4487 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4488 free_backoff(backoff);
4489
4490 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4491 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4492 free_spg_mapping(spg);
4493 }
4494
4495 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4496 struct ceph_osd_request *req =
4497 rb_entry(n, struct ceph_osd_request, r_node);
4498
4499 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4500 /*
4501 * Match against @m, not @backoff -- the PG may
4502 * have split on the OSD.
4503 */
4504 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4505 /*
4506 * If no other installed backoff applies,
4507 * resend.
4508 */
4509 send_request(req);
4510 }
4511 }
4512 }
4513 }
4514
handle_backoff(struct ceph_osd * osd,struct ceph_msg * msg)4515 static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4516 {
4517 struct ceph_osd_client *osdc = osd->o_osdc;
4518 struct MOSDBackoff m;
4519 int ret;
4520
4521 down_read(&osdc->lock);
4522 if (!osd_registered(osd)) {
4523 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4524 up_read(&osdc->lock);
4525 return;
4526 }
4527 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4528
4529 mutex_lock(&osd->lock);
4530 ret = decode_MOSDBackoff(msg, &m);
4531 if (ret) {
4532 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4533 ceph_msg_dump(msg);
4534 goto out_unlock;
4535 }
4536
4537 switch (m.op) {
4538 case CEPH_OSD_BACKOFF_OP_BLOCK:
4539 handle_backoff_block(osd, &m);
4540 break;
4541 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4542 handle_backoff_unblock(osd, &m);
4543 break;
4544 default:
4545 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4546 }
4547
4548 free_hoid(m.begin);
4549 free_hoid(m.end);
4550
4551 out_unlock:
4552 mutex_unlock(&osd->lock);
4553 up_read(&osdc->lock);
4554 }
4555
4556 /*
4557 * Process osd watch notifications
4558 */
handle_watch_notify(struct ceph_osd_client * osdc,struct ceph_msg * msg)4559 static void handle_watch_notify(struct ceph_osd_client *osdc,
4560 struct ceph_msg *msg)
4561 {
4562 void *p = msg->front.iov_base;
4563 void *const end = p + msg->front.iov_len;
4564 struct ceph_osd_linger_request *lreq;
4565 struct linger_work *lwork;
4566 u8 proto_ver, opcode;
4567 u64 cookie, notify_id;
4568 u64 notifier_id = 0;
4569 s32 return_code = 0;
4570 void *payload = NULL;
4571 u32 payload_len = 0;
4572
4573 ceph_decode_8_safe(&p, end, proto_ver, bad);
4574 ceph_decode_8_safe(&p, end, opcode, bad);
4575 ceph_decode_64_safe(&p, end, cookie, bad);
4576 p += 8; /* skip ver */
4577 ceph_decode_64_safe(&p, end, notify_id, bad);
4578
4579 if (proto_ver >= 1) {
4580 ceph_decode_32_safe(&p, end, payload_len, bad);
4581 ceph_decode_need(&p, end, payload_len, bad);
4582 payload = p;
4583 p += payload_len;
4584 }
4585
4586 if (le16_to_cpu(msg->hdr.version) >= 2)
4587 ceph_decode_32_safe(&p, end, return_code, bad);
4588
4589 if (le16_to_cpu(msg->hdr.version) >= 3)
4590 ceph_decode_64_safe(&p, end, notifier_id, bad);
4591
4592 down_read(&osdc->lock);
4593 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4594 if (!lreq) {
4595 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4596 cookie);
4597 goto out_unlock_osdc;
4598 }
4599
4600 mutex_lock(&lreq->lock);
4601 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4602 opcode, cookie, lreq, lreq->is_watch);
4603 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4604 if (!lreq->last_error) {
4605 lreq->last_error = -ENOTCONN;
4606 queue_watch_error(lreq);
4607 }
4608 } else if (!lreq->is_watch) {
4609 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4610 if (lreq->notify_id && lreq->notify_id != notify_id) {
4611 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4612 lreq->notify_id, notify_id);
4613 } else if (!completion_done(&lreq->notify_finish_wait)) {
4614 struct ceph_msg_data *data =
4615 msg->num_data_items ? &msg->data[0] : NULL;
4616
4617 if (data) {
4618 if (lreq->preply_pages) {
4619 WARN_ON(data->type !=
4620 CEPH_MSG_DATA_PAGES);
4621 *lreq->preply_pages = data->pages;
4622 *lreq->preply_len = data->length;
4623 data->own_pages = false;
4624 }
4625 }
4626 lreq->notify_finish_error = return_code;
4627 complete_all(&lreq->notify_finish_wait);
4628 }
4629 } else {
4630 /* CEPH_WATCH_EVENT_NOTIFY */
4631 lwork = lwork_alloc(lreq, do_watch_notify);
4632 if (!lwork) {
4633 pr_err("failed to allocate notify-lwork\n");
4634 goto out_unlock_lreq;
4635 }
4636
4637 lwork->notify.notify_id = notify_id;
4638 lwork->notify.notifier_id = notifier_id;
4639 lwork->notify.payload = payload;
4640 lwork->notify.payload_len = payload_len;
4641 lwork->notify.msg = ceph_msg_get(msg);
4642 lwork_queue(lwork);
4643 }
4644
4645 out_unlock_lreq:
4646 mutex_unlock(&lreq->lock);
4647 out_unlock_osdc:
4648 up_read(&osdc->lock);
4649 return;
4650
4651 bad:
4652 pr_err("osdc handle_watch_notify corrupt msg\n");
4653 }
4654
4655 /*
4656 * Register request, send initial attempt.
4657 */
ceph_osdc_start_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)4658 void ceph_osdc_start_request(struct ceph_osd_client *osdc,
4659 struct ceph_osd_request *req)
4660 {
4661 down_read(&osdc->lock);
4662 submit_request(req, false);
4663 up_read(&osdc->lock);
4664 }
4665 EXPORT_SYMBOL(ceph_osdc_start_request);
4666
4667 /*
4668 * Unregister request. If @req was registered, it isn't completed:
4669 * r_result isn't set and __complete_request() isn't invoked.
4670 *
4671 * If @req wasn't registered, this call may have raced with
4672 * handle_reply(), in which case r_result would already be set and
4673 * __complete_request() would be getting invoked, possibly even
4674 * concurrently with this call.
4675 */
ceph_osdc_cancel_request(struct ceph_osd_request * req)4676 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4677 {
4678 struct ceph_osd_client *osdc = req->r_osdc;
4679
4680 down_write(&osdc->lock);
4681 if (req->r_osd)
4682 cancel_request(req);
4683 up_write(&osdc->lock);
4684 }
4685 EXPORT_SYMBOL(ceph_osdc_cancel_request);
4686
4687 /*
4688 * @timeout: in jiffies, 0 means "wait forever"
4689 */
wait_request_timeout(struct ceph_osd_request * req,unsigned long timeout)4690 static int wait_request_timeout(struct ceph_osd_request *req,
4691 unsigned long timeout)
4692 {
4693 long left;
4694
4695 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
4696 left = wait_for_completion_killable_timeout(&req->r_completion,
4697 ceph_timeout_jiffies(timeout));
4698 if (left <= 0) {
4699 left = left ?: -ETIMEDOUT;
4700 ceph_osdc_cancel_request(req);
4701 } else {
4702 left = req->r_result; /* completed */
4703 }
4704
4705 return left;
4706 }
4707
4708 /*
4709 * wait for a request to complete
4710 */
ceph_osdc_wait_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)4711 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4712 struct ceph_osd_request *req)
4713 {
4714 return wait_request_timeout(req, 0);
4715 }
4716 EXPORT_SYMBOL(ceph_osdc_wait_request);
4717
4718 /*
4719 * sync - wait for all in-flight requests to flush. avoid starvation.
4720 */
ceph_osdc_sync(struct ceph_osd_client * osdc)4721 void ceph_osdc_sync(struct ceph_osd_client *osdc)
4722 {
4723 struct rb_node *n, *p;
4724 u64 last_tid = atomic64_read(&osdc->last_tid);
4725
4726 again:
4727 down_read(&osdc->lock);
4728 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4729 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4730
4731 mutex_lock(&osd->lock);
4732 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4733 struct ceph_osd_request *req =
4734 rb_entry(p, struct ceph_osd_request, r_node);
4735
4736 if (req->r_tid > last_tid)
4737 break;
4738
4739 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4740 continue;
4741
4742 ceph_osdc_get_request(req);
4743 mutex_unlock(&osd->lock);
4744 up_read(&osdc->lock);
4745 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4746 __func__, req, req->r_tid, last_tid);
4747 wait_for_completion(&req->r_completion);
4748 ceph_osdc_put_request(req);
4749 goto again;
4750 }
4751
4752 mutex_unlock(&osd->lock);
4753 }
4754
4755 up_read(&osdc->lock);
4756 dout("%s done last_tid %llu\n", __func__, last_tid);
4757 }
4758 EXPORT_SYMBOL(ceph_osdc_sync);
4759
4760 /*
4761 * Returns a handle, caller owns a ref.
4762 */
4763 struct ceph_osd_linger_request *
ceph_osdc_watch(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,rados_watchcb2_t wcb,rados_watcherrcb_t errcb,void * data)4764 ceph_osdc_watch(struct ceph_osd_client *osdc,
4765 struct ceph_object_id *oid,
4766 struct ceph_object_locator *oloc,
4767 rados_watchcb2_t wcb,
4768 rados_watcherrcb_t errcb,
4769 void *data)
4770 {
4771 struct ceph_osd_linger_request *lreq;
4772 int ret;
4773
4774 lreq = linger_alloc(osdc);
4775 if (!lreq)
4776 return ERR_PTR(-ENOMEM);
4777
4778 lreq->is_watch = true;
4779 lreq->wcb = wcb;
4780 lreq->errcb = errcb;
4781 lreq->data = data;
4782 lreq->watch_valid_thru = jiffies;
4783
4784 ceph_oid_copy(&lreq->t.base_oid, oid);
4785 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4786 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4787 ktime_get_real_ts64(&lreq->mtime);
4788
4789 linger_submit(lreq);
4790 ret = linger_reg_commit_wait(lreq);
4791 if (ret) {
4792 linger_cancel(lreq);
4793 goto err_put_lreq;
4794 }
4795
4796 return lreq;
4797
4798 err_put_lreq:
4799 linger_put(lreq);
4800 return ERR_PTR(ret);
4801 }
4802 EXPORT_SYMBOL(ceph_osdc_watch);
4803
4804 /*
4805 * Releases a ref.
4806 *
4807 * Times out after mount_timeout to preserve rbd unmap behaviour
4808 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4809 * with mount_timeout").
4810 */
ceph_osdc_unwatch(struct ceph_osd_client * osdc,struct ceph_osd_linger_request * lreq)4811 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4812 struct ceph_osd_linger_request *lreq)
4813 {
4814 struct ceph_options *opts = osdc->client->options;
4815 struct ceph_osd_request *req;
4816 int ret;
4817
4818 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4819 if (!req)
4820 return -ENOMEM;
4821
4822 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4823 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4824 req->r_flags = CEPH_OSD_FLAG_WRITE;
4825 ktime_get_real_ts64(&req->r_mtime);
4826 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH,
4827 lreq->linger_id, 0);
4828
4829 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4830 if (ret)
4831 goto out_put_req;
4832
4833 ceph_osdc_start_request(osdc, req);
4834 linger_cancel(lreq);
4835 linger_put(lreq);
4836 ret = wait_request_timeout(req, opts->mount_timeout);
4837
4838 out_put_req:
4839 ceph_osdc_put_request(req);
4840 return ret;
4841 }
4842 EXPORT_SYMBOL(ceph_osdc_unwatch);
4843
osd_req_op_notify_ack_init(struct ceph_osd_request * req,int which,u64 notify_id,u64 cookie,void * payload,u32 payload_len)4844 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4845 u64 notify_id, u64 cookie, void *payload,
4846 u32 payload_len)
4847 {
4848 struct ceph_osd_req_op *op;
4849 struct ceph_pagelist *pl;
4850 int ret;
4851
4852 op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4853
4854 pl = ceph_pagelist_alloc(GFP_NOIO);
4855 if (!pl)
4856 return -ENOMEM;
4857
4858 ret = ceph_pagelist_encode_64(pl, notify_id);
4859 ret |= ceph_pagelist_encode_64(pl, cookie);
4860 if (payload) {
4861 ret |= ceph_pagelist_encode_32(pl, payload_len);
4862 ret |= ceph_pagelist_append(pl, payload, payload_len);
4863 } else {
4864 ret |= ceph_pagelist_encode_32(pl, 0);
4865 }
4866 if (ret) {
4867 ceph_pagelist_release(pl);
4868 return -ENOMEM;
4869 }
4870
4871 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4872 op->indata_len = pl->length;
4873 return 0;
4874 }
4875
ceph_osdc_notify_ack(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,u64 notify_id,u64 cookie,void * payload,u32 payload_len)4876 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4877 struct ceph_object_id *oid,
4878 struct ceph_object_locator *oloc,
4879 u64 notify_id,
4880 u64 cookie,
4881 void *payload,
4882 u32 payload_len)
4883 {
4884 struct ceph_osd_request *req;
4885 int ret;
4886
4887 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4888 if (!req)
4889 return -ENOMEM;
4890
4891 ceph_oid_copy(&req->r_base_oid, oid);
4892 ceph_oloc_copy(&req->r_base_oloc, oloc);
4893 req->r_flags = CEPH_OSD_FLAG_READ;
4894
4895 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4896 payload_len);
4897 if (ret)
4898 goto out_put_req;
4899
4900 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4901 if (ret)
4902 goto out_put_req;
4903
4904 ceph_osdc_start_request(osdc, req);
4905 ret = ceph_osdc_wait_request(osdc, req);
4906
4907 out_put_req:
4908 ceph_osdc_put_request(req);
4909 return ret;
4910 }
4911 EXPORT_SYMBOL(ceph_osdc_notify_ack);
4912
4913 /*
4914 * @timeout: in seconds
4915 *
4916 * @preply_{pages,len} are initialized both on success and error.
4917 * The caller is responsible for:
4918 *
4919 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4920 */
ceph_osdc_notify(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,void * payload,u32 payload_len,u32 timeout,struct page *** preply_pages,size_t * preply_len)4921 int ceph_osdc_notify(struct ceph_osd_client *osdc,
4922 struct ceph_object_id *oid,
4923 struct ceph_object_locator *oloc,
4924 void *payload,
4925 u32 payload_len,
4926 u32 timeout,
4927 struct page ***preply_pages,
4928 size_t *preply_len)
4929 {
4930 struct ceph_osd_linger_request *lreq;
4931 int ret;
4932
4933 WARN_ON(!timeout);
4934 if (preply_pages) {
4935 *preply_pages = NULL;
4936 *preply_len = 0;
4937 }
4938
4939 lreq = linger_alloc(osdc);
4940 if (!lreq)
4941 return -ENOMEM;
4942
4943 lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO);
4944 if (!lreq->request_pl) {
4945 ret = -ENOMEM;
4946 goto out_put_lreq;
4947 }
4948
4949 ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */
4950 ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout);
4951 ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len);
4952 ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len);
4953 if (ret) {
4954 ret = -ENOMEM;
4955 goto out_put_lreq;
4956 }
4957
4958 /* for notify_id */
4959 lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO);
4960 if (IS_ERR(lreq->notify_id_pages)) {
4961 ret = PTR_ERR(lreq->notify_id_pages);
4962 lreq->notify_id_pages = NULL;
4963 goto out_put_lreq;
4964 }
4965
4966 lreq->preply_pages = preply_pages;
4967 lreq->preply_len = preply_len;
4968
4969 ceph_oid_copy(&lreq->t.base_oid, oid);
4970 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4971 lreq->t.flags = CEPH_OSD_FLAG_READ;
4972
4973 linger_submit(lreq);
4974 ret = linger_reg_commit_wait(lreq);
4975 if (!ret)
4976 ret = linger_notify_finish_wait(lreq,
4977 msecs_to_jiffies(2 * timeout * MSEC_PER_SEC));
4978 else
4979 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4980
4981 linger_cancel(lreq);
4982 out_put_lreq:
4983 linger_put(lreq);
4984 return ret;
4985 }
4986 EXPORT_SYMBOL(ceph_osdc_notify);
4987
decode_watcher(void ** p,void * end,struct ceph_watch_item * item)4988 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4989 {
4990 u8 struct_v;
4991 u32 struct_len;
4992 int ret;
4993
4994 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4995 &struct_v, &struct_len);
4996 if (ret)
4997 goto bad;
4998
4999 ret = -EINVAL;
5000 ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
5001 ceph_decode_64_safe(p, end, item->cookie, bad);
5002 ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
5003
5004 if (struct_v >= 2) {
5005 ret = ceph_decode_entity_addr(p, end, &item->addr);
5006 if (ret)
5007 goto bad;
5008 } else {
5009 ret = 0;
5010 }
5011
5012 dout("%s %s%llu cookie %llu addr %s\n", __func__,
5013 ENTITY_NAME(item->name), item->cookie,
5014 ceph_pr_addr(&item->addr));
5015 bad:
5016 return ret;
5017 }
5018
decode_watchers(void ** p,void * end,struct ceph_watch_item ** watchers,u32 * num_watchers)5019 static int decode_watchers(void **p, void *end,
5020 struct ceph_watch_item **watchers,
5021 u32 *num_watchers)
5022 {
5023 u8 struct_v;
5024 u32 struct_len;
5025 int i;
5026 int ret;
5027
5028 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
5029 &struct_v, &struct_len);
5030 if (ret)
5031 return ret;
5032
5033 *num_watchers = ceph_decode_32(p);
5034 *watchers = kzalloc_objs(**watchers, *num_watchers, GFP_NOIO);
5035 if (!*watchers)
5036 return -ENOMEM;
5037
5038 for (i = 0; i < *num_watchers; i++) {
5039 ret = decode_watcher(p, end, *watchers + i);
5040 if (ret) {
5041 kfree(*watchers);
5042 return ret;
5043 }
5044 }
5045
5046 return 0;
5047 }
5048
5049 /*
5050 * On success, the caller is responsible for:
5051 *
5052 * kfree(watchers);
5053 */
ceph_osdc_list_watchers(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,struct ceph_watch_item ** watchers,u32 * num_watchers)5054 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
5055 struct ceph_object_id *oid,
5056 struct ceph_object_locator *oloc,
5057 struct ceph_watch_item **watchers,
5058 u32 *num_watchers)
5059 {
5060 struct ceph_osd_request *req;
5061 struct page **pages;
5062 int ret;
5063
5064 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5065 if (!req)
5066 return -ENOMEM;
5067
5068 ceph_oid_copy(&req->r_base_oid, oid);
5069 ceph_oloc_copy(&req->r_base_oloc, oloc);
5070 req->r_flags = CEPH_OSD_FLAG_READ;
5071
5072 pages = ceph_alloc_page_vector(1, GFP_NOIO);
5073 if (IS_ERR(pages)) {
5074 ret = PTR_ERR(pages);
5075 goto out_put_req;
5076 }
5077
5078 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
5079 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
5080 response_data),
5081 pages, PAGE_SIZE, 0, false, true);
5082
5083 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
5084 if (ret)
5085 goto out_put_req;
5086
5087 ceph_osdc_start_request(osdc, req);
5088 ret = ceph_osdc_wait_request(osdc, req);
5089 if (ret >= 0) {
5090 void *p = page_address(pages[0]);
5091 void *const end = p + req->r_ops[0].outdata_len;
5092
5093 ret = decode_watchers(&p, end, watchers, num_watchers);
5094 }
5095
5096 out_put_req:
5097 ceph_osdc_put_request(req);
5098 return ret;
5099 }
5100 EXPORT_SYMBOL(ceph_osdc_list_watchers);
5101
5102 /*
5103 * Call all pending notify callbacks - for use after a watch is
5104 * unregistered, to make sure no more callbacks for it will be invoked
5105 */
ceph_osdc_flush_notifies(struct ceph_osd_client * osdc)5106 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5107 {
5108 dout("%s osdc %p\n", __func__, osdc);
5109 flush_workqueue(osdc->notify_wq);
5110 }
5111 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5112
ceph_osdc_maybe_request_map(struct ceph_osd_client * osdc)5113 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
5114 {
5115 down_read(&osdc->lock);
5116 maybe_request_map(osdc);
5117 up_read(&osdc->lock);
5118 }
5119 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5120
5121 /*
5122 * Execute an OSD class method on an object.
5123 *
5124 * @flags: CEPH_OSD_FLAG_*
5125 * @resp_len: in/out param for reply length
5126 */
ceph_osdc_call(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,const char * class,const char * method,unsigned int flags,struct page * req_page,size_t req_len,struct page ** resp_pages,size_t * resp_len)5127 int ceph_osdc_call(struct ceph_osd_client *osdc,
5128 struct ceph_object_id *oid,
5129 struct ceph_object_locator *oloc,
5130 const char *class, const char *method,
5131 unsigned int flags,
5132 struct page *req_page, size_t req_len,
5133 struct page **resp_pages, size_t *resp_len)
5134 {
5135 struct ceph_osd_request *req;
5136 int ret;
5137
5138 if (req_len > PAGE_SIZE)
5139 return -E2BIG;
5140
5141 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5142 if (!req)
5143 return -ENOMEM;
5144
5145 ceph_oid_copy(&req->r_base_oid, oid);
5146 ceph_oloc_copy(&req->r_base_oloc, oloc);
5147 req->r_flags = flags;
5148
5149 ret = osd_req_op_cls_init(req, 0, class, method);
5150 if (ret)
5151 goto out_put_req;
5152
5153 if (req_page)
5154 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5155 0, false, false);
5156 if (resp_pages)
5157 osd_req_op_cls_response_data_pages(req, 0, resp_pages,
5158 *resp_len, 0, false, false);
5159
5160 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
5161 if (ret)
5162 goto out_put_req;
5163
5164 ceph_osdc_start_request(osdc, req);
5165 ret = ceph_osdc_wait_request(osdc, req);
5166 if (ret >= 0) {
5167 ret = req->r_ops[0].rval;
5168 if (resp_pages)
5169 *resp_len = req->r_ops[0].outdata_len;
5170 }
5171
5172 out_put_req:
5173 ceph_osdc_put_request(req);
5174 return ret;
5175 }
5176 EXPORT_SYMBOL(ceph_osdc_call);
5177
5178 /*
5179 * reset all osd connections
5180 */
ceph_osdc_reopen_osds(struct ceph_osd_client * osdc)5181 void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5182 {
5183 struct rb_node *n;
5184
5185 down_write(&osdc->lock);
5186 for (n = rb_first(&osdc->osds); n; ) {
5187 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5188
5189 n = rb_next(n);
5190 if (!reopen_osd(osd))
5191 kick_osd_requests(osd);
5192 }
5193 up_write(&osdc->lock);
5194 }
5195
5196 /*
5197 * init, shutdown
5198 */
ceph_osdc_init(struct ceph_osd_client * osdc,struct ceph_client * client)5199 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5200 {
5201 int err;
5202
5203 dout("init\n");
5204 osdc->client = client;
5205 init_rwsem(&osdc->lock);
5206 osdc->osds = RB_ROOT;
5207 INIT_LIST_HEAD(&osdc->osd_lru);
5208 spin_lock_init(&osdc->osd_lru_lock);
5209 osd_init(&osdc->homeless_osd);
5210 osdc->homeless_osd.o_osdc = osdc;
5211 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5212 osdc->last_linger_id = CEPH_LINGER_ID_START;
5213 osdc->linger_requests = RB_ROOT;
5214 osdc->map_checks = RB_ROOT;
5215 osdc->linger_map_checks = RB_ROOT;
5216 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
5217 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5218
5219 err = -ENOMEM;
5220 osdc->osdmap = ceph_osdmap_alloc();
5221 if (!osdc->osdmap)
5222 goto out;
5223
5224 osdc->req_mempool = mempool_create_slab_pool(10,
5225 ceph_osd_request_cache);
5226 if (!osdc->req_mempool)
5227 goto out_map;
5228
5229 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
5230 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
5231 if (err < 0)
5232 goto out_mempool;
5233 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
5234 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
5235 "osd_op_reply");
5236 if (err < 0)
5237 goto out_msgpool;
5238
5239 err = -ENOMEM;
5240 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5241 if (!osdc->notify_wq)
5242 goto out_msgpool_reply;
5243
5244 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5245 if (!osdc->completion_wq)
5246 goto out_notify_wq;
5247
5248 schedule_delayed_work(&osdc->timeout_work,
5249 osdc->client->options->osd_keepalive_timeout);
5250 schedule_delayed_work(&osdc->osds_timeout_work,
5251 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5252
5253 return 0;
5254
5255 out_notify_wq:
5256 destroy_workqueue(osdc->notify_wq);
5257 out_msgpool_reply:
5258 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5259 out_msgpool:
5260 ceph_msgpool_destroy(&osdc->msgpool_op);
5261 out_mempool:
5262 mempool_destroy(osdc->req_mempool);
5263 out_map:
5264 ceph_osdmap_destroy(osdc->osdmap);
5265 out:
5266 return err;
5267 }
5268
ceph_osdc_stop(struct ceph_osd_client * osdc)5269 void ceph_osdc_stop(struct ceph_osd_client *osdc)
5270 {
5271 destroy_workqueue(osdc->completion_wq);
5272 destroy_workqueue(osdc->notify_wq);
5273 cancel_delayed_work_sync(&osdc->timeout_work);
5274 cancel_delayed_work_sync(&osdc->osds_timeout_work);
5275
5276 down_write(&osdc->lock);
5277 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5278 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5279 struct ceph_osd, o_node);
5280 close_osd(osd);
5281 }
5282 up_write(&osdc->lock);
5283 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5284 osd_cleanup(&osdc->homeless_osd);
5285
5286 WARN_ON(!list_empty(&osdc->osd_lru));
5287 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
5288 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5289 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5290 WARN_ON(atomic_read(&osdc->num_requests));
5291 WARN_ON(atomic_read(&osdc->num_homeless));
5292
5293 ceph_osdmap_destroy(osdc->osdmap);
5294 mempool_destroy(osdc->req_mempool);
5295 ceph_msgpool_destroy(&osdc->msgpool_op);
5296 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5297 }
5298
osd_req_op_copy_from_init(struct ceph_osd_request * req,u64 src_snapid,u64 src_version,struct ceph_object_id * src_oid,struct ceph_object_locator * src_oloc,u32 src_fadvise_flags,u32 dst_fadvise_flags,u32 truncate_seq,u64 truncate_size,u8 copy_from_flags)5299 int osd_req_op_copy_from_init(struct ceph_osd_request *req,
5300 u64 src_snapid, u64 src_version,
5301 struct ceph_object_id *src_oid,
5302 struct ceph_object_locator *src_oloc,
5303 u32 src_fadvise_flags,
5304 u32 dst_fadvise_flags,
5305 u32 truncate_seq, u64 truncate_size,
5306 u8 copy_from_flags)
5307 {
5308 struct ceph_osd_req_op *op;
5309 struct page **pages;
5310 void *p, *end;
5311
5312 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
5313 if (IS_ERR(pages))
5314 return PTR_ERR(pages);
5315
5316 op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
5317 dst_fadvise_flags);
5318 op->copy_from.snapid = src_snapid;
5319 op->copy_from.src_version = src_version;
5320 op->copy_from.flags = copy_from_flags;
5321 op->copy_from.src_fadvise_flags = src_fadvise_flags;
5322
5323 p = page_address(pages[0]);
5324 end = p + PAGE_SIZE;
5325 ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
5326 encode_oloc(&p, end, src_oloc);
5327 ceph_encode_32(&p, truncate_seq);
5328 ceph_encode_64(&p, truncate_size);
5329 op->indata_len = PAGE_SIZE - (end - p);
5330
5331 ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
5332 op->indata_len, 0, false, true);
5333 return 0;
5334 }
5335 EXPORT_SYMBOL(osd_req_op_copy_from_init);
5336
ceph_osdc_setup(void)5337 int __init ceph_osdc_setup(void)
5338 {
5339 size_t size = sizeof(struct ceph_osd_request) +
5340 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5341
5342 BUG_ON(ceph_osd_request_cache);
5343 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5344 0, 0, NULL);
5345
5346 return ceph_osd_request_cache ? 0 : -ENOMEM;
5347 }
5348
ceph_osdc_cleanup(void)5349 void ceph_osdc_cleanup(void)
5350 {
5351 BUG_ON(!ceph_osd_request_cache);
5352 kmem_cache_destroy(ceph_osd_request_cache);
5353 ceph_osd_request_cache = NULL;
5354 }
5355
5356 /*
5357 * handle incoming message
5358 */
osd_dispatch(struct ceph_connection * con,struct ceph_msg * msg)5359 static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5360 {
5361 struct ceph_osd *osd = con->private;
5362 struct ceph_osd_client *osdc = osd->o_osdc;
5363 int type = le16_to_cpu(msg->hdr.type);
5364
5365 switch (type) {
5366 case CEPH_MSG_OSD_MAP:
5367 ceph_osdc_handle_map(osdc, msg);
5368 break;
5369 case CEPH_MSG_OSD_OPREPLY:
5370 handle_reply(osd, msg);
5371 break;
5372 case CEPH_MSG_OSD_BACKOFF:
5373 handle_backoff(osd, msg);
5374 break;
5375 case CEPH_MSG_WATCH_NOTIFY:
5376 handle_watch_notify(osdc, msg);
5377 break;
5378
5379 default:
5380 pr_err("received unknown message type %d %s\n", type,
5381 ceph_msg_type_name(type));
5382 }
5383
5384 ceph_msg_put(msg);
5385 }
5386
5387 /* How much sparse data was requested? */
sparse_data_requested(struct ceph_osd_request * req)5388 static u64 sparse_data_requested(struct ceph_osd_request *req)
5389 {
5390 u64 len = 0;
5391
5392 if (req->r_flags & CEPH_OSD_FLAG_READ) {
5393 int i;
5394
5395 for (i = 0; i < req->r_num_ops; ++i) {
5396 struct ceph_osd_req_op *op = &req->r_ops[i];
5397
5398 if (op->op == CEPH_OSD_OP_SPARSE_READ)
5399 len += op->extent.length;
5400 }
5401 }
5402 return len;
5403 }
5404
5405 /*
5406 * Lookup and return message for incoming reply. Don't try to do
5407 * anything about a larger than preallocated data portion of the
5408 * message at the moment - for now, just skip the message.
5409 */
get_reply(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)5410 static struct ceph_msg *get_reply(struct ceph_connection *con,
5411 struct ceph_msg_header *hdr,
5412 int *skip)
5413 {
5414 struct ceph_osd *osd = con->private;
5415 struct ceph_osd_client *osdc = osd->o_osdc;
5416 struct ceph_msg *m = NULL;
5417 struct ceph_osd_request *req;
5418 int front_len = le32_to_cpu(hdr->front_len);
5419 int data_len = le32_to_cpu(hdr->data_len);
5420 u64 tid = le64_to_cpu(hdr->tid);
5421 u64 srlen;
5422
5423 down_read(&osdc->lock);
5424 if (!osd_registered(osd)) {
5425 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5426 *skip = 1;
5427 goto out_unlock_osdc;
5428 }
5429 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5430
5431 mutex_lock(&osd->lock);
5432 req = lookup_request(&osd->o_requests, tid);
5433 if (!req) {
5434 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5435 osd->o_osd, tid);
5436 *skip = 1;
5437 goto out_unlock_session;
5438 }
5439
5440 ceph_msg_revoke_incoming(req->r_reply);
5441
5442 if (front_len > req->r_reply->front_alloc_len) {
5443 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5444 __func__, osd->o_osd, req->r_tid, front_len,
5445 req->r_reply->front_alloc_len);
5446 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5447 false);
5448 if (!m)
5449 goto out_unlock_session;
5450 ceph_msg_put(req->r_reply);
5451 req->r_reply = m;
5452 }
5453
5454 srlen = sparse_data_requested(req);
5455 if (!srlen && data_len > req->r_reply->data_length) {
5456 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5457 __func__, osd->o_osd, req->r_tid, data_len,
5458 req->r_reply->data_length);
5459 m = NULL;
5460 *skip = 1;
5461 goto out_unlock_session;
5462 }
5463
5464 m = ceph_msg_get(req->r_reply);
5465 m->sparse_read_total = srlen;
5466
5467 dout("get_reply tid %lld %p\n", tid, m);
5468
5469 out_unlock_session:
5470 mutex_unlock(&osd->lock);
5471 out_unlock_osdc:
5472 up_read(&osdc->lock);
5473 return m;
5474 }
5475
alloc_msg_with_page_vector(struct ceph_msg_header * hdr)5476 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5477 {
5478 struct ceph_msg *m;
5479 int type = le16_to_cpu(hdr->type);
5480 u32 front_len = le32_to_cpu(hdr->front_len);
5481 u32 data_len = le32_to_cpu(hdr->data_len);
5482
5483 m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
5484 if (!m)
5485 return NULL;
5486
5487 if (data_len) {
5488 struct page **pages;
5489
5490 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5491 GFP_NOIO);
5492 if (IS_ERR(pages)) {
5493 ceph_msg_put(m);
5494 return NULL;
5495 }
5496
5497 ceph_msg_data_add_pages(m, pages, data_len, 0, true);
5498 }
5499
5500 return m;
5501 }
5502
osd_alloc_msg(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)5503 static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con,
5504 struct ceph_msg_header *hdr,
5505 int *skip)
5506 {
5507 struct ceph_osd *osd = con->private;
5508 int type = le16_to_cpu(hdr->type);
5509
5510 *skip = 0;
5511 switch (type) {
5512 case CEPH_MSG_OSD_MAP:
5513 case CEPH_MSG_OSD_BACKOFF:
5514 case CEPH_MSG_WATCH_NOTIFY:
5515 return alloc_msg_with_page_vector(hdr);
5516 case CEPH_MSG_OSD_OPREPLY:
5517 return get_reply(con, hdr, skip);
5518 default:
5519 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5520 osd->o_osd, type);
5521 *skip = 1;
5522 return NULL;
5523 }
5524 }
5525
5526 /*
5527 * Wrappers to refcount containing ceph_osd struct
5528 */
osd_get_con(struct ceph_connection * con)5529 static struct ceph_connection *osd_get_con(struct ceph_connection *con)
5530 {
5531 struct ceph_osd *osd = con->private;
5532 if (get_osd(osd))
5533 return con;
5534 return NULL;
5535 }
5536
osd_put_con(struct ceph_connection * con)5537 static void osd_put_con(struct ceph_connection *con)
5538 {
5539 struct ceph_osd *osd = con->private;
5540 put_osd(osd);
5541 }
5542
5543 /*
5544 * authentication
5545 */
5546
5547 /*
5548 * Note: returned pointer is the address of a structure that's
5549 * managed separately. Caller must *not* attempt to free it.
5550 */
5551 static struct ceph_auth_handshake *
osd_get_authorizer(struct ceph_connection * con,int * proto,int force_new)5552 osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
5553 {
5554 struct ceph_osd *o = con->private;
5555 struct ceph_osd_client *osdc = o->o_osdc;
5556 struct ceph_auth_client *ac = osdc->client->monc.auth;
5557 struct ceph_auth_handshake *auth = &o->o_auth;
5558 int ret;
5559
5560 ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5561 force_new, proto, NULL, NULL);
5562 if (ret)
5563 return ERR_PTR(ret);
5564
5565 return auth;
5566 }
5567
osd_add_authorizer_challenge(struct ceph_connection * con,void * challenge_buf,int challenge_buf_len)5568 static int osd_add_authorizer_challenge(struct ceph_connection *con,
5569 void *challenge_buf, int challenge_buf_len)
5570 {
5571 struct ceph_osd *o = con->private;
5572 struct ceph_osd_client *osdc = o->o_osdc;
5573 struct ceph_auth_client *ac = osdc->client->monc.auth;
5574
5575 return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
5576 challenge_buf, challenge_buf_len);
5577 }
5578
osd_verify_authorizer_reply(struct ceph_connection * con)5579 static int osd_verify_authorizer_reply(struct ceph_connection *con)
5580 {
5581 struct ceph_osd *o = con->private;
5582 struct ceph_osd_client *osdc = o->o_osdc;
5583 struct ceph_auth_client *ac = osdc->client->monc.auth;
5584 struct ceph_auth_handshake *auth = &o->o_auth;
5585
5586 return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5587 auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5588 NULL, NULL, NULL, NULL);
5589 }
5590
osd_invalidate_authorizer(struct ceph_connection * con)5591 static int osd_invalidate_authorizer(struct ceph_connection *con)
5592 {
5593 struct ceph_osd *o = con->private;
5594 struct ceph_osd_client *osdc = o->o_osdc;
5595 struct ceph_auth_client *ac = osdc->client->monc.auth;
5596
5597 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
5598 return ceph_monc_validate_auth(&osdc->client->monc);
5599 }
5600
osd_get_auth_request(struct ceph_connection * con,void * buf,int * buf_len,void ** authorizer,int * authorizer_len)5601 static int osd_get_auth_request(struct ceph_connection *con,
5602 void *buf, int *buf_len,
5603 void **authorizer, int *authorizer_len)
5604 {
5605 struct ceph_osd *o = con->private;
5606 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5607 struct ceph_auth_handshake *auth = &o->o_auth;
5608 int ret;
5609
5610 ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5611 buf, buf_len);
5612 if (ret)
5613 return ret;
5614
5615 *authorizer = auth->authorizer_buf;
5616 *authorizer_len = auth->authorizer_buf_len;
5617 return 0;
5618 }
5619
osd_handle_auth_reply_more(struct ceph_connection * con,void * reply,int reply_len,void * buf,int * buf_len,void ** authorizer,int * authorizer_len)5620 static int osd_handle_auth_reply_more(struct ceph_connection *con,
5621 void *reply, int reply_len,
5622 void *buf, int *buf_len,
5623 void **authorizer, int *authorizer_len)
5624 {
5625 struct ceph_osd *o = con->private;
5626 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5627 struct ceph_auth_handshake *auth = &o->o_auth;
5628 int ret;
5629
5630 ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5631 buf, buf_len);
5632 if (ret)
5633 return ret;
5634
5635 *authorizer = auth->authorizer_buf;
5636 *authorizer_len = auth->authorizer_buf_len;
5637 return 0;
5638 }
5639
osd_handle_auth_done(struct ceph_connection * con,u64 global_id,void * reply,int reply_len,u8 * session_key,int * session_key_len,u8 * con_secret,int * con_secret_len)5640 static int osd_handle_auth_done(struct ceph_connection *con,
5641 u64 global_id, void *reply, int reply_len,
5642 u8 *session_key, int *session_key_len,
5643 u8 *con_secret, int *con_secret_len)
5644 {
5645 struct ceph_osd *o = con->private;
5646 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5647 struct ceph_auth_handshake *auth = &o->o_auth;
5648
5649 return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5650 session_key, session_key_len,
5651 con_secret, con_secret_len);
5652 }
5653
osd_handle_auth_bad_method(struct ceph_connection * con,int used_proto,int result,const int * allowed_protos,int proto_cnt,const int * allowed_modes,int mode_cnt)5654 static int osd_handle_auth_bad_method(struct ceph_connection *con,
5655 int used_proto, int result,
5656 const int *allowed_protos, int proto_cnt,
5657 const int *allowed_modes, int mode_cnt)
5658 {
5659 struct ceph_osd *o = con->private;
5660 struct ceph_mon_client *monc = &o->o_osdc->client->monc;
5661 int ret;
5662
5663 if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD,
5664 used_proto, result,
5665 allowed_protos, proto_cnt,
5666 allowed_modes, mode_cnt)) {
5667 ret = ceph_monc_validate_auth(monc);
5668 if (ret)
5669 return ret;
5670 }
5671
5672 return -EACCES;
5673 }
5674
osd_reencode_message(struct ceph_msg * msg)5675 static void osd_reencode_message(struct ceph_msg *msg)
5676 {
5677 int type = le16_to_cpu(msg->hdr.type);
5678
5679 if (type == CEPH_MSG_OSD_OP)
5680 encode_request_finish(msg);
5681 }
5682
osd_sign_message(struct ceph_msg * msg)5683 static int osd_sign_message(struct ceph_msg *msg)
5684 {
5685 struct ceph_osd *o = msg->con->private;
5686 struct ceph_auth_handshake *auth = &o->o_auth;
5687
5688 return ceph_auth_sign_message(auth, msg);
5689 }
5690
osd_check_message_signature(struct ceph_msg * msg)5691 static int osd_check_message_signature(struct ceph_msg *msg)
5692 {
5693 struct ceph_osd *o = msg->con->private;
5694 struct ceph_auth_handshake *auth = &o->o_auth;
5695
5696 return ceph_auth_check_message_signature(auth, msg);
5697 }
5698
advance_cursor(struct ceph_msg_data_cursor * cursor,size_t len,bool zero)5699 static void advance_cursor(struct ceph_msg_data_cursor *cursor, size_t len,
5700 bool zero)
5701 {
5702 while (len) {
5703 struct page *page;
5704 size_t poff, plen;
5705
5706 page = ceph_msg_data_next(cursor, &poff, &plen);
5707 if (plen > len)
5708 plen = len;
5709 if (zero)
5710 zero_user_segment(page, poff, poff + plen);
5711 len -= plen;
5712 ceph_msg_data_advance(cursor, plen);
5713 }
5714 }
5715
prep_next_sparse_read(struct ceph_connection * con,struct ceph_msg_data_cursor * cursor)5716 static int prep_next_sparse_read(struct ceph_connection *con,
5717 struct ceph_msg_data_cursor *cursor)
5718 {
5719 struct ceph_osd *o = con->private;
5720 struct ceph_sparse_read *sr = &o->o_sparse_read;
5721 struct ceph_osd_request *req;
5722 struct ceph_osd_req_op *op;
5723
5724 spin_lock(&o->o_requests_lock);
5725 req = lookup_request(&o->o_requests, le64_to_cpu(con->in_msg->hdr.tid));
5726 if (!req) {
5727 spin_unlock(&o->o_requests_lock);
5728 return -EBADR;
5729 }
5730
5731 if (o->o_sparse_op_idx < 0) {
5732 dout("%s: [%d] starting new sparse read req\n",
5733 __func__, o->o_osd);
5734 } else {
5735 u64 end;
5736
5737 op = &req->r_ops[o->o_sparse_op_idx];
5738
5739 WARN_ON_ONCE(op->extent.sparse_ext);
5740
5741 /* hand back buffer we took earlier */
5742 op->extent.sparse_ext = sr->sr_extent;
5743 sr->sr_extent = NULL;
5744 op->extent.sparse_ext_cnt = sr->sr_count;
5745 sr->sr_ext_len = 0;
5746 dout("%s: [%d] completed extent array len %d cursor->resid %zd\n",
5747 __func__, o->o_osd, op->extent.sparse_ext_cnt, cursor->resid);
5748 /* Advance to end of data for this operation */
5749 end = ceph_sparse_ext_map_end(op);
5750 if (end < sr->sr_req_len)
5751 advance_cursor(cursor, sr->sr_req_len - end, false);
5752 }
5753
5754 ceph_init_sparse_read(sr);
5755
5756 /* find next op in this request (if any) */
5757 while (++o->o_sparse_op_idx < req->r_num_ops) {
5758 op = &req->r_ops[o->o_sparse_op_idx];
5759 if (op->op == CEPH_OSD_OP_SPARSE_READ)
5760 goto found;
5761 }
5762
5763 /* reset for next sparse read request */
5764 spin_unlock(&o->o_requests_lock);
5765 o->o_sparse_op_idx = -1;
5766 return 0;
5767 found:
5768 sr->sr_req_off = op->extent.offset;
5769 sr->sr_req_len = op->extent.length;
5770 sr->sr_pos = sr->sr_req_off;
5771 dout("%s: [%d] new sparse read op at idx %d 0x%llx~0x%llx\n", __func__,
5772 o->o_osd, o->o_sparse_op_idx, sr->sr_req_off, sr->sr_req_len);
5773
5774 /* hand off request's sparse extent map buffer */
5775 sr->sr_ext_len = op->extent.sparse_ext_cnt;
5776 op->extent.sparse_ext_cnt = 0;
5777 sr->sr_extent = op->extent.sparse_ext;
5778 op->extent.sparse_ext = NULL;
5779
5780 spin_unlock(&o->o_requests_lock);
5781 return 1;
5782 }
5783
5784 #ifdef __BIG_ENDIAN
convert_extent_map(struct ceph_sparse_read * sr)5785 static inline void convert_extent_map(struct ceph_sparse_read *sr)
5786 {
5787 int i;
5788
5789 for (i = 0; i < sr->sr_count; i++) {
5790 struct ceph_sparse_extent *ext = &sr->sr_extent[i];
5791
5792 ext->off = le64_to_cpu((__force __le64)ext->off);
5793 ext->len = le64_to_cpu((__force __le64)ext->len);
5794 }
5795 }
5796 #else
convert_extent_map(struct ceph_sparse_read * sr)5797 static inline void convert_extent_map(struct ceph_sparse_read *sr)
5798 {
5799 }
5800 #endif
5801
osd_sparse_read(struct ceph_connection * con,struct ceph_msg_data_cursor * cursor,char ** pbuf)5802 static int osd_sparse_read(struct ceph_connection *con,
5803 struct ceph_msg_data_cursor *cursor,
5804 char **pbuf)
5805 {
5806 struct ceph_osd *o = con->private;
5807 struct ceph_sparse_read *sr = &o->o_sparse_read;
5808 u32 count = sr->sr_count;
5809 u64 eoff, elen, len = 0;
5810 int i, ret;
5811
5812 switch (sr->sr_state) {
5813 case CEPH_SPARSE_READ_HDR:
5814 next_op:
5815 ret = prep_next_sparse_read(con, cursor);
5816 if (ret <= 0)
5817 return ret;
5818
5819 /* number of extents */
5820 ret = sizeof(sr->sr_count);
5821 *pbuf = (char *)&sr->sr_count;
5822 sr->sr_state = CEPH_SPARSE_READ_EXTENTS;
5823 break;
5824 case CEPH_SPARSE_READ_EXTENTS:
5825 /* Convert sr_count to host-endian */
5826 count = le32_to_cpu((__force __le32)sr->sr_count);
5827 sr->sr_count = count;
5828 dout("[%d] got %u extents\n", o->o_osd, count);
5829
5830 if (count > 0) {
5831 if (!sr->sr_extent || count > sr->sr_ext_len) {
5832 /* no extent array provided, or too short */
5833 kfree(sr->sr_extent);
5834 sr->sr_extent = kmalloc_objs(*sr->sr_extent,
5835 count, GFP_NOIO);
5836 if (!sr->sr_extent) {
5837 pr_err("%s: failed to allocate %u extents\n",
5838 __func__, count);
5839 return -ENOMEM;
5840 }
5841 sr->sr_ext_len = count;
5842 }
5843 ret = count * sizeof(*sr->sr_extent);
5844 *pbuf = (char *)sr->sr_extent;
5845 sr->sr_state = CEPH_SPARSE_READ_DATA_LEN;
5846 break;
5847 }
5848 /* No extents? Read data len */
5849 fallthrough;
5850 case CEPH_SPARSE_READ_DATA_LEN:
5851 convert_extent_map(sr);
5852 ret = sizeof(sr->sr_datalen);
5853 *pbuf = (char *)&sr->sr_datalen;
5854 sr->sr_state = CEPH_SPARSE_READ_DATA_PRE;
5855 break;
5856 case CEPH_SPARSE_READ_DATA_PRE:
5857 /* Convert sr_datalen to host-endian */
5858 sr->sr_datalen = le32_to_cpu((__force __le32)sr->sr_datalen);
5859 for (i = 0; i < count; i++)
5860 len += sr->sr_extent[i].len;
5861 if (sr->sr_datalen != len) {
5862 pr_warn_ratelimited("data len %u != extent len %llu\n",
5863 sr->sr_datalen, len);
5864 return -EREMOTEIO;
5865 }
5866 sr->sr_state = CEPH_SPARSE_READ_DATA;
5867 fallthrough;
5868 case CEPH_SPARSE_READ_DATA:
5869 if (sr->sr_index >= count) {
5870 sr->sr_state = CEPH_SPARSE_READ_HDR;
5871 goto next_op;
5872 }
5873
5874 eoff = sr->sr_extent[sr->sr_index].off;
5875 elen = sr->sr_extent[sr->sr_index].len;
5876
5877 dout("[%d] ext %d off 0x%llx len 0x%llx\n",
5878 o->o_osd, sr->sr_index, eoff, elen);
5879
5880 if (elen > INT_MAX) {
5881 dout("Sparse read extent length too long (0x%llx)\n",
5882 elen);
5883 return -EREMOTEIO;
5884 }
5885
5886 /* zero out anything from sr_pos to start of extent */
5887 if (sr->sr_pos < eoff)
5888 advance_cursor(cursor, eoff - sr->sr_pos, true);
5889
5890 /* Set position to end of extent */
5891 sr->sr_pos = eoff + elen;
5892
5893 /* send back the new length and nullify the ptr */
5894 cursor->sr_resid = elen;
5895 ret = elen;
5896 *pbuf = NULL;
5897
5898 /* Bump the array index */
5899 ++sr->sr_index;
5900 break;
5901 }
5902 return ret;
5903 }
5904
5905 static const struct ceph_connection_operations osd_con_ops = {
5906 .get = osd_get_con,
5907 .put = osd_put_con,
5908 .sparse_read = osd_sparse_read,
5909 .alloc_msg = osd_alloc_msg,
5910 .dispatch = osd_dispatch,
5911 .fault = osd_fault,
5912 .reencode_message = osd_reencode_message,
5913 .get_authorizer = osd_get_authorizer,
5914 .add_authorizer_challenge = osd_add_authorizer_challenge,
5915 .verify_authorizer_reply = osd_verify_authorizer_reply,
5916 .invalidate_authorizer = osd_invalidate_authorizer,
5917 .sign_message = osd_sign_message,
5918 .check_message_signature = osd_check_message_signature,
5919 .get_auth_request = osd_get_auth_request,
5920 .handle_auth_reply_more = osd_handle_auth_reply_more,
5921 .handle_auth_done = osd_handle_auth_done,
5922 .handle_auth_bad_method = osd_handle_auth_bad_method,
5923 };
5924