blob: a7e090d2c9571e2b8e85c24793511c0046997b83 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Alex Eldera4ce40a2013-04-05 01:27:12 -05002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07004
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07006#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>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#ifdef CONFIG_BLOCK
13#include <linux/bio.h>
14#endif
Sage Weilf24e9982009-10-06 11:31:10 -070015
Ilya Dryomov8cb441c2017-06-15 16:30:54 +020016#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#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>
Ilya Dryomov08c1ac52018-02-17 10:41:20 +010023#include <linux/ceph/striper.h>
Sage Weilf24e9982009-10-06 11:31:10 -070024
Sage Weilc16e7862010-03-01 13:02:00 -080025#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080026
Alex Elder5522ae02013-05-01 12:43:04 -050027static struct kmem_cache *ceph_osd_request_cache;
28
Tobias Klauser9e327892010-05-20 10:40:19 +020029static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070030
Sage Weilf24e9982009-10-06 11:31:10 -070031/*
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
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020049static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
50static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
Ilya Dryomov922dab62016-05-26 01:15:02 +020051static void link_linger(struct ceph_osd *osd,
52 struct ceph_osd_linger_request *lreq);
53static void unlink_linger(struct ceph_osd *osd,
54 struct ceph_osd_linger_request *lreq);
Ilya Dryomova02a9462017-06-19 12:18:05 +020055static void clear_backoffs(struct ceph_osd *osd);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020056
57#if 1
58static 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}
69static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
70{
71 WARN_ON(!rwsem_is_locked(&osdc->lock));
72}
73static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
74{
75 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
76}
77static 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}
Ilya Dryomov922dab62016-05-26 01:15:02 +020085static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86{
87 WARN_ON(!mutex_is_locked(&lreq->lock));
88}
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020089#else
90static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
91static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
92static inline void verify_osd_locked(struct ceph_osd *osd) { }
Ilya Dryomov922dab62016-05-26 01:15:02 +020093static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020094#endif
95
Sage Weilf24e9982009-10-06 11:31:10 -070096/*
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 */
Alex Elderdbe0fc42013-02-15 22:10:17 -0600103static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -0500104 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -0700105{
Alex Elder60e56f12013-02-15 11:42:29 -0600106 u64 orig_len = *plen;
Ilya Dryomovdccbf082018-02-17 09:29:58 +0100107 u32 xlen;
Sage Weilf24e9982009-10-06 11:31:10 -0700108
Alex Elder60e56f12013-02-15 11:42:29 -0600109 /* object extent? */
Ilya Dryomovdccbf082018-02-17 09:29:58 +0100110 ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111 objoff, &xlen);
112 *objlen = xlen;
Alex Elder75d1c942013-03-13 20:50:00 -0500113 if (*objlen < orig_len) {
114 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -0600115 dout(" skipping last %llu, final file extent %llu~%llu\n",
116 orig_len - *plen, off, *plen);
117 }
118
Alex Elder75d1c942013-03-13 20:50:00 -0500119 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600120 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700121}
122
Alex Elderc54d47b2013-04-03 01:28:57 -0500123static 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
Alex Eldera4ce40a2013-04-05 01:27:12 -0500129static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500130 struct page **pages, u64 length, u32 alignment,
131 bool pages_from_pool, bool own_pages)
132{
133 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
134 osd_data->pages = pages;
135 osd_data->length = length;
136 osd_data->alignment = alignment;
137 osd_data->pages_from_pool = pages_from_pool;
138 osd_data->own_pages = own_pages;
139}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500140
Alex Eldera4ce40a2013-04-05 01:27:12 -0500141static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500142 struct ceph_pagelist *pagelist)
143{
144 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
145 osd_data->pagelist = pagelist;
146}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500147
148#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500149static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Ilya Dryomov5359a172018-01-20 10:30:10 +0100150 struct ceph_bio_iter *bio_pos,
151 u32 bio_length)
Alex Elder43bfe5d2013-04-03 01:28:57 -0500152{
153 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100154 osd_data->bio_pos = *bio_pos;
Alex Elder43bfe5d2013-04-03 01:28:57 -0500155 osd_data->bio_length = bio_length;
156}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500157#endif /* CONFIG_BLOCK */
158
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100159static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
Ilya Dryomov0010f702018-05-04 16:57:30 +0200160 struct ceph_bvec_iter *bvec_pos,
161 u32 num_bvecs)
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100162{
163 osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
164 osd_data->bvec_pos = *bvec_pos;
Ilya Dryomov0010f702018-05-04 16:57:30 +0200165 osd_data->num_bvecs = num_bvecs;
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100166}
167
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300168#define osd_req_op_data(oreq, whch, typ, fld) \
169({ \
170 struct ceph_osd_request *__oreq = (oreq); \
171 unsigned int __whch = (whch); \
172 BUG_ON(__whch >= __oreq->r_num_ops); \
173 &__oreq->r_ops[__whch].typ.fld; \
174})
Alex Elder863c7eb2013-04-15 14:50:36 -0500175
Alex Elder49719772013-02-11 12:33:24 -0600176static struct ceph_osd_data *
177osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
178{
179 BUG_ON(which >= osd_req->r_num_ops);
180
181 return &osd_req->r_ops[which].raw_data_in;
182}
183
Alex Eldera4ce40a2013-04-05 01:27:12 -0500184struct ceph_osd_data *
185osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500186 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500187{
Alex Elder863c7eb2013-04-15 14:50:36 -0500188 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500189}
190EXPORT_SYMBOL(osd_req_op_extent_osd_data);
191
Alex Elder49719772013-02-11 12:33:24 -0600192void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
193 unsigned int which, struct page **pages,
194 u64 length, u32 alignment,
195 bool pages_from_pool, bool own_pages)
196{
197 struct ceph_osd_data *osd_data;
198
199 osd_data = osd_req_op_raw_data_in(osd_req, which);
200 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
201 pages_from_pool, own_pages);
202}
203EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
204
Alex Eldera4ce40a2013-04-05 01:27:12 -0500205void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500206 unsigned int which, struct page **pages,
207 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500208 bool pages_from_pool, bool own_pages)
209{
210 struct ceph_osd_data *osd_data;
211
Alex Elder863c7eb2013-04-15 14:50:36 -0500212 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500213 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
214 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500215}
216EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
217
218void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500219 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500220{
221 struct ceph_osd_data *osd_data;
222
Alex Elder863c7eb2013-04-15 14:50:36 -0500223 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500224 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500225}
226EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
227
228#ifdef CONFIG_BLOCK
229void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Ilya Dryomov5359a172018-01-20 10:30:10 +0100230 unsigned int which,
231 struct ceph_bio_iter *bio_pos,
232 u32 bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500233{
234 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500235
236 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Ilya Dryomov5359a172018-01-20 10:30:10 +0100237 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500238}
239EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
240#endif /* CONFIG_BLOCK */
241
Ilya Dryomov0010f702018-05-04 16:57:30 +0200242void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
243 unsigned int which,
244 struct bio_vec *bvecs, u32 num_bvecs,
245 u32 bytes)
246{
247 struct ceph_osd_data *osd_data;
248 struct ceph_bvec_iter it = {
249 .bvecs = bvecs,
250 .iter = { .bi_size = bytes },
251 };
252
253 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
254 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
255}
256EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
257
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100258void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
259 unsigned int which,
260 struct ceph_bvec_iter *bvec_pos)
261{
262 struct ceph_osd_data *osd_data;
263
264 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Ilya Dryomov0010f702018-05-04 16:57:30 +0200265 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100266}
267EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
268
Alex Eldera4ce40a2013-04-05 01:27:12 -0500269static void osd_req_op_cls_request_info_pagelist(
270 struct ceph_osd_request *osd_req,
271 unsigned int which, struct ceph_pagelist *pagelist)
272{
273 struct ceph_osd_data *osd_data;
274
Alex Elder863c7eb2013-04-15 14:50:36 -0500275 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500276 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500277}
278
Alex Elder04017e22013-04-05 14:46:02 -0500279void osd_req_op_cls_request_data_pagelist(
280 struct ceph_osd_request *osd_req,
281 unsigned int which, struct ceph_pagelist *pagelist)
282{
283 struct ceph_osd_data *osd_data;
284
Alex Elder863c7eb2013-04-15 14:50:36 -0500285 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500286 ceph_osd_data_pagelist_init(osd_data, pagelist);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200287 osd_req->r_ops[which].cls.indata_len += pagelist->length;
288 osd_req->r_ops[which].indata_len += pagelist->length;
Alex Elder04017e22013-04-05 14:46:02 -0500289}
290EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
291
Alex Elder6c57b552013-04-19 15:34:49 -0500292void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
293 unsigned int which, struct page **pages, u64 length,
294 u32 alignment, bool pages_from_pool, bool own_pages)
295{
296 struct ceph_osd_data *osd_data;
297
298 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
299 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
300 pages_from_pool, own_pages);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200301 osd_req->r_ops[which].cls.indata_len += length;
302 osd_req->r_ops[which].indata_len += length;
Alex Elder6c57b552013-04-19 15:34:49 -0500303}
304EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
305
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100306void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
307 unsigned int which,
Ilya Dryomov0010f702018-05-04 16:57:30 +0200308 struct bio_vec *bvecs, u32 num_bvecs,
309 u32 bytes)
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100310{
311 struct ceph_osd_data *osd_data;
312 struct ceph_bvec_iter it = {
313 .bvecs = bvecs,
314 .iter = { .bi_size = bytes },
315 };
316
317 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Ilya Dryomov0010f702018-05-04 16:57:30 +0200318 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100319 osd_req->r_ops[which].cls.indata_len += bytes;
320 osd_req->r_ops[which].indata_len += bytes;
321}
322EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
323
Alex Eldera4ce40a2013-04-05 01:27:12 -0500324void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
325 unsigned int which, struct page **pages, u64 length,
326 u32 alignment, bool pages_from_pool, bool own_pages)
327{
328 struct ceph_osd_data *osd_data;
329
Alex Elder863c7eb2013-04-15 14:50:36 -0500330 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500331 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
332 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500333}
334EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
335
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500336static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
337{
338 switch (osd_data->type) {
339 case CEPH_OSD_DATA_TYPE_NONE:
340 return 0;
341 case CEPH_OSD_DATA_TYPE_PAGES:
342 return osd_data->length;
343 case CEPH_OSD_DATA_TYPE_PAGELIST:
344 return (u64)osd_data->pagelist->length;
345#ifdef CONFIG_BLOCK
346 case CEPH_OSD_DATA_TYPE_BIO:
347 return (u64)osd_data->bio_length;
348#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100349 case CEPH_OSD_DATA_TYPE_BVECS:
350 return osd_data->bvec_pos.iter.bi_size;
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500351 default:
352 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
353 return 0;
354 }
355}
356
Alex Elderc54d47b2013-04-03 01:28:57 -0500357static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
358{
Alex Elder54764922013-04-05 01:27:12 -0500359 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500360 int num_pages;
361
362 num_pages = calc_pages_for((u64)osd_data->alignment,
363 (u64)osd_data->length);
364 ceph_release_page_vector(osd_data->pages, num_pages);
365 }
Alex Elder54764922013-04-05 01:27:12 -0500366 ceph_osd_data_init(osd_data);
367}
368
369static 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:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200380 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500381 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);
Alex Elder04017e22013-04-05 14:46:02 -0500385 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500386 ceph_osd_data_release(&op->cls.response_data);
387 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800388 case CEPH_OSD_OP_SETXATTR:
389 case CEPH_OSD_OP_CMPXATTR:
390 ceph_osd_data_release(&op->xattr.osd_data);
391 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800392 case CEPH_OSD_OP_STAT:
393 ceph_osd_data_release(&op->raw_data_in);
394 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200395 case CEPH_OSD_OP_NOTIFY_ACK:
396 ceph_osd_data_release(&op->notify_ack.request_data);
397 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200398 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;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700402 case CEPH_OSD_OP_LIST_WATCHERS:
403 ceph_osd_data_release(&op->list_watchers.response_data);
404 break;
Alex Elder54764922013-04-05 01:27:12 -0500405 default:
406 break;
407 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500408}
409
Sage Weilf24e9982009-10-06 11:31:10 -0700410/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200411 * Assumes @t is zero-initialized.
412 */
413static void target_init(struct ceph_osd_request_target *t)
414{
415 ceph_oid_init(&t->base_oid);
416 ceph_oloc_init(&t->base_oloc);
417 ceph_oid_init(&t->target_oid);
418 ceph_oloc_init(&t->target_oloc);
419
420 ceph_osds_init(&t->acting);
421 ceph_osds_init(&t->up);
422 t->size = -1;
423 t->min_size = -1;
424
425 t->osd = CEPH_HOMELESS_OSD;
426}
427
Ilya Dryomov922dab62016-05-26 01:15:02 +0200428static void target_copy(struct ceph_osd_request_target *dest,
429 const struct ceph_osd_request_target *src)
430{
431 ceph_oid_copy(&dest->base_oid, &src->base_oid);
432 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
433 ceph_oid_copy(&dest->target_oid, &src->target_oid);
434 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
435
436 dest->pgid = src->pgid; /* struct */
Ilya Dryomovdc98ff72017-06-15 16:30:53 +0200437 dest->spgid = src->spgid; /* struct */
Ilya Dryomov922dab62016-05-26 01:15:02 +0200438 dest->pg_num = src->pg_num;
439 dest->pg_num_mask = src->pg_num_mask;
440 ceph_osds_copy(&dest->acting, &src->acting);
441 ceph_osds_copy(&dest->up, &src->up);
442 dest->size = src->size;
443 dest->min_size = src->min_size;
444 dest->sort_bitwise = src->sort_bitwise;
445
446 dest->flags = src->flags;
447 dest->paused = src->paused;
448
Ilya Dryomov04c7d782017-06-15 16:30:55 +0200449 dest->epoch = src->epoch;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +0200450 dest->last_force_resend = src->last_force_resend;
451
Ilya Dryomov922dab62016-05-26 01:15:02 +0200452 dest->osd = src->osd;
453}
454
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200455static void target_destroy(struct ceph_osd_request_target *t)
456{
457 ceph_oid_destroy(&t->base_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800458 ceph_oloc_destroy(&t->base_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200459 ceph_oid_destroy(&t->target_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800460 ceph_oloc_destroy(&t->target_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200461}
462
463/*
Sage Weilf24e9982009-10-06 11:31:10 -0700464 * requests
465 */
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200466static void request_release_checks(struct ceph_osd_request *req)
467{
468 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
Ilya Dryomov46092452016-04-28 16:07:27 +0200469 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200470 WARN_ON(!list_empty(&req->r_unsafe_item));
471 WARN_ON(req->r_osd);
472}
473
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400474static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700475{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400476 struct ceph_osd_request *req = container_of(kref,
477 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500478 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800479
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400480 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
481 req->r_request, req->r_reply);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200482 request_release_checks(req);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400483
Sage Weil415e49a2009-12-07 13:37:03 -0800484 if (req->r_request)
485 ceph_msg_put(req->r_request);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200486 if (req->r_reply)
Alex Elderab8cb342012-06-04 14:43:32 -0500487 ceph_msg_put(req->r_reply);
Alex Elder0fff87e2013-02-14 12:16:43 -0600488
Alex Elder54764922013-04-05 01:27:12 -0500489 for (which = 0; which < req->r_num_ops; which++)
490 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600491
Ilya Dryomova66dd382016-04-28 16:07:23 +0200492 target_destroy(&req->r_t);
Sage Weil415e49a2009-12-07 13:37:03 -0800493 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200494
Sage Weil415e49a2009-12-07 13:37:03 -0800495 if (req->r_mempool)
496 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100497 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500498 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100499 else
500 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700501}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400502
503void ceph_osdc_get_request(struct ceph_osd_request *req)
504{
505 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100506 kref_read(&req->r_kref));
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400507 kref_get(&req->r_kref);
508}
509EXPORT_SYMBOL(ceph_osdc_get_request);
510
511void ceph_osdc_put_request(struct ceph_osd_request *req)
512{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200513 if (req) {
514 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100515 kref_read(&req->r_kref));
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200516 kref_put(&req->r_kref, ceph_osdc_release_request);
517 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400518}
519EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700520
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200521static void request_init(struct ceph_osd_request *req)
522{
523 /* req only, each op is zeroed in _osd_req_op_init() */
524 memset(req, 0, sizeof(*req));
525
526 kref_init(&req->r_kref);
527 init_completion(&req->r_completion);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200528 RB_CLEAR_NODE(&req->r_node);
Ilya Dryomov46092452016-04-28 16:07:27 +0200529 RB_CLEAR_NODE(&req->r_mc_node);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200530 INIT_LIST_HEAD(&req->r_unsafe_item);
531
532 target_init(&req->r_t);
533}
534
Ilya Dryomov922dab62016-05-26 01:15:02 +0200535/*
536 * This is ugly, but it allows us to reuse linger registration and ping
537 * requests, keeping the structure of the code around send_linger{_ping}()
538 * reasonable. Setting up a min_nr=2 mempool for each linger request
539 * and dealing with copying ops (this blasts req only, watch op remains
540 * intact) isn't any better.
541 */
542static void request_reinit(struct ceph_osd_request *req)
543{
544 struct ceph_osd_client *osdc = req->r_osdc;
545 bool mempool = req->r_mempool;
546 unsigned int num_ops = req->r_num_ops;
547 u64 snapid = req->r_snapid;
548 struct ceph_snap_context *snapc = req->r_snapc;
549 bool linger = req->r_linger;
550 struct ceph_msg *request_msg = req->r_request;
551 struct ceph_msg *reply_msg = req->r_reply;
552
553 dout("%s req %p\n", __func__, req);
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100554 WARN_ON(kref_read(&req->r_kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200555 request_release_checks(req);
556
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100557 WARN_ON(kref_read(&request_msg->kref) != 1);
558 WARN_ON(kref_read(&reply_msg->kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200559 target_destroy(&req->r_t);
560
561 request_init(req);
562 req->r_osdc = osdc;
563 req->r_mempool = mempool;
564 req->r_num_ops = num_ops;
565 req->r_snapid = snapid;
566 req->r_snapc = snapc;
567 req->r_linger = linger;
568 req->r_request = request_msg;
569 req->r_reply = reply_msg;
570}
571
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700572struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700573 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800574 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700575 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600576 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700577{
578 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700579
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700580 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100581 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700582 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100583 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
584 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700585 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100586 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
587 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
588 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700589 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100590 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700591 return NULL;
592
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200593 request_init(req);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700594 req->r_osdc = osdc;
595 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500596 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200597 req->r_snapid = CEPH_NOSNAP;
598 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700599
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200600 dout("%s req %p\n", __func__, req);
601 return req;
602}
603EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100604
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +0200605static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
Yan, Zheng30c156d2016-02-14 11:24:31 +0800606{
607 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
608}
609
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200610int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
611{
612 struct ceph_osd_client *osdc = req->r_osdc;
613 struct ceph_msg *msg;
614 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700615
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200616 WARN_ON(ceph_oid_empty(&req->r_base_oid));
Yan, Zheng30c156d2016-02-14 11:24:31 +0800617 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200618
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200619 /* create request message */
Ilya Dryomov8cb441c2017-06-15 16:30:54 +0200620 msg_size = CEPH_ENCODING_START_BLK_LEN +
621 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
622 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
623 msg_size += CEPH_ENCODING_START_BLK_LEN +
624 sizeof(struct ceph_osd_reqid); /* reqid */
625 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
626 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
Yan, Zheng30c156d2016-02-14 11:24:31 +0800627 msg_size += CEPH_ENCODING_START_BLK_LEN +
628 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200629 msg_size += 4 + req->r_base_oid.name_len; /* oid */
630 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100631 msg_size += 8; /* snapid */
632 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200633 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +0200634 msg_size += 4 + 8; /* retry_attempt, features */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100635
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200636 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700637 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
638 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200639 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
640 if (!msg)
641 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700642
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700643 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700644 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700645
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200646 /* create reply message */
647 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200648 msg_size += req->r_base_oid.name_len;
649 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200650
651 if (req->r_mempool)
652 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
653 else
654 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
655 if (!msg)
656 return -ENOMEM;
657
658 req->r_reply = msg;
659
660 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700661}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200662EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700663
Alex Eldera8dd0a32013-03-13 20:50:00 -0500664static bool osd_req_opcode_valid(u16 opcode)
665{
666 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400667#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
668__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
669#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500670 default:
671 return false;
672 }
673}
674
Alex Elder33803f32013-03-13 20:50:00 -0500675/*
676 * This is an osd op init function for opcodes that have no data or
677 * other information associated with them. It also serves as a
678 * common init routine for all the other init functions, below.
679 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500680static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600681_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800682 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500683{
Alex Elderc99d2d42013-04-05 01:27:11 -0500684 struct ceph_osd_req_op *op;
685
686 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500687 BUG_ON(!osd_req_opcode_valid(opcode));
688
Alex Elderc99d2d42013-04-05 01:27:11 -0500689 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500690 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500691 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800692 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500693
694 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500695}
696
Alex Elder49719772013-02-11 12:33:24 -0600697void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800698 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600699{
Yan, Zheng144cba12015-04-27 11:09:54 +0800700 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600701}
702EXPORT_SYMBOL(osd_req_op_init);
703
Alex Elderc99d2d42013-04-05 01:27:11 -0500704void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
705 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500706 u64 offset, u64 length,
707 u64 truncate_size, u32 truncate_seq)
708{
Yan, Zheng144cba12015-04-27 11:09:54 +0800709 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
710 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500711 size_t payload_len = 0;
712
Li Wangad7a60d2013-08-15 11:51:44 +0800713 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200714 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
715 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500716
Alex Elder33803f32013-03-13 20:50:00 -0500717 op->extent.offset = offset;
718 op->extent.length = length;
719 op->extent.truncate_size = truncate_size;
720 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200721 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500722 payload_len += length;
723
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100724 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500725}
726EXPORT_SYMBOL(osd_req_op_extent_init);
727
Alex Elderc99d2d42013-04-05 01:27:11 -0500728void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
729 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500730{
Alex Elderc99d2d42013-04-05 01:27:11 -0500731 struct ceph_osd_req_op *op;
732 u64 previous;
733
734 BUG_ON(which >= osd_req->r_num_ops);
735 op = &osd_req->r_ops[which];
736 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500737
738 if (length == previous)
739 return; /* Nothing to do */
740 BUG_ON(length > previous);
741
742 op->extent.length = length;
Yan, Zhengd641df82017-01-19 11:21:29 +0800743 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
744 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500745}
746EXPORT_SYMBOL(osd_req_op_extent_update);
747
Yan, Zheng2c63f492016-01-07 17:32:54 +0800748void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
749 unsigned int which, u64 offset_inc)
750{
751 struct ceph_osd_req_op *op, *prev_op;
752
753 BUG_ON(which + 1 >= osd_req->r_num_ops);
754
755 prev_op = &osd_req->r_ops[which];
756 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
757 /* dup previous one */
758 op->indata_len = prev_op->indata_len;
759 op->outdata_len = prev_op->outdata_len;
760 op->extent = prev_op->extent;
761 /* adjust offset */
762 op->extent.offset += offset_inc;
763 op->extent.length -= offset_inc;
764
765 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
766 op->indata_len -= offset_inc;
767}
768EXPORT_SYMBOL(osd_req_op_extent_dup_last);
769
Chengguang Xufe943d52018-04-12 12:04:55 +0800770int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500771 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500772{
Yan, Zheng144cba12015-04-27 11:09:54 +0800773 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
774 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500775 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500776 size_t payload_len = 0;
777 size_t size;
778
779 BUG_ON(opcode != CEPH_OSD_OP_CALL);
780
Alex Elder5f562df2013-04-05 01:27:12 -0500781 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
Chengguang Xufe943d52018-04-12 12:04:55 +0800782 if (!pagelist)
783 return -ENOMEM;
784
Alex Elder5f562df2013-04-05 01:27:12 -0500785 ceph_pagelist_init(pagelist);
786
Alex Elder33803f32013-03-13 20:50:00 -0500787 op->cls.class_name = class;
788 size = strlen(class);
789 BUG_ON(size > (size_t) U8_MAX);
790 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500791 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500792 payload_len += size;
793
794 op->cls.method_name = method;
795 size = strlen(method);
796 BUG_ON(size > (size_t) U8_MAX);
797 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500798 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500799 payload_len += size;
800
Alex Eldera4ce40a2013-04-05 01:27:12 -0500801 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500802
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100803 op->indata_len = payload_len;
Chengguang Xufe943d52018-04-12 12:04:55 +0800804 return 0;
Alex Elder33803f32013-03-13 20:50:00 -0500805}
806EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500807
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800808int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
809 u16 opcode, const char *name, const void *value,
810 size_t size, u8 cmp_op, u8 cmp_mode)
811{
Yan, Zheng144cba12015-04-27 11:09:54 +0800812 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
813 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800814 struct ceph_pagelist *pagelist;
815 size_t payload_len;
816
817 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
818
819 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
820 if (!pagelist)
821 return -ENOMEM;
822
823 ceph_pagelist_init(pagelist);
824
825 payload_len = strlen(name);
826 op->xattr.name_len = payload_len;
827 ceph_pagelist_append(pagelist, name, payload_len);
828
829 op->xattr.value_len = size;
830 ceph_pagelist_append(pagelist, value, size);
831 payload_len += size;
832
833 op->xattr.cmp_op = cmp_op;
834 op->xattr.cmp_mode = cmp_mode;
835
836 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100837 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800838 return 0;
839}
840EXPORT_SYMBOL(osd_req_op_xattr_init);
841
Ilya Dryomov922dab62016-05-26 01:15:02 +0200842/*
843 * @watch_opcode: CEPH_OSD_WATCH_OP_*
844 */
845static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
846 u64 cookie, u8 watch_opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500847{
Ilya Dryomov922dab62016-05-26 01:15:02 +0200848 struct ceph_osd_req_op *op;
Alex Elder33803f32013-03-13 20:50:00 -0500849
Ilya Dryomov922dab62016-05-26 01:15:02 +0200850 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500851 op->watch.cookie = cookie;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200852 op->watch.op = watch_opcode;
853 op->watch.gen = 0;
Alex Elder33803f32013-03-13 20:50:00 -0500854}
Alex Elder33803f32013-03-13 20:50:00 -0500855
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200856void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
857 unsigned int which,
858 u64 expected_object_size,
859 u64 expected_write_size)
860{
861 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800862 CEPH_OSD_OP_SETALLOCHINT,
863 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200864
865 op->alloc_hint.expected_object_size = expected_object_size;
866 op->alloc_hint.expected_write_size = expected_write_size;
867
868 /*
869 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
870 * not worth a feature bit. Set FAILOK per-op flag to make
871 * sure older osds don't trip over an unsupported opcode.
872 */
873 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
874}
875EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
876
Alex Elder90af3602013-04-05 14:46:01 -0500877static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500878 struct ceph_osd_data *osd_data)
879{
880 u64 length = ceph_osd_data_length(osd_data);
881
882 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
883 BUG_ON(length > (u64) SIZE_MAX);
884 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500885 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500886 length, osd_data->alignment);
887 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
888 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500889 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500890#ifdef CONFIG_BLOCK
891 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Ilya Dryomov5359a172018-01-20 10:30:10 +0100892 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500893#endif
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100894 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
895 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
Alex Elderec9123c2013-04-05 01:27:12 -0500896 } else {
897 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
898 }
899}
900
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200901static u32 osd_req_encode_op(struct ceph_osd_op *dst,
902 const struct ceph_osd_req_op *src)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700903{
Alex Eldera8dd0a32013-03-13 20:50:00 -0500904 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
905 pr_err("unrecognized osd opcode %d\n", src->op);
906
907 return 0;
908 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700909
Alex Elder065a68f2012-04-20 15:49:43 -0500910 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600911 case CEPH_OSD_OP_STAT:
912 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700913 case CEPH_OSD_OP_READ:
914 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200915 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800916 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800917 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600918 dst->extent.offset = cpu_to_le64(src->extent.offset);
919 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700920 dst->extent.truncate_size =
921 cpu_to_le64(src->extent.truncate_size);
922 dst->extent.truncate_seq =
923 cpu_to_le32(src->extent.truncate_seq);
924 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700925 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700926 dst->cls.class_len = src->cls.class_len;
927 dst->cls.method_len = src->cls.method_len;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200928 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700929 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700930 case CEPH_OSD_OP_WATCH:
931 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200932 dst->watch.ver = cpu_to_le64(0);
933 dst->watch.op = src->watch.op;
934 dst->watch.gen = cpu_to_le32(src->watch.gen);
935 break;
936 case CEPH_OSD_OP_NOTIFY_ACK:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700937 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200938 case CEPH_OSD_OP_NOTIFY:
939 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
940 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700941 case CEPH_OSD_OP_LIST_WATCHERS:
942 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200943 case CEPH_OSD_OP_SETALLOCHINT:
944 dst->alloc_hint.expected_object_size =
945 cpu_to_le64(src->alloc_hint.expected_object_size);
946 dst->alloc_hint.expected_write_size =
947 cpu_to_le64(src->alloc_hint.expected_write_size);
948 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800949 case CEPH_OSD_OP_SETXATTR:
950 case CEPH_OSD_OP_CMPXATTR:
951 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
952 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
953 dst->xattr.cmp_op = src->xattr.cmp_op;
954 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800955 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800956 case CEPH_OSD_OP_CREATE:
957 case CEPH_OSD_OP_DELETE:
958 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700959 default:
Alex Elder4c464592013-02-15 11:42:30 -0600960 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600961 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600962 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500963
964 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700965 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200966
Alex Eldera8dd0a32013-03-13 20:50:00 -0500967 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200968 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100969 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600970
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200971 return src->indata_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700972}
973
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700974/*
Sage Weilf24e9982009-10-06 11:31:10 -0700975 * build new request AND message, calculate layout, and adjust file
976 * extent as needed.
977 *
978 * if the file was recently truncated, we include information about its
979 * old and new size so that the object can be updated appropriately. (we
980 * avoid synchronously deleting truncated objects because it's slow.)
Sage Weilf24e9982009-10-06 11:31:10 -0700981 */
982struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
983 struct ceph_file_layout *layout,
984 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800985 u64 off, u64 *plen,
986 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700987 int opcode, int flags,
988 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700989 u32 truncate_seq,
990 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600991 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700992{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700993 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500994 u64 objnum = 0;
995 u64 objoff = 0;
996 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700997 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700998
Li Wangad7a60d2013-08-15 11:51:44 +0800999 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +08001000 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1001 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001002
Alex Elderacead002013-03-14 14:09:05 -05001003 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -06001004 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001005 if (!req) {
1006 r = -ENOMEM;
1007 goto fail;
1008 }
Alex Elder79528732013-04-03 21:32:51 -05001009
Sage Weilf24e9982009-10-06 11:31:10 -07001010 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -05001011 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001012 if (r)
1013 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -05001014
Yan, Zheng864e9192014-11-13 10:47:25 +08001015 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +08001016 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +08001017 } else {
Yan, Zheng76271512016-02-03 21:24:49 +08001018 u32 object_size = layout->object_size;
Yan, Zheng864e9192014-11-13 10:47:25 +08001019 u32 object_base = off - objoff;
1020 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1021 if (truncate_size <= object_base) {
1022 truncate_size = 0;
1023 } else {
1024 truncate_size -= object_base;
1025 if (truncate_size > object_size)
1026 truncate_size = object_size;
1027 }
Yan, Zhengccca4e32013-06-02 18:40:23 +08001028 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +08001029 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +08001030 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -05001031 }
Alex Elderd18d1e22013-03-13 20:50:01 -05001032
Jeff Laytona1f40202017-04-04 08:39:37 -04001033 req->r_abort_on_full = true;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001034 req->r_flags = flags;
Yan, Zheng76271512016-02-03 21:24:49 +08001035 req->r_base_oloc.pool = layout->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +08001036 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001037 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -06001038
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001039 req->r_snapid = vino.snap;
1040 if (flags & CEPH_OSD_FLAG_WRITE)
1041 req->r_data_offset = off;
1042
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001043 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1044 if (r)
1045 goto fail;
1046
Sage Weilf24e9982009-10-06 11:31:10 -07001047 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001048
1049fail:
1050 ceph_osdc_put_request(req);
1051 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -07001052}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001053EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001054
1055/*
1056 * We keep osd requests in an rbtree, sorted by ->r_tid.
1057 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001058DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02001059DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
Sage Weilf24e9982009-10-06 11:31:10 -07001060
Ilya Dryomov66850df2018-05-15 15:47:58 +02001061/*
1062 * Call @fn on each OSD request as long as @fn returns 0.
1063 */
1064static void for_each_request(struct ceph_osd_client *osdc,
1065 int (*fn)(struct ceph_osd_request *req, void *arg),
1066 void *arg)
1067{
1068 struct rb_node *n, *p;
1069
1070 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1071 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1072
1073 for (p = rb_first(&osd->o_requests); p; ) {
1074 struct ceph_osd_request *req =
1075 rb_entry(p, struct ceph_osd_request, r_node);
1076
1077 p = rb_next(p);
1078 if (fn(req, arg))
1079 return;
1080 }
1081 }
1082
1083 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1084 struct ceph_osd_request *req =
1085 rb_entry(p, struct ceph_osd_request, r_node);
1086
1087 p = rb_next(p);
1088 if (fn(req, arg))
1089 return;
1090 }
1091}
1092
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001093static bool osd_homeless(struct ceph_osd *osd)
1094{
1095 return osd->o_osd == CEPH_HOMELESS_OSD;
1096}
1097
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001098static bool osd_registered(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001099{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001100 verify_osdc_locked(osd->o_osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001101
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001102 return !RB_EMPTY_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001103}
1104
1105/*
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001106 * Assumes @osd is zero-initialized.
1107 */
1108static void osd_init(struct ceph_osd *osd)
1109{
Elena Reshetova02113a02017-03-17 14:10:28 +02001110 refcount_set(&osd->o_ref, 1);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001111 RB_CLEAR_NODE(&osd->o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001112 osd->o_requests = RB_ROOT;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001113 osd->o_linger_requests = RB_ROOT;
Ilya Dryomova02a9462017-06-19 12:18:05 +02001114 osd->o_backoff_mappings = RB_ROOT;
1115 osd->o_backoffs_by_id = RB_ROOT;
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001116 INIT_LIST_HEAD(&osd->o_osd_lru);
1117 INIT_LIST_HEAD(&osd->o_keepalive_item);
1118 osd->o_incarnation = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001119 mutex_init(&osd->lock);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001120}
1121
1122static void osd_cleanup(struct ceph_osd *osd)
1123{
1124 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001125 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001126 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomova02a9462017-06-19 12:18:05 +02001127 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1128 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001129 WARN_ON(!list_empty(&osd->o_osd_lru));
1130 WARN_ON(!list_empty(&osd->o_keepalive_item));
1131
1132 if (osd->o_auth.authorizer) {
1133 WARN_ON(osd_homeless(osd));
1134 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1135 }
1136}
1137
1138/*
Sage Weilf24e9982009-10-06 11:31:10 -07001139 * Track open sessions with osds.
1140 */
Alex Eldere10006f2012-05-26 23:26:43 -05001141static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001142{
1143 struct ceph_osd *osd;
1144
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001145 WARN_ON(onum == CEPH_HOMELESS_OSD);
1146
Ilya Dryomov7a28f592016-04-28 16:07:25 +02001147 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001148 osd_init(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001149 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001150 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -07001151
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001152 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001153
Sage Weilf24e9982009-10-06 11:31:10 -07001154 return osd;
1155}
1156
1157static struct ceph_osd *get_osd(struct ceph_osd *osd)
1158{
Elena Reshetova02113a02017-03-17 14:10:28 +02001159 if (refcount_inc_not_zero(&osd->o_ref)) {
1160 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1161 refcount_read(&osd->o_ref));
Sage Weilf24e9982009-10-06 11:31:10 -07001162 return osd;
1163 } else {
1164 dout("get_osd %p FAIL\n", osd);
1165 return NULL;
1166 }
1167}
1168
1169static void put_osd(struct ceph_osd *osd)
1170{
Elena Reshetova02113a02017-03-17 14:10:28 +02001171 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1172 refcount_read(&osd->o_ref) - 1);
1173 if (refcount_dec_and_test(&osd->o_ref)) {
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001174 osd_cleanup(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001175 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001176 }
Sage Weilf24e9982009-10-06 11:31:10 -07001177}
1178
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001179DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1180
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001181static void __move_osd_to_lru(struct ceph_osd *osd)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001182{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001183 struct ceph_osd_client *osdc = osd->o_osdc;
1184
1185 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001186 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001187
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001188 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001189 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001190 spin_unlock(&osdc->osd_lru_lock);
1191
Ilya Dryomova319bf52015-05-15 12:02:17 +03001192 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001193}
1194
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001195static void maybe_move_osd_to_lru(struct ceph_osd *osd)
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001196{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001197 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001198 RB_EMPTY_ROOT(&osd->o_linger_requests))
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001199 __move_osd_to_lru(osd);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001200}
1201
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001202static void __remove_osd_from_lru(struct ceph_osd *osd)
1203{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001204 struct ceph_osd_client *osdc = osd->o_osdc;
1205
1206 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1207
1208 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001209 if (!list_empty(&osd->o_osd_lru))
1210 list_del_init(&osd->o_osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001211 spin_unlock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001212}
1213
Sage Weilf24e9982009-10-06 11:31:10 -07001214/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001215 * Close the connection and assign any leftover requests to the
1216 * homeless session.
1217 */
1218static void close_osd(struct ceph_osd *osd)
1219{
1220 struct ceph_osd_client *osdc = osd->o_osdc;
1221 struct rb_node *n;
1222
1223 verify_osdc_wrlocked(osdc);
1224 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1225
1226 ceph_con_close(&osd->o_con);
1227
1228 for (n = rb_first(&osd->o_requests); n; ) {
1229 struct ceph_osd_request *req =
1230 rb_entry(n, struct ceph_osd_request, r_node);
1231
1232 n = rb_next(n); /* unlink_request() */
1233
1234 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1235 unlink_request(osd, req);
1236 link_request(&osdc->homeless_osd, req);
1237 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02001238 for (n = rb_first(&osd->o_linger_requests); n; ) {
1239 struct ceph_osd_linger_request *lreq =
1240 rb_entry(n, struct ceph_osd_linger_request, node);
1241
1242 n = rb_next(n); /* unlink_linger() */
1243
1244 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1245 lreq->linger_id);
1246 unlink_linger(osd, lreq);
1247 link_linger(&osdc->homeless_osd, lreq);
1248 }
Ilya Dryomova02a9462017-06-19 12:18:05 +02001249 clear_backoffs(osd);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001250
1251 __remove_osd_from_lru(osd);
1252 erase_osd(&osdc->osds, osd);
1253 put_osd(osd);
1254}
1255
1256/*
Sage Weilf24e9982009-10-06 11:31:10 -07001257 * reset osd connect
1258 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001259static int reopen_osd(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001260{
Alex Elderc3acb182012-12-07 09:57:58 -06001261 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001262
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001263 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1264
1265 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001266 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001267 close_osd(osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001268 return -ENODEV;
1269 }
1270
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001271 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
Alex Elderc3acb182012-12-07 09:57:58 -06001272 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1273 !ceph_con_opened(&osd->o_con)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001274 struct rb_node *n;
Alex Elderc3acb182012-12-07 09:57:58 -06001275
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001276 dout("osd addr hasn't changed and connection never opened, "
1277 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001278 /* touch each r_stamp for handle_timeout()'s benfit */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001279 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1280 struct ceph_osd_request *req =
1281 rb_entry(n, struct ceph_osd_request, r_node);
Sage Weil87b315a2010-03-22 14:51:18 -07001282 req->r_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001283 }
Alex Elderc3acb182012-12-07 09:57:58 -06001284
1285 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001286 }
Alex Elderc3acb182012-12-07 09:57:58 -06001287
1288 ceph_con_close(&osd->o_con);
1289 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1290 osd->o_incarnation++;
1291
1292 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001293}
1294
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001295static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1296 bool wrlocked)
Sage Weilf24e9982009-10-06 11:31:10 -07001297{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001298 struct ceph_osd *osd;
1299
1300 if (wrlocked)
1301 verify_osdc_wrlocked(osdc);
1302 else
1303 verify_osdc_locked(osdc);
1304
1305 if (o != CEPH_HOMELESS_OSD)
1306 osd = lookup_osd(&osdc->osds, o);
1307 else
1308 osd = &osdc->homeless_osd;
1309 if (!osd) {
1310 if (!wrlocked)
1311 return ERR_PTR(-EAGAIN);
1312
1313 osd = create_osd(osdc, o);
1314 insert_osd(&osdc->osds, osd);
1315 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1316 &osdc->osdmap->osd_addr[osd->o_osd]);
1317 }
1318
1319 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1320 return osd;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001321}
1322
Sage Weilf24e9982009-10-06 11:31:10 -07001323/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001324 * Create request <-> OSD session relation.
1325 *
1326 * @req has to be assigned a tid, @osd may be homeless.
Sage Weilf24e9982009-10-06 11:31:10 -07001327 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001328static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001329{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001330 verify_osd_locked(osd);
1331 WARN_ON(!req->r_tid || req->r_osd);
1332 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1333 req, req->r_tid);
Sage Weil35f9f8a2012-05-16 15:16:38 -05001334
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001335 if (!osd_homeless(osd))
1336 __remove_osd_from_lru(osd);
1337 else
1338 atomic_inc(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001339
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001340 get_osd(osd);
1341 insert_request(&osd->o_requests, req);
1342 req->r_osd = osd;
Sage Weilf24e9982009-10-06 11:31:10 -07001343}
1344
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001345static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001346{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001347 verify_osd_locked(osd);
1348 WARN_ON(req->r_osd != osd);
1349 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1350 req, req->r_tid);
1351
1352 req->r_osd = NULL;
1353 erase_request(&osd->o_requests, req);
1354 put_osd(osd);
1355
1356 if (!osd_homeless(osd))
1357 maybe_move_osd_to_lru(osd);
1358 else
1359 atomic_dec(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001360}
1361
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001362static bool __pool_full(struct ceph_pg_pool_info *pi)
1363{
1364 return pi->flags & CEPH_POOL_FLAG_FULL;
1365}
1366
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001367static bool have_pool_full(struct ceph_osd_client *osdc)
1368{
1369 struct rb_node *n;
1370
1371 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1372 struct ceph_pg_pool_info *pi =
1373 rb_entry(n, struct ceph_pg_pool_info, node);
1374
1375 if (__pool_full(pi))
1376 return true;
1377 }
1378
1379 return false;
1380}
1381
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001382static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1383{
1384 struct ceph_pg_pool_info *pi;
1385
1386 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1387 if (!pi)
1388 return false;
1389
1390 return __pool_full(pi);
1391}
1392
Sage Weilf24e9982009-10-06 11:31:10 -07001393/*
Josh Durgind29adb32013-12-02 19:11:48 -08001394 * Returns whether a request should be blocked from being sent
1395 * based on the current osdmap and osd_client settings.
Josh Durgind29adb32013-12-02 19:11:48 -08001396 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001397static bool target_should_be_paused(struct ceph_osd_client *osdc,
1398 const struct ceph_osd_request_target *t,
1399 struct ceph_pg_pool_info *pi)
1400{
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001401 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1402 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1403 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001404 __pool_full(pi);
1405
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001406 WARN_ON(pi->id != t->target_oloc.pool);
Jeff Layton58eb7932017-04-18 09:21:16 -04001407 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1408 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1409 (osdc->osdmap->epoch < osdc->epoch_barrier);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001410}
1411
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001412enum calc_target_result {
1413 CALC_TARGET_NO_ACTION = 0,
1414 CALC_TARGET_NEED_RESEND,
1415 CALC_TARGET_POOL_DNE,
1416};
1417
1418static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1419 struct ceph_osd_request_target *t,
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001420 struct ceph_connection *con,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001421 bool any_change)
1422{
1423 struct ceph_pg_pool_info *pi;
1424 struct ceph_pg pgid, last_pgid;
1425 struct ceph_osds up, acting;
1426 bool force_resend = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001427 bool unpaused = false;
1428 bool legacy_change;
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001429 bool split = false;
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001430 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001431 bool recovery_deletes = ceph_osdmap_flag(osdc,
1432 CEPH_OSDMAP_RECOVERY_DELETES);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001433 enum calc_target_result ct_res;
1434 int ret;
1435
Ilya Dryomov04c7d782017-06-15 16:30:55 +02001436 t->epoch = osdc->osdmap->epoch;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001437 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1438 if (!pi) {
1439 t->osd = CEPH_HOMELESS_OSD;
1440 ct_res = CALC_TARGET_POOL_DNE;
1441 goto out;
1442 }
1443
1444 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001445 if (t->last_force_resend < pi->last_force_request_resend) {
1446 t->last_force_resend = pi->last_force_request_resend;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001447 force_resend = true;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001448 } else if (t->last_force_resend == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001449 force_resend = true;
1450 }
1451 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001452
Ilya Dryomovdb098ec2017-06-15 16:30:55 +02001453 /* apply tiering */
1454 ceph_oid_copy(&t->target_oid, &t->base_oid);
1455 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1456 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001457 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1458 t->target_oloc.pool = pi->read_tier;
1459 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1460 t->target_oloc.pool = pi->write_tier;
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001461
1462 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1463 if (!pi) {
1464 t->osd = CEPH_HOMELESS_OSD;
1465 ct_res = CALC_TARGET_POOL_DNE;
1466 goto out;
1467 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001468 }
1469
Ilya Dryomovdf281522017-06-15 16:30:56 +02001470 ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1471 &pgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001472 if (ret) {
1473 WARN_ON(ret != -ENOENT);
1474 t->osd = CEPH_HOMELESS_OSD;
1475 ct_res = CALC_TARGET_POOL_DNE;
1476 goto out;
1477 }
1478 last_pgid.pool = pgid.pool;
1479 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1480
Ilya Dryomovdf281522017-06-15 16:30:56 +02001481 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001482 if (any_change &&
1483 ceph_is_new_interval(&t->acting,
1484 &acting,
1485 &t->up,
1486 &up,
1487 t->size,
1488 pi->size,
1489 t->min_size,
1490 pi->min_size,
1491 t->pg_num,
1492 pi->pg_num,
1493 t->sort_bitwise,
1494 sort_bitwise,
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001495 t->recovery_deletes,
1496 recovery_deletes,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001497 &last_pgid))
1498 force_resend = true;
1499
1500 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1501 t->paused = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001502 unpaused = true;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001503 }
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001504 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1505 ceph_osds_changed(&t->acting, &acting, any_change);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001506 if (t->pg_num)
1507 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001508
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001509 if (legacy_change || force_resend || split) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001510 t->pgid = pgid; /* struct */
Ilya Dryomovdf281522017-06-15 16:30:56 +02001511 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001512 ceph_osds_copy(&t->acting, &acting);
1513 ceph_osds_copy(&t->up, &up);
1514 t->size = pi->size;
1515 t->min_size = pi->min_size;
1516 t->pg_num = pi->pg_num;
1517 t->pg_num_mask = pi->pg_num_mask;
1518 t->sort_bitwise = sort_bitwise;
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001519 t->recovery_deletes = recovery_deletes;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001520
1521 t->osd = acting.primary;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001522 }
1523
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001524 if (unpaused || legacy_change || force_resend ||
1525 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1526 RESEND_ON_SPLIT)))
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001527 ct_res = CALC_TARGET_NEED_RESEND;
1528 else
1529 ct_res = CALC_TARGET_NO_ACTION;
1530
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001531out:
1532 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1533 return ct_res;
1534}
1535
Ilya Dryomova02a9462017-06-19 12:18:05 +02001536static struct ceph_spg_mapping *alloc_spg_mapping(void)
1537{
1538 struct ceph_spg_mapping *spg;
1539
1540 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1541 if (!spg)
1542 return NULL;
1543
1544 RB_CLEAR_NODE(&spg->node);
1545 spg->backoffs = RB_ROOT;
1546 return spg;
1547}
1548
1549static void free_spg_mapping(struct ceph_spg_mapping *spg)
1550{
1551 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1552 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1553
1554 kfree(spg);
1555}
1556
1557/*
1558 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1559 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1560 * defined only within a specific spgid; it does not pass anything to
1561 * children on split, or to another primary.
1562 */
1563DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1564 RB_BYPTR, const struct ceph_spg *, node)
1565
1566static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1567{
1568 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1569}
1570
1571static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1572 void **pkey, size_t *pkey_len)
1573{
1574 if (hoid->key_len) {
1575 *pkey = hoid->key;
1576 *pkey_len = hoid->key_len;
1577 } else {
1578 *pkey = hoid->oid;
1579 *pkey_len = hoid->oid_len;
1580 }
1581}
1582
1583static int compare_names(const void *name1, size_t name1_len,
1584 const void *name2, size_t name2_len)
1585{
1586 int ret;
1587
1588 ret = memcmp(name1, name2, min(name1_len, name2_len));
1589 if (!ret) {
1590 if (name1_len < name2_len)
1591 ret = -1;
1592 else if (name1_len > name2_len)
1593 ret = 1;
1594 }
1595 return ret;
1596}
1597
1598static int hoid_compare(const struct ceph_hobject_id *lhs,
1599 const struct ceph_hobject_id *rhs)
1600{
1601 void *effective_key1, *effective_key2;
1602 size_t effective_key1_len, effective_key2_len;
1603 int ret;
1604
1605 if (lhs->is_max < rhs->is_max)
1606 return -1;
1607 if (lhs->is_max > rhs->is_max)
1608 return 1;
1609
1610 if (lhs->pool < rhs->pool)
1611 return -1;
1612 if (lhs->pool > rhs->pool)
1613 return 1;
1614
1615 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1616 return -1;
1617 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1618 return 1;
1619
1620 ret = compare_names(lhs->nspace, lhs->nspace_len,
1621 rhs->nspace, rhs->nspace_len);
1622 if (ret)
1623 return ret;
1624
1625 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1626 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1627 ret = compare_names(effective_key1, effective_key1_len,
1628 effective_key2, effective_key2_len);
1629 if (ret)
1630 return ret;
1631
1632 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1633 if (ret)
1634 return ret;
1635
1636 if (lhs->snapid < rhs->snapid)
1637 return -1;
1638 if (lhs->snapid > rhs->snapid)
1639 return 1;
1640
1641 return 0;
1642}
1643
1644/*
1645 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1646 * compat stuff here.
1647 *
1648 * Assumes @hoid is zero-initialized.
1649 */
1650static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1651{
1652 u8 struct_v;
1653 u32 struct_len;
1654 int ret;
1655
1656 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1657 &struct_len);
1658 if (ret)
1659 return ret;
1660
1661 if (struct_v < 4) {
1662 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1663 goto e_inval;
1664 }
1665
1666 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1667 GFP_NOIO);
1668 if (IS_ERR(hoid->key)) {
1669 ret = PTR_ERR(hoid->key);
1670 hoid->key = NULL;
1671 return ret;
1672 }
1673
1674 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1675 GFP_NOIO);
1676 if (IS_ERR(hoid->oid)) {
1677 ret = PTR_ERR(hoid->oid);
1678 hoid->oid = NULL;
1679 return ret;
1680 }
1681
1682 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1683 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1684 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1685
1686 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1687 GFP_NOIO);
1688 if (IS_ERR(hoid->nspace)) {
1689 ret = PTR_ERR(hoid->nspace);
1690 hoid->nspace = NULL;
1691 return ret;
1692 }
1693
1694 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1695
1696 ceph_hoid_build_hash_cache(hoid);
1697 return 0;
1698
1699e_inval:
1700 return -EINVAL;
1701}
1702
1703static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1704{
1705 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1706 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1707}
1708
1709static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1710{
1711 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1712 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1713 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1714 ceph_encode_64(p, hoid->snapid);
1715 ceph_encode_32(p, hoid->hash);
1716 ceph_encode_8(p, hoid->is_max);
1717 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1718 ceph_encode_64(p, hoid->pool);
1719}
1720
1721static void free_hoid(struct ceph_hobject_id *hoid)
1722{
1723 if (hoid) {
1724 kfree(hoid->key);
1725 kfree(hoid->oid);
1726 kfree(hoid->nspace);
1727 kfree(hoid);
1728 }
1729}
1730
1731static struct ceph_osd_backoff *alloc_backoff(void)
1732{
1733 struct ceph_osd_backoff *backoff;
1734
1735 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1736 if (!backoff)
1737 return NULL;
1738
1739 RB_CLEAR_NODE(&backoff->spg_node);
1740 RB_CLEAR_NODE(&backoff->id_node);
1741 return backoff;
1742}
1743
1744static void free_backoff(struct ceph_osd_backoff *backoff)
1745{
1746 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1747 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1748
1749 free_hoid(backoff->begin);
1750 free_hoid(backoff->end);
1751 kfree(backoff);
1752}
1753
1754/*
1755 * Within a specific spgid, backoffs are managed by ->begin hoid.
1756 */
1757DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1758 RB_BYVAL, spg_node);
1759
1760static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1761 const struct ceph_hobject_id *hoid)
1762{
1763 struct rb_node *n = root->rb_node;
1764
1765 while (n) {
1766 struct ceph_osd_backoff *cur =
1767 rb_entry(n, struct ceph_osd_backoff, spg_node);
1768 int cmp;
1769
1770 cmp = hoid_compare(hoid, cur->begin);
1771 if (cmp < 0) {
1772 n = n->rb_left;
1773 } else if (cmp > 0) {
1774 if (hoid_compare(hoid, cur->end) < 0)
1775 return cur;
1776
1777 n = n->rb_right;
1778 } else {
1779 return cur;
1780 }
1781 }
1782
1783 return NULL;
1784}
1785
1786/*
1787 * Each backoff has a unique id within its OSD session.
1788 */
1789DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1790
1791static void clear_backoffs(struct ceph_osd *osd)
1792{
1793 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1794 struct ceph_spg_mapping *spg =
1795 rb_entry(rb_first(&osd->o_backoff_mappings),
1796 struct ceph_spg_mapping, node);
1797
1798 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1799 struct ceph_osd_backoff *backoff =
1800 rb_entry(rb_first(&spg->backoffs),
1801 struct ceph_osd_backoff, spg_node);
1802
1803 erase_backoff(&spg->backoffs, backoff);
1804 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1805 free_backoff(backoff);
1806 }
1807 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1808 free_spg_mapping(spg);
1809 }
1810}
1811
1812/*
1813 * Set up a temporary, non-owning view into @t.
1814 */
1815static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1816 const struct ceph_osd_request_target *t)
1817{
1818 hoid->key = NULL;
1819 hoid->key_len = 0;
1820 hoid->oid = t->target_oid.name;
1821 hoid->oid_len = t->target_oid.name_len;
1822 hoid->snapid = CEPH_NOSNAP;
1823 hoid->hash = t->pgid.seed;
1824 hoid->is_max = false;
1825 if (t->target_oloc.pool_ns) {
1826 hoid->nspace = t->target_oloc.pool_ns->str;
1827 hoid->nspace_len = t->target_oloc.pool_ns->len;
1828 } else {
1829 hoid->nspace = NULL;
1830 hoid->nspace_len = 0;
1831 }
1832 hoid->pool = t->target_oloc.pool;
1833 ceph_hoid_build_hash_cache(hoid);
1834}
1835
1836static bool should_plug_request(struct ceph_osd_request *req)
1837{
1838 struct ceph_osd *osd = req->r_osd;
1839 struct ceph_spg_mapping *spg;
1840 struct ceph_osd_backoff *backoff;
1841 struct ceph_hobject_id hoid;
1842
1843 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1844 if (!spg)
1845 return false;
1846
1847 hoid_fill_from_target(&hoid, &req->r_t);
1848 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1849 if (!backoff)
1850 return false;
1851
1852 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1853 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1854 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1855 return true;
1856}
1857
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001858static void setup_request_data(struct ceph_osd_request *req,
1859 struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001860{
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001861 u32 data_len = 0;
1862 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001863
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001864 if (!list_empty(&msg->data))
1865 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001866
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001867 WARN_ON(msg->data_length);
1868 for (i = 0; i < req->r_num_ops; i++) {
1869 struct ceph_osd_req_op *op = &req->r_ops[i];
1870
1871 switch (op->op) {
1872 /* request */
1873 case CEPH_OSD_OP_WRITE:
1874 case CEPH_OSD_OP_WRITEFULL:
1875 WARN_ON(op->indata_len != op->extent.length);
1876 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1877 break;
1878 case CEPH_OSD_OP_SETXATTR:
1879 case CEPH_OSD_OP_CMPXATTR:
1880 WARN_ON(op->indata_len != op->xattr.name_len +
1881 op->xattr.value_len);
1882 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1883 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001884 case CEPH_OSD_OP_NOTIFY_ACK:
1885 ceph_osdc_msg_data_add(msg,
1886 &op->notify_ack.request_data);
1887 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001888
1889 /* reply */
1890 case CEPH_OSD_OP_STAT:
1891 ceph_osdc_msg_data_add(req->r_reply,
1892 &op->raw_data_in);
1893 break;
1894 case CEPH_OSD_OP_READ:
1895 ceph_osdc_msg_data_add(req->r_reply,
1896 &op->extent.osd_data);
1897 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001898 case CEPH_OSD_OP_LIST_WATCHERS:
1899 ceph_osdc_msg_data_add(req->r_reply,
1900 &op->list_watchers.response_data);
1901 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001902
1903 /* both */
1904 case CEPH_OSD_OP_CALL:
1905 WARN_ON(op->indata_len != op->cls.class_len +
1906 op->cls.method_len +
1907 op->cls.indata_len);
1908 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1909 /* optional, can be NONE */
1910 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1911 /* optional, can be NONE */
1912 ceph_osdc_msg_data_add(req->r_reply,
1913 &op->cls.response_data);
1914 break;
Ilya Dryomov19079202016-04-28 16:07:27 +02001915 case CEPH_OSD_OP_NOTIFY:
1916 ceph_osdc_msg_data_add(msg,
1917 &op->notify.request_data);
1918 ceph_osdc_msg_data_add(req->r_reply,
1919 &op->notify.response_data);
1920 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001921 }
1922
1923 data_len += op->indata_len;
1924 }
1925
1926 WARN_ON(data_len != msg->data_length);
1927}
1928
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001929static void encode_pgid(void **p, const struct ceph_pg *pgid)
1930{
1931 ceph_encode_8(p, 1);
1932 ceph_encode_64(p, pgid->pool);
1933 ceph_encode_32(p, pgid->seed);
1934 ceph_encode_32(p, -1); /* preferred */
1935}
1936
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001937static void encode_spgid(void **p, const struct ceph_spg *spgid)
1938{
1939 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1940 encode_pgid(p, &spgid->pgid);
1941 ceph_encode_8(p, spgid->shard);
1942}
1943
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001944static void encode_oloc(void **p, void *end,
1945 const struct ceph_object_locator *oloc)
1946{
1947 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1948 ceph_encode_64(p, oloc->pool);
1949 ceph_encode_32(p, -1); /* preferred */
1950 ceph_encode_32(p, 0); /* key len */
1951 if (oloc->pool_ns)
1952 ceph_encode_string(p, end, oloc->pool_ns->str,
1953 oloc->pool_ns->len);
1954 else
1955 ceph_encode_32(p, 0);
1956}
1957
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001958static void encode_request_partial(struct ceph_osd_request *req,
1959 struct ceph_msg *msg)
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001960{
1961 void *p = msg->front.iov_base;
1962 void *const end = p + msg->front_alloc_len;
1963 u32 data_len = 0;
1964 int i;
1965
1966 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1967 /* snapshots aren't writeable */
1968 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1969 } else {
1970 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1971 req->r_data_offset || req->r_snapc);
1972 }
1973
1974 setup_request_data(req, msg);
1975
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001976 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1977 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001978 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1979 ceph_encode_32(&p, req->r_flags);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001980
1981 /* reqid */
1982 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1983 memset(p, 0, sizeof(struct ceph_osd_reqid));
1984 p += sizeof(struct ceph_osd_reqid);
1985
1986 /* trace */
1987 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1988 p += sizeof(struct ceph_blkin_trace_info);
1989
1990 ceph_encode_32(&p, 0); /* client_inc, always 0 */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001991 ceph_encode_timespec(p, &req->r_mtime);
1992 p += sizeof(struct ceph_timespec);
Jeff Laytonaa26d662017-04-04 08:39:36 -04001993
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001994 encode_oloc(&p, end, &req->r_t.target_oloc);
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001995 ceph_encode_string(&p, end, req->r_t.target_oid.name,
1996 req->r_t.target_oid.name_len);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001997
1998 /* ops, can imply data */
1999 ceph_encode_16(&p, req->r_num_ops);
2000 for (i = 0; i < req->r_num_ops; i++) {
2001 data_len += osd_req_encode_op(p, &req->r_ops[i]);
2002 p += sizeof(struct ceph_osd_op);
2003 }
2004
2005 ceph_encode_64(&p, req->r_snapid); /* snapid */
2006 if (req->r_snapc) {
2007 ceph_encode_64(&p, req->r_snapc->seq);
2008 ceph_encode_32(&p, req->r_snapc->num_snaps);
2009 for (i = 0; i < req->r_snapc->num_snaps; i++)
2010 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2011 } else {
2012 ceph_encode_64(&p, 0); /* snap_seq */
2013 ceph_encode_32(&p, 0); /* snaps len */
2014 }
2015
2016 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002017 BUG_ON(p > end - 8); /* space for features */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002018
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002019 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2020 /* front_len is finalized in encode_request_finish() */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002021 msg->front.iov_len = p - msg->front.iov_base;
2022 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002023 msg->hdr.data_len = cpu_to_le32(data_len);
2024 /*
2025 * The header "data_off" is a hint to the receiver allowing it
2026 * to align received data into its buffers such that there's no
2027 * need to re-copy it before writing it to disk (direct I/O).
2028 */
2029 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2030
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002031 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2032 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2033}
2034
2035static void encode_request_finish(struct ceph_msg *msg)
2036{
2037 void *p = msg->front.iov_base;
Ilya Dryomov986e8982017-07-25 14:40:03 +02002038 void *const partial_end = p + msg->front.iov_len;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002039 void *const end = p + msg->front_alloc_len;
2040
2041 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2042 /* luminous OSD -- encode features and be done */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002043 p = partial_end;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002044 ceph_encode_64(&p, msg->con->peer_features);
2045 } else {
2046 struct {
2047 char spgid[CEPH_ENCODING_START_BLK_LEN +
2048 CEPH_PGID_ENCODING_LEN + 1];
2049 __le32 hash;
2050 __le32 epoch;
2051 __le32 flags;
2052 char reqid[CEPH_ENCODING_START_BLK_LEN +
2053 sizeof(struct ceph_osd_reqid)];
2054 char trace[sizeof(struct ceph_blkin_trace_info)];
2055 __le32 client_inc;
2056 struct ceph_timespec mtime;
2057 } __packed head;
2058 struct ceph_pg pgid;
2059 void *oloc, *oid, *tail;
2060 int oloc_len, oid_len, tail_len;
2061 int len;
2062
2063 /*
2064 * Pre-luminous OSD -- reencode v8 into v4 using @head
2065 * as a temporary buffer. Encode the raw PG; the rest
2066 * is just a matter of moving oloc, oid and tail blobs
2067 * around.
2068 */
2069 memcpy(&head, p, sizeof(head));
2070 p += sizeof(head);
2071
2072 oloc = p;
2073 p += CEPH_ENCODING_START_BLK_LEN;
2074 pgid.pool = ceph_decode_64(&p);
2075 p += 4 + 4; /* preferred, key len */
2076 len = ceph_decode_32(&p);
2077 p += len; /* nspace */
2078 oloc_len = p - oloc;
2079
2080 oid = p;
2081 len = ceph_decode_32(&p);
2082 p += len;
2083 oid_len = p - oid;
2084
2085 tail = p;
Ilya Dryomov986e8982017-07-25 14:40:03 +02002086 tail_len = partial_end - p;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002087
2088 p = msg->front.iov_base;
2089 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2090 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2091 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2092 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2093
2094 /* reassert_version */
2095 memset(p, 0, sizeof(struct ceph_eversion));
2096 p += sizeof(struct ceph_eversion);
2097
2098 BUG_ON(p >= oloc);
2099 memmove(p, oloc, oloc_len);
2100 p += oloc_len;
2101
2102 pgid.seed = le32_to_cpu(head.hash);
2103 encode_pgid(&p, &pgid); /* raw pg */
2104
2105 BUG_ON(p >= oid);
2106 memmove(p, oid, oid_len);
2107 p += oid_len;
2108
2109 /* tail -- ops, snapid, snapc, retry_attempt */
2110 BUG_ON(p >= tail);
2111 memmove(p, tail, tail_len);
2112 p += tail_len;
2113
2114 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2115 }
2116
2117 BUG_ON(p > end);
2118 msg->front.iov_len = p - msg->front.iov_base;
2119 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2120
2121 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2122 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2123 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2124 le16_to_cpu(msg->hdr.version));
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002125}
2126
2127/*
2128 * @req has to be assigned a tid and registered.
2129 */
2130static void send_request(struct ceph_osd_request *req)
2131{
2132 struct ceph_osd *osd = req->r_osd;
2133
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002134 verify_osd_locked(osd);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002135 WARN_ON(osd->o_osd != req->r_t.osd);
2136
Ilya Dryomova02a9462017-06-19 12:18:05 +02002137 /* backoff? */
2138 if (should_plug_request(req))
2139 return;
2140
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002141 /*
2142 * We may have a previously queued request message hanging
2143 * around. Cancel it to avoid corrupting the msgr.
2144 */
2145 if (req->r_sent)
2146 ceph_msg_revoke(req->r_request);
2147
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002148 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2149 if (req->r_attempts)
2150 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2151 else
2152 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2153
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002154 encode_request_partial(req, req->r_request);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002155
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002156 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002157 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002158 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002159 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2160 req->r_attempts);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002161
2162 req->r_t.paused = false;
Sage Weil3dd72fc2010-03-22 14:42:30 -07002163 req->r_stamp = jiffies;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002164 req->r_attempts++;
Sage Weilf24e9982009-10-06 11:31:10 -07002165
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002166 req->r_sent = osd->o_incarnation;
2167 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2168 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
Sage Weilf24e9982009-10-06 11:31:10 -07002169}
2170
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002171static void maybe_request_map(struct ceph_osd_client *osdc)
2172{
2173 bool continuous = false;
2174
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002175 verify_osdc_locked(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002176 WARN_ON(!osdc->osdmap->epoch);
2177
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002178 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2179 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2180 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002181 dout("%s osdc %p continuous\n", __func__, osdc);
2182 continuous = true;
2183 } else {
2184 dout("%s osdc %p onetime\n", __func__, osdc);
2185 }
2186
2187 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2188 osdc->osdmap->epoch + 1, continuous))
2189 ceph_monc_renew_subs(&osdc->client->monc);
2190}
2191
Jeff Laytona1f40202017-04-04 08:39:37 -04002192static void complete_request(struct ceph_osd_request *req, int err);
Ilya Dryomov46092452016-04-28 16:07:27 +02002193static void send_map_check(struct ceph_osd_request *req);
2194
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002195static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002196{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002197 struct ceph_osd_client *osdc = req->r_osdc;
2198 struct ceph_osd *osd;
Ilya Dryomov46092452016-04-28 16:07:27 +02002199 enum calc_target_result ct_res;
Ilya Dryomov66850df2018-05-15 15:47:58 +02002200 int err = 0;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002201 bool need_send = false;
2202 bool promoted = false;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002203
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002204 WARN_ON(req->r_tid);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002205 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2206
2207again:
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002208 ct_res = calc_target(osdc, &req->r_t, NULL, false);
Ilya Dryomov46092452016-04-28 16:07:27 +02002209 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2210 goto promote;
2211
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002212 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2213 if (IS_ERR(osd)) {
2214 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2215 goto promote;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002216 }
2217
Ilya Dryomov66850df2018-05-15 15:47:58 +02002218 if (osdc->abort_err) {
2219 dout("req %p abort_err %d\n", req, osdc->abort_err);
2220 err = osdc->abort_err;
2221 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
Jeff Layton58eb7932017-04-18 09:21:16 -04002222 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2223 osdc->epoch_barrier);
2224 req->r_t.paused = true;
2225 maybe_request_map(osdc);
2226 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2227 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002228 dout("req %p pausewr\n", req);
2229 req->r_t.paused = true;
2230 maybe_request_map(osdc);
2231 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002232 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002233 dout("req %p pauserd\n", req);
2234 req->r_t.paused = true;
2235 maybe_request_map(osdc);
2236 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2237 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2238 CEPH_OSD_FLAG_FULL_FORCE)) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002239 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002240 pool_full(osdc, req->r_t.base_oloc.pool))) {
2241 dout("req %p full/pool_full\n", req);
2242 pr_warn_ratelimited("FULL or reached pool quota\n");
2243 req->r_t.paused = true;
2244 maybe_request_map(osdc);
Jeff Laytona1f40202017-04-04 08:39:37 -04002245 if (req->r_abort_on_full)
Ilya Dryomov66850df2018-05-15 15:47:58 +02002246 err = -ENOSPC;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002247 } else if (!osd_homeless(osd)) {
2248 need_send = true;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002249 } else {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002250 maybe_request_map(osdc);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002251 }
2252
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002253 mutex_lock(&osd->lock);
2254 /*
2255 * Assign the tid atomically with send_request() to protect
2256 * multiple writes to the same object from racing with each
2257 * other, resulting in out of order ops on the OSDs.
2258 */
2259 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2260 link_request(osd, req);
2261 if (need_send)
2262 send_request(req);
Ilya Dryomov66850df2018-05-15 15:47:58 +02002263 else if (err)
2264 complete_request(req, err);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002265 mutex_unlock(&osd->lock);
2266
Ilya Dryomov46092452016-04-28 16:07:27 +02002267 if (ct_res == CALC_TARGET_POOL_DNE)
2268 send_map_check(req);
2269
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002270 if (promoted)
2271 downgrade_write(&osdc->lock);
2272 return;
2273
2274promote:
2275 up_read(&osdc->lock);
2276 down_write(&osdc->lock);
2277 wrlocked = true;
2278 promoted = true;
2279 goto again;
2280}
2281
2282static void account_request(struct ceph_osd_request *req)
2283{
Ilya Dryomov54ea0042017-02-11 18:48:41 +01002284 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002285 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002286
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002287 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002288 atomic_inc(&req->r_osdc->num_requests);
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002289
2290 req->r_start_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002291}
2292
2293static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2294{
2295 ceph_osdc_get_request(req);
2296 account_request(req);
2297 __submit_request(req, wrlocked);
2298}
2299
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002300static void finish_request(struct ceph_osd_request *req)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002301{
2302 struct ceph_osd_client *osdc = req->r_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002303
Ilya Dryomov46092452016-04-28 16:07:27 +02002304 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002305 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2306
2307 if (req->r_osd)
2308 unlink_request(req->r_osd, req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002309 atomic_dec(&osdc->num_requests);
2310
2311 /*
2312 * If an OSD has failed or returned and a request has been sent
2313 * twice, it's possible to get a reply and end up here while the
2314 * request message is queued for delivery. We will ignore the
2315 * reply, so not a big deal, but better to try and catch it.
2316 */
2317 ceph_msg_revoke(req->r_request);
2318 ceph_msg_revoke_incoming(req->r_reply);
2319}
2320
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002321static void __complete_request(struct ceph_osd_request *req)
2322{
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002323 if (req->r_callback) {
2324 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2325 req->r_tid, req->r_callback, req->r_result);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002326 req->r_callback(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002327 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002328}
2329
Ilya Dryomov46092452016-04-28 16:07:27 +02002330/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002331 * This is open-coded in handle_reply().
Ilya Dryomov46092452016-04-28 16:07:27 +02002332 */
2333static void complete_request(struct ceph_osd_request *req, int err)
2334{
2335 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2336
2337 req->r_result = err;
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002338 finish_request(req);
Ilya Dryomov46092452016-04-28 16:07:27 +02002339 __complete_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002340 complete_all(&req->r_completion);
Ilya Dryomov46092452016-04-28 16:07:27 +02002341 ceph_osdc_put_request(req);
2342}
2343
2344static void cancel_map_check(struct ceph_osd_request *req)
2345{
2346 struct ceph_osd_client *osdc = req->r_osdc;
2347 struct ceph_osd_request *lookup_req;
2348
2349 verify_osdc_wrlocked(osdc);
2350
2351 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2352 if (!lookup_req)
2353 return;
2354
2355 WARN_ON(lookup_req != req);
2356 erase_request_mc(&osdc->map_checks, req);
2357 ceph_osdc_put_request(req);
2358}
2359
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002360static void cancel_request(struct ceph_osd_request *req)
2361{
2362 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2363
Ilya Dryomov46092452016-04-28 16:07:27 +02002364 cancel_map_check(req);
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002365 finish_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002366 complete_all(&req->r_completion);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01002367 ceph_osdc_put_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002368}
2369
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002370static void abort_request(struct ceph_osd_request *req, int err)
2371{
2372 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2373
2374 cancel_map_check(req);
2375 complete_request(req, err);
2376}
2377
Ilya Dryomov66850df2018-05-15 15:47:58 +02002378static int abort_fn(struct ceph_osd_request *req, void *arg)
2379{
2380 int err = *(int *)arg;
2381
2382 abort_request(req, err);
2383 return 0; /* continue iteration */
2384}
2385
2386/*
2387 * Abort all in-flight requests with @err and arrange for all future
2388 * requests to be failed immediately.
2389 */
2390void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2391{
2392 dout("%s osdc %p err %d\n", __func__, osdc, err);
2393 down_write(&osdc->lock);
2394 for_each_request(osdc, abort_fn, &err);
2395 osdc->abort_err = err;
2396 up_write(&osdc->lock);
2397}
2398EXPORT_SYMBOL(ceph_osdc_abort_requests);
2399
Jeff Layton58eb7932017-04-18 09:21:16 -04002400static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2401{
2402 if (likely(eb > osdc->epoch_barrier)) {
2403 dout("updating epoch_barrier from %u to %u\n",
2404 osdc->epoch_barrier, eb);
2405 osdc->epoch_barrier = eb;
2406 /* Request map if we're not to the barrier yet */
2407 if (eb > osdc->osdmap->epoch)
2408 maybe_request_map(osdc);
2409 }
2410}
2411
2412void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2413{
2414 down_read(&osdc->lock);
2415 if (unlikely(eb > osdc->epoch_barrier)) {
2416 up_read(&osdc->lock);
2417 down_write(&osdc->lock);
2418 update_epoch_barrier(osdc, eb);
2419 up_write(&osdc->lock);
2420 } else {
2421 up_read(&osdc->lock);
2422 }
2423}
2424EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2425
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002426/*
2427 * Drop all pending requests that are stalled waiting on a full condition to
Jeff Layton58eb7932017-04-18 09:21:16 -04002428 * clear, and complete them with ENOSPC as the return code. Set the
2429 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2430 * cancelled.
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002431 */
2432static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2433{
2434 struct rb_node *n;
Jeff Layton58eb7932017-04-18 09:21:16 -04002435 bool victims = false;
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002436
2437 dout("enter abort_on_full\n");
2438
2439 if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2440 goto out;
2441
Jeff Layton58eb7932017-04-18 09:21:16 -04002442 /* Scan list and see if there is anything to abort */
2443 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2444 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2445 struct rb_node *m;
2446
2447 m = rb_first(&osd->o_requests);
2448 while (m) {
2449 struct ceph_osd_request *req = rb_entry(m,
2450 struct ceph_osd_request, r_node);
2451 m = rb_next(m);
2452
2453 if (req->r_abort_on_full) {
2454 victims = true;
2455 break;
2456 }
2457 }
2458 if (victims)
2459 break;
2460 }
2461
2462 if (!victims)
2463 goto out;
2464
2465 /*
2466 * Update the barrier to current epoch if it's behind that point,
2467 * since we know we have some calls to be aborted in the tree.
2468 */
2469 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2470
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002471 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2472 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2473 struct rb_node *m;
2474
2475 m = rb_first(&osd->o_requests);
2476 while (m) {
2477 struct ceph_osd_request *req = rb_entry(m,
2478 struct ceph_osd_request, r_node);
2479 m = rb_next(m);
2480
2481 if (req->r_abort_on_full &&
2482 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2483 pool_full(osdc, req->r_t.target_oloc.pool)))
2484 abort_request(req, -ENOSPC);
2485 }
2486 }
2487out:
Jeff Layton58eb7932017-04-18 09:21:16 -04002488 dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002489}
2490
Ilya Dryomov46092452016-04-28 16:07:27 +02002491static void check_pool_dne(struct ceph_osd_request *req)
2492{
2493 struct ceph_osd_client *osdc = req->r_osdc;
2494 struct ceph_osdmap *map = osdc->osdmap;
2495
2496 verify_osdc_wrlocked(osdc);
2497 WARN_ON(!map->epoch);
2498
2499 if (req->r_attempts) {
2500 /*
2501 * We sent a request earlier, which means that
2502 * previously the pool existed, and now it does not
2503 * (i.e., it was deleted).
2504 */
2505 req->r_map_dne_bound = map->epoch;
2506 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2507 req->r_tid);
2508 } else {
2509 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2510 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2511 }
2512
2513 if (req->r_map_dne_bound) {
2514 if (map->epoch >= req->r_map_dne_bound) {
2515 /* we had a new enough map */
2516 pr_info_ratelimited("tid %llu pool does not exist\n",
2517 req->r_tid);
2518 complete_request(req, -ENOENT);
2519 }
2520 } else {
2521 send_map_check(req);
2522 }
2523}
2524
2525static void map_check_cb(struct ceph_mon_generic_request *greq)
2526{
2527 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2528 struct ceph_osd_request *req;
2529 u64 tid = greq->private_data;
2530
2531 WARN_ON(greq->result || !greq->u.newest);
2532
2533 down_write(&osdc->lock);
2534 req = lookup_request_mc(&osdc->map_checks, tid);
2535 if (!req) {
2536 dout("%s tid %llu dne\n", __func__, tid);
2537 goto out_unlock;
2538 }
2539
2540 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2541 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2542 if (!req->r_map_dne_bound)
2543 req->r_map_dne_bound = greq->u.newest;
2544 erase_request_mc(&osdc->map_checks, req);
2545 check_pool_dne(req);
2546
2547 ceph_osdc_put_request(req);
2548out_unlock:
2549 up_write(&osdc->lock);
2550}
2551
2552static void send_map_check(struct ceph_osd_request *req)
2553{
2554 struct ceph_osd_client *osdc = req->r_osdc;
2555 struct ceph_osd_request *lookup_req;
2556 int ret;
2557
2558 verify_osdc_wrlocked(osdc);
2559
2560 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2561 if (lookup_req) {
2562 WARN_ON(lookup_req != req);
2563 return;
2564 }
2565
2566 ceph_osdc_get_request(req);
2567 insert_request_mc(&osdc->map_checks, req);
2568 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2569 map_check_cb, req->r_tid);
2570 WARN_ON(ret);
2571}
2572
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002573/*
Ilya Dryomov922dab62016-05-26 01:15:02 +02002574 * lingering requests, watch/notify v2 infrastructure
2575 */
2576static void linger_release(struct kref *kref)
2577{
2578 struct ceph_osd_linger_request *lreq =
2579 container_of(kref, struct ceph_osd_linger_request, kref);
2580
2581 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2582 lreq->reg_req, lreq->ping_req);
2583 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2584 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
Ilya Dryomov46092452016-04-28 16:07:27 +02002585 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002586 WARN_ON(!list_empty(&lreq->scan_item));
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002587 WARN_ON(!list_empty(&lreq->pending_lworks));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002588 WARN_ON(lreq->osd);
2589
2590 if (lreq->reg_req)
2591 ceph_osdc_put_request(lreq->reg_req);
2592 if (lreq->ping_req)
2593 ceph_osdc_put_request(lreq->ping_req);
2594 target_destroy(&lreq->t);
2595 kfree(lreq);
2596}
2597
2598static void linger_put(struct ceph_osd_linger_request *lreq)
2599{
2600 if (lreq)
2601 kref_put(&lreq->kref, linger_release);
2602}
2603
2604static struct ceph_osd_linger_request *
2605linger_get(struct ceph_osd_linger_request *lreq)
2606{
2607 kref_get(&lreq->kref);
2608 return lreq;
2609}
2610
2611static struct ceph_osd_linger_request *
2612linger_alloc(struct ceph_osd_client *osdc)
2613{
2614 struct ceph_osd_linger_request *lreq;
2615
2616 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2617 if (!lreq)
2618 return NULL;
2619
2620 kref_init(&lreq->kref);
2621 mutex_init(&lreq->lock);
2622 RB_CLEAR_NODE(&lreq->node);
2623 RB_CLEAR_NODE(&lreq->osdc_node);
Ilya Dryomov46092452016-04-28 16:07:27 +02002624 RB_CLEAR_NODE(&lreq->mc_node);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002625 INIT_LIST_HEAD(&lreq->scan_item);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002626 INIT_LIST_HEAD(&lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002627 init_completion(&lreq->reg_commit_wait);
Ilya Dryomov19079202016-04-28 16:07:27 +02002628 init_completion(&lreq->notify_finish_wait);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002629
2630 lreq->osdc = osdc;
2631 target_init(&lreq->t);
2632
2633 dout("%s lreq %p\n", __func__, lreq);
2634 return lreq;
2635}
2636
2637DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2638DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02002639DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002640
2641/*
2642 * Create linger request <-> OSD session relation.
2643 *
2644 * @lreq has to be registered, @osd may be homeless.
2645 */
2646static void link_linger(struct ceph_osd *osd,
2647 struct ceph_osd_linger_request *lreq)
2648{
2649 verify_osd_locked(osd);
2650 WARN_ON(!lreq->linger_id || lreq->osd);
2651 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2652 osd->o_osd, lreq, lreq->linger_id);
2653
2654 if (!osd_homeless(osd))
2655 __remove_osd_from_lru(osd);
2656 else
2657 atomic_inc(&osd->o_osdc->num_homeless);
2658
2659 get_osd(osd);
2660 insert_linger(&osd->o_linger_requests, lreq);
2661 lreq->osd = osd;
2662}
2663
2664static void unlink_linger(struct ceph_osd *osd,
2665 struct ceph_osd_linger_request *lreq)
2666{
2667 verify_osd_locked(osd);
2668 WARN_ON(lreq->osd != osd);
2669 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2670 osd->o_osd, lreq, lreq->linger_id);
2671
2672 lreq->osd = NULL;
2673 erase_linger(&osd->o_linger_requests, lreq);
2674 put_osd(osd);
2675
2676 if (!osd_homeless(osd))
2677 maybe_move_osd_to_lru(osd);
2678 else
2679 atomic_dec(&osd->o_osdc->num_homeless);
2680}
2681
2682static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2683{
2684 verify_osdc_locked(lreq->osdc);
2685
2686 return !RB_EMPTY_NODE(&lreq->osdc_node);
2687}
2688
2689static bool linger_registered(struct ceph_osd_linger_request *lreq)
2690{
2691 struct ceph_osd_client *osdc = lreq->osdc;
2692 bool registered;
2693
2694 down_read(&osdc->lock);
2695 registered = __linger_registered(lreq);
2696 up_read(&osdc->lock);
2697
2698 return registered;
2699}
2700
2701static void linger_register(struct ceph_osd_linger_request *lreq)
2702{
2703 struct ceph_osd_client *osdc = lreq->osdc;
2704
2705 verify_osdc_wrlocked(osdc);
2706 WARN_ON(lreq->linger_id);
2707
2708 linger_get(lreq);
2709 lreq->linger_id = ++osdc->last_linger_id;
2710 insert_linger_osdc(&osdc->linger_requests, lreq);
2711}
2712
2713static void linger_unregister(struct ceph_osd_linger_request *lreq)
2714{
2715 struct ceph_osd_client *osdc = lreq->osdc;
2716
2717 verify_osdc_wrlocked(osdc);
2718
2719 erase_linger_osdc(&osdc->linger_requests, lreq);
2720 linger_put(lreq);
2721}
2722
2723static void cancel_linger_request(struct ceph_osd_request *req)
2724{
2725 struct ceph_osd_linger_request *lreq = req->r_priv;
2726
2727 WARN_ON(!req->r_linger);
2728 cancel_request(req);
2729 linger_put(lreq);
2730}
2731
2732struct linger_work {
2733 struct work_struct work;
2734 struct ceph_osd_linger_request *lreq;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002735 struct list_head pending_item;
2736 unsigned long queued_stamp;
Ilya Dryomov922dab62016-05-26 01:15:02 +02002737
2738 union {
2739 struct {
2740 u64 notify_id;
2741 u64 notifier_id;
2742 void *payload; /* points into @msg front */
2743 size_t payload_len;
2744
2745 struct ceph_msg *msg; /* for ceph_msg_put() */
2746 } notify;
2747 struct {
2748 int err;
2749 } error;
2750 };
2751};
2752
2753static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2754 work_func_t workfn)
2755{
2756 struct linger_work *lwork;
2757
2758 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2759 if (!lwork)
2760 return NULL;
2761
2762 INIT_WORK(&lwork->work, workfn);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002763 INIT_LIST_HEAD(&lwork->pending_item);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002764 lwork->lreq = linger_get(lreq);
2765
2766 return lwork;
2767}
2768
2769static void lwork_free(struct linger_work *lwork)
2770{
2771 struct ceph_osd_linger_request *lreq = lwork->lreq;
2772
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002773 mutex_lock(&lreq->lock);
2774 list_del(&lwork->pending_item);
2775 mutex_unlock(&lreq->lock);
2776
Ilya Dryomov922dab62016-05-26 01:15:02 +02002777 linger_put(lreq);
2778 kfree(lwork);
2779}
2780
2781static void lwork_queue(struct linger_work *lwork)
2782{
2783 struct ceph_osd_linger_request *lreq = lwork->lreq;
2784 struct ceph_osd_client *osdc = lreq->osdc;
2785
2786 verify_lreq_locked(lreq);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002787 WARN_ON(!list_empty(&lwork->pending_item));
2788
2789 lwork->queued_stamp = jiffies;
2790 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002791 queue_work(osdc->notify_wq, &lwork->work);
2792}
2793
2794static void do_watch_notify(struct work_struct *w)
2795{
2796 struct linger_work *lwork = container_of(w, struct linger_work, work);
2797 struct ceph_osd_linger_request *lreq = lwork->lreq;
2798
2799 if (!linger_registered(lreq)) {
2800 dout("%s lreq %p not registered\n", __func__, lreq);
2801 goto out;
2802 }
2803
Ilya Dryomov19079202016-04-28 16:07:27 +02002804 WARN_ON(!lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002805 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2806 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2807 lwork->notify.payload_len);
2808 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2809 lwork->notify.notifier_id, lwork->notify.payload,
2810 lwork->notify.payload_len);
2811
2812out:
2813 ceph_msg_put(lwork->notify.msg);
2814 lwork_free(lwork);
2815}
2816
2817static void do_watch_error(struct work_struct *w)
2818{
2819 struct linger_work *lwork = container_of(w, struct linger_work, work);
2820 struct ceph_osd_linger_request *lreq = lwork->lreq;
2821
2822 if (!linger_registered(lreq)) {
2823 dout("%s lreq %p not registered\n", __func__, lreq);
2824 goto out;
2825 }
2826
2827 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2828 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2829
2830out:
2831 lwork_free(lwork);
2832}
2833
2834static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2835{
2836 struct linger_work *lwork;
2837
2838 lwork = lwork_alloc(lreq, do_watch_error);
2839 if (!lwork) {
2840 pr_err("failed to allocate error-lwork\n");
2841 return;
2842 }
2843
2844 lwork->error.err = lreq->last_error;
2845 lwork_queue(lwork);
2846}
2847
2848static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2849 int result)
2850{
2851 if (!completion_done(&lreq->reg_commit_wait)) {
2852 lreq->reg_commit_error = (result <= 0 ? result : 0);
2853 complete_all(&lreq->reg_commit_wait);
2854 }
2855}
2856
2857static void linger_commit_cb(struct ceph_osd_request *req)
2858{
2859 struct ceph_osd_linger_request *lreq = req->r_priv;
2860
2861 mutex_lock(&lreq->lock);
2862 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2863 lreq->linger_id, req->r_result);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002864 linger_reg_commit_complete(lreq, req->r_result);
2865 lreq->committed = true;
2866
Ilya Dryomov19079202016-04-28 16:07:27 +02002867 if (!lreq->is_watch) {
2868 struct ceph_osd_data *osd_data =
2869 osd_req_op_data(req, 0, notify, response_data);
2870 void *p = page_address(osd_data->pages[0]);
2871
2872 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2873 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2874
2875 /* make note of the notify_id */
2876 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2877 lreq->notify_id = ceph_decode_64(&p);
2878 dout("lreq %p notify_id %llu\n", lreq,
2879 lreq->notify_id);
2880 } else {
2881 dout("lreq %p no notify_id\n", lreq);
2882 }
2883 }
2884
Ilya Dryomov922dab62016-05-26 01:15:02 +02002885 mutex_unlock(&lreq->lock);
2886 linger_put(lreq);
2887}
2888
2889static int normalize_watch_error(int err)
2890{
2891 /*
2892 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2893 * notification and a failure to reconnect because we raced with
2894 * the delete appear the same to the user.
2895 */
2896 if (err == -ENOENT)
2897 err = -ENOTCONN;
2898
2899 return err;
2900}
2901
2902static void linger_reconnect_cb(struct ceph_osd_request *req)
2903{
2904 struct ceph_osd_linger_request *lreq = req->r_priv;
2905
2906 mutex_lock(&lreq->lock);
2907 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2908 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2909 if (req->r_result < 0) {
2910 if (!lreq->last_error) {
2911 lreq->last_error = normalize_watch_error(req->r_result);
2912 queue_watch_error(lreq);
2913 }
2914 }
2915
2916 mutex_unlock(&lreq->lock);
2917 linger_put(lreq);
2918}
2919
2920static void send_linger(struct ceph_osd_linger_request *lreq)
2921{
2922 struct ceph_osd_request *req = lreq->reg_req;
2923 struct ceph_osd_req_op *op = &req->r_ops[0];
2924
2925 verify_osdc_wrlocked(req->r_osdc);
2926 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2927
2928 if (req->r_osd)
2929 cancel_linger_request(req);
2930
2931 request_reinit(req);
2932 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2933 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2934 req->r_flags = lreq->t.flags;
2935 req->r_mtime = lreq->mtime;
2936
2937 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002938 if (lreq->is_watch && lreq->committed) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002939 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2940 op->watch.cookie != lreq->linger_id);
2941 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2942 op->watch.gen = ++lreq->register_gen;
2943 dout("lreq %p reconnect register_gen %u\n", lreq,
2944 op->watch.gen);
2945 req->r_callback = linger_reconnect_cb;
2946 } else {
Ilya Dryomov19079202016-04-28 16:07:27 +02002947 if (!lreq->is_watch)
2948 lreq->notify_id = 0;
2949 else
2950 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002951 dout("lreq %p register\n", lreq);
2952 req->r_callback = linger_commit_cb;
2953 }
2954 mutex_unlock(&lreq->lock);
2955
2956 req->r_priv = linger_get(lreq);
2957 req->r_linger = true;
2958
2959 submit_request(req, true);
2960}
2961
2962static void linger_ping_cb(struct ceph_osd_request *req)
2963{
2964 struct ceph_osd_linger_request *lreq = req->r_priv;
2965
2966 mutex_lock(&lreq->lock);
2967 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2968 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2969 lreq->last_error);
2970 if (lreq->register_gen == req->r_ops[0].watch.gen) {
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002971 if (!req->r_result) {
2972 lreq->watch_valid_thru = lreq->ping_sent;
2973 } else if (!lreq->last_error) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002974 lreq->last_error = normalize_watch_error(req->r_result);
2975 queue_watch_error(lreq);
2976 }
2977 } else {
2978 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2979 lreq->register_gen, req->r_ops[0].watch.gen);
2980 }
2981
2982 mutex_unlock(&lreq->lock);
2983 linger_put(lreq);
2984}
2985
2986static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2987{
2988 struct ceph_osd_client *osdc = lreq->osdc;
2989 struct ceph_osd_request *req = lreq->ping_req;
2990 struct ceph_osd_req_op *op = &req->r_ops[0];
2991
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002992 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002993 dout("%s PAUSERD\n", __func__);
2994 return;
2995 }
2996
2997 lreq->ping_sent = jiffies;
2998 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2999 __func__, lreq, lreq->linger_id, lreq->ping_sent,
3000 lreq->register_gen);
3001
3002 if (req->r_osd)
3003 cancel_linger_request(req);
3004
3005 request_reinit(req);
3006 target_copy(&req->r_t, &lreq->t);
3007
3008 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3009 op->watch.cookie != lreq->linger_id ||
3010 op->watch.op != CEPH_OSD_WATCH_OP_PING);
3011 op->watch.gen = lreq->register_gen;
3012 req->r_callback = linger_ping_cb;
3013 req->r_priv = linger_get(lreq);
3014 req->r_linger = true;
3015
3016 ceph_osdc_get_request(req);
3017 account_request(req);
3018 req->r_tid = atomic64_inc_return(&osdc->last_tid);
3019 link_request(lreq->osd, req);
3020 send_request(req);
3021}
3022
3023static void linger_submit(struct ceph_osd_linger_request *lreq)
3024{
3025 struct ceph_osd_client *osdc = lreq->osdc;
3026 struct ceph_osd *osd;
3027
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003028 calc_target(osdc, &lreq->t, NULL, false);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003029 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3030 link_linger(osd, lreq);
3031
3032 send_linger(lreq);
3033}
3034
Ilya Dryomov46092452016-04-28 16:07:27 +02003035static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3036{
3037 struct ceph_osd_client *osdc = lreq->osdc;
3038 struct ceph_osd_linger_request *lookup_lreq;
3039
3040 verify_osdc_wrlocked(osdc);
3041
3042 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3043 lreq->linger_id);
3044 if (!lookup_lreq)
3045 return;
3046
3047 WARN_ON(lookup_lreq != lreq);
3048 erase_linger_mc(&osdc->linger_map_checks, lreq);
3049 linger_put(lreq);
3050}
3051
Ilya Dryomov922dab62016-05-26 01:15:02 +02003052/*
3053 * @lreq has to be both registered and linked.
3054 */
3055static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3056{
Ilya Dryomov19079202016-04-28 16:07:27 +02003057 if (lreq->is_watch && lreq->ping_req->r_osd)
Ilya Dryomov922dab62016-05-26 01:15:02 +02003058 cancel_linger_request(lreq->ping_req);
3059 if (lreq->reg_req->r_osd)
3060 cancel_linger_request(lreq->reg_req);
Ilya Dryomov46092452016-04-28 16:07:27 +02003061 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003062 unlink_linger(lreq->osd, lreq);
3063 linger_unregister(lreq);
3064}
3065
3066static void linger_cancel(struct ceph_osd_linger_request *lreq)
3067{
3068 struct ceph_osd_client *osdc = lreq->osdc;
3069
3070 down_write(&osdc->lock);
3071 if (__linger_registered(lreq))
3072 __linger_cancel(lreq);
3073 up_write(&osdc->lock);
3074}
3075
Ilya Dryomov46092452016-04-28 16:07:27 +02003076static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3077
3078static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3079{
3080 struct ceph_osd_client *osdc = lreq->osdc;
3081 struct ceph_osdmap *map = osdc->osdmap;
3082
3083 verify_osdc_wrlocked(osdc);
3084 WARN_ON(!map->epoch);
3085
3086 if (lreq->register_gen) {
3087 lreq->map_dne_bound = map->epoch;
3088 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3089 lreq, lreq->linger_id);
3090 } else {
3091 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3092 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3093 map->epoch);
3094 }
3095
3096 if (lreq->map_dne_bound) {
3097 if (map->epoch >= lreq->map_dne_bound) {
3098 /* we had a new enough map */
3099 pr_info("linger_id %llu pool does not exist\n",
3100 lreq->linger_id);
3101 linger_reg_commit_complete(lreq, -ENOENT);
3102 __linger_cancel(lreq);
3103 }
3104 } else {
3105 send_linger_map_check(lreq);
3106 }
3107}
3108
3109static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3110{
3111 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3112 struct ceph_osd_linger_request *lreq;
3113 u64 linger_id = greq->private_data;
3114
3115 WARN_ON(greq->result || !greq->u.newest);
3116
3117 down_write(&osdc->lock);
3118 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3119 if (!lreq) {
3120 dout("%s linger_id %llu dne\n", __func__, linger_id);
3121 goto out_unlock;
3122 }
3123
3124 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3125 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3126 greq->u.newest);
3127 if (!lreq->map_dne_bound)
3128 lreq->map_dne_bound = greq->u.newest;
3129 erase_linger_mc(&osdc->linger_map_checks, lreq);
3130 check_linger_pool_dne(lreq);
3131
3132 linger_put(lreq);
3133out_unlock:
3134 up_write(&osdc->lock);
3135}
3136
3137static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3138{
3139 struct ceph_osd_client *osdc = lreq->osdc;
3140 struct ceph_osd_linger_request *lookup_lreq;
3141 int ret;
3142
3143 verify_osdc_wrlocked(osdc);
3144
3145 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3146 lreq->linger_id);
3147 if (lookup_lreq) {
3148 WARN_ON(lookup_lreq != lreq);
3149 return;
3150 }
3151
3152 linger_get(lreq);
3153 insert_linger_mc(&osdc->linger_map_checks, lreq);
3154 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3155 linger_map_check_cb, lreq->linger_id);
3156 WARN_ON(ret);
3157}
3158
Ilya Dryomov922dab62016-05-26 01:15:02 +02003159static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3160{
3161 int ret;
3162
3163 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3164 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3165 return ret ?: lreq->reg_commit_error;
3166}
3167
Ilya Dryomov19079202016-04-28 16:07:27 +02003168static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3169{
3170 int ret;
3171
3172 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3173 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3174 return ret ?: lreq->notify_finish_error;
3175}
3176
Ilya Dryomov922dab62016-05-26 01:15:02 +02003177/*
Ilya Dryomovfbca9632016-04-28 16:07:24 +02003178 * Timeout callback, called every N seconds. When 1 or more OSD
3179 * requests has been active for more than N seconds, we send a keepalive
3180 * (tag + timestamp) to its OSD to ensure any communications channel
3181 * reset is detected.
Sage Weilf24e9982009-10-06 11:31:10 -07003182 */
3183static void handle_timeout(struct work_struct *work)
3184{
3185 struct ceph_osd_client *osdc =
3186 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03003187 struct ceph_options *opts = osdc->client->options;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003188 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003189 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003190 LIST_HEAD(slow_osds);
3191 struct rb_node *n, *p;
Sage Weilf24e9982009-10-06 11:31:10 -07003192
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003193 dout("%s osdc %p\n", __func__, osdc);
3194 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003195
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003196 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003197 * ping osds that are a bit slow. this ensures that if there
3198 * is a break in the TCP connection we will notice, and reopen
3199 * a connection with that osd (from the fault callback).
3200 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003201 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3202 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3203 bool found = false;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003204
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003205 for (p = rb_first(&osd->o_requests); p; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003206 struct ceph_osd_request *req =
3207 rb_entry(p, struct ceph_osd_request, r_node);
3208
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003209 p = rb_next(p); /* abort_request() */
3210
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003211 if (time_before(req->r_stamp, cutoff)) {
3212 dout(" req %p tid %llu on osd%d is laggy\n",
3213 req, req->r_tid, osd->o_osd);
3214 found = true;
3215 }
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003216 if (opts->osd_request_timeout &&
3217 time_before(req->r_start_stamp, expiry_cutoff)) {
3218 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3219 req->r_tid, osd->o_osd);
3220 abort_request(req, -ETIMEDOUT);
3221 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003222 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003223 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3224 struct ceph_osd_linger_request *lreq =
3225 rb_entry(p, struct ceph_osd_linger_request, node);
3226
3227 dout(" lreq %p linger_id %llu is served by osd%d\n",
3228 lreq, lreq->linger_id, osd->o_osd);
3229 found = true;
3230
3231 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02003232 if (lreq->is_watch && lreq->committed && !lreq->last_error)
Ilya Dryomov922dab62016-05-26 01:15:02 +02003233 send_linger_ping(lreq);
3234 mutex_unlock(&lreq->lock);
3235 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003236
3237 if (found)
3238 list_move_tail(&osd->o_keepalive_item, &slow_osds);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003239 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003240
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003241 if (opts->osd_request_timeout) {
3242 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3243 struct ceph_osd_request *req =
3244 rb_entry(p, struct ceph_osd_request, r_node);
3245
3246 p = rb_next(p); /* abort_request() */
3247
3248 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3249 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3250 req->r_tid, osdc->homeless_osd.o_osd);
3251 abort_request(req, -ETIMEDOUT);
3252 }
3253 }
3254 }
3255
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003256 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3257 maybe_request_map(osdc);
3258
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003259 while (!list_empty(&slow_osds)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003260 struct ceph_osd *osd = list_first_entry(&slow_osds,
3261 struct ceph_osd,
3262 o_keepalive_item);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003263 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07003264 ceph_con_keepalive(&osd->o_con);
3265 }
3266
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003267 up_write(&osdc->lock);
Ilya Dryomovfbca9632016-04-28 16:07:24 +02003268 schedule_delayed_work(&osdc->timeout_work,
3269 osdc->client->options->osd_keepalive_timeout);
Sage Weilf24e9982009-10-06 11:31:10 -07003270}
3271
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003272static void handle_osds_timeout(struct work_struct *work)
3273{
3274 struct ceph_osd_client *osdc =
3275 container_of(work, struct ceph_osd_client,
3276 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03003277 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003278 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003279
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003280 dout("%s osdc %p\n", __func__, osdc);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003281 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003282 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3283 if (time_before(jiffies, osd->lru_ttl))
3284 break;
3285
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003286 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02003287 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003288 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003289 }
3290
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003291 up_write(&osdc->lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003292 schedule_delayed_work(&osdc->osds_timeout_work,
3293 round_jiffies_relative(delay));
3294}
3295
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003296static int ceph_oloc_decode(void **p, void *end,
3297 struct ceph_object_locator *oloc)
3298{
3299 u8 struct_v, struct_cv;
3300 u32 len;
3301 void *struct_end;
3302 int ret = 0;
3303
3304 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3305 struct_v = ceph_decode_8(p);
3306 struct_cv = ceph_decode_8(p);
3307 if (struct_v < 3) {
3308 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3309 struct_v, struct_cv);
3310 goto e_inval;
3311 }
3312 if (struct_cv > 6) {
3313 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3314 struct_v, struct_cv);
3315 goto e_inval;
3316 }
3317 len = ceph_decode_32(p);
3318 ceph_decode_need(p, end, len, e_inval);
3319 struct_end = *p + len;
3320
3321 oloc->pool = ceph_decode_64(p);
3322 *p += 4; /* skip preferred */
3323
3324 len = ceph_decode_32(p);
3325 if (len > 0) {
3326 pr_warn("ceph_object_locator::key is set\n");
3327 goto e_inval;
3328 }
3329
3330 if (struct_v >= 5) {
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003331 bool changed = false;
3332
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003333 len = ceph_decode_32(p);
3334 if (len > 0) {
Yan, Zheng30c156d2016-02-14 11:24:31 +08003335 ceph_decode_need(p, end, len, e_inval);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003336 if (!oloc->pool_ns ||
3337 ceph_compare_string(oloc->pool_ns, *p, len))
3338 changed = true;
Yan, Zheng30c156d2016-02-14 11:24:31 +08003339 *p += len;
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003340 } else {
3341 if (oloc->pool_ns)
3342 changed = true;
3343 }
3344 if (changed) {
3345 /* redirect changes namespace */
3346 pr_warn("ceph_object_locator::nspace is changed\n");
3347 goto e_inval;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003348 }
3349 }
3350
3351 if (struct_v >= 6) {
3352 s64 hash = ceph_decode_64(p);
3353 if (hash != -1) {
3354 pr_warn("ceph_object_locator::hash is set\n");
3355 goto e_inval;
3356 }
3357 }
3358
3359 /* skip the rest */
3360 *p = struct_end;
3361out:
3362 return ret;
3363
3364e_inval:
3365 ret = -EINVAL;
3366 goto out;
3367}
3368
3369static int ceph_redirect_decode(void **p, void *end,
3370 struct ceph_request_redirect *redir)
3371{
3372 u8 struct_v, struct_cv;
3373 u32 len;
3374 void *struct_end;
3375 int ret;
3376
3377 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3378 struct_v = ceph_decode_8(p);
3379 struct_cv = ceph_decode_8(p);
3380 if (struct_cv > 1) {
3381 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3382 struct_v, struct_cv);
3383 goto e_inval;
3384 }
3385 len = ceph_decode_32(p);
3386 ceph_decode_need(p, end, len, e_inval);
3387 struct_end = *p + len;
3388
3389 ret = ceph_oloc_decode(p, end, &redir->oloc);
3390 if (ret)
3391 goto out;
3392
3393 len = ceph_decode_32(p);
3394 if (len > 0) {
3395 pr_warn("ceph_request_redirect::object_name is set\n");
3396 goto e_inval;
3397 }
3398
3399 len = ceph_decode_32(p);
3400 *p += len; /* skip osd_instructions */
3401
3402 /* skip the rest */
3403 *p = struct_end;
3404out:
3405 return ret;
3406
3407e_inval:
3408 ret = -EINVAL;
3409 goto out;
3410}
3411
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003412struct MOSDOpReply {
3413 struct ceph_pg pgid;
3414 u64 flags;
3415 int result;
3416 u32 epoch;
3417 int num_ops;
3418 u32 outdata_len[CEPH_OSD_MAX_OPS];
3419 s32 rval[CEPH_OSD_MAX_OPS];
3420 int retry_attempt;
3421 struct ceph_eversion replay_version;
3422 u64 user_version;
3423 struct ceph_request_redirect redirect;
3424};
Sage Weil25845472011-06-03 09:37:09 -07003425
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003426static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
Sage Weilf24e9982009-10-06 11:31:10 -07003427{
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003428 void *p = msg->front.iov_base;
3429 void *const end = p + msg->front.iov_len;
3430 u16 version = le16_to_cpu(msg->hdr.version);
3431 struct ceph_eversion bad_replay_version;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003432 u8 decode_redir;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003433 u32 len;
3434 int ret;
3435 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07003436
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003437 ceph_decode_32_safe(&p, end, len, e_inval);
3438 ceph_decode_need(&p, end, len, e_inval);
3439 p += len; /* skip oid */
Sage Weil1b83bef2013-02-25 16:11:12 -08003440
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003441 ret = ceph_decode_pgid(&p, end, &m->pgid);
3442 if (ret)
3443 return ret;
Sage Weil1b83bef2013-02-25 16:11:12 -08003444
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003445 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3446 ceph_decode_32_safe(&p, end, m->result, e_inval);
3447 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3448 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3449 p += sizeof(bad_replay_version);
3450 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
Sage Weil1b83bef2013-02-25 16:11:12 -08003451
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003452 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3453 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3454 goto e_inval;
Sage Weil1b83bef2013-02-25 16:11:12 -08003455
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003456 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3457 e_inval);
3458 for (i = 0; i < m->num_ops; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -08003459 struct ceph_osd_op *op = p;
Sage Weil1b83bef2013-02-25 16:11:12 -08003460
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003461 m->outdata_len[i] = le32_to_cpu(op->payload_len);
Sage Weil1b83bef2013-02-25 16:11:12 -08003462 p += sizeof(*op);
3463 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003464
3465 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3466 for (i = 0; i < m->num_ops; i++)
3467 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3468
3469 if (version >= 5) {
3470 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3471 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3472 p += sizeof(m->replay_version);
3473 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3474 } else {
3475 m->replay_version = bad_replay_version; /* struct */
3476 m->user_version = le64_to_cpu(m->replay_version.version);
Sage Weil1b83bef2013-02-25 16:11:12 -08003477 }
3478
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003479 if (version >= 6) {
3480 if (version >= 7)
3481 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003482 else
3483 decode_redir = 1;
3484 } else {
3485 decode_redir = 0;
3486 }
3487
3488 if (decode_redir) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003489 ret = ceph_redirect_decode(&p, end, &m->redirect);
3490 if (ret)
3491 return ret;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003492 } else {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003493 ceph_oloc_init(&m->redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003494 }
3495
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003496 return 0;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003497
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003498e_inval:
3499 return -EINVAL;
3500}
3501
3502/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003503 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3504 * specified.
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003505 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003506static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003507{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003508 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003509 struct ceph_osd_request *req;
3510 struct MOSDOpReply m;
3511 u64 tid = le64_to_cpu(msg->hdr.tid);
3512 u32 data_len = 0;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003513 int ret;
3514 int i;
3515
3516 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3517
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003518 down_read(&osdc->lock);
3519 if (!osd_registered(osd)) {
3520 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3521 goto out_unlock_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003522 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003523 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3524
3525 mutex_lock(&osd->lock);
3526 req = lookup_request(&osd->o_requests, tid);
3527 if (!req) {
3528 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3529 goto out_unlock_session;
3530 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003531
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003532 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003533 ret = decode_MOSDOpReply(msg, &m);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003534 m.redirect.oloc.pool_ns = NULL;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003535 if (ret) {
3536 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3537 req->r_tid, ret);
3538 ceph_msg_dump(msg);
3539 goto fail_request;
3540 }
3541 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3542 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3543 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3544 le64_to_cpu(m.replay_version.version), m.user_version);
3545
3546 if (m.retry_attempt >= 0) {
3547 if (m.retry_attempt != req->r_attempts - 1) {
3548 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3549 req, req->r_tid, m.retry_attempt,
3550 req->r_attempts - 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003551 goto out_unlock_session;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003552 }
3553 } else {
3554 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3555 }
3556
3557 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3558 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3559 m.redirect.oloc.pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003560 unlink_request(osd, req);
3561 mutex_unlock(&osd->lock);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003562
Yan, Zheng30c156d2016-02-14 11:24:31 +08003563 /*
3564 * Not ceph_oloc_copy() - changing pool_ns is not
3565 * supported.
3566 */
3567 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003568 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3569 req->r_tid = 0;
3570 __submit_request(req, false);
3571 goto out_unlock_osdc;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003572 }
3573
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003574 if (m.num_ops != req->r_num_ops) {
3575 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3576 req->r_num_ops, req->r_tid);
3577 goto fail_request;
3578 }
3579 for (i = 0; i < req->r_num_ops; i++) {
3580 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3581 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3582 req->r_ops[i].rval = m.rval[i];
3583 req->r_ops[i].outdata_len = m.outdata_len[i];
3584 data_len += m.outdata_len[i];
3585 }
3586 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3587 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3588 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3589 goto fail_request;
3590 }
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003591 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3592 req, req->r_tid, m.result, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07003593
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003594 /*
3595 * Since we only ever request ONDISK, we should only ever get
3596 * one (type of) reply back.
3597 */
3598 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3599 req->r_result = m.result ?: data_len;
3600 finish_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003601 mutex_unlock(&osd->lock);
3602 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003603
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003604 __complete_request(req);
3605 complete_all(&req->r_completion);
3606 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07003607 return;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003608
3609fail_request:
Ilya Dryomov46092452016-04-28 16:07:27 +02003610 complete_request(req, -EIO);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003611out_unlock_session:
3612 mutex_unlock(&osd->lock);
3613out_unlock_osdc:
3614 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003615}
3616
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003617static void set_pool_was_full(struct ceph_osd_client *osdc)
3618{
3619 struct rb_node *n;
3620
3621 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3622 struct ceph_pg_pool_info *pi =
3623 rb_entry(n, struct ceph_pg_pool_info, node);
3624
3625 pi->was_full = __pool_full(pi);
3626 }
3627}
3628
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003629static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
Sage Weilf24e9982009-10-06 11:31:10 -07003630{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003631 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07003632
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003633 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3634 if (!pi)
3635 return false;
Sage Weilf24e9982009-10-06 11:31:10 -07003636
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003637 return pi->was_full && !__pool_full(pi);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003638}
3639
Ilya Dryomov922dab62016-05-26 01:15:02 +02003640static enum calc_target_result
3641recalc_linger_target(struct ceph_osd_linger_request *lreq)
3642{
3643 struct ceph_osd_client *osdc = lreq->osdc;
3644 enum calc_target_result ct_res;
3645
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003646 ct_res = calc_target(osdc, &lreq->t, NULL, true);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003647 if (ct_res == CALC_TARGET_NEED_RESEND) {
3648 struct ceph_osd *osd;
3649
3650 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3651 if (osd != lreq->osd) {
3652 unlink_linger(lreq->osd, lreq);
3653 link_linger(osd, lreq);
3654 }
3655 }
3656
3657 return ct_res;
3658}
3659
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003660/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003661 * Requeue requests whose mapping to an OSD has changed.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003662 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003663static void scan_requests(struct ceph_osd *osd,
3664 bool force_resend,
3665 bool cleared_full,
3666 bool check_pool_cleared_full,
3667 struct rb_root *need_resend,
3668 struct list_head *need_resend_linger)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003669{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003670 struct ceph_osd_client *osdc = osd->o_osdc;
3671 struct rb_node *n;
3672 bool force_resend_writes;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003673
Ilya Dryomov922dab62016-05-26 01:15:02 +02003674 for (n = rb_first(&osd->o_linger_requests); n; ) {
3675 struct ceph_osd_linger_request *lreq =
3676 rb_entry(n, struct ceph_osd_linger_request, node);
3677 enum calc_target_result ct_res;
3678
3679 n = rb_next(n); /* recalc_linger_target() */
3680
3681 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3682 lreq->linger_id);
3683 ct_res = recalc_linger_target(lreq);
3684 switch (ct_res) {
3685 case CALC_TARGET_NO_ACTION:
3686 force_resend_writes = cleared_full ||
3687 (check_pool_cleared_full &&
3688 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3689 if (!force_resend && !force_resend_writes)
3690 break;
3691
3692 /* fall through */
3693 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003694 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003695 /*
3696 * scan_requests() for the previous epoch(s)
3697 * may have already added it to the list, since
3698 * it's not unlinked here.
3699 */
3700 if (list_empty(&lreq->scan_item))
3701 list_add_tail(&lreq->scan_item, need_resend_linger);
3702 break;
3703 case CALC_TARGET_POOL_DNE:
Ilya Dryomova10bcb12017-06-15 16:30:55 +02003704 list_del_init(&lreq->scan_item);
Ilya Dryomov46092452016-04-28 16:07:27 +02003705 check_linger_pool_dne(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003706 break;
3707 }
3708 }
3709
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003710 for (n = rb_first(&osd->o_requests); n; ) {
3711 struct ceph_osd_request *req =
3712 rb_entry(n, struct ceph_osd_request, r_node);
3713 enum calc_target_result ct_res;
Alex Elderab60b162012-12-19 15:52:36 -06003714
Ilya Dryomov46092452016-04-28 16:07:27 +02003715 n = rb_next(n); /* unlink_request(), check_pool_dne() */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003716
3717 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003718 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3719 false);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003720 switch (ct_res) {
3721 case CALC_TARGET_NO_ACTION:
3722 force_resend_writes = cleared_full ||
3723 (check_pool_cleared_full &&
3724 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3725 if (!force_resend &&
3726 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3727 !force_resend_writes))
3728 break;
3729
3730 /* fall through */
3731 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003732 cancel_map_check(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003733 unlink_request(osd, req);
3734 insert_request(need_resend, req);
3735 break;
3736 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003737 check_pool_dne(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003738 break;
Alex Elderab60b162012-12-19 15:52:36 -06003739 }
Sage Weilf24e9982009-10-06 11:31:10 -07003740 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003741}
Sage Weil6f6c7002011-01-17 20:34:08 -08003742
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003743static int handle_one_map(struct ceph_osd_client *osdc,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003744 void *p, void *end, bool incremental,
3745 struct rb_root *need_resend,
3746 struct list_head *need_resend_linger)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003747{
3748 struct ceph_osdmap *newmap;
3749 struct rb_node *n;
3750 bool skipped_map = false;
3751 bool was_full;
3752
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003753 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003754 set_pool_was_full(osdc);
3755
3756 if (incremental)
3757 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3758 else
3759 newmap = ceph_osdmap_decode(&p, end);
3760 if (IS_ERR(newmap))
3761 return PTR_ERR(newmap);
3762
3763 if (newmap != osdc->osdmap) {
3764 /*
3765 * Preserve ->was_full before destroying the old map.
3766 * For pools that weren't in the old map, ->was_full
3767 * should be false.
3768 */
3769 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3770 struct ceph_pg_pool_info *pi =
3771 rb_entry(n, struct ceph_pg_pool_info, node);
3772 struct ceph_pg_pool_info *old_pi;
3773
3774 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3775 if (old_pi)
3776 pi->was_full = old_pi->was_full;
3777 else
3778 WARN_ON(pi->was_full);
3779 }
3780
3781 if (osdc->osdmap->epoch &&
3782 osdc->osdmap->epoch + 1 < newmap->epoch) {
3783 WARN_ON(incremental);
3784 skipped_map = true;
3785 }
3786
3787 ceph_osdmap_destroy(osdc->osdmap);
3788 osdc->osdmap = newmap;
3789 }
3790
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003791 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003792 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3793 need_resend, need_resend_linger);
3794
3795 for (n = rb_first(&osdc->osds); n; ) {
3796 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3797
3798 n = rb_next(n); /* close_osd() */
3799
3800 scan_requests(osd, skipped_map, was_full, true, need_resend,
3801 need_resend_linger);
3802 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3803 memcmp(&osd->o_con.peer_addr,
3804 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3805 sizeof(struct ceph_entity_addr)))
3806 close_osd(osd);
3807 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003808
3809 return 0;
3810}
Sage Weil6f6c7002011-01-17 20:34:08 -08003811
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003812static void kick_requests(struct ceph_osd_client *osdc,
3813 struct rb_root *need_resend,
3814 struct list_head *need_resend_linger)
3815{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003816 struct ceph_osd_linger_request *lreq, *nlreq;
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003817 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003818 struct rb_node *n;
3819
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003820 /* make sure need_resend targets reflect latest map */
3821 for (n = rb_first(need_resend); n; ) {
3822 struct ceph_osd_request *req =
3823 rb_entry(n, struct ceph_osd_request, r_node);
3824
3825 n = rb_next(n);
3826
3827 if (req->r_t.epoch < osdc->osdmap->epoch) {
3828 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3829 if (ct_res == CALC_TARGET_POOL_DNE) {
3830 erase_request(need_resend, req);
3831 check_pool_dne(req);
3832 }
3833 }
3834 }
3835
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003836 for (n = rb_first(need_resend); n; ) {
3837 struct ceph_osd_request *req =
3838 rb_entry(n, struct ceph_osd_request, r_node);
3839 struct ceph_osd *osd;
3840
3841 n = rb_next(n);
3842 erase_request(need_resend, req); /* before link_request() */
3843
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003844 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3845 link_request(osd, req);
3846 if (!req->r_linger) {
3847 if (!osd_homeless(osd) && !req->r_t.paused)
3848 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003849 } else {
3850 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003851 }
3852 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003853
3854 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3855 if (!osd_homeless(lreq->osd))
3856 send_linger(lreq);
3857
3858 list_del_init(&lreq->scan_item);
3859 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003860}
3861
Sage Weilf24e9982009-10-06 11:31:10 -07003862/*
3863 * Process updated osd map.
3864 *
3865 * The message contains any number of incremental and full maps, normally
3866 * indicating some sort of topology change in the cluster. Kick requests
3867 * off to different OSDs as needed.
3868 */
3869void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3870{
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003871 void *p = msg->front.iov_base;
3872 void *const end = p + msg->front.iov_len;
Sage Weilf24e9982009-10-06 11:31:10 -07003873 u32 nr_maps, maplen;
3874 u32 epoch;
Sage Weilf24e9982009-10-06 11:31:10 -07003875 struct ceph_fsid fsid;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003876 struct rb_root need_resend = RB_ROOT;
3877 LIST_HEAD(need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003878 bool handled_incremental = false;
3879 bool was_pauserd, was_pausewr;
3880 bool pauserd, pausewr;
3881 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07003882
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003883 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003884 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003885
3886 /* verify fsid */
3887 ceph_decode_need(&p, end, sizeof(fsid), bad);
3888 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08003889 if (ceph_check_fsid(osdc->client, &fsid) < 0)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003890 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003891
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003892 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3893 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3894 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003895 have_pool_full(osdc);
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08003896
Sage Weilf24e9982009-10-06 11:31:10 -07003897 /* incremental maps */
3898 ceph_decode_32_safe(&p, end, nr_maps, bad);
3899 dout(" %d inc maps\n", nr_maps);
3900 while (nr_maps > 0) {
3901 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003902 epoch = ceph_decode_32(&p);
3903 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003904 ceph_decode_need(&p, end, maplen, bad);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003905 if (osdc->osdmap->epoch &&
3906 osdc->osdmap->epoch + 1 == epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003907 dout("applying incremental map %u len %d\n",
3908 epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003909 err = handle_one_map(osdc, p, p + maplen, true,
3910 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003911 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003912 goto bad;
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003913 handled_incremental = true;
Sage Weilf24e9982009-10-06 11:31:10 -07003914 } else {
3915 dout("ignoring incremental map %u len %d\n",
3916 epoch, maplen);
3917 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003918 p += maplen;
Sage Weilf24e9982009-10-06 11:31:10 -07003919 nr_maps--;
3920 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003921 if (handled_incremental)
Sage Weilf24e9982009-10-06 11:31:10 -07003922 goto done;
3923
3924 /* full maps */
3925 ceph_decode_32_safe(&p, end, nr_maps, bad);
3926 dout(" %d full maps\n", nr_maps);
3927 while (nr_maps) {
3928 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003929 epoch = ceph_decode_32(&p);
3930 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003931 ceph_decode_need(&p, end, maplen, bad);
3932 if (nr_maps > 1) {
3933 dout("skipping non-latest full map %u len %d\n",
3934 epoch, maplen);
Ilya Dryomove5253a72016-04-28 16:07:25 +02003935 } else if (osdc->osdmap->epoch >= epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003936 dout("skipping full map %u len %d, "
3937 "older than our %u\n", epoch, maplen,
3938 osdc->osdmap->epoch);
3939 } else {
3940 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003941 err = handle_one_map(osdc, p, p + maplen, false,
3942 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003943 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003944 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003945 }
3946 p += maplen;
3947 nr_maps--;
3948 }
3949
3950done:
Sage Weilcd634fb2011-05-12 09:29:18 -07003951 /*
3952 * subscribe to subsequent osdmap updates if full to ensure
3953 * we find out when we are no longer full and stop returning
3954 * ENOSPC.
3955 */
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003956 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3957 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3958 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003959 have_pool_full(osdc);
Jeff Layton58eb7932017-04-18 09:21:16 -04003960 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3961 osdc->osdmap->epoch < osdc->epoch_barrier)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003962 maybe_request_map(osdc);
Sage Weilcd634fb2011-05-12 09:29:18 -07003963
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003964 kick_requests(osdc, &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003965
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04003966 ceph_osdc_abort_on_full(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003967 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3968 osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003969 up_write(&osdc->lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07003970 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07003971 return;
3972
3973bad:
3974 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003975 ceph_msg_dump(msg);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003976 up_write(&osdc->lock);
3977}
3978
3979/*
3980 * Resubmit requests pending on the given osd.
3981 */
3982static void kick_osd_requests(struct ceph_osd *osd)
3983{
3984 struct rb_node *n;
3985
Ilya Dryomova02a9462017-06-19 12:18:05 +02003986 clear_backoffs(osd);
3987
Ilya Dryomov922dab62016-05-26 01:15:02 +02003988 for (n = rb_first(&osd->o_requests); n; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003989 struct ceph_osd_request *req =
3990 rb_entry(n, struct ceph_osd_request, r_node);
3991
Ilya Dryomov922dab62016-05-26 01:15:02 +02003992 n = rb_next(n); /* cancel_linger_request() */
3993
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003994 if (!req->r_linger) {
3995 if (!req->r_t.paused)
3996 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003997 } else {
3998 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003999 }
4000 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004001 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4002 struct ceph_osd_linger_request *lreq =
4003 rb_entry(n, struct ceph_osd_linger_request, node);
4004
4005 send_linger(lreq);
4006 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004007}
4008
4009/*
4010 * If the osd connection drops, we need to resubmit all requests.
4011 */
4012static void osd_fault(struct ceph_connection *con)
4013{
4014 struct ceph_osd *osd = con->private;
4015 struct ceph_osd_client *osdc = osd->o_osdc;
4016
4017 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4018
4019 down_write(&osdc->lock);
4020 if (!osd_registered(osd)) {
4021 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4022 goto out_unlock;
4023 }
4024
4025 if (!reopen_osd(osd))
4026 kick_osd_requests(osd);
4027 maybe_request_map(osdc);
4028
4029out_unlock:
4030 up_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004031}
4032
Ilya Dryomova02a9462017-06-19 12:18:05 +02004033struct MOSDBackoff {
4034 struct ceph_spg spgid;
4035 u32 map_epoch;
4036 u8 op;
4037 u64 id;
4038 struct ceph_hobject_id *begin;
4039 struct ceph_hobject_id *end;
4040};
4041
4042static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4043{
4044 void *p = msg->front.iov_base;
4045 void *const end = p + msg->front.iov_len;
4046 u8 struct_v;
4047 u32 struct_len;
4048 int ret;
4049
4050 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4051 if (ret)
4052 return ret;
4053
4054 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4055 if (ret)
4056 return ret;
4057
4058 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4059 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4060 ceph_decode_8_safe(&p, end, m->op, e_inval);
4061 ceph_decode_64_safe(&p, end, m->id, e_inval);
4062
4063 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4064 if (!m->begin)
4065 return -ENOMEM;
4066
4067 ret = decode_hoid(&p, end, m->begin);
4068 if (ret) {
4069 free_hoid(m->begin);
4070 return ret;
4071 }
4072
4073 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4074 if (!m->end) {
4075 free_hoid(m->begin);
4076 return -ENOMEM;
4077 }
4078
4079 ret = decode_hoid(&p, end, m->end);
4080 if (ret) {
4081 free_hoid(m->begin);
4082 free_hoid(m->end);
4083 return ret;
4084 }
4085
4086 return 0;
4087
4088e_inval:
4089 return -EINVAL;
4090}
4091
4092static struct ceph_msg *create_backoff_message(
4093 const struct ceph_osd_backoff *backoff,
4094 u32 map_epoch)
4095{
4096 struct ceph_msg *msg;
4097 void *p, *end;
4098 int msg_size;
4099
4100 msg_size = CEPH_ENCODING_START_BLK_LEN +
4101 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4102 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4103 msg_size += CEPH_ENCODING_START_BLK_LEN +
4104 hoid_encoding_size(backoff->begin);
4105 msg_size += CEPH_ENCODING_START_BLK_LEN +
4106 hoid_encoding_size(backoff->end);
4107
4108 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4109 if (!msg)
4110 return NULL;
4111
4112 p = msg->front.iov_base;
4113 end = p + msg->front_alloc_len;
4114
4115 encode_spgid(&p, &backoff->spgid);
4116 ceph_encode_32(&p, map_epoch);
4117 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4118 ceph_encode_64(&p, backoff->id);
4119 encode_hoid(&p, end, backoff->begin);
4120 encode_hoid(&p, end, backoff->end);
4121 BUG_ON(p != end);
4122
4123 msg->front.iov_len = p - msg->front.iov_base;
4124 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4125 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4126
4127 return msg;
4128}
4129
4130static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4131{
4132 struct ceph_spg_mapping *spg;
4133 struct ceph_osd_backoff *backoff;
4134 struct ceph_msg *msg;
4135
4136 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4137 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4138
4139 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4140 if (!spg) {
4141 spg = alloc_spg_mapping();
4142 if (!spg) {
4143 pr_err("%s failed to allocate spg\n", __func__);
4144 return;
4145 }
4146 spg->spgid = m->spgid; /* struct */
4147 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4148 }
4149
4150 backoff = alloc_backoff();
4151 if (!backoff) {
4152 pr_err("%s failed to allocate backoff\n", __func__);
4153 return;
4154 }
4155 backoff->spgid = m->spgid; /* struct */
4156 backoff->id = m->id;
4157 backoff->begin = m->begin;
4158 m->begin = NULL; /* backoff now owns this */
4159 backoff->end = m->end;
4160 m->end = NULL; /* ditto */
4161
4162 insert_backoff(&spg->backoffs, backoff);
4163 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4164
4165 /*
4166 * Ack with original backoff's epoch so that the OSD can
4167 * discard this if there was a PG split.
4168 */
4169 msg = create_backoff_message(backoff, m->map_epoch);
4170 if (!msg) {
4171 pr_err("%s failed to allocate msg\n", __func__);
4172 return;
4173 }
4174 ceph_con_send(&osd->o_con, msg);
4175}
4176
4177static bool target_contained_by(const struct ceph_osd_request_target *t,
4178 const struct ceph_hobject_id *begin,
4179 const struct ceph_hobject_id *end)
4180{
4181 struct ceph_hobject_id hoid;
4182 int cmp;
4183
4184 hoid_fill_from_target(&hoid, t);
4185 cmp = hoid_compare(&hoid, begin);
4186 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4187}
4188
4189static void handle_backoff_unblock(struct ceph_osd *osd,
4190 const struct MOSDBackoff *m)
4191{
4192 struct ceph_spg_mapping *spg;
4193 struct ceph_osd_backoff *backoff;
4194 struct rb_node *n;
4195
4196 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4197 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4198
4199 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4200 if (!backoff) {
4201 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4202 __func__, osd->o_osd, m->spgid.pgid.pool,
4203 m->spgid.pgid.seed, m->spgid.shard, m->id);
4204 return;
4205 }
4206
4207 if (hoid_compare(backoff->begin, m->begin) &&
4208 hoid_compare(backoff->end, m->end)) {
4209 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4210 __func__, osd->o_osd, m->spgid.pgid.pool,
4211 m->spgid.pgid.seed, m->spgid.shard, m->id);
4212 /* unblock it anyway... */
4213 }
4214
4215 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4216 BUG_ON(!spg);
4217
4218 erase_backoff(&spg->backoffs, backoff);
4219 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4220 free_backoff(backoff);
4221
4222 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4223 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4224 free_spg_mapping(spg);
4225 }
4226
4227 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4228 struct ceph_osd_request *req =
4229 rb_entry(n, struct ceph_osd_request, r_node);
4230
4231 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4232 /*
4233 * Match against @m, not @backoff -- the PG may
4234 * have split on the OSD.
4235 */
4236 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4237 /*
4238 * If no other installed backoff applies,
4239 * resend.
4240 */
4241 send_request(req);
4242 }
4243 }
4244 }
4245}
4246
4247static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4248{
4249 struct ceph_osd_client *osdc = osd->o_osdc;
4250 struct MOSDBackoff m;
4251 int ret;
4252
4253 down_read(&osdc->lock);
4254 if (!osd_registered(osd)) {
4255 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4256 up_read(&osdc->lock);
4257 return;
4258 }
4259 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4260
4261 mutex_lock(&osd->lock);
4262 ret = decode_MOSDBackoff(msg, &m);
4263 if (ret) {
4264 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4265 ceph_msg_dump(msg);
4266 goto out_unlock;
4267 }
4268
4269 switch (m.op) {
4270 case CEPH_OSD_BACKOFF_OP_BLOCK:
4271 handle_backoff_block(osd, &m);
4272 break;
4273 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4274 handle_backoff_unblock(osd, &m);
4275 break;
4276 default:
4277 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4278 }
4279
4280 free_hoid(m.begin);
4281 free_hoid(m.end);
4282
4283out_unlock:
4284 mutex_unlock(&osd->lock);
4285 up_read(&osdc->lock);
4286}
4287
Sage Weilf24e9982009-10-06 11:31:10 -07004288/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004289 * Process osd watch notifications
4290 */
Alex Elder3c663bb2013-02-15 11:42:30 -06004291static void handle_watch_notify(struct ceph_osd_client *osdc,
4292 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004293{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004294 void *p = msg->front.iov_base;
4295 void *const end = p + msg->front.iov_len;
4296 struct ceph_osd_linger_request *lreq;
4297 struct linger_work *lwork;
4298 u8 proto_ver, opcode;
4299 u64 cookie, notify_id;
4300 u64 notifier_id = 0;
Ilya Dryomov19079202016-04-28 16:07:27 +02004301 s32 return_code = 0;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004302 void *payload = NULL;
4303 u32 payload_len = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004304
4305 ceph_decode_8_safe(&p, end, proto_ver, bad);
4306 ceph_decode_8_safe(&p, end, opcode, bad);
4307 ceph_decode_64_safe(&p, end, cookie, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004308 p += 8; /* skip ver */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004309 ceph_decode_64_safe(&p, end, notify_id, bad);
4310
Ilya Dryomov922dab62016-05-26 01:15:02 +02004311 if (proto_ver >= 1) {
4312 ceph_decode_32_safe(&p, end, payload_len, bad);
4313 ceph_decode_need(&p, end, payload_len, bad);
4314 payload = p;
4315 p += payload_len;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004316 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004317
4318 if (le16_to_cpu(msg->hdr.version) >= 2)
Ilya Dryomov19079202016-04-28 16:07:27 +02004319 ceph_decode_32_safe(&p, end, return_code, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004320
4321 if (le16_to_cpu(msg->hdr.version) >= 3)
4322 ceph_decode_64_safe(&p, end, notifier_id, bad);
4323
4324 down_read(&osdc->lock);
4325 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4326 if (!lreq) {
4327 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4328 cookie);
4329 goto out_unlock_osdc;
4330 }
4331
4332 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02004333 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4334 opcode, cookie, lreq, lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004335 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4336 if (!lreq->last_error) {
4337 lreq->last_error = -ENOTCONN;
4338 queue_watch_error(lreq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004339 }
Ilya Dryomov19079202016-04-28 16:07:27 +02004340 } else if (!lreq->is_watch) {
4341 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4342 if (lreq->notify_id && lreq->notify_id != notify_id) {
4343 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4344 lreq->notify_id, notify_id);
4345 } else if (!completion_done(&lreq->notify_finish_wait)) {
4346 struct ceph_msg_data *data =
4347 list_first_entry_or_null(&msg->data,
4348 struct ceph_msg_data,
4349 links);
4350
4351 if (data) {
4352 if (lreq->preply_pages) {
4353 WARN_ON(data->type !=
4354 CEPH_MSG_DATA_PAGES);
4355 *lreq->preply_pages = data->pages;
4356 *lreq->preply_len = data->length;
4357 } else {
4358 ceph_release_page_vector(data->pages,
4359 calc_pages_for(0, data->length));
4360 }
4361 }
4362 lreq->notify_finish_error = return_code;
4363 complete_all(&lreq->notify_finish_wait);
4364 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004365 } else {
4366 /* CEPH_WATCH_EVENT_NOTIFY */
4367 lwork = lwork_alloc(lreq, do_watch_notify);
4368 if (!lwork) {
4369 pr_err("failed to allocate notify-lwork\n");
4370 goto out_unlock_lreq;
4371 }
Ilya Dryomov91883cd2014-09-11 12:18:53 +04004372
Ilya Dryomov922dab62016-05-26 01:15:02 +02004373 lwork->notify.notify_id = notify_id;
4374 lwork->notify.notifier_id = notifier_id;
4375 lwork->notify.payload = payload;
4376 lwork->notify.payload_len = payload_len;
4377 lwork->notify.msg = ceph_msg_get(msg);
4378 lwork_queue(lwork);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004379 }
4380
Ilya Dryomov922dab62016-05-26 01:15:02 +02004381out_unlock_lreq:
4382 mutex_unlock(&lreq->lock);
4383out_unlock_osdc:
4384 up_read(&osdc->lock);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004385 return;
4386
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004387bad:
4388 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004389}
4390
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004391/*
Sage Weilf24e9982009-10-06 11:31:10 -07004392 * Register request, send initial attempt.
4393 */
4394int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4395 struct ceph_osd_request *req,
4396 bool nofail)
4397{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004398 down_read(&osdc->lock);
4399 submit_request(req, false);
4400 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004401
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004402 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07004403}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004404EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07004405
4406/*
Ilya Dryomovc297eb422016-12-02 14:01:55 +01004407 * Unregister a registered request. The request is not completed:
4408 * ->r_result isn't set and __complete_request() isn't called.
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04004409 */
4410void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4411{
4412 struct ceph_osd_client *osdc = req->r_osdc;
4413
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004414 down_write(&osdc->lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004415 if (req->r_osd)
4416 cancel_request(req);
4417 up_write(&osdc->lock);
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04004418}
4419EXPORT_SYMBOL(ceph_osdc_cancel_request);
4420
4421/*
Ilya Dryomov42b06962016-04-28 16:07:26 +02004422 * @timeout: in jiffies, 0 means "wait forever"
4423 */
4424static int wait_request_timeout(struct ceph_osd_request *req,
4425 unsigned long timeout)
4426{
4427 long left;
4428
4429 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Yan, Zheng0e76abf2016-05-13 11:04:33 +08004430 left = wait_for_completion_killable_timeout(&req->r_completion,
Ilya Dryomov42b06962016-04-28 16:07:26 +02004431 ceph_timeout_jiffies(timeout));
4432 if (left <= 0) {
4433 left = left ?: -ETIMEDOUT;
4434 ceph_osdc_cancel_request(req);
Ilya Dryomov42b06962016-04-28 16:07:26 +02004435 } else {
4436 left = req->r_result; /* completed */
4437 }
4438
4439 return left;
4440}
4441
4442/*
Sage Weilf24e9982009-10-06 11:31:10 -07004443 * wait for a request to complete
4444 */
4445int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4446 struct ceph_osd_request *req)
4447{
Ilya Dryomov42b06962016-04-28 16:07:26 +02004448 return wait_request_timeout(req, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07004449}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004450EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07004451
4452/*
4453 * sync - wait for all in-flight requests to flush. avoid starvation.
4454 */
4455void ceph_osdc_sync(struct ceph_osd_client *osdc)
4456{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004457 struct rb_node *n, *p;
4458 u64 last_tid = atomic64_read(&osdc->last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004459
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004460again:
4461 down_read(&osdc->lock);
4462 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4463 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07004464
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004465 mutex_lock(&osd->lock);
4466 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4467 struct ceph_osd_request *req =
4468 rb_entry(p, struct ceph_osd_request, r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07004469
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004470 if (req->r_tid > last_tid)
4471 break;
4472
4473 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4474 continue;
4475
4476 ceph_osdc_get_request(req);
4477 mutex_unlock(&osd->lock);
4478 up_read(&osdc->lock);
4479 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4480 __func__, req, req->r_tid, last_tid);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01004481 wait_for_completion(&req->r_completion);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004482 ceph_osdc_put_request(req);
4483 goto again;
4484 }
4485
4486 mutex_unlock(&osd->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004487 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004488
4489 up_read(&osdc->lock);
4490 dout("%s done last_tid %llu\n", __func__, last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004491}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004492EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07004493
Ilya Dryomov922dab62016-05-26 01:15:02 +02004494static struct ceph_osd_request *
4495alloc_linger_request(struct ceph_osd_linger_request *lreq)
4496{
4497 struct ceph_osd_request *req;
4498
4499 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4500 if (!req)
4501 return NULL;
4502
4503 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4504 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4505
4506 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4507 ceph_osdc_put_request(req);
4508 return NULL;
4509 }
4510
4511 return req;
4512}
4513
4514/*
4515 * Returns a handle, caller owns a ref.
4516 */
4517struct ceph_osd_linger_request *
4518ceph_osdc_watch(struct ceph_osd_client *osdc,
4519 struct ceph_object_id *oid,
4520 struct ceph_object_locator *oloc,
4521 rados_watchcb2_t wcb,
4522 rados_watcherrcb_t errcb,
4523 void *data)
4524{
4525 struct ceph_osd_linger_request *lreq;
4526 int ret;
4527
4528 lreq = linger_alloc(osdc);
4529 if (!lreq)
4530 return ERR_PTR(-ENOMEM);
4531
Ilya Dryomov19079202016-04-28 16:07:27 +02004532 lreq->is_watch = true;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004533 lreq->wcb = wcb;
4534 lreq->errcb = errcb;
4535 lreq->data = data;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004536 lreq->watch_valid_thru = jiffies;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004537
4538 ceph_oid_copy(&lreq->t.base_oid, oid);
4539 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004540 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07004541 ktime_get_real_ts(&lreq->mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004542
4543 lreq->reg_req = alloc_linger_request(lreq);
4544 if (!lreq->reg_req) {
4545 ret = -ENOMEM;
4546 goto err_put_lreq;
4547 }
4548
4549 lreq->ping_req = alloc_linger_request(lreq);
4550 if (!lreq->ping_req) {
4551 ret = -ENOMEM;
4552 goto err_put_lreq;
4553 }
4554
4555 down_write(&osdc->lock);
4556 linger_register(lreq); /* before osd_req_op_* */
4557 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4558 CEPH_OSD_WATCH_OP_WATCH);
4559 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4560 CEPH_OSD_WATCH_OP_PING);
4561 linger_submit(lreq);
4562 up_write(&osdc->lock);
4563
4564 ret = linger_reg_commit_wait(lreq);
4565 if (ret) {
4566 linger_cancel(lreq);
4567 goto err_put_lreq;
4568 }
4569
4570 return lreq;
4571
4572err_put_lreq:
4573 linger_put(lreq);
4574 return ERR_PTR(ret);
4575}
4576EXPORT_SYMBOL(ceph_osdc_watch);
4577
4578/*
4579 * Releases a ref.
4580 *
4581 * Times out after mount_timeout to preserve rbd unmap behaviour
4582 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4583 * with mount_timeout").
4584 */
4585int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4586 struct ceph_osd_linger_request *lreq)
4587{
4588 struct ceph_options *opts = osdc->client->options;
4589 struct ceph_osd_request *req;
4590 int ret;
4591
4592 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4593 if (!req)
4594 return -ENOMEM;
4595
4596 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4597 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004598 req->r_flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07004599 ktime_get_real_ts(&req->r_mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004600 osd_req_op_watch_init(req, 0, lreq->linger_id,
4601 CEPH_OSD_WATCH_OP_UNWATCH);
4602
4603 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4604 if (ret)
4605 goto out_put_req;
4606
4607 ceph_osdc_start_request(osdc, req, false);
4608 linger_cancel(lreq);
4609 linger_put(lreq);
4610 ret = wait_request_timeout(req, opts->mount_timeout);
4611
4612out_put_req:
4613 ceph_osdc_put_request(req);
4614 return ret;
4615}
4616EXPORT_SYMBOL(ceph_osdc_unwatch);
4617
4618static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4619 u64 notify_id, u64 cookie, void *payload,
4620 size_t payload_len)
4621{
4622 struct ceph_osd_req_op *op;
4623 struct ceph_pagelist *pl;
4624 int ret;
4625
4626 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4627
4628 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4629 if (!pl)
4630 return -ENOMEM;
4631
4632 ceph_pagelist_init(pl);
4633 ret = ceph_pagelist_encode_64(pl, notify_id);
4634 ret |= ceph_pagelist_encode_64(pl, cookie);
4635 if (payload) {
4636 ret |= ceph_pagelist_encode_32(pl, payload_len);
4637 ret |= ceph_pagelist_append(pl, payload, payload_len);
4638 } else {
4639 ret |= ceph_pagelist_encode_32(pl, 0);
4640 }
4641 if (ret) {
4642 ceph_pagelist_release(pl);
4643 return -ENOMEM;
4644 }
4645
4646 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4647 op->indata_len = pl->length;
4648 return 0;
4649}
4650
4651int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4652 struct ceph_object_id *oid,
4653 struct ceph_object_locator *oloc,
4654 u64 notify_id,
4655 u64 cookie,
4656 void *payload,
4657 size_t payload_len)
4658{
4659 struct ceph_osd_request *req;
4660 int ret;
4661
4662 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4663 if (!req)
4664 return -ENOMEM;
4665
4666 ceph_oid_copy(&req->r_base_oid, oid);
4667 ceph_oloc_copy(&req->r_base_oloc, oloc);
4668 req->r_flags = CEPH_OSD_FLAG_READ;
4669
4670 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4671 if (ret)
4672 goto out_put_req;
4673
4674 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4675 payload_len);
4676 if (ret)
4677 goto out_put_req;
4678
4679 ceph_osdc_start_request(osdc, req, false);
4680 ret = ceph_osdc_wait_request(osdc, req);
4681
4682out_put_req:
4683 ceph_osdc_put_request(req);
4684 return ret;
4685}
4686EXPORT_SYMBOL(ceph_osdc_notify_ack);
4687
Ilya Dryomov19079202016-04-28 16:07:27 +02004688static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4689 u64 cookie, u32 prot_ver, u32 timeout,
4690 void *payload, size_t payload_len)
4691{
4692 struct ceph_osd_req_op *op;
4693 struct ceph_pagelist *pl;
4694 int ret;
4695
4696 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4697 op->notify.cookie = cookie;
4698
4699 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4700 if (!pl)
4701 return -ENOMEM;
4702
4703 ceph_pagelist_init(pl);
4704 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4705 ret |= ceph_pagelist_encode_32(pl, timeout);
4706 ret |= ceph_pagelist_encode_32(pl, payload_len);
4707 ret |= ceph_pagelist_append(pl, payload, payload_len);
4708 if (ret) {
4709 ceph_pagelist_release(pl);
4710 return -ENOMEM;
4711 }
4712
4713 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4714 op->indata_len = pl->length;
4715 return 0;
4716}
4717
4718/*
4719 * @timeout: in seconds
4720 *
4721 * @preply_{pages,len} are initialized both on success and error.
4722 * The caller is responsible for:
4723 *
4724 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4725 */
4726int ceph_osdc_notify(struct ceph_osd_client *osdc,
4727 struct ceph_object_id *oid,
4728 struct ceph_object_locator *oloc,
4729 void *payload,
4730 size_t payload_len,
4731 u32 timeout,
4732 struct page ***preply_pages,
4733 size_t *preply_len)
4734{
4735 struct ceph_osd_linger_request *lreq;
4736 struct page **pages;
4737 int ret;
4738
4739 WARN_ON(!timeout);
4740 if (preply_pages) {
4741 *preply_pages = NULL;
4742 *preply_len = 0;
4743 }
4744
4745 lreq = linger_alloc(osdc);
4746 if (!lreq)
4747 return -ENOMEM;
4748
4749 lreq->preply_pages = preply_pages;
4750 lreq->preply_len = preply_len;
4751
4752 ceph_oid_copy(&lreq->t.base_oid, oid);
4753 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4754 lreq->t.flags = CEPH_OSD_FLAG_READ;
4755
4756 lreq->reg_req = alloc_linger_request(lreq);
4757 if (!lreq->reg_req) {
4758 ret = -ENOMEM;
4759 goto out_put_lreq;
4760 }
4761
4762 /* for notify_id */
4763 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4764 if (IS_ERR(pages)) {
4765 ret = PTR_ERR(pages);
4766 goto out_put_lreq;
4767 }
4768
4769 down_write(&osdc->lock);
4770 linger_register(lreq); /* before osd_req_op_* */
4771 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4772 timeout, payload, payload_len);
4773 if (ret) {
4774 linger_unregister(lreq);
4775 up_write(&osdc->lock);
4776 ceph_release_page_vector(pages, 1);
4777 goto out_put_lreq;
4778 }
4779 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4780 response_data),
4781 pages, PAGE_SIZE, 0, false, true);
4782 linger_submit(lreq);
4783 up_write(&osdc->lock);
4784
4785 ret = linger_reg_commit_wait(lreq);
4786 if (!ret)
4787 ret = linger_notify_finish_wait(lreq);
4788 else
4789 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4790
4791 linger_cancel(lreq);
4792out_put_lreq:
4793 linger_put(lreq);
4794 return ret;
4795}
4796EXPORT_SYMBOL(ceph_osdc_notify);
4797
Sage Weilf24e9982009-10-06 11:31:10 -07004798/*
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004799 * Return the number of milliseconds since the watch was last
4800 * confirmed, or an error. If there is an error, the watch is no
4801 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4802 */
4803int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4804 struct ceph_osd_linger_request *lreq)
4805{
4806 unsigned long stamp, age;
4807 int ret;
4808
4809 down_read(&osdc->lock);
4810 mutex_lock(&lreq->lock);
4811 stamp = lreq->watch_valid_thru;
4812 if (!list_empty(&lreq->pending_lworks)) {
4813 struct linger_work *lwork =
4814 list_first_entry(&lreq->pending_lworks,
4815 struct linger_work,
4816 pending_item);
4817
4818 if (time_before(lwork->queued_stamp, stamp))
4819 stamp = lwork->queued_stamp;
4820 }
4821 age = jiffies - stamp;
4822 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4823 lreq, lreq->linger_id, age, lreq->last_error);
4824 /* we are truncating to msecs, so return a safe upper bound */
4825 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4826
4827 mutex_unlock(&lreq->lock);
4828 up_read(&osdc->lock);
4829 return ret;
4830}
4831
Douglas Fullera4ed38d2015-07-17 13:18:07 -07004832static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4833{
4834 u8 struct_v;
4835 u32 struct_len;
4836 int ret;
4837
4838 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4839 &struct_v, &struct_len);
4840 if (ret)
4841 return ret;
4842
4843 ceph_decode_copy(p, &item->name, sizeof(item->name));
4844 item->cookie = ceph_decode_64(p);
4845 *p += 4; /* skip timeout_seconds */
4846 if (struct_v >= 2) {
4847 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4848 ceph_decode_addr(&item->addr);
4849 }
4850
4851 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4852 ENTITY_NAME(item->name), item->cookie,
4853 ceph_pr_addr(&item->addr.in_addr));
4854 return 0;
4855}
4856
4857static int decode_watchers(void **p, void *end,
4858 struct ceph_watch_item **watchers,
4859 u32 *num_watchers)
4860{
4861 u8 struct_v;
4862 u32 struct_len;
4863 int i;
4864 int ret;
4865
4866 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4867 &struct_v, &struct_len);
4868 if (ret)
4869 return ret;
4870
4871 *num_watchers = ceph_decode_32(p);
4872 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4873 if (!*watchers)
4874 return -ENOMEM;
4875
4876 for (i = 0; i < *num_watchers; i++) {
4877 ret = decode_watcher(p, end, *watchers + i);
4878 if (ret) {
4879 kfree(*watchers);
4880 return ret;
4881 }
4882 }
4883
4884 return 0;
4885}
4886
4887/*
4888 * On success, the caller is responsible for:
4889 *
4890 * kfree(watchers);
4891 */
4892int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4893 struct ceph_object_id *oid,
4894 struct ceph_object_locator *oloc,
4895 struct ceph_watch_item **watchers,
4896 u32 *num_watchers)
4897{
4898 struct ceph_osd_request *req;
4899 struct page **pages;
4900 int ret;
4901
4902 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4903 if (!req)
4904 return -ENOMEM;
4905
4906 ceph_oid_copy(&req->r_base_oid, oid);
4907 ceph_oloc_copy(&req->r_base_oloc, oloc);
4908 req->r_flags = CEPH_OSD_FLAG_READ;
4909
4910 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4911 if (ret)
4912 goto out_put_req;
4913
4914 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4915 if (IS_ERR(pages)) {
4916 ret = PTR_ERR(pages);
4917 goto out_put_req;
4918 }
4919
4920 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4921 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4922 response_data),
4923 pages, PAGE_SIZE, 0, false, true);
4924
4925 ceph_osdc_start_request(osdc, req, false);
4926 ret = ceph_osdc_wait_request(osdc, req);
4927 if (ret >= 0) {
4928 void *p = page_address(pages[0]);
4929 void *const end = p + req->r_ops[0].outdata_len;
4930
4931 ret = decode_watchers(&p, end, watchers, num_watchers);
4932 }
4933
4934out_put_req:
4935 ceph_osdc_put_request(req);
4936 return ret;
4937}
4938EXPORT_SYMBOL(ceph_osdc_list_watchers);
4939
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004940/*
Josh Durgindd935f42013-08-28 21:43:09 -07004941 * Call all pending notify callbacks - for use after a watch is
4942 * unregistered, to make sure no more callbacks for it will be invoked
4943 */
stephen hemmingerf64794492014-06-10 20:30:13 -07004944void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07004945{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004946 dout("%s osdc %p\n", __func__, osdc);
Josh Durgindd935f42013-08-28 21:43:09 -07004947 flush_workqueue(osdc->notify_wq);
4948}
4949EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4950
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02004951void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4952{
4953 down_read(&osdc->lock);
4954 maybe_request_map(osdc);
4955 up_read(&osdc->lock);
4956}
4957EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
Josh Durgindd935f42013-08-28 21:43:09 -07004958
4959/*
Douglas Fuller428a7152015-06-17 14:49:45 -04004960 * Execute an OSD class method on an object.
4961 *
4962 * @flags: CEPH_OSD_FLAG_*
Ilya Dryomov2544a022017-01-25 18:16:21 +01004963 * @resp_len: in/out param for reply length
Douglas Fuller428a7152015-06-17 14:49:45 -04004964 */
4965int ceph_osdc_call(struct ceph_osd_client *osdc,
4966 struct ceph_object_id *oid,
4967 struct ceph_object_locator *oloc,
4968 const char *class, const char *method,
4969 unsigned int flags,
4970 struct page *req_page, size_t req_len,
4971 struct page *resp_page, size_t *resp_len)
4972{
4973 struct ceph_osd_request *req;
4974 int ret;
4975
Ilya Dryomov2544a022017-01-25 18:16:21 +01004976 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4977 return -E2BIG;
4978
Douglas Fuller428a7152015-06-17 14:49:45 -04004979 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4980 if (!req)
4981 return -ENOMEM;
4982
4983 ceph_oid_copy(&req->r_base_oid, oid);
4984 ceph_oloc_copy(&req->r_base_oloc, oloc);
4985 req->r_flags = flags;
4986
4987 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4988 if (ret)
4989 goto out_put_req;
4990
Chengguang Xufe943d52018-04-12 12:04:55 +08004991 ret = osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4992 if (ret)
4993 goto out_put_req;
4994
Douglas Fuller428a7152015-06-17 14:49:45 -04004995 if (req_page)
4996 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4997 0, false, false);
4998 if (resp_page)
4999 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
Ilya Dryomov2544a022017-01-25 18:16:21 +01005000 *resp_len, 0, false, false);
Douglas Fuller428a7152015-06-17 14:49:45 -04005001
5002 ceph_osdc_start_request(osdc, req, false);
5003 ret = ceph_osdc_wait_request(osdc, req);
5004 if (ret >= 0) {
5005 ret = req->r_ops[0].rval;
5006 if (resp_page)
5007 *resp_len = req->r_ops[0].outdata_len;
5008 }
5009
5010out_put_req:
5011 ceph_osdc_put_request(req);
5012 return ret;
5013}
5014EXPORT_SYMBOL(ceph_osdc_call);
5015
5016/*
Sage Weilf24e9982009-10-06 11:31:10 -07005017 * init, shutdown
5018 */
5019int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5020{
5021 int err;
5022
5023 dout("init\n");
5024 osdc->client = client;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005025 init_rwsem(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07005026 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005027 INIT_LIST_HEAD(&osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02005028 spin_lock_init(&osdc->osd_lru_lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005029 osd_init(&osdc->homeless_osd);
5030 osdc->homeless_osd.o_osdc = osdc;
5031 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
Ilya Dryomov264048a2016-11-08 15:15:24 +01005032 osdc->last_linger_id = CEPH_LINGER_ID_START;
Ilya Dryomov922dab62016-05-26 01:15:02 +02005033 osdc->linger_requests = RB_ROOT;
Ilya Dryomov46092452016-04-28 16:07:27 +02005034 osdc->map_checks = RB_ROOT;
5035 osdc->linger_map_checks = RB_ROOT;
Sage Weilf24e9982009-10-06 11:31:10 -07005036 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005037 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5038
Sage Weil5f44f142009-11-18 14:52:18 -08005039 err = -ENOMEM;
Ilya Dryomove5253a72016-04-28 16:07:25 +02005040 osdc->osdmap = ceph_osdmap_alloc();
5041 if (!osdc->osdmap)
5042 goto out;
5043
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01005044 osdc->req_mempool = mempool_create_slab_pool(10,
5045 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07005046 if (!osdc->req_mempool)
Ilya Dryomove5253a72016-04-28 16:07:25 +02005047 goto out_map;
Sage Weilf24e9982009-10-06 11:31:10 -07005048
Sage Weild50b4092012-07-09 14:22:34 -07005049 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02005050 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07005051 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08005052 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07005053 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02005054 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08005055 if (err < 0)
5056 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005057
Dan Carpenterdbcae082013-08-15 08:58:59 +03005058 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005059 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03005060 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02005061 goto out_msgpool_reply;
5062
Ilya Dryomovfbca9632016-04-28 16:07:24 +02005063 schedule_delayed_work(&osdc->timeout_work,
5064 osdc->client->options->osd_keepalive_timeout);
Ilya Dryomovb37ee1b2016-04-28 16:07:24 +02005065 schedule_delayed_work(&osdc->osds_timeout_work,
5066 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5067
Sage Weilf24e9982009-10-06 11:31:10 -07005068 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08005069
Ilya Dryomovc172ec52014-01-31 17:49:22 +02005070out_msgpool_reply:
5071 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08005072out_msgpool:
5073 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08005074out_mempool:
5075 mempool_destroy(osdc->req_mempool);
Ilya Dryomove5253a72016-04-28 16:07:25 +02005076out_map:
5077 ceph_osdmap_destroy(osdc->osdmap);
Sage Weil5f44f142009-11-18 14:52:18 -08005078out:
5079 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07005080}
5081
5082void ceph_osdc_stop(struct ceph_osd_client *osdc)
5083{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005084 flush_workqueue(osdc->notify_wq);
5085 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07005086 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005087 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005088
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005089 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005090 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5091 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5092 struct ceph_osd, o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005093 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005094 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005095 up_write(&osdc->lock);
Elena Reshetova02113a02017-03-17 14:10:28 +02005096 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005097 osd_cleanup(&osdc->homeless_osd);
5098
5099 WARN_ON(!list_empty(&osdc->osd_lru));
Ilya Dryomov922dab62016-05-26 01:15:02 +02005100 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
Ilya Dryomov46092452016-04-28 16:07:27 +02005101 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5102 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005103 WARN_ON(atomic_read(&osdc->num_requests));
5104 WARN_ON(atomic_read(&osdc->num_homeless));
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005105
Ilya Dryomove5253a72016-04-28 16:07:25 +02005106 ceph_osdmap_destroy(osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07005107 mempool_destroy(osdc->req_mempool);
5108 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08005109 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07005110}
5111
5112/*
5113 * Read some contiguous pages. If we cross a stripe boundary, shorten
5114 * *plen. Return number of bytes read, or error.
5115 */
5116int ceph_osdc_readpages(struct ceph_osd_client *osdc,
5117 struct ceph_vino vino, struct ceph_file_layout *layout,
5118 u64 off, u64 *plen,
5119 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08005120 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07005121{
5122 struct ceph_osd_request *req;
5123 int rc = 0;
5124
5125 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
5126 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08005127 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07005128 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05005129 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06005130 false);
Sage Weil68162822012-09-24 21:01:02 -07005131 if (IS_ERR(req))
5132 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07005133
5134 /* it may be a short read due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05005135 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05005136 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07005137
Alex Eldere0c59482013-03-07 15:38:25 -06005138 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05005139 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07005140
5141 rc = ceph_osdc_start_request(osdc, req, false);
5142 if (!rc)
5143 rc = ceph_osdc_wait_request(osdc, req);
5144
5145 ceph_osdc_put_request(req);
5146 dout("readpages result %d\n", rc);
5147 return rc;
5148}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005149EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07005150
5151/*
5152 * do a synchronous write on N pages
5153 */
5154int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5155 struct ceph_file_layout *layout,
5156 struct ceph_snap_context *snapc,
5157 u64 off, u64 len,
5158 u32 truncate_seq, u64 truncate_size,
5159 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06005160 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07005161{
5162 struct ceph_osd_request *req;
5163 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08005164 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07005165
Yan, Zheng715e4cd2014-11-13 14:40:37 +08005166 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01005167 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05005168 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06005169 true);
Sage Weil68162822012-09-24 21:01:02 -07005170 if (IS_ERR(req))
5171 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07005172
5173 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05005174 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05005175 false, false);
5176 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07005177
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02005178 req->r_mtime = *mtime;
Alex Elder87f979d2013-02-15 11:42:29 -06005179 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07005180 if (!rc)
5181 rc = ceph_osdc_wait_request(osdc, req);
5182
5183 ceph_osdc_put_request(req);
5184 if (rc == 0)
5185 rc = len;
5186 dout("writepages result %d\n", rc);
5187 return rc;
5188}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005189EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07005190
Chengguang Xu57a35df2018-03-10 20:32:05 +08005191int __init ceph_osdc_setup(void)
Alex Elder5522ae02013-05-01 12:43:04 -05005192{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01005193 size_t size = sizeof(struct ceph_osd_request) +
5194 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5195
Alex Elder5522ae02013-05-01 12:43:04 -05005196 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01005197 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5198 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05005199
5200 return ceph_osd_request_cache ? 0 : -ENOMEM;
5201}
Alex Elder5522ae02013-05-01 12:43:04 -05005202
5203void ceph_osdc_cleanup(void)
5204{
5205 BUG_ON(!ceph_osd_request_cache);
5206 kmem_cache_destroy(ceph_osd_request_cache);
5207 ceph_osd_request_cache = NULL;
5208}
Alex Elder5522ae02013-05-01 12:43:04 -05005209
Sage Weilf24e9982009-10-06 11:31:10 -07005210/*
5211 * handle incoming message
5212 */
5213static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5214{
5215 struct ceph_osd *osd = con->private;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005216 struct ceph_osd_client *osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07005217 int type = le16_to_cpu(msg->hdr.type);
5218
Sage Weilf24e9982009-10-06 11:31:10 -07005219 switch (type) {
5220 case CEPH_MSG_OSD_MAP:
5221 ceph_osdc_handle_map(osdc, msg);
5222 break;
5223 case CEPH_MSG_OSD_OPREPLY:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005224 handle_reply(osd, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07005225 break;
Ilya Dryomova02a9462017-06-19 12:18:05 +02005226 case CEPH_MSG_OSD_BACKOFF:
5227 handle_backoff(osd, msg);
5228 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005229 case CEPH_MSG_WATCH_NOTIFY:
5230 handle_watch_notify(osdc, msg);
5231 break;
Sage Weilf24e9982009-10-06 11:31:10 -07005232
5233 default:
5234 pr_err("received unknown message type %d %s\n", type,
5235 ceph_msg_type_name(type));
5236 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005237
Sage Weilf24e9982009-10-06 11:31:10 -07005238 ceph_msg_put(msg);
5239}
5240
Sage Weil5b3a4db2010-02-19 21:43:23 -08005241/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005242 * Lookup and return message for incoming reply. Don't try to do
5243 * anything about a larger than preallocated data portion of the
5244 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08005245 */
5246static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08005247 struct ceph_msg_header *hdr,
5248 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07005249{
5250 struct ceph_osd *osd = con->private;
5251 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005252 struct ceph_msg *m = NULL;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005253 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02005254 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005255 int data_len = le32_to_cpu(hdr->data_len);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005256 u64 tid = le64_to_cpu(hdr->tid);
Sage Weilf24e9982009-10-06 11:31:10 -07005257
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005258 down_read(&osdc->lock);
5259 if (!osd_registered(osd)) {
5260 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5261 *skip = 1;
5262 goto out_unlock_osdc;
5263 }
5264 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5265
5266 mutex_lock(&osd->lock);
5267 req = lookup_request(&osd->o_requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005268 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01005269 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5270 osd->o_osd, tid);
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005271 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005272 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005273 }
Sage Weilc16e7862010-03-01 13:02:00 -08005274
Alex Elderace6d3a2013-04-01 16:12:14 -05005275 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08005276
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02005277 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005278 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5279 __func__, osd->o_osd, req->r_tid, front_len,
5280 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02005281 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5282 false);
Sage Weila79832f2010-04-01 16:06:19 -07005283 if (!m)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005284 goto out_unlock_session;
Sage Weilc16e7862010-03-01 13:02:00 -08005285 ceph_msg_put(req->r_reply);
5286 req->r_reply = m;
5287 }
Sage Weilc16e7862010-03-01 13:02:00 -08005288
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005289 if (data_len > req->r_reply->data_length) {
5290 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5291 __func__, osd->o_osd, req->r_tid, data_len,
5292 req->r_reply->data_length);
5293 m = NULL;
5294 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005295 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005296 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005297
5298 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08005299 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005300
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005301out_unlock_session:
5302 mutex_unlock(&osd->lock);
5303out_unlock_osdc:
5304 up_read(&osdc->lock);
Yehuda Sadeh24504182010-01-08 13:58:34 -08005305 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08005306}
5307
Ilya Dryomov19079202016-04-28 16:07:27 +02005308/*
5309 * TODO: switch to a msg-owned pagelist
5310 */
5311static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5312{
5313 struct ceph_msg *m;
5314 int type = le16_to_cpu(hdr->type);
5315 u32 front_len = le32_to_cpu(hdr->front_len);
5316 u32 data_len = le32_to_cpu(hdr->data_len);
5317
5318 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
5319 if (!m)
5320 return NULL;
5321
5322 if (data_len) {
5323 struct page **pages;
5324 struct ceph_osd_data osd_data;
5325
5326 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5327 GFP_NOIO);
Wei Yongjunc22e8532016-07-30 00:37:57 +00005328 if (IS_ERR(pages)) {
Ilya Dryomov19079202016-04-28 16:07:27 +02005329 ceph_msg_put(m);
5330 return NULL;
5331 }
5332
5333 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5334 false);
5335 ceph_osdc_msg_data_add(m, &osd_data);
5336 }
5337
5338 return m;
5339}
5340
Sage Weil5b3a4db2010-02-19 21:43:23 -08005341static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5342 struct ceph_msg_header *hdr,
5343 int *skip)
5344{
5345 struct ceph_osd *osd = con->private;
5346 int type = le16_to_cpu(hdr->type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005347
Alex Elder1c20f2d2012-06-04 14:43:32 -05005348 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08005349 switch (type) {
5350 case CEPH_MSG_OSD_MAP:
Ilya Dryomova02a9462017-06-19 12:18:05 +02005351 case CEPH_MSG_OSD_BACKOFF:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005352 case CEPH_MSG_WATCH_NOTIFY:
Ilya Dryomov19079202016-04-28 16:07:27 +02005353 return alloc_msg_with_page_vector(hdr);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005354 case CEPH_MSG_OSD_OPREPLY:
5355 return get_reply(con, hdr, skip);
5356 default:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005357 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5358 osd->o_osd, type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005359 *skip = 1;
5360 return NULL;
5361 }
Sage Weilf24e9982009-10-06 11:31:10 -07005362}
5363
5364/*
5365 * Wrappers to refcount containing ceph_osd struct
5366 */
5367static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5368{
5369 struct ceph_osd *osd = con->private;
5370 if (get_osd(osd))
5371 return con;
5372 return NULL;
5373}
5374
5375static void put_osd_con(struct ceph_connection *con)
5376{
5377 struct ceph_osd *osd = con->private;
5378 put_osd(osd);
5379}
5380
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005381/*
5382 * authentication
5383 */
Alex Eldera3530df2012-05-16 15:16:39 -05005384/*
5385 * Note: returned pointer is the address of a structure that's
5386 * managed separately. Caller must *not* attempt to free it.
5387 */
5388static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05005389 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005390{
5391 struct ceph_osd *o = con->private;
5392 struct ceph_osd_client *osdc = o->o_osdc;
5393 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05005394 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005395
Alex Elder74f18692012-05-16 15:16:39 -05005396 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02005397 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05005398 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005399 }
Sage Weil27859f92013-03-25 10:26:14 -07005400 if (!auth->authorizer) {
5401 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5402 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005403 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05005404 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07005405 } else {
5406 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07005407 auth);
5408 if (ret)
5409 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005410 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005411 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05005412
Alex Eldera3530df2012-05-16 15:16:39 -05005413 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005414}
5415
5416
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005417static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005418{
5419 struct ceph_osd *o = con->private;
5420 struct ceph_osd_client *osdc = o->o_osdc;
5421 struct ceph_auth_client *ac = osdc->client->monc.auth;
5422
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005423 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005424}
5425
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005426static int invalidate_authorizer(struct ceph_connection *con)
5427{
5428 struct ceph_osd *o = con->private;
5429 struct ceph_osd_client *osdc = o->o_osdc;
5430 struct ceph_auth_client *ac = osdc->client->monc.auth;
5431
Sage Weil27859f92013-03-25 10:26:14 -07005432 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005433 return ceph_monc_validate_auth(&osdc->client->monc);
5434}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005435
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005436static void osd_reencode_message(struct ceph_msg *msg)
5437{
Ilya Dryomov914902a2017-07-14 16:08:54 +02005438 int type = le16_to_cpu(msg->hdr.type);
5439
5440 if (type == CEPH_MSG_OSD_OP)
5441 encode_request_finish(msg);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005442}
5443
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005444static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005445{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005446 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005447 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005448
Yan, Zheng33d07332014-11-04 16:33:37 +08005449 return ceph_auth_sign_message(auth, msg);
5450}
5451
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005452static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005453{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005454 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005455 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005456
Yan, Zheng33d07332014-11-04 16:33:37 +08005457 return ceph_auth_check_message_signature(auth, msg);
5458}
5459
Tobias Klauser9e327892010-05-20 10:40:19 +02005460static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07005461 .get = get_osd_con,
5462 .put = put_osd_con,
5463 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005464 .get_authorizer = get_authorizer,
5465 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005466 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07005467 .alloc_msg = alloc_msg,
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005468 .reencode_message = osd_reencode_message,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005469 .sign_message = osd_sign_message,
5470 .check_message_signature = osd_check_message_signature,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005471 .fault = osd_fault,
Sage Weilf24e9982009-10-06 11:31:10 -07005472};