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