blob: f2584fe1246fa088f59ba4aca1e1e8a646e3c644 [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
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001033 req->r_flags = flags;
Yan, Zheng76271512016-02-03 21:24:49 +08001034 req->r_base_oloc.pool = layout->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +08001035 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001036 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -06001037
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001038 req->r_snapid = vino.snap;
1039 if (flags & CEPH_OSD_FLAG_WRITE)
1040 req->r_data_offset = off;
1041
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001042 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1043 if (r)
1044 goto fail;
1045
Sage Weilf24e9982009-10-06 11:31:10 -07001046 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001047
1048fail:
1049 ceph_osdc_put_request(req);
1050 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -07001051}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001052EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001053
1054/*
1055 * We keep osd requests in an rbtree, sorted by ->r_tid.
1056 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001057DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02001058DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
Sage Weilf24e9982009-10-06 11:31:10 -07001059
Ilya Dryomov66850df2018-05-15 15:47:58 +02001060/*
1061 * Call @fn on each OSD request as long as @fn returns 0.
1062 */
1063static void for_each_request(struct ceph_osd_client *osdc,
1064 int (*fn)(struct ceph_osd_request *req, void *arg),
1065 void *arg)
1066{
1067 struct rb_node *n, *p;
1068
1069 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1070 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1071
1072 for (p = rb_first(&osd->o_requests); p; ) {
1073 struct ceph_osd_request *req =
1074 rb_entry(p, struct ceph_osd_request, r_node);
1075
1076 p = rb_next(p);
1077 if (fn(req, arg))
1078 return;
1079 }
1080 }
1081
1082 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1083 struct ceph_osd_request *req =
1084 rb_entry(p, struct ceph_osd_request, r_node);
1085
1086 p = rb_next(p);
1087 if (fn(req, arg))
1088 return;
1089 }
1090}
1091
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001092static bool osd_homeless(struct ceph_osd *osd)
1093{
1094 return osd->o_osd == CEPH_HOMELESS_OSD;
1095}
1096
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001097static bool osd_registered(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001098{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001099 verify_osdc_locked(osd->o_osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001100
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001101 return !RB_EMPTY_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001102}
1103
1104/*
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001105 * Assumes @osd is zero-initialized.
1106 */
1107static void osd_init(struct ceph_osd *osd)
1108{
Elena Reshetova02113a02017-03-17 14:10:28 +02001109 refcount_set(&osd->o_ref, 1);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001110 RB_CLEAR_NODE(&osd->o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001111 osd->o_requests = RB_ROOT;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001112 osd->o_linger_requests = RB_ROOT;
Ilya Dryomova02a9462017-06-19 12:18:05 +02001113 osd->o_backoff_mappings = RB_ROOT;
1114 osd->o_backoffs_by_id = RB_ROOT;
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001115 INIT_LIST_HEAD(&osd->o_osd_lru);
1116 INIT_LIST_HEAD(&osd->o_keepalive_item);
1117 osd->o_incarnation = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001118 mutex_init(&osd->lock);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001119}
1120
1121static void osd_cleanup(struct ceph_osd *osd)
1122{
1123 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001124 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001125 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomova02a9462017-06-19 12:18:05 +02001126 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1127 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001128 WARN_ON(!list_empty(&osd->o_osd_lru));
1129 WARN_ON(!list_empty(&osd->o_keepalive_item));
1130
1131 if (osd->o_auth.authorizer) {
1132 WARN_ON(osd_homeless(osd));
1133 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1134 }
1135}
1136
1137/*
Sage Weilf24e9982009-10-06 11:31:10 -07001138 * Track open sessions with osds.
1139 */
Alex Eldere10006f2012-05-26 23:26:43 -05001140static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001141{
1142 struct ceph_osd *osd;
1143
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001144 WARN_ON(onum == CEPH_HOMELESS_OSD);
1145
Ilya Dryomov7a28f592016-04-28 16:07:25 +02001146 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001147 osd_init(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001148 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001149 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -07001150
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001151 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001152
Sage Weilf24e9982009-10-06 11:31:10 -07001153 return osd;
1154}
1155
1156static struct ceph_osd *get_osd(struct ceph_osd *osd)
1157{
Elena Reshetova02113a02017-03-17 14:10:28 +02001158 if (refcount_inc_not_zero(&osd->o_ref)) {
1159 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1160 refcount_read(&osd->o_ref));
Sage Weilf24e9982009-10-06 11:31:10 -07001161 return osd;
1162 } else {
1163 dout("get_osd %p FAIL\n", osd);
1164 return NULL;
1165 }
1166}
1167
1168static void put_osd(struct ceph_osd *osd)
1169{
Elena Reshetova02113a02017-03-17 14:10:28 +02001170 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1171 refcount_read(&osd->o_ref) - 1);
1172 if (refcount_dec_and_test(&osd->o_ref)) {
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001173 osd_cleanup(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001174 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001175 }
Sage Weilf24e9982009-10-06 11:31:10 -07001176}
1177
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001178DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1179
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001180static void __move_osd_to_lru(struct ceph_osd *osd)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001181{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001182 struct ceph_osd_client *osdc = osd->o_osdc;
1183
1184 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001185 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001186
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001187 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001188 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001189 spin_unlock(&osdc->osd_lru_lock);
1190
Ilya Dryomova319bf52015-05-15 12:02:17 +03001191 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001192}
1193
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001194static void maybe_move_osd_to_lru(struct ceph_osd *osd)
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001195{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001196 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001197 RB_EMPTY_ROOT(&osd->o_linger_requests))
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001198 __move_osd_to_lru(osd);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001199}
1200
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001201static void __remove_osd_from_lru(struct ceph_osd *osd)
1202{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001203 struct ceph_osd_client *osdc = osd->o_osdc;
1204
1205 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1206
1207 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001208 if (!list_empty(&osd->o_osd_lru))
1209 list_del_init(&osd->o_osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001210 spin_unlock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001211}
1212
Sage Weilf24e9982009-10-06 11:31:10 -07001213/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001214 * Close the connection and assign any leftover requests to the
1215 * homeless session.
1216 */
1217static void close_osd(struct ceph_osd *osd)
1218{
1219 struct ceph_osd_client *osdc = osd->o_osdc;
1220 struct rb_node *n;
1221
1222 verify_osdc_wrlocked(osdc);
1223 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1224
1225 ceph_con_close(&osd->o_con);
1226
1227 for (n = rb_first(&osd->o_requests); n; ) {
1228 struct ceph_osd_request *req =
1229 rb_entry(n, struct ceph_osd_request, r_node);
1230
1231 n = rb_next(n); /* unlink_request() */
1232
1233 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1234 unlink_request(osd, req);
1235 link_request(&osdc->homeless_osd, req);
1236 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02001237 for (n = rb_first(&osd->o_linger_requests); n; ) {
1238 struct ceph_osd_linger_request *lreq =
1239 rb_entry(n, struct ceph_osd_linger_request, node);
1240
1241 n = rb_next(n); /* unlink_linger() */
1242
1243 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1244 lreq->linger_id);
1245 unlink_linger(osd, lreq);
1246 link_linger(&osdc->homeless_osd, lreq);
1247 }
Ilya Dryomova02a9462017-06-19 12:18:05 +02001248 clear_backoffs(osd);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001249
1250 __remove_osd_from_lru(osd);
1251 erase_osd(&osdc->osds, osd);
1252 put_osd(osd);
1253}
1254
1255/*
Sage Weilf24e9982009-10-06 11:31:10 -07001256 * reset osd connect
1257 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001258static int reopen_osd(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001259{
Alex Elderc3acb182012-12-07 09:57:58 -06001260 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001261
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001262 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1263
1264 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001265 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001266 close_osd(osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001267 return -ENODEV;
1268 }
1269
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001270 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
Alex Elderc3acb182012-12-07 09:57:58 -06001271 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1272 !ceph_con_opened(&osd->o_con)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001273 struct rb_node *n;
Alex Elderc3acb182012-12-07 09:57:58 -06001274
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001275 dout("osd addr hasn't changed and connection never opened, "
1276 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001277 /* touch each r_stamp for handle_timeout()'s benfit */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001278 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1279 struct ceph_osd_request *req =
1280 rb_entry(n, struct ceph_osd_request, r_node);
Sage Weil87b315a2010-03-22 14:51:18 -07001281 req->r_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001282 }
Alex Elderc3acb182012-12-07 09:57:58 -06001283
1284 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001285 }
Alex Elderc3acb182012-12-07 09:57:58 -06001286
1287 ceph_con_close(&osd->o_con);
1288 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1289 osd->o_incarnation++;
1290
1291 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001292}
1293
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001294static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1295 bool wrlocked)
Sage Weilf24e9982009-10-06 11:31:10 -07001296{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001297 struct ceph_osd *osd;
1298
1299 if (wrlocked)
1300 verify_osdc_wrlocked(osdc);
1301 else
1302 verify_osdc_locked(osdc);
1303
1304 if (o != CEPH_HOMELESS_OSD)
1305 osd = lookup_osd(&osdc->osds, o);
1306 else
1307 osd = &osdc->homeless_osd;
1308 if (!osd) {
1309 if (!wrlocked)
1310 return ERR_PTR(-EAGAIN);
1311
1312 osd = create_osd(osdc, o);
1313 insert_osd(&osdc->osds, osd);
1314 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1315 &osdc->osdmap->osd_addr[osd->o_osd]);
1316 }
1317
1318 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1319 return osd;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001320}
1321
Sage Weilf24e9982009-10-06 11:31:10 -07001322/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001323 * Create request <-> OSD session relation.
1324 *
1325 * @req has to be assigned a tid, @osd may be homeless.
Sage Weilf24e9982009-10-06 11:31:10 -07001326 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001327static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001328{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001329 verify_osd_locked(osd);
1330 WARN_ON(!req->r_tid || req->r_osd);
1331 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1332 req, req->r_tid);
Sage Weil35f9f8a2012-05-16 15:16:38 -05001333
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001334 if (!osd_homeless(osd))
1335 __remove_osd_from_lru(osd);
1336 else
1337 atomic_inc(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001338
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001339 get_osd(osd);
1340 insert_request(&osd->o_requests, req);
1341 req->r_osd = osd;
Sage Weilf24e9982009-10-06 11:31:10 -07001342}
1343
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001344static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001345{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001346 verify_osd_locked(osd);
1347 WARN_ON(req->r_osd != osd);
1348 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1349 req, req->r_tid);
1350
1351 req->r_osd = NULL;
1352 erase_request(&osd->o_requests, req);
1353 put_osd(osd);
1354
1355 if (!osd_homeless(osd))
1356 maybe_move_osd_to_lru(osd);
1357 else
1358 atomic_dec(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001359}
1360
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001361static bool __pool_full(struct ceph_pg_pool_info *pi)
1362{
1363 return pi->flags & CEPH_POOL_FLAG_FULL;
1364}
1365
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001366static bool have_pool_full(struct ceph_osd_client *osdc)
1367{
1368 struct rb_node *n;
1369
1370 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1371 struct ceph_pg_pool_info *pi =
1372 rb_entry(n, struct ceph_pg_pool_info, node);
1373
1374 if (__pool_full(pi))
1375 return true;
1376 }
1377
1378 return false;
1379}
1380
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001381static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1382{
1383 struct ceph_pg_pool_info *pi;
1384
1385 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1386 if (!pi)
1387 return false;
1388
1389 return __pool_full(pi);
1390}
1391
Sage Weilf24e9982009-10-06 11:31:10 -07001392/*
Josh Durgind29adb32013-12-02 19:11:48 -08001393 * Returns whether a request should be blocked from being sent
1394 * based on the current osdmap and osd_client settings.
Josh Durgind29adb32013-12-02 19:11:48 -08001395 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001396static bool target_should_be_paused(struct ceph_osd_client *osdc,
1397 const struct ceph_osd_request_target *t,
1398 struct ceph_pg_pool_info *pi)
1399{
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001400 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1401 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1402 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001403 __pool_full(pi);
1404
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001405 WARN_ON(pi->id != t->target_oloc.pool);
Jeff Layton58eb7932017-04-18 09:21:16 -04001406 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1407 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1408 (osdc->osdmap->epoch < osdc->epoch_barrier);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001409}
1410
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001411enum calc_target_result {
1412 CALC_TARGET_NO_ACTION = 0,
1413 CALC_TARGET_NEED_RESEND,
1414 CALC_TARGET_POOL_DNE,
1415};
1416
1417static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1418 struct ceph_osd_request_target *t,
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001419 struct ceph_connection *con,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001420 bool any_change)
1421{
1422 struct ceph_pg_pool_info *pi;
1423 struct ceph_pg pgid, last_pgid;
1424 struct ceph_osds up, acting;
1425 bool force_resend = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001426 bool unpaused = false;
1427 bool legacy_change;
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001428 bool split = false;
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001429 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001430 bool recovery_deletes = ceph_osdmap_flag(osdc,
1431 CEPH_OSDMAP_RECOVERY_DELETES);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001432 enum calc_target_result ct_res;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001433
Ilya Dryomov04c7d782017-06-15 16:30:55 +02001434 t->epoch = osdc->osdmap->epoch;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001435 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1436 if (!pi) {
1437 t->osd = CEPH_HOMELESS_OSD;
1438 ct_res = CALC_TARGET_POOL_DNE;
1439 goto out;
1440 }
1441
1442 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001443 if (t->last_force_resend < pi->last_force_request_resend) {
1444 t->last_force_resend = pi->last_force_request_resend;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001445 force_resend = true;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001446 } else if (t->last_force_resend == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001447 force_resend = true;
1448 }
1449 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001450
Ilya Dryomovdb098ec2017-06-15 16:30:55 +02001451 /* apply tiering */
1452 ceph_oid_copy(&t->target_oid, &t->base_oid);
1453 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1454 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001455 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1456 t->target_oloc.pool = pi->read_tier;
1457 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1458 t->target_oloc.pool = pi->write_tier;
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001459
1460 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1461 if (!pi) {
1462 t->osd = CEPH_HOMELESS_OSD;
1463 ct_res = CALC_TARGET_POOL_DNE;
1464 goto out;
1465 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001466 }
1467
Ilya Dryomova86f0092018-05-23 14:46:53 +02001468 __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001469 last_pgid.pool = pgid.pool;
1470 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1471
Ilya Dryomovdf281522017-06-15 16:30:56 +02001472 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001473 if (any_change &&
1474 ceph_is_new_interval(&t->acting,
1475 &acting,
1476 &t->up,
1477 &up,
1478 t->size,
1479 pi->size,
1480 t->min_size,
1481 pi->min_size,
1482 t->pg_num,
1483 pi->pg_num,
1484 t->sort_bitwise,
1485 sort_bitwise,
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001486 t->recovery_deletes,
1487 recovery_deletes,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001488 &last_pgid))
1489 force_resend = true;
1490
1491 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1492 t->paused = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001493 unpaused = true;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001494 }
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001495 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1496 ceph_osds_changed(&t->acting, &acting, any_change);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001497 if (t->pg_num)
1498 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001499
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001500 if (legacy_change || force_resend || split) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001501 t->pgid = pgid; /* struct */
Ilya Dryomovdf281522017-06-15 16:30:56 +02001502 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001503 ceph_osds_copy(&t->acting, &acting);
1504 ceph_osds_copy(&t->up, &up);
1505 t->size = pi->size;
1506 t->min_size = pi->min_size;
1507 t->pg_num = pi->pg_num;
1508 t->pg_num_mask = pi->pg_num_mask;
1509 t->sort_bitwise = sort_bitwise;
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001510 t->recovery_deletes = recovery_deletes;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001511
1512 t->osd = acting.primary;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001513 }
1514
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001515 if (unpaused || legacy_change || force_resend ||
1516 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1517 RESEND_ON_SPLIT)))
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001518 ct_res = CALC_TARGET_NEED_RESEND;
1519 else
1520 ct_res = CALC_TARGET_NO_ACTION;
1521
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001522out:
1523 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1524 return ct_res;
1525}
1526
Ilya Dryomova02a9462017-06-19 12:18:05 +02001527static struct ceph_spg_mapping *alloc_spg_mapping(void)
1528{
1529 struct ceph_spg_mapping *spg;
1530
1531 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1532 if (!spg)
1533 return NULL;
1534
1535 RB_CLEAR_NODE(&spg->node);
1536 spg->backoffs = RB_ROOT;
1537 return spg;
1538}
1539
1540static void free_spg_mapping(struct ceph_spg_mapping *spg)
1541{
1542 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1543 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1544
1545 kfree(spg);
1546}
1547
1548/*
1549 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1550 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1551 * defined only within a specific spgid; it does not pass anything to
1552 * children on split, or to another primary.
1553 */
1554DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1555 RB_BYPTR, const struct ceph_spg *, node)
1556
1557static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1558{
1559 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1560}
1561
1562static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1563 void **pkey, size_t *pkey_len)
1564{
1565 if (hoid->key_len) {
1566 *pkey = hoid->key;
1567 *pkey_len = hoid->key_len;
1568 } else {
1569 *pkey = hoid->oid;
1570 *pkey_len = hoid->oid_len;
1571 }
1572}
1573
1574static int compare_names(const void *name1, size_t name1_len,
1575 const void *name2, size_t name2_len)
1576{
1577 int ret;
1578
1579 ret = memcmp(name1, name2, min(name1_len, name2_len));
1580 if (!ret) {
1581 if (name1_len < name2_len)
1582 ret = -1;
1583 else if (name1_len > name2_len)
1584 ret = 1;
1585 }
1586 return ret;
1587}
1588
1589static int hoid_compare(const struct ceph_hobject_id *lhs,
1590 const struct ceph_hobject_id *rhs)
1591{
1592 void *effective_key1, *effective_key2;
1593 size_t effective_key1_len, effective_key2_len;
1594 int ret;
1595
1596 if (lhs->is_max < rhs->is_max)
1597 return -1;
1598 if (lhs->is_max > rhs->is_max)
1599 return 1;
1600
1601 if (lhs->pool < rhs->pool)
1602 return -1;
1603 if (lhs->pool > rhs->pool)
1604 return 1;
1605
1606 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1607 return -1;
1608 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1609 return 1;
1610
1611 ret = compare_names(lhs->nspace, lhs->nspace_len,
1612 rhs->nspace, rhs->nspace_len);
1613 if (ret)
1614 return ret;
1615
1616 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1617 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1618 ret = compare_names(effective_key1, effective_key1_len,
1619 effective_key2, effective_key2_len);
1620 if (ret)
1621 return ret;
1622
1623 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1624 if (ret)
1625 return ret;
1626
1627 if (lhs->snapid < rhs->snapid)
1628 return -1;
1629 if (lhs->snapid > rhs->snapid)
1630 return 1;
1631
1632 return 0;
1633}
1634
1635/*
1636 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1637 * compat stuff here.
1638 *
1639 * Assumes @hoid is zero-initialized.
1640 */
1641static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1642{
1643 u8 struct_v;
1644 u32 struct_len;
1645 int ret;
1646
1647 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1648 &struct_len);
1649 if (ret)
1650 return ret;
1651
1652 if (struct_v < 4) {
1653 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1654 goto e_inval;
1655 }
1656
1657 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1658 GFP_NOIO);
1659 if (IS_ERR(hoid->key)) {
1660 ret = PTR_ERR(hoid->key);
1661 hoid->key = NULL;
1662 return ret;
1663 }
1664
1665 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1666 GFP_NOIO);
1667 if (IS_ERR(hoid->oid)) {
1668 ret = PTR_ERR(hoid->oid);
1669 hoid->oid = NULL;
1670 return ret;
1671 }
1672
1673 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1674 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1675 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1676
1677 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1678 GFP_NOIO);
1679 if (IS_ERR(hoid->nspace)) {
1680 ret = PTR_ERR(hoid->nspace);
1681 hoid->nspace = NULL;
1682 return ret;
1683 }
1684
1685 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1686
1687 ceph_hoid_build_hash_cache(hoid);
1688 return 0;
1689
1690e_inval:
1691 return -EINVAL;
1692}
1693
1694static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1695{
1696 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1697 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1698}
1699
1700static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1701{
1702 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1703 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1704 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1705 ceph_encode_64(p, hoid->snapid);
1706 ceph_encode_32(p, hoid->hash);
1707 ceph_encode_8(p, hoid->is_max);
1708 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1709 ceph_encode_64(p, hoid->pool);
1710}
1711
1712static void free_hoid(struct ceph_hobject_id *hoid)
1713{
1714 if (hoid) {
1715 kfree(hoid->key);
1716 kfree(hoid->oid);
1717 kfree(hoid->nspace);
1718 kfree(hoid);
1719 }
1720}
1721
1722static struct ceph_osd_backoff *alloc_backoff(void)
1723{
1724 struct ceph_osd_backoff *backoff;
1725
1726 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1727 if (!backoff)
1728 return NULL;
1729
1730 RB_CLEAR_NODE(&backoff->spg_node);
1731 RB_CLEAR_NODE(&backoff->id_node);
1732 return backoff;
1733}
1734
1735static void free_backoff(struct ceph_osd_backoff *backoff)
1736{
1737 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1738 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1739
1740 free_hoid(backoff->begin);
1741 free_hoid(backoff->end);
1742 kfree(backoff);
1743}
1744
1745/*
1746 * Within a specific spgid, backoffs are managed by ->begin hoid.
1747 */
1748DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1749 RB_BYVAL, spg_node);
1750
1751static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1752 const struct ceph_hobject_id *hoid)
1753{
1754 struct rb_node *n = root->rb_node;
1755
1756 while (n) {
1757 struct ceph_osd_backoff *cur =
1758 rb_entry(n, struct ceph_osd_backoff, spg_node);
1759 int cmp;
1760
1761 cmp = hoid_compare(hoid, cur->begin);
1762 if (cmp < 0) {
1763 n = n->rb_left;
1764 } else if (cmp > 0) {
1765 if (hoid_compare(hoid, cur->end) < 0)
1766 return cur;
1767
1768 n = n->rb_right;
1769 } else {
1770 return cur;
1771 }
1772 }
1773
1774 return NULL;
1775}
1776
1777/*
1778 * Each backoff has a unique id within its OSD session.
1779 */
1780DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1781
1782static void clear_backoffs(struct ceph_osd *osd)
1783{
1784 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1785 struct ceph_spg_mapping *spg =
1786 rb_entry(rb_first(&osd->o_backoff_mappings),
1787 struct ceph_spg_mapping, node);
1788
1789 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1790 struct ceph_osd_backoff *backoff =
1791 rb_entry(rb_first(&spg->backoffs),
1792 struct ceph_osd_backoff, spg_node);
1793
1794 erase_backoff(&spg->backoffs, backoff);
1795 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1796 free_backoff(backoff);
1797 }
1798 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1799 free_spg_mapping(spg);
1800 }
1801}
1802
1803/*
1804 * Set up a temporary, non-owning view into @t.
1805 */
1806static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1807 const struct ceph_osd_request_target *t)
1808{
1809 hoid->key = NULL;
1810 hoid->key_len = 0;
1811 hoid->oid = t->target_oid.name;
1812 hoid->oid_len = t->target_oid.name_len;
1813 hoid->snapid = CEPH_NOSNAP;
1814 hoid->hash = t->pgid.seed;
1815 hoid->is_max = false;
1816 if (t->target_oloc.pool_ns) {
1817 hoid->nspace = t->target_oloc.pool_ns->str;
1818 hoid->nspace_len = t->target_oloc.pool_ns->len;
1819 } else {
1820 hoid->nspace = NULL;
1821 hoid->nspace_len = 0;
1822 }
1823 hoid->pool = t->target_oloc.pool;
1824 ceph_hoid_build_hash_cache(hoid);
1825}
1826
1827static bool should_plug_request(struct ceph_osd_request *req)
1828{
1829 struct ceph_osd *osd = req->r_osd;
1830 struct ceph_spg_mapping *spg;
1831 struct ceph_osd_backoff *backoff;
1832 struct ceph_hobject_id hoid;
1833
1834 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1835 if (!spg)
1836 return false;
1837
1838 hoid_fill_from_target(&hoid, &req->r_t);
1839 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1840 if (!backoff)
1841 return false;
1842
1843 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1844 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1845 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1846 return true;
1847}
1848
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001849static void setup_request_data(struct ceph_osd_request *req,
1850 struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001851{
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001852 u32 data_len = 0;
1853 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001854
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001855 if (!list_empty(&msg->data))
1856 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001857
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001858 WARN_ON(msg->data_length);
1859 for (i = 0; i < req->r_num_ops; i++) {
1860 struct ceph_osd_req_op *op = &req->r_ops[i];
1861
1862 switch (op->op) {
1863 /* request */
1864 case CEPH_OSD_OP_WRITE:
1865 case CEPH_OSD_OP_WRITEFULL:
1866 WARN_ON(op->indata_len != op->extent.length);
1867 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1868 break;
1869 case CEPH_OSD_OP_SETXATTR:
1870 case CEPH_OSD_OP_CMPXATTR:
1871 WARN_ON(op->indata_len != op->xattr.name_len +
1872 op->xattr.value_len);
1873 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1874 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001875 case CEPH_OSD_OP_NOTIFY_ACK:
1876 ceph_osdc_msg_data_add(msg,
1877 &op->notify_ack.request_data);
1878 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001879
1880 /* reply */
1881 case CEPH_OSD_OP_STAT:
1882 ceph_osdc_msg_data_add(req->r_reply,
1883 &op->raw_data_in);
1884 break;
1885 case CEPH_OSD_OP_READ:
1886 ceph_osdc_msg_data_add(req->r_reply,
1887 &op->extent.osd_data);
1888 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001889 case CEPH_OSD_OP_LIST_WATCHERS:
1890 ceph_osdc_msg_data_add(req->r_reply,
1891 &op->list_watchers.response_data);
1892 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001893
1894 /* both */
1895 case CEPH_OSD_OP_CALL:
1896 WARN_ON(op->indata_len != op->cls.class_len +
1897 op->cls.method_len +
1898 op->cls.indata_len);
1899 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1900 /* optional, can be NONE */
1901 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1902 /* optional, can be NONE */
1903 ceph_osdc_msg_data_add(req->r_reply,
1904 &op->cls.response_data);
1905 break;
Ilya Dryomov19079202016-04-28 16:07:27 +02001906 case CEPH_OSD_OP_NOTIFY:
1907 ceph_osdc_msg_data_add(msg,
1908 &op->notify.request_data);
1909 ceph_osdc_msg_data_add(req->r_reply,
1910 &op->notify.response_data);
1911 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001912 }
1913
1914 data_len += op->indata_len;
1915 }
1916
1917 WARN_ON(data_len != msg->data_length);
1918}
1919
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001920static void encode_pgid(void **p, const struct ceph_pg *pgid)
1921{
1922 ceph_encode_8(p, 1);
1923 ceph_encode_64(p, pgid->pool);
1924 ceph_encode_32(p, pgid->seed);
1925 ceph_encode_32(p, -1); /* preferred */
1926}
1927
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001928static void encode_spgid(void **p, const struct ceph_spg *spgid)
1929{
1930 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1931 encode_pgid(p, &spgid->pgid);
1932 ceph_encode_8(p, spgid->shard);
1933}
1934
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001935static void encode_oloc(void **p, void *end,
1936 const struct ceph_object_locator *oloc)
1937{
1938 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1939 ceph_encode_64(p, oloc->pool);
1940 ceph_encode_32(p, -1); /* preferred */
1941 ceph_encode_32(p, 0); /* key len */
1942 if (oloc->pool_ns)
1943 ceph_encode_string(p, end, oloc->pool_ns->str,
1944 oloc->pool_ns->len);
1945 else
1946 ceph_encode_32(p, 0);
1947}
1948
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001949static void encode_request_partial(struct ceph_osd_request *req,
1950 struct ceph_msg *msg)
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001951{
1952 void *p = msg->front.iov_base;
1953 void *const end = p + msg->front_alloc_len;
1954 u32 data_len = 0;
1955 int i;
1956
1957 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1958 /* snapshots aren't writeable */
1959 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1960 } else {
1961 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1962 req->r_data_offset || req->r_snapc);
1963 }
1964
1965 setup_request_data(req, msg);
1966
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001967 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1968 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001969 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1970 ceph_encode_32(&p, req->r_flags);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001971
1972 /* reqid */
1973 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1974 memset(p, 0, sizeof(struct ceph_osd_reqid));
1975 p += sizeof(struct ceph_osd_reqid);
1976
1977 /* trace */
1978 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1979 p += sizeof(struct ceph_blkin_trace_info);
1980
1981 ceph_encode_32(&p, 0); /* client_inc, always 0 */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001982 ceph_encode_timespec(p, &req->r_mtime);
1983 p += sizeof(struct ceph_timespec);
Jeff Laytonaa26d662017-04-04 08:39:36 -04001984
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001985 encode_oloc(&p, end, &req->r_t.target_oloc);
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001986 ceph_encode_string(&p, end, req->r_t.target_oid.name,
1987 req->r_t.target_oid.name_len);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001988
1989 /* ops, can imply data */
1990 ceph_encode_16(&p, req->r_num_ops);
1991 for (i = 0; i < req->r_num_ops; i++) {
1992 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1993 p += sizeof(struct ceph_osd_op);
1994 }
1995
1996 ceph_encode_64(&p, req->r_snapid); /* snapid */
1997 if (req->r_snapc) {
1998 ceph_encode_64(&p, req->r_snapc->seq);
1999 ceph_encode_32(&p, req->r_snapc->num_snaps);
2000 for (i = 0; i < req->r_snapc->num_snaps; i++)
2001 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2002 } else {
2003 ceph_encode_64(&p, 0); /* snap_seq */
2004 ceph_encode_32(&p, 0); /* snaps len */
2005 }
2006
2007 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002008 BUG_ON(p > end - 8); /* space for features */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002009
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002010 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2011 /* front_len is finalized in encode_request_finish() */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002012 msg->front.iov_len = p - msg->front.iov_base;
2013 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002014 msg->hdr.data_len = cpu_to_le32(data_len);
2015 /*
2016 * The header "data_off" is a hint to the receiver allowing it
2017 * to align received data into its buffers such that there's no
2018 * need to re-copy it before writing it to disk (direct I/O).
2019 */
2020 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2021
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002022 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2023 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2024}
2025
2026static void encode_request_finish(struct ceph_msg *msg)
2027{
2028 void *p = msg->front.iov_base;
Ilya Dryomov986e8982017-07-25 14:40:03 +02002029 void *const partial_end = p + msg->front.iov_len;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002030 void *const end = p + msg->front_alloc_len;
2031
2032 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2033 /* luminous OSD -- encode features and be done */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002034 p = partial_end;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002035 ceph_encode_64(&p, msg->con->peer_features);
2036 } else {
2037 struct {
2038 char spgid[CEPH_ENCODING_START_BLK_LEN +
2039 CEPH_PGID_ENCODING_LEN + 1];
2040 __le32 hash;
2041 __le32 epoch;
2042 __le32 flags;
2043 char reqid[CEPH_ENCODING_START_BLK_LEN +
2044 sizeof(struct ceph_osd_reqid)];
2045 char trace[sizeof(struct ceph_blkin_trace_info)];
2046 __le32 client_inc;
2047 struct ceph_timespec mtime;
2048 } __packed head;
2049 struct ceph_pg pgid;
2050 void *oloc, *oid, *tail;
2051 int oloc_len, oid_len, tail_len;
2052 int len;
2053
2054 /*
2055 * Pre-luminous OSD -- reencode v8 into v4 using @head
2056 * as a temporary buffer. Encode the raw PG; the rest
2057 * is just a matter of moving oloc, oid and tail blobs
2058 * around.
2059 */
2060 memcpy(&head, p, sizeof(head));
2061 p += sizeof(head);
2062
2063 oloc = p;
2064 p += CEPH_ENCODING_START_BLK_LEN;
2065 pgid.pool = ceph_decode_64(&p);
2066 p += 4 + 4; /* preferred, key len */
2067 len = ceph_decode_32(&p);
2068 p += len; /* nspace */
2069 oloc_len = p - oloc;
2070
2071 oid = p;
2072 len = ceph_decode_32(&p);
2073 p += len;
2074 oid_len = p - oid;
2075
2076 tail = p;
Ilya Dryomov986e8982017-07-25 14:40:03 +02002077 tail_len = partial_end - p;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002078
2079 p = msg->front.iov_base;
2080 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2081 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2082 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2083 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2084
2085 /* reassert_version */
2086 memset(p, 0, sizeof(struct ceph_eversion));
2087 p += sizeof(struct ceph_eversion);
2088
2089 BUG_ON(p >= oloc);
2090 memmove(p, oloc, oloc_len);
2091 p += oloc_len;
2092
2093 pgid.seed = le32_to_cpu(head.hash);
2094 encode_pgid(&p, &pgid); /* raw pg */
2095
2096 BUG_ON(p >= oid);
2097 memmove(p, oid, oid_len);
2098 p += oid_len;
2099
2100 /* tail -- ops, snapid, snapc, retry_attempt */
2101 BUG_ON(p >= tail);
2102 memmove(p, tail, tail_len);
2103 p += tail_len;
2104
2105 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2106 }
2107
2108 BUG_ON(p > end);
2109 msg->front.iov_len = p - msg->front.iov_base;
2110 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2111
2112 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2113 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2114 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2115 le16_to_cpu(msg->hdr.version));
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002116}
2117
2118/*
2119 * @req has to be assigned a tid and registered.
2120 */
2121static void send_request(struct ceph_osd_request *req)
2122{
2123 struct ceph_osd *osd = req->r_osd;
2124
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002125 verify_osd_locked(osd);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002126 WARN_ON(osd->o_osd != req->r_t.osd);
2127
Ilya Dryomova02a9462017-06-19 12:18:05 +02002128 /* backoff? */
2129 if (should_plug_request(req))
2130 return;
2131
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002132 /*
2133 * We may have a previously queued request message hanging
2134 * around. Cancel it to avoid corrupting the msgr.
2135 */
2136 if (req->r_sent)
2137 ceph_msg_revoke(req->r_request);
2138
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002139 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2140 if (req->r_attempts)
2141 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2142 else
2143 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2144
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002145 encode_request_partial(req, req->r_request);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002146
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002147 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 +02002148 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002149 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002150 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2151 req->r_attempts);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002152
2153 req->r_t.paused = false;
Sage Weil3dd72fc2010-03-22 14:42:30 -07002154 req->r_stamp = jiffies;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002155 req->r_attempts++;
Sage Weilf24e9982009-10-06 11:31:10 -07002156
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002157 req->r_sent = osd->o_incarnation;
2158 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2159 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
Sage Weilf24e9982009-10-06 11:31:10 -07002160}
2161
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002162static void maybe_request_map(struct ceph_osd_client *osdc)
2163{
2164 bool continuous = false;
2165
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002166 verify_osdc_locked(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002167 WARN_ON(!osdc->osdmap->epoch);
2168
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002169 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2170 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2171 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002172 dout("%s osdc %p continuous\n", __func__, osdc);
2173 continuous = true;
2174 } else {
2175 dout("%s osdc %p onetime\n", __func__, osdc);
2176 }
2177
2178 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2179 osdc->osdmap->epoch + 1, continuous))
2180 ceph_monc_renew_subs(&osdc->client->monc);
2181}
2182
Jeff Laytona1f40202017-04-04 08:39:37 -04002183static void complete_request(struct ceph_osd_request *req, int err);
Ilya Dryomov46092452016-04-28 16:07:27 +02002184static void send_map_check(struct ceph_osd_request *req);
2185
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002186static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002187{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002188 struct ceph_osd_client *osdc = req->r_osdc;
2189 struct ceph_osd *osd;
Ilya Dryomov46092452016-04-28 16:07:27 +02002190 enum calc_target_result ct_res;
Ilya Dryomov66850df2018-05-15 15:47:58 +02002191 int err = 0;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002192 bool need_send = false;
2193 bool promoted = false;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002194
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002195 WARN_ON(req->r_tid);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002196 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2197
2198again:
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002199 ct_res = calc_target(osdc, &req->r_t, NULL, false);
Ilya Dryomov46092452016-04-28 16:07:27 +02002200 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2201 goto promote;
2202
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002203 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2204 if (IS_ERR(osd)) {
2205 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2206 goto promote;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002207 }
2208
Ilya Dryomov66850df2018-05-15 15:47:58 +02002209 if (osdc->abort_err) {
2210 dout("req %p abort_err %d\n", req, osdc->abort_err);
2211 err = osdc->abort_err;
2212 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
Jeff Layton58eb7932017-04-18 09:21:16 -04002213 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2214 osdc->epoch_barrier);
2215 req->r_t.paused = true;
2216 maybe_request_map(osdc);
2217 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2218 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002219 dout("req %p pausewr\n", req);
2220 req->r_t.paused = true;
2221 maybe_request_map(osdc);
2222 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002223 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002224 dout("req %p pauserd\n", req);
2225 req->r_t.paused = true;
2226 maybe_request_map(osdc);
2227 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2228 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2229 CEPH_OSD_FLAG_FULL_FORCE)) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002230 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002231 pool_full(osdc, req->r_t.base_oloc.pool))) {
2232 dout("req %p full/pool_full\n", req);
Ilya Dryomovc843d132018-05-30 16:29:14 +02002233 if (osdc->abort_on_full) {
Ilya Dryomov66850df2018-05-15 15:47:58 +02002234 err = -ENOSPC;
Ilya Dryomov29e87822018-05-17 16:13:07 +02002235 } else {
2236 pr_warn_ratelimited("FULL or reached pool quota\n");
2237 req->r_t.paused = true;
2238 maybe_request_map(osdc);
2239 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002240 } else if (!osd_homeless(osd)) {
2241 need_send = true;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002242 } else {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002243 maybe_request_map(osdc);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002244 }
2245
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002246 mutex_lock(&osd->lock);
2247 /*
2248 * Assign the tid atomically with send_request() to protect
2249 * multiple writes to the same object from racing with each
2250 * other, resulting in out of order ops on the OSDs.
2251 */
2252 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2253 link_request(osd, req);
2254 if (need_send)
2255 send_request(req);
Ilya Dryomov66850df2018-05-15 15:47:58 +02002256 else if (err)
2257 complete_request(req, err);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002258 mutex_unlock(&osd->lock);
2259
Ilya Dryomov60015672018-05-22 16:26:51 +02002260 if (!err && ct_res == CALC_TARGET_POOL_DNE)
Ilya Dryomov46092452016-04-28 16:07:27 +02002261 send_map_check(req);
2262
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002263 if (promoted)
2264 downgrade_write(&osdc->lock);
2265 return;
2266
2267promote:
2268 up_read(&osdc->lock);
2269 down_write(&osdc->lock);
2270 wrlocked = true;
2271 promoted = true;
2272 goto again;
2273}
2274
2275static void account_request(struct ceph_osd_request *req)
2276{
Ilya Dryomov54ea0042017-02-11 18:48:41 +01002277 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002278 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002279
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002280 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002281 atomic_inc(&req->r_osdc->num_requests);
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002282
2283 req->r_start_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002284}
2285
2286static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2287{
2288 ceph_osdc_get_request(req);
2289 account_request(req);
2290 __submit_request(req, wrlocked);
2291}
2292
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002293static void finish_request(struct ceph_osd_request *req)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002294{
2295 struct ceph_osd_client *osdc = req->r_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002296
Ilya Dryomov46092452016-04-28 16:07:27 +02002297 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002298 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2299
2300 if (req->r_osd)
2301 unlink_request(req->r_osd, req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002302 atomic_dec(&osdc->num_requests);
2303
2304 /*
2305 * If an OSD has failed or returned and a request has been sent
2306 * twice, it's possible to get a reply and end up here while the
2307 * request message is queued for delivery. We will ignore the
2308 * reply, so not a big deal, but better to try and catch it.
2309 */
2310 ceph_msg_revoke(req->r_request);
2311 ceph_msg_revoke_incoming(req->r_reply);
2312}
2313
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002314static void __complete_request(struct ceph_osd_request *req)
2315{
Ilya Dryomov26df7262018-05-21 15:33:48 +02002316 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2317 req->r_tid, req->r_callback, req->r_result);
2318
2319 if (req->r_callback)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002320 req->r_callback(req);
Ilya Dryomov26df7262018-05-21 15:33:48 +02002321 complete_all(&req->r_completion);
2322 ceph_osdc_put_request(req);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002323}
2324
Ilya Dryomov88bc1922018-05-21 16:00:29 +02002325static void complete_request_workfn(struct work_struct *work)
2326{
2327 struct ceph_osd_request *req =
2328 container_of(work, struct ceph_osd_request, r_complete_work);
2329
2330 __complete_request(req);
2331}
2332
Ilya Dryomov46092452016-04-28 16:07:27 +02002333/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002334 * This is open-coded in handle_reply().
Ilya Dryomov46092452016-04-28 16:07:27 +02002335 */
2336static void complete_request(struct ceph_osd_request *req, int err)
2337{
2338 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2339
2340 req->r_result = err;
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002341 finish_request(req);
Ilya Dryomov88bc1922018-05-21 16:00:29 +02002342
2343 INIT_WORK(&req->r_complete_work, complete_request_workfn);
2344 queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
Ilya Dryomov46092452016-04-28 16:07:27 +02002345}
2346
2347static void cancel_map_check(struct ceph_osd_request *req)
2348{
2349 struct ceph_osd_client *osdc = req->r_osdc;
2350 struct ceph_osd_request *lookup_req;
2351
2352 verify_osdc_wrlocked(osdc);
2353
2354 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2355 if (!lookup_req)
2356 return;
2357
2358 WARN_ON(lookup_req != req);
2359 erase_request_mc(&osdc->map_checks, req);
2360 ceph_osdc_put_request(req);
2361}
2362
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002363static void cancel_request(struct ceph_osd_request *req)
2364{
2365 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2366
Ilya Dryomov46092452016-04-28 16:07:27 +02002367 cancel_map_check(req);
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002368 finish_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002369 complete_all(&req->r_completion);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01002370 ceph_osdc_put_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002371}
2372
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002373static void abort_request(struct ceph_osd_request *req, int err)
2374{
2375 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2376
2377 cancel_map_check(req);
2378 complete_request(req, err);
2379}
2380
Ilya Dryomov66850df2018-05-15 15:47:58 +02002381static int abort_fn(struct ceph_osd_request *req, void *arg)
2382{
2383 int err = *(int *)arg;
2384
2385 abort_request(req, err);
2386 return 0; /* continue iteration */
2387}
2388
2389/*
2390 * Abort all in-flight requests with @err and arrange for all future
2391 * requests to be failed immediately.
2392 */
2393void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2394{
2395 dout("%s osdc %p err %d\n", __func__, osdc, err);
2396 down_write(&osdc->lock);
2397 for_each_request(osdc, abort_fn, &err);
2398 osdc->abort_err = err;
2399 up_write(&osdc->lock);
2400}
2401EXPORT_SYMBOL(ceph_osdc_abort_requests);
2402
Jeff Layton58eb7932017-04-18 09:21:16 -04002403static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2404{
2405 if (likely(eb > osdc->epoch_barrier)) {
2406 dout("updating epoch_barrier from %u to %u\n",
2407 osdc->epoch_barrier, eb);
2408 osdc->epoch_barrier = eb;
2409 /* Request map if we're not to the barrier yet */
2410 if (eb > osdc->osdmap->epoch)
2411 maybe_request_map(osdc);
2412 }
2413}
2414
2415void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2416{
2417 down_read(&osdc->lock);
2418 if (unlikely(eb > osdc->epoch_barrier)) {
2419 up_read(&osdc->lock);
2420 down_write(&osdc->lock);
2421 update_epoch_barrier(osdc, eb);
2422 up_write(&osdc->lock);
2423 } else {
2424 up_read(&osdc->lock);
2425 }
2426}
2427EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2428
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002429/*
Ilya Dryomov4eea0fe2018-05-16 18:21:34 +02002430 * We can end up releasing caps as a result of abort_request().
2431 * In that case, we probably want to ensure that the cap release message
2432 * has an updated epoch barrier in it, so set the epoch barrier prior to
2433 * aborting the first request.
2434 */
2435static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2436{
2437 struct ceph_osd_client *osdc = req->r_osdc;
2438 bool *victims = arg;
2439
Ilya Dryomovc843d132018-05-30 16:29:14 +02002440 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
Ilya Dryomov4eea0fe2018-05-16 18:21:34 +02002441 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov690f9512018-05-30 14:58:25 +02002442 pool_full(osdc, req->r_t.base_oloc.pool))) {
Ilya Dryomov4eea0fe2018-05-16 18:21:34 +02002443 if (!*victims) {
2444 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2445 *victims = true;
2446 }
2447 abort_request(req, -ENOSPC);
2448 }
2449
2450 return 0; /* continue iteration */
2451}
2452
2453/*
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002454 * Drop all pending requests that are stalled waiting on a full condition to
Jeff Layton58eb7932017-04-18 09:21:16 -04002455 * clear, and complete them with ENOSPC as the return code. Set the
2456 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2457 * cancelled.
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002458 */
2459static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2460{
Jeff Layton58eb7932017-04-18 09:21:16 -04002461 bool victims = false;
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002462
Ilya Dryomovc843d132018-05-30 16:29:14 +02002463 if (osdc->abort_on_full &&
2464 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
Ilya Dryomov4eea0fe2018-05-16 18:21:34 +02002465 for_each_request(osdc, abort_on_full_fn, &victims);
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002466}
2467
Ilya Dryomov46092452016-04-28 16:07:27 +02002468static void check_pool_dne(struct ceph_osd_request *req)
2469{
2470 struct ceph_osd_client *osdc = req->r_osdc;
2471 struct ceph_osdmap *map = osdc->osdmap;
2472
2473 verify_osdc_wrlocked(osdc);
2474 WARN_ON(!map->epoch);
2475
2476 if (req->r_attempts) {
2477 /*
2478 * We sent a request earlier, which means that
2479 * previously the pool existed, and now it does not
2480 * (i.e., it was deleted).
2481 */
2482 req->r_map_dne_bound = map->epoch;
2483 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2484 req->r_tid);
2485 } else {
2486 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2487 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2488 }
2489
2490 if (req->r_map_dne_bound) {
2491 if (map->epoch >= req->r_map_dne_bound) {
2492 /* we had a new enough map */
2493 pr_info_ratelimited("tid %llu pool does not exist\n",
2494 req->r_tid);
2495 complete_request(req, -ENOENT);
2496 }
2497 } else {
2498 send_map_check(req);
2499 }
2500}
2501
2502static void map_check_cb(struct ceph_mon_generic_request *greq)
2503{
2504 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2505 struct ceph_osd_request *req;
2506 u64 tid = greq->private_data;
2507
2508 WARN_ON(greq->result || !greq->u.newest);
2509
2510 down_write(&osdc->lock);
2511 req = lookup_request_mc(&osdc->map_checks, tid);
2512 if (!req) {
2513 dout("%s tid %llu dne\n", __func__, tid);
2514 goto out_unlock;
2515 }
2516
2517 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2518 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2519 if (!req->r_map_dne_bound)
2520 req->r_map_dne_bound = greq->u.newest;
2521 erase_request_mc(&osdc->map_checks, req);
2522 check_pool_dne(req);
2523
2524 ceph_osdc_put_request(req);
2525out_unlock:
2526 up_write(&osdc->lock);
2527}
2528
2529static void send_map_check(struct ceph_osd_request *req)
2530{
2531 struct ceph_osd_client *osdc = req->r_osdc;
2532 struct ceph_osd_request *lookup_req;
2533 int ret;
2534
2535 verify_osdc_wrlocked(osdc);
2536
2537 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2538 if (lookup_req) {
2539 WARN_ON(lookup_req != req);
2540 return;
2541 }
2542
2543 ceph_osdc_get_request(req);
2544 insert_request_mc(&osdc->map_checks, req);
2545 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2546 map_check_cb, req->r_tid);
2547 WARN_ON(ret);
2548}
2549
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002550/*
Ilya Dryomov922dab62016-05-26 01:15:02 +02002551 * lingering requests, watch/notify v2 infrastructure
2552 */
2553static void linger_release(struct kref *kref)
2554{
2555 struct ceph_osd_linger_request *lreq =
2556 container_of(kref, struct ceph_osd_linger_request, kref);
2557
2558 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2559 lreq->reg_req, lreq->ping_req);
2560 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2561 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
Ilya Dryomov46092452016-04-28 16:07:27 +02002562 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002563 WARN_ON(!list_empty(&lreq->scan_item));
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002564 WARN_ON(!list_empty(&lreq->pending_lworks));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002565 WARN_ON(lreq->osd);
2566
2567 if (lreq->reg_req)
2568 ceph_osdc_put_request(lreq->reg_req);
2569 if (lreq->ping_req)
2570 ceph_osdc_put_request(lreq->ping_req);
2571 target_destroy(&lreq->t);
2572 kfree(lreq);
2573}
2574
2575static void linger_put(struct ceph_osd_linger_request *lreq)
2576{
2577 if (lreq)
2578 kref_put(&lreq->kref, linger_release);
2579}
2580
2581static struct ceph_osd_linger_request *
2582linger_get(struct ceph_osd_linger_request *lreq)
2583{
2584 kref_get(&lreq->kref);
2585 return lreq;
2586}
2587
2588static struct ceph_osd_linger_request *
2589linger_alloc(struct ceph_osd_client *osdc)
2590{
2591 struct ceph_osd_linger_request *lreq;
2592
2593 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2594 if (!lreq)
2595 return NULL;
2596
2597 kref_init(&lreq->kref);
2598 mutex_init(&lreq->lock);
2599 RB_CLEAR_NODE(&lreq->node);
2600 RB_CLEAR_NODE(&lreq->osdc_node);
Ilya Dryomov46092452016-04-28 16:07:27 +02002601 RB_CLEAR_NODE(&lreq->mc_node);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002602 INIT_LIST_HEAD(&lreq->scan_item);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002603 INIT_LIST_HEAD(&lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002604 init_completion(&lreq->reg_commit_wait);
Ilya Dryomov19079202016-04-28 16:07:27 +02002605 init_completion(&lreq->notify_finish_wait);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002606
2607 lreq->osdc = osdc;
2608 target_init(&lreq->t);
2609
2610 dout("%s lreq %p\n", __func__, lreq);
2611 return lreq;
2612}
2613
2614DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2615DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02002616DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002617
2618/*
2619 * Create linger request <-> OSD session relation.
2620 *
2621 * @lreq has to be registered, @osd may be homeless.
2622 */
2623static void link_linger(struct ceph_osd *osd,
2624 struct ceph_osd_linger_request *lreq)
2625{
2626 verify_osd_locked(osd);
2627 WARN_ON(!lreq->linger_id || lreq->osd);
2628 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2629 osd->o_osd, lreq, lreq->linger_id);
2630
2631 if (!osd_homeless(osd))
2632 __remove_osd_from_lru(osd);
2633 else
2634 atomic_inc(&osd->o_osdc->num_homeless);
2635
2636 get_osd(osd);
2637 insert_linger(&osd->o_linger_requests, lreq);
2638 lreq->osd = osd;
2639}
2640
2641static void unlink_linger(struct ceph_osd *osd,
2642 struct ceph_osd_linger_request *lreq)
2643{
2644 verify_osd_locked(osd);
2645 WARN_ON(lreq->osd != osd);
2646 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2647 osd->o_osd, lreq, lreq->linger_id);
2648
2649 lreq->osd = NULL;
2650 erase_linger(&osd->o_linger_requests, lreq);
2651 put_osd(osd);
2652
2653 if (!osd_homeless(osd))
2654 maybe_move_osd_to_lru(osd);
2655 else
2656 atomic_dec(&osd->o_osdc->num_homeless);
2657}
2658
2659static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2660{
2661 verify_osdc_locked(lreq->osdc);
2662
2663 return !RB_EMPTY_NODE(&lreq->osdc_node);
2664}
2665
2666static bool linger_registered(struct ceph_osd_linger_request *lreq)
2667{
2668 struct ceph_osd_client *osdc = lreq->osdc;
2669 bool registered;
2670
2671 down_read(&osdc->lock);
2672 registered = __linger_registered(lreq);
2673 up_read(&osdc->lock);
2674
2675 return registered;
2676}
2677
2678static void linger_register(struct ceph_osd_linger_request *lreq)
2679{
2680 struct ceph_osd_client *osdc = lreq->osdc;
2681
2682 verify_osdc_wrlocked(osdc);
2683 WARN_ON(lreq->linger_id);
2684
2685 linger_get(lreq);
2686 lreq->linger_id = ++osdc->last_linger_id;
2687 insert_linger_osdc(&osdc->linger_requests, lreq);
2688}
2689
2690static void linger_unregister(struct ceph_osd_linger_request *lreq)
2691{
2692 struct ceph_osd_client *osdc = lreq->osdc;
2693
2694 verify_osdc_wrlocked(osdc);
2695
2696 erase_linger_osdc(&osdc->linger_requests, lreq);
2697 linger_put(lreq);
2698}
2699
2700static void cancel_linger_request(struct ceph_osd_request *req)
2701{
2702 struct ceph_osd_linger_request *lreq = req->r_priv;
2703
2704 WARN_ON(!req->r_linger);
2705 cancel_request(req);
2706 linger_put(lreq);
2707}
2708
2709struct linger_work {
2710 struct work_struct work;
2711 struct ceph_osd_linger_request *lreq;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002712 struct list_head pending_item;
2713 unsigned long queued_stamp;
Ilya Dryomov922dab62016-05-26 01:15:02 +02002714
2715 union {
2716 struct {
2717 u64 notify_id;
2718 u64 notifier_id;
2719 void *payload; /* points into @msg front */
2720 size_t payload_len;
2721
2722 struct ceph_msg *msg; /* for ceph_msg_put() */
2723 } notify;
2724 struct {
2725 int err;
2726 } error;
2727 };
2728};
2729
2730static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2731 work_func_t workfn)
2732{
2733 struct linger_work *lwork;
2734
2735 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2736 if (!lwork)
2737 return NULL;
2738
2739 INIT_WORK(&lwork->work, workfn);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002740 INIT_LIST_HEAD(&lwork->pending_item);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002741 lwork->lreq = linger_get(lreq);
2742
2743 return lwork;
2744}
2745
2746static void lwork_free(struct linger_work *lwork)
2747{
2748 struct ceph_osd_linger_request *lreq = lwork->lreq;
2749
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002750 mutex_lock(&lreq->lock);
2751 list_del(&lwork->pending_item);
2752 mutex_unlock(&lreq->lock);
2753
Ilya Dryomov922dab62016-05-26 01:15:02 +02002754 linger_put(lreq);
2755 kfree(lwork);
2756}
2757
2758static void lwork_queue(struct linger_work *lwork)
2759{
2760 struct ceph_osd_linger_request *lreq = lwork->lreq;
2761 struct ceph_osd_client *osdc = lreq->osdc;
2762
2763 verify_lreq_locked(lreq);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002764 WARN_ON(!list_empty(&lwork->pending_item));
2765
2766 lwork->queued_stamp = jiffies;
2767 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002768 queue_work(osdc->notify_wq, &lwork->work);
2769}
2770
2771static void do_watch_notify(struct work_struct *w)
2772{
2773 struct linger_work *lwork = container_of(w, struct linger_work, work);
2774 struct ceph_osd_linger_request *lreq = lwork->lreq;
2775
2776 if (!linger_registered(lreq)) {
2777 dout("%s lreq %p not registered\n", __func__, lreq);
2778 goto out;
2779 }
2780
Ilya Dryomov19079202016-04-28 16:07:27 +02002781 WARN_ON(!lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002782 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2783 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2784 lwork->notify.payload_len);
2785 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2786 lwork->notify.notifier_id, lwork->notify.payload,
2787 lwork->notify.payload_len);
2788
2789out:
2790 ceph_msg_put(lwork->notify.msg);
2791 lwork_free(lwork);
2792}
2793
2794static void do_watch_error(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
2804 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2805 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2806
2807out:
2808 lwork_free(lwork);
2809}
2810
2811static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2812{
2813 struct linger_work *lwork;
2814
2815 lwork = lwork_alloc(lreq, do_watch_error);
2816 if (!lwork) {
2817 pr_err("failed to allocate error-lwork\n");
2818 return;
2819 }
2820
2821 lwork->error.err = lreq->last_error;
2822 lwork_queue(lwork);
2823}
2824
2825static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2826 int result)
2827{
2828 if (!completion_done(&lreq->reg_commit_wait)) {
2829 lreq->reg_commit_error = (result <= 0 ? result : 0);
2830 complete_all(&lreq->reg_commit_wait);
2831 }
2832}
2833
2834static void linger_commit_cb(struct ceph_osd_request *req)
2835{
2836 struct ceph_osd_linger_request *lreq = req->r_priv;
2837
2838 mutex_lock(&lreq->lock);
2839 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2840 lreq->linger_id, req->r_result);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002841 linger_reg_commit_complete(lreq, req->r_result);
2842 lreq->committed = true;
2843
Ilya Dryomov19079202016-04-28 16:07:27 +02002844 if (!lreq->is_watch) {
2845 struct ceph_osd_data *osd_data =
2846 osd_req_op_data(req, 0, notify, response_data);
2847 void *p = page_address(osd_data->pages[0]);
2848
2849 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2850 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2851
2852 /* make note of the notify_id */
2853 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2854 lreq->notify_id = ceph_decode_64(&p);
2855 dout("lreq %p notify_id %llu\n", lreq,
2856 lreq->notify_id);
2857 } else {
2858 dout("lreq %p no notify_id\n", lreq);
2859 }
2860 }
2861
Ilya Dryomov922dab62016-05-26 01:15:02 +02002862 mutex_unlock(&lreq->lock);
2863 linger_put(lreq);
2864}
2865
2866static int normalize_watch_error(int err)
2867{
2868 /*
2869 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2870 * notification and a failure to reconnect because we raced with
2871 * the delete appear the same to the user.
2872 */
2873 if (err == -ENOENT)
2874 err = -ENOTCONN;
2875
2876 return err;
2877}
2878
2879static void linger_reconnect_cb(struct ceph_osd_request *req)
2880{
2881 struct ceph_osd_linger_request *lreq = req->r_priv;
2882
2883 mutex_lock(&lreq->lock);
2884 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2885 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2886 if (req->r_result < 0) {
2887 if (!lreq->last_error) {
2888 lreq->last_error = normalize_watch_error(req->r_result);
2889 queue_watch_error(lreq);
2890 }
2891 }
2892
2893 mutex_unlock(&lreq->lock);
2894 linger_put(lreq);
2895}
2896
2897static void send_linger(struct ceph_osd_linger_request *lreq)
2898{
2899 struct ceph_osd_request *req = lreq->reg_req;
2900 struct ceph_osd_req_op *op = &req->r_ops[0];
2901
2902 verify_osdc_wrlocked(req->r_osdc);
2903 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2904
2905 if (req->r_osd)
2906 cancel_linger_request(req);
2907
2908 request_reinit(req);
2909 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2910 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2911 req->r_flags = lreq->t.flags;
2912 req->r_mtime = lreq->mtime;
2913
2914 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002915 if (lreq->is_watch && lreq->committed) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002916 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2917 op->watch.cookie != lreq->linger_id);
2918 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2919 op->watch.gen = ++lreq->register_gen;
2920 dout("lreq %p reconnect register_gen %u\n", lreq,
2921 op->watch.gen);
2922 req->r_callback = linger_reconnect_cb;
2923 } else {
Ilya Dryomov19079202016-04-28 16:07:27 +02002924 if (!lreq->is_watch)
2925 lreq->notify_id = 0;
2926 else
2927 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002928 dout("lreq %p register\n", lreq);
2929 req->r_callback = linger_commit_cb;
2930 }
2931 mutex_unlock(&lreq->lock);
2932
2933 req->r_priv = linger_get(lreq);
2934 req->r_linger = true;
2935
2936 submit_request(req, true);
2937}
2938
2939static void linger_ping_cb(struct ceph_osd_request *req)
2940{
2941 struct ceph_osd_linger_request *lreq = req->r_priv;
2942
2943 mutex_lock(&lreq->lock);
2944 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2945 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2946 lreq->last_error);
2947 if (lreq->register_gen == req->r_ops[0].watch.gen) {
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002948 if (!req->r_result) {
2949 lreq->watch_valid_thru = lreq->ping_sent;
2950 } else if (!lreq->last_error) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002951 lreq->last_error = normalize_watch_error(req->r_result);
2952 queue_watch_error(lreq);
2953 }
2954 } else {
2955 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2956 lreq->register_gen, req->r_ops[0].watch.gen);
2957 }
2958
2959 mutex_unlock(&lreq->lock);
2960 linger_put(lreq);
2961}
2962
2963static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2964{
2965 struct ceph_osd_client *osdc = lreq->osdc;
2966 struct ceph_osd_request *req = lreq->ping_req;
2967 struct ceph_osd_req_op *op = &req->r_ops[0];
2968
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002969 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002970 dout("%s PAUSERD\n", __func__);
2971 return;
2972 }
2973
2974 lreq->ping_sent = jiffies;
2975 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2976 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2977 lreq->register_gen);
2978
2979 if (req->r_osd)
2980 cancel_linger_request(req);
2981
2982 request_reinit(req);
2983 target_copy(&req->r_t, &lreq->t);
2984
2985 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2986 op->watch.cookie != lreq->linger_id ||
2987 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2988 op->watch.gen = lreq->register_gen;
2989 req->r_callback = linger_ping_cb;
2990 req->r_priv = linger_get(lreq);
2991 req->r_linger = true;
2992
2993 ceph_osdc_get_request(req);
2994 account_request(req);
2995 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2996 link_request(lreq->osd, req);
2997 send_request(req);
2998}
2999
3000static void linger_submit(struct ceph_osd_linger_request *lreq)
3001{
3002 struct ceph_osd_client *osdc = lreq->osdc;
3003 struct ceph_osd *osd;
3004
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003005 calc_target(osdc, &lreq->t, NULL, false);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003006 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3007 link_linger(osd, lreq);
3008
3009 send_linger(lreq);
3010}
3011
Ilya Dryomov46092452016-04-28 16:07:27 +02003012static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3013{
3014 struct ceph_osd_client *osdc = lreq->osdc;
3015 struct ceph_osd_linger_request *lookup_lreq;
3016
3017 verify_osdc_wrlocked(osdc);
3018
3019 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3020 lreq->linger_id);
3021 if (!lookup_lreq)
3022 return;
3023
3024 WARN_ON(lookup_lreq != lreq);
3025 erase_linger_mc(&osdc->linger_map_checks, lreq);
3026 linger_put(lreq);
3027}
3028
Ilya Dryomov922dab62016-05-26 01:15:02 +02003029/*
3030 * @lreq has to be both registered and linked.
3031 */
3032static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3033{
Ilya Dryomov19079202016-04-28 16:07:27 +02003034 if (lreq->is_watch && lreq->ping_req->r_osd)
Ilya Dryomov922dab62016-05-26 01:15:02 +02003035 cancel_linger_request(lreq->ping_req);
3036 if (lreq->reg_req->r_osd)
3037 cancel_linger_request(lreq->reg_req);
Ilya Dryomov46092452016-04-28 16:07:27 +02003038 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003039 unlink_linger(lreq->osd, lreq);
3040 linger_unregister(lreq);
3041}
3042
3043static void linger_cancel(struct ceph_osd_linger_request *lreq)
3044{
3045 struct ceph_osd_client *osdc = lreq->osdc;
3046
3047 down_write(&osdc->lock);
3048 if (__linger_registered(lreq))
3049 __linger_cancel(lreq);
3050 up_write(&osdc->lock);
3051}
3052
Ilya Dryomov46092452016-04-28 16:07:27 +02003053static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3054
3055static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3056{
3057 struct ceph_osd_client *osdc = lreq->osdc;
3058 struct ceph_osdmap *map = osdc->osdmap;
3059
3060 verify_osdc_wrlocked(osdc);
3061 WARN_ON(!map->epoch);
3062
3063 if (lreq->register_gen) {
3064 lreq->map_dne_bound = map->epoch;
3065 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3066 lreq, lreq->linger_id);
3067 } else {
3068 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3069 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3070 map->epoch);
3071 }
3072
3073 if (lreq->map_dne_bound) {
3074 if (map->epoch >= lreq->map_dne_bound) {
3075 /* we had a new enough map */
3076 pr_info("linger_id %llu pool does not exist\n",
3077 lreq->linger_id);
3078 linger_reg_commit_complete(lreq, -ENOENT);
3079 __linger_cancel(lreq);
3080 }
3081 } else {
3082 send_linger_map_check(lreq);
3083 }
3084}
3085
3086static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3087{
3088 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3089 struct ceph_osd_linger_request *lreq;
3090 u64 linger_id = greq->private_data;
3091
3092 WARN_ON(greq->result || !greq->u.newest);
3093
3094 down_write(&osdc->lock);
3095 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3096 if (!lreq) {
3097 dout("%s linger_id %llu dne\n", __func__, linger_id);
3098 goto out_unlock;
3099 }
3100
3101 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3102 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3103 greq->u.newest);
3104 if (!lreq->map_dne_bound)
3105 lreq->map_dne_bound = greq->u.newest;
3106 erase_linger_mc(&osdc->linger_map_checks, lreq);
3107 check_linger_pool_dne(lreq);
3108
3109 linger_put(lreq);
3110out_unlock:
3111 up_write(&osdc->lock);
3112}
3113
3114static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3115{
3116 struct ceph_osd_client *osdc = lreq->osdc;
3117 struct ceph_osd_linger_request *lookup_lreq;
3118 int ret;
3119
3120 verify_osdc_wrlocked(osdc);
3121
3122 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3123 lreq->linger_id);
3124 if (lookup_lreq) {
3125 WARN_ON(lookup_lreq != lreq);
3126 return;
3127 }
3128
3129 linger_get(lreq);
3130 insert_linger_mc(&osdc->linger_map_checks, lreq);
3131 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3132 linger_map_check_cb, lreq->linger_id);
3133 WARN_ON(ret);
3134}
3135
Ilya Dryomov922dab62016-05-26 01:15:02 +02003136static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3137{
3138 int ret;
3139
3140 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3141 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3142 return ret ?: lreq->reg_commit_error;
3143}
3144
Ilya Dryomov19079202016-04-28 16:07:27 +02003145static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3146{
3147 int ret;
3148
3149 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3150 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3151 return ret ?: lreq->notify_finish_error;
3152}
3153
Ilya Dryomov922dab62016-05-26 01:15:02 +02003154/*
Ilya Dryomovfbca9632016-04-28 16:07:24 +02003155 * Timeout callback, called every N seconds. When 1 or more OSD
3156 * requests has been active for more than N seconds, we send a keepalive
3157 * (tag + timestamp) to its OSD to ensure any communications channel
3158 * reset is detected.
Sage Weilf24e9982009-10-06 11:31:10 -07003159 */
3160static void handle_timeout(struct work_struct *work)
3161{
3162 struct ceph_osd_client *osdc =
3163 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03003164 struct ceph_options *opts = osdc->client->options;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003165 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003166 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003167 LIST_HEAD(slow_osds);
3168 struct rb_node *n, *p;
Sage Weilf24e9982009-10-06 11:31:10 -07003169
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003170 dout("%s osdc %p\n", __func__, osdc);
3171 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003172
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003173 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003174 * ping osds that are a bit slow. this ensures that if there
3175 * is a break in the TCP connection we will notice, and reopen
3176 * a connection with that osd (from the fault callback).
3177 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003178 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3179 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3180 bool found = false;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003181
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003182 for (p = rb_first(&osd->o_requests); p; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003183 struct ceph_osd_request *req =
3184 rb_entry(p, struct ceph_osd_request, r_node);
3185
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003186 p = rb_next(p); /* abort_request() */
3187
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003188 if (time_before(req->r_stamp, cutoff)) {
3189 dout(" req %p tid %llu on osd%d is laggy\n",
3190 req, req->r_tid, osd->o_osd);
3191 found = true;
3192 }
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003193 if (opts->osd_request_timeout &&
3194 time_before(req->r_start_stamp, expiry_cutoff)) {
3195 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3196 req->r_tid, osd->o_osd);
3197 abort_request(req, -ETIMEDOUT);
3198 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003199 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003200 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3201 struct ceph_osd_linger_request *lreq =
3202 rb_entry(p, struct ceph_osd_linger_request, node);
3203
3204 dout(" lreq %p linger_id %llu is served by osd%d\n",
3205 lreq, lreq->linger_id, osd->o_osd);
3206 found = true;
3207
3208 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02003209 if (lreq->is_watch && lreq->committed && !lreq->last_error)
Ilya Dryomov922dab62016-05-26 01:15:02 +02003210 send_linger_ping(lreq);
3211 mutex_unlock(&lreq->lock);
3212 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003213
3214 if (found)
3215 list_move_tail(&osd->o_keepalive_item, &slow_osds);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003216 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003217
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003218 if (opts->osd_request_timeout) {
3219 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3220 struct ceph_osd_request *req =
3221 rb_entry(p, struct ceph_osd_request, r_node);
3222
3223 p = rb_next(p); /* abort_request() */
3224
3225 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3226 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3227 req->r_tid, osdc->homeless_osd.o_osd);
3228 abort_request(req, -ETIMEDOUT);
3229 }
3230 }
3231 }
3232
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003233 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3234 maybe_request_map(osdc);
3235
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003236 while (!list_empty(&slow_osds)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003237 struct ceph_osd *osd = list_first_entry(&slow_osds,
3238 struct ceph_osd,
3239 o_keepalive_item);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003240 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07003241 ceph_con_keepalive(&osd->o_con);
3242 }
3243
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003244 up_write(&osdc->lock);
Ilya Dryomovfbca9632016-04-28 16:07:24 +02003245 schedule_delayed_work(&osdc->timeout_work,
3246 osdc->client->options->osd_keepalive_timeout);
Sage Weilf24e9982009-10-06 11:31:10 -07003247}
3248
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003249static void handle_osds_timeout(struct work_struct *work)
3250{
3251 struct ceph_osd_client *osdc =
3252 container_of(work, struct ceph_osd_client,
3253 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03003254 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003255 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003256
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003257 dout("%s osdc %p\n", __func__, osdc);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003258 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003259 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3260 if (time_before(jiffies, osd->lru_ttl))
3261 break;
3262
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003263 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02003264 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003265 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003266 }
3267
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003268 up_write(&osdc->lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003269 schedule_delayed_work(&osdc->osds_timeout_work,
3270 round_jiffies_relative(delay));
3271}
3272
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003273static int ceph_oloc_decode(void **p, void *end,
3274 struct ceph_object_locator *oloc)
3275{
3276 u8 struct_v, struct_cv;
3277 u32 len;
3278 void *struct_end;
3279 int ret = 0;
3280
3281 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3282 struct_v = ceph_decode_8(p);
3283 struct_cv = ceph_decode_8(p);
3284 if (struct_v < 3) {
3285 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3286 struct_v, struct_cv);
3287 goto e_inval;
3288 }
3289 if (struct_cv > 6) {
3290 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3291 struct_v, struct_cv);
3292 goto e_inval;
3293 }
3294 len = ceph_decode_32(p);
3295 ceph_decode_need(p, end, len, e_inval);
3296 struct_end = *p + len;
3297
3298 oloc->pool = ceph_decode_64(p);
3299 *p += 4; /* skip preferred */
3300
3301 len = ceph_decode_32(p);
3302 if (len > 0) {
3303 pr_warn("ceph_object_locator::key is set\n");
3304 goto e_inval;
3305 }
3306
3307 if (struct_v >= 5) {
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003308 bool changed = false;
3309
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003310 len = ceph_decode_32(p);
3311 if (len > 0) {
Yan, Zheng30c156d2016-02-14 11:24:31 +08003312 ceph_decode_need(p, end, len, e_inval);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003313 if (!oloc->pool_ns ||
3314 ceph_compare_string(oloc->pool_ns, *p, len))
3315 changed = true;
Yan, Zheng30c156d2016-02-14 11:24:31 +08003316 *p += len;
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003317 } else {
3318 if (oloc->pool_ns)
3319 changed = true;
3320 }
3321 if (changed) {
3322 /* redirect changes namespace */
3323 pr_warn("ceph_object_locator::nspace is changed\n");
3324 goto e_inval;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003325 }
3326 }
3327
3328 if (struct_v >= 6) {
3329 s64 hash = ceph_decode_64(p);
3330 if (hash != -1) {
3331 pr_warn("ceph_object_locator::hash is set\n");
3332 goto e_inval;
3333 }
3334 }
3335
3336 /* skip the rest */
3337 *p = struct_end;
3338out:
3339 return ret;
3340
3341e_inval:
3342 ret = -EINVAL;
3343 goto out;
3344}
3345
3346static int ceph_redirect_decode(void **p, void *end,
3347 struct ceph_request_redirect *redir)
3348{
3349 u8 struct_v, struct_cv;
3350 u32 len;
3351 void *struct_end;
3352 int ret;
3353
3354 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3355 struct_v = ceph_decode_8(p);
3356 struct_cv = ceph_decode_8(p);
3357 if (struct_cv > 1) {
3358 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3359 struct_v, struct_cv);
3360 goto e_inval;
3361 }
3362 len = ceph_decode_32(p);
3363 ceph_decode_need(p, end, len, e_inval);
3364 struct_end = *p + len;
3365
3366 ret = ceph_oloc_decode(p, end, &redir->oloc);
3367 if (ret)
3368 goto out;
3369
3370 len = ceph_decode_32(p);
3371 if (len > 0) {
3372 pr_warn("ceph_request_redirect::object_name is set\n");
3373 goto e_inval;
3374 }
3375
3376 len = ceph_decode_32(p);
3377 *p += len; /* skip osd_instructions */
3378
3379 /* skip the rest */
3380 *p = struct_end;
3381out:
3382 return ret;
3383
3384e_inval:
3385 ret = -EINVAL;
3386 goto out;
3387}
3388
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003389struct MOSDOpReply {
3390 struct ceph_pg pgid;
3391 u64 flags;
3392 int result;
3393 u32 epoch;
3394 int num_ops;
3395 u32 outdata_len[CEPH_OSD_MAX_OPS];
3396 s32 rval[CEPH_OSD_MAX_OPS];
3397 int retry_attempt;
3398 struct ceph_eversion replay_version;
3399 u64 user_version;
3400 struct ceph_request_redirect redirect;
3401};
Sage Weil25845472011-06-03 09:37:09 -07003402
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003403static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
Sage Weilf24e9982009-10-06 11:31:10 -07003404{
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003405 void *p = msg->front.iov_base;
3406 void *const end = p + msg->front.iov_len;
3407 u16 version = le16_to_cpu(msg->hdr.version);
3408 struct ceph_eversion bad_replay_version;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003409 u8 decode_redir;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003410 u32 len;
3411 int ret;
3412 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07003413
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003414 ceph_decode_32_safe(&p, end, len, e_inval);
3415 ceph_decode_need(&p, end, len, e_inval);
3416 p += len; /* skip oid */
Sage Weil1b83bef2013-02-25 16:11:12 -08003417
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003418 ret = ceph_decode_pgid(&p, end, &m->pgid);
3419 if (ret)
3420 return ret;
Sage Weil1b83bef2013-02-25 16:11:12 -08003421
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003422 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3423 ceph_decode_32_safe(&p, end, m->result, e_inval);
3424 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3425 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3426 p += sizeof(bad_replay_version);
3427 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
Sage Weil1b83bef2013-02-25 16:11:12 -08003428
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003429 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3430 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3431 goto e_inval;
Sage Weil1b83bef2013-02-25 16:11:12 -08003432
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003433 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3434 e_inval);
3435 for (i = 0; i < m->num_ops; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -08003436 struct ceph_osd_op *op = p;
Sage Weil1b83bef2013-02-25 16:11:12 -08003437
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003438 m->outdata_len[i] = le32_to_cpu(op->payload_len);
Sage Weil1b83bef2013-02-25 16:11:12 -08003439 p += sizeof(*op);
3440 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003441
3442 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3443 for (i = 0; i < m->num_ops; i++)
3444 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3445
3446 if (version >= 5) {
3447 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3448 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3449 p += sizeof(m->replay_version);
3450 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3451 } else {
3452 m->replay_version = bad_replay_version; /* struct */
3453 m->user_version = le64_to_cpu(m->replay_version.version);
Sage Weil1b83bef2013-02-25 16:11:12 -08003454 }
3455
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003456 if (version >= 6) {
3457 if (version >= 7)
3458 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003459 else
3460 decode_redir = 1;
3461 } else {
3462 decode_redir = 0;
3463 }
3464
3465 if (decode_redir) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003466 ret = ceph_redirect_decode(&p, end, &m->redirect);
3467 if (ret)
3468 return ret;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003469 } else {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003470 ceph_oloc_init(&m->redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003471 }
3472
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003473 return 0;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003474
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003475e_inval:
3476 return -EINVAL;
3477}
3478
3479/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003480 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3481 * specified.
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003482 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003483static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003484{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003485 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003486 struct ceph_osd_request *req;
3487 struct MOSDOpReply m;
3488 u64 tid = le64_to_cpu(msg->hdr.tid);
3489 u32 data_len = 0;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003490 int ret;
3491 int i;
3492
3493 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3494
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003495 down_read(&osdc->lock);
3496 if (!osd_registered(osd)) {
3497 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3498 goto out_unlock_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003499 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003500 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3501
3502 mutex_lock(&osd->lock);
3503 req = lookup_request(&osd->o_requests, tid);
3504 if (!req) {
3505 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3506 goto out_unlock_session;
3507 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003508
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003509 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003510 ret = decode_MOSDOpReply(msg, &m);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003511 m.redirect.oloc.pool_ns = NULL;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003512 if (ret) {
3513 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3514 req->r_tid, ret);
3515 ceph_msg_dump(msg);
3516 goto fail_request;
3517 }
3518 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3519 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3520 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3521 le64_to_cpu(m.replay_version.version), m.user_version);
3522
3523 if (m.retry_attempt >= 0) {
3524 if (m.retry_attempt != req->r_attempts - 1) {
3525 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3526 req, req->r_tid, m.retry_attempt,
3527 req->r_attempts - 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003528 goto out_unlock_session;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003529 }
3530 } else {
3531 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3532 }
3533
3534 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3535 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3536 m.redirect.oloc.pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003537 unlink_request(osd, req);
3538 mutex_unlock(&osd->lock);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003539
Yan, Zheng30c156d2016-02-14 11:24:31 +08003540 /*
3541 * Not ceph_oloc_copy() - changing pool_ns is not
3542 * supported.
3543 */
3544 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003545 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3546 req->r_tid = 0;
3547 __submit_request(req, false);
3548 goto out_unlock_osdc;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003549 }
3550
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003551 if (m.num_ops != req->r_num_ops) {
3552 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3553 req->r_num_ops, req->r_tid);
3554 goto fail_request;
3555 }
3556 for (i = 0; i < req->r_num_ops; i++) {
3557 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3558 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3559 req->r_ops[i].rval = m.rval[i];
3560 req->r_ops[i].outdata_len = m.outdata_len[i];
3561 data_len += m.outdata_len[i];
3562 }
3563 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3564 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3565 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3566 goto fail_request;
3567 }
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003568 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3569 req, req->r_tid, m.result, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07003570
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003571 /*
3572 * Since we only ever request ONDISK, we should only ever get
3573 * one (type of) reply back.
3574 */
3575 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3576 req->r_result = m.result ?: data_len;
3577 finish_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003578 mutex_unlock(&osd->lock);
3579 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003580
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003581 __complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07003582 return;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003583
3584fail_request:
Ilya Dryomov46092452016-04-28 16:07:27 +02003585 complete_request(req, -EIO);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003586out_unlock_session:
3587 mutex_unlock(&osd->lock);
3588out_unlock_osdc:
3589 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003590}
3591
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003592static void set_pool_was_full(struct ceph_osd_client *osdc)
3593{
3594 struct rb_node *n;
3595
3596 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3597 struct ceph_pg_pool_info *pi =
3598 rb_entry(n, struct ceph_pg_pool_info, node);
3599
3600 pi->was_full = __pool_full(pi);
3601 }
3602}
3603
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003604static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
Sage Weilf24e9982009-10-06 11:31:10 -07003605{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003606 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07003607
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003608 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3609 if (!pi)
3610 return false;
Sage Weilf24e9982009-10-06 11:31:10 -07003611
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003612 return pi->was_full && !__pool_full(pi);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003613}
3614
Ilya Dryomov922dab62016-05-26 01:15:02 +02003615static enum calc_target_result
3616recalc_linger_target(struct ceph_osd_linger_request *lreq)
3617{
3618 struct ceph_osd_client *osdc = lreq->osdc;
3619 enum calc_target_result ct_res;
3620
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003621 ct_res = calc_target(osdc, &lreq->t, NULL, true);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003622 if (ct_res == CALC_TARGET_NEED_RESEND) {
3623 struct ceph_osd *osd;
3624
3625 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3626 if (osd != lreq->osd) {
3627 unlink_linger(lreq->osd, lreq);
3628 link_linger(osd, lreq);
3629 }
3630 }
3631
3632 return ct_res;
3633}
3634
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003635/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003636 * Requeue requests whose mapping to an OSD has changed.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003637 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003638static void scan_requests(struct ceph_osd *osd,
3639 bool force_resend,
3640 bool cleared_full,
3641 bool check_pool_cleared_full,
3642 struct rb_root *need_resend,
3643 struct list_head *need_resend_linger)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003644{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003645 struct ceph_osd_client *osdc = osd->o_osdc;
3646 struct rb_node *n;
3647 bool force_resend_writes;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003648
Ilya Dryomov922dab62016-05-26 01:15:02 +02003649 for (n = rb_first(&osd->o_linger_requests); n; ) {
3650 struct ceph_osd_linger_request *lreq =
3651 rb_entry(n, struct ceph_osd_linger_request, node);
3652 enum calc_target_result ct_res;
3653
3654 n = rb_next(n); /* recalc_linger_target() */
3655
3656 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3657 lreq->linger_id);
3658 ct_res = recalc_linger_target(lreq);
3659 switch (ct_res) {
3660 case CALC_TARGET_NO_ACTION:
3661 force_resend_writes = cleared_full ||
3662 (check_pool_cleared_full &&
3663 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3664 if (!force_resend && !force_resend_writes)
3665 break;
3666
3667 /* fall through */
3668 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003669 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003670 /*
3671 * scan_requests() for the previous epoch(s)
3672 * may have already added it to the list, since
3673 * it's not unlinked here.
3674 */
3675 if (list_empty(&lreq->scan_item))
3676 list_add_tail(&lreq->scan_item, need_resend_linger);
3677 break;
3678 case CALC_TARGET_POOL_DNE:
Ilya Dryomova10bcb12017-06-15 16:30:55 +02003679 list_del_init(&lreq->scan_item);
Ilya Dryomov46092452016-04-28 16:07:27 +02003680 check_linger_pool_dne(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003681 break;
3682 }
3683 }
3684
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003685 for (n = rb_first(&osd->o_requests); n; ) {
3686 struct ceph_osd_request *req =
3687 rb_entry(n, struct ceph_osd_request, r_node);
3688 enum calc_target_result ct_res;
Alex Elderab60b162012-12-19 15:52:36 -06003689
Ilya Dryomov46092452016-04-28 16:07:27 +02003690 n = rb_next(n); /* unlink_request(), check_pool_dne() */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003691
3692 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003693 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3694 false);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003695 switch (ct_res) {
3696 case CALC_TARGET_NO_ACTION:
3697 force_resend_writes = cleared_full ||
3698 (check_pool_cleared_full &&
3699 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3700 if (!force_resend &&
3701 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3702 !force_resend_writes))
3703 break;
3704
3705 /* fall through */
3706 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003707 cancel_map_check(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003708 unlink_request(osd, req);
3709 insert_request(need_resend, req);
3710 break;
3711 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003712 check_pool_dne(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003713 break;
Alex Elderab60b162012-12-19 15:52:36 -06003714 }
Sage Weilf24e9982009-10-06 11:31:10 -07003715 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003716}
Sage Weil6f6c7002011-01-17 20:34:08 -08003717
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003718static int handle_one_map(struct ceph_osd_client *osdc,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003719 void *p, void *end, bool incremental,
3720 struct rb_root *need_resend,
3721 struct list_head *need_resend_linger)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003722{
3723 struct ceph_osdmap *newmap;
3724 struct rb_node *n;
3725 bool skipped_map = false;
3726 bool was_full;
3727
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003728 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003729 set_pool_was_full(osdc);
3730
3731 if (incremental)
3732 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3733 else
3734 newmap = ceph_osdmap_decode(&p, end);
3735 if (IS_ERR(newmap))
3736 return PTR_ERR(newmap);
3737
3738 if (newmap != osdc->osdmap) {
3739 /*
3740 * Preserve ->was_full before destroying the old map.
3741 * For pools that weren't in the old map, ->was_full
3742 * should be false.
3743 */
3744 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3745 struct ceph_pg_pool_info *pi =
3746 rb_entry(n, struct ceph_pg_pool_info, node);
3747 struct ceph_pg_pool_info *old_pi;
3748
3749 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3750 if (old_pi)
3751 pi->was_full = old_pi->was_full;
3752 else
3753 WARN_ON(pi->was_full);
3754 }
3755
3756 if (osdc->osdmap->epoch &&
3757 osdc->osdmap->epoch + 1 < newmap->epoch) {
3758 WARN_ON(incremental);
3759 skipped_map = true;
3760 }
3761
3762 ceph_osdmap_destroy(osdc->osdmap);
3763 osdc->osdmap = newmap;
3764 }
3765
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003766 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003767 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3768 need_resend, need_resend_linger);
3769
3770 for (n = rb_first(&osdc->osds); n; ) {
3771 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3772
3773 n = rb_next(n); /* close_osd() */
3774
3775 scan_requests(osd, skipped_map, was_full, true, need_resend,
3776 need_resend_linger);
3777 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3778 memcmp(&osd->o_con.peer_addr,
3779 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3780 sizeof(struct ceph_entity_addr)))
3781 close_osd(osd);
3782 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003783
3784 return 0;
3785}
Sage Weil6f6c7002011-01-17 20:34:08 -08003786
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003787static void kick_requests(struct ceph_osd_client *osdc,
3788 struct rb_root *need_resend,
3789 struct list_head *need_resend_linger)
3790{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003791 struct ceph_osd_linger_request *lreq, *nlreq;
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003792 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003793 struct rb_node *n;
3794
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003795 /* make sure need_resend targets reflect latest map */
3796 for (n = rb_first(need_resend); n; ) {
3797 struct ceph_osd_request *req =
3798 rb_entry(n, struct ceph_osd_request, r_node);
3799
3800 n = rb_next(n);
3801
3802 if (req->r_t.epoch < osdc->osdmap->epoch) {
3803 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3804 if (ct_res == CALC_TARGET_POOL_DNE) {
3805 erase_request(need_resend, req);
3806 check_pool_dne(req);
3807 }
3808 }
3809 }
3810
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003811 for (n = rb_first(need_resend); n; ) {
3812 struct ceph_osd_request *req =
3813 rb_entry(n, struct ceph_osd_request, r_node);
3814 struct ceph_osd *osd;
3815
3816 n = rb_next(n);
3817 erase_request(need_resend, req); /* before link_request() */
3818
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003819 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3820 link_request(osd, req);
3821 if (!req->r_linger) {
3822 if (!osd_homeless(osd) && !req->r_t.paused)
3823 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003824 } else {
3825 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003826 }
3827 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003828
3829 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3830 if (!osd_homeless(lreq->osd))
3831 send_linger(lreq);
3832
3833 list_del_init(&lreq->scan_item);
3834 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003835}
3836
Sage Weilf24e9982009-10-06 11:31:10 -07003837/*
3838 * Process updated osd map.
3839 *
3840 * The message contains any number of incremental and full maps, normally
3841 * indicating some sort of topology change in the cluster. Kick requests
3842 * off to different OSDs as needed.
3843 */
3844void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3845{
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003846 void *p = msg->front.iov_base;
3847 void *const end = p + msg->front.iov_len;
Sage Weilf24e9982009-10-06 11:31:10 -07003848 u32 nr_maps, maplen;
3849 u32 epoch;
Sage Weilf24e9982009-10-06 11:31:10 -07003850 struct ceph_fsid fsid;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003851 struct rb_root need_resend = RB_ROOT;
3852 LIST_HEAD(need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003853 bool handled_incremental = false;
3854 bool was_pauserd, was_pausewr;
3855 bool pauserd, pausewr;
3856 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07003857
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003858 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003859 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003860
3861 /* verify fsid */
3862 ceph_decode_need(&p, end, sizeof(fsid), bad);
3863 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08003864 if (ceph_check_fsid(osdc->client, &fsid) < 0)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003865 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003866
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003867 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3868 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3869 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003870 have_pool_full(osdc);
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08003871
Sage Weilf24e9982009-10-06 11:31:10 -07003872 /* incremental maps */
3873 ceph_decode_32_safe(&p, end, nr_maps, bad);
3874 dout(" %d inc maps\n", nr_maps);
3875 while (nr_maps > 0) {
3876 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003877 epoch = ceph_decode_32(&p);
3878 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003879 ceph_decode_need(&p, end, maplen, bad);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003880 if (osdc->osdmap->epoch &&
3881 osdc->osdmap->epoch + 1 == epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003882 dout("applying incremental map %u len %d\n",
3883 epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003884 err = handle_one_map(osdc, p, p + maplen, true,
3885 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003886 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003887 goto bad;
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003888 handled_incremental = true;
Sage Weilf24e9982009-10-06 11:31:10 -07003889 } else {
3890 dout("ignoring incremental map %u len %d\n",
3891 epoch, maplen);
3892 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003893 p += maplen;
Sage Weilf24e9982009-10-06 11:31:10 -07003894 nr_maps--;
3895 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003896 if (handled_incremental)
Sage Weilf24e9982009-10-06 11:31:10 -07003897 goto done;
3898
3899 /* full maps */
3900 ceph_decode_32_safe(&p, end, nr_maps, bad);
3901 dout(" %d full maps\n", nr_maps);
3902 while (nr_maps) {
3903 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003904 epoch = ceph_decode_32(&p);
3905 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003906 ceph_decode_need(&p, end, maplen, bad);
3907 if (nr_maps > 1) {
3908 dout("skipping non-latest full map %u len %d\n",
3909 epoch, maplen);
Ilya Dryomove5253a72016-04-28 16:07:25 +02003910 } else if (osdc->osdmap->epoch >= epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003911 dout("skipping full map %u len %d, "
3912 "older than our %u\n", epoch, maplen,
3913 osdc->osdmap->epoch);
3914 } else {
3915 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003916 err = handle_one_map(osdc, p, p + maplen, false,
3917 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003918 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003919 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003920 }
3921 p += maplen;
3922 nr_maps--;
3923 }
3924
3925done:
Sage Weilcd634fb2011-05-12 09:29:18 -07003926 /*
3927 * subscribe to subsequent osdmap updates if full to ensure
3928 * we find out when we are no longer full and stop returning
3929 * ENOSPC.
3930 */
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003931 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3932 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3933 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003934 have_pool_full(osdc);
Jeff Layton58eb7932017-04-18 09:21:16 -04003935 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3936 osdc->osdmap->epoch < osdc->epoch_barrier)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003937 maybe_request_map(osdc);
Sage Weilcd634fb2011-05-12 09:29:18 -07003938
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003939 kick_requests(osdc, &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003940
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04003941 ceph_osdc_abort_on_full(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003942 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3943 osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003944 up_write(&osdc->lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07003945 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07003946 return;
3947
3948bad:
3949 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003950 ceph_msg_dump(msg);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003951 up_write(&osdc->lock);
3952}
3953
3954/*
3955 * Resubmit requests pending on the given osd.
3956 */
3957static void kick_osd_requests(struct ceph_osd *osd)
3958{
3959 struct rb_node *n;
3960
Ilya Dryomova02a9462017-06-19 12:18:05 +02003961 clear_backoffs(osd);
3962
Ilya Dryomov922dab62016-05-26 01:15:02 +02003963 for (n = rb_first(&osd->o_requests); n; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003964 struct ceph_osd_request *req =
3965 rb_entry(n, struct ceph_osd_request, r_node);
3966
Ilya Dryomov922dab62016-05-26 01:15:02 +02003967 n = rb_next(n); /* cancel_linger_request() */
3968
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003969 if (!req->r_linger) {
3970 if (!req->r_t.paused)
3971 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003972 } else {
3973 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003974 }
3975 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003976 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3977 struct ceph_osd_linger_request *lreq =
3978 rb_entry(n, struct ceph_osd_linger_request, node);
3979
3980 send_linger(lreq);
3981 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003982}
3983
3984/*
3985 * If the osd connection drops, we need to resubmit all requests.
3986 */
3987static void osd_fault(struct ceph_connection *con)
3988{
3989 struct ceph_osd *osd = con->private;
3990 struct ceph_osd_client *osdc = osd->o_osdc;
3991
3992 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3993
3994 down_write(&osdc->lock);
3995 if (!osd_registered(osd)) {
3996 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3997 goto out_unlock;
3998 }
3999
4000 if (!reopen_osd(osd))
4001 kick_osd_requests(osd);
4002 maybe_request_map(osdc);
4003
4004out_unlock:
4005 up_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004006}
4007
Ilya Dryomova02a9462017-06-19 12:18:05 +02004008struct MOSDBackoff {
4009 struct ceph_spg spgid;
4010 u32 map_epoch;
4011 u8 op;
4012 u64 id;
4013 struct ceph_hobject_id *begin;
4014 struct ceph_hobject_id *end;
4015};
4016
4017static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4018{
4019 void *p = msg->front.iov_base;
4020 void *const end = p + msg->front.iov_len;
4021 u8 struct_v;
4022 u32 struct_len;
4023 int ret;
4024
4025 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4026 if (ret)
4027 return ret;
4028
4029 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4030 if (ret)
4031 return ret;
4032
4033 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4034 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4035 ceph_decode_8_safe(&p, end, m->op, e_inval);
4036 ceph_decode_64_safe(&p, end, m->id, e_inval);
4037
4038 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4039 if (!m->begin)
4040 return -ENOMEM;
4041
4042 ret = decode_hoid(&p, end, m->begin);
4043 if (ret) {
4044 free_hoid(m->begin);
4045 return ret;
4046 }
4047
4048 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4049 if (!m->end) {
4050 free_hoid(m->begin);
4051 return -ENOMEM;
4052 }
4053
4054 ret = decode_hoid(&p, end, m->end);
4055 if (ret) {
4056 free_hoid(m->begin);
4057 free_hoid(m->end);
4058 return ret;
4059 }
4060
4061 return 0;
4062
4063e_inval:
4064 return -EINVAL;
4065}
4066
4067static struct ceph_msg *create_backoff_message(
4068 const struct ceph_osd_backoff *backoff,
4069 u32 map_epoch)
4070{
4071 struct ceph_msg *msg;
4072 void *p, *end;
4073 int msg_size;
4074
4075 msg_size = CEPH_ENCODING_START_BLK_LEN +
4076 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4077 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4078 msg_size += CEPH_ENCODING_START_BLK_LEN +
4079 hoid_encoding_size(backoff->begin);
4080 msg_size += CEPH_ENCODING_START_BLK_LEN +
4081 hoid_encoding_size(backoff->end);
4082
4083 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4084 if (!msg)
4085 return NULL;
4086
4087 p = msg->front.iov_base;
4088 end = p + msg->front_alloc_len;
4089
4090 encode_spgid(&p, &backoff->spgid);
4091 ceph_encode_32(&p, map_epoch);
4092 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4093 ceph_encode_64(&p, backoff->id);
4094 encode_hoid(&p, end, backoff->begin);
4095 encode_hoid(&p, end, backoff->end);
4096 BUG_ON(p != end);
4097
4098 msg->front.iov_len = p - msg->front.iov_base;
4099 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4100 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4101
4102 return msg;
4103}
4104
4105static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4106{
4107 struct ceph_spg_mapping *spg;
4108 struct ceph_osd_backoff *backoff;
4109 struct ceph_msg *msg;
4110
4111 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4112 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4113
4114 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4115 if (!spg) {
4116 spg = alloc_spg_mapping();
4117 if (!spg) {
4118 pr_err("%s failed to allocate spg\n", __func__);
4119 return;
4120 }
4121 spg->spgid = m->spgid; /* struct */
4122 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4123 }
4124
4125 backoff = alloc_backoff();
4126 if (!backoff) {
4127 pr_err("%s failed to allocate backoff\n", __func__);
4128 return;
4129 }
4130 backoff->spgid = m->spgid; /* struct */
4131 backoff->id = m->id;
4132 backoff->begin = m->begin;
4133 m->begin = NULL; /* backoff now owns this */
4134 backoff->end = m->end;
4135 m->end = NULL; /* ditto */
4136
4137 insert_backoff(&spg->backoffs, backoff);
4138 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4139
4140 /*
4141 * Ack with original backoff's epoch so that the OSD can
4142 * discard this if there was a PG split.
4143 */
4144 msg = create_backoff_message(backoff, m->map_epoch);
4145 if (!msg) {
4146 pr_err("%s failed to allocate msg\n", __func__);
4147 return;
4148 }
4149 ceph_con_send(&osd->o_con, msg);
4150}
4151
4152static bool target_contained_by(const struct ceph_osd_request_target *t,
4153 const struct ceph_hobject_id *begin,
4154 const struct ceph_hobject_id *end)
4155{
4156 struct ceph_hobject_id hoid;
4157 int cmp;
4158
4159 hoid_fill_from_target(&hoid, t);
4160 cmp = hoid_compare(&hoid, begin);
4161 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4162}
4163
4164static void handle_backoff_unblock(struct ceph_osd *osd,
4165 const struct MOSDBackoff *m)
4166{
4167 struct ceph_spg_mapping *spg;
4168 struct ceph_osd_backoff *backoff;
4169 struct rb_node *n;
4170
4171 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4172 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4173
4174 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4175 if (!backoff) {
4176 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4177 __func__, osd->o_osd, m->spgid.pgid.pool,
4178 m->spgid.pgid.seed, m->spgid.shard, m->id);
4179 return;
4180 }
4181
4182 if (hoid_compare(backoff->begin, m->begin) &&
4183 hoid_compare(backoff->end, m->end)) {
4184 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4185 __func__, osd->o_osd, m->spgid.pgid.pool,
4186 m->spgid.pgid.seed, m->spgid.shard, m->id);
4187 /* unblock it anyway... */
4188 }
4189
4190 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4191 BUG_ON(!spg);
4192
4193 erase_backoff(&spg->backoffs, backoff);
4194 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4195 free_backoff(backoff);
4196
4197 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4198 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4199 free_spg_mapping(spg);
4200 }
4201
4202 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4203 struct ceph_osd_request *req =
4204 rb_entry(n, struct ceph_osd_request, r_node);
4205
4206 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4207 /*
4208 * Match against @m, not @backoff -- the PG may
4209 * have split on the OSD.
4210 */
4211 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4212 /*
4213 * If no other installed backoff applies,
4214 * resend.
4215 */
4216 send_request(req);
4217 }
4218 }
4219 }
4220}
4221
4222static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4223{
4224 struct ceph_osd_client *osdc = osd->o_osdc;
4225 struct MOSDBackoff m;
4226 int ret;
4227
4228 down_read(&osdc->lock);
4229 if (!osd_registered(osd)) {
4230 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4231 up_read(&osdc->lock);
4232 return;
4233 }
4234 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4235
4236 mutex_lock(&osd->lock);
4237 ret = decode_MOSDBackoff(msg, &m);
4238 if (ret) {
4239 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4240 ceph_msg_dump(msg);
4241 goto out_unlock;
4242 }
4243
4244 switch (m.op) {
4245 case CEPH_OSD_BACKOFF_OP_BLOCK:
4246 handle_backoff_block(osd, &m);
4247 break;
4248 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4249 handle_backoff_unblock(osd, &m);
4250 break;
4251 default:
4252 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4253 }
4254
4255 free_hoid(m.begin);
4256 free_hoid(m.end);
4257
4258out_unlock:
4259 mutex_unlock(&osd->lock);
4260 up_read(&osdc->lock);
4261}
4262
Sage Weilf24e9982009-10-06 11:31:10 -07004263/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004264 * Process osd watch notifications
4265 */
Alex Elder3c663bb2013-02-15 11:42:30 -06004266static void handle_watch_notify(struct ceph_osd_client *osdc,
4267 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004268{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004269 void *p = msg->front.iov_base;
4270 void *const end = p + msg->front.iov_len;
4271 struct ceph_osd_linger_request *lreq;
4272 struct linger_work *lwork;
4273 u8 proto_ver, opcode;
4274 u64 cookie, notify_id;
4275 u64 notifier_id = 0;
Ilya Dryomov19079202016-04-28 16:07:27 +02004276 s32 return_code = 0;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004277 void *payload = NULL;
4278 u32 payload_len = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004279
4280 ceph_decode_8_safe(&p, end, proto_ver, bad);
4281 ceph_decode_8_safe(&p, end, opcode, bad);
4282 ceph_decode_64_safe(&p, end, cookie, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004283 p += 8; /* skip ver */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004284 ceph_decode_64_safe(&p, end, notify_id, bad);
4285
Ilya Dryomov922dab62016-05-26 01:15:02 +02004286 if (proto_ver >= 1) {
4287 ceph_decode_32_safe(&p, end, payload_len, bad);
4288 ceph_decode_need(&p, end, payload_len, bad);
4289 payload = p;
4290 p += payload_len;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004291 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004292
4293 if (le16_to_cpu(msg->hdr.version) >= 2)
Ilya Dryomov19079202016-04-28 16:07:27 +02004294 ceph_decode_32_safe(&p, end, return_code, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004295
4296 if (le16_to_cpu(msg->hdr.version) >= 3)
4297 ceph_decode_64_safe(&p, end, notifier_id, bad);
4298
4299 down_read(&osdc->lock);
4300 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4301 if (!lreq) {
4302 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4303 cookie);
4304 goto out_unlock_osdc;
4305 }
4306
4307 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02004308 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4309 opcode, cookie, lreq, lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004310 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4311 if (!lreq->last_error) {
4312 lreq->last_error = -ENOTCONN;
4313 queue_watch_error(lreq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004314 }
Ilya Dryomov19079202016-04-28 16:07:27 +02004315 } else if (!lreq->is_watch) {
4316 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4317 if (lreq->notify_id && lreq->notify_id != notify_id) {
4318 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4319 lreq->notify_id, notify_id);
4320 } else if (!completion_done(&lreq->notify_finish_wait)) {
4321 struct ceph_msg_data *data =
4322 list_first_entry_or_null(&msg->data,
4323 struct ceph_msg_data,
4324 links);
4325
4326 if (data) {
4327 if (lreq->preply_pages) {
4328 WARN_ON(data->type !=
4329 CEPH_MSG_DATA_PAGES);
4330 *lreq->preply_pages = data->pages;
4331 *lreq->preply_len = data->length;
4332 } else {
4333 ceph_release_page_vector(data->pages,
4334 calc_pages_for(0, data->length));
4335 }
4336 }
4337 lreq->notify_finish_error = return_code;
4338 complete_all(&lreq->notify_finish_wait);
4339 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004340 } else {
4341 /* CEPH_WATCH_EVENT_NOTIFY */
4342 lwork = lwork_alloc(lreq, do_watch_notify);
4343 if (!lwork) {
4344 pr_err("failed to allocate notify-lwork\n");
4345 goto out_unlock_lreq;
4346 }
Ilya Dryomov91883cd2014-09-11 12:18:53 +04004347
Ilya Dryomov922dab62016-05-26 01:15:02 +02004348 lwork->notify.notify_id = notify_id;
4349 lwork->notify.notifier_id = notifier_id;
4350 lwork->notify.payload = payload;
4351 lwork->notify.payload_len = payload_len;
4352 lwork->notify.msg = ceph_msg_get(msg);
4353 lwork_queue(lwork);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004354 }
4355
Ilya Dryomov922dab62016-05-26 01:15:02 +02004356out_unlock_lreq:
4357 mutex_unlock(&lreq->lock);
4358out_unlock_osdc:
4359 up_read(&osdc->lock);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004360 return;
4361
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004362bad:
4363 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004364}
4365
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004366/*
Sage Weilf24e9982009-10-06 11:31:10 -07004367 * Register request, send initial attempt.
4368 */
4369int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4370 struct ceph_osd_request *req,
4371 bool nofail)
4372{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004373 down_read(&osdc->lock);
4374 submit_request(req, false);
4375 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004376
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004377 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07004378}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004379EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07004380
4381/*
Ilya Dryomovc297eb422016-12-02 14:01:55 +01004382 * Unregister a registered request. The request is not completed:
4383 * ->r_result isn't set and __complete_request() isn't called.
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04004384 */
4385void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4386{
4387 struct ceph_osd_client *osdc = req->r_osdc;
4388
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004389 down_write(&osdc->lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004390 if (req->r_osd)
4391 cancel_request(req);
4392 up_write(&osdc->lock);
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04004393}
4394EXPORT_SYMBOL(ceph_osdc_cancel_request);
4395
4396/*
Ilya Dryomov42b06962016-04-28 16:07:26 +02004397 * @timeout: in jiffies, 0 means "wait forever"
4398 */
4399static int wait_request_timeout(struct ceph_osd_request *req,
4400 unsigned long timeout)
4401{
4402 long left;
4403
4404 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Yan, Zheng0e76abf2016-05-13 11:04:33 +08004405 left = wait_for_completion_killable_timeout(&req->r_completion,
Ilya Dryomov42b06962016-04-28 16:07:26 +02004406 ceph_timeout_jiffies(timeout));
4407 if (left <= 0) {
4408 left = left ?: -ETIMEDOUT;
4409 ceph_osdc_cancel_request(req);
Ilya Dryomov42b06962016-04-28 16:07:26 +02004410 } else {
4411 left = req->r_result; /* completed */
4412 }
4413
4414 return left;
4415}
4416
4417/*
Sage Weilf24e9982009-10-06 11:31:10 -07004418 * wait for a request to complete
4419 */
4420int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4421 struct ceph_osd_request *req)
4422{
Ilya Dryomov42b06962016-04-28 16:07:26 +02004423 return wait_request_timeout(req, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07004424}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004425EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07004426
4427/*
4428 * sync - wait for all in-flight requests to flush. avoid starvation.
4429 */
4430void ceph_osdc_sync(struct ceph_osd_client *osdc)
4431{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004432 struct rb_node *n, *p;
4433 u64 last_tid = atomic64_read(&osdc->last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004434
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004435again:
4436 down_read(&osdc->lock);
4437 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4438 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07004439
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004440 mutex_lock(&osd->lock);
4441 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4442 struct ceph_osd_request *req =
4443 rb_entry(p, struct ceph_osd_request, r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07004444
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004445 if (req->r_tid > last_tid)
4446 break;
4447
4448 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4449 continue;
4450
4451 ceph_osdc_get_request(req);
4452 mutex_unlock(&osd->lock);
4453 up_read(&osdc->lock);
4454 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4455 __func__, req, req->r_tid, last_tid);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01004456 wait_for_completion(&req->r_completion);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004457 ceph_osdc_put_request(req);
4458 goto again;
4459 }
4460
4461 mutex_unlock(&osd->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004462 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004463
4464 up_read(&osdc->lock);
4465 dout("%s done last_tid %llu\n", __func__, last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004466}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004467EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07004468
Ilya Dryomov922dab62016-05-26 01:15:02 +02004469static struct ceph_osd_request *
4470alloc_linger_request(struct ceph_osd_linger_request *lreq)
4471{
4472 struct ceph_osd_request *req;
4473
4474 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4475 if (!req)
4476 return NULL;
4477
4478 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4479 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4480
4481 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4482 ceph_osdc_put_request(req);
4483 return NULL;
4484 }
4485
4486 return req;
4487}
4488
4489/*
4490 * Returns a handle, caller owns a ref.
4491 */
4492struct ceph_osd_linger_request *
4493ceph_osdc_watch(struct ceph_osd_client *osdc,
4494 struct ceph_object_id *oid,
4495 struct ceph_object_locator *oloc,
4496 rados_watchcb2_t wcb,
4497 rados_watcherrcb_t errcb,
4498 void *data)
4499{
4500 struct ceph_osd_linger_request *lreq;
4501 int ret;
4502
4503 lreq = linger_alloc(osdc);
4504 if (!lreq)
4505 return ERR_PTR(-ENOMEM);
4506
Ilya Dryomov19079202016-04-28 16:07:27 +02004507 lreq->is_watch = true;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004508 lreq->wcb = wcb;
4509 lreq->errcb = errcb;
4510 lreq->data = data;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004511 lreq->watch_valid_thru = jiffies;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004512
4513 ceph_oid_copy(&lreq->t.base_oid, oid);
4514 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004515 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07004516 ktime_get_real_ts(&lreq->mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004517
4518 lreq->reg_req = alloc_linger_request(lreq);
4519 if (!lreq->reg_req) {
4520 ret = -ENOMEM;
4521 goto err_put_lreq;
4522 }
4523
4524 lreq->ping_req = alloc_linger_request(lreq);
4525 if (!lreq->ping_req) {
4526 ret = -ENOMEM;
4527 goto err_put_lreq;
4528 }
4529
4530 down_write(&osdc->lock);
4531 linger_register(lreq); /* before osd_req_op_* */
4532 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4533 CEPH_OSD_WATCH_OP_WATCH);
4534 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4535 CEPH_OSD_WATCH_OP_PING);
4536 linger_submit(lreq);
4537 up_write(&osdc->lock);
4538
4539 ret = linger_reg_commit_wait(lreq);
4540 if (ret) {
4541 linger_cancel(lreq);
4542 goto err_put_lreq;
4543 }
4544
4545 return lreq;
4546
4547err_put_lreq:
4548 linger_put(lreq);
4549 return ERR_PTR(ret);
4550}
4551EXPORT_SYMBOL(ceph_osdc_watch);
4552
4553/*
4554 * Releases a ref.
4555 *
4556 * Times out after mount_timeout to preserve rbd unmap behaviour
4557 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4558 * with mount_timeout").
4559 */
4560int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4561 struct ceph_osd_linger_request *lreq)
4562{
4563 struct ceph_options *opts = osdc->client->options;
4564 struct ceph_osd_request *req;
4565 int ret;
4566
4567 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4568 if (!req)
4569 return -ENOMEM;
4570
4571 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4572 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004573 req->r_flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07004574 ktime_get_real_ts(&req->r_mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004575 osd_req_op_watch_init(req, 0, lreq->linger_id,
4576 CEPH_OSD_WATCH_OP_UNWATCH);
4577
4578 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4579 if (ret)
4580 goto out_put_req;
4581
4582 ceph_osdc_start_request(osdc, req, false);
4583 linger_cancel(lreq);
4584 linger_put(lreq);
4585 ret = wait_request_timeout(req, opts->mount_timeout);
4586
4587out_put_req:
4588 ceph_osdc_put_request(req);
4589 return ret;
4590}
4591EXPORT_SYMBOL(ceph_osdc_unwatch);
4592
4593static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4594 u64 notify_id, u64 cookie, void *payload,
4595 size_t payload_len)
4596{
4597 struct ceph_osd_req_op *op;
4598 struct ceph_pagelist *pl;
4599 int ret;
4600
4601 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4602
4603 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4604 if (!pl)
4605 return -ENOMEM;
4606
4607 ceph_pagelist_init(pl);
4608 ret = ceph_pagelist_encode_64(pl, notify_id);
4609 ret |= ceph_pagelist_encode_64(pl, cookie);
4610 if (payload) {
4611 ret |= ceph_pagelist_encode_32(pl, payload_len);
4612 ret |= ceph_pagelist_append(pl, payload, payload_len);
4613 } else {
4614 ret |= ceph_pagelist_encode_32(pl, 0);
4615 }
4616 if (ret) {
4617 ceph_pagelist_release(pl);
4618 return -ENOMEM;
4619 }
4620
4621 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4622 op->indata_len = pl->length;
4623 return 0;
4624}
4625
4626int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4627 struct ceph_object_id *oid,
4628 struct ceph_object_locator *oloc,
4629 u64 notify_id,
4630 u64 cookie,
4631 void *payload,
4632 size_t payload_len)
4633{
4634 struct ceph_osd_request *req;
4635 int ret;
4636
4637 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4638 if (!req)
4639 return -ENOMEM;
4640
4641 ceph_oid_copy(&req->r_base_oid, oid);
4642 ceph_oloc_copy(&req->r_base_oloc, oloc);
4643 req->r_flags = CEPH_OSD_FLAG_READ;
4644
4645 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4646 if (ret)
4647 goto out_put_req;
4648
4649 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4650 payload_len);
4651 if (ret)
4652 goto out_put_req;
4653
4654 ceph_osdc_start_request(osdc, req, false);
4655 ret = ceph_osdc_wait_request(osdc, req);
4656
4657out_put_req:
4658 ceph_osdc_put_request(req);
4659 return ret;
4660}
4661EXPORT_SYMBOL(ceph_osdc_notify_ack);
4662
Ilya Dryomov19079202016-04-28 16:07:27 +02004663static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4664 u64 cookie, u32 prot_ver, u32 timeout,
4665 void *payload, size_t payload_len)
4666{
4667 struct ceph_osd_req_op *op;
4668 struct ceph_pagelist *pl;
4669 int ret;
4670
4671 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4672 op->notify.cookie = cookie;
4673
4674 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4675 if (!pl)
4676 return -ENOMEM;
4677
4678 ceph_pagelist_init(pl);
4679 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4680 ret |= ceph_pagelist_encode_32(pl, timeout);
4681 ret |= ceph_pagelist_encode_32(pl, payload_len);
4682 ret |= ceph_pagelist_append(pl, payload, payload_len);
4683 if (ret) {
4684 ceph_pagelist_release(pl);
4685 return -ENOMEM;
4686 }
4687
4688 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4689 op->indata_len = pl->length;
4690 return 0;
4691}
4692
4693/*
4694 * @timeout: in seconds
4695 *
4696 * @preply_{pages,len} are initialized both on success and error.
4697 * The caller is responsible for:
4698 *
4699 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4700 */
4701int ceph_osdc_notify(struct ceph_osd_client *osdc,
4702 struct ceph_object_id *oid,
4703 struct ceph_object_locator *oloc,
4704 void *payload,
4705 size_t payload_len,
4706 u32 timeout,
4707 struct page ***preply_pages,
4708 size_t *preply_len)
4709{
4710 struct ceph_osd_linger_request *lreq;
4711 struct page **pages;
4712 int ret;
4713
4714 WARN_ON(!timeout);
4715 if (preply_pages) {
4716 *preply_pages = NULL;
4717 *preply_len = 0;
4718 }
4719
4720 lreq = linger_alloc(osdc);
4721 if (!lreq)
4722 return -ENOMEM;
4723
4724 lreq->preply_pages = preply_pages;
4725 lreq->preply_len = preply_len;
4726
4727 ceph_oid_copy(&lreq->t.base_oid, oid);
4728 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4729 lreq->t.flags = CEPH_OSD_FLAG_READ;
4730
4731 lreq->reg_req = alloc_linger_request(lreq);
4732 if (!lreq->reg_req) {
4733 ret = -ENOMEM;
4734 goto out_put_lreq;
4735 }
4736
4737 /* for notify_id */
4738 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4739 if (IS_ERR(pages)) {
4740 ret = PTR_ERR(pages);
4741 goto out_put_lreq;
4742 }
4743
4744 down_write(&osdc->lock);
4745 linger_register(lreq); /* before osd_req_op_* */
4746 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4747 timeout, payload, payload_len);
4748 if (ret) {
4749 linger_unregister(lreq);
4750 up_write(&osdc->lock);
4751 ceph_release_page_vector(pages, 1);
4752 goto out_put_lreq;
4753 }
4754 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4755 response_data),
4756 pages, PAGE_SIZE, 0, false, true);
4757 linger_submit(lreq);
4758 up_write(&osdc->lock);
4759
4760 ret = linger_reg_commit_wait(lreq);
4761 if (!ret)
4762 ret = linger_notify_finish_wait(lreq);
4763 else
4764 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4765
4766 linger_cancel(lreq);
4767out_put_lreq:
4768 linger_put(lreq);
4769 return ret;
4770}
4771EXPORT_SYMBOL(ceph_osdc_notify);
4772
Sage Weilf24e9982009-10-06 11:31:10 -07004773/*
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004774 * Return the number of milliseconds since the watch was last
4775 * confirmed, or an error. If there is an error, the watch is no
4776 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4777 */
4778int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4779 struct ceph_osd_linger_request *lreq)
4780{
4781 unsigned long stamp, age;
4782 int ret;
4783
4784 down_read(&osdc->lock);
4785 mutex_lock(&lreq->lock);
4786 stamp = lreq->watch_valid_thru;
4787 if (!list_empty(&lreq->pending_lworks)) {
4788 struct linger_work *lwork =
4789 list_first_entry(&lreq->pending_lworks,
4790 struct linger_work,
4791 pending_item);
4792
4793 if (time_before(lwork->queued_stamp, stamp))
4794 stamp = lwork->queued_stamp;
4795 }
4796 age = jiffies - stamp;
4797 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4798 lreq, lreq->linger_id, age, lreq->last_error);
4799 /* we are truncating to msecs, so return a safe upper bound */
4800 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4801
4802 mutex_unlock(&lreq->lock);
4803 up_read(&osdc->lock);
4804 return ret;
4805}
4806
Douglas Fullera4ed38d2015-07-17 13:18:07 -07004807static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4808{
4809 u8 struct_v;
4810 u32 struct_len;
4811 int ret;
4812
4813 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4814 &struct_v, &struct_len);
4815 if (ret)
4816 return ret;
4817
4818 ceph_decode_copy(p, &item->name, sizeof(item->name));
4819 item->cookie = ceph_decode_64(p);
4820 *p += 4; /* skip timeout_seconds */
4821 if (struct_v >= 2) {
4822 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4823 ceph_decode_addr(&item->addr);
4824 }
4825
4826 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4827 ENTITY_NAME(item->name), item->cookie,
4828 ceph_pr_addr(&item->addr.in_addr));
4829 return 0;
4830}
4831
4832static int decode_watchers(void **p, void *end,
4833 struct ceph_watch_item **watchers,
4834 u32 *num_watchers)
4835{
4836 u8 struct_v;
4837 u32 struct_len;
4838 int i;
4839 int ret;
4840
4841 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4842 &struct_v, &struct_len);
4843 if (ret)
4844 return ret;
4845
4846 *num_watchers = ceph_decode_32(p);
4847 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4848 if (!*watchers)
4849 return -ENOMEM;
4850
4851 for (i = 0; i < *num_watchers; i++) {
4852 ret = decode_watcher(p, end, *watchers + i);
4853 if (ret) {
4854 kfree(*watchers);
4855 return ret;
4856 }
4857 }
4858
4859 return 0;
4860}
4861
4862/*
4863 * On success, the caller is responsible for:
4864 *
4865 * kfree(watchers);
4866 */
4867int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4868 struct ceph_object_id *oid,
4869 struct ceph_object_locator *oloc,
4870 struct ceph_watch_item **watchers,
4871 u32 *num_watchers)
4872{
4873 struct ceph_osd_request *req;
4874 struct page **pages;
4875 int ret;
4876
4877 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4878 if (!req)
4879 return -ENOMEM;
4880
4881 ceph_oid_copy(&req->r_base_oid, oid);
4882 ceph_oloc_copy(&req->r_base_oloc, oloc);
4883 req->r_flags = CEPH_OSD_FLAG_READ;
4884
4885 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4886 if (ret)
4887 goto out_put_req;
4888
4889 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4890 if (IS_ERR(pages)) {
4891 ret = PTR_ERR(pages);
4892 goto out_put_req;
4893 }
4894
4895 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4896 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4897 response_data),
4898 pages, PAGE_SIZE, 0, false, true);
4899
4900 ceph_osdc_start_request(osdc, req, false);
4901 ret = ceph_osdc_wait_request(osdc, req);
4902 if (ret >= 0) {
4903 void *p = page_address(pages[0]);
4904 void *const end = p + req->r_ops[0].outdata_len;
4905
4906 ret = decode_watchers(&p, end, watchers, num_watchers);
4907 }
4908
4909out_put_req:
4910 ceph_osdc_put_request(req);
4911 return ret;
4912}
4913EXPORT_SYMBOL(ceph_osdc_list_watchers);
4914
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004915/*
Josh Durgindd935f42013-08-28 21:43:09 -07004916 * Call all pending notify callbacks - for use after a watch is
4917 * unregistered, to make sure no more callbacks for it will be invoked
4918 */
stephen hemmingerf64794492014-06-10 20:30:13 -07004919void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07004920{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004921 dout("%s osdc %p\n", __func__, osdc);
Josh Durgindd935f42013-08-28 21:43:09 -07004922 flush_workqueue(osdc->notify_wq);
4923}
4924EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4925
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02004926void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4927{
4928 down_read(&osdc->lock);
4929 maybe_request_map(osdc);
4930 up_read(&osdc->lock);
4931}
4932EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
Josh Durgindd935f42013-08-28 21:43:09 -07004933
4934/*
Douglas Fuller428a7152015-06-17 14:49:45 -04004935 * Execute an OSD class method on an object.
4936 *
4937 * @flags: CEPH_OSD_FLAG_*
Ilya Dryomov2544a022017-01-25 18:16:21 +01004938 * @resp_len: in/out param for reply length
Douglas Fuller428a7152015-06-17 14:49:45 -04004939 */
4940int ceph_osdc_call(struct ceph_osd_client *osdc,
4941 struct ceph_object_id *oid,
4942 struct ceph_object_locator *oloc,
4943 const char *class, const char *method,
4944 unsigned int flags,
4945 struct page *req_page, size_t req_len,
4946 struct page *resp_page, size_t *resp_len)
4947{
4948 struct ceph_osd_request *req;
4949 int ret;
4950
Ilya Dryomov2544a022017-01-25 18:16:21 +01004951 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4952 return -E2BIG;
4953
Douglas Fuller428a7152015-06-17 14:49:45 -04004954 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4955 if (!req)
4956 return -ENOMEM;
4957
4958 ceph_oid_copy(&req->r_base_oid, oid);
4959 ceph_oloc_copy(&req->r_base_oloc, oloc);
4960 req->r_flags = flags;
4961
4962 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4963 if (ret)
4964 goto out_put_req;
4965
Chengguang Xufe943d52018-04-12 12:04:55 +08004966 ret = osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4967 if (ret)
4968 goto out_put_req;
4969
Douglas Fuller428a7152015-06-17 14:49:45 -04004970 if (req_page)
4971 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4972 0, false, false);
4973 if (resp_page)
4974 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
Ilya Dryomov2544a022017-01-25 18:16:21 +01004975 *resp_len, 0, false, false);
Douglas Fuller428a7152015-06-17 14:49:45 -04004976
4977 ceph_osdc_start_request(osdc, req, false);
4978 ret = ceph_osdc_wait_request(osdc, req);
4979 if (ret >= 0) {
4980 ret = req->r_ops[0].rval;
4981 if (resp_page)
4982 *resp_len = req->r_ops[0].outdata_len;
4983 }
4984
4985out_put_req:
4986 ceph_osdc_put_request(req);
4987 return ret;
4988}
4989EXPORT_SYMBOL(ceph_osdc_call);
4990
4991/*
Sage Weilf24e9982009-10-06 11:31:10 -07004992 * init, shutdown
4993 */
4994int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4995{
4996 int err;
4997
4998 dout("init\n");
4999 osdc->client = client;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005000 init_rwsem(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07005001 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005002 INIT_LIST_HEAD(&osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02005003 spin_lock_init(&osdc->osd_lru_lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005004 osd_init(&osdc->homeless_osd);
5005 osdc->homeless_osd.o_osdc = osdc;
5006 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
Ilya Dryomov264048a2016-11-08 15:15:24 +01005007 osdc->last_linger_id = CEPH_LINGER_ID_START;
Ilya Dryomov922dab62016-05-26 01:15:02 +02005008 osdc->linger_requests = RB_ROOT;
Ilya Dryomov46092452016-04-28 16:07:27 +02005009 osdc->map_checks = RB_ROOT;
5010 osdc->linger_map_checks = RB_ROOT;
Sage Weilf24e9982009-10-06 11:31:10 -07005011 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005012 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5013
Sage Weil5f44f142009-11-18 14:52:18 -08005014 err = -ENOMEM;
Ilya Dryomove5253a72016-04-28 16:07:25 +02005015 osdc->osdmap = ceph_osdmap_alloc();
5016 if (!osdc->osdmap)
5017 goto out;
5018
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01005019 osdc->req_mempool = mempool_create_slab_pool(10,
5020 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07005021 if (!osdc->req_mempool)
Ilya Dryomove5253a72016-04-28 16:07:25 +02005022 goto out_map;
Sage Weilf24e9982009-10-06 11:31:10 -07005023
Sage Weild50b4092012-07-09 14:22:34 -07005024 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02005025 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07005026 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08005027 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07005028 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02005029 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08005030 if (err < 0)
5031 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005032
Dan Carpenterdbcae082013-08-15 08:58:59 +03005033 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005034 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03005035 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02005036 goto out_msgpool_reply;
5037
Ilya Dryomov88bc1922018-05-21 16:00:29 +02005038 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5039 if (!osdc->completion_wq)
5040 goto out_notify_wq;
5041
Ilya Dryomovfbca9632016-04-28 16:07:24 +02005042 schedule_delayed_work(&osdc->timeout_work,
5043 osdc->client->options->osd_keepalive_timeout);
Ilya Dryomovb37ee1b2016-04-28 16:07:24 +02005044 schedule_delayed_work(&osdc->osds_timeout_work,
5045 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5046
Sage Weilf24e9982009-10-06 11:31:10 -07005047 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08005048
Ilya Dryomov88bc1922018-05-21 16:00:29 +02005049out_notify_wq:
5050 destroy_workqueue(osdc->notify_wq);
Ilya Dryomovc172ec52014-01-31 17:49:22 +02005051out_msgpool_reply:
5052 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08005053out_msgpool:
5054 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08005055out_mempool:
5056 mempool_destroy(osdc->req_mempool);
Ilya Dryomove5253a72016-04-28 16:07:25 +02005057out_map:
5058 ceph_osdmap_destroy(osdc->osdmap);
Sage Weil5f44f142009-11-18 14:52:18 -08005059out:
5060 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07005061}
5062
5063void ceph_osdc_stop(struct ceph_osd_client *osdc)
5064{
Ilya Dryomov88bc1922018-05-21 16:00:29 +02005065 destroy_workqueue(osdc->completion_wq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005066 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07005067 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005068 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005069
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005070 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005071 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5072 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5073 struct ceph_osd, o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005074 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005075 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005076 up_write(&osdc->lock);
Elena Reshetova02113a02017-03-17 14:10:28 +02005077 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005078 osd_cleanup(&osdc->homeless_osd);
5079
5080 WARN_ON(!list_empty(&osdc->osd_lru));
Ilya Dryomov922dab62016-05-26 01:15:02 +02005081 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
Ilya Dryomov46092452016-04-28 16:07:27 +02005082 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5083 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005084 WARN_ON(atomic_read(&osdc->num_requests));
5085 WARN_ON(atomic_read(&osdc->num_homeless));
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005086
Ilya Dryomove5253a72016-04-28 16:07:25 +02005087 ceph_osdmap_destroy(osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07005088 mempool_destroy(osdc->req_mempool);
5089 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08005090 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07005091}
5092
5093/*
5094 * Read some contiguous pages. If we cross a stripe boundary, shorten
5095 * *plen. Return number of bytes read, or error.
5096 */
5097int ceph_osdc_readpages(struct ceph_osd_client *osdc,
5098 struct ceph_vino vino, struct ceph_file_layout *layout,
5099 u64 off, u64 *plen,
5100 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08005101 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07005102{
5103 struct ceph_osd_request *req;
5104 int rc = 0;
5105
5106 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
5107 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08005108 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07005109 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05005110 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06005111 false);
Sage Weil68162822012-09-24 21:01:02 -07005112 if (IS_ERR(req))
5113 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07005114
5115 /* it may be a short read due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05005116 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05005117 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07005118
Alex Eldere0c59482013-03-07 15:38:25 -06005119 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05005120 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07005121
5122 rc = ceph_osdc_start_request(osdc, req, false);
5123 if (!rc)
5124 rc = ceph_osdc_wait_request(osdc, req);
5125
5126 ceph_osdc_put_request(req);
5127 dout("readpages result %d\n", rc);
5128 return rc;
5129}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005130EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07005131
5132/*
5133 * do a synchronous write on N pages
5134 */
5135int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5136 struct ceph_file_layout *layout,
5137 struct ceph_snap_context *snapc,
5138 u64 off, u64 len,
5139 u32 truncate_seq, u64 truncate_size,
5140 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06005141 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07005142{
5143 struct ceph_osd_request *req;
5144 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08005145 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07005146
Yan, Zheng715e4cd2014-11-13 14:40:37 +08005147 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01005148 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05005149 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06005150 true);
Sage Weil68162822012-09-24 21:01:02 -07005151 if (IS_ERR(req))
5152 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07005153
5154 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05005155 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05005156 false, false);
5157 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07005158
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02005159 req->r_mtime = *mtime;
Alex Elder87f979d2013-02-15 11:42:29 -06005160 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07005161 if (!rc)
5162 rc = ceph_osdc_wait_request(osdc, req);
5163
5164 ceph_osdc_put_request(req);
5165 if (rc == 0)
5166 rc = len;
5167 dout("writepages result %d\n", rc);
5168 return rc;
5169}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005170EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07005171
Chengguang Xu57a35df2018-03-10 20:32:05 +08005172int __init ceph_osdc_setup(void)
Alex Elder5522ae02013-05-01 12:43:04 -05005173{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01005174 size_t size = sizeof(struct ceph_osd_request) +
5175 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5176
Alex Elder5522ae02013-05-01 12:43:04 -05005177 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01005178 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5179 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05005180
5181 return ceph_osd_request_cache ? 0 : -ENOMEM;
5182}
Alex Elder5522ae02013-05-01 12:43:04 -05005183
5184void ceph_osdc_cleanup(void)
5185{
5186 BUG_ON(!ceph_osd_request_cache);
5187 kmem_cache_destroy(ceph_osd_request_cache);
5188 ceph_osd_request_cache = NULL;
5189}
Alex Elder5522ae02013-05-01 12:43:04 -05005190
Sage Weilf24e9982009-10-06 11:31:10 -07005191/*
5192 * handle incoming message
5193 */
5194static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5195{
5196 struct ceph_osd *osd = con->private;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005197 struct ceph_osd_client *osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07005198 int type = le16_to_cpu(msg->hdr.type);
5199
Sage Weilf24e9982009-10-06 11:31:10 -07005200 switch (type) {
5201 case CEPH_MSG_OSD_MAP:
5202 ceph_osdc_handle_map(osdc, msg);
5203 break;
5204 case CEPH_MSG_OSD_OPREPLY:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005205 handle_reply(osd, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07005206 break;
Ilya Dryomova02a9462017-06-19 12:18:05 +02005207 case CEPH_MSG_OSD_BACKOFF:
5208 handle_backoff(osd, msg);
5209 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005210 case CEPH_MSG_WATCH_NOTIFY:
5211 handle_watch_notify(osdc, msg);
5212 break;
Sage Weilf24e9982009-10-06 11:31:10 -07005213
5214 default:
5215 pr_err("received unknown message type %d %s\n", type,
5216 ceph_msg_type_name(type));
5217 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005218
Sage Weilf24e9982009-10-06 11:31:10 -07005219 ceph_msg_put(msg);
5220}
5221
Sage Weil5b3a4db2010-02-19 21:43:23 -08005222/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005223 * Lookup and return message for incoming reply. Don't try to do
5224 * anything about a larger than preallocated data portion of the
5225 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08005226 */
5227static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08005228 struct ceph_msg_header *hdr,
5229 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07005230{
5231 struct ceph_osd *osd = con->private;
5232 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005233 struct ceph_msg *m = NULL;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005234 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02005235 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005236 int data_len = le32_to_cpu(hdr->data_len);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005237 u64 tid = le64_to_cpu(hdr->tid);
Sage Weilf24e9982009-10-06 11:31:10 -07005238
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005239 down_read(&osdc->lock);
5240 if (!osd_registered(osd)) {
5241 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5242 *skip = 1;
5243 goto out_unlock_osdc;
5244 }
5245 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5246
5247 mutex_lock(&osd->lock);
5248 req = lookup_request(&osd->o_requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005249 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01005250 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5251 osd->o_osd, tid);
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005252 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005253 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005254 }
Sage Weilc16e7862010-03-01 13:02:00 -08005255
Alex Elderace6d3a2013-04-01 16:12:14 -05005256 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08005257
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02005258 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005259 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5260 __func__, osd->o_osd, req->r_tid, front_len,
5261 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02005262 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5263 false);
Sage Weila79832f2010-04-01 16:06:19 -07005264 if (!m)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005265 goto out_unlock_session;
Sage Weilc16e7862010-03-01 13:02:00 -08005266 ceph_msg_put(req->r_reply);
5267 req->r_reply = m;
5268 }
Sage Weilc16e7862010-03-01 13:02:00 -08005269
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005270 if (data_len > req->r_reply->data_length) {
5271 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5272 __func__, osd->o_osd, req->r_tid, data_len,
5273 req->r_reply->data_length);
5274 m = NULL;
5275 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005276 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005277 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005278
5279 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08005280 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005281
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005282out_unlock_session:
5283 mutex_unlock(&osd->lock);
5284out_unlock_osdc:
5285 up_read(&osdc->lock);
Yehuda Sadeh24504182010-01-08 13:58:34 -08005286 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08005287}
5288
Ilya Dryomov19079202016-04-28 16:07:27 +02005289/*
5290 * TODO: switch to a msg-owned pagelist
5291 */
5292static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5293{
5294 struct ceph_msg *m;
5295 int type = le16_to_cpu(hdr->type);
5296 u32 front_len = le32_to_cpu(hdr->front_len);
5297 u32 data_len = le32_to_cpu(hdr->data_len);
5298
5299 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
5300 if (!m)
5301 return NULL;
5302
5303 if (data_len) {
5304 struct page **pages;
5305 struct ceph_osd_data osd_data;
5306
5307 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5308 GFP_NOIO);
Wei Yongjunc22e8532016-07-30 00:37:57 +00005309 if (IS_ERR(pages)) {
Ilya Dryomov19079202016-04-28 16:07:27 +02005310 ceph_msg_put(m);
5311 return NULL;
5312 }
5313
5314 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5315 false);
5316 ceph_osdc_msg_data_add(m, &osd_data);
5317 }
5318
5319 return m;
5320}
5321
Sage Weil5b3a4db2010-02-19 21:43:23 -08005322static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5323 struct ceph_msg_header *hdr,
5324 int *skip)
5325{
5326 struct ceph_osd *osd = con->private;
5327 int type = le16_to_cpu(hdr->type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005328
Alex Elder1c20f2d2012-06-04 14:43:32 -05005329 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08005330 switch (type) {
5331 case CEPH_MSG_OSD_MAP:
Ilya Dryomova02a9462017-06-19 12:18:05 +02005332 case CEPH_MSG_OSD_BACKOFF:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005333 case CEPH_MSG_WATCH_NOTIFY:
Ilya Dryomov19079202016-04-28 16:07:27 +02005334 return alloc_msg_with_page_vector(hdr);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005335 case CEPH_MSG_OSD_OPREPLY:
5336 return get_reply(con, hdr, skip);
5337 default:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005338 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5339 osd->o_osd, type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005340 *skip = 1;
5341 return NULL;
5342 }
Sage Weilf24e9982009-10-06 11:31:10 -07005343}
5344
5345/*
5346 * Wrappers to refcount containing ceph_osd struct
5347 */
5348static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5349{
5350 struct ceph_osd *osd = con->private;
5351 if (get_osd(osd))
5352 return con;
5353 return NULL;
5354}
5355
5356static void put_osd_con(struct ceph_connection *con)
5357{
5358 struct ceph_osd *osd = con->private;
5359 put_osd(osd);
5360}
5361
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005362/*
5363 * authentication
5364 */
Alex Eldera3530df2012-05-16 15:16:39 -05005365/*
5366 * Note: returned pointer is the address of a structure that's
5367 * managed separately. Caller must *not* attempt to free it.
5368 */
5369static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05005370 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005371{
5372 struct ceph_osd *o = con->private;
5373 struct ceph_osd_client *osdc = o->o_osdc;
5374 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05005375 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005376
Alex Elder74f18692012-05-16 15:16:39 -05005377 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02005378 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05005379 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005380 }
Sage Weil27859f92013-03-25 10:26:14 -07005381 if (!auth->authorizer) {
5382 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5383 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005384 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05005385 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07005386 } else {
5387 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07005388 auth);
5389 if (ret)
5390 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005391 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005392 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05005393
Alex Eldera3530df2012-05-16 15:16:39 -05005394 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005395}
5396
5397
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005398static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005399{
5400 struct ceph_osd *o = con->private;
5401 struct ceph_osd_client *osdc = o->o_osdc;
5402 struct ceph_auth_client *ac = osdc->client->monc.auth;
5403
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005404 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005405}
5406
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005407static int invalidate_authorizer(struct ceph_connection *con)
5408{
5409 struct ceph_osd *o = con->private;
5410 struct ceph_osd_client *osdc = o->o_osdc;
5411 struct ceph_auth_client *ac = osdc->client->monc.auth;
5412
Sage Weil27859f92013-03-25 10:26:14 -07005413 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005414 return ceph_monc_validate_auth(&osdc->client->monc);
5415}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005416
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005417static void osd_reencode_message(struct ceph_msg *msg)
5418{
Ilya Dryomov914902a2017-07-14 16:08:54 +02005419 int type = le16_to_cpu(msg->hdr.type);
5420
5421 if (type == CEPH_MSG_OSD_OP)
5422 encode_request_finish(msg);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005423}
5424
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005425static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005426{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005427 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005428 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005429
Yan, Zheng33d07332014-11-04 16:33:37 +08005430 return ceph_auth_sign_message(auth, msg);
5431}
5432
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005433static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005434{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005435 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005436 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005437
Yan, Zheng33d07332014-11-04 16:33:37 +08005438 return ceph_auth_check_message_signature(auth, msg);
5439}
5440
Tobias Klauser9e327892010-05-20 10:40:19 +02005441static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07005442 .get = get_osd_con,
5443 .put = put_osd_con,
5444 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005445 .get_authorizer = get_authorizer,
5446 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005447 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07005448 .alloc_msg = alloc_msg,
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005449 .reencode_message = osd_reencode_message,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005450 .sign_message = osd_sign_message,
5451 .check_message_signature = osd_check_message_signature,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005452 .fault = osd_fault,
Sage Weilf24e9982009-10-06 11:31:10 -07005453};