blob: d23a9f81f3d784123f0f97aaba1b529081350fae [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
Ilya Dryomov894868332018-09-28 16:02:53 +0200129/*
130 * Consumes @pages if @own_pages is true.
131 */
Alex Eldera4ce40a2013-04-05 01:27:12 -0500132static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500133 struct page **pages, u64 length, u32 alignment,
134 bool pages_from_pool, bool own_pages)
135{
136 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
137 osd_data->pages = pages;
138 osd_data->length = length;
139 osd_data->alignment = alignment;
140 osd_data->pages_from_pool = pages_from_pool;
141 osd_data->own_pages = own_pages;
142}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500143
Ilya Dryomov894868332018-09-28 16:02:53 +0200144/*
145 * Consumes a ref on @pagelist.
146 */
Alex Eldera4ce40a2013-04-05 01:27:12 -0500147static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500148 struct ceph_pagelist *pagelist)
149{
150 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
151 osd_data->pagelist = pagelist;
152}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500153
154#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500155static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Ilya Dryomov5359a172018-01-20 10:30:10 +0100156 struct ceph_bio_iter *bio_pos,
157 u32 bio_length)
Alex Elder43bfe5d2013-04-03 01:28:57 -0500158{
159 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100160 osd_data->bio_pos = *bio_pos;
Alex Elder43bfe5d2013-04-03 01:28:57 -0500161 osd_data->bio_length = bio_length;
162}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500163#endif /* CONFIG_BLOCK */
164
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100165static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
Ilya Dryomov0010f702018-05-04 16:57:30 +0200166 struct ceph_bvec_iter *bvec_pos,
167 u32 num_bvecs)
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100168{
169 osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
170 osd_data->bvec_pos = *bvec_pos;
Ilya Dryomov0010f702018-05-04 16:57:30 +0200171 osd_data->num_bvecs = num_bvecs;
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100172}
173
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300174#define osd_req_op_data(oreq, whch, typ, fld) \
175({ \
176 struct ceph_osd_request *__oreq = (oreq); \
177 unsigned int __whch = (whch); \
178 BUG_ON(__whch >= __oreq->r_num_ops); \
179 &__oreq->r_ops[__whch].typ.fld; \
180})
Alex Elder863c7eb2013-04-15 14:50:36 -0500181
Alex Elder49719772013-02-11 12:33:24 -0600182static struct ceph_osd_data *
183osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
184{
185 BUG_ON(which >= osd_req->r_num_ops);
186
187 return &osd_req->r_ops[which].raw_data_in;
188}
189
Alex Eldera4ce40a2013-04-05 01:27:12 -0500190struct ceph_osd_data *
191osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500192 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500193{
Alex Elder863c7eb2013-04-15 14:50:36 -0500194 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500195}
196EXPORT_SYMBOL(osd_req_op_extent_osd_data);
197
Alex Elder49719772013-02-11 12:33:24 -0600198void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
199 unsigned int which, struct page **pages,
200 u64 length, u32 alignment,
201 bool pages_from_pool, bool own_pages)
202{
203 struct ceph_osd_data *osd_data;
204
205 osd_data = osd_req_op_raw_data_in(osd_req, which);
206 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
207 pages_from_pool, own_pages);
208}
209EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
210
Alex Eldera4ce40a2013-04-05 01:27:12 -0500211void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500212 unsigned int which, struct page **pages,
213 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500214 bool pages_from_pool, bool own_pages)
215{
216 struct ceph_osd_data *osd_data;
217
Alex Elder863c7eb2013-04-15 14:50:36 -0500218 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500219 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
220 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500221}
222EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
223
224void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500225 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500226{
227 struct ceph_osd_data *osd_data;
228
Alex Elder863c7eb2013-04-15 14:50:36 -0500229 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500230 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500231}
232EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
233
234#ifdef CONFIG_BLOCK
235void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Ilya Dryomov5359a172018-01-20 10:30:10 +0100236 unsigned int which,
237 struct ceph_bio_iter *bio_pos,
238 u32 bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500239{
240 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500241
242 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Ilya Dryomov5359a172018-01-20 10:30:10 +0100243 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500244}
245EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
246#endif /* CONFIG_BLOCK */
247
Ilya Dryomov0010f702018-05-04 16:57:30 +0200248void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
249 unsigned int which,
250 struct bio_vec *bvecs, u32 num_bvecs,
251 u32 bytes)
252{
253 struct ceph_osd_data *osd_data;
254 struct ceph_bvec_iter it = {
255 .bvecs = bvecs,
256 .iter = { .bi_size = bytes },
257 };
258
259 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
260 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
261}
262EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
263
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100264void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
265 unsigned int which,
266 struct ceph_bvec_iter *bvec_pos)
267{
268 struct ceph_osd_data *osd_data;
269
270 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Ilya Dryomov0010f702018-05-04 16:57:30 +0200271 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100272}
273EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
274
Alex Eldera4ce40a2013-04-05 01:27:12 -0500275static void osd_req_op_cls_request_info_pagelist(
276 struct ceph_osd_request *osd_req,
277 unsigned int which, struct ceph_pagelist *pagelist)
278{
279 struct ceph_osd_data *osd_data;
280
Alex Elder863c7eb2013-04-15 14:50:36 -0500281 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500282 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500283}
284
Alex Elder04017e22013-04-05 14:46:02 -0500285void osd_req_op_cls_request_data_pagelist(
286 struct ceph_osd_request *osd_req,
287 unsigned int which, struct ceph_pagelist *pagelist)
288{
289 struct ceph_osd_data *osd_data;
290
Alex Elder863c7eb2013-04-15 14:50:36 -0500291 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500292 ceph_osd_data_pagelist_init(osd_data, pagelist);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200293 osd_req->r_ops[which].cls.indata_len += pagelist->length;
294 osd_req->r_ops[which].indata_len += pagelist->length;
Alex Elder04017e22013-04-05 14:46:02 -0500295}
296EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
297
Alex Elder6c57b552013-04-19 15:34:49 -0500298void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
299 unsigned int which, struct page **pages, u64 length,
300 u32 alignment, bool pages_from_pool, bool own_pages)
301{
302 struct ceph_osd_data *osd_data;
303
304 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
305 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
306 pages_from_pool, own_pages);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200307 osd_req->r_ops[which].cls.indata_len += length;
308 osd_req->r_ops[which].indata_len += length;
Alex Elder6c57b552013-04-19 15:34:49 -0500309}
310EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
311
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100312void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
313 unsigned int which,
Ilya Dryomov0010f702018-05-04 16:57:30 +0200314 struct bio_vec *bvecs, u32 num_bvecs,
315 u32 bytes)
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100316{
317 struct ceph_osd_data *osd_data;
318 struct ceph_bvec_iter it = {
319 .bvecs = bvecs,
320 .iter = { .bi_size = bytes },
321 };
322
323 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Ilya Dryomov0010f702018-05-04 16:57:30 +0200324 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100325 osd_req->r_ops[which].cls.indata_len += bytes;
326 osd_req->r_ops[which].indata_len += bytes;
327}
328EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
329
Alex Eldera4ce40a2013-04-05 01:27:12 -0500330void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
331 unsigned int which, struct page **pages, u64 length,
332 u32 alignment, bool pages_from_pool, bool own_pages)
333{
334 struct ceph_osd_data *osd_data;
335
Alex Elder863c7eb2013-04-15 14:50:36 -0500336 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500337 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
338 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500339}
340EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
341
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500342static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
343{
344 switch (osd_data->type) {
345 case CEPH_OSD_DATA_TYPE_NONE:
346 return 0;
347 case CEPH_OSD_DATA_TYPE_PAGES:
348 return osd_data->length;
349 case CEPH_OSD_DATA_TYPE_PAGELIST:
350 return (u64)osd_data->pagelist->length;
351#ifdef CONFIG_BLOCK
352 case CEPH_OSD_DATA_TYPE_BIO:
353 return (u64)osd_data->bio_length;
354#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100355 case CEPH_OSD_DATA_TYPE_BVECS:
356 return osd_data->bvec_pos.iter.bi_size;
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500357 default:
358 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
359 return 0;
360 }
361}
362
Alex Elderc54d47b2013-04-03 01:28:57 -0500363static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
364{
Alex Elder54764922013-04-05 01:27:12 -0500365 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500366 int num_pages;
367
368 num_pages = calc_pages_for((u64)osd_data->alignment,
369 (u64)osd_data->length);
370 ceph_release_page_vector(osd_data->pages, num_pages);
Ilya Dryomov894868332018-09-28 16:02:53 +0200371 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
372 ceph_pagelist_release(osd_data->pagelist);
Alex Elderc54d47b2013-04-03 01:28:57 -0500373 }
Alex Elder54764922013-04-05 01:27:12 -0500374 ceph_osd_data_init(osd_data);
375}
376
377static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
378 unsigned int which)
379{
380 struct ceph_osd_req_op *op;
381
382 BUG_ON(which >= osd_req->r_num_ops);
383 op = &osd_req->r_ops[which];
384
385 switch (op->op) {
386 case CEPH_OSD_OP_READ:
387 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200388 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500389 ceph_osd_data_release(&op->extent.osd_data);
390 break;
391 case CEPH_OSD_OP_CALL:
392 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500393 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500394 ceph_osd_data_release(&op->cls.response_data);
395 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800396 case CEPH_OSD_OP_SETXATTR:
397 case CEPH_OSD_OP_CMPXATTR:
398 ceph_osd_data_release(&op->xattr.osd_data);
399 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800400 case CEPH_OSD_OP_STAT:
401 ceph_osd_data_release(&op->raw_data_in);
402 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200403 case CEPH_OSD_OP_NOTIFY_ACK:
404 ceph_osd_data_release(&op->notify_ack.request_data);
405 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200406 case CEPH_OSD_OP_NOTIFY:
407 ceph_osd_data_release(&op->notify.request_data);
408 ceph_osd_data_release(&op->notify.response_data);
409 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700410 case CEPH_OSD_OP_LIST_WATCHERS:
411 ceph_osd_data_release(&op->list_watchers.response_data);
412 break;
Luis Henriques23ddf9b2018-10-15 16:45:58 +0100413 case CEPH_OSD_OP_COPY_FROM:
414 ceph_osd_data_release(&op->copy_from.osd_data);
415 break;
Alex Elder54764922013-04-05 01:27:12 -0500416 default:
417 break;
418 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500419}
420
Sage Weilf24e9982009-10-06 11:31:10 -0700421/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200422 * Assumes @t is zero-initialized.
423 */
424static void target_init(struct ceph_osd_request_target *t)
425{
426 ceph_oid_init(&t->base_oid);
427 ceph_oloc_init(&t->base_oloc);
428 ceph_oid_init(&t->target_oid);
429 ceph_oloc_init(&t->target_oloc);
430
431 ceph_osds_init(&t->acting);
432 ceph_osds_init(&t->up);
433 t->size = -1;
434 t->min_size = -1;
435
436 t->osd = CEPH_HOMELESS_OSD;
437}
438
Ilya Dryomov922dab62016-05-26 01:15:02 +0200439static void target_copy(struct ceph_osd_request_target *dest,
440 const struct ceph_osd_request_target *src)
441{
442 ceph_oid_copy(&dest->base_oid, &src->base_oid);
443 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
444 ceph_oid_copy(&dest->target_oid, &src->target_oid);
445 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
446
447 dest->pgid = src->pgid; /* struct */
Ilya Dryomovdc98ff72017-06-15 16:30:53 +0200448 dest->spgid = src->spgid; /* struct */
Ilya Dryomov922dab62016-05-26 01:15:02 +0200449 dest->pg_num = src->pg_num;
450 dest->pg_num_mask = src->pg_num_mask;
451 ceph_osds_copy(&dest->acting, &src->acting);
452 ceph_osds_copy(&dest->up, &src->up);
453 dest->size = src->size;
454 dest->min_size = src->min_size;
455 dest->sort_bitwise = src->sort_bitwise;
456
457 dest->flags = src->flags;
458 dest->paused = src->paused;
459
Ilya Dryomov04c7d782017-06-15 16:30:55 +0200460 dest->epoch = src->epoch;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +0200461 dest->last_force_resend = src->last_force_resend;
462
Ilya Dryomov922dab62016-05-26 01:15:02 +0200463 dest->osd = src->osd;
464}
465
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200466static void target_destroy(struct ceph_osd_request_target *t)
467{
468 ceph_oid_destroy(&t->base_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800469 ceph_oloc_destroy(&t->base_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200470 ceph_oid_destroy(&t->target_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800471 ceph_oloc_destroy(&t->target_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200472}
473
474/*
Sage Weilf24e9982009-10-06 11:31:10 -0700475 * requests
476 */
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200477static void request_release_checks(struct ceph_osd_request *req)
478{
479 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
Ilya Dryomov46092452016-04-28 16:07:27 +0200480 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200481 WARN_ON(!list_empty(&req->r_unsafe_item));
482 WARN_ON(req->r_osd);
483}
484
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400485static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700486{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400487 struct ceph_osd_request *req = container_of(kref,
488 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500489 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800490
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400491 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
492 req->r_request, req->r_reply);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200493 request_release_checks(req);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400494
Sage Weil415e49a2009-12-07 13:37:03 -0800495 if (req->r_request)
496 ceph_msg_put(req->r_request);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200497 if (req->r_reply)
Alex Elderab8cb342012-06-04 14:43:32 -0500498 ceph_msg_put(req->r_reply);
Alex Elder0fff87e2013-02-14 12:16:43 -0600499
Alex Elder54764922013-04-05 01:27:12 -0500500 for (which = 0; which < req->r_num_ops; which++)
501 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600502
Ilya Dryomova66dd382016-04-28 16:07:23 +0200503 target_destroy(&req->r_t);
Sage Weil415e49a2009-12-07 13:37:03 -0800504 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200505
Sage Weil415e49a2009-12-07 13:37:03 -0800506 if (req->r_mempool)
507 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100508 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500509 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100510 else
511 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700512}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400513
514void ceph_osdc_get_request(struct ceph_osd_request *req)
515{
516 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100517 kref_read(&req->r_kref));
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400518 kref_get(&req->r_kref);
519}
520EXPORT_SYMBOL(ceph_osdc_get_request);
521
522void ceph_osdc_put_request(struct ceph_osd_request *req)
523{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200524 if (req) {
525 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100526 kref_read(&req->r_kref));
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200527 kref_put(&req->r_kref, ceph_osdc_release_request);
528 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400529}
530EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700531
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200532static void request_init(struct ceph_osd_request *req)
533{
534 /* req only, each op is zeroed in _osd_req_op_init() */
535 memset(req, 0, sizeof(*req));
536
537 kref_init(&req->r_kref);
538 init_completion(&req->r_completion);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200539 RB_CLEAR_NODE(&req->r_node);
Ilya Dryomov46092452016-04-28 16:07:27 +0200540 RB_CLEAR_NODE(&req->r_mc_node);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200541 INIT_LIST_HEAD(&req->r_unsafe_item);
542
543 target_init(&req->r_t);
544}
545
Ilya Dryomov922dab62016-05-26 01:15:02 +0200546/*
547 * This is ugly, but it allows us to reuse linger registration and ping
548 * requests, keeping the structure of the code around send_linger{_ping}()
549 * reasonable. Setting up a min_nr=2 mempool for each linger request
550 * and dealing with copying ops (this blasts req only, watch op remains
551 * intact) isn't any better.
552 */
553static void request_reinit(struct ceph_osd_request *req)
554{
555 struct ceph_osd_client *osdc = req->r_osdc;
556 bool mempool = req->r_mempool;
557 unsigned int num_ops = req->r_num_ops;
558 u64 snapid = req->r_snapid;
559 struct ceph_snap_context *snapc = req->r_snapc;
560 bool linger = req->r_linger;
561 struct ceph_msg *request_msg = req->r_request;
562 struct ceph_msg *reply_msg = req->r_reply;
563
564 dout("%s req %p\n", __func__, req);
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100565 WARN_ON(kref_read(&req->r_kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200566 request_release_checks(req);
567
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100568 WARN_ON(kref_read(&request_msg->kref) != 1);
569 WARN_ON(kref_read(&reply_msg->kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200570 target_destroy(&req->r_t);
571
572 request_init(req);
573 req->r_osdc = osdc;
574 req->r_mempool = mempool;
575 req->r_num_ops = num_ops;
576 req->r_snapid = snapid;
577 req->r_snapc = snapc;
578 req->r_linger = linger;
579 req->r_request = request_msg;
580 req->r_reply = reply_msg;
581}
582
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700583struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700584 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800585 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700586 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600587 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700588{
589 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700590
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700591 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100592 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700593 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100594 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
595 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700596 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100597 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
Kees Cookacafe7e2018-05-08 13:45:50 -0700598 req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700599 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100600 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700601 return NULL;
602
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200603 request_init(req);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700604 req->r_osdc = osdc;
605 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500606 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200607 req->r_snapid = CEPH_NOSNAP;
608 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700609
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200610 dout("%s req %p\n", __func__, req);
611 return req;
612}
613EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100614
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +0200615static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
Yan, Zheng30c156d2016-02-14 11:24:31 +0800616{
617 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
618}
619
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200620static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
621 int num_request_data_items,
622 int num_reply_data_items)
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200623{
624 struct ceph_osd_client *osdc = req->r_osdc;
625 struct ceph_msg *msg;
626 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700627
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200628 WARN_ON(req->r_request || req->r_reply);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200629 WARN_ON(ceph_oid_empty(&req->r_base_oid));
Yan, Zheng30c156d2016-02-14 11:24:31 +0800630 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200631
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200632 /* create request message */
Ilya Dryomov8cb441c2017-06-15 16:30:54 +0200633 msg_size = CEPH_ENCODING_START_BLK_LEN +
634 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
635 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
636 msg_size += CEPH_ENCODING_START_BLK_LEN +
637 sizeof(struct ceph_osd_reqid); /* reqid */
638 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
639 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
Yan, Zheng30c156d2016-02-14 11:24:31 +0800640 msg_size += CEPH_ENCODING_START_BLK_LEN +
641 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200642 msg_size += 4 + req->r_base_oid.name_len; /* oid */
643 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100644 msg_size += 8; /* snapid */
645 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200646 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +0200647 msg_size += 4 + 8; /* retry_attempt, features */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100648
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200649 if (req->r_mempool)
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200650 msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
651 num_request_data_items);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700652 else
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200653 msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
654 num_request_data_items, gfp, true);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200655 if (!msg)
656 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700657
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700658 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700659 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700660
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200661 /* create reply message */
662 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200663 msg_size += req->r_base_oid.name_len;
664 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200665
666 if (req->r_mempool)
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200667 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
668 num_reply_data_items);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200669 else
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200670 msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
671 num_reply_data_items, gfp, true);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200672 if (!msg)
673 return -ENOMEM;
674
675 req->r_reply = msg;
676
677 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700678}
679
Alex Eldera8dd0a32013-03-13 20:50:00 -0500680static bool osd_req_opcode_valid(u16 opcode)
681{
682 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400683#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
684__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
685#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500686 default:
687 return false;
688 }
689}
690
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200691static void get_num_data_items(struct ceph_osd_request *req,
692 int *num_request_data_items,
693 int *num_reply_data_items)
694{
695 struct ceph_osd_req_op *op;
696
697 *num_request_data_items = 0;
698 *num_reply_data_items = 0;
699
700 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
701 switch (op->op) {
702 /* request */
703 case CEPH_OSD_OP_WRITE:
704 case CEPH_OSD_OP_WRITEFULL:
705 case CEPH_OSD_OP_SETXATTR:
706 case CEPH_OSD_OP_CMPXATTR:
707 case CEPH_OSD_OP_NOTIFY_ACK:
Luis Henriques23ddf9b2018-10-15 16:45:58 +0100708 case CEPH_OSD_OP_COPY_FROM:
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200709 *num_request_data_items += 1;
710 break;
711
712 /* reply */
713 case CEPH_OSD_OP_STAT:
714 case CEPH_OSD_OP_READ:
715 case CEPH_OSD_OP_LIST_WATCHERS:
716 *num_reply_data_items += 1;
717 break;
718
719 /* both */
720 case CEPH_OSD_OP_NOTIFY:
721 *num_request_data_items += 1;
722 *num_reply_data_items += 1;
723 break;
724 case CEPH_OSD_OP_CALL:
725 *num_request_data_items += 2;
726 *num_reply_data_items += 1;
727 break;
728
729 default:
730 WARN_ON(!osd_req_opcode_valid(op->op));
731 break;
732 }
733 }
734}
735
736/*
737 * oid, oloc and OSD op opcode(s) must be filled in before this function
738 * is called.
739 */
740int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
741{
742 int num_request_data_items, num_reply_data_items;
743
744 get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
745 return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
746 num_reply_data_items);
747}
748EXPORT_SYMBOL(ceph_osdc_alloc_messages);
749
Alex Elder33803f32013-03-13 20:50:00 -0500750/*
751 * This is an osd op init function for opcodes that have no data or
752 * other information associated with them. It also serves as a
753 * common init routine for all the other init functions, below.
754 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500755static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600756_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800757 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500758{
Alex Elderc99d2d42013-04-05 01:27:11 -0500759 struct ceph_osd_req_op *op;
760
761 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500762 BUG_ON(!osd_req_opcode_valid(opcode));
763
Alex Elderc99d2d42013-04-05 01:27:11 -0500764 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500765 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500766 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800767 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500768
769 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500770}
771
Alex Elder49719772013-02-11 12:33:24 -0600772void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800773 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600774{
Yan, Zheng144cba12015-04-27 11:09:54 +0800775 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600776}
777EXPORT_SYMBOL(osd_req_op_init);
778
Alex Elderc99d2d42013-04-05 01:27:11 -0500779void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
780 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500781 u64 offset, u64 length,
782 u64 truncate_size, u32 truncate_seq)
783{
Yan, Zheng144cba12015-04-27 11:09:54 +0800784 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
785 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500786 size_t payload_len = 0;
787
Li Wangad7a60d2013-08-15 11:51:44 +0800788 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200789 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
790 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500791
Alex Elder33803f32013-03-13 20:50:00 -0500792 op->extent.offset = offset;
793 op->extent.length = length;
794 op->extent.truncate_size = truncate_size;
795 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200796 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500797 payload_len += length;
798
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100799 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500800}
801EXPORT_SYMBOL(osd_req_op_extent_init);
802
Alex Elderc99d2d42013-04-05 01:27:11 -0500803void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
804 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500805{
Alex Elderc99d2d42013-04-05 01:27:11 -0500806 struct ceph_osd_req_op *op;
807 u64 previous;
808
809 BUG_ON(which >= osd_req->r_num_ops);
810 op = &osd_req->r_ops[which];
811 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500812
813 if (length == previous)
814 return; /* Nothing to do */
815 BUG_ON(length > previous);
816
817 op->extent.length = length;
Yan, Zhengd641df82017-01-19 11:21:29 +0800818 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
819 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500820}
821EXPORT_SYMBOL(osd_req_op_extent_update);
822
Yan, Zheng2c63f492016-01-07 17:32:54 +0800823void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
824 unsigned int which, u64 offset_inc)
825{
826 struct ceph_osd_req_op *op, *prev_op;
827
828 BUG_ON(which + 1 >= osd_req->r_num_ops);
829
830 prev_op = &osd_req->r_ops[which];
831 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
832 /* dup previous one */
833 op->indata_len = prev_op->indata_len;
834 op->outdata_len = prev_op->outdata_len;
835 op->extent = prev_op->extent;
836 /* adjust offset */
837 op->extent.offset += offset_inc;
838 op->extent.length -= offset_inc;
839
840 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
841 op->indata_len -= offset_inc;
842}
843EXPORT_SYMBOL(osd_req_op_extent_dup_last);
844
Chengguang Xufe943d52018-04-12 12:04:55 +0800845int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Ilya Dryomov24639ce562018-09-26 19:12:07 +0200846 const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500847{
Ilya Dryomov24639ce562018-09-26 19:12:07 +0200848 struct ceph_osd_req_op *op;
Alex Elder5f562df2013-04-05 01:27:12 -0500849 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500850 size_t payload_len = 0;
851 size_t size;
852
Ilya Dryomov24639ce562018-09-26 19:12:07 +0200853 op = _osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500854
Ilya Dryomov33165d42018-09-28 15:38:34 +0200855 pagelist = ceph_pagelist_alloc(GFP_NOFS);
Chengguang Xufe943d52018-04-12 12:04:55 +0800856 if (!pagelist)
857 return -ENOMEM;
858
Alex Elder33803f32013-03-13 20:50:00 -0500859 op->cls.class_name = class;
860 size = strlen(class);
861 BUG_ON(size > (size_t) U8_MAX);
862 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500863 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500864 payload_len += size;
865
866 op->cls.method_name = method;
867 size = strlen(method);
868 BUG_ON(size > (size_t) U8_MAX);
869 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500870 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500871 payload_len += size;
872
Alex Eldera4ce40a2013-04-05 01:27:12 -0500873 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500874
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100875 op->indata_len = payload_len;
Chengguang Xufe943d52018-04-12 12:04:55 +0800876 return 0;
Alex Elder33803f32013-03-13 20:50:00 -0500877}
878EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500879
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800880int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
881 u16 opcode, const char *name, const void *value,
882 size_t size, u8 cmp_op, u8 cmp_mode)
883{
Yan, Zheng144cba12015-04-27 11:09:54 +0800884 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
885 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800886 struct ceph_pagelist *pagelist;
887 size_t payload_len;
888
889 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
890
Ilya Dryomov33165d42018-09-28 15:38:34 +0200891 pagelist = ceph_pagelist_alloc(GFP_NOFS);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800892 if (!pagelist)
893 return -ENOMEM;
894
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800895 payload_len = strlen(name);
896 op->xattr.name_len = payload_len;
897 ceph_pagelist_append(pagelist, name, payload_len);
898
899 op->xattr.value_len = size;
900 ceph_pagelist_append(pagelist, value, size);
901 payload_len += size;
902
903 op->xattr.cmp_op = cmp_op;
904 op->xattr.cmp_mode = cmp_mode;
905
906 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100907 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800908 return 0;
909}
910EXPORT_SYMBOL(osd_req_op_xattr_init);
911
Ilya Dryomov922dab62016-05-26 01:15:02 +0200912/*
913 * @watch_opcode: CEPH_OSD_WATCH_OP_*
914 */
915static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
916 u64 cookie, u8 watch_opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500917{
Ilya Dryomov922dab62016-05-26 01:15:02 +0200918 struct ceph_osd_req_op *op;
Alex Elder33803f32013-03-13 20:50:00 -0500919
Ilya Dryomov922dab62016-05-26 01:15:02 +0200920 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500921 op->watch.cookie = cookie;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200922 op->watch.op = watch_opcode;
923 op->watch.gen = 0;
Alex Elder33803f32013-03-13 20:50:00 -0500924}
Alex Elder33803f32013-03-13 20:50:00 -0500925
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200926void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
927 unsigned int which,
928 u64 expected_object_size,
929 u64 expected_write_size)
930{
931 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800932 CEPH_OSD_OP_SETALLOCHINT,
933 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200934
935 op->alloc_hint.expected_object_size = expected_object_size;
936 op->alloc_hint.expected_write_size = expected_write_size;
937
938 /*
939 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
940 * not worth a feature bit. Set FAILOK per-op flag to make
941 * sure older osds don't trip over an unsupported opcode.
942 */
943 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
944}
945EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
946
Alex Elder90af3602013-04-05 14:46:01 -0500947static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500948 struct ceph_osd_data *osd_data)
949{
950 u64 length = ceph_osd_data_length(osd_data);
951
952 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
953 BUG_ON(length > (u64) SIZE_MAX);
954 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500955 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500956 length, osd_data->alignment);
957 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
958 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500959 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500960#ifdef CONFIG_BLOCK
961 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Ilya Dryomov5359a172018-01-20 10:30:10 +0100962 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500963#endif
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100964 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
965 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
Alex Elderec9123c2013-04-05 01:27:12 -0500966 } else {
967 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
968 }
969}
970
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200971static u32 osd_req_encode_op(struct ceph_osd_op *dst,
972 const struct ceph_osd_req_op *src)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700973{
Alex Elder065a68f2012-04-20 15:49:43 -0500974 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600975 case CEPH_OSD_OP_STAT:
976 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700977 case CEPH_OSD_OP_READ:
978 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200979 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800980 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800981 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600982 dst->extent.offset = cpu_to_le64(src->extent.offset);
983 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700984 dst->extent.truncate_size =
985 cpu_to_le64(src->extent.truncate_size);
986 dst->extent.truncate_seq =
987 cpu_to_le32(src->extent.truncate_seq);
988 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700989 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700990 dst->cls.class_len = src->cls.class_len;
991 dst->cls.method_len = src->cls.method_len;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200992 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700993 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700994 case CEPH_OSD_OP_WATCH:
995 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200996 dst->watch.ver = cpu_to_le64(0);
997 dst->watch.op = src->watch.op;
998 dst->watch.gen = cpu_to_le32(src->watch.gen);
999 break;
1000 case CEPH_OSD_OP_NOTIFY_ACK:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001001 break;
Ilya Dryomov19079202016-04-28 16:07:27 +02001002 case CEPH_OSD_OP_NOTIFY:
1003 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
1004 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001005 case CEPH_OSD_OP_LIST_WATCHERS:
1006 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +02001007 case CEPH_OSD_OP_SETALLOCHINT:
1008 dst->alloc_hint.expected_object_size =
1009 cpu_to_le64(src->alloc_hint.expected_object_size);
1010 dst->alloc_hint.expected_write_size =
1011 cpu_to_le64(src->alloc_hint.expected_write_size);
1012 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +08001013 case CEPH_OSD_OP_SETXATTR:
1014 case CEPH_OSD_OP_CMPXATTR:
1015 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1016 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1017 dst->xattr.cmp_op = src->xattr.cmp_op;
1018 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Yan, Zhengd74b50b2014-11-12 14:00:43 +08001019 break;
Yan, Zheng864e9192014-11-13 10:47:25 +08001020 case CEPH_OSD_OP_CREATE:
1021 case CEPH_OSD_OP_DELETE:
1022 break;
Luis Henriques23ddf9b2018-10-15 16:45:58 +01001023 case CEPH_OSD_OP_COPY_FROM:
1024 dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
1025 dst->copy_from.src_version =
1026 cpu_to_le64(src->copy_from.src_version);
1027 dst->copy_from.flags = src->copy_from.flags;
1028 dst->copy_from.src_fadvise_flags =
1029 cpu_to_le32(src->copy_from.src_fadvise_flags);
1030 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001031 default:
Alex Elder4c464592013-02-15 11:42:30 -06001032 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -06001033 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -06001034 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -05001035
1036 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001037 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +02001038
Alex Eldera8dd0a32013-03-13 20:50:00 -05001039 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +02001040 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +01001041 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -06001042
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001043 return src->indata_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001044}
1045
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -07001046/*
Sage Weilf24e9982009-10-06 11:31:10 -07001047 * build new request AND message, calculate layout, and adjust file
1048 * extent as needed.
1049 *
1050 * if the file was recently truncated, we include information about its
1051 * old and new size so that the object can be updated appropriately. (we
1052 * avoid synchronously deleting truncated objects because it's slow.)
Sage Weilf24e9982009-10-06 11:31:10 -07001053 */
1054struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
1055 struct ceph_file_layout *layout,
1056 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +08001057 u64 off, u64 *plen,
1058 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -07001059 int opcode, int flags,
1060 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -07001061 u32 truncate_seq,
1062 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06001063 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -07001064{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001065 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -05001066 u64 objnum = 0;
1067 u64 objoff = 0;
1068 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -07001069 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001070
Li Wangad7a60d2013-08-15 11:51:44 +08001071 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +08001072 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1073 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001074
Alex Elderacead002013-03-14 14:09:05 -05001075 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -06001076 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001077 if (!req) {
1078 r = -ENOMEM;
1079 goto fail;
1080 }
Alex Elder79528732013-04-03 21:32:51 -05001081
Sage Weilf24e9982009-10-06 11:31:10 -07001082 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -05001083 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001084 if (r)
1085 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -05001086
Yan, Zheng864e9192014-11-13 10:47:25 +08001087 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +08001088 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +08001089 } else {
Yan, Zheng76271512016-02-03 21:24:49 +08001090 u32 object_size = layout->object_size;
Yan, Zheng864e9192014-11-13 10:47:25 +08001091 u32 object_base = off - objoff;
1092 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1093 if (truncate_size <= object_base) {
1094 truncate_size = 0;
1095 } else {
1096 truncate_size -= object_base;
1097 if (truncate_size > object_size)
1098 truncate_size = object_size;
1099 }
Yan, Zhengccca4e32013-06-02 18:40:23 +08001100 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +08001101 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +08001102 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -05001103 }
Alex Elderd18d1e22013-03-13 20:50:01 -05001104
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001105 req->r_flags = flags;
Yan, Zheng76271512016-02-03 21:24:49 +08001106 req->r_base_oloc.pool = layout->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +08001107 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001108 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -06001109
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001110 req->r_snapid = vino.snap;
1111 if (flags & CEPH_OSD_FLAG_WRITE)
1112 req->r_data_offset = off;
1113
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001114 if (num_ops > 1)
1115 /*
1116 * This is a special case for ceph_writepages_start(), but it
1117 * also covers ceph_uninline_data(). If more multi-op request
1118 * use cases emerge, we will need a separate helper.
1119 */
1120 r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
1121 else
1122 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001123 if (r)
1124 goto fail;
1125
Sage Weilf24e9982009-10-06 11:31:10 -07001126 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001127
1128fail:
1129 ceph_osdc_put_request(req);
1130 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -07001131}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001132EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001133
1134/*
1135 * We keep osd requests in an rbtree, sorted by ->r_tid.
1136 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001137DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02001138DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
Sage Weilf24e9982009-10-06 11:31:10 -07001139
Ilya Dryomov66850df2018-05-15 15:47:58 +02001140/*
1141 * Call @fn on each OSD request as long as @fn returns 0.
1142 */
1143static void for_each_request(struct ceph_osd_client *osdc,
1144 int (*fn)(struct ceph_osd_request *req, void *arg),
1145 void *arg)
1146{
1147 struct rb_node *n, *p;
1148
1149 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1150 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1151
1152 for (p = rb_first(&osd->o_requests); p; ) {
1153 struct ceph_osd_request *req =
1154 rb_entry(p, struct ceph_osd_request, r_node);
1155
1156 p = rb_next(p);
1157 if (fn(req, arg))
1158 return;
1159 }
1160 }
1161
1162 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1163 struct ceph_osd_request *req =
1164 rb_entry(p, struct ceph_osd_request, r_node);
1165
1166 p = rb_next(p);
1167 if (fn(req, arg))
1168 return;
1169 }
1170}
1171
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001172static bool osd_homeless(struct ceph_osd *osd)
1173{
1174 return osd->o_osd == CEPH_HOMELESS_OSD;
1175}
1176
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001177static bool osd_registered(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001178{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001179 verify_osdc_locked(osd->o_osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001180
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001181 return !RB_EMPTY_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001182}
1183
1184/*
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001185 * Assumes @osd is zero-initialized.
1186 */
1187static void osd_init(struct ceph_osd *osd)
1188{
Elena Reshetova02113a02017-03-17 14:10:28 +02001189 refcount_set(&osd->o_ref, 1);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001190 RB_CLEAR_NODE(&osd->o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001191 osd->o_requests = RB_ROOT;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001192 osd->o_linger_requests = RB_ROOT;
Ilya Dryomova02a9462017-06-19 12:18:05 +02001193 osd->o_backoff_mappings = RB_ROOT;
1194 osd->o_backoffs_by_id = RB_ROOT;
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001195 INIT_LIST_HEAD(&osd->o_osd_lru);
1196 INIT_LIST_HEAD(&osd->o_keepalive_item);
1197 osd->o_incarnation = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001198 mutex_init(&osd->lock);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001199}
1200
1201static void osd_cleanup(struct ceph_osd *osd)
1202{
1203 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001204 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001205 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomova02a9462017-06-19 12:18:05 +02001206 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1207 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001208 WARN_ON(!list_empty(&osd->o_osd_lru));
1209 WARN_ON(!list_empty(&osd->o_keepalive_item));
1210
1211 if (osd->o_auth.authorizer) {
1212 WARN_ON(osd_homeless(osd));
1213 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1214 }
1215}
1216
1217/*
Sage Weilf24e9982009-10-06 11:31:10 -07001218 * Track open sessions with osds.
1219 */
Alex Eldere10006f2012-05-26 23:26:43 -05001220static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001221{
1222 struct ceph_osd *osd;
1223
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001224 WARN_ON(onum == CEPH_HOMELESS_OSD);
1225
Ilya Dryomov7a28f592016-04-28 16:07:25 +02001226 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001227 osd_init(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001228 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001229 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -07001230
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001231 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001232
Sage Weilf24e9982009-10-06 11:31:10 -07001233 return osd;
1234}
1235
1236static struct ceph_osd *get_osd(struct ceph_osd *osd)
1237{
Elena Reshetova02113a02017-03-17 14:10:28 +02001238 if (refcount_inc_not_zero(&osd->o_ref)) {
1239 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1240 refcount_read(&osd->o_ref));
Sage Weilf24e9982009-10-06 11:31:10 -07001241 return osd;
1242 } else {
1243 dout("get_osd %p FAIL\n", osd);
1244 return NULL;
1245 }
1246}
1247
1248static void put_osd(struct ceph_osd *osd)
1249{
Elena Reshetova02113a02017-03-17 14:10:28 +02001250 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1251 refcount_read(&osd->o_ref) - 1);
1252 if (refcount_dec_and_test(&osd->o_ref)) {
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001253 osd_cleanup(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001254 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001255 }
Sage Weilf24e9982009-10-06 11:31:10 -07001256}
1257
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001258DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1259
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001260static void __move_osd_to_lru(struct ceph_osd *osd)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001261{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001262 struct ceph_osd_client *osdc = osd->o_osdc;
1263
1264 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001265 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001266
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001267 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001268 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001269 spin_unlock(&osdc->osd_lru_lock);
1270
Ilya Dryomova319bf52015-05-15 12:02:17 +03001271 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001272}
1273
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001274static void maybe_move_osd_to_lru(struct ceph_osd *osd)
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001275{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001276 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001277 RB_EMPTY_ROOT(&osd->o_linger_requests))
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001278 __move_osd_to_lru(osd);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001279}
1280
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001281static void __remove_osd_from_lru(struct ceph_osd *osd)
1282{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001283 struct ceph_osd_client *osdc = osd->o_osdc;
1284
1285 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1286
1287 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001288 if (!list_empty(&osd->o_osd_lru))
1289 list_del_init(&osd->o_osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001290 spin_unlock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001291}
1292
Sage Weilf24e9982009-10-06 11:31:10 -07001293/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001294 * Close the connection and assign any leftover requests to the
1295 * homeless session.
1296 */
1297static void close_osd(struct ceph_osd *osd)
1298{
1299 struct ceph_osd_client *osdc = osd->o_osdc;
1300 struct rb_node *n;
1301
1302 verify_osdc_wrlocked(osdc);
1303 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1304
1305 ceph_con_close(&osd->o_con);
1306
1307 for (n = rb_first(&osd->o_requests); n; ) {
1308 struct ceph_osd_request *req =
1309 rb_entry(n, struct ceph_osd_request, r_node);
1310
1311 n = rb_next(n); /* unlink_request() */
1312
1313 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1314 unlink_request(osd, req);
1315 link_request(&osdc->homeless_osd, req);
1316 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02001317 for (n = rb_first(&osd->o_linger_requests); n; ) {
1318 struct ceph_osd_linger_request *lreq =
1319 rb_entry(n, struct ceph_osd_linger_request, node);
1320
1321 n = rb_next(n); /* unlink_linger() */
1322
1323 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1324 lreq->linger_id);
1325 unlink_linger(osd, lreq);
1326 link_linger(&osdc->homeless_osd, lreq);
1327 }
Ilya Dryomova02a9462017-06-19 12:18:05 +02001328 clear_backoffs(osd);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001329
1330 __remove_osd_from_lru(osd);
1331 erase_osd(&osdc->osds, osd);
1332 put_osd(osd);
1333}
1334
1335/*
Sage Weilf24e9982009-10-06 11:31:10 -07001336 * reset osd connect
1337 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001338static int reopen_osd(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001339{
Alex Elderc3acb182012-12-07 09:57:58 -06001340 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001341
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001342 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1343
1344 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001345 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001346 close_osd(osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001347 return -ENODEV;
1348 }
1349
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001350 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
Alex Elderc3acb182012-12-07 09:57:58 -06001351 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1352 !ceph_con_opened(&osd->o_con)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001353 struct rb_node *n;
Alex Elderc3acb182012-12-07 09:57:58 -06001354
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001355 dout("osd addr hasn't changed and connection never opened, "
1356 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001357 /* touch each r_stamp for handle_timeout()'s benfit */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001358 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1359 struct ceph_osd_request *req =
1360 rb_entry(n, struct ceph_osd_request, r_node);
Sage Weil87b315a2010-03-22 14:51:18 -07001361 req->r_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001362 }
Alex Elderc3acb182012-12-07 09:57:58 -06001363
1364 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001365 }
Alex Elderc3acb182012-12-07 09:57:58 -06001366
1367 ceph_con_close(&osd->o_con);
1368 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1369 osd->o_incarnation++;
1370
1371 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001372}
1373
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001374static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1375 bool wrlocked)
Sage Weilf24e9982009-10-06 11:31:10 -07001376{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001377 struct ceph_osd *osd;
1378
1379 if (wrlocked)
1380 verify_osdc_wrlocked(osdc);
1381 else
1382 verify_osdc_locked(osdc);
1383
1384 if (o != CEPH_HOMELESS_OSD)
1385 osd = lookup_osd(&osdc->osds, o);
1386 else
1387 osd = &osdc->homeless_osd;
1388 if (!osd) {
1389 if (!wrlocked)
1390 return ERR_PTR(-EAGAIN);
1391
1392 osd = create_osd(osdc, o);
1393 insert_osd(&osdc->osds, osd);
1394 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1395 &osdc->osdmap->osd_addr[osd->o_osd]);
1396 }
1397
1398 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1399 return osd;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001400}
1401
Sage Weilf24e9982009-10-06 11:31:10 -07001402/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001403 * Create request <-> OSD session relation.
1404 *
1405 * @req has to be assigned a tid, @osd may be homeless.
Sage Weilf24e9982009-10-06 11:31:10 -07001406 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001407static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001408{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001409 verify_osd_locked(osd);
1410 WARN_ON(!req->r_tid || req->r_osd);
1411 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1412 req, req->r_tid);
Sage Weil35f9f8a2012-05-16 15:16:38 -05001413
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001414 if (!osd_homeless(osd))
1415 __remove_osd_from_lru(osd);
1416 else
1417 atomic_inc(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001418
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001419 get_osd(osd);
1420 insert_request(&osd->o_requests, req);
1421 req->r_osd = osd;
Sage Weilf24e9982009-10-06 11:31:10 -07001422}
1423
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001424static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001425{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001426 verify_osd_locked(osd);
1427 WARN_ON(req->r_osd != osd);
1428 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1429 req, req->r_tid);
1430
1431 req->r_osd = NULL;
1432 erase_request(&osd->o_requests, req);
1433 put_osd(osd);
1434
1435 if (!osd_homeless(osd))
1436 maybe_move_osd_to_lru(osd);
1437 else
1438 atomic_dec(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001439}
1440
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001441static bool __pool_full(struct ceph_pg_pool_info *pi)
1442{
1443 return pi->flags & CEPH_POOL_FLAG_FULL;
1444}
1445
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001446static bool have_pool_full(struct ceph_osd_client *osdc)
1447{
1448 struct rb_node *n;
1449
1450 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1451 struct ceph_pg_pool_info *pi =
1452 rb_entry(n, struct ceph_pg_pool_info, node);
1453
1454 if (__pool_full(pi))
1455 return true;
1456 }
1457
1458 return false;
1459}
1460
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001461static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1462{
1463 struct ceph_pg_pool_info *pi;
1464
1465 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1466 if (!pi)
1467 return false;
1468
1469 return __pool_full(pi);
1470}
1471
Sage Weilf24e9982009-10-06 11:31:10 -07001472/*
Josh Durgind29adb32013-12-02 19:11:48 -08001473 * Returns whether a request should be blocked from being sent
1474 * based on the current osdmap and osd_client settings.
Josh Durgind29adb32013-12-02 19:11:48 -08001475 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001476static bool target_should_be_paused(struct ceph_osd_client *osdc,
1477 const struct ceph_osd_request_target *t,
1478 struct ceph_pg_pool_info *pi)
1479{
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001480 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1481 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1482 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001483 __pool_full(pi);
1484
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001485 WARN_ON(pi->id != t->target_oloc.pool);
Jeff Layton58eb7932017-04-18 09:21:16 -04001486 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1487 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1488 (osdc->osdmap->epoch < osdc->epoch_barrier);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001489}
1490
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001491enum calc_target_result {
1492 CALC_TARGET_NO_ACTION = 0,
1493 CALC_TARGET_NEED_RESEND,
1494 CALC_TARGET_POOL_DNE,
1495};
1496
1497static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1498 struct ceph_osd_request_target *t,
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001499 struct ceph_connection *con,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001500 bool any_change)
1501{
1502 struct ceph_pg_pool_info *pi;
1503 struct ceph_pg pgid, last_pgid;
1504 struct ceph_osds up, acting;
1505 bool force_resend = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001506 bool unpaused = false;
1507 bool legacy_change;
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001508 bool split = false;
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001509 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001510 bool recovery_deletes = ceph_osdmap_flag(osdc,
1511 CEPH_OSDMAP_RECOVERY_DELETES);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001512 enum calc_target_result ct_res;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001513
Ilya Dryomov04c7d782017-06-15 16:30:55 +02001514 t->epoch = osdc->osdmap->epoch;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001515 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1516 if (!pi) {
1517 t->osd = CEPH_HOMELESS_OSD;
1518 ct_res = CALC_TARGET_POOL_DNE;
1519 goto out;
1520 }
1521
1522 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001523 if (t->last_force_resend < pi->last_force_request_resend) {
1524 t->last_force_resend = pi->last_force_request_resend;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001525 force_resend = true;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001526 } else if (t->last_force_resend == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001527 force_resend = true;
1528 }
1529 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001530
Ilya Dryomovdb098ec2017-06-15 16:30:55 +02001531 /* apply tiering */
1532 ceph_oid_copy(&t->target_oid, &t->base_oid);
1533 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1534 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001535 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1536 t->target_oloc.pool = pi->read_tier;
1537 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1538 t->target_oloc.pool = pi->write_tier;
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001539
1540 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1541 if (!pi) {
1542 t->osd = CEPH_HOMELESS_OSD;
1543 ct_res = CALC_TARGET_POOL_DNE;
1544 goto out;
1545 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001546 }
1547
Ilya Dryomova86f0092018-05-23 14:46:53 +02001548 __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001549 last_pgid.pool = pgid.pool;
1550 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1551
Ilya Dryomovdf281522017-06-15 16:30:56 +02001552 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001553 if (any_change &&
1554 ceph_is_new_interval(&t->acting,
1555 &acting,
1556 &t->up,
1557 &up,
1558 t->size,
1559 pi->size,
1560 t->min_size,
1561 pi->min_size,
1562 t->pg_num,
1563 pi->pg_num,
1564 t->sort_bitwise,
1565 sort_bitwise,
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001566 t->recovery_deletes,
1567 recovery_deletes,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001568 &last_pgid))
1569 force_resend = true;
1570
1571 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1572 t->paused = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001573 unpaused = true;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001574 }
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001575 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1576 ceph_osds_changed(&t->acting, &acting, any_change);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001577 if (t->pg_num)
1578 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001579
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001580 if (legacy_change || force_resend || split) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001581 t->pgid = pgid; /* struct */
Ilya Dryomovdf281522017-06-15 16:30:56 +02001582 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001583 ceph_osds_copy(&t->acting, &acting);
1584 ceph_osds_copy(&t->up, &up);
1585 t->size = pi->size;
1586 t->min_size = pi->min_size;
1587 t->pg_num = pi->pg_num;
1588 t->pg_num_mask = pi->pg_num_mask;
1589 t->sort_bitwise = sort_bitwise;
Ilya Dryomovae78dd82017-07-27 17:59:14 +02001590 t->recovery_deletes = recovery_deletes;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001591
1592 t->osd = acting.primary;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001593 }
1594
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001595 if (unpaused || legacy_change || force_resend ||
1596 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1597 RESEND_ON_SPLIT)))
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001598 ct_res = CALC_TARGET_NEED_RESEND;
1599 else
1600 ct_res = CALC_TARGET_NO_ACTION;
1601
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001602out:
1603 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1604 return ct_res;
1605}
1606
Ilya Dryomova02a9462017-06-19 12:18:05 +02001607static struct ceph_spg_mapping *alloc_spg_mapping(void)
1608{
1609 struct ceph_spg_mapping *spg;
1610
1611 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1612 if (!spg)
1613 return NULL;
1614
1615 RB_CLEAR_NODE(&spg->node);
1616 spg->backoffs = RB_ROOT;
1617 return spg;
1618}
1619
1620static void free_spg_mapping(struct ceph_spg_mapping *spg)
1621{
1622 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1623 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1624
1625 kfree(spg);
1626}
1627
1628/*
1629 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1630 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1631 * defined only within a specific spgid; it does not pass anything to
1632 * children on split, or to another primary.
1633 */
1634DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1635 RB_BYPTR, const struct ceph_spg *, node)
1636
1637static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1638{
1639 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1640}
1641
1642static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1643 void **pkey, size_t *pkey_len)
1644{
1645 if (hoid->key_len) {
1646 *pkey = hoid->key;
1647 *pkey_len = hoid->key_len;
1648 } else {
1649 *pkey = hoid->oid;
1650 *pkey_len = hoid->oid_len;
1651 }
1652}
1653
1654static int compare_names(const void *name1, size_t name1_len,
1655 const void *name2, size_t name2_len)
1656{
1657 int ret;
1658
1659 ret = memcmp(name1, name2, min(name1_len, name2_len));
1660 if (!ret) {
1661 if (name1_len < name2_len)
1662 ret = -1;
1663 else if (name1_len > name2_len)
1664 ret = 1;
1665 }
1666 return ret;
1667}
1668
1669static int hoid_compare(const struct ceph_hobject_id *lhs,
1670 const struct ceph_hobject_id *rhs)
1671{
1672 void *effective_key1, *effective_key2;
1673 size_t effective_key1_len, effective_key2_len;
1674 int ret;
1675
1676 if (lhs->is_max < rhs->is_max)
1677 return -1;
1678 if (lhs->is_max > rhs->is_max)
1679 return 1;
1680
1681 if (lhs->pool < rhs->pool)
1682 return -1;
1683 if (lhs->pool > rhs->pool)
1684 return 1;
1685
1686 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1687 return -1;
1688 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1689 return 1;
1690
1691 ret = compare_names(lhs->nspace, lhs->nspace_len,
1692 rhs->nspace, rhs->nspace_len);
1693 if (ret)
1694 return ret;
1695
1696 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1697 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1698 ret = compare_names(effective_key1, effective_key1_len,
1699 effective_key2, effective_key2_len);
1700 if (ret)
1701 return ret;
1702
1703 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1704 if (ret)
1705 return ret;
1706
1707 if (lhs->snapid < rhs->snapid)
1708 return -1;
1709 if (lhs->snapid > rhs->snapid)
1710 return 1;
1711
1712 return 0;
1713}
1714
1715/*
1716 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1717 * compat stuff here.
1718 *
1719 * Assumes @hoid is zero-initialized.
1720 */
1721static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1722{
1723 u8 struct_v;
1724 u32 struct_len;
1725 int ret;
1726
1727 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1728 &struct_len);
1729 if (ret)
1730 return ret;
1731
1732 if (struct_v < 4) {
1733 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1734 goto e_inval;
1735 }
1736
1737 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1738 GFP_NOIO);
1739 if (IS_ERR(hoid->key)) {
1740 ret = PTR_ERR(hoid->key);
1741 hoid->key = NULL;
1742 return ret;
1743 }
1744
1745 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1746 GFP_NOIO);
1747 if (IS_ERR(hoid->oid)) {
1748 ret = PTR_ERR(hoid->oid);
1749 hoid->oid = NULL;
1750 return ret;
1751 }
1752
1753 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1754 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1755 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1756
1757 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1758 GFP_NOIO);
1759 if (IS_ERR(hoid->nspace)) {
1760 ret = PTR_ERR(hoid->nspace);
1761 hoid->nspace = NULL;
1762 return ret;
1763 }
1764
1765 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1766
1767 ceph_hoid_build_hash_cache(hoid);
1768 return 0;
1769
1770e_inval:
1771 return -EINVAL;
1772}
1773
1774static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1775{
1776 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1777 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1778}
1779
1780static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1781{
1782 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1783 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1784 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1785 ceph_encode_64(p, hoid->snapid);
1786 ceph_encode_32(p, hoid->hash);
1787 ceph_encode_8(p, hoid->is_max);
1788 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1789 ceph_encode_64(p, hoid->pool);
1790}
1791
1792static void free_hoid(struct ceph_hobject_id *hoid)
1793{
1794 if (hoid) {
1795 kfree(hoid->key);
1796 kfree(hoid->oid);
1797 kfree(hoid->nspace);
1798 kfree(hoid);
1799 }
1800}
1801
1802static struct ceph_osd_backoff *alloc_backoff(void)
1803{
1804 struct ceph_osd_backoff *backoff;
1805
1806 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1807 if (!backoff)
1808 return NULL;
1809
1810 RB_CLEAR_NODE(&backoff->spg_node);
1811 RB_CLEAR_NODE(&backoff->id_node);
1812 return backoff;
1813}
1814
1815static void free_backoff(struct ceph_osd_backoff *backoff)
1816{
1817 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1818 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1819
1820 free_hoid(backoff->begin);
1821 free_hoid(backoff->end);
1822 kfree(backoff);
1823}
1824
1825/*
1826 * Within a specific spgid, backoffs are managed by ->begin hoid.
1827 */
1828DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1829 RB_BYVAL, spg_node);
1830
1831static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1832 const struct ceph_hobject_id *hoid)
1833{
1834 struct rb_node *n = root->rb_node;
1835
1836 while (n) {
1837 struct ceph_osd_backoff *cur =
1838 rb_entry(n, struct ceph_osd_backoff, spg_node);
1839 int cmp;
1840
1841 cmp = hoid_compare(hoid, cur->begin);
1842 if (cmp < 0) {
1843 n = n->rb_left;
1844 } else if (cmp > 0) {
1845 if (hoid_compare(hoid, cur->end) < 0)
1846 return cur;
1847
1848 n = n->rb_right;
1849 } else {
1850 return cur;
1851 }
1852 }
1853
1854 return NULL;
1855}
1856
1857/*
1858 * Each backoff has a unique id within its OSD session.
1859 */
1860DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1861
1862static void clear_backoffs(struct ceph_osd *osd)
1863{
1864 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1865 struct ceph_spg_mapping *spg =
1866 rb_entry(rb_first(&osd->o_backoff_mappings),
1867 struct ceph_spg_mapping, node);
1868
1869 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1870 struct ceph_osd_backoff *backoff =
1871 rb_entry(rb_first(&spg->backoffs),
1872 struct ceph_osd_backoff, spg_node);
1873
1874 erase_backoff(&spg->backoffs, backoff);
1875 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1876 free_backoff(backoff);
1877 }
1878 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1879 free_spg_mapping(spg);
1880 }
1881}
1882
1883/*
1884 * Set up a temporary, non-owning view into @t.
1885 */
1886static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1887 const struct ceph_osd_request_target *t)
1888{
1889 hoid->key = NULL;
1890 hoid->key_len = 0;
1891 hoid->oid = t->target_oid.name;
1892 hoid->oid_len = t->target_oid.name_len;
1893 hoid->snapid = CEPH_NOSNAP;
1894 hoid->hash = t->pgid.seed;
1895 hoid->is_max = false;
1896 if (t->target_oloc.pool_ns) {
1897 hoid->nspace = t->target_oloc.pool_ns->str;
1898 hoid->nspace_len = t->target_oloc.pool_ns->len;
1899 } else {
1900 hoid->nspace = NULL;
1901 hoid->nspace_len = 0;
1902 }
1903 hoid->pool = t->target_oloc.pool;
1904 ceph_hoid_build_hash_cache(hoid);
1905}
1906
1907static bool should_plug_request(struct ceph_osd_request *req)
1908{
1909 struct ceph_osd *osd = req->r_osd;
1910 struct ceph_spg_mapping *spg;
1911 struct ceph_osd_backoff *backoff;
1912 struct ceph_hobject_id hoid;
1913
1914 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1915 if (!spg)
1916 return false;
1917
1918 hoid_fill_from_target(&hoid, &req->r_t);
1919 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1920 if (!backoff)
1921 return false;
1922
1923 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1924 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1925 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1926 return true;
1927}
1928
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001929/*
1930 * Keep get_num_data_items() in sync with this function.
1931 */
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001932static void setup_request_data(struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001933{
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001934 struct ceph_msg *request_msg = req->r_request;
1935 struct ceph_msg *reply_msg = req->r_reply;
1936 struct ceph_osd_req_op *op;
Sage Weilf24e9982009-10-06 11:31:10 -07001937
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001938 if (req->r_request->num_data_items || req->r_reply->num_data_items)
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001939 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001940
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001941 WARN_ON(request_msg->data_length || reply_msg->data_length);
1942 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001943 switch (op->op) {
1944 /* request */
1945 case CEPH_OSD_OP_WRITE:
1946 case CEPH_OSD_OP_WRITEFULL:
1947 WARN_ON(op->indata_len != op->extent.length);
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001948 ceph_osdc_msg_data_add(request_msg,
1949 &op->extent.osd_data);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001950 break;
1951 case CEPH_OSD_OP_SETXATTR:
1952 case CEPH_OSD_OP_CMPXATTR:
1953 WARN_ON(op->indata_len != op->xattr.name_len +
1954 op->xattr.value_len);
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001955 ceph_osdc_msg_data_add(request_msg,
1956 &op->xattr.osd_data);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001957 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001958 case CEPH_OSD_OP_NOTIFY_ACK:
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001959 ceph_osdc_msg_data_add(request_msg,
Ilya Dryomov922dab62016-05-26 01:15:02 +02001960 &op->notify_ack.request_data);
1961 break;
Luis Henriques23ddf9b2018-10-15 16:45:58 +01001962 case CEPH_OSD_OP_COPY_FROM:
1963 ceph_osdc_msg_data_add(request_msg,
1964 &op->copy_from.osd_data);
1965 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001966
1967 /* reply */
1968 case CEPH_OSD_OP_STAT:
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001969 ceph_osdc_msg_data_add(reply_msg,
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001970 &op->raw_data_in);
1971 break;
1972 case CEPH_OSD_OP_READ:
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001973 ceph_osdc_msg_data_add(reply_msg,
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001974 &op->extent.osd_data);
1975 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001976 case CEPH_OSD_OP_LIST_WATCHERS:
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001977 ceph_osdc_msg_data_add(reply_msg,
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001978 &op->list_watchers.response_data);
1979 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001980
1981 /* both */
1982 case CEPH_OSD_OP_CALL:
1983 WARN_ON(op->indata_len != op->cls.class_len +
1984 op->cls.method_len +
1985 op->cls.indata_len);
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001986 ceph_osdc_msg_data_add(request_msg,
1987 &op->cls.request_info);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001988 /* optional, can be NONE */
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001989 ceph_osdc_msg_data_add(request_msg,
1990 &op->cls.request_data);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001991 /* optional, can be NONE */
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001992 ceph_osdc_msg_data_add(reply_msg,
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001993 &op->cls.response_data);
1994 break;
Ilya Dryomov19079202016-04-28 16:07:27 +02001995 case CEPH_OSD_OP_NOTIFY:
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001996 ceph_osdc_msg_data_add(request_msg,
Ilya Dryomov19079202016-04-28 16:07:27 +02001997 &op->notify.request_data);
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02001998 ceph_osdc_msg_data_add(reply_msg,
Ilya Dryomov19079202016-04-28 16:07:27 +02001999 &op->notify.response_data);
2000 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002001 }
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002002 }
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002003}
2004
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02002005static void encode_pgid(void **p, const struct ceph_pg *pgid)
2006{
2007 ceph_encode_8(p, 1);
2008 ceph_encode_64(p, pgid->pool);
2009 ceph_encode_32(p, pgid->seed);
2010 ceph_encode_32(p, -1); /* preferred */
2011}
2012
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002013static void encode_spgid(void **p, const struct ceph_spg *spgid)
2014{
2015 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
2016 encode_pgid(p, &spgid->pgid);
2017 ceph_encode_8(p, spgid->shard);
2018}
2019
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02002020static void encode_oloc(void **p, void *end,
2021 const struct ceph_object_locator *oloc)
2022{
2023 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
2024 ceph_encode_64(p, oloc->pool);
2025 ceph_encode_32(p, -1); /* preferred */
2026 ceph_encode_32(p, 0); /* key len */
2027 if (oloc->pool_ns)
2028 ceph_encode_string(p, end, oloc->pool_ns->str,
2029 oloc->pool_ns->len);
2030 else
2031 ceph_encode_32(p, 0);
2032}
2033
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002034static void encode_request_partial(struct ceph_osd_request *req,
2035 struct ceph_msg *msg)
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002036{
2037 void *p = msg->front.iov_base;
2038 void *const end = p + msg->front_alloc_len;
2039 u32 data_len = 0;
2040 int i;
2041
2042 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2043 /* snapshots aren't writeable */
2044 WARN_ON(req->r_snapid != CEPH_NOSNAP);
2045 } else {
2046 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2047 req->r_data_offset || req->r_snapc);
2048 }
2049
Ilya Dryomov98c4bfe2018-10-17 14:23:04 +02002050 setup_request_data(req);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002051
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002052 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
2053 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002054 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2055 ceph_encode_32(&p, req->r_flags);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002056
2057 /* reqid */
2058 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
2059 memset(p, 0, sizeof(struct ceph_osd_reqid));
2060 p += sizeof(struct ceph_osd_reqid);
2061
2062 /* trace */
2063 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
2064 p += sizeof(struct ceph_blkin_trace_info);
2065
2066 ceph_encode_32(&p, 0); /* client_inc, always 0 */
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02002067 ceph_encode_timespec64(p, &req->r_mtime);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002068 p += sizeof(struct ceph_timespec);
Jeff Laytonaa26d662017-04-04 08:39:36 -04002069
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02002070 encode_oloc(&p, end, &req->r_t.target_oloc);
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02002071 ceph_encode_string(&p, end, req->r_t.target_oid.name,
2072 req->r_t.target_oid.name_len);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002073
2074 /* ops, can imply data */
2075 ceph_encode_16(&p, req->r_num_ops);
2076 for (i = 0; i < req->r_num_ops; i++) {
2077 data_len += osd_req_encode_op(p, &req->r_ops[i]);
2078 p += sizeof(struct ceph_osd_op);
2079 }
2080
2081 ceph_encode_64(&p, req->r_snapid); /* snapid */
2082 if (req->r_snapc) {
2083 ceph_encode_64(&p, req->r_snapc->seq);
2084 ceph_encode_32(&p, req->r_snapc->num_snaps);
2085 for (i = 0; i < req->r_snapc->num_snaps; i++)
2086 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2087 } else {
2088 ceph_encode_64(&p, 0); /* snap_seq */
2089 ceph_encode_32(&p, 0); /* snaps len */
2090 }
2091
2092 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002093 BUG_ON(p > end - 8); /* space for features */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002094
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002095 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2096 /* front_len is finalized in encode_request_finish() */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002097 msg->front.iov_len = p - msg->front.iov_base;
2098 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002099 msg->hdr.data_len = cpu_to_le32(data_len);
2100 /*
2101 * The header "data_off" is a hint to the receiver allowing it
2102 * to align received data into its buffers such that there's no
2103 * need to re-copy it before writing it to disk (direct I/O).
2104 */
2105 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2106
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002107 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2108 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2109}
2110
2111static void encode_request_finish(struct ceph_msg *msg)
2112{
2113 void *p = msg->front.iov_base;
Ilya Dryomov986e8982017-07-25 14:40:03 +02002114 void *const partial_end = p + msg->front.iov_len;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002115 void *const end = p + msg->front_alloc_len;
2116
2117 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2118 /* luminous OSD -- encode features and be done */
Ilya Dryomov986e8982017-07-25 14:40:03 +02002119 p = partial_end;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002120 ceph_encode_64(&p, msg->con->peer_features);
2121 } else {
2122 struct {
2123 char spgid[CEPH_ENCODING_START_BLK_LEN +
2124 CEPH_PGID_ENCODING_LEN + 1];
2125 __le32 hash;
2126 __le32 epoch;
2127 __le32 flags;
2128 char reqid[CEPH_ENCODING_START_BLK_LEN +
2129 sizeof(struct ceph_osd_reqid)];
2130 char trace[sizeof(struct ceph_blkin_trace_info)];
2131 __le32 client_inc;
2132 struct ceph_timespec mtime;
2133 } __packed head;
2134 struct ceph_pg pgid;
2135 void *oloc, *oid, *tail;
2136 int oloc_len, oid_len, tail_len;
2137 int len;
2138
2139 /*
2140 * Pre-luminous OSD -- reencode v8 into v4 using @head
2141 * as a temporary buffer. Encode the raw PG; the rest
2142 * is just a matter of moving oloc, oid and tail blobs
2143 * around.
2144 */
2145 memcpy(&head, p, sizeof(head));
2146 p += sizeof(head);
2147
2148 oloc = p;
2149 p += CEPH_ENCODING_START_BLK_LEN;
2150 pgid.pool = ceph_decode_64(&p);
2151 p += 4 + 4; /* preferred, key len */
2152 len = ceph_decode_32(&p);
2153 p += len; /* nspace */
2154 oloc_len = p - oloc;
2155
2156 oid = p;
2157 len = ceph_decode_32(&p);
2158 p += len;
2159 oid_len = p - oid;
2160
2161 tail = p;
Ilya Dryomov986e8982017-07-25 14:40:03 +02002162 tail_len = partial_end - p;
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002163
2164 p = msg->front.iov_base;
2165 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2166 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2167 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2168 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2169
2170 /* reassert_version */
2171 memset(p, 0, sizeof(struct ceph_eversion));
2172 p += sizeof(struct ceph_eversion);
2173
2174 BUG_ON(p >= oloc);
2175 memmove(p, oloc, oloc_len);
2176 p += oloc_len;
2177
2178 pgid.seed = le32_to_cpu(head.hash);
2179 encode_pgid(&p, &pgid); /* raw pg */
2180
2181 BUG_ON(p >= oid);
2182 memmove(p, oid, oid_len);
2183 p += oid_len;
2184
2185 /* tail -- ops, snapid, snapc, retry_attempt */
2186 BUG_ON(p >= tail);
2187 memmove(p, tail, tail_len);
2188 p += tail_len;
2189
2190 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2191 }
2192
2193 BUG_ON(p > end);
2194 msg->front.iov_len = p - msg->front.iov_base;
2195 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2196
2197 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2198 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2199 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2200 le16_to_cpu(msg->hdr.version));
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002201}
2202
2203/*
2204 * @req has to be assigned a tid and registered.
2205 */
2206static void send_request(struct ceph_osd_request *req)
2207{
2208 struct ceph_osd *osd = req->r_osd;
2209
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002210 verify_osd_locked(osd);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002211 WARN_ON(osd->o_osd != req->r_t.osd);
2212
Ilya Dryomova02a9462017-06-19 12:18:05 +02002213 /* backoff? */
2214 if (should_plug_request(req))
2215 return;
2216
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002217 /*
2218 * We may have a previously queued request message hanging
2219 * around. Cancel it to avoid corrupting the msgr.
2220 */
2221 if (req->r_sent)
2222 ceph_msg_revoke(req->r_request);
2223
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002224 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2225 if (req->r_attempts)
2226 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2227 else
2228 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2229
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002230 encode_request_partial(req, req->r_request);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002231
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002232 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 +02002233 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002234 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002235 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2236 req->r_attempts);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002237
2238 req->r_t.paused = false;
Sage Weil3dd72fc2010-03-22 14:42:30 -07002239 req->r_stamp = jiffies;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002240 req->r_attempts++;
Sage Weilf24e9982009-10-06 11:31:10 -07002241
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002242 req->r_sent = osd->o_incarnation;
2243 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2244 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
Sage Weilf24e9982009-10-06 11:31:10 -07002245}
2246
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002247static void maybe_request_map(struct ceph_osd_client *osdc)
2248{
2249 bool continuous = false;
2250
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002251 verify_osdc_locked(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002252 WARN_ON(!osdc->osdmap->epoch);
2253
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002254 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2255 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2256 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002257 dout("%s osdc %p continuous\n", __func__, osdc);
2258 continuous = true;
2259 } else {
2260 dout("%s osdc %p onetime\n", __func__, osdc);
2261 }
2262
2263 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2264 osdc->osdmap->epoch + 1, continuous))
2265 ceph_monc_renew_subs(&osdc->client->monc);
2266}
2267
Jeff Laytona1f40202017-04-04 08:39:37 -04002268static void complete_request(struct ceph_osd_request *req, int err);
Ilya Dryomov46092452016-04-28 16:07:27 +02002269static void send_map_check(struct ceph_osd_request *req);
2270
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002271static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002272{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002273 struct ceph_osd_client *osdc = req->r_osdc;
2274 struct ceph_osd *osd;
Ilya Dryomov46092452016-04-28 16:07:27 +02002275 enum calc_target_result ct_res;
Ilya Dryomov66850df2018-05-15 15:47:58 +02002276 int err = 0;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002277 bool need_send = false;
2278 bool promoted = false;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002279
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002280 WARN_ON(req->r_tid);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002281 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2282
2283again:
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002284 ct_res = calc_target(osdc, &req->r_t, NULL, false);
Ilya Dryomov46092452016-04-28 16:07:27 +02002285 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2286 goto promote;
2287
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002288 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2289 if (IS_ERR(osd)) {
2290 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2291 goto promote;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002292 }
2293
Ilya Dryomov66850df2018-05-15 15:47:58 +02002294 if (osdc->abort_err) {
2295 dout("req %p abort_err %d\n", req, osdc->abort_err);
2296 err = osdc->abort_err;
2297 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
Jeff Layton58eb7932017-04-18 09:21:16 -04002298 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2299 osdc->epoch_barrier);
2300 req->r_t.paused = true;
2301 maybe_request_map(osdc);
2302 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2303 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002304 dout("req %p pausewr\n", req);
2305 req->r_t.paused = true;
2306 maybe_request_map(osdc);
2307 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002308 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002309 dout("req %p pauserd\n", req);
2310 req->r_t.paused = true;
2311 maybe_request_map(osdc);
2312 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2313 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2314 CEPH_OSD_FLAG_FULL_FORCE)) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002315 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002316 pool_full(osdc, req->r_t.base_oloc.pool))) {
2317 dout("req %p full/pool_full\n", req);
Ilya Dryomovc843d132018-05-30 16:29:14 +02002318 if (osdc->abort_on_full) {
Ilya Dryomov66850df2018-05-15 15:47:58 +02002319 err = -ENOSPC;
Ilya Dryomov29e87822018-05-17 16:13:07 +02002320 } else {
2321 pr_warn_ratelimited("FULL or reached pool quota\n");
2322 req->r_t.paused = true;
2323 maybe_request_map(osdc);
2324 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002325 } else if (!osd_homeless(osd)) {
2326 need_send = true;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002327 } else {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002328 maybe_request_map(osdc);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002329 }
2330
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002331 mutex_lock(&osd->lock);
2332 /*
2333 * Assign the tid atomically with send_request() to protect
2334 * multiple writes to the same object from racing with each
2335 * other, resulting in out of order ops on the OSDs.
2336 */
2337 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2338 link_request(osd, req);
2339 if (need_send)
2340 send_request(req);
Ilya Dryomov66850df2018-05-15 15:47:58 +02002341 else if (err)
2342 complete_request(req, err);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002343 mutex_unlock(&osd->lock);
2344
Ilya Dryomov60015672018-05-22 16:26:51 +02002345 if (!err && ct_res == CALC_TARGET_POOL_DNE)
Ilya Dryomov46092452016-04-28 16:07:27 +02002346 send_map_check(req);
2347
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002348 if (promoted)
2349 downgrade_write(&osdc->lock);
2350 return;
2351
2352promote:
2353 up_read(&osdc->lock);
2354 down_write(&osdc->lock);
2355 wrlocked = true;
2356 promoted = true;
2357 goto again;
2358}
2359
2360static void account_request(struct ceph_osd_request *req)
2361{
Ilya Dryomov54ea0042017-02-11 18:48:41 +01002362 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002363 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002364
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002365 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002366 atomic_inc(&req->r_osdc->num_requests);
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002367
2368 req->r_start_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002369}
2370
2371static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2372{
2373 ceph_osdc_get_request(req);
2374 account_request(req);
2375 __submit_request(req, wrlocked);
2376}
2377
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002378static void finish_request(struct ceph_osd_request *req)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002379{
2380 struct ceph_osd_client *osdc = req->r_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002381
Ilya Dryomov46092452016-04-28 16:07:27 +02002382 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002383 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2384
2385 if (req->r_osd)
2386 unlink_request(req->r_osd, req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002387 atomic_dec(&osdc->num_requests);
2388
2389 /*
2390 * If an OSD has failed or returned and a request has been sent
2391 * twice, it's possible to get a reply and end up here while the
2392 * request message is queued for delivery. We will ignore the
2393 * reply, so not a big deal, but better to try and catch it.
2394 */
2395 ceph_msg_revoke(req->r_request);
2396 ceph_msg_revoke_incoming(req->r_reply);
2397}
2398
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002399static void __complete_request(struct ceph_osd_request *req)
2400{
Ilya Dryomov26df7262018-05-21 15:33:48 +02002401 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2402 req->r_tid, req->r_callback, req->r_result);
2403
2404 if (req->r_callback)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002405 req->r_callback(req);
Ilya Dryomov26df7262018-05-21 15:33:48 +02002406 complete_all(&req->r_completion);
2407 ceph_osdc_put_request(req);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002408}
2409
Ilya Dryomov88bc1922018-05-21 16:00:29 +02002410static void complete_request_workfn(struct work_struct *work)
2411{
2412 struct ceph_osd_request *req =
2413 container_of(work, struct ceph_osd_request, r_complete_work);
2414
2415 __complete_request(req);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002416}
2417
Ilya Dryomov46092452016-04-28 16:07:27 +02002418/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002419 * This is open-coded in handle_reply().
Ilya Dryomov46092452016-04-28 16:07:27 +02002420 */
2421static void complete_request(struct ceph_osd_request *req, int err)
2422{
2423 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2424
2425 req->r_result = err;
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002426 finish_request(req);
Ilya Dryomov88bc1922018-05-21 16:00:29 +02002427
2428 INIT_WORK(&req->r_complete_work, complete_request_workfn);
2429 queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
Ilya Dryomov46092452016-04-28 16:07:27 +02002430}
2431
2432static void cancel_map_check(struct ceph_osd_request *req)
2433{
2434 struct ceph_osd_client *osdc = req->r_osdc;
2435 struct ceph_osd_request *lookup_req;
2436
2437 verify_osdc_wrlocked(osdc);
2438
2439 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2440 if (!lookup_req)
2441 return;
2442
2443 WARN_ON(lookup_req != req);
2444 erase_request_mc(&osdc->map_checks, req);
2445 ceph_osdc_put_request(req);
2446}
2447
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002448static void cancel_request(struct ceph_osd_request *req)
2449{
2450 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2451
Ilya Dryomov46092452016-04-28 16:07:27 +02002452 cancel_map_check(req);
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002453 finish_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002454 complete_all(&req->r_completion);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01002455 ceph_osdc_put_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002456}
2457
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002458static void abort_request(struct ceph_osd_request *req, int err)
2459{
2460 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2461
2462 cancel_map_check(req);
2463 complete_request(req, err);
2464}
2465
Ilya Dryomov66850df2018-05-15 15:47:58 +02002466static int abort_fn(struct ceph_osd_request *req, void *arg)
2467{
2468 int err = *(int *)arg;
2469
2470 abort_request(req, err);
2471 return 0; /* continue iteration */
2472}
2473
2474/*
2475 * Abort all in-flight requests with @err and arrange for all future
2476 * requests to be failed immediately.
2477 */
2478void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2479{
2480 dout("%s osdc %p err %d\n", __func__, osdc, err);
2481 down_write(&osdc->lock);
2482 for_each_request(osdc, abort_fn, &err);
2483 osdc->abort_err = err;
2484 up_write(&osdc->lock);
2485}
2486EXPORT_SYMBOL(ceph_osdc_abort_requests);
2487
Jeff Layton58eb7932017-04-18 09:21:16 -04002488static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2489{
2490 if (likely(eb > osdc->epoch_barrier)) {
2491 dout("updating epoch_barrier from %u to %u\n",
2492 osdc->epoch_barrier, eb);
2493 osdc->epoch_barrier = eb;
2494 /* Request map if we're not to the barrier yet */
2495 if (eb > osdc->osdmap->epoch)
2496 maybe_request_map(osdc);
2497 }
2498}
2499
2500void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2501{
2502 down_read(&osdc->lock);
2503 if (unlikely(eb > osdc->epoch_barrier)) {
2504 up_read(&osdc->lock);
2505 down_write(&osdc->lock);
2506 update_epoch_barrier(osdc, eb);
2507 up_write(&osdc->lock);
2508 } else {
2509 up_read(&osdc->lock);
2510 }
2511}
2512EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2513
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002514/*
Ilya Dryomov4eea0fe2018-05-16 18:21:34 +02002515 * We can end up releasing caps as a result of abort_request().
2516 * In that case, we probably want to ensure that the cap release message
2517 * has an updated epoch barrier in it, so set the epoch barrier prior to
2518 * aborting the first request.
2519 */
2520static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2521{
2522 struct ceph_osd_client *osdc = req->r_osdc;
2523 bool *victims = arg;
2524
Ilya Dryomovc843d132018-05-30 16:29:14 +02002525 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
Ilya Dryomov4eea0fe2018-05-16 18:21:34 +02002526 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov690f9512018-05-30 14:58:25 +02002527 pool_full(osdc, req->r_t.base_oloc.pool))) {
Ilya Dryomov4eea0fe2018-05-16 18:21:34 +02002528 if (!*victims) {
2529 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2530 *victims = true;
2531 }
2532 abort_request(req, -ENOSPC);
2533 }
2534
2535 return 0; /* continue iteration */
2536}
2537
2538/*
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002539 * Drop all pending requests that are stalled waiting on a full condition to
Jeff Layton58eb7932017-04-18 09:21:16 -04002540 * clear, and complete them with ENOSPC as the return code. Set the
2541 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2542 * cancelled.
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002543 */
2544static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2545{
Jeff Layton58eb7932017-04-18 09:21:16 -04002546 bool victims = false;
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002547
Ilya Dryomovc843d132018-05-30 16:29:14 +02002548 if (osdc->abort_on_full &&
2549 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
Ilya Dryomov4eea0fe2018-05-16 18:21:34 +02002550 for_each_request(osdc, abort_on_full_fn, &victims);
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002551}
2552
Ilya Dryomov46092452016-04-28 16:07:27 +02002553static void check_pool_dne(struct ceph_osd_request *req)
2554{
2555 struct ceph_osd_client *osdc = req->r_osdc;
2556 struct ceph_osdmap *map = osdc->osdmap;
2557
2558 verify_osdc_wrlocked(osdc);
2559 WARN_ON(!map->epoch);
2560
2561 if (req->r_attempts) {
2562 /*
2563 * We sent a request earlier, which means that
2564 * previously the pool existed, and now it does not
2565 * (i.e., it was deleted).
2566 */
2567 req->r_map_dne_bound = map->epoch;
2568 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2569 req->r_tid);
2570 } else {
2571 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2572 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2573 }
2574
2575 if (req->r_map_dne_bound) {
2576 if (map->epoch >= req->r_map_dne_bound) {
2577 /* we had a new enough map */
2578 pr_info_ratelimited("tid %llu pool does not exist\n",
2579 req->r_tid);
2580 complete_request(req, -ENOENT);
2581 }
2582 } else {
2583 send_map_check(req);
2584 }
2585}
2586
2587static void map_check_cb(struct ceph_mon_generic_request *greq)
2588{
2589 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2590 struct ceph_osd_request *req;
2591 u64 tid = greq->private_data;
2592
2593 WARN_ON(greq->result || !greq->u.newest);
2594
2595 down_write(&osdc->lock);
2596 req = lookup_request_mc(&osdc->map_checks, tid);
2597 if (!req) {
2598 dout("%s tid %llu dne\n", __func__, tid);
2599 goto out_unlock;
2600 }
2601
2602 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2603 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2604 if (!req->r_map_dne_bound)
2605 req->r_map_dne_bound = greq->u.newest;
2606 erase_request_mc(&osdc->map_checks, req);
2607 check_pool_dne(req);
2608
2609 ceph_osdc_put_request(req);
2610out_unlock:
2611 up_write(&osdc->lock);
2612}
2613
2614static void send_map_check(struct ceph_osd_request *req)
2615{
2616 struct ceph_osd_client *osdc = req->r_osdc;
2617 struct ceph_osd_request *lookup_req;
2618 int ret;
2619
2620 verify_osdc_wrlocked(osdc);
2621
2622 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2623 if (lookup_req) {
2624 WARN_ON(lookup_req != req);
2625 return;
2626 }
2627
2628 ceph_osdc_get_request(req);
2629 insert_request_mc(&osdc->map_checks, req);
2630 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2631 map_check_cb, req->r_tid);
2632 WARN_ON(ret);
2633}
2634
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002635/*
Ilya Dryomov922dab62016-05-26 01:15:02 +02002636 * lingering requests, watch/notify v2 infrastructure
2637 */
2638static void linger_release(struct kref *kref)
2639{
2640 struct ceph_osd_linger_request *lreq =
2641 container_of(kref, struct ceph_osd_linger_request, kref);
2642
2643 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2644 lreq->reg_req, lreq->ping_req);
2645 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2646 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
Ilya Dryomov46092452016-04-28 16:07:27 +02002647 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002648 WARN_ON(!list_empty(&lreq->scan_item));
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002649 WARN_ON(!list_empty(&lreq->pending_lworks));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002650 WARN_ON(lreq->osd);
2651
2652 if (lreq->reg_req)
2653 ceph_osdc_put_request(lreq->reg_req);
2654 if (lreq->ping_req)
2655 ceph_osdc_put_request(lreq->ping_req);
2656 target_destroy(&lreq->t);
2657 kfree(lreq);
2658}
2659
2660static void linger_put(struct ceph_osd_linger_request *lreq)
2661{
2662 if (lreq)
2663 kref_put(&lreq->kref, linger_release);
2664}
2665
2666static struct ceph_osd_linger_request *
2667linger_get(struct ceph_osd_linger_request *lreq)
2668{
2669 kref_get(&lreq->kref);
2670 return lreq;
2671}
2672
2673static struct ceph_osd_linger_request *
2674linger_alloc(struct ceph_osd_client *osdc)
2675{
2676 struct ceph_osd_linger_request *lreq;
2677
2678 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2679 if (!lreq)
2680 return NULL;
2681
2682 kref_init(&lreq->kref);
2683 mutex_init(&lreq->lock);
2684 RB_CLEAR_NODE(&lreq->node);
2685 RB_CLEAR_NODE(&lreq->osdc_node);
Ilya Dryomov46092452016-04-28 16:07:27 +02002686 RB_CLEAR_NODE(&lreq->mc_node);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002687 INIT_LIST_HEAD(&lreq->scan_item);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002688 INIT_LIST_HEAD(&lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002689 init_completion(&lreq->reg_commit_wait);
Ilya Dryomov19079202016-04-28 16:07:27 +02002690 init_completion(&lreq->notify_finish_wait);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002691
2692 lreq->osdc = osdc;
2693 target_init(&lreq->t);
2694
2695 dout("%s lreq %p\n", __func__, lreq);
2696 return lreq;
2697}
2698
2699DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2700DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02002701DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002702
2703/*
2704 * Create linger request <-> OSD session relation.
2705 *
2706 * @lreq has to be registered, @osd may be homeless.
2707 */
2708static void link_linger(struct ceph_osd *osd,
2709 struct ceph_osd_linger_request *lreq)
2710{
2711 verify_osd_locked(osd);
2712 WARN_ON(!lreq->linger_id || lreq->osd);
2713 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2714 osd->o_osd, lreq, lreq->linger_id);
2715
2716 if (!osd_homeless(osd))
2717 __remove_osd_from_lru(osd);
2718 else
2719 atomic_inc(&osd->o_osdc->num_homeless);
2720
2721 get_osd(osd);
2722 insert_linger(&osd->o_linger_requests, lreq);
2723 lreq->osd = osd;
2724}
2725
2726static void unlink_linger(struct ceph_osd *osd,
2727 struct ceph_osd_linger_request *lreq)
2728{
2729 verify_osd_locked(osd);
2730 WARN_ON(lreq->osd != osd);
2731 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2732 osd->o_osd, lreq, lreq->linger_id);
2733
2734 lreq->osd = NULL;
2735 erase_linger(&osd->o_linger_requests, lreq);
2736 put_osd(osd);
2737
2738 if (!osd_homeless(osd))
2739 maybe_move_osd_to_lru(osd);
2740 else
2741 atomic_dec(&osd->o_osdc->num_homeless);
2742}
2743
2744static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2745{
2746 verify_osdc_locked(lreq->osdc);
2747
2748 return !RB_EMPTY_NODE(&lreq->osdc_node);
2749}
2750
2751static bool linger_registered(struct ceph_osd_linger_request *lreq)
2752{
2753 struct ceph_osd_client *osdc = lreq->osdc;
2754 bool registered;
2755
2756 down_read(&osdc->lock);
2757 registered = __linger_registered(lreq);
2758 up_read(&osdc->lock);
2759
2760 return registered;
2761}
2762
2763static void linger_register(struct ceph_osd_linger_request *lreq)
2764{
2765 struct ceph_osd_client *osdc = lreq->osdc;
2766
2767 verify_osdc_wrlocked(osdc);
2768 WARN_ON(lreq->linger_id);
2769
2770 linger_get(lreq);
2771 lreq->linger_id = ++osdc->last_linger_id;
2772 insert_linger_osdc(&osdc->linger_requests, lreq);
2773}
2774
2775static void linger_unregister(struct ceph_osd_linger_request *lreq)
2776{
2777 struct ceph_osd_client *osdc = lreq->osdc;
2778
2779 verify_osdc_wrlocked(osdc);
2780
2781 erase_linger_osdc(&osdc->linger_requests, lreq);
2782 linger_put(lreq);
2783}
2784
2785static void cancel_linger_request(struct ceph_osd_request *req)
2786{
2787 struct ceph_osd_linger_request *lreq = req->r_priv;
2788
2789 WARN_ON(!req->r_linger);
2790 cancel_request(req);
2791 linger_put(lreq);
2792}
2793
2794struct linger_work {
2795 struct work_struct work;
2796 struct ceph_osd_linger_request *lreq;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002797 struct list_head pending_item;
2798 unsigned long queued_stamp;
Ilya Dryomov922dab62016-05-26 01:15:02 +02002799
2800 union {
2801 struct {
2802 u64 notify_id;
2803 u64 notifier_id;
2804 void *payload; /* points into @msg front */
2805 size_t payload_len;
2806
2807 struct ceph_msg *msg; /* for ceph_msg_put() */
2808 } notify;
2809 struct {
2810 int err;
2811 } error;
2812 };
2813};
2814
2815static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2816 work_func_t workfn)
2817{
2818 struct linger_work *lwork;
2819
2820 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2821 if (!lwork)
2822 return NULL;
2823
2824 INIT_WORK(&lwork->work, workfn);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002825 INIT_LIST_HEAD(&lwork->pending_item);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002826 lwork->lreq = linger_get(lreq);
2827
2828 return lwork;
2829}
2830
2831static void lwork_free(struct linger_work *lwork)
2832{
2833 struct ceph_osd_linger_request *lreq = lwork->lreq;
2834
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002835 mutex_lock(&lreq->lock);
2836 list_del(&lwork->pending_item);
2837 mutex_unlock(&lreq->lock);
2838
Ilya Dryomov922dab62016-05-26 01:15:02 +02002839 linger_put(lreq);
2840 kfree(lwork);
2841}
2842
2843static void lwork_queue(struct linger_work *lwork)
2844{
2845 struct ceph_osd_linger_request *lreq = lwork->lreq;
2846 struct ceph_osd_client *osdc = lreq->osdc;
2847
2848 verify_lreq_locked(lreq);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002849 WARN_ON(!list_empty(&lwork->pending_item));
2850
2851 lwork->queued_stamp = jiffies;
2852 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002853 queue_work(osdc->notify_wq, &lwork->work);
2854}
2855
2856static void do_watch_notify(struct work_struct *w)
2857{
2858 struct linger_work *lwork = container_of(w, struct linger_work, work);
2859 struct ceph_osd_linger_request *lreq = lwork->lreq;
2860
2861 if (!linger_registered(lreq)) {
2862 dout("%s lreq %p not registered\n", __func__, lreq);
2863 goto out;
2864 }
2865
Ilya Dryomov19079202016-04-28 16:07:27 +02002866 WARN_ON(!lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002867 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2868 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2869 lwork->notify.payload_len);
2870 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2871 lwork->notify.notifier_id, lwork->notify.payload,
2872 lwork->notify.payload_len);
2873
2874out:
2875 ceph_msg_put(lwork->notify.msg);
2876 lwork_free(lwork);
2877}
2878
2879static void do_watch_error(struct work_struct *w)
2880{
2881 struct linger_work *lwork = container_of(w, struct linger_work, work);
2882 struct ceph_osd_linger_request *lreq = lwork->lreq;
2883
2884 if (!linger_registered(lreq)) {
2885 dout("%s lreq %p not registered\n", __func__, lreq);
2886 goto out;
2887 }
2888
2889 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2890 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2891
2892out:
2893 lwork_free(lwork);
2894}
2895
2896static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2897{
2898 struct linger_work *lwork;
2899
2900 lwork = lwork_alloc(lreq, do_watch_error);
2901 if (!lwork) {
2902 pr_err("failed to allocate error-lwork\n");
2903 return;
2904 }
2905
2906 lwork->error.err = lreq->last_error;
2907 lwork_queue(lwork);
2908}
2909
2910static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2911 int result)
2912{
2913 if (!completion_done(&lreq->reg_commit_wait)) {
2914 lreq->reg_commit_error = (result <= 0 ? result : 0);
2915 complete_all(&lreq->reg_commit_wait);
2916 }
2917}
2918
2919static void linger_commit_cb(struct ceph_osd_request *req)
2920{
2921 struct ceph_osd_linger_request *lreq = req->r_priv;
2922
2923 mutex_lock(&lreq->lock);
2924 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2925 lreq->linger_id, req->r_result);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002926 linger_reg_commit_complete(lreq, req->r_result);
2927 lreq->committed = true;
2928
Ilya Dryomov19079202016-04-28 16:07:27 +02002929 if (!lreq->is_watch) {
2930 struct ceph_osd_data *osd_data =
2931 osd_req_op_data(req, 0, notify, response_data);
2932 void *p = page_address(osd_data->pages[0]);
2933
2934 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2935 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2936
2937 /* make note of the notify_id */
2938 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2939 lreq->notify_id = ceph_decode_64(&p);
2940 dout("lreq %p notify_id %llu\n", lreq,
2941 lreq->notify_id);
2942 } else {
2943 dout("lreq %p no notify_id\n", lreq);
2944 }
2945 }
2946
Ilya Dryomov922dab62016-05-26 01:15:02 +02002947 mutex_unlock(&lreq->lock);
2948 linger_put(lreq);
2949}
2950
2951static int normalize_watch_error(int err)
2952{
2953 /*
2954 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2955 * notification and a failure to reconnect because we raced with
2956 * the delete appear the same to the user.
2957 */
2958 if (err == -ENOENT)
2959 err = -ENOTCONN;
2960
2961 return err;
2962}
2963
2964static void linger_reconnect_cb(struct ceph_osd_request *req)
2965{
2966 struct ceph_osd_linger_request *lreq = req->r_priv;
2967
2968 mutex_lock(&lreq->lock);
2969 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2970 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2971 if (req->r_result < 0) {
2972 if (!lreq->last_error) {
2973 lreq->last_error = normalize_watch_error(req->r_result);
2974 queue_watch_error(lreq);
2975 }
2976 }
2977
2978 mutex_unlock(&lreq->lock);
2979 linger_put(lreq);
2980}
2981
2982static void send_linger(struct ceph_osd_linger_request *lreq)
2983{
2984 struct ceph_osd_request *req = lreq->reg_req;
2985 struct ceph_osd_req_op *op = &req->r_ops[0];
2986
2987 verify_osdc_wrlocked(req->r_osdc);
2988 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2989
2990 if (req->r_osd)
2991 cancel_linger_request(req);
2992
2993 request_reinit(req);
2994 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2995 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2996 req->r_flags = lreq->t.flags;
2997 req->r_mtime = lreq->mtime;
2998
2999 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02003000 if (lreq->is_watch && lreq->committed) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02003001 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3002 op->watch.cookie != lreq->linger_id);
3003 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
3004 op->watch.gen = ++lreq->register_gen;
3005 dout("lreq %p reconnect register_gen %u\n", lreq,
3006 op->watch.gen);
3007 req->r_callback = linger_reconnect_cb;
3008 } else {
Ilya Dryomov19079202016-04-28 16:07:27 +02003009 if (!lreq->is_watch)
3010 lreq->notify_id = 0;
3011 else
3012 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003013 dout("lreq %p register\n", lreq);
3014 req->r_callback = linger_commit_cb;
3015 }
3016 mutex_unlock(&lreq->lock);
3017
3018 req->r_priv = linger_get(lreq);
3019 req->r_linger = true;
3020
3021 submit_request(req, true);
3022}
3023
3024static void linger_ping_cb(struct ceph_osd_request *req)
3025{
3026 struct ceph_osd_linger_request *lreq = req->r_priv;
3027
3028 mutex_lock(&lreq->lock);
3029 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3030 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3031 lreq->last_error);
3032 if (lreq->register_gen == req->r_ops[0].watch.gen) {
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02003033 if (!req->r_result) {
3034 lreq->watch_valid_thru = lreq->ping_sent;
3035 } else if (!lreq->last_error) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02003036 lreq->last_error = normalize_watch_error(req->r_result);
3037 queue_watch_error(lreq);
3038 }
3039 } else {
3040 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3041 lreq->register_gen, req->r_ops[0].watch.gen);
3042 }
3043
3044 mutex_unlock(&lreq->lock);
3045 linger_put(lreq);
3046}
3047
3048static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3049{
3050 struct ceph_osd_client *osdc = lreq->osdc;
3051 struct ceph_osd_request *req = lreq->ping_req;
3052 struct ceph_osd_req_op *op = &req->r_ops[0];
3053
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003054 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02003055 dout("%s PAUSERD\n", __func__);
3056 return;
3057 }
3058
3059 lreq->ping_sent = jiffies;
3060 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3061 __func__, lreq, lreq->linger_id, lreq->ping_sent,
3062 lreq->register_gen);
3063
3064 if (req->r_osd)
3065 cancel_linger_request(req);
3066
3067 request_reinit(req);
3068 target_copy(&req->r_t, &lreq->t);
3069
3070 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3071 op->watch.cookie != lreq->linger_id ||
3072 op->watch.op != CEPH_OSD_WATCH_OP_PING);
3073 op->watch.gen = lreq->register_gen;
3074 req->r_callback = linger_ping_cb;
3075 req->r_priv = linger_get(lreq);
3076 req->r_linger = true;
3077
3078 ceph_osdc_get_request(req);
3079 account_request(req);
3080 req->r_tid = atomic64_inc_return(&osdc->last_tid);
3081 link_request(lreq->osd, req);
3082 send_request(req);
3083}
3084
3085static void linger_submit(struct ceph_osd_linger_request *lreq)
3086{
3087 struct ceph_osd_client *osdc = lreq->osdc;
3088 struct ceph_osd *osd;
3089
Ilya Dryomov81c65212018-10-11 12:58:33 +02003090 down_write(&osdc->lock);
3091 linger_register(lreq);
3092 if (lreq->is_watch) {
3093 lreq->reg_req->r_ops[0].watch.cookie = lreq->linger_id;
3094 lreq->ping_req->r_ops[0].watch.cookie = lreq->linger_id;
3095 } else {
3096 lreq->reg_req->r_ops[0].notify.cookie = lreq->linger_id;
3097 }
3098
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003099 calc_target(osdc, &lreq->t, NULL, false);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003100 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3101 link_linger(osd, lreq);
3102
3103 send_linger(lreq);
Ilya Dryomov81c65212018-10-11 12:58:33 +02003104 up_write(&osdc->lock);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003105}
3106
Ilya Dryomov46092452016-04-28 16:07:27 +02003107static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3108{
3109 struct ceph_osd_client *osdc = lreq->osdc;
3110 struct ceph_osd_linger_request *lookup_lreq;
3111
3112 verify_osdc_wrlocked(osdc);
3113
3114 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3115 lreq->linger_id);
3116 if (!lookup_lreq)
3117 return;
3118
3119 WARN_ON(lookup_lreq != lreq);
3120 erase_linger_mc(&osdc->linger_map_checks, lreq);
3121 linger_put(lreq);
3122}
3123
Ilya Dryomov922dab62016-05-26 01:15:02 +02003124/*
3125 * @lreq has to be both registered and linked.
3126 */
3127static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3128{
Ilya Dryomov19079202016-04-28 16:07:27 +02003129 if (lreq->is_watch && lreq->ping_req->r_osd)
Ilya Dryomov922dab62016-05-26 01:15:02 +02003130 cancel_linger_request(lreq->ping_req);
3131 if (lreq->reg_req->r_osd)
3132 cancel_linger_request(lreq->reg_req);
Ilya Dryomov46092452016-04-28 16:07:27 +02003133 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003134 unlink_linger(lreq->osd, lreq);
3135 linger_unregister(lreq);
3136}
3137
3138static void linger_cancel(struct ceph_osd_linger_request *lreq)
3139{
3140 struct ceph_osd_client *osdc = lreq->osdc;
3141
3142 down_write(&osdc->lock);
3143 if (__linger_registered(lreq))
3144 __linger_cancel(lreq);
3145 up_write(&osdc->lock);
3146}
3147
Ilya Dryomov46092452016-04-28 16:07:27 +02003148static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3149
3150static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3151{
3152 struct ceph_osd_client *osdc = lreq->osdc;
3153 struct ceph_osdmap *map = osdc->osdmap;
3154
3155 verify_osdc_wrlocked(osdc);
3156 WARN_ON(!map->epoch);
3157
3158 if (lreq->register_gen) {
3159 lreq->map_dne_bound = map->epoch;
3160 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3161 lreq, lreq->linger_id);
3162 } else {
3163 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3164 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3165 map->epoch);
3166 }
3167
3168 if (lreq->map_dne_bound) {
3169 if (map->epoch >= lreq->map_dne_bound) {
3170 /* we had a new enough map */
3171 pr_info("linger_id %llu pool does not exist\n",
3172 lreq->linger_id);
3173 linger_reg_commit_complete(lreq, -ENOENT);
3174 __linger_cancel(lreq);
3175 }
3176 } else {
3177 send_linger_map_check(lreq);
3178 }
3179}
3180
3181static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3182{
3183 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3184 struct ceph_osd_linger_request *lreq;
3185 u64 linger_id = greq->private_data;
3186
3187 WARN_ON(greq->result || !greq->u.newest);
3188
3189 down_write(&osdc->lock);
3190 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3191 if (!lreq) {
3192 dout("%s linger_id %llu dne\n", __func__, linger_id);
3193 goto out_unlock;
3194 }
3195
3196 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3197 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3198 greq->u.newest);
3199 if (!lreq->map_dne_bound)
3200 lreq->map_dne_bound = greq->u.newest;
3201 erase_linger_mc(&osdc->linger_map_checks, lreq);
3202 check_linger_pool_dne(lreq);
3203
3204 linger_put(lreq);
3205out_unlock:
3206 up_write(&osdc->lock);
3207}
3208
3209static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3210{
3211 struct ceph_osd_client *osdc = lreq->osdc;
3212 struct ceph_osd_linger_request *lookup_lreq;
3213 int ret;
3214
3215 verify_osdc_wrlocked(osdc);
3216
3217 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3218 lreq->linger_id);
3219 if (lookup_lreq) {
3220 WARN_ON(lookup_lreq != lreq);
3221 return;
3222 }
3223
3224 linger_get(lreq);
3225 insert_linger_mc(&osdc->linger_map_checks, lreq);
3226 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3227 linger_map_check_cb, lreq->linger_id);
3228 WARN_ON(ret);
3229}
3230
Ilya Dryomov922dab62016-05-26 01:15:02 +02003231static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3232{
3233 int ret;
3234
3235 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3236 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3237 return ret ?: lreq->reg_commit_error;
3238}
3239
Ilya Dryomov19079202016-04-28 16:07:27 +02003240static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3241{
3242 int ret;
3243
3244 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3245 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3246 return ret ?: lreq->notify_finish_error;
3247}
3248
Ilya Dryomov922dab62016-05-26 01:15:02 +02003249/*
Ilya Dryomovfbca9632016-04-28 16:07:24 +02003250 * Timeout callback, called every N seconds. When 1 or more OSD
3251 * requests has been active for more than N seconds, we send a keepalive
3252 * (tag + timestamp) to its OSD to ensure any communications channel
3253 * reset is detected.
Sage Weilf24e9982009-10-06 11:31:10 -07003254 */
3255static void handle_timeout(struct work_struct *work)
3256{
3257 struct ceph_osd_client *osdc =
3258 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03003259 struct ceph_options *opts = osdc->client->options;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003260 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003261 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003262 LIST_HEAD(slow_osds);
3263 struct rb_node *n, *p;
Sage Weilf24e9982009-10-06 11:31:10 -07003264
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003265 dout("%s osdc %p\n", __func__, osdc);
3266 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003267
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003268 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003269 * ping osds that are a bit slow. this ensures that if there
3270 * is a break in the TCP connection we will notice, and reopen
3271 * a connection with that osd (from the fault callback).
3272 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003273 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3274 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3275 bool found = false;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003276
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003277 for (p = rb_first(&osd->o_requests); p; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003278 struct ceph_osd_request *req =
3279 rb_entry(p, struct ceph_osd_request, r_node);
3280
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003281 p = rb_next(p); /* abort_request() */
3282
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003283 if (time_before(req->r_stamp, cutoff)) {
3284 dout(" req %p tid %llu on osd%d is laggy\n",
3285 req, req->r_tid, osd->o_osd);
3286 found = true;
3287 }
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003288 if (opts->osd_request_timeout &&
3289 time_before(req->r_start_stamp, expiry_cutoff)) {
3290 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3291 req->r_tid, osd->o_osd);
3292 abort_request(req, -ETIMEDOUT);
3293 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003294 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003295 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3296 struct ceph_osd_linger_request *lreq =
3297 rb_entry(p, struct ceph_osd_linger_request, node);
3298
3299 dout(" lreq %p linger_id %llu is served by osd%d\n",
3300 lreq, lreq->linger_id, osd->o_osd);
3301 found = true;
3302
3303 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02003304 if (lreq->is_watch && lreq->committed && !lreq->last_error)
Ilya Dryomov922dab62016-05-26 01:15:02 +02003305 send_linger_ping(lreq);
3306 mutex_unlock(&lreq->lock);
3307 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003308
3309 if (found)
3310 list_move_tail(&osd->o_keepalive_item, &slow_osds);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003311 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003312
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003313 if (opts->osd_request_timeout) {
3314 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3315 struct ceph_osd_request *req =
3316 rb_entry(p, struct ceph_osd_request, r_node);
3317
3318 p = rb_next(p); /* abort_request() */
3319
3320 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3321 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3322 req->r_tid, osdc->homeless_osd.o_osd);
3323 abort_request(req, -ETIMEDOUT);
3324 }
3325 }
3326 }
3327
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003328 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3329 maybe_request_map(osdc);
3330
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003331 while (!list_empty(&slow_osds)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003332 struct ceph_osd *osd = list_first_entry(&slow_osds,
3333 struct ceph_osd,
3334 o_keepalive_item);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003335 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07003336 ceph_con_keepalive(&osd->o_con);
3337 }
3338
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003339 up_write(&osdc->lock);
Ilya Dryomovfbca9632016-04-28 16:07:24 +02003340 schedule_delayed_work(&osdc->timeout_work,
3341 osdc->client->options->osd_keepalive_timeout);
Sage Weilf24e9982009-10-06 11:31:10 -07003342}
3343
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003344static void handle_osds_timeout(struct work_struct *work)
3345{
3346 struct ceph_osd_client *osdc =
3347 container_of(work, struct ceph_osd_client,
3348 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03003349 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003350 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003351
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003352 dout("%s osdc %p\n", __func__, osdc);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003353 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003354 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3355 if (time_before(jiffies, osd->lru_ttl))
3356 break;
3357
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003358 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02003359 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003360 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003361 }
3362
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003363 up_write(&osdc->lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003364 schedule_delayed_work(&osdc->osds_timeout_work,
3365 round_jiffies_relative(delay));
3366}
3367
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003368static int ceph_oloc_decode(void **p, void *end,
3369 struct ceph_object_locator *oloc)
3370{
3371 u8 struct_v, struct_cv;
3372 u32 len;
3373 void *struct_end;
3374 int ret = 0;
3375
3376 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3377 struct_v = ceph_decode_8(p);
3378 struct_cv = ceph_decode_8(p);
3379 if (struct_v < 3) {
3380 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3381 struct_v, struct_cv);
3382 goto e_inval;
3383 }
3384 if (struct_cv > 6) {
3385 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3386 struct_v, struct_cv);
3387 goto e_inval;
3388 }
3389 len = ceph_decode_32(p);
3390 ceph_decode_need(p, end, len, e_inval);
3391 struct_end = *p + len;
3392
3393 oloc->pool = ceph_decode_64(p);
3394 *p += 4; /* skip preferred */
3395
3396 len = ceph_decode_32(p);
3397 if (len > 0) {
3398 pr_warn("ceph_object_locator::key is set\n");
3399 goto e_inval;
3400 }
3401
3402 if (struct_v >= 5) {
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003403 bool changed = false;
3404
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003405 len = ceph_decode_32(p);
3406 if (len > 0) {
Yan, Zheng30c156d2016-02-14 11:24:31 +08003407 ceph_decode_need(p, end, len, e_inval);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003408 if (!oloc->pool_ns ||
3409 ceph_compare_string(oloc->pool_ns, *p, len))
3410 changed = true;
Yan, Zheng30c156d2016-02-14 11:24:31 +08003411 *p += len;
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003412 } else {
3413 if (oloc->pool_ns)
3414 changed = true;
3415 }
3416 if (changed) {
3417 /* redirect changes namespace */
3418 pr_warn("ceph_object_locator::nspace is changed\n");
3419 goto e_inval;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003420 }
3421 }
3422
3423 if (struct_v >= 6) {
3424 s64 hash = ceph_decode_64(p);
3425 if (hash != -1) {
3426 pr_warn("ceph_object_locator::hash is set\n");
3427 goto e_inval;
3428 }
3429 }
3430
3431 /* skip the rest */
3432 *p = struct_end;
3433out:
3434 return ret;
3435
3436e_inval:
3437 ret = -EINVAL;
3438 goto out;
3439}
3440
3441static int ceph_redirect_decode(void **p, void *end,
3442 struct ceph_request_redirect *redir)
3443{
3444 u8 struct_v, struct_cv;
3445 u32 len;
3446 void *struct_end;
3447 int ret;
3448
3449 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3450 struct_v = ceph_decode_8(p);
3451 struct_cv = ceph_decode_8(p);
3452 if (struct_cv > 1) {
3453 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3454 struct_v, struct_cv);
3455 goto e_inval;
3456 }
3457 len = ceph_decode_32(p);
3458 ceph_decode_need(p, end, len, e_inval);
3459 struct_end = *p + len;
3460
3461 ret = ceph_oloc_decode(p, end, &redir->oloc);
3462 if (ret)
3463 goto out;
3464
3465 len = ceph_decode_32(p);
3466 if (len > 0) {
3467 pr_warn("ceph_request_redirect::object_name is set\n");
3468 goto e_inval;
3469 }
3470
3471 len = ceph_decode_32(p);
3472 *p += len; /* skip osd_instructions */
3473
3474 /* skip the rest */
3475 *p = struct_end;
3476out:
3477 return ret;
3478
3479e_inval:
3480 ret = -EINVAL;
3481 goto out;
3482}
3483
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003484struct MOSDOpReply {
3485 struct ceph_pg pgid;
3486 u64 flags;
3487 int result;
3488 u32 epoch;
3489 int num_ops;
3490 u32 outdata_len[CEPH_OSD_MAX_OPS];
3491 s32 rval[CEPH_OSD_MAX_OPS];
3492 int retry_attempt;
3493 struct ceph_eversion replay_version;
3494 u64 user_version;
3495 struct ceph_request_redirect redirect;
3496};
Sage Weil25845472011-06-03 09:37:09 -07003497
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003498static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
Sage Weilf24e9982009-10-06 11:31:10 -07003499{
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003500 void *p = msg->front.iov_base;
3501 void *const end = p + msg->front.iov_len;
3502 u16 version = le16_to_cpu(msg->hdr.version);
3503 struct ceph_eversion bad_replay_version;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003504 u8 decode_redir;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003505 u32 len;
3506 int ret;
3507 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07003508
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003509 ceph_decode_32_safe(&p, end, len, e_inval);
3510 ceph_decode_need(&p, end, len, e_inval);
3511 p += len; /* skip oid */
Sage Weil1b83bef2013-02-25 16:11:12 -08003512
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003513 ret = ceph_decode_pgid(&p, end, &m->pgid);
3514 if (ret)
3515 return ret;
Sage Weil1b83bef2013-02-25 16:11:12 -08003516
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003517 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3518 ceph_decode_32_safe(&p, end, m->result, e_inval);
3519 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3520 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3521 p += sizeof(bad_replay_version);
3522 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
Sage Weil1b83bef2013-02-25 16:11:12 -08003523
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003524 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3525 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3526 goto e_inval;
Sage Weil1b83bef2013-02-25 16:11:12 -08003527
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003528 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3529 e_inval);
3530 for (i = 0; i < m->num_ops; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -08003531 struct ceph_osd_op *op = p;
Sage Weil1b83bef2013-02-25 16:11:12 -08003532
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003533 m->outdata_len[i] = le32_to_cpu(op->payload_len);
Sage Weil1b83bef2013-02-25 16:11:12 -08003534 p += sizeof(*op);
3535 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003536
3537 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3538 for (i = 0; i < m->num_ops; i++)
3539 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3540
3541 if (version >= 5) {
3542 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3543 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3544 p += sizeof(m->replay_version);
3545 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3546 } else {
3547 m->replay_version = bad_replay_version; /* struct */
3548 m->user_version = le64_to_cpu(m->replay_version.version);
Sage Weil1b83bef2013-02-25 16:11:12 -08003549 }
3550
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003551 if (version >= 6) {
3552 if (version >= 7)
3553 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003554 else
3555 decode_redir = 1;
3556 } else {
3557 decode_redir = 0;
3558 }
3559
3560 if (decode_redir) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003561 ret = ceph_redirect_decode(&p, end, &m->redirect);
3562 if (ret)
3563 return ret;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003564 } else {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003565 ceph_oloc_init(&m->redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003566 }
3567
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003568 return 0;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003569
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003570e_inval:
3571 return -EINVAL;
3572}
3573
3574/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003575 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3576 * specified.
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003577 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003578static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003579{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003580 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003581 struct ceph_osd_request *req;
3582 struct MOSDOpReply m;
3583 u64 tid = le64_to_cpu(msg->hdr.tid);
3584 u32 data_len = 0;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003585 int ret;
3586 int i;
3587
3588 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3589
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003590 down_read(&osdc->lock);
3591 if (!osd_registered(osd)) {
3592 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3593 goto out_unlock_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003594 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003595 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3596
3597 mutex_lock(&osd->lock);
3598 req = lookup_request(&osd->o_requests, tid);
3599 if (!req) {
3600 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3601 goto out_unlock_session;
3602 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003603
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003604 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003605 ret = decode_MOSDOpReply(msg, &m);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003606 m.redirect.oloc.pool_ns = NULL;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003607 if (ret) {
3608 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3609 req->r_tid, ret);
3610 ceph_msg_dump(msg);
3611 goto fail_request;
3612 }
3613 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3614 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3615 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3616 le64_to_cpu(m.replay_version.version), m.user_version);
3617
3618 if (m.retry_attempt >= 0) {
3619 if (m.retry_attempt != req->r_attempts - 1) {
3620 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3621 req, req->r_tid, m.retry_attempt,
3622 req->r_attempts - 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003623 goto out_unlock_session;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003624 }
3625 } else {
3626 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3627 }
3628
3629 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3630 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3631 m.redirect.oloc.pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003632 unlink_request(osd, req);
3633 mutex_unlock(&osd->lock);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003634
Yan, Zheng30c156d2016-02-14 11:24:31 +08003635 /*
3636 * Not ceph_oloc_copy() - changing pool_ns is not
3637 * supported.
3638 */
3639 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003640 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3641 req->r_tid = 0;
3642 __submit_request(req, false);
3643 goto out_unlock_osdc;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003644 }
3645
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003646 if (m.num_ops != req->r_num_ops) {
3647 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3648 req->r_num_ops, req->r_tid);
3649 goto fail_request;
3650 }
3651 for (i = 0; i < req->r_num_ops; i++) {
3652 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3653 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3654 req->r_ops[i].rval = m.rval[i];
3655 req->r_ops[i].outdata_len = m.outdata_len[i];
3656 data_len += m.outdata_len[i];
3657 }
3658 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3659 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3660 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3661 goto fail_request;
3662 }
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003663 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3664 req, req->r_tid, m.result, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07003665
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003666 /*
3667 * Since we only ever request ONDISK, we should only ever get
3668 * one (type of) reply back.
3669 */
3670 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3671 req->r_result = m.result ?: data_len;
3672 finish_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003673 mutex_unlock(&osd->lock);
3674 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003675
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003676 __complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07003677 return;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003678
3679fail_request:
Ilya Dryomov46092452016-04-28 16:07:27 +02003680 complete_request(req, -EIO);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003681out_unlock_session:
3682 mutex_unlock(&osd->lock);
3683out_unlock_osdc:
3684 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003685}
3686
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003687static void set_pool_was_full(struct ceph_osd_client *osdc)
3688{
3689 struct rb_node *n;
3690
3691 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3692 struct ceph_pg_pool_info *pi =
3693 rb_entry(n, struct ceph_pg_pool_info, node);
3694
3695 pi->was_full = __pool_full(pi);
3696 }
3697}
3698
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003699static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
Sage Weilf24e9982009-10-06 11:31:10 -07003700{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003701 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07003702
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003703 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3704 if (!pi)
3705 return false;
Sage Weilf24e9982009-10-06 11:31:10 -07003706
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003707 return pi->was_full && !__pool_full(pi);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003708}
3709
Ilya Dryomov922dab62016-05-26 01:15:02 +02003710static enum calc_target_result
3711recalc_linger_target(struct ceph_osd_linger_request *lreq)
3712{
3713 struct ceph_osd_client *osdc = lreq->osdc;
3714 enum calc_target_result ct_res;
3715
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003716 ct_res = calc_target(osdc, &lreq->t, NULL, true);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003717 if (ct_res == CALC_TARGET_NEED_RESEND) {
3718 struct ceph_osd *osd;
3719
3720 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3721 if (osd != lreq->osd) {
3722 unlink_linger(lreq->osd, lreq);
3723 link_linger(osd, lreq);
3724 }
3725 }
3726
3727 return ct_res;
3728}
3729
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003730/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003731 * Requeue requests whose mapping to an OSD has changed.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003732 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003733static void scan_requests(struct ceph_osd *osd,
3734 bool force_resend,
3735 bool cleared_full,
3736 bool check_pool_cleared_full,
3737 struct rb_root *need_resend,
3738 struct list_head *need_resend_linger)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003739{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003740 struct ceph_osd_client *osdc = osd->o_osdc;
3741 struct rb_node *n;
3742 bool force_resend_writes;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003743
Ilya Dryomov922dab62016-05-26 01:15:02 +02003744 for (n = rb_first(&osd->o_linger_requests); n; ) {
3745 struct ceph_osd_linger_request *lreq =
3746 rb_entry(n, struct ceph_osd_linger_request, node);
3747 enum calc_target_result ct_res;
3748
3749 n = rb_next(n); /* recalc_linger_target() */
3750
3751 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3752 lreq->linger_id);
3753 ct_res = recalc_linger_target(lreq);
3754 switch (ct_res) {
3755 case CALC_TARGET_NO_ACTION:
3756 force_resend_writes = cleared_full ||
3757 (check_pool_cleared_full &&
3758 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3759 if (!force_resend && !force_resend_writes)
3760 break;
3761
3762 /* fall through */
3763 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003764 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003765 /*
3766 * scan_requests() for the previous epoch(s)
3767 * may have already added it to the list, since
3768 * it's not unlinked here.
3769 */
3770 if (list_empty(&lreq->scan_item))
3771 list_add_tail(&lreq->scan_item, need_resend_linger);
3772 break;
3773 case CALC_TARGET_POOL_DNE:
Ilya Dryomova10bcb12017-06-15 16:30:55 +02003774 list_del_init(&lreq->scan_item);
Ilya Dryomov46092452016-04-28 16:07:27 +02003775 check_linger_pool_dne(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003776 break;
3777 }
3778 }
3779
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003780 for (n = rb_first(&osd->o_requests); n; ) {
3781 struct ceph_osd_request *req =
3782 rb_entry(n, struct ceph_osd_request, r_node);
3783 enum calc_target_result ct_res;
Alex Elderab60b162012-12-19 15:52:36 -06003784
Ilya Dryomov46092452016-04-28 16:07:27 +02003785 n = rb_next(n); /* unlink_request(), check_pool_dne() */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003786
3787 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003788 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3789 false);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003790 switch (ct_res) {
3791 case CALC_TARGET_NO_ACTION:
3792 force_resend_writes = cleared_full ||
3793 (check_pool_cleared_full &&
3794 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3795 if (!force_resend &&
3796 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3797 !force_resend_writes))
3798 break;
3799
3800 /* fall through */
3801 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003802 cancel_map_check(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003803 unlink_request(osd, req);
3804 insert_request(need_resend, req);
3805 break;
3806 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003807 check_pool_dne(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003808 break;
Alex Elderab60b162012-12-19 15:52:36 -06003809 }
Sage Weilf24e9982009-10-06 11:31:10 -07003810 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003811}
Sage Weil6f6c7002011-01-17 20:34:08 -08003812
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003813static int handle_one_map(struct ceph_osd_client *osdc,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003814 void *p, void *end, bool incremental,
3815 struct rb_root *need_resend,
3816 struct list_head *need_resend_linger)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003817{
3818 struct ceph_osdmap *newmap;
3819 struct rb_node *n;
3820 bool skipped_map = false;
3821 bool was_full;
3822
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003823 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003824 set_pool_was_full(osdc);
3825
3826 if (incremental)
3827 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3828 else
3829 newmap = ceph_osdmap_decode(&p, end);
3830 if (IS_ERR(newmap))
3831 return PTR_ERR(newmap);
3832
3833 if (newmap != osdc->osdmap) {
3834 /*
3835 * Preserve ->was_full before destroying the old map.
3836 * For pools that weren't in the old map, ->was_full
3837 * should be false.
3838 */
3839 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3840 struct ceph_pg_pool_info *pi =
3841 rb_entry(n, struct ceph_pg_pool_info, node);
3842 struct ceph_pg_pool_info *old_pi;
3843
3844 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3845 if (old_pi)
3846 pi->was_full = old_pi->was_full;
3847 else
3848 WARN_ON(pi->was_full);
3849 }
3850
3851 if (osdc->osdmap->epoch &&
3852 osdc->osdmap->epoch + 1 < newmap->epoch) {
3853 WARN_ON(incremental);
3854 skipped_map = true;
3855 }
3856
3857 ceph_osdmap_destroy(osdc->osdmap);
3858 osdc->osdmap = newmap;
3859 }
3860
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003861 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003862 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3863 need_resend, need_resend_linger);
3864
3865 for (n = rb_first(&osdc->osds); n; ) {
3866 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3867
3868 n = rb_next(n); /* close_osd() */
3869
3870 scan_requests(osd, skipped_map, was_full, true, need_resend,
3871 need_resend_linger);
3872 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3873 memcmp(&osd->o_con.peer_addr,
3874 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3875 sizeof(struct ceph_entity_addr)))
3876 close_osd(osd);
3877 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003878
3879 return 0;
3880}
Sage Weil6f6c7002011-01-17 20:34:08 -08003881
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003882static void kick_requests(struct ceph_osd_client *osdc,
3883 struct rb_root *need_resend,
3884 struct list_head *need_resend_linger)
3885{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003886 struct ceph_osd_linger_request *lreq, *nlreq;
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003887 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003888 struct rb_node *n;
3889
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003890 /* make sure need_resend targets reflect latest map */
3891 for (n = rb_first(need_resend); n; ) {
3892 struct ceph_osd_request *req =
3893 rb_entry(n, struct ceph_osd_request, r_node);
3894
3895 n = rb_next(n);
3896
3897 if (req->r_t.epoch < osdc->osdmap->epoch) {
3898 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3899 if (ct_res == CALC_TARGET_POOL_DNE) {
3900 erase_request(need_resend, req);
3901 check_pool_dne(req);
3902 }
3903 }
3904 }
3905
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003906 for (n = rb_first(need_resend); n; ) {
3907 struct ceph_osd_request *req =
3908 rb_entry(n, struct ceph_osd_request, r_node);
3909 struct ceph_osd *osd;
3910
3911 n = rb_next(n);
3912 erase_request(need_resend, req); /* before link_request() */
3913
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003914 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3915 link_request(osd, req);
3916 if (!req->r_linger) {
3917 if (!osd_homeless(osd) && !req->r_t.paused)
3918 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003919 } else {
3920 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003921 }
3922 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003923
3924 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3925 if (!osd_homeless(lreq->osd))
3926 send_linger(lreq);
3927
3928 list_del_init(&lreq->scan_item);
3929 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003930}
3931
Sage Weilf24e9982009-10-06 11:31:10 -07003932/*
3933 * Process updated osd map.
3934 *
3935 * The message contains any number of incremental and full maps, normally
3936 * indicating some sort of topology change in the cluster. Kick requests
3937 * off to different OSDs as needed.
3938 */
3939void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3940{
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003941 void *p = msg->front.iov_base;
3942 void *const end = p + msg->front.iov_len;
Sage Weilf24e9982009-10-06 11:31:10 -07003943 u32 nr_maps, maplen;
3944 u32 epoch;
Sage Weilf24e9982009-10-06 11:31:10 -07003945 struct ceph_fsid fsid;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003946 struct rb_root need_resend = RB_ROOT;
3947 LIST_HEAD(need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003948 bool handled_incremental = false;
3949 bool was_pauserd, was_pausewr;
3950 bool pauserd, pausewr;
3951 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07003952
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003953 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003954 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003955
3956 /* verify fsid */
3957 ceph_decode_need(&p, end, sizeof(fsid), bad);
3958 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08003959 if (ceph_check_fsid(osdc->client, &fsid) < 0)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003960 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003961
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003962 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3963 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3964 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003965 have_pool_full(osdc);
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08003966
Sage Weilf24e9982009-10-06 11:31:10 -07003967 /* incremental maps */
3968 ceph_decode_32_safe(&p, end, nr_maps, bad);
3969 dout(" %d inc maps\n", nr_maps);
3970 while (nr_maps > 0) {
3971 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003972 epoch = ceph_decode_32(&p);
3973 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003974 ceph_decode_need(&p, end, maplen, bad);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003975 if (osdc->osdmap->epoch &&
3976 osdc->osdmap->epoch + 1 == epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003977 dout("applying incremental map %u len %d\n",
3978 epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003979 err = handle_one_map(osdc, p, p + maplen, true,
3980 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003981 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003982 goto bad;
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003983 handled_incremental = true;
Sage Weilf24e9982009-10-06 11:31:10 -07003984 } else {
3985 dout("ignoring incremental map %u len %d\n",
3986 epoch, maplen);
3987 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003988 p += maplen;
Sage Weilf24e9982009-10-06 11:31:10 -07003989 nr_maps--;
3990 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003991 if (handled_incremental)
Sage Weilf24e9982009-10-06 11:31:10 -07003992 goto done;
3993
3994 /* full maps */
3995 ceph_decode_32_safe(&p, end, nr_maps, bad);
3996 dout(" %d full maps\n", nr_maps);
3997 while (nr_maps) {
3998 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003999 epoch = ceph_decode_32(&p);
4000 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07004001 ceph_decode_need(&p, end, maplen, bad);
4002 if (nr_maps > 1) {
4003 dout("skipping non-latest full map %u len %d\n",
4004 epoch, maplen);
Ilya Dryomove5253a72016-04-28 16:07:25 +02004005 } else if (osdc->osdmap->epoch >= epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07004006 dout("skipping full map %u len %d, "
4007 "older than our %u\n", epoch, maplen,
4008 osdc->osdmap->epoch);
4009 } else {
4010 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004011 err = handle_one_map(osdc, p, p + maplen, false,
4012 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02004013 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07004014 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07004015 }
4016 p += maplen;
4017 nr_maps--;
4018 }
4019
4020done:
Sage Weilcd634fb2011-05-12 09:29:18 -07004021 /*
4022 * subscribe to subsequent osdmap updates if full to ensure
4023 * we find out when we are no longer full and stop returning
4024 * ENOSPC.
4025 */
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02004026 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4027 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4028 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02004029 have_pool_full(osdc);
Jeff Layton58eb7932017-04-18 09:21:16 -04004030 if (was_pauserd || was_pausewr || pauserd || pausewr ||
4031 osdc->osdmap->epoch < osdc->epoch_barrier)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02004032 maybe_request_map(osdc);
Sage Weilcd634fb2011-05-12 09:29:18 -07004033
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004034 kick_requests(osdc, &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02004035
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04004036 ceph_osdc_abort_on_full(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02004037 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
4038 osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004039 up_write(&osdc->lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07004040 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07004041 return;
4042
4043bad:
4044 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08004045 ceph_msg_dump(msg);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004046 up_write(&osdc->lock);
4047}
4048
4049/*
4050 * Resubmit requests pending on the given osd.
4051 */
4052static void kick_osd_requests(struct ceph_osd *osd)
4053{
4054 struct rb_node *n;
4055
Ilya Dryomova02a9462017-06-19 12:18:05 +02004056 clear_backoffs(osd);
4057
Ilya Dryomov922dab62016-05-26 01:15:02 +02004058 for (n = rb_first(&osd->o_requests); n; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004059 struct ceph_osd_request *req =
4060 rb_entry(n, struct ceph_osd_request, r_node);
4061
Ilya Dryomov922dab62016-05-26 01:15:02 +02004062 n = rb_next(n); /* cancel_linger_request() */
4063
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004064 if (!req->r_linger) {
4065 if (!req->r_t.paused)
4066 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004067 } else {
4068 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004069 }
4070 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004071 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4072 struct ceph_osd_linger_request *lreq =
4073 rb_entry(n, struct ceph_osd_linger_request, node);
4074
4075 send_linger(lreq);
4076 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004077}
4078
4079/*
4080 * If the osd connection drops, we need to resubmit all requests.
4081 */
4082static void osd_fault(struct ceph_connection *con)
4083{
4084 struct ceph_osd *osd = con->private;
4085 struct ceph_osd_client *osdc = osd->o_osdc;
4086
4087 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4088
4089 down_write(&osdc->lock);
4090 if (!osd_registered(osd)) {
4091 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4092 goto out_unlock;
4093 }
4094
4095 if (!reopen_osd(osd))
4096 kick_osd_requests(osd);
4097 maybe_request_map(osdc);
4098
4099out_unlock:
4100 up_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004101}
4102
Ilya Dryomova02a9462017-06-19 12:18:05 +02004103struct MOSDBackoff {
4104 struct ceph_spg spgid;
4105 u32 map_epoch;
4106 u8 op;
4107 u64 id;
4108 struct ceph_hobject_id *begin;
4109 struct ceph_hobject_id *end;
4110};
4111
4112static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4113{
4114 void *p = msg->front.iov_base;
4115 void *const end = p + msg->front.iov_len;
4116 u8 struct_v;
4117 u32 struct_len;
4118 int ret;
4119
4120 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4121 if (ret)
4122 return ret;
4123
4124 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4125 if (ret)
4126 return ret;
4127
4128 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4129 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4130 ceph_decode_8_safe(&p, end, m->op, e_inval);
4131 ceph_decode_64_safe(&p, end, m->id, e_inval);
4132
4133 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4134 if (!m->begin)
4135 return -ENOMEM;
4136
4137 ret = decode_hoid(&p, end, m->begin);
4138 if (ret) {
4139 free_hoid(m->begin);
4140 return ret;
4141 }
4142
4143 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4144 if (!m->end) {
4145 free_hoid(m->begin);
4146 return -ENOMEM;
4147 }
4148
4149 ret = decode_hoid(&p, end, m->end);
4150 if (ret) {
4151 free_hoid(m->begin);
4152 free_hoid(m->end);
4153 return ret;
4154 }
4155
4156 return 0;
4157
4158e_inval:
4159 return -EINVAL;
4160}
4161
4162static struct ceph_msg *create_backoff_message(
4163 const struct ceph_osd_backoff *backoff,
4164 u32 map_epoch)
4165{
4166 struct ceph_msg *msg;
4167 void *p, *end;
4168 int msg_size;
4169
4170 msg_size = CEPH_ENCODING_START_BLK_LEN +
4171 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4172 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4173 msg_size += CEPH_ENCODING_START_BLK_LEN +
4174 hoid_encoding_size(backoff->begin);
4175 msg_size += CEPH_ENCODING_START_BLK_LEN +
4176 hoid_encoding_size(backoff->end);
4177
4178 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4179 if (!msg)
4180 return NULL;
4181
4182 p = msg->front.iov_base;
4183 end = p + msg->front_alloc_len;
4184
4185 encode_spgid(&p, &backoff->spgid);
4186 ceph_encode_32(&p, map_epoch);
4187 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4188 ceph_encode_64(&p, backoff->id);
4189 encode_hoid(&p, end, backoff->begin);
4190 encode_hoid(&p, end, backoff->end);
4191 BUG_ON(p != end);
4192
4193 msg->front.iov_len = p - msg->front.iov_base;
4194 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4195 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4196
4197 return msg;
4198}
4199
4200static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4201{
4202 struct ceph_spg_mapping *spg;
4203 struct ceph_osd_backoff *backoff;
4204 struct ceph_msg *msg;
4205
4206 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4207 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4208
4209 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4210 if (!spg) {
4211 spg = alloc_spg_mapping();
4212 if (!spg) {
4213 pr_err("%s failed to allocate spg\n", __func__);
4214 return;
4215 }
4216 spg->spgid = m->spgid; /* struct */
4217 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4218 }
4219
4220 backoff = alloc_backoff();
4221 if (!backoff) {
4222 pr_err("%s failed to allocate backoff\n", __func__);
4223 return;
4224 }
4225 backoff->spgid = m->spgid; /* struct */
4226 backoff->id = m->id;
4227 backoff->begin = m->begin;
4228 m->begin = NULL; /* backoff now owns this */
4229 backoff->end = m->end;
4230 m->end = NULL; /* ditto */
4231
4232 insert_backoff(&spg->backoffs, backoff);
4233 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4234
4235 /*
4236 * Ack with original backoff's epoch so that the OSD can
4237 * discard this if there was a PG split.
4238 */
4239 msg = create_backoff_message(backoff, m->map_epoch);
4240 if (!msg) {
4241 pr_err("%s failed to allocate msg\n", __func__);
4242 return;
4243 }
4244 ceph_con_send(&osd->o_con, msg);
4245}
4246
4247static bool target_contained_by(const struct ceph_osd_request_target *t,
4248 const struct ceph_hobject_id *begin,
4249 const struct ceph_hobject_id *end)
4250{
4251 struct ceph_hobject_id hoid;
4252 int cmp;
4253
4254 hoid_fill_from_target(&hoid, t);
4255 cmp = hoid_compare(&hoid, begin);
4256 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4257}
4258
4259static void handle_backoff_unblock(struct ceph_osd *osd,
4260 const struct MOSDBackoff *m)
4261{
4262 struct ceph_spg_mapping *spg;
4263 struct ceph_osd_backoff *backoff;
4264 struct rb_node *n;
4265
4266 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4267 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4268
4269 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4270 if (!backoff) {
4271 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4272 __func__, osd->o_osd, m->spgid.pgid.pool,
4273 m->spgid.pgid.seed, m->spgid.shard, m->id);
4274 return;
4275 }
4276
4277 if (hoid_compare(backoff->begin, m->begin) &&
4278 hoid_compare(backoff->end, m->end)) {
4279 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4280 __func__, osd->o_osd, m->spgid.pgid.pool,
4281 m->spgid.pgid.seed, m->spgid.shard, m->id);
4282 /* unblock it anyway... */
4283 }
4284
4285 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4286 BUG_ON(!spg);
4287
4288 erase_backoff(&spg->backoffs, backoff);
4289 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4290 free_backoff(backoff);
4291
4292 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4293 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4294 free_spg_mapping(spg);
4295 }
4296
4297 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4298 struct ceph_osd_request *req =
4299 rb_entry(n, struct ceph_osd_request, r_node);
4300
4301 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4302 /*
4303 * Match against @m, not @backoff -- the PG may
4304 * have split on the OSD.
4305 */
4306 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4307 /*
4308 * If no other installed backoff applies,
4309 * resend.
4310 */
4311 send_request(req);
4312 }
4313 }
4314 }
4315}
4316
4317static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4318{
4319 struct ceph_osd_client *osdc = osd->o_osdc;
4320 struct MOSDBackoff m;
4321 int ret;
4322
4323 down_read(&osdc->lock);
4324 if (!osd_registered(osd)) {
4325 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4326 up_read(&osdc->lock);
4327 return;
4328 }
4329 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4330
4331 mutex_lock(&osd->lock);
4332 ret = decode_MOSDBackoff(msg, &m);
4333 if (ret) {
4334 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4335 ceph_msg_dump(msg);
4336 goto out_unlock;
4337 }
4338
4339 switch (m.op) {
4340 case CEPH_OSD_BACKOFF_OP_BLOCK:
4341 handle_backoff_block(osd, &m);
4342 break;
4343 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4344 handle_backoff_unblock(osd, &m);
4345 break;
4346 default:
4347 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4348 }
4349
4350 free_hoid(m.begin);
4351 free_hoid(m.end);
4352
4353out_unlock:
4354 mutex_unlock(&osd->lock);
4355 up_read(&osdc->lock);
4356}
4357
Sage Weilf24e9982009-10-06 11:31:10 -07004358/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004359 * Process osd watch notifications
4360 */
Alex Elder3c663bb2013-02-15 11:42:30 -06004361static void handle_watch_notify(struct ceph_osd_client *osdc,
4362 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004363{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004364 void *p = msg->front.iov_base;
4365 void *const end = p + msg->front.iov_len;
4366 struct ceph_osd_linger_request *lreq;
4367 struct linger_work *lwork;
4368 u8 proto_ver, opcode;
4369 u64 cookie, notify_id;
4370 u64 notifier_id = 0;
Ilya Dryomov19079202016-04-28 16:07:27 +02004371 s32 return_code = 0;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004372 void *payload = NULL;
4373 u32 payload_len = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004374
4375 ceph_decode_8_safe(&p, end, proto_ver, bad);
4376 ceph_decode_8_safe(&p, end, opcode, bad);
4377 ceph_decode_64_safe(&p, end, cookie, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004378 p += 8; /* skip ver */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004379 ceph_decode_64_safe(&p, end, notify_id, bad);
4380
Ilya Dryomov922dab62016-05-26 01:15:02 +02004381 if (proto_ver >= 1) {
4382 ceph_decode_32_safe(&p, end, payload_len, bad);
4383 ceph_decode_need(&p, end, payload_len, bad);
4384 payload = p;
4385 p += payload_len;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004386 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004387
4388 if (le16_to_cpu(msg->hdr.version) >= 2)
Ilya Dryomov19079202016-04-28 16:07:27 +02004389 ceph_decode_32_safe(&p, end, return_code, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004390
4391 if (le16_to_cpu(msg->hdr.version) >= 3)
4392 ceph_decode_64_safe(&p, end, notifier_id, bad);
4393
4394 down_read(&osdc->lock);
4395 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4396 if (!lreq) {
4397 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4398 cookie);
4399 goto out_unlock_osdc;
4400 }
4401
4402 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02004403 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4404 opcode, cookie, lreq, lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004405 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4406 if (!lreq->last_error) {
4407 lreq->last_error = -ENOTCONN;
4408 queue_watch_error(lreq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004409 }
Ilya Dryomov19079202016-04-28 16:07:27 +02004410 } else if (!lreq->is_watch) {
4411 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4412 if (lreq->notify_id && lreq->notify_id != notify_id) {
4413 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4414 lreq->notify_id, notify_id);
4415 } else if (!completion_done(&lreq->notify_finish_wait)) {
4416 struct ceph_msg_data *data =
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02004417 msg->num_data_items ? &msg->data[0] : NULL;
Ilya Dryomov19079202016-04-28 16:07:27 +02004418
4419 if (data) {
4420 if (lreq->preply_pages) {
4421 WARN_ON(data->type !=
4422 CEPH_MSG_DATA_PAGES);
4423 *lreq->preply_pages = data->pages;
4424 *lreq->preply_len = data->length;
4425 } else {
4426 ceph_release_page_vector(data->pages,
4427 calc_pages_for(0, data->length));
4428 }
4429 }
4430 lreq->notify_finish_error = return_code;
4431 complete_all(&lreq->notify_finish_wait);
4432 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004433 } else {
4434 /* CEPH_WATCH_EVENT_NOTIFY */
4435 lwork = lwork_alloc(lreq, do_watch_notify);
4436 if (!lwork) {
4437 pr_err("failed to allocate notify-lwork\n");
4438 goto out_unlock_lreq;
4439 }
Ilya Dryomov91883cd2014-09-11 12:18:53 +04004440
Ilya Dryomov922dab62016-05-26 01:15:02 +02004441 lwork->notify.notify_id = notify_id;
4442 lwork->notify.notifier_id = notifier_id;
4443 lwork->notify.payload = payload;
4444 lwork->notify.payload_len = payload_len;
4445 lwork->notify.msg = ceph_msg_get(msg);
4446 lwork_queue(lwork);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004447 }
4448
Ilya Dryomov922dab62016-05-26 01:15:02 +02004449out_unlock_lreq:
4450 mutex_unlock(&lreq->lock);
4451out_unlock_osdc:
4452 up_read(&osdc->lock);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004453 return;
4454
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004455bad:
4456 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004457}
4458
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004459/*
Sage Weilf24e9982009-10-06 11:31:10 -07004460 * Register request, send initial attempt.
4461 */
4462int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4463 struct ceph_osd_request *req,
4464 bool nofail)
4465{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004466 down_read(&osdc->lock);
4467 submit_request(req, false);
4468 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004469
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004470 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07004471}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004472EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07004473
4474/*
Ilya Dryomovc297eb422016-12-02 14:01:55 +01004475 * Unregister a registered request. The request is not completed:
4476 * ->r_result isn't set and __complete_request() isn't called.
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04004477 */
4478void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4479{
4480 struct ceph_osd_client *osdc = req->r_osdc;
4481
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004482 down_write(&osdc->lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004483 if (req->r_osd)
4484 cancel_request(req);
4485 up_write(&osdc->lock);
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04004486}
4487EXPORT_SYMBOL(ceph_osdc_cancel_request);
4488
4489/*
Ilya Dryomov42b06962016-04-28 16:07:26 +02004490 * @timeout: in jiffies, 0 means "wait forever"
4491 */
4492static int wait_request_timeout(struct ceph_osd_request *req,
4493 unsigned long timeout)
4494{
4495 long left;
4496
4497 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Yan, Zheng0e76abf2016-05-13 11:04:33 +08004498 left = wait_for_completion_killable_timeout(&req->r_completion,
Ilya Dryomov42b06962016-04-28 16:07:26 +02004499 ceph_timeout_jiffies(timeout));
4500 if (left <= 0) {
4501 left = left ?: -ETIMEDOUT;
4502 ceph_osdc_cancel_request(req);
Ilya Dryomov42b06962016-04-28 16:07:26 +02004503 } else {
4504 left = req->r_result; /* completed */
4505 }
4506
4507 return left;
4508}
4509
4510/*
Sage Weilf24e9982009-10-06 11:31:10 -07004511 * wait for a request to complete
4512 */
4513int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4514 struct ceph_osd_request *req)
4515{
Ilya Dryomov42b06962016-04-28 16:07:26 +02004516 return wait_request_timeout(req, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07004517}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004518EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07004519
4520/*
4521 * sync - wait for all in-flight requests to flush. avoid starvation.
4522 */
4523void ceph_osdc_sync(struct ceph_osd_client *osdc)
4524{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004525 struct rb_node *n, *p;
4526 u64 last_tid = atomic64_read(&osdc->last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004527
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004528again:
4529 down_read(&osdc->lock);
4530 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4531 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07004532
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004533 mutex_lock(&osd->lock);
4534 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4535 struct ceph_osd_request *req =
4536 rb_entry(p, struct ceph_osd_request, r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07004537
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004538 if (req->r_tid > last_tid)
4539 break;
4540
4541 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4542 continue;
4543
4544 ceph_osdc_get_request(req);
4545 mutex_unlock(&osd->lock);
4546 up_read(&osdc->lock);
4547 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4548 __func__, req, req->r_tid, last_tid);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01004549 wait_for_completion(&req->r_completion);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004550 ceph_osdc_put_request(req);
4551 goto again;
4552 }
4553
4554 mutex_unlock(&osd->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004555 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004556
4557 up_read(&osdc->lock);
4558 dout("%s done last_tid %llu\n", __func__, last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004559}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004560EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07004561
Ilya Dryomov922dab62016-05-26 01:15:02 +02004562static struct ceph_osd_request *
4563alloc_linger_request(struct ceph_osd_linger_request *lreq)
4564{
4565 struct ceph_osd_request *req;
4566
4567 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4568 if (!req)
4569 return NULL;
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 Dryomov922dab62016-05-26 01:15:02 +02004573 return req;
4574}
4575
Ilya Dryomov39e58c32018-10-15 15:26:27 +02004576static struct ceph_osd_request *
4577alloc_watch_request(struct ceph_osd_linger_request *lreq, u8 watch_opcode)
4578{
4579 struct ceph_osd_request *req;
4580
4581 req = alloc_linger_request(lreq);
4582 if (!req)
4583 return NULL;
4584
4585 /*
4586 * Pass 0 for cookie because we don't know it yet, it will be
4587 * filled in by linger_submit().
4588 */
4589 osd_req_op_watch_init(req, 0, 0, watch_opcode);
Ilya Dryomov26f887e2018-10-15 16:11:37 +02004590
4591 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4592 ceph_osdc_put_request(req);
4593 return NULL;
4594 }
4595
Ilya Dryomov39e58c32018-10-15 15:26:27 +02004596 return req;
4597}
4598
Ilya Dryomov922dab62016-05-26 01:15:02 +02004599/*
4600 * Returns a handle, caller owns a ref.
4601 */
4602struct ceph_osd_linger_request *
4603ceph_osdc_watch(struct ceph_osd_client *osdc,
4604 struct ceph_object_id *oid,
4605 struct ceph_object_locator *oloc,
4606 rados_watchcb2_t wcb,
4607 rados_watcherrcb_t errcb,
4608 void *data)
4609{
4610 struct ceph_osd_linger_request *lreq;
4611 int ret;
4612
4613 lreq = linger_alloc(osdc);
4614 if (!lreq)
4615 return ERR_PTR(-ENOMEM);
4616
Ilya Dryomov19079202016-04-28 16:07:27 +02004617 lreq->is_watch = true;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004618 lreq->wcb = wcb;
4619 lreq->errcb = errcb;
4620 lreq->data = data;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004621 lreq->watch_valid_thru = jiffies;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004622
4623 ceph_oid_copy(&lreq->t.base_oid, oid);
4624 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004625 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02004626 ktime_get_real_ts64(&lreq->mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004627
Ilya Dryomov39e58c32018-10-15 15:26:27 +02004628 lreq->reg_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_WATCH);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004629 if (!lreq->reg_req) {
4630 ret = -ENOMEM;
4631 goto err_put_lreq;
4632 }
4633
Ilya Dryomov39e58c32018-10-15 15:26:27 +02004634 lreq->ping_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_PING);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004635 if (!lreq->ping_req) {
4636 ret = -ENOMEM;
4637 goto err_put_lreq;
4638 }
4639
Ilya Dryomov81c65212018-10-11 12:58:33 +02004640 linger_submit(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004641 ret = linger_reg_commit_wait(lreq);
4642 if (ret) {
4643 linger_cancel(lreq);
4644 goto err_put_lreq;
4645 }
4646
4647 return lreq;
4648
4649err_put_lreq:
4650 linger_put(lreq);
4651 return ERR_PTR(ret);
4652}
4653EXPORT_SYMBOL(ceph_osdc_watch);
4654
4655/*
4656 * Releases a ref.
4657 *
4658 * Times out after mount_timeout to preserve rbd unmap behaviour
4659 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4660 * with mount_timeout").
4661 */
4662int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4663 struct ceph_osd_linger_request *lreq)
4664{
4665 struct ceph_options *opts = osdc->client->options;
4666 struct ceph_osd_request *req;
4667 int ret;
4668
4669 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4670 if (!req)
4671 return -ENOMEM;
4672
4673 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4674 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004675 req->r_flags = CEPH_OSD_FLAG_WRITE;
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02004676 ktime_get_real_ts64(&req->r_mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004677 osd_req_op_watch_init(req, 0, lreq->linger_id,
4678 CEPH_OSD_WATCH_OP_UNWATCH);
4679
4680 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4681 if (ret)
4682 goto out_put_req;
4683
4684 ceph_osdc_start_request(osdc, req, false);
4685 linger_cancel(lreq);
4686 linger_put(lreq);
4687 ret = wait_request_timeout(req, opts->mount_timeout);
4688
4689out_put_req:
4690 ceph_osdc_put_request(req);
4691 return ret;
4692}
4693EXPORT_SYMBOL(ceph_osdc_unwatch);
4694
4695static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4696 u64 notify_id, u64 cookie, void *payload,
Ilya Dryomov6d542282018-06-25 17:26:55 +02004697 u32 payload_len)
Ilya Dryomov922dab62016-05-26 01:15:02 +02004698{
4699 struct ceph_osd_req_op *op;
4700 struct ceph_pagelist *pl;
4701 int ret;
4702
4703 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4704
Ilya Dryomov33165d42018-09-28 15:38:34 +02004705 pl = ceph_pagelist_alloc(GFP_NOIO);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004706 if (!pl)
4707 return -ENOMEM;
4708
Ilya Dryomov922dab62016-05-26 01:15:02 +02004709 ret = ceph_pagelist_encode_64(pl, notify_id);
4710 ret |= ceph_pagelist_encode_64(pl, cookie);
4711 if (payload) {
4712 ret |= ceph_pagelist_encode_32(pl, payload_len);
4713 ret |= ceph_pagelist_append(pl, payload, payload_len);
4714 } else {
4715 ret |= ceph_pagelist_encode_32(pl, 0);
4716 }
4717 if (ret) {
4718 ceph_pagelist_release(pl);
4719 return -ENOMEM;
4720 }
4721
4722 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4723 op->indata_len = pl->length;
4724 return 0;
4725}
4726
4727int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4728 struct ceph_object_id *oid,
4729 struct ceph_object_locator *oloc,
4730 u64 notify_id,
4731 u64 cookie,
4732 void *payload,
Ilya Dryomov6d542282018-06-25 17:26:55 +02004733 u32 payload_len)
Ilya Dryomov922dab62016-05-26 01:15:02 +02004734{
4735 struct ceph_osd_request *req;
4736 int ret;
4737
4738 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4739 if (!req)
4740 return -ENOMEM;
4741
4742 ceph_oid_copy(&req->r_base_oid, oid);
4743 ceph_oloc_copy(&req->r_base_oloc, oloc);
4744 req->r_flags = CEPH_OSD_FLAG_READ;
4745
Ilya Dryomov26f887e2018-10-15 16:11:37 +02004746 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4747 payload_len);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004748 if (ret)
4749 goto out_put_req;
4750
Ilya Dryomov26f887e2018-10-15 16:11:37 +02004751 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004752 if (ret)
4753 goto out_put_req;
4754
4755 ceph_osdc_start_request(osdc, req, false);
4756 ret = ceph_osdc_wait_request(osdc, req);
4757
4758out_put_req:
4759 ceph_osdc_put_request(req);
4760 return ret;
4761}
4762EXPORT_SYMBOL(ceph_osdc_notify_ack);
4763
Ilya Dryomov19079202016-04-28 16:07:27 +02004764static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4765 u64 cookie, u32 prot_ver, u32 timeout,
Ilya Dryomov6d542282018-06-25 17:26:55 +02004766 void *payload, u32 payload_len)
Ilya Dryomov19079202016-04-28 16:07:27 +02004767{
4768 struct ceph_osd_req_op *op;
4769 struct ceph_pagelist *pl;
4770 int ret;
4771
4772 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4773 op->notify.cookie = cookie;
4774
Ilya Dryomov33165d42018-09-28 15:38:34 +02004775 pl = ceph_pagelist_alloc(GFP_NOIO);
Ilya Dryomov19079202016-04-28 16:07:27 +02004776 if (!pl)
4777 return -ENOMEM;
4778
Ilya Dryomov19079202016-04-28 16:07:27 +02004779 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4780 ret |= ceph_pagelist_encode_32(pl, timeout);
4781 ret |= ceph_pagelist_encode_32(pl, payload_len);
4782 ret |= ceph_pagelist_append(pl, payload, payload_len);
4783 if (ret) {
4784 ceph_pagelist_release(pl);
4785 return -ENOMEM;
4786 }
4787
4788 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4789 op->indata_len = pl->length;
4790 return 0;
4791}
4792
4793/*
4794 * @timeout: in seconds
4795 *
4796 * @preply_{pages,len} are initialized both on success and error.
4797 * The caller is responsible for:
4798 *
4799 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4800 */
4801int ceph_osdc_notify(struct ceph_osd_client *osdc,
4802 struct ceph_object_id *oid,
4803 struct ceph_object_locator *oloc,
4804 void *payload,
Ilya Dryomov6d542282018-06-25 17:26:55 +02004805 u32 payload_len,
Ilya Dryomov19079202016-04-28 16:07:27 +02004806 u32 timeout,
4807 struct page ***preply_pages,
4808 size_t *preply_len)
4809{
4810 struct ceph_osd_linger_request *lreq;
4811 struct page **pages;
4812 int ret;
4813
4814 WARN_ON(!timeout);
4815 if (preply_pages) {
4816 *preply_pages = NULL;
4817 *preply_len = 0;
4818 }
4819
4820 lreq = linger_alloc(osdc);
4821 if (!lreq)
4822 return -ENOMEM;
4823
4824 lreq->preply_pages = preply_pages;
4825 lreq->preply_len = preply_len;
4826
4827 ceph_oid_copy(&lreq->t.base_oid, oid);
4828 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4829 lreq->t.flags = CEPH_OSD_FLAG_READ;
4830
4831 lreq->reg_req = alloc_linger_request(lreq);
4832 if (!lreq->reg_req) {
4833 ret = -ENOMEM;
4834 goto out_put_lreq;
4835 }
4836
Ilya Dryomov81c65212018-10-11 12:58:33 +02004837 /*
4838 * Pass 0 for cookie because we don't know it yet, it will be
4839 * filled in by linger_submit().
4840 */
4841 ret = osd_req_op_notify_init(lreq->reg_req, 0, 0, 1, timeout,
4842 payload, payload_len);
4843 if (ret)
4844 goto out_put_lreq;
4845
Ilya Dryomov19079202016-04-28 16:07:27 +02004846 /* for notify_id */
4847 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4848 if (IS_ERR(pages)) {
4849 ret = PTR_ERR(pages);
4850 goto out_put_lreq;
4851 }
Ilya Dryomov19079202016-04-28 16:07:27 +02004852 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4853 response_data),
4854 pages, PAGE_SIZE, 0, false, true);
Ilya Dryomov19079202016-04-28 16:07:27 +02004855
Ilya Dryomov26f887e2018-10-15 16:11:37 +02004856 ret = ceph_osdc_alloc_messages(lreq->reg_req, GFP_NOIO);
4857 if (ret)
4858 goto out_put_lreq;
4859
Ilya Dryomov81c65212018-10-11 12:58:33 +02004860 linger_submit(lreq);
Ilya Dryomov19079202016-04-28 16:07:27 +02004861 ret = linger_reg_commit_wait(lreq);
4862 if (!ret)
4863 ret = linger_notify_finish_wait(lreq);
4864 else
4865 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4866
4867 linger_cancel(lreq);
4868out_put_lreq:
4869 linger_put(lreq);
4870 return ret;
4871}
4872EXPORT_SYMBOL(ceph_osdc_notify);
4873
Sage Weilf24e9982009-10-06 11:31:10 -07004874/*
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004875 * Return the number of milliseconds since the watch was last
4876 * confirmed, or an error. If there is an error, the watch is no
4877 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4878 */
4879int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4880 struct ceph_osd_linger_request *lreq)
4881{
4882 unsigned long stamp, age;
4883 int ret;
4884
4885 down_read(&osdc->lock);
4886 mutex_lock(&lreq->lock);
4887 stamp = lreq->watch_valid_thru;
4888 if (!list_empty(&lreq->pending_lworks)) {
4889 struct linger_work *lwork =
4890 list_first_entry(&lreq->pending_lworks,
4891 struct linger_work,
4892 pending_item);
4893
4894 if (time_before(lwork->queued_stamp, stamp))
4895 stamp = lwork->queued_stamp;
4896 }
4897 age = jiffies - stamp;
4898 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4899 lreq, lreq->linger_id, age, lreq->last_error);
4900 /* we are truncating to msecs, so return a safe upper bound */
4901 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4902
4903 mutex_unlock(&lreq->lock);
4904 up_read(&osdc->lock);
4905 return ret;
4906}
4907
Douglas Fullera4ed38d2015-07-17 13:18:07 -07004908static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4909{
4910 u8 struct_v;
4911 u32 struct_len;
4912 int ret;
4913
4914 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4915 &struct_v, &struct_len);
4916 if (ret)
4917 return ret;
4918
4919 ceph_decode_copy(p, &item->name, sizeof(item->name));
4920 item->cookie = ceph_decode_64(p);
4921 *p += 4; /* skip timeout_seconds */
4922 if (struct_v >= 2) {
4923 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4924 ceph_decode_addr(&item->addr);
4925 }
4926
4927 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4928 ENTITY_NAME(item->name), item->cookie,
4929 ceph_pr_addr(&item->addr.in_addr));
4930 return 0;
4931}
4932
4933static int decode_watchers(void **p, void *end,
4934 struct ceph_watch_item **watchers,
4935 u32 *num_watchers)
4936{
4937 u8 struct_v;
4938 u32 struct_len;
4939 int i;
4940 int ret;
4941
4942 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4943 &struct_v, &struct_len);
4944 if (ret)
4945 return ret;
4946
4947 *num_watchers = ceph_decode_32(p);
4948 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4949 if (!*watchers)
4950 return -ENOMEM;
4951
4952 for (i = 0; i < *num_watchers; i++) {
4953 ret = decode_watcher(p, end, *watchers + i);
4954 if (ret) {
4955 kfree(*watchers);
4956 return ret;
4957 }
4958 }
4959
4960 return 0;
4961}
4962
4963/*
4964 * On success, the caller is responsible for:
4965 *
4966 * kfree(watchers);
4967 */
4968int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4969 struct ceph_object_id *oid,
4970 struct ceph_object_locator *oloc,
4971 struct ceph_watch_item **watchers,
4972 u32 *num_watchers)
4973{
4974 struct ceph_osd_request *req;
4975 struct page **pages;
4976 int ret;
4977
4978 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4979 if (!req)
4980 return -ENOMEM;
4981
4982 ceph_oid_copy(&req->r_base_oid, oid);
4983 ceph_oloc_copy(&req->r_base_oloc, oloc);
4984 req->r_flags = CEPH_OSD_FLAG_READ;
4985
Douglas Fullera4ed38d2015-07-17 13:18:07 -07004986 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4987 if (IS_ERR(pages)) {
4988 ret = PTR_ERR(pages);
4989 goto out_put_req;
4990 }
4991
4992 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4993 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4994 response_data),
4995 pages, PAGE_SIZE, 0, false, true);
4996
Ilya Dryomov26f887e2018-10-15 16:11:37 +02004997 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4998 if (ret)
4999 goto out_put_req;
5000
Douglas Fullera4ed38d2015-07-17 13:18:07 -07005001 ceph_osdc_start_request(osdc, req, false);
5002 ret = ceph_osdc_wait_request(osdc, req);
5003 if (ret >= 0) {
5004 void *p = page_address(pages[0]);
5005 void *const end = p + req->r_ops[0].outdata_len;
5006
5007 ret = decode_watchers(&p, end, watchers, num_watchers);
5008 }
5009
5010out_put_req:
5011 ceph_osdc_put_request(req);
5012 return ret;
5013}
5014EXPORT_SYMBOL(ceph_osdc_list_watchers);
5015
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02005016/*
Josh Durgindd935f42013-08-28 21:43:09 -07005017 * Call all pending notify callbacks - for use after a watch is
5018 * unregistered, to make sure no more callbacks for it will be invoked
5019 */
stephen hemmingerf64794492014-06-10 20:30:13 -07005020void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07005021{
Ilya Dryomov99d16942016-08-12 16:11:41 +02005022 dout("%s osdc %p\n", __func__, osdc);
Josh Durgindd935f42013-08-28 21:43:09 -07005023 flush_workqueue(osdc->notify_wq);
5024}
5025EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5026
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005027void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
5028{
5029 down_read(&osdc->lock);
5030 maybe_request_map(osdc);
5031 up_read(&osdc->lock);
5032}
5033EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
Josh Durgindd935f42013-08-28 21:43:09 -07005034
5035/*
Douglas Fuller428a7152015-06-17 14:49:45 -04005036 * Execute an OSD class method on an object.
5037 *
5038 * @flags: CEPH_OSD_FLAG_*
Ilya Dryomov2544a022017-01-25 18:16:21 +01005039 * @resp_len: in/out param for reply length
Douglas Fuller428a7152015-06-17 14:49:45 -04005040 */
5041int ceph_osdc_call(struct ceph_osd_client *osdc,
5042 struct ceph_object_id *oid,
5043 struct ceph_object_locator *oloc,
5044 const char *class, const char *method,
5045 unsigned int flags,
5046 struct page *req_page, size_t req_len,
5047 struct page *resp_page, size_t *resp_len)
5048{
5049 struct ceph_osd_request *req;
5050 int ret;
5051
Ilya Dryomov2544a022017-01-25 18:16:21 +01005052 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
5053 return -E2BIG;
5054
Douglas Fuller428a7152015-06-17 14:49:45 -04005055 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5056 if (!req)
5057 return -ENOMEM;
5058
5059 ceph_oid_copy(&req->r_base_oid, oid);
5060 ceph_oloc_copy(&req->r_base_oloc, oloc);
5061 req->r_flags = flags;
5062
Ilya Dryomov24639ce562018-09-26 19:12:07 +02005063 ret = osd_req_op_cls_init(req, 0, class, method);
Chengguang Xufe943d52018-04-12 12:04:55 +08005064 if (ret)
5065 goto out_put_req;
5066
Douglas Fuller428a7152015-06-17 14:49:45 -04005067 if (req_page)
5068 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5069 0, false, false);
5070 if (resp_page)
5071 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
Ilya Dryomov2544a022017-01-25 18:16:21 +01005072 *resp_len, 0, false, false);
Douglas Fuller428a7152015-06-17 14:49:45 -04005073
Ilya Dryomov26f887e2018-10-15 16:11:37 +02005074 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
5075 if (ret)
5076 goto out_put_req;
5077
Douglas Fuller428a7152015-06-17 14:49:45 -04005078 ceph_osdc_start_request(osdc, req, false);
5079 ret = ceph_osdc_wait_request(osdc, req);
5080 if (ret >= 0) {
5081 ret = req->r_ops[0].rval;
5082 if (resp_page)
5083 *resp_len = req->r_ops[0].outdata_len;
5084 }
5085
5086out_put_req:
5087 ceph_osdc_put_request(req);
5088 return ret;
5089}
5090EXPORT_SYMBOL(ceph_osdc_call);
5091
5092/*
Sage Weilf24e9982009-10-06 11:31:10 -07005093 * init, shutdown
5094 */
5095int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5096{
5097 int err;
5098
5099 dout("init\n");
5100 osdc->client = client;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005101 init_rwsem(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07005102 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005103 INIT_LIST_HEAD(&osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02005104 spin_lock_init(&osdc->osd_lru_lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005105 osd_init(&osdc->homeless_osd);
5106 osdc->homeless_osd.o_osdc = osdc;
5107 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
Ilya Dryomov264048a2016-11-08 15:15:24 +01005108 osdc->last_linger_id = CEPH_LINGER_ID_START;
Ilya Dryomov922dab62016-05-26 01:15:02 +02005109 osdc->linger_requests = RB_ROOT;
Ilya Dryomov46092452016-04-28 16:07:27 +02005110 osdc->map_checks = RB_ROOT;
5111 osdc->linger_map_checks = RB_ROOT;
Sage Weilf24e9982009-10-06 11:31:10 -07005112 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005113 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5114
Sage Weil5f44f142009-11-18 14:52:18 -08005115 err = -ENOMEM;
Ilya Dryomove5253a72016-04-28 16:07:25 +02005116 osdc->osdmap = ceph_osdmap_alloc();
5117 if (!osdc->osdmap)
5118 goto out;
5119
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01005120 osdc->req_mempool = mempool_create_slab_pool(10,
5121 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07005122 if (!osdc->req_mempool)
Ilya Dryomove5253a72016-04-28 16:07:25 +02005123 goto out_map;
Sage Weilf24e9982009-10-06 11:31:10 -07005124
Sage Weild50b4092012-07-09 14:22:34 -07005125 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02005126 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07005127 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08005128 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07005129 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02005130 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
5131 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08005132 if (err < 0)
5133 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005134
Dan Carpenterdbcae082013-08-15 08:58:59 +03005135 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005136 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03005137 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02005138 goto out_msgpool_reply;
5139
Ilya Dryomov88bc1922018-05-21 16:00:29 +02005140 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5141 if (!osdc->completion_wq)
5142 goto out_notify_wq;
5143
Ilya Dryomovfbca9632016-04-28 16:07:24 +02005144 schedule_delayed_work(&osdc->timeout_work,
5145 osdc->client->options->osd_keepalive_timeout);
Ilya Dryomovb37ee1b2016-04-28 16:07:24 +02005146 schedule_delayed_work(&osdc->osds_timeout_work,
5147 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5148
Sage Weilf24e9982009-10-06 11:31:10 -07005149 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08005150
Ilya Dryomov88bc1922018-05-21 16:00:29 +02005151out_notify_wq:
5152 destroy_workqueue(osdc->notify_wq);
Ilya Dryomovc172ec52014-01-31 17:49:22 +02005153out_msgpool_reply:
5154 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08005155out_msgpool:
5156 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08005157out_mempool:
5158 mempool_destroy(osdc->req_mempool);
Ilya Dryomove5253a72016-04-28 16:07:25 +02005159out_map:
5160 ceph_osdmap_destroy(osdc->osdmap);
Sage Weil5f44f142009-11-18 14:52:18 -08005161out:
5162 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07005163}
5164
5165void ceph_osdc_stop(struct ceph_osd_client *osdc)
5166{
Ilya Dryomov88bc1922018-05-21 16:00:29 +02005167 destroy_workqueue(osdc->completion_wq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005168 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07005169 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08005170 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005171
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005172 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005173 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5174 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5175 struct ceph_osd, o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005176 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005177 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005178 up_write(&osdc->lock);
Elena Reshetova02113a02017-03-17 14:10:28 +02005179 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005180 osd_cleanup(&osdc->homeless_osd);
5181
5182 WARN_ON(!list_empty(&osdc->osd_lru));
Ilya Dryomov922dab62016-05-26 01:15:02 +02005183 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
Ilya Dryomov46092452016-04-28 16:07:27 +02005184 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5185 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005186 WARN_ON(atomic_read(&osdc->num_requests));
5187 WARN_ON(atomic_read(&osdc->num_homeless));
Ilya Dryomov42a2c092016-04-28 16:07:22 +02005188
Ilya Dryomove5253a72016-04-28 16:07:25 +02005189 ceph_osdmap_destroy(osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07005190 mempool_destroy(osdc->req_mempool);
5191 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08005192 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07005193}
5194
5195/*
5196 * Read some contiguous pages. If we cross a stripe boundary, shorten
5197 * *plen. Return number of bytes read, or error.
5198 */
5199int ceph_osdc_readpages(struct ceph_osd_client *osdc,
5200 struct ceph_vino vino, struct ceph_file_layout *layout,
5201 u64 off, u64 *plen,
5202 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08005203 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07005204{
5205 struct ceph_osd_request *req;
5206 int rc = 0;
5207
5208 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
5209 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08005210 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07005211 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05005212 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06005213 false);
Sage Weil68162822012-09-24 21:01:02 -07005214 if (IS_ERR(req))
5215 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07005216
5217 /* it may be a short read due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05005218 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05005219 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07005220
Alex Eldere0c59482013-03-07 15:38:25 -06005221 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05005222 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07005223
5224 rc = ceph_osdc_start_request(osdc, req, false);
5225 if (!rc)
5226 rc = ceph_osdc_wait_request(osdc, req);
5227
5228 ceph_osdc_put_request(req);
5229 dout("readpages result %d\n", rc);
5230 return rc;
5231}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005232EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07005233
5234/*
5235 * do a synchronous write on N pages
5236 */
5237int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5238 struct ceph_file_layout *layout,
5239 struct ceph_snap_context *snapc,
5240 u64 off, u64 len,
5241 u32 truncate_seq, u64 truncate_size,
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02005242 struct timespec64 *mtime,
Alex Elder24808822013-02-15 11:42:29 -06005243 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07005244{
5245 struct ceph_osd_request *req;
5246 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08005247 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07005248
Yan, Zheng715e4cd2014-11-13 14:40:37 +08005249 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01005250 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05005251 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06005252 true);
Sage Weil68162822012-09-24 21:01:02 -07005253 if (IS_ERR(req))
5254 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07005255
5256 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05005257 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05005258 false, false);
5259 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07005260
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02005261 req->r_mtime = *mtime;
Alex Elder87f979d2013-02-15 11:42:29 -06005262 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07005263 if (!rc)
5264 rc = ceph_osdc_wait_request(osdc, req);
5265
5266 ceph_osdc_put_request(req);
5267 if (rc == 0)
5268 rc = len;
5269 dout("writepages result %d\n", rc);
5270 return rc;
5271}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005272EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07005273
Luis Henriques23ddf9b2018-10-15 16:45:58 +01005274static int osd_req_op_copy_from_init(struct ceph_osd_request *req,
5275 u64 src_snapid, u64 src_version,
5276 struct ceph_object_id *src_oid,
5277 struct ceph_object_locator *src_oloc,
5278 u32 src_fadvise_flags,
5279 u32 dst_fadvise_flags,
5280 u8 copy_from_flags)
5281{
5282 struct ceph_osd_req_op *op;
5283 struct page **pages;
5284 void *p, *end;
5285
5286 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
5287 if (IS_ERR(pages))
5288 return PTR_ERR(pages);
5289
5290 op = _osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM, dst_fadvise_flags);
5291 op->copy_from.snapid = src_snapid;
5292 op->copy_from.src_version = src_version;
5293 op->copy_from.flags = copy_from_flags;
5294 op->copy_from.src_fadvise_flags = src_fadvise_flags;
5295
5296 p = page_address(pages[0]);
5297 end = p + PAGE_SIZE;
5298 ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
5299 encode_oloc(&p, end, src_oloc);
5300 op->indata_len = PAGE_SIZE - (end - p);
5301
5302 ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
5303 op->indata_len, 0, false, true);
5304 return 0;
5305}
5306
5307int ceph_osdc_copy_from(struct ceph_osd_client *osdc,
5308 u64 src_snapid, u64 src_version,
5309 struct ceph_object_id *src_oid,
5310 struct ceph_object_locator *src_oloc,
5311 u32 src_fadvise_flags,
5312 struct ceph_object_id *dst_oid,
5313 struct ceph_object_locator *dst_oloc,
5314 u32 dst_fadvise_flags,
5315 u8 copy_from_flags)
5316{
5317 struct ceph_osd_request *req;
5318 int ret;
5319
5320 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
5321 if (!req)
5322 return -ENOMEM;
5323
5324 req->r_flags = CEPH_OSD_FLAG_WRITE;
5325
5326 ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
5327 ceph_oid_copy(&req->r_t.base_oid, dst_oid);
5328
5329 ret = osd_req_op_copy_from_init(req, src_snapid, src_version, src_oid,
5330 src_oloc, src_fadvise_flags,
5331 dst_fadvise_flags, copy_from_flags);
5332 if (ret)
5333 goto out;
5334
5335 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
5336 if (ret)
5337 goto out;
5338
5339 ceph_osdc_start_request(osdc, req, false);
5340 ret = ceph_osdc_wait_request(osdc, req);
5341
5342out:
5343 ceph_osdc_put_request(req);
5344 return ret;
5345}
5346EXPORT_SYMBOL(ceph_osdc_copy_from);
5347
Chengguang Xu57a35df2018-03-10 20:32:05 +08005348int __init ceph_osdc_setup(void)
Alex Elder5522ae02013-05-01 12:43:04 -05005349{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01005350 size_t size = sizeof(struct ceph_osd_request) +
5351 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5352
Alex Elder5522ae02013-05-01 12:43:04 -05005353 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01005354 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5355 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05005356
5357 return ceph_osd_request_cache ? 0 : -ENOMEM;
5358}
Alex Elder5522ae02013-05-01 12:43:04 -05005359
5360void ceph_osdc_cleanup(void)
5361{
5362 BUG_ON(!ceph_osd_request_cache);
5363 kmem_cache_destroy(ceph_osd_request_cache);
5364 ceph_osd_request_cache = NULL;
5365}
Alex Elder5522ae02013-05-01 12:43:04 -05005366
Sage Weilf24e9982009-10-06 11:31:10 -07005367/*
5368 * handle incoming message
5369 */
5370static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5371{
5372 struct ceph_osd *osd = con->private;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005373 struct ceph_osd_client *osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07005374 int type = le16_to_cpu(msg->hdr.type);
5375
Sage Weilf24e9982009-10-06 11:31:10 -07005376 switch (type) {
5377 case CEPH_MSG_OSD_MAP:
5378 ceph_osdc_handle_map(osdc, msg);
5379 break;
5380 case CEPH_MSG_OSD_OPREPLY:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005381 handle_reply(osd, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07005382 break;
Ilya Dryomova02a9462017-06-19 12:18:05 +02005383 case CEPH_MSG_OSD_BACKOFF:
5384 handle_backoff(osd, msg);
5385 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005386 case CEPH_MSG_WATCH_NOTIFY:
5387 handle_watch_notify(osdc, msg);
5388 break;
Sage Weilf24e9982009-10-06 11:31:10 -07005389
5390 default:
5391 pr_err("received unknown message type %d %s\n", type,
5392 ceph_msg_type_name(type));
5393 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005394
Sage Weilf24e9982009-10-06 11:31:10 -07005395 ceph_msg_put(msg);
5396}
5397
Sage Weil5b3a4db2010-02-19 21:43:23 -08005398/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005399 * Lookup and return message for incoming reply. Don't try to do
5400 * anything about a larger than preallocated data portion of the
5401 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08005402 */
5403static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08005404 struct ceph_msg_header *hdr,
5405 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07005406{
5407 struct ceph_osd *osd = con->private;
5408 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005409 struct ceph_msg *m = NULL;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005410 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02005411 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005412 int data_len = le32_to_cpu(hdr->data_len);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005413 u64 tid = le64_to_cpu(hdr->tid);
Sage Weilf24e9982009-10-06 11:31:10 -07005414
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005415 down_read(&osdc->lock);
5416 if (!osd_registered(osd)) {
5417 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5418 *skip = 1;
5419 goto out_unlock_osdc;
5420 }
5421 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5422
5423 mutex_lock(&osd->lock);
5424 req = lookup_request(&osd->o_requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005425 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01005426 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5427 osd->o_osd, tid);
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005428 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005429 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005430 }
Sage Weilc16e7862010-03-01 13:02:00 -08005431
Alex Elderace6d3a2013-04-01 16:12:14 -05005432 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08005433
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02005434 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005435 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5436 __func__, osd->o_osd, req->r_tid, front_len,
5437 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02005438 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5439 false);
Sage Weila79832f2010-04-01 16:06:19 -07005440 if (!m)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005441 goto out_unlock_session;
Sage Weilc16e7862010-03-01 13:02:00 -08005442 ceph_msg_put(req->r_reply);
5443 req->r_reply = m;
5444 }
Sage Weilc16e7862010-03-01 13:02:00 -08005445
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005446 if (data_len > req->r_reply->data_length) {
5447 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5448 __func__, osd->o_osd, req->r_tid, data_len,
5449 req->r_reply->data_length);
5450 m = NULL;
5451 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005452 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005453 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005454
5455 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08005456 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005457
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005458out_unlock_session:
5459 mutex_unlock(&osd->lock);
5460out_unlock_osdc:
5461 up_read(&osdc->lock);
Yehuda Sadeh24504182010-01-08 13:58:34 -08005462 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08005463}
5464
Ilya Dryomov19079202016-04-28 16:07:27 +02005465/*
5466 * TODO: switch to a msg-owned pagelist
5467 */
5468static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5469{
5470 struct ceph_msg *m;
5471 int type = le16_to_cpu(hdr->type);
5472 u32 front_len = le32_to_cpu(hdr->front_len);
5473 u32 data_len = le32_to_cpu(hdr->data_len);
5474
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02005475 m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
Ilya Dryomov19079202016-04-28 16:07:27 +02005476 if (!m)
5477 return NULL;
5478
5479 if (data_len) {
5480 struct page **pages;
5481 struct ceph_osd_data osd_data;
5482
5483 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5484 GFP_NOIO);
Wei Yongjunc22e8532016-07-30 00:37:57 +00005485 if (IS_ERR(pages)) {
Ilya Dryomov19079202016-04-28 16:07:27 +02005486 ceph_msg_put(m);
5487 return NULL;
5488 }
5489
5490 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5491 false);
5492 ceph_osdc_msg_data_add(m, &osd_data);
5493 }
5494
5495 return m;
5496}
5497
Sage Weil5b3a4db2010-02-19 21:43:23 -08005498static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5499 struct ceph_msg_header *hdr,
5500 int *skip)
5501{
5502 struct ceph_osd *osd = con->private;
5503 int type = le16_to_cpu(hdr->type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005504
Alex Elder1c20f2d2012-06-04 14:43:32 -05005505 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08005506 switch (type) {
5507 case CEPH_MSG_OSD_MAP:
Ilya Dryomova02a9462017-06-19 12:18:05 +02005508 case CEPH_MSG_OSD_BACKOFF:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005509 case CEPH_MSG_WATCH_NOTIFY:
Ilya Dryomov19079202016-04-28 16:07:27 +02005510 return alloc_msg_with_page_vector(hdr);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005511 case CEPH_MSG_OSD_OPREPLY:
5512 return get_reply(con, hdr, skip);
5513 default:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005514 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5515 osd->o_osd, type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005516 *skip = 1;
5517 return NULL;
5518 }
Sage Weilf24e9982009-10-06 11:31:10 -07005519}
5520
5521/*
5522 * Wrappers to refcount containing ceph_osd struct
5523 */
5524static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5525{
5526 struct ceph_osd *osd = con->private;
5527 if (get_osd(osd))
5528 return con;
5529 return NULL;
5530}
5531
5532static void put_osd_con(struct ceph_connection *con)
5533{
5534 struct ceph_osd *osd = con->private;
5535 put_osd(osd);
5536}
5537
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005538/*
5539 * authentication
5540 */
Alex Eldera3530df2012-05-16 15:16:39 -05005541/*
5542 * Note: returned pointer is the address of a structure that's
5543 * managed separately. Caller must *not* attempt to free it.
5544 */
5545static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05005546 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005547{
5548 struct ceph_osd *o = con->private;
5549 struct ceph_osd_client *osdc = o->o_osdc;
5550 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05005551 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005552
Alex Elder74f18692012-05-16 15:16:39 -05005553 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02005554 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05005555 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005556 }
Sage Weil27859f92013-03-25 10:26:14 -07005557 if (!auth->authorizer) {
5558 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5559 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005560 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05005561 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07005562 } else {
5563 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07005564 auth);
5565 if (ret)
5566 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005567 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005568 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05005569
Alex Eldera3530df2012-05-16 15:16:39 -05005570 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005571}
5572
Ilya Dryomov6daca132018-07-27 19:18:34 +02005573static int add_authorizer_challenge(struct ceph_connection *con,
5574 void *challenge_buf, int challenge_buf_len)
5575{
5576 struct ceph_osd *o = con->private;
5577 struct ceph_osd_client *osdc = o->o_osdc;
5578 struct ceph_auth_client *ac = osdc->client->monc.auth;
5579
5580 return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
5581 challenge_buf, challenge_buf_len);
5582}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005583
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005584static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005585{
5586 struct ceph_osd *o = con->private;
5587 struct ceph_osd_client *osdc = o->o_osdc;
5588 struct ceph_auth_client *ac = osdc->client->monc.auth;
5589
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005590 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005591}
5592
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005593static int invalidate_authorizer(struct ceph_connection *con)
5594{
5595 struct ceph_osd *o = con->private;
5596 struct ceph_osd_client *osdc = o->o_osdc;
5597 struct ceph_auth_client *ac = osdc->client->monc.auth;
5598
Sage Weil27859f92013-03-25 10:26:14 -07005599 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005600 return ceph_monc_validate_auth(&osdc->client->monc);
5601}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005602
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005603static void osd_reencode_message(struct ceph_msg *msg)
5604{
Ilya Dryomov914902a2017-07-14 16:08:54 +02005605 int type = le16_to_cpu(msg->hdr.type);
5606
5607 if (type == CEPH_MSG_OSD_OP)
5608 encode_request_finish(msg);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005609}
5610
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005611static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005612{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005613 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005614 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005615
Yan, Zheng33d07332014-11-04 16:33:37 +08005616 return ceph_auth_sign_message(auth, msg);
5617}
5618
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005619static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005620{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005621 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005622 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005623
Yan, Zheng33d07332014-11-04 16:33:37 +08005624 return ceph_auth_check_message_signature(auth, msg);
5625}
5626
Tobias Klauser9e327892010-05-20 10:40:19 +02005627static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07005628 .get = get_osd_con,
5629 .put = put_osd_con,
5630 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005631 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02005632 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005633 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005634 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07005635 .alloc_msg = alloc_msg,
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005636 .reencode_message = osd_reencode_message,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005637 .sign_message = osd_sign_message,
5638 .check_message_signature = osd_check_message_signature,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005639 .fault = osd_fault,
Sage Weilf24e9982009-10-06 11:31:10 -07005640};