blob: 86a9737d8e3ff7f86c0ea12e9d417e084b725816 [file] [log] [blame]
Alex Eldera4ce40a2013-04-05 01:27:12 -05001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07005#include <linux/err.h>
6#include <linux/highmem.h>
7#include <linux/mm.h>
8#include <linux/pagemap.h>
9#include <linux/slab.h>
10#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070011#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
Sage Weilf24e9982009-10-06 11:31:10 -070014
Ilya Dryomov8cb441c2017-06-15 16:30:54 +020015#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070016#include <linux/ceph/libceph.h>
17#include <linux/ceph/osd_client.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/auth.h>
21#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070022
Sage Weilc16e7862010-03-01 13:02:00 -080023#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080024
Alex Elder5522ae02013-05-01 12:43:04 -050025static struct kmem_cache *ceph_osd_request_cache;
26
Tobias Klauser9e327892010-05-20 10:40:19 +020027static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070028
Sage Weilf24e9982009-10-06 11:31:10 -070029/*
30 * Implement client access to distributed object storage cluster.
31 *
32 * All data objects are stored within a cluster/cloud of OSDs, or
33 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
34 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
35 * remote daemons serving up and coordinating consistent and safe
36 * access to storage.
37 *
38 * Cluster membership and the mapping of data objects onto storage devices
39 * are described by the osd map.
40 *
41 * We keep track of pending OSD requests (read, write), resubmit
42 * requests to different OSDs when the cluster topology/data layout
43 * change, or retry the affected requests when the communications
44 * channel with an OSD is reset.
45 */
46
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020047static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
48static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
Ilya Dryomov922dab62016-05-26 01:15:02 +020049static void link_linger(struct ceph_osd *osd,
50 struct ceph_osd_linger_request *lreq);
51static void unlink_linger(struct ceph_osd *osd,
52 struct ceph_osd_linger_request *lreq);
Ilya Dryomova02a9462017-06-19 12:18:05 +020053static void clear_backoffs(struct ceph_osd *osd);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020054
55#if 1
56static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
57{
58 bool wrlocked = true;
59
60 if (unlikely(down_read_trylock(sem))) {
61 wrlocked = false;
62 up_read(sem);
63 }
64
65 return wrlocked;
66}
67static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
68{
69 WARN_ON(!rwsem_is_locked(&osdc->lock));
70}
71static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
72{
73 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
74}
75static inline void verify_osd_locked(struct ceph_osd *osd)
76{
77 struct ceph_osd_client *osdc = osd->o_osdc;
78
79 WARN_ON(!(mutex_is_locked(&osd->lock) &&
80 rwsem_is_locked(&osdc->lock)) &&
81 !rwsem_is_wrlocked(&osdc->lock));
82}
Ilya Dryomov922dab62016-05-26 01:15:02 +020083static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
84{
85 WARN_ON(!mutex_is_locked(&lreq->lock));
86}
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020087#else
88static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
89static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
90static inline void verify_osd_locked(struct ceph_osd *osd) { }
Ilya Dryomov922dab62016-05-26 01:15:02 +020091static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020092#endif
93
Sage Weilf24e9982009-10-06 11:31:10 -070094/*
95 * calculate the mapping of a file extent onto an object, and fill out the
96 * request accordingly. shorten extent as necessary if it crosses an
97 * object boundary.
98 *
99 * fill osd op in request message.
100 */
Alex Elderdbe0fc42013-02-15 22:10:17 -0600101static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -0500102 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -0700103{
Alex Elder60e56f12013-02-15 11:42:29 -0600104 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -0700105 int r;
Sage Weilf24e9982009-10-06 11:31:10 -0700106
Alex Elder60e56f12013-02-15 11:42:29 -0600107 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -0500108 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
109 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -0700110 if (r < 0)
111 return r;
Alex Elder75d1c942013-03-13 20:50:00 -0500112 if (*objlen < orig_len) {
113 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -0600114 dout(" skipping last %llu, final file extent %llu~%llu\n",
115 orig_len - *plen, off, *plen);
116 }
117
Alex Elder75d1c942013-03-13 20:50:00 -0500118 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -0700119
Alex Elder3ff5f382013-02-15 22:10:17 -0600120 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700121}
122
Alex Elderc54d47b2013-04-03 01:28:57 -0500123static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124{
125 memset(osd_data, 0, sizeof (*osd_data));
126 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127}
128
Alex Eldera4ce40a2013-04-05 01:27:12 -0500129static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500130 struct page **pages, u64 length, u32 alignment,
131 bool pages_from_pool, bool own_pages)
132{
133 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
134 osd_data->pages = pages;
135 osd_data->length = length;
136 osd_data->alignment = alignment;
137 osd_data->pages_from_pool = pages_from_pool;
138 osd_data->own_pages = own_pages;
139}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500140
Alex Eldera4ce40a2013-04-05 01:27:12 -0500141static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500142 struct ceph_pagelist *pagelist)
143{
144 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
145 osd_data->pagelist = pagelist;
146}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500147
148#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500149static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500150 struct bio *bio, size_t bio_length)
151{
152 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
153 osd_data->bio = bio;
154 osd_data->bio_length = bio_length;
155}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500156#endif /* CONFIG_BLOCK */
157
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300158#define osd_req_op_data(oreq, whch, typ, fld) \
159({ \
160 struct ceph_osd_request *__oreq = (oreq); \
161 unsigned int __whch = (whch); \
162 BUG_ON(__whch >= __oreq->r_num_ops); \
163 &__oreq->r_ops[__whch].typ.fld; \
164})
Alex Elder863c7eb2013-04-15 14:50:36 -0500165
Alex Elder49719772013-02-11 12:33:24 -0600166static struct ceph_osd_data *
167osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
168{
169 BUG_ON(which >= osd_req->r_num_ops);
170
171 return &osd_req->r_ops[which].raw_data_in;
172}
173
Alex Eldera4ce40a2013-04-05 01:27:12 -0500174struct ceph_osd_data *
175osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500176 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500177{
Alex Elder863c7eb2013-04-15 14:50:36 -0500178 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500179}
180EXPORT_SYMBOL(osd_req_op_extent_osd_data);
181
Alex Elder49719772013-02-11 12:33:24 -0600182void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
183 unsigned int which, struct page **pages,
184 u64 length, u32 alignment,
185 bool pages_from_pool, bool own_pages)
186{
187 struct ceph_osd_data *osd_data;
188
189 osd_data = osd_req_op_raw_data_in(osd_req, which);
190 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
191 pages_from_pool, own_pages);
192}
193EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
194
Alex Eldera4ce40a2013-04-05 01:27:12 -0500195void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500196 unsigned int which, struct page **pages,
197 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500198 bool pages_from_pool, bool own_pages)
199{
200 struct ceph_osd_data *osd_data;
201
Alex Elder863c7eb2013-04-15 14:50:36 -0500202 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500203 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
204 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500205}
206EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
207
208void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500209 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500210{
211 struct ceph_osd_data *osd_data;
212
Alex Elder863c7eb2013-04-15 14:50:36 -0500213 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500214 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500215}
216EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
217
218#ifdef CONFIG_BLOCK
219void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500220 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500221{
222 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500223
224 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500225 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500226}
227EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
228#endif /* CONFIG_BLOCK */
229
230static void osd_req_op_cls_request_info_pagelist(
231 struct ceph_osd_request *osd_req,
232 unsigned int which, struct ceph_pagelist *pagelist)
233{
234 struct ceph_osd_data *osd_data;
235
Alex Elder863c7eb2013-04-15 14:50:36 -0500236 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500237 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500238}
239
Alex Elder04017e22013-04-05 14:46:02 -0500240void osd_req_op_cls_request_data_pagelist(
241 struct ceph_osd_request *osd_req,
242 unsigned int which, struct ceph_pagelist *pagelist)
243{
244 struct ceph_osd_data *osd_data;
245
Alex Elder863c7eb2013-04-15 14:50:36 -0500246 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500247 ceph_osd_data_pagelist_init(osd_data, pagelist);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200248 osd_req->r_ops[which].cls.indata_len += pagelist->length;
249 osd_req->r_ops[which].indata_len += pagelist->length;
Alex Elder04017e22013-04-05 14:46:02 -0500250}
251EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
252
Alex Elder6c57b552013-04-19 15:34:49 -0500253void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
254 unsigned int which, struct page **pages, u64 length,
255 u32 alignment, bool pages_from_pool, bool own_pages)
256{
257 struct ceph_osd_data *osd_data;
258
259 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
260 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
261 pages_from_pool, own_pages);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200262 osd_req->r_ops[which].cls.indata_len += length;
263 osd_req->r_ops[which].indata_len += length;
Alex Elder6c57b552013-04-19 15:34:49 -0500264}
265EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
266
Alex Eldera4ce40a2013-04-05 01:27:12 -0500267void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
268 unsigned int which, struct page **pages, u64 length,
269 u32 alignment, bool pages_from_pool, bool own_pages)
270{
271 struct ceph_osd_data *osd_data;
272
Alex Elder863c7eb2013-04-15 14:50:36 -0500273 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500274 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
275 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500276}
277EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
278
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500279static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
280{
281 switch (osd_data->type) {
282 case CEPH_OSD_DATA_TYPE_NONE:
283 return 0;
284 case CEPH_OSD_DATA_TYPE_PAGES:
285 return osd_data->length;
286 case CEPH_OSD_DATA_TYPE_PAGELIST:
287 return (u64)osd_data->pagelist->length;
288#ifdef CONFIG_BLOCK
289 case CEPH_OSD_DATA_TYPE_BIO:
290 return (u64)osd_data->bio_length;
291#endif /* CONFIG_BLOCK */
292 default:
293 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
294 return 0;
295 }
296}
297
Alex Elderc54d47b2013-04-03 01:28:57 -0500298static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
299{
Alex Elder54764922013-04-05 01:27:12 -0500300 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500301 int num_pages;
302
303 num_pages = calc_pages_for((u64)osd_data->alignment,
304 (u64)osd_data->length);
305 ceph_release_page_vector(osd_data->pages, num_pages);
306 }
Alex Elder54764922013-04-05 01:27:12 -0500307 ceph_osd_data_init(osd_data);
308}
309
310static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
311 unsigned int which)
312{
313 struct ceph_osd_req_op *op;
314
315 BUG_ON(which >= osd_req->r_num_ops);
316 op = &osd_req->r_ops[which];
317
318 switch (op->op) {
319 case CEPH_OSD_OP_READ:
320 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200321 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500322 ceph_osd_data_release(&op->extent.osd_data);
323 break;
324 case CEPH_OSD_OP_CALL:
325 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500326 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500327 ceph_osd_data_release(&op->cls.response_data);
328 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800329 case CEPH_OSD_OP_SETXATTR:
330 case CEPH_OSD_OP_CMPXATTR:
331 ceph_osd_data_release(&op->xattr.osd_data);
332 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800333 case CEPH_OSD_OP_STAT:
334 ceph_osd_data_release(&op->raw_data_in);
335 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200336 case CEPH_OSD_OP_NOTIFY_ACK:
337 ceph_osd_data_release(&op->notify_ack.request_data);
338 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200339 case CEPH_OSD_OP_NOTIFY:
340 ceph_osd_data_release(&op->notify.request_data);
341 ceph_osd_data_release(&op->notify.response_data);
342 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700343 case CEPH_OSD_OP_LIST_WATCHERS:
344 ceph_osd_data_release(&op->list_watchers.response_data);
345 break;
Alex Elder54764922013-04-05 01:27:12 -0500346 default:
347 break;
348 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500349}
350
Sage Weilf24e9982009-10-06 11:31:10 -0700351/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200352 * Assumes @t is zero-initialized.
353 */
354static void target_init(struct ceph_osd_request_target *t)
355{
356 ceph_oid_init(&t->base_oid);
357 ceph_oloc_init(&t->base_oloc);
358 ceph_oid_init(&t->target_oid);
359 ceph_oloc_init(&t->target_oloc);
360
361 ceph_osds_init(&t->acting);
362 ceph_osds_init(&t->up);
363 t->size = -1;
364 t->min_size = -1;
365
366 t->osd = CEPH_HOMELESS_OSD;
367}
368
Ilya Dryomov922dab62016-05-26 01:15:02 +0200369static void target_copy(struct ceph_osd_request_target *dest,
370 const struct ceph_osd_request_target *src)
371{
372 ceph_oid_copy(&dest->base_oid, &src->base_oid);
373 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
374 ceph_oid_copy(&dest->target_oid, &src->target_oid);
375 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
376
377 dest->pgid = src->pgid; /* struct */
Ilya Dryomovdc98ff72017-06-15 16:30:53 +0200378 dest->spgid = src->spgid; /* struct */
Ilya Dryomov922dab62016-05-26 01:15:02 +0200379 dest->pg_num = src->pg_num;
380 dest->pg_num_mask = src->pg_num_mask;
381 ceph_osds_copy(&dest->acting, &src->acting);
382 ceph_osds_copy(&dest->up, &src->up);
383 dest->size = src->size;
384 dest->min_size = src->min_size;
385 dest->sort_bitwise = src->sort_bitwise;
386
387 dest->flags = src->flags;
388 dest->paused = src->paused;
389
Ilya Dryomov04c7d782017-06-15 16:30:55 +0200390 dest->epoch = src->epoch;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +0200391 dest->last_force_resend = src->last_force_resend;
392
Ilya Dryomov922dab62016-05-26 01:15:02 +0200393 dest->osd = src->osd;
394}
395
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200396static void target_destroy(struct ceph_osd_request_target *t)
397{
398 ceph_oid_destroy(&t->base_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800399 ceph_oloc_destroy(&t->base_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200400 ceph_oid_destroy(&t->target_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800401 ceph_oloc_destroy(&t->target_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200402}
403
404/*
Sage Weilf24e9982009-10-06 11:31:10 -0700405 * requests
406 */
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200407static void request_release_checks(struct ceph_osd_request *req)
408{
409 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
Ilya Dryomov46092452016-04-28 16:07:27 +0200410 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200411 WARN_ON(!list_empty(&req->r_unsafe_item));
412 WARN_ON(req->r_osd);
413}
414
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400415static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700416{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400417 struct ceph_osd_request *req = container_of(kref,
418 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500419 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800420
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400421 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
422 req->r_request, req->r_reply);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200423 request_release_checks(req);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400424
Sage Weil415e49a2009-12-07 13:37:03 -0800425 if (req->r_request)
426 ceph_msg_put(req->r_request);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200427 if (req->r_reply)
Alex Elderab8cb342012-06-04 14:43:32 -0500428 ceph_msg_put(req->r_reply);
Alex Elder0fff87e2013-02-14 12:16:43 -0600429
Alex Elder54764922013-04-05 01:27:12 -0500430 for (which = 0; which < req->r_num_ops; which++)
431 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600432
Ilya Dryomova66dd382016-04-28 16:07:23 +0200433 target_destroy(&req->r_t);
Sage Weil415e49a2009-12-07 13:37:03 -0800434 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200435
Sage Weil415e49a2009-12-07 13:37:03 -0800436 if (req->r_mempool)
437 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100438 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500439 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100440 else
441 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700442}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400443
444void ceph_osdc_get_request(struct ceph_osd_request *req)
445{
446 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100447 kref_read(&req->r_kref));
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400448 kref_get(&req->r_kref);
449}
450EXPORT_SYMBOL(ceph_osdc_get_request);
451
452void ceph_osdc_put_request(struct ceph_osd_request *req)
453{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200454 if (req) {
455 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100456 kref_read(&req->r_kref));
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200457 kref_put(&req->r_kref, ceph_osdc_release_request);
458 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400459}
460EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700461
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200462static void request_init(struct ceph_osd_request *req)
463{
464 /* req only, each op is zeroed in _osd_req_op_init() */
465 memset(req, 0, sizeof(*req));
466
467 kref_init(&req->r_kref);
468 init_completion(&req->r_completion);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200469 RB_CLEAR_NODE(&req->r_node);
Ilya Dryomov46092452016-04-28 16:07:27 +0200470 RB_CLEAR_NODE(&req->r_mc_node);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200471 INIT_LIST_HEAD(&req->r_unsafe_item);
472
473 target_init(&req->r_t);
474}
475
Ilya Dryomov922dab62016-05-26 01:15:02 +0200476/*
477 * This is ugly, but it allows us to reuse linger registration and ping
478 * requests, keeping the structure of the code around send_linger{_ping}()
479 * reasonable. Setting up a min_nr=2 mempool for each linger request
480 * and dealing with copying ops (this blasts req only, watch op remains
481 * intact) isn't any better.
482 */
483static void request_reinit(struct ceph_osd_request *req)
484{
485 struct ceph_osd_client *osdc = req->r_osdc;
486 bool mempool = req->r_mempool;
487 unsigned int num_ops = req->r_num_ops;
488 u64 snapid = req->r_snapid;
489 struct ceph_snap_context *snapc = req->r_snapc;
490 bool linger = req->r_linger;
491 struct ceph_msg *request_msg = req->r_request;
492 struct ceph_msg *reply_msg = req->r_reply;
493
494 dout("%s req %p\n", __func__, req);
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100495 WARN_ON(kref_read(&req->r_kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200496 request_release_checks(req);
497
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100498 WARN_ON(kref_read(&request_msg->kref) != 1);
499 WARN_ON(kref_read(&reply_msg->kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200500 target_destroy(&req->r_t);
501
502 request_init(req);
503 req->r_osdc = osdc;
504 req->r_mempool = mempool;
505 req->r_num_ops = num_ops;
506 req->r_snapid = snapid;
507 req->r_snapc = snapc;
508 req->r_linger = linger;
509 req->r_request = request_msg;
510 req->r_reply = reply_msg;
511}
512
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700513struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700514 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800515 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700516 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600517 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700518{
519 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700520
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700521 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100522 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700523 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100524 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
525 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700526 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100527 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
528 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
529 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700530 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100531 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700532 return NULL;
533
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200534 request_init(req);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700535 req->r_osdc = osdc;
536 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500537 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200538 req->r_snapid = CEPH_NOSNAP;
539 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700540
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200541 dout("%s req %p\n", __func__, req);
542 return req;
543}
544EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100545
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +0200546static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
Yan, Zheng30c156d2016-02-14 11:24:31 +0800547{
548 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
549}
550
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200551int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
552{
553 struct ceph_osd_client *osdc = req->r_osdc;
554 struct ceph_msg *msg;
555 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700556
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200557 WARN_ON(ceph_oid_empty(&req->r_base_oid));
Yan, Zheng30c156d2016-02-14 11:24:31 +0800558 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200559
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200560 /* create request message */
Ilya Dryomov8cb441c2017-06-15 16:30:54 +0200561 msg_size = CEPH_ENCODING_START_BLK_LEN +
562 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
563 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
564 msg_size += CEPH_ENCODING_START_BLK_LEN +
565 sizeof(struct ceph_osd_reqid); /* reqid */
566 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
567 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
Yan, Zheng30c156d2016-02-14 11:24:31 +0800568 msg_size += CEPH_ENCODING_START_BLK_LEN +
569 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200570 msg_size += 4 + req->r_base_oid.name_len; /* oid */
571 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100572 msg_size += 8; /* snapid */
573 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200574 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +0200575 msg_size += 4 + 8; /* retry_attempt, features */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100576
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200577 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700578 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
579 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200580 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
581 if (!msg)
582 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700583
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700584 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700585 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700586
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200587 /* create reply message */
588 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200589 msg_size += req->r_base_oid.name_len;
590 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200591
592 if (req->r_mempool)
593 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
594 else
595 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
596 if (!msg)
597 return -ENOMEM;
598
599 req->r_reply = msg;
600
601 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700602}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200603EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700604
Alex Eldera8dd0a32013-03-13 20:50:00 -0500605static bool osd_req_opcode_valid(u16 opcode)
606{
607 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400608#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
609__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
610#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500611 default:
612 return false;
613 }
614}
615
Alex Elder33803f32013-03-13 20:50:00 -0500616/*
617 * This is an osd op init function for opcodes that have no data or
618 * other information associated with them. It also serves as a
619 * common init routine for all the other init functions, below.
620 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500621static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600622_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800623 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500624{
Alex Elderc99d2d42013-04-05 01:27:11 -0500625 struct ceph_osd_req_op *op;
626
627 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500628 BUG_ON(!osd_req_opcode_valid(opcode));
629
Alex Elderc99d2d42013-04-05 01:27:11 -0500630 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500631 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500632 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800633 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500634
635 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500636}
637
Alex Elder49719772013-02-11 12:33:24 -0600638void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800639 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600640{
Yan, Zheng144cba12015-04-27 11:09:54 +0800641 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600642}
643EXPORT_SYMBOL(osd_req_op_init);
644
Alex Elderc99d2d42013-04-05 01:27:11 -0500645void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
646 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500647 u64 offset, u64 length,
648 u64 truncate_size, u32 truncate_seq)
649{
Yan, Zheng144cba12015-04-27 11:09:54 +0800650 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
651 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500652 size_t payload_len = 0;
653
Li Wangad7a60d2013-08-15 11:51:44 +0800654 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200655 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
656 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500657
Alex Elder33803f32013-03-13 20:50:00 -0500658 op->extent.offset = offset;
659 op->extent.length = length;
660 op->extent.truncate_size = truncate_size;
661 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200662 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500663 payload_len += length;
664
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100665 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500666}
667EXPORT_SYMBOL(osd_req_op_extent_init);
668
Alex Elderc99d2d42013-04-05 01:27:11 -0500669void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
670 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500671{
Alex Elderc99d2d42013-04-05 01:27:11 -0500672 struct ceph_osd_req_op *op;
673 u64 previous;
674
675 BUG_ON(which >= osd_req->r_num_ops);
676 op = &osd_req->r_ops[which];
677 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500678
679 if (length == previous)
680 return; /* Nothing to do */
681 BUG_ON(length > previous);
682
683 op->extent.length = length;
Yan, Zhengd641df82017-01-19 11:21:29 +0800684 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
685 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500686}
687EXPORT_SYMBOL(osd_req_op_extent_update);
688
Yan, Zheng2c63f492016-01-07 17:32:54 +0800689void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
690 unsigned int which, u64 offset_inc)
691{
692 struct ceph_osd_req_op *op, *prev_op;
693
694 BUG_ON(which + 1 >= osd_req->r_num_ops);
695
696 prev_op = &osd_req->r_ops[which];
697 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
698 /* dup previous one */
699 op->indata_len = prev_op->indata_len;
700 op->outdata_len = prev_op->outdata_len;
701 op->extent = prev_op->extent;
702 /* adjust offset */
703 op->extent.offset += offset_inc;
704 op->extent.length -= offset_inc;
705
706 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
707 op->indata_len -= offset_inc;
708}
709EXPORT_SYMBOL(osd_req_op_extent_dup_last);
710
Alex Elderc99d2d42013-04-05 01:27:11 -0500711void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500712 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500713{
Yan, Zheng144cba12015-04-27 11:09:54 +0800714 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
715 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500716 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500717 size_t payload_len = 0;
718 size_t size;
719
720 BUG_ON(opcode != CEPH_OSD_OP_CALL);
721
Alex Elder5f562df2013-04-05 01:27:12 -0500722 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
723 BUG_ON(!pagelist);
724 ceph_pagelist_init(pagelist);
725
Alex Elder33803f32013-03-13 20:50:00 -0500726 op->cls.class_name = class;
727 size = strlen(class);
728 BUG_ON(size > (size_t) U8_MAX);
729 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500730 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500731 payload_len += size;
732
733 op->cls.method_name = method;
734 size = strlen(method);
735 BUG_ON(size > (size_t) U8_MAX);
736 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500737 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500738 payload_len += size;
739
Alex Eldera4ce40a2013-04-05 01:27:12 -0500740 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500741
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100742 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500743}
744EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500745
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800746int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
747 u16 opcode, const char *name, const void *value,
748 size_t size, u8 cmp_op, u8 cmp_mode)
749{
Yan, Zheng144cba12015-04-27 11:09:54 +0800750 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
751 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800752 struct ceph_pagelist *pagelist;
753 size_t payload_len;
754
755 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
756
757 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
758 if (!pagelist)
759 return -ENOMEM;
760
761 ceph_pagelist_init(pagelist);
762
763 payload_len = strlen(name);
764 op->xattr.name_len = payload_len;
765 ceph_pagelist_append(pagelist, name, payload_len);
766
767 op->xattr.value_len = size;
768 ceph_pagelist_append(pagelist, value, size);
769 payload_len += size;
770
771 op->xattr.cmp_op = cmp_op;
772 op->xattr.cmp_mode = cmp_mode;
773
774 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100775 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800776 return 0;
777}
778EXPORT_SYMBOL(osd_req_op_xattr_init);
779
Ilya Dryomov922dab62016-05-26 01:15:02 +0200780/*
781 * @watch_opcode: CEPH_OSD_WATCH_OP_*
782 */
783static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
784 u64 cookie, u8 watch_opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500785{
Ilya Dryomov922dab62016-05-26 01:15:02 +0200786 struct ceph_osd_req_op *op;
Alex Elder33803f32013-03-13 20:50:00 -0500787
Ilya Dryomov922dab62016-05-26 01:15:02 +0200788 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500789 op->watch.cookie = cookie;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200790 op->watch.op = watch_opcode;
791 op->watch.gen = 0;
Alex Elder33803f32013-03-13 20:50:00 -0500792}
Alex Elder33803f32013-03-13 20:50:00 -0500793
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200794void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
795 unsigned int which,
796 u64 expected_object_size,
797 u64 expected_write_size)
798{
799 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800800 CEPH_OSD_OP_SETALLOCHINT,
801 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200802
803 op->alloc_hint.expected_object_size = expected_object_size;
804 op->alloc_hint.expected_write_size = expected_write_size;
805
806 /*
807 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
808 * not worth a feature bit. Set FAILOK per-op flag to make
809 * sure older osds don't trip over an unsupported opcode.
810 */
811 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
812}
813EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
814
Alex Elder90af3602013-04-05 14:46:01 -0500815static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500816 struct ceph_osd_data *osd_data)
817{
818 u64 length = ceph_osd_data_length(osd_data);
819
820 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
821 BUG_ON(length > (u64) SIZE_MAX);
822 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500823 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500824 length, osd_data->alignment);
825 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
826 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500827 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500828#ifdef CONFIG_BLOCK
829 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500830 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500831#endif
832 } else {
833 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
834 }
835}
836
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200837static u32 osd_req_encode_op(struct ceph_osd_op *dst,
838 const struct ceph_osd_req_op *src)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700839{
Alex Eldera8dd0a32013-03-13 20:50:00 -0500840 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
841 pr_err("unrecognized osd opcode %d\n", src->op);
842
843 return 0;
844 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700845
Alex Elder065a68f2012-04-20 15:49:43 -0500846 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600847 case CEPH_OSD_OP_STAT:
848 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700849 case CEPH_OSD_OP_READ:
850 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200851 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800852 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800853 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600854 dst->extent.offset = cpu_to_le64(src->extent.offset);
855 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700856 dst->extent.truncate_size =
857 cpu_to_le64(src->extent.truncate_size);
858 dst->extent.truncate_seq =
859 cpu_to_le32(src->extent.truncate_seq);
860 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700861 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700862 dst->cls.class_len = src->cls.class_len;
863 dst->cls.method_len = src->cls.method_len;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200864 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700865 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700866 case CEPH_OSD_OP_STARTSYNC:
867 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700868 case CEPH_OSD_OP_WATCH:
869 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200870 dst->watch.ver = cpu_to_le64(0);
871 dst->watch.op = src->watch.op;
872 dst->watch.gen = cpu_to_le32(src->watch.gen);
873 break;
874 case CEPH_OSD_OP_NOTIFY_ACK:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700875 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200876 case CEPH_OSD_OP_NOTIFY:
877 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
878 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700879 case CEPH_OSD_OP_LIST_WATCHERS:
880 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200881 case CEPH_OSD_OP_SETALLOCHINT:
882 dst->alloc_hint.expected_object_size =
883 cpu_to_le64(src->alloc_hint.expected_object_size);
884 dst->alloc_hint.expected_write_size =
885 cpu_to_le64(src->alloc_hint.expected_write_size);
886 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800887 case CEPH_OSD_OP_SETXATTR:
888 case CEPH_OSD_OP_CMPXATTR:
889 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
890 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
891 dst->xattr.cmp_op = src->xattr.cmp_op;
892 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800893 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800894 case CEPH_OSD_OP_CREATE:
895 case CEPH_OSD_OP_DELETE:
896 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700897 default:
Alex Elder4c464592013-02-15 11:42:30 -0600898 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600899 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600900 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500901
902 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700903 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200904
Alex Eldera8dd0a32013-03-13 20:50:00 -0500905 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200906 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100907 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600908
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200909 return src->indata_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700910}
911
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700912/*
Sage Weilf24e9982009-10-06 11:31:10 -0700913 * build new request AND message, calculate layout, and adjust file
914 * extent as needed.
915 *
916 * if the file was recently truncated, we include information about its
917 * old and new size so that the object can be updated appropriately. (we
918 * avoid synchronously deleting truncated objects because it's slow.)
919 *
920 * if @do_sync, include a 'startsync' command so that the osd will flush
921 * data quickly.
922 */
923struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
924 struct ceph_file_layout *layout,
925 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800926 u64 off, u64 *plen,
927 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700928 int opcode, int flags,
929 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700930 u32 truncate_seq,
931 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600932 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700933{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700934 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500935 u64 objnum = 0;
936 u64 objoff = 0;
937 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700938 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700939
Li Wangad7a60d2013-08-15 11:51:44 +0800940 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800941 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
942 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700943
Alex Elderacead002013-03-14 14:09:05 -0500944 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600945 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200946 if (!req) {
947 r = -ENOMEM;
948 goto fail;
949 }
Alex Elder79528732013-04-03 21:32:51 -0500950
Sage Weilf24e9982009-10-06 11:31:10 -0700951 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500952 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200953 if (r)
954 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500955
Yan, Zheng864e9192014-11-13 10:47:25 +0800956 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800957 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800958 } else {
Yan, Zheng76271512016-02-03 21:24:49 +0800959 u32 object_size = layout->object_size;
Yan, Zheng864e9192014-11-13 10:47:25 +0800960 u32 object_base = off - objoff;
961 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
962 if (truncate_size <= object_base) {
963 truncate_size = 0;
964 } else {
965 truncate_size -= object_base;
966 if (truncate_size > object_size)
967 truncate_size = object_size;
968 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800969 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800970 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800971 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500972 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500973
Jeff Laytona1f40202017-04-04 08:39:37 -0400974 req->r_abort_on_full = true;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200975 req->r_flags = flags;
Yan, Zheng76271512016-02-03 21:24:49 +0800976 req->r_base_oloc.pool = layout->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +0800977 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200978 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600979
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200980 req->r_snapid = vino.snap;
981 if (flags & CEPH_OSD_FLAG_WRITE)
982 req->r_data_offset = off;
983
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200984 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
985 if (r)
986 goto fail;
987
Sage Weilf24e9982009-10-06 11:31:10 -0700988 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200989
990fail:
991 ceph_osdc_put_request(req);
992 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700993}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700994EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700995
996/*
997 * We keep osd requests in an rbtree, sorted by ->r_tid.
998 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200999DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02001000DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
Sage Weilf24e9982009-10-06 11:31:10 -07001001
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001002static bool osd_homeless(struct ceph_osd *osd)
1003{
1004 return osd->o_osd == CEPH_HOMELESS_OSD;
1005}
1006
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001007static bool osd_registered(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001008{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001009 verify_osdc_locked(osd->o_osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001010
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001011 return !RB_EMPTY_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001012}
1013
1014/*
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001015 * Assumes @osd is zero-initialized.
1016 */
1017static void osd_init(struct ceph_osd *osd)
1018{
Elena Reshetova02113a02017-03-17 14:10:28 +02001019 refcount_set(&osd->o_ref, 1);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001020 RB_CLEAR_NODE(&osd->o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001021 osd->o_requests = RB_ROOT;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001022 osd->o_linger_requests = RB_ROOT;
Ilya Dryomova02a9462017-06-19 12:18:05 +02001023 osd->o_backoff_mappings = RB_ROOT;
1024 osd->o_backoffs_by_id = RB_ROOT;
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001025 INIT_LIST_HEAD(&osd->o_osd_lru);
1026 INIT_LIST_HEAD(&osd->o_keepalive_item);
1027 osd->o_incarnation = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001028 mutex_init(&osd->lock);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001029}
1030
1031static void osd_cleanup(struct ceph_osd *osd)
1032{
1033 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001034 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001035 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomova02a9462017-06-19 12:18:05 +02001036 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1037 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001038 WARN_ON(!list_empty(&osd->o_osd_lru));
1039 WARN_ON(!list_empty(&osd->o_keepalive_item));
1040
1041 if (osd->o_auth.authorizer) {
1042 WARN_ON(osd_homeless(osd));
1043 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1044 }
1045}
1046
1047/*
Sage Weilf24e9982009-10-06 11:31:10 -07001048 * Track open sessions with osds.
1049 */
Alex Eldere10006f2012-05-26 23:26:43 -05001050static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001051{
1052 struct ceph_osd *osd;
1053
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001054 WARN_ON(onum == CEPH_HOMELESS_OSD);
1055
Ilya Dryomov7a28f592016-04-28 16:07:25 +02001056 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001057 osd_init(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001058 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001059 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -07001060
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001061 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001062
Sage Weilf24e9982009-10-06 11:31:10 -07001063 return osd;
1064}
1065
1066static struct ceph_osd *get_osd(struct ceph_osd *osd)
1067{
Elena Reshetova02113a02017-03-17 14:10:28 +02001068 if (refcount_inc_not_zero(&osd->o_ref)) {
1069 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1070 refcount_read(&osd->o_ref));
Sage Weilf24e9982009-10-06 11:31:10 -07001071 return osd;
1072 } else {
1073 dout("get_osd %p FAIL\n", osd);
1074 return NULL;
1075 }
1076}
1077
1078static void put_osd(struct ceph_osd *osd)
1079{
Elena Reshetova02113a02017-03-17 14:10:28 +02001080 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1081 refcount_read(&osd->o_ref) - 1);
1082 if (refcount_dec_and_test(&osd->o_ref)) {
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001083 osd_cleanup(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001084 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001085 }
Sage Weilf24e9982009-10-06 11:31:10 -07001086}
1087
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001088DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1089
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001090static void __move_osd_to_lru(struct ceph_osd *osd)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001091{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001092 struct ceph_osd_client *osdc = osd->o_osdc;
1093
1094 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001095 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001096
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001097 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001098 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001099 spin_unlock(&osdc->osd_lru_lock);
1100
Ilya Dryomova319bf52015-05-15 12:02:17 +03001101 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001102}
1103
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001104static void maybe_move_osd_to_lru(struct ceph_osd *osd)
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001105{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001106 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001107 RB_EMPTY_ROOT(&osd->o_linger_requests))
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001108 __move_osd_to_lru(osd);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001109}
1110
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001111static void __remove_osd_from_lru(struct ceph_osd *osd)
1112{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001113 struct ceph_osd_client *osdc = osd->o_osdc;
1114
1115 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1116
1117 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001118 if (!list_empty(&osd->o_osd_lru))
1119 list_del_init(&osd->o_osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001120 spin_unlock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001121}
1122
Sage Weilf24e9982009-10-06 11:31:10 -07001123/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001124 * Close the connection and assign any leftover requests to the
1125 * homeless session.
1126 */
1127static void close_osd(struct ceph_osd *osd)
1128{
1129 struct ceph_osd_client *osdc = osd->o_osdc;
1130 struct rb_node *n;
1131
1132 verify_osdc_wrlocked(osdc);
1133 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1134
1135 ceph_con_close(&osd->o_con);
1136
1137 for (n = rb_first(&osd->o_requests); n; ) {
1138 struct ceph_osd_request *req =
1139 rb_entry(n, struct ceph_osd_request, r_node);
1140
1141 n = rb_next(n); /* unlink_request() */
1142
1143 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1144 unlink_request(osd, req);
1145 link_request(&osdc->homeless_osd, req);
1146 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02001147 for (n = rb_first(&osd->o_linger_requests); n; ) {
1148 struct ceph_osd_linger_request *lreq =
1149 rb_entry(n, struct ceph_osd_linger_request, node);
1150
1151 n = rb_next(n); /* unlink_linger() */
1152
1153 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1154 lreq->linger_id);
1155 unlink_linger(osd, lreq);
1156 link_linger(&osdc->homeless_osd, lreq);
1157 }
Ilya Dryomova02a9462017-06-19 12:18:05 +02001158 clear_backoffs(osd);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001159
1160 __remove_osd_from_lru(osd);
1161 erase_osd(&osdc->osds, osd);
1162 put_osd(osd);
1163}
1164
1165/*
Sage Weilf24e9982009-10-06 11:31:10 -07001166 * reset osd connect
1167 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001168static int reopen_osd(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001169{
Alex Elderc3acb182012-12-07 09:57:58 -06001170 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001171
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001172 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1173
1174 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001175 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001176 close_osd(osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001177 return -ENODEV;
1178 }
1179
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001180 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
Alex Elderc3acb182012-12-07 09:57:58 -06001181 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1182 !ceph_con_opened(&osd->o_con)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001183 struct rb_node *n;
Alex Elderc3acb182012-12-07 09:57:58 -06001184
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001185 dout("osd addr hasn't changed and connection never opened, "
1186 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001187 /* touch each r_stamp for handle_timeout()'s benfit */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001188 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1189 struct ceph_osd_request *req =
1190 rb_entry(n, struct ceph_osd_request, r_node);
Sage Weil87b315a2010-03-22 14:51:18 -07001191 req->r_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001192 }
Alex Elderc3acb182012-12-07 09:57:58 -06001193
1194 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001195 }
Alex Elderc3acb182012-12-07 09:57:58 -06001196
1197 ceph_con_close(&osd->o_con);
1198 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1199 osd->o_incarnation++;
1200
1201 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001202}
1203
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001204static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1205 bool wrlocked)
Sage Weilf24e9982009-10-06 11:31:10 -07001206{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001207 struct ceph_osd *osd;
1208
1209 if (wrlocked)
1210 verify_osdc_wrlocked(osdc);
1211 else
1212 verify_osdc_locked(osdc);
1213
1214 if (o != CEPH_HOMELESS_OSD)
1215 osd = lookup_osd(&osdc->osds, o);
1216 else
1217 osd = &osdc->homeless_osd;
1218 if (!osd) {
1219 if (!wrlocked)
1220 return ERR_PTR(-EAGAIN);
1221
1222 osd = create_osd(osdc, o);
1223 insert_osd(&osdc->osds, osd);
1224 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1225 &osdc->osdmap->osd_addr[osd->o_osd]);
1226 }
1227
1228 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1229 return osd;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001230}
1231
Sage Weilf24e9982009-10-06 11:31:10 -07001232/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001233 * Create request <-> OSD session relation.
1234 *
1235 * @req has to be assigned a tid, @osd may be homeless.
Sage Weilf24e9982009-10-06 11:31:10 -07001236 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001237static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001238{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001239 verify_osd_locked(osd);
1240 WARN_ON(!req->r_tid || req->r_osd);
1241 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1242 req, req->r_tid);
Sage Weil35f9f8a2012-05-16 15:16:38 -05001243
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001244 if (!osd_homeless(osd))
1245 __remove_osd_from_lru(osd);
1246 else
1247 atomic_inc(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001248
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001249 get_osd(osd);
1250 insert_request(&osd->o_requests, req);
1251 req->r_osd = osd;
Sage Weilf24e9982009-10-06 11:31:10 -07001252}
1253
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001254static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001255{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001256 verify_osd_locked(osd);
1257 WARN_ON(req->r_osd != osd);
1258 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1259 req, req->r_tid);
1260
1261 req->r_osd = NULL;
1262 erase_request(&osd->o_requests, req);
1263 put_osd(osd);
1264
1265 if (!osd_homeless(osd))
1266 maybe_move_osd_to_lru(osd);
1267 else
1268 atomic_dec(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001269}
1270
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001271static bool __pool_full(struct ceph_pg_pool_info *pi)
1272{
1273 return pi->flags & CEPH_POOL_FLAG_FULL;
1274}
1275
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001276static bool have_pool_full(struct ceph_osd_client *osdc)
1277{
1278 struct rb_node *n;
1279
1280 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1281 struct ceph_pg_pool_info *pi =
1282 rb_entry(n, struct ceph_pg_pool_info, node);
1283
1284 if (__pool_full(pi))
1285 return true;
1286 }
1287
1288 return false;
1289}
1290
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001291static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1292{
1293 struct ceph_pg_pool_info *pi;
1294
1295 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1296 if (!pi)
1297 return false;
1298
1299 return __pool_full(pi);
1300}
1301
Sage Weilf24e9982009-10-06 11:31:10 -07001302/*
Josh Durgind29adb32013-12-02 19:11:48 -08001303 * Returns whether a request should be blocked from being sent
1304 * based on the current osdmap and osd_client settings.
Josh Durgind29adb32013-12-02 19:11:48 -08001305 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001306static bool target_should_be_paused(struct ceph_osd_client *osdc,
1307 const struct ceph_osd_request_target *t,
1308 struct ceph_pg_pool_info *pi)
1309{
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001310 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1311 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1312 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001313 __pool_full(pi);
1314
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001315 WARN_ON(pi->id != t->target_oloc.pool);
Jeff Layton58eb7932017-04-18 09:21:16 -04001316 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1317 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1318 (osdc->osdmap->epoch < osdc->epoch_barrier);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001319}
1320
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001321enum calc_target_result {
1322 CALC_TARGET_NO_ACTION = 0,
1323 CALC_TARGET_NEED_RESEND,
1324 CALC_TARGET_POOL_DNE,
1325};
1326
1327static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1328 struct ceph_osd_request_target *t,
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001329 struct ceph_connection *con,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001330 bool any_change)
1331{
1332 struct ceph_pg_pool_info *pi;
1333 struct ceph_pg pgid, last_pgid;
1334 struct ceph_osds up, acting;
1335 bool force_resend = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001336 bool unpaused = false;
1337 bool legacy_change;
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001338 bool split = false;
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001339 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001340 enum calc_target_result ct_res;
1341 int ret;
1342
Ilya Dryomov04c7d782017-06-15 16:30:55 +02001343 t->epoch = osdc->osdmap->epoch;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001344 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1345 if (!pi) {
1346 t->osd = CEPH_HOMELESS_OSD;
1347 ct_res = CALC_TARGET_POOL_DNE;
1348 goto out;
1349 }
1350
1351 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001352 if (t->last_force_resend < pi->last_force_request_resend) {
1353 t->last_force_resend = pi->last_force_request_resend;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001354 force_resend = true;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001355 } else if (t->last_force_resend == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001356 force_resend = true;
1357 }
1358 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001359
Ilya Dryomovdb098ec2017-06-15 16:30:55 +02001360 /* apply tiering */
1361 ceph_oid_copy(&t->target_oid, &t->base_oid);
1362 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1363 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001364 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1365 t->target_oloc.pool = pi->read_tier;
1366 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1367 t->target_oloc.pool = pi->write_tier;
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001368
1369 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1370 if (!pi) {
1371 t->osd = CEPH_HOMELESS_OSD;
1372 ct_res = CALC_TARGET_POOL_DNE;
1373 goto out;
1374 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001375 }
1376
Ilya Dryomovdf281522017-06-15 16:30:56 +02001377 ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1378 &pgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001379 if (ret) {
1380 WARN_ON(ret != -ENOENT);
1381 t->osd = CEPH_HOMELESS_OSD;
1382 ct_res = CALC_TARGET_POOL_DNE;
1383 goto out;
1384 }
1385 last_pgid.pool = pgid.pool;
1386 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1387
Ilya Dryomovdf281522017-06-15 16:30:56 +02001388 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001389 if (any_change &&
1390 ceph_is_new_interval(&t->acting,
1391 &acting,
1392 &t->up,
1393 &up,
1394 t->size,
1395 pi->size,
1396 t->min_size,
1397 pi->min_size,
1398 t->pg_num,
1399 pi->pg_num,
1400 t->sort_bitwise,
1401 sort_bitwise,
1402 &last_pgid))
1403 force_resend = true;
1404
1405 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1406 t->paused = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001407 unpaused = true;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001408 }
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001409 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1410 ceph_osds_changed(&t->acting, &acting, any_change);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001411 if (t->pg_num)
1412 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001413
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001414 if (legacy_change || force_resend || split) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001415 t->pgid = pgid; /* struct */
Ilya Dryomovdf281522017-06-15 16:30:56 +02001416 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001417 ceph_osds_copy(&t->acting, &acting);
1418 ceph_osds_copy(&t->up, &up);
1419 t->size = pi->size;
1420 t->min_size = pi->min_size;
1421 t->pg_num = pi->pg_num;
1422 t->pg_num_mask = pi->pg_num_mask;
1423 t->sort_bitwise = sort_bitwise;
1424
1425 t->osd = acting.primary;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001426 }
1427
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001428 if (unpaused || legacy_change || force_resend ||
1429 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1430 RESEND_ON_SPLIT)))
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001431 ct_res = CALC_TARGET_NEED_RESEND;
1432 else
1433 ct_res = CALC_TARGET_NO_ACTION;
1434
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001435out:
1436 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1437 return ct_res;
1438}
1439
Ilya Dryomova02a9462017-06-19 12:18:05 +02001440static struct ceph_spg_mapping *alloc_spg_mapping(void)
1441{
1442 struct ceph_spg_mapping *spg;
1443
1444 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1445 if (!spg)
1446 return NULL;
1447
1448 RB_CLEAR_NODE(&spg->node);
1449 spg->backoffs = RB_ROOT;
1450 return spg;
1451}
1452
1453static void free_spg_mapping(struct ceph_spg_mapping *spg)
1454{
1455 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1456 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1457
1458 kfree(spg);
1459}
1460
1461/*
1462 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1463 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1464 * defined only within a specific spgid; it does not pass anything to
1465 * children on split, or to another primary.
1466 */
1467DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1468 RB_BYPTR, const struct ceph_spg *, node)
1469
1470static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1471{
1472 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1473}
1474
1475static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1476 void **pkey, size_t *pkey_len)
1477{
1478 if (hoid->key_len) {
1479 *pkey = hoid->key;
1480 *pkey_len = hoid->key_len;
1481 } else {
1482 *pkey = hoid->oid;
1483 *pkey_len = hoid->oid_len;
1484 }
1485}
1486
1487static int compare_names(const void *name1, size_t name1_len,
1488 const void *name2, size_t name2_len)
1489{
1490 int ret;
1491
1492 ret = memcmp(name1, name2, min(name1_len, name2_len));
1493 if (!ret) {
1494 if (name1_len < name2_len)
1495 ret = -1;
1496 else if (name1_len > name2_len)
1497 ret = 1;
1498 }
1499 return ret;
1500}
1501
1502static int hoid_compare(const struct ceph_hobject_id *lhs,
1503 const struct ceph_hobject_id *rhs)
1504{
1505 void *effective_key1, *effective_key2;
1506 size_t effective_key1_len, effective_key2_len;
1507 int ret;
1508
1509 if (lhs->is_max < rhs->is_max)
1510 return -1;
1511 if (lhs->is_max > rhs->is_max)
1512 return 1;
1513
1514 if (lhs->pool < rhs->pool)
1515 return -1;
1516 if (lhs->pool > rhs->pool)
1517 return 1;
1518
1519 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1520 return -1;
1521 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1522 return 1;
1523
1524 ret = compare_names(lhs->nspace, lhs->nspace_len,
1525 rhs->nspace, rhs->nspace_len);
1526 if (ret)
1527 return ret;
1528
1529 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1530 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1531 ret = compare_names(effective_key1, effective_key1_len,
1532 effective_key2, effective_key2_len);
1533 if (ret)
1534 return ret;
1535
1536 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1537 if (ret)
1538 return ret;
1539
1540 if (lhs->snapid < rhs->snapid)
1541 return -1;
1542 if (lhs->snapid > rhs->snapid)
1543 return 1;
1544
1545 return 0;
1546}
1547
1548/*
1549 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1550 * compat stuff here.
1551 *
1552 * Assumes @hoid is zero-initialized.
1553 */
1554static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1555{
1556 u8 struct_v;
1557 u32 struct_len;
1558 int ret;
1559
1560 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1561 &struct_len);
1562 if (ret)
1563 return ret;
1564
1565 if (struct_v < 4) {
1566 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1567 goto e_inval;
1568 }
1569
1570 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1571 GFP_NOIO);
1572 if (IS_ERR(hoid->key)) {
1573 ret = PTR_ERR(hoid->key);
1574 hoid->key = NULL;
1575 return ret;
1576 }
1577
1578 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1579 GFP_NOIO);
1580 if (IS_ERR(hoid->oid)) {
1581 ret = PTR_ERR(hoid->oid);
1582 hoid->oid = NULL;
1583 return ret;
1584 }
1585
1586 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1587 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1588 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1589
1590 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1591 GFP_NOIO);
1592 if (IS_ERR(hoid->nspace)) {
1593 ret = PTR_ERR(hoid->nspace);
1594 hoid->nspace = NULL;
1595 return ret;
1596 }
1597
1598 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1599
1600 ceph_hoid_build_hash_cache(hoid);
1601 return 0;
1602
1603e_inval:
1604 return -EINVAL;
1605}
1606
1607static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1608{
1609 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1610 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1611}
1612
1613static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1614{
1615 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1616 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1617 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1618 ceph_encode_64(p, hoid->snapid);
1619 ceph_encode_32(p, hoid->hash);
1620 ceph_encode_8(p, hoid->is_max);
1621 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1622 ceph_encode_64(p, hoid->pool);
1623}
1624
1625static void free_hoid(struct ceph_hobject_id *hoid)
1626{
1627 if (hoid) {
1628 kfree(hoid->key);
1629 kfree(hoid->oid);
1630 kfree(hoid->nspace);
1631 kfree(hoid);
1632 }
1633}
1634
1635static struct ceph_osd_backoff *alloc_backoff(void)
1636{
1637 struct ceph_osd_backoff *backoff;
1638
1639 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1640 if (!backoff)
1641 return NULL;
1642
1643 RB_CLEAR_NODE(&backoff->spg_node);
1644 RB_CLEAR_NODE(&backoff->id_node);
1645 return backoff;
1646}
1647
1648static void free_backoff(struct ceph_osd_backoff *backoff)
1649{
1650 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1651 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1652
1653 free_hoid(backoff->begin);
1654 free_hoid(backoff->end);
1655 kfree(backoff);
1656}
1657
1658/*
1659 * Within a specific spgid, backoffs are managed by ->begin hoid.
1660 */
1661DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1662 RB_BYVAL, spg_node);
1663
1664static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1665 const struct ceph_hobject_id *hoid)
1666{
1667 struct rb_node *n = root->rb_node;
1668
1669 while (n) {
1670 struct ceph_osd_backoff *cur =
1671 rb_entry(n, struct ceph_osd_backoff, spg_node);
1672 int cmp;
1673
1674 cmp = hoid_compare(hoid, cur->begin);
1675 if (cmp < 0) {
1676 n = n->rb_left;
1677 } else if (cmp > 0) {
1678 if (hoid_compare(hoid, cur->end) < 0)
1679 return cur;
1680
1681 n = n->rb_right;
1682 } else {
1683 return cur;
1684 }
1685 }
1686
1687 return NULL;
1688}
1689
1690/*
1691 * Each backoff has a unique id within its OSD session.
1692 */
1693DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1694
1695static void clear_backoffs(struct ceph_osd *osd)
1696{
1697 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1698 struct ceph_spg_mapping *spg =
1699 rb_entry(rb_first(&osd->o_backoff_mappings),
1700 struct ceph_spg_mapping, node);
1701
1702 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1703 struct ceph_osd_backoff *backoff =
1704 rb_entry(rb_first(&spg->backoffs),
1705 struct ceph_osd_backoff, spg_node);
1706
1707 erase_backoff(&spg->backoffs, backoff);
1708 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1709 free_backoff(backoff);
1710 }
1711 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1712 free_spg_mapping(spg);
1713 }
1714}
1715
1716/*
1717 * Set up a temporary, non-owning view into @t.
1718 */
1719static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1720 const struct ceph_osd_request_target *t)
1721{
1722 hoid->key = NULL;
1723 hoid->key_len = 0;
1724 hoid->oid = t->target_oid.name;
1725 hoid->oid_len = t->target_oid.name_len;
1726 hoid->snapid = CEPH_NOSNAP;
1727 hoid->hash = t->pgid.seed;
1728 hoid->is_max = false;
1729 if (t->target_oloc.pool_ns) {
1730 hoid->nspace = t->target_oloc.pool_ns->str;
1731 hoid->nspace_len = t->target_oloc.pool_ns->len;
1732 } else {
1733 hoid->nspace = NULL;
1734 hoid->nspace_len = 0;
1735 }
1736 hoid->pool = t->target_oloc.pool;
1737 ceph_hoid_build_hash_cache(hoid);
1738}
1739
1740static bool should_plug_request(struct ceph_osd_request *req)
1741{
1742 struct ceph_osd *osd = req->r_osd;
1743 struct ceph_spg_mapping *spg;
1744 struct ceph_osd_backoff *backoff;
1745 struct ceph_hobject_id hoid;
1746
1747 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1748 if (!spg)
1749 return false;
1750
1751 hoid_fill_from_target(&hoid, &req->r_t);
1752 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1753 if (!backoff)
1754 return false;
1755
1756 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1757 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1758 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1759 return true;
1760}
1761
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001762static void setup_request_data(struct ceph_osd_request *req,
1763 struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001764{
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001765 u32 data_len = 0;
1766 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001767
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001768 if (!list_empty(&msg->data))
1769 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001770
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001771 WARN_ON(msg->data_length);
1772 for (i = 0; i < req->r_num_ops; i++) {
1773 struct ceph_osd_req_op *op = &req->r_ops[i];
1774
1775 switch (op->op) {
1776 /* request */
1777 case CEPH_OSD_OP_WRITE:
1778 case CEPH_OSD_OP_WRITEFULL:
1779 WARN_ON(op->indata_len != op->extent.length);
1780 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1781 break;
1782 case CEPH_OSD_OP_SETXATTR:
1783 case CEPH_OSD_OP_CMPXATTR:
1784 WARN_ON(op->indata_len != op->xattr.name_len +
1785 op->xattr.value_len);
1786 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1787 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001788 case CEPH_OSD_OP_NOTIFY_ACK:
1789 ceph_osdc_msg_data_add(msg,
1790 &op->notify_ack.request_data);
1791 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001792
1793 /* reply */
1794 case CEPH_OSD_OP_STAT:
1795 ceph_osdc_msg_data_add(req->r_reply,
1796 &op->raw_data_in);
1797 break;
1798 case CEPH_OSD_OP_READ:
1799 ceph_osdc_msg_data_add(req->r_reply,
1800 &op->extent.osd_data);
1801 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001802 case CEPH_OSD_OP_LIST_WATCHERS:
1803 ceph_osdc_msg_data_add(req->r_reply,
1804 &op->list_watchers.response_data);
1805 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001806
1807 /* both */
1808 case CEPH_OSD_OP_CALL:
1809 WARN_ON(op->indata_len != op->cls.class_len +
1810 op->cls.method_len +
1811 op->cls.indata_len);
1812 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1813 /* optional, can be NONE */
1814 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1815 /* optional, can be NONE */
1816 ceph_osdc_msg_data_add(req->r_reply,
1817 &op->cls.response_data);
1818 break;
Ilya Dryomov19079202016-04-28 16:07:27 +02001819 case CEPH_OSD_OP_NOTIFY:
1820 ceph_osdc_msg_data_add(msg,
1821 &op->notify.request_data);
1822 ceph_osdc_msg_data_add(req->r_reply,
1823 &op->notify.response_data);
1824 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001825 }
1826
1827 data_len += op->indata_len;
1828 }
1829
1830 WARN_ON(data_len != msg->data_length);
1831}
1832
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001833static void encode_pgid(void **p, const struct ceph_pg *pgid)
1834{
1835 ceph_encode_8(p, 1);
1836 ceph_encode_64(p, pgid->pool);
1837 ceph_encode_32(p, pgid->seed);
1838 ceph_encode_32(p, -1); /* preferred */
1839}
1840
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001841static void encode_spgid(void **p, const struct ceph_spg *spgid)
1842{
1843 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1844 encode_pgid(p, &spgid->pgid);
1845 ceph_encode_8(p, spgid->shard);
1846}
1847
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001848static void encode_oloc(void **p, void *end,
1849 const struct ceph_object_locator *oloc)
1850{
1851 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1852 ceph_encode_64(p, oloc->pool);
1853 ceph_encode_32(p, -1); /* preferred */
1854 ceph_encode_32(p, 0); /* key len */
1855 if (oloc->pool_ns)
1856 ceph_encode_string(p, end, oloc->pool_ns->str,
1857 oloc->pool_ns->len);
1858 else
1859 ceph_encode_32(p, 0);
1860}
1861
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001862static void encode_request_partial(struct ceph_osd_request *req,
1863 struct ceph_msg *msg)
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001864{
1865 void *p = msg->front.iov_base;
1866 void *const end = p + msg->front_alloc_len;
1867 u32 data_len = 0;
1868 int i;
1869
1870 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1871 /* snapshots aren't writeable */
1872 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1873 } else {
1874 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1875 req->r_data_offset || req->r_snapc);
1876 }
1877
1878 setup_request_data(req, msg);
1879
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001880 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1881 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001882 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1883 ceph_encode_32(&p, req->r_flags);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001884
1885 /* reqid */
1886 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1887 memset(p, 0, sizeof(struct ceph_osd_reqid));
1888 p += sizeof(struct ceph_osd_reqid);
1889
1890 /* trace */
1891 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1892 p += sizeof(struct ceph_blkin_trace_info);
1893
1894 ceph_encode_32(&p, 0); /* client_inc, always 0 */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001895 ceph_encode_timespec(p, &req->r_mtime);
1896 p += sizeof(struct ceph_timespec);
Jeff Laytonaa26d662017-04-04 08:39:36 -04001897
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001898 encode_oloc(&p, end, &req->r_t.target_oloc);
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001899 ceph_encode_string(&p, end, req->r_t.target_oid.name,
1900 req->r_t.target_oid.name_len);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001901
1902 /* ops, can imply data */
1903 ceph_encode_16(&p, req->r_num_ops);
1904 for (i = 0; i < req->r_num_ops; i++) {
1905 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1906 p += sizeof(struct ceph_osd_op);
1907 }
1908
1909 ceph_encode_64(&p, req->r_snapid); /* snapid */
1910 if (req->r_snapc) {
1911 ceph_encode_64(&p, req->r_snapc->seq);
1912 ceph_encode_32(&p, req->r_snapc->num_snaps);
1913 for (i = 0; i < req->r_snapc->num_snaps; i++)
1914 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1915 } else {
1916 ceph_encode_64(&p, 0); /* snap_seq */
1917 ceph_encode_32(&p, 0); /* snaps len */
1918 }
1919
1920 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001921 BUG_ON(p != end - 8); /* space for features */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001922
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001923 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
1924 /* front_len is finalized in encode_request_finish() */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001925 msg->hdr.data_len = cpu_to_le32(data_len);
1926 /*
1927 * The header "data_off" is a hint to the receiver allowing it
1928 * to align received data into its buffers such that there's no
1929 * need to re-copy it before writing it to disk (direct I/O).
1930 */
1931 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1932
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001933 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
1934 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1935}
1936
1937static void encode_request_finish(struct ceph_msg *msg)
1938{
1939 void *p = msg->front.iov_base;
1940 void *const end = p + msg->front_alloc_len;
1941
1942 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
1943 /* luminous OSD -- encode features and be done */
1944 p = end - 8;
1945 ceph_encode_64(&p, msg->con->peer_features);
1946 } else {
1947 struct {
1948 char spgid[CEPH_ENCODING_START_BLK_LEN +
1949 CEPH_PGID_ENCODING_LEN + 1];
1950 __le32 hash;
1951 __le32 epoch;
1952 __le32 flags;
1953 char reqid[CEPH_ENCODING_START_BLK_LEN +
1954 sizeof(struct ceph_osd_reqid)];
1955 char trace[sizeof(struct ceph_blkin_trace_info)];
1956 __le32 client_inc;
1957 struct ceph_timespec mtime;
1958 } __packed head;
1959 struct ceph_pg pgid;
1960 void *oloc, *oid, *tail;
1961 int oloc_len, oid_len, tail_len;
1962 int len;
1963
1964 /*
1965 * Pre-luminous OSD -- reencode v8 into v4 using @head
1966 * as a temporary buffer. Encode the raw PG; the rest
1967 * is just a matter of moving oloc, oid and tail blobs
1968 * around.
1969 */
1970 memcpy(&head, p, sizeof(head));
1971 p += sizeof(head);
1972
1973 oloc = p;
1974 p += CEPH_ENCODING_START_BLK_LEN;
1975 pgid.pool = ceph_decode_64(&p);
1976 p += 4 + 4; /* preferred, key len */
1977 len = ceph_decode_32(&p);
1978 p += len; /* nspace */
1979 oloc_len = p - oloc;
1980
1981 oid = p;
1982 len = ceph_decode_32(&p);
1983 p += len;
1984 oid_len = p - oid;
1985
1986 tail = p;
1987 tail_len = (end - p) - 8;
1988
1989 p = msg->front.iov_base;
1990 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
1991 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
1992 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
1993 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
1994
1995 /* reassert_version */
1996 memset(p, 0, sizeof(struct ceph_eversion));
1997 p += sizeof(struct ceph_eversion);
1998
1999 BUG_ON(p >= oloc);
2000 memmove(p, oloc, oloc_len);
2001 p += oloc_len;
2002
2003 pgid.seed = le32_to_cpu(head.hash);
2004 encode_pgid(&p, &pgid); /* raw pg */
2005
2006 BUG_ON(p >= oid);
2007 memmove(p, oid, oid_len);
2008 p += oid_len;
2009
2010 /* tail -- ops, snapid, snapc, retry_attempt */
2011 BUG_ON(p >= tail);
2012 memmove(p, tail, tail_len);
2013 p += tail_len;
2014
2015 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2016 }
2017
2018 BUG_ON(p > end);
2019 msg->front.iov_len = p - msg->front.iov_base;
2020 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2021
2022 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2023 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2024 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2025 le16_to_cpu(msg->hdr.version));
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002026}
2027
2028/*
2029 * @req has to be assigned a tid and registered.
2030 */
2031static void send_request(struct ceph_osd_request *req)
2032{
2033 struct ceph_osd *osd = req->r_osd;
2034
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002035 verify_osd_locked(osd);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002036 WARN_ON(osd->o_osd != req->r_t.osd);
2037
Ilya Dryomova02a9462017-06-19 12:18:05 +02002038 /* backoff? */
2039 if (should_plug_request(req))
2040 return;
2041
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002042 /*
2043 * We may have a previously queued request message hanging
2044 * around. Cancel it to avoid corrupting the msgr.
2045 */
2046 if (req->r_sent)
2047 ceph_msg_revoke(req->r_request);
2048
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002049 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2050 if (req->r_attempts)
2051 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2052 else
2053 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2054
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02002055 encode_request_partial(req, req->r_request);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002056
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002057 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 +02002058 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002059 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002060 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2061 req->r_attempts);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002062
2063 req->r_t.paused = false;
Sage Weil3dd72fc2010-03-22 14:42:30 -07002064 req->r_stamp = jiffies;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002065 req->r_attempts++;
Sage Weilf24e9982009-10-06 11:31:10 -07002066
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02002067 req->r_sent = osd->o_incarnation;
2068 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2069 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
Sage Weilf24e9982009-10-06 11:31:10 -07002070}
2071
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002072static void maybe_request_map(struct ceph_osd_client *osdc)
2073{
2074 bool continuous = false;
2075
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002076 verify_osdc_locked(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002077 WARN_ON(!osdc->osdmap->epoch);
2078
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002079 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2080 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2081 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002082 dout("%s osdc %p continuous\n", __func__, osdc);
2083 continuous = true;
2084 } else {
2085 dout("%s osdc %p onetime\n", __func__, osdc);
2086 }
2087
2088 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2089 osdc->osdmap->epoch + 1, continuous))
2090 ceph_monc_renew_subs(&osdc->client->monc);
2091}
2092
Jeff Laytona1f40202017-04-04 08:39:37 -04002093static void complete_request(struct ceph_osd_request *req, int err);
Ilya Dryomov46092452016-04-28 16:07:27 +02002094static void send_map_check(struct ceph_osd_request *req);
2095
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002096static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002097{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002098 struct ceph_osd_client *osdc = req->r_osdc;
2099 struct ceph_osd *osd;
Ilya Dryomov46092452016-04-28 16:07:27 +02002100 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002101 bool need_send = false;
2102 bool promoted = false;
Jeff Laytona1f40202017-04-04 08:39:37 -04002103 bool need_abort = false;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002104
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002105 WARN_ON(req->r_tid);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002106 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2107
2108again:
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002109 ct_res = calc_target(osdc, &req->r_t, NULL, false);
Ilya Dryomov46092452016-04-28 16:07:27 +02002110 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2111 goto promote;
2112
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002113 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2114 if (IS_ERR(osd)) {
2115 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2116 goto promote;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002117 }
2118
Jeff Layton58eb7932017-04-18 09:21:16 -04002119 if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2120 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2121 osdc->epoch_barrier);
2122 req->r_t.paused = true;
2123 maybe_request_map(osdc);
2124 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2125 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002126 dout("req %p pausewr\n", req);
2127 req->r_t.paused = true;
2128 maybe_request_map(osdc);
2129 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002130 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002131 dout("req %p pauserd\n", req);
2132 req->r_t.paused = true;
2133 maybe_request_map(osdc);
2134 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2135 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2136 CEPH_OSD_FLAG_FULL_FORCE)) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002137 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002138 pool_full(osdc, req->r_t.base_oloc.pool))) {
2139 dout("req %p full/pool_full\n", req);
2140 pr_warn_ratelimited("FULL or reached pool quota\n");
2141 req->r_t.paused = true;
2142 maybe_request_map(osdc);
Jeff Laytona1f40202017-04-04 08:39:37 -04002143 if (req->r_abort_on_full)
2144 need_abort = true;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002145 } else if (!osd_homeless(osd)) {
2146 need_send = true;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002147 } else {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002148 maybe_request_map(osdc);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002149 }
2150
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002151 mutex_lock(&osd->lock);
2152 /*
2153 * Assign the tid atomically with send_request() to protect
2154 * multiple writes to the same object from racing with each
2155 * other, resulting in out of order ops on the OSDs.
2156 */
2157 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2158 link_request(osd, req);
2159 if (need_send)
2160 send_request(req);
Jeff Laytona1f40202017-04-04 08:39:37 -04002161 else if (need_abort)
2162 complete_request(req, -ENOSPC);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002163 mutex_unlock(&osd->lock);
2164
Ilya Dryomov46092452016-04-28 16:07:27 +02002165 if (ct_res == CALC_TARGET_POOL_DNE)
2166 send_map_check(req);
2167
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002168 if (promoted)
2169 downgrade_write(&osdc->lock);
2170 return;
2171
2172promote:
2173 up_read(&osdc->lock);
2174 down_write(&osdc->lock);
2175 wrlocked = true;
2176 promoted = true;
2177 goto again;
2178}
2179
2180static void account_request(struct ceph_osd_request *req)
2181{
Ilya Dryomov54ea0042017-02-11 18:48:41 +01002182 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002183 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002184
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002185 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002186 atomic_inc(&req->r_osdc->num_requests);
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002187
2188 req->r_start_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002189}
2190
2191static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2192{
2193 ceph_osdc_get_request(req);
2194 account_request(req);
2195 __submit_request(req, wrlocked);
2196}
2197
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002198static void finish_request(struct ceph_osd_request *req)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002199{
2200 struct ceph_osd_client *osdc = req->r_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002201
Ilya Dryomov46092452016-04-28 16:07:27 +02002202 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
Ilya Dryomov04c7d782017-06-15 16:30:55 +02002203 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2204
2205 if (req->r_osd)
2206 unlink_request(req->r_osd, req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002207 atomic_dec(&osdc->num_requests);
2208
2209 /*
2210 * If an OSD has failed or returned and a request has been sent
2211 * twice, it's possible to get a reply and end up here while the
2212 * request message is queued for delivery. We will ignore the
2213 * reply, so not a big deal, but better to try and catch it.
2214 */
2215 ceph_msg_revoke(req->r_request);
2216 ceph_msg_revoke_incoming(req->r_reply);
2217}
2218
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002219static void __complete_request(struct ceph_osd_request *req)
2220{
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002221 if (req->r_callback) {
2222 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2223 req->r_tid, req->r_callback, req->r_result);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002224 req->r_callback(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002225 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002226}
2227
Ilya Dryomov46092452016-04-28 16:07:27 +02002228/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002229 * This is open-coded in handle_reply().
Ilya Dryomov46092452016-04-28 16:07:27 +02002230 */
2231static void complete_request(struct ceph_osd_request *req, int err)
2232{
2233 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2234
2235 req->r_result = err;
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002236 finish_request(req);
Ilya Dryomov46092452016-04-28 16:07:27 +02002237 __complete_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002238 complete_all(&req->r_completion);
Ilya Dryomov46092452016-04-28 16:07:27 +02002239 ceph_osdc_put_request(req);
2240}
2241
2242static void cancel_map_check(struct ceph_osd_request *req)
2243{
2244 struct ceph_osd_client *osdc = req->r_osdc;
2245 struct ceph_osd_request *lookup_req;
2246
2247 verify_osdc_wrlocked(osdc);
2248
2249 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2250 if (!lookup_req)
2251 return;
2252
2253 WARN_ON(lookup_req != req);
2254 erase_request_mc(&osdc->map_checks, req);
2255 ceph_osdc_put_request(req);
2256}
2257
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002258static void cancel_request(struct ceph_osd_request *req)
2259{
2260 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2261
Ilya Dryomov46092452016-04-28 16:07:27 +02002262 cancel_map_check(req);
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002263 finish_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002264 complete_all(&req->r_completion);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01002265 ceph_osdc_put_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002266}
2267
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002268static void abort_request(struct ceph_osd_request *req, int err)
2269{
2270 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2271
2272 cancel_map_check(req);
2273 complete_request(req, err);
2274}
2275
Jeff Layton58eb7932017-04-18 09:21:16 -04002276static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2277{
2278 if (likely(eb > osdc->epoch_barrier)) {
2279 dout("updating epoch_barrier from %u to %u\n",
2280 osdc->epoch_barrier, eb);
2281 osdc->epoch_barrier = eb;
2282 /* Request map if we're not to the barrier yet */
2283 if (eb > osdc->osdmap->epoch)
2284 maybe_request_map(osdc);
2285 }
2286}
2287
2288void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2289{
2290 down_read(&osdc->lock);
2291 if (unlikely(eb > osdc->epoch_barrier)) {
2292 up_read(&osdc->lock);
2293 down_write(&osdc->lock);
2294 update_epoch_barrier(osdc, eb);
2295 up_write(&osdc->lock);
2296 } else {
2297 up_read(&osdc->lock);
2298 }
2299}
2300EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2301
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002302/*
2303 * Drop all pending requests that are stalled waiting on a full condition to
Jeff Layton58eb7932017-04-18 09:21:16 -04002304 * clear, and complete them with ENOSPC as the return code. Set the
2305 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2306 * cancelled.
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002307 */
2308static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2309{
2310 struct rb_node *n;
Jeff Layton58eb7932017-04-18 09:21:16 -04002311 bool victims = false;
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002312
2313 dout("enter abort_on_full\n");
2314
2315 if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2316 goto out;
2317
Jeff Layton58eb7932017-04-18 09:21:16 -04002318 /* Scan list and see if there is anything to abort */
2319 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2320 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2321 struct rb_node *m;
2322
2323 m = rb_first(&osd->o_requests);
2324 while (m) {
2325 struct ceph_osd_request *req = rb_entry(m,
2326 struct ceph_osd_request, r_node);
2327 m = rb_next(m);
2328
2329 if (req->r_abort_on_full) {
2330 victims = true;
2331 break;
2332 }
2333 }
2334 if (victims)
2335 break;
2336 }
2337
2338 if (!victims)
2339 goto out;
2340
2341 /*
2342 * Update the barrier to current epoch if it's behind that point,
2343 * since we know we have some calls to be aborted in the tree.
2344 */
2345 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2346
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002347 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2348 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2349 struct rb_node *m;
2350
2351 m = rb_first(&osd->o_requests);
2352 while (m) {
2353 struct ceph_osd_request *req = rb_entry(m,
2354 struct ceph_osd_request, r_node);
2355 m = rb_next(m);
2356
2357 if (req->r_abort_on_full &&
2358 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2359 pool_full(osdc, req->r_t.target_oloc.pool)))
2360 abort_request(req, -ENOSPC);
2361 }
2362 }
2363out:
Jeff Layton58eb7932017-04-18 09:21:16 -04002364 dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002365}
2366
Ilya Dryomov46092452016-04-28 16:07:27 +02002367static void check_pool_dne(struct ceph_osd_request *req)
2368{
2369 struct ceph_osd_client *osdc = req->r_osdc;
2370 struct ceph_osdmap *map = osdc->osdmap;
2371
2372 verify_osdc_wrlocked(osdc);
2373 WARN_ON(!map->epoch);
2374
2375 if (req->r_attempts) {
2376 /*
2377 * We sent a request earlier, which means that
2378 * previously the pool existed, and now it does not
2379 * (i.e., it was deleted).
2380 */
2381 req->r_map_dne_bound = map->epoch;
2382 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2383 req->r_tid);
2384 } else {
2385 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2386 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2387 }
2388
2389 if (req->r_map_dne_bound) {
2390 if (map->epoch >= req->r_map_dne_bound) {
2391 /* we had a new enough map */
2392 pr_info_ratelimited("tid %llu pool does not exist\n",
2393 req->r_tid);
2394 complete_request(req, -ENOENT);
2395 }
2396 } else {
2397 send_map_check(req);
2398 }
2399}
2400
2401static void map_check_cb(struct ceph_mon_generic_request *greq)
2402{
2403 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2404 struct ceph_osd_request *req;
2405 u64 tid = greq->private_data;
2406
2407 WARN_ON(greq->result || !greq->u.newest);
2408
2409 down_write(&osdc->lock);
2410 req = lookup_request_mc(&osdc->map_checks, tid);
2411 if (!req) {
2412 dout("%s tid %llu dne\n", __func__, tid);
2413 goto out_unlock;
2414 }
2415
2416 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2417 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2418 if (!req->r_map_dne_bound)
2419 req->r_map_dne_bound = greq->u.newest;
2420 erase_request_mc(&osdc->map_checks, req);
2421 check_pool_dne(req);
2422
2423 ceph_osdc_put_request(req);
2424out_unlock:
2425 up_write(&osdc->lock);
2426}
2427
2428static void send_map_check(struct ceph_osd_request *req)
2429{
2430 struct ceph_osd_client *osdc = req->r_osdc;
2431 struct ceph_osd_request *lookup_req;
2432 int ret;
2433
2434 verify_osdc_wrlocked(osdc);
2435
2436 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2437 if (lookup_req) {
2438 WARN_ON(lookup_req != req);
2439 return;
2440 }
2441
2442 ceph_osdc_get_request(req);
2443 insert_request_mc(&osdc->map_checks, req);
2444 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2445 map_check_cb, req->r_tid);
2446 WARN_ON(ret);
2447}
2448
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002449/*
Ilya Dryomov922dab62016-05-26 01:15:02 +02002450 * lingering requests, watch/notify v2 infrastructure
2451 */
2452static void linger_release(struct kref *kref)
2453{
2454 struct ceph_osd_linger_request *lreq =
2455 container_of(kref, struct ceph_osd_linger_request, kref);
2456
2457 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2458 lreq->reg_req, lreq->ping_req);
2459 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2460 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
Ilya Dryomov46092452016-04-28 16:07:27 +02002461 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002462 WARN_ON(!list_empty(&lreq->scan_item));
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002463 WARN_ON(!list_empty(&lreq->pending_lworks));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002464 WARN_ON(lreq->osd);
2465
2466 if (lreq->reg_req)
2467 ceph_osdc_put_request(lreq->reg_req);
2468 if (lreq->ping_req)
2469 ceph_osdc_put_request(lreq->ping_req);
2470 target_destroy(&lreq->t);
2471 kfree(lreq);
2472}
2473
2474static void linger_put(struct ceph_osd_linger_request *lreq)
2475{
2476 if (lreq)
2477 kref_put(&lreq->kref, linger_release);
2478}
2479
2480static struct ceph_osd_linger_request *
2481linger_get(struct ceph_osd_linger_request *lreq)
2482{
2483 kref_get(&lreq->kref);
2484 return lreq;
2485}
2486
2487static struct ceph_osd_linger_request *
2488linger_alloc(struct ceph_osd_client *osdc)
2489{
2490 struct ceph_osd_linger_request *lreq;
2491
2492 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2493 if (!lreq)
2494 return NULL;
2495
2496 kref_init(&lreq->kref);
2497 mutex_init(&lreq->lock);
2498 RB_CLEAR_NODE(&lreq->node);
2499 RB_CLEAR_NODE(&lreq->osdc_node);
Ilya Dryomov46092452016-04-28 16:07:27 +02002500 RB_CLEAR_NODE(&lreq->mc_node);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002501 INIT_LIST_HEAD(&lreq->scan_item);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002502 INIT_LIST_HEAD(&lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002503 init_completion(&lreq->reg_commit_wait);
Ilya Dryomov19079202016-04-28 16:07:27 +02002504 init_completion(&lreq->notify_finish_wait);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002505
2506 lreq->osdc = osdc;
2507 target_init(&lreq->t);
2508
2509 dout("%s lreq %p\n", __func__, lreq);
2510 return lreq;
2511}
2512
2513DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2514DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02002515DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002516
2517/*
2518 * Create linger request <-> OSD session relation.
2519 *
2520 * @lreq has to be registered, @osd may be homeless.
2521 */
2522static void link_linger(struct ceph_osd *osd,
2523 struct ceph_osd_linger_request *lreq)
2524{
2525 verify_osd_locked(osd);
2526 WARN_ON(!lreq->linger_id || lreq->osd);
2527 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2528 osd->o_osd, lreq, lreq->linger_id);
2529
2530 if (!osd_homeless(osd))
2531 __remove_osd_from_lru(osd);
2532 else
2533 atomic_inc(&osd->o_osdc->num_homeless);
2534
2535 get_osd(osd);
2536 insert_linger(&osd->o_linger_requests, lreq);
2537 lreq->osd = osd;
2538}
2539
2540static void unlink_linger(struct ceph_osd *osd,
2541 struct ceph_osd_linger_request *lreq)
2542{
2543 verify_osd_locked(osd);
2544 WARN_ON(lreq->osd != osd);
2545 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2546 osd->o_osd, lreq, lreq->linger_id);
2547
2548 lreq->osd = NULL;
2549 erase_linger(&osd->o_linger_requests, lreq);
2550 put_osd(osd);
2551
2552 if (!osd_homeless(osd))
2553 maybe_move_osd_to_lru(osd);
2554 else
2555 atomic_dec(&osd->o_osdc->num_homeless);
2556}
2557
2558static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2559{
2560 verify_osdc_locked(lreq->osdc);
2561
2562 return !RB_EMPTY_NODE(&lreq->osdc_node);
2563}
2564
2565static bool linger_registered(struct ceph_osd_linger_request *lreq)
2566{
2567 struct ceph_osd_client *osdc = lreq->osdc;
2568 bool registered;
2569
2570 down_read(&osdc->lock);
2571 registered = __linger_registered(lreq);
2572 up_read(&osdc->lock);
2573
2574 return registered;
2575}
2576
2577static void linger_register(struct ceph_osd_linger_request *lreq)
2578{
2579 struct ceph_osd_client *osdc = lreq->osdc;
2580
2581 verify_osdc_wrlocked(osdc);
2582 WARN_ON(lreq->linger_id);
2583
2584 linger_get(lreq);
2585 lreq->linger_id = ++osdc->last_linger_id;
2586 insert_linger_osdc(&osdc->linger_requests, lreq);
2587}
2588
2589static void linger_unregister(struct ceph_osd_linger_request *lreq)
2590{
2591 struct ceph_osd_client *osdc = lreq->osdc;
2592
2593 verify_osdc_wrlocked(osdc);
2594
2595 erase_linger_osdc(&osdc->linger_requests, lreq);
2596 linger_put(lreq);
2597}
2598
2599static void cancel_linger_request(struct ceph_osd_request *req)
2600{
2601 struct ceph_osd_linger_request *lreq = req->r_priv;
2602
2603 WARN_ON(!req->r_linger);
2604 cancel_request(req);
2605 linger_put(lreq);
2606}
2607
2608struct linger_work {
2609 struct work_struct work;
2610 struct ceph_osd_linger_request *lreq;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002611 struct list_head pending_item;
2612 unsigned long queued_stamp;
Ilya Dryomov922dab62016-05-26 01:15:02 +02002613
2614 union {
2615 struct {
2616 u64 notify_id;
2617 u64 notifier_id;
2618 void *payload; /* points into @msg front */
2619 size_t payload_len;
2620
2621 struct ceph_msg *msg; /* for ceph_msg_put() */
2622 } notify;
2623 struct {
2624 int err;
2625 } error;
2626 };
2627};
2628
2629static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2630 work_func_t workfn)
2631{
2632 struct linger_work *lwork;
2633
2634 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2635 if (!lwork)
2636 return NULL;
2637
2638 INIT_WORK(&lwork->work, workfn);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002639 INIT_LIST_HEAD(&lwork->pending_item);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002640 lwork->lreq = linger_get(lreq);
2641
2642 return lwork;
2643}
2644
2645static void lwork_free(struct linger_work *lwork)
2646{
2647 struct ceph_osd_linger_request *lreq = lwork->lreq;
2648
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002649 mutex_lock(&lreq->lock);
2650 list_del(&lwork->pending_item);
2651 mutex_unlock(&lreq->lock);
2652
Ilya Dryomov922dab62016-05-26 01:15:02 +02002653 linger_put(lreq);
2654 kfree(lwork);
2655}
2656
2657static void lwork_queue(struct linger_work *lwork)
2658{
2659 struct ceph_osd_linger_request *lreq = lwork->lreq;
2660 struct ceph_osd_client *osdc = lreq->osdc;
2661
2662 verify_lreq_locked(lreq);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002663 WARN_ON(!list_empty(&lwork->pending_item));
2664
2665 lwork->queued_stamp = jiffies;
2666 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002667 queue_work(osdc->notify_wq, &lwork->work);
2668}
2669
2670static void do_watch_notify(struct work_struct *w)
2671{
2672 struct linger_work *lwork = container_of(w, struct linger_work, work);
2673 struct ceph_osd_linger_request *lreq = lwork->lreq;
2674
2675 if (!linger_registered(lreq)) {
2676 dout("%s lreq %p not registered\n", __func__, lreq);
2677 goto out;
2678 }
2679
Ilya Dryomov19079202016-04-28 16:07:27 +02002680 WARN_ON(!lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002681 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2682 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2683 lwork->notify.payload_len);
2684 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2685 lwork->notify.notifier_id, lwork->notify.payload,
2686 lwork->notify.payload_len);
2687
2688out:
2689 ceph_msg_put(lwork->notify.msg);
2690 lwork_free(lwork);
2691}
2692
2693static void do_watch_error(struct work_struct *w)
2694{
2695 struct linger_work *lwork = container_of(w, struct linger_work, work);
2696 struct ceph_osd_linger_request *lreq = lwork->lreq;
2697
2698 if (!linger_registered(lreq)) {
2699 dout("%s lreq %p not registered\n", __func__, lreq);
2700 goto out;
2701 }
2702
2703 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2704 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2705
2706out:
2707 lwork_free(lwork);
2708}
2709
2710static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2711{
2712 struct linger_work *lwork;
2713
2714 lwork = lwork_alloc(lreq, do_watch_error);
2715 if (!lwork) {
2716 pr_err("failed to allocate error-lwork\n");
2717 return;
2718 }
2719
2720 lwork->error.err = lreq->last_error;
2721 lwork_queue(lwork);
2722}
2723
2724static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2725 int result)
2726{
2727 if (!completion_done(&lreq->reg_commit_wait)) {
2728 lreq->reg_commit_error = (result <= 0 ? result : 0);
2729 complete_all(&lreq->reg_commit_wait);
2730 }
2731}
2732
2733static void linger_commit_cb(struct ceph_osd_request *req)
2734{
2735 struct ceph_osd_linger_request *lreq = req->r_priv;
2736
2737 mutex_lock(&lreq->lock);
2738 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2739 lreq->linger_id, req->r_result);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002740 linger_reg_commit_complete(lreq, req->r_result);
2741 lreq->committed = true;
2742
Ilya Dryomov19079202016-04-28 16:07:27 +02002743 if (!lreq->is_watch) {
2744 struct ceph_osd_data *osd_data =
2745 osd_req_op_data(req, 0, notify, response_data);
2746 void *p = page_address(osd_data->pages[0]);
2747
2748 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2749 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2750
2751 /* make note of the notify_id */
2752 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2753 lreq->notify_id = ceph_decode_64(&p);
2754 dout("lreq %p notify_id %llu\n", lreq,
2755 lreq->notify_id);
2756 } else {
2757 dout("lreq %p no notify_id\n", lreq);
2758 }
2759 }
2760
Ilya Dryomov922dab62016-05-26 01:15:02 +02002761 mutex_unlock(&lreq->lock);
2762 linger_put(lreq);
2763}
2764
2765static int normalize_watch_error(int err)
2766{
2767 /*
2768 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2769 * notification and a failure to reconnect because we raced with
2770 * the delete appear the same to the user.
2771 */
2772 if (err == -ENOENT)
2773 err = -ENOTCONN;
2774
2775 return err;
2776}
2777
2778static void linger_reconnect_cb(struct ceph_osd_request *req)
2779{
2780 struct ceph_osd_linger_request *lreq = req->r_priv;
2781
2782 mutex_lock(&lreq->lock);
2783 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2784 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2785 if (req->r_result < 0) {
2786 if (!lreq->last_error) {
2787 lreq->last_error = normalize_watch_error(req->r_result);
2788 queue_watch_error(lreq);
2789 }
2790 }
2791
2792 mutex_unlock(&lreq->lock);
2793 linger_put(lreq);
2794}
2795
2796static void send_linger(struct ceph_osd_linger_request *lreq)
2797{
2798 struct ceph_osd_request *req = lreq->reg_req;
2799 struct ceph_osd_req_op *op = &req->r_ops[0];
2800
2801 verify_osdc_wrlocked(req->r_osdc);
2802 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2803
2804 if (req->r_osd)
2805 cancel_linger_request(req);
2806
2807 request_reinit(req);
2808 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2809 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2810 req->r_flags = lreq->t.flags;
2811 req->r_mtime = lreq->mtime;
2812
2813 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002814 if (lreq->is_watch && lreq->committed) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002815 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2816 op->watch.cookie != lreq->linger_id);
2817 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2818 op->watch.gen = ++lreq->register_gen;
2819 dout("lreq %p reconnect register_gen %u\n", lreq,
2820 op->watch.gen);
2821 req->r_callback = linger_reconnect_cb;
2822 } else {
Ilya Dryomov19079202016-04-28 16:07:27 +02002823 if (!lreq->is_watch)
2824 lreq->notify_id = 0;
2825 else
2826 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002827 dout("lreq %p register\n", lreq);
2828 req->r_callback = linger_commit_cb;
2829 }
2830 mutex_unlock(&lreq->lock);
2831
2832 req->r_priv = linger_get(lreq);
2833 req->r_linger = true;
2834
2835 submit_request(req, true);
2836}
2837
2838static void linger_ping_cb(struct ceph_osd_request *req)
2839{
2840 struct ceph_osd_linger_request *lreq = req->r_priv;
2841
2842 mutex_lock(&lreq->lock);
2843 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2844 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2845 lreq->last_error);
2846 if (lreq->register_gen == req->r_ops[0].watch.gen) {
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002847 if (!req->r_result) {
2848 lreq->watch_valid_thru = lreq->ping_sent;
2849 } else if (!lreq->last_error) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002850 lreq->last_error = normalize_watch_error(req->r_result);
2851 queue_watch_error(lreq);
2852 }
2853 } else {
2854 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2855 lreq->register_gen, req->r_ops[0].watch.gen);
2856 }
2857
2858 mutex_unlock(&lreq->lock);
2859 linger_put(lreq);
2860}
2861
2862static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2863{
2864 struct ceph_osd_client *osdc = lreq->osdc;
2865 struct ceph_osd_request *req = lreq->ping_req;
2866 struct ceph_osd_req_op *op = &req->r_ops[0];
2867
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002868 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002869 dout("%s PAUSERD\n", __func__);
2870 return;
2871 }
2872
2873 lreq->ping_sent = jiffies;
2874 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2875 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2876 lreq->register_gen);
2877
2878 if (req->r_osd)
2879 cancel_linger_request(req);
2880
2881 request_reinit(req);
2882 target_copy(&req->r_t, &lreq->t);
2883
2884 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2885 op->watch.cookie != lreq->linger_id ||
2886 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2887 op->watch.gen = lreq->register_gen;
2888 req->r_callback = linger_ping_cb;
2889 req->r_priv = linger_get(lreq);
2890 req->r_linger = true;
2891
2892 ceph_osdc_get_request(req);
2893 account_request(req);
2894 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2895 link_request(lreq->osd, req);
2896 send_request(req);
2897}
2898
2899static void linger_submit(struct ceph_osd_linger_request *lreq)
2900{
2901 struct ceph_osd_client *osdc = lreq->osdc;
2902 struct ceph_osd *osd;
2903
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002904 calc_target(osdc, &lreq->t, NULL, false);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002905 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2906 link_linger(osd, lreq);
2907
2908 send_linger(lreq);
2909}
2910
Ilya Dryomov46092452016-04-28 16:07:27 +02002911static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2912{
2913 struct ceph_osd_client *osdc = lreq->osdc;
2914 struct ceph_osd_linger_request *lookup_lreq;
2915
2916 verify_osdc_wrlocked(osdc);
2917
2918 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2919 lreq->linger_id);
2920 if (!lookup_lreq)
2921 return;
2922
2923 WARN_ON(lookup_lreq != lreq);
2924 erase_linger_mc(&osdc->linger_map_checks, lreq);
2925 linger_put(lreq);
2926}
2927
Ilya Dryomov922dab62016-05-26 01:15:02 +02002928/*
2929 * @lreq has to be both registered and linked.
2930 */
2931static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2932{
Ilya Dryomov19079202016-04-28 16:07:27 +02002933 if (lreq->is_watch && lreq->ping_req->r_osd)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002934 cancel_linger_request(lreq->ping_req);
2935 if (lreq->reg_req->r_osd)
2936 cancel_linger_request(lreq->reg_req);
Ilya Dryomov46092452016-04-28 16:07:27 +02002937 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002938 unlink_linger(lreq->osd, lreq);
2939 linger_unregister(lreq);
2940}
2941
2942static void linger_cancel(struct ceph_osd_linger_request *lreq)
2943{
2944 struct ceph_osd_client *osdc = lreq->osdc;
2945
2946 down_write(&osdc->lock);
2947 if (__linger_registered(lreq))
2948 __linger_cancel(lreq);
2949 up_write(&osdc->lock);
2950}
2951
Ilya Dryomov46092452016-04-28 16:07:27 +02002952static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2953
2954static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2955{
2956 struct ceph_osd_client *osdc = lreq->osdc;
2957 struct ceph_osdmap *map = osdc->osdmap;
2958
2959 verify_osdc_wrlocked(osdc);
2960 WARN_ON(!map->epoch);
2961
2962 if (lreq->register_gen) {
2963 lreq->map_dne_bound = map->epoch;
2964 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2965 lreq, lreq->linger_id);
2966 } else {
2967 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2968 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2969 map->epoch);
2970 }
2971
2972 if (lreq->map_dne_bound) {
2973 if (map->epoch >= lreq->map_dne_bound) {
2974 /* we had a new enough map */
2975 pr_info("linger_id %llu pool does not exist\n",
2976 lreq->linger_id);
2977 linger_reg_commit_complete(lreq, -ENOENT);
2978 __linger_cancel(lreq);
2979 }
2980 } else {
2981 send_linger_map_check(lreq);
2982 }
2983}
2984
2985static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2986{
2987 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2988 struct ceph_osd_linger_request *lreq;
2989 u64 linger_id = greq->private_data;
2990
2991 WARN_ON(greq->result || !greq->u.newest);
2992
2993 down_write(&osdc->lock);
2994 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2995 if (!lreq) {
2996 dout("%s linger_id %llu dne\n", __func__, linger_id);
2997 goto out_unlock;
2998 }
2999
3000 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3001 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3002 greq->u.newest);
3003 if (!lreq->map_dne_bound)
3004 lreq->map_dne_bound = greq->u.newest;
3005 erase_linger_mc(&osdc->linger_map_checks, lreq);
3006 check_linger_pool_dne(lreq);
3007
3008 linger_put(lreq);
3009out_unlock:
3010 up_write(&osdc->lock);
3011}
3012
3013static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3014{
3015 struct ceph_osd_client *osdc = lreq->osdc;
3016 struct ceph_osd_linger_request *lookup_lreq;
3017 int ret;
3018
3019 verify_osdc_wrlocked(osdc);
3020
3021 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3022 lreq->linger_id);
3023 if (lookup_lreq) {
3024 WARN_ON(lookup_lreq != lreq);
3025 return;
3026 }
3027
3028 linger_get(lreq);
3029 insert_linger_mc(&osdc->linger_map_checks, lreq);
3030 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3031 linger_map_check_cb, lreq->linger_id);
3032 WARN_ON(ret);
3033}
3034
Ilya Dryomov922dab62016-05-26 01:15:02 +02003035static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3036{
3037 int ret;
3038
3039 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3040 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3041 return ret ?: lreq->reg_commit_error;
3042}
3043
Ilya Dryomov19079202016-04-28 16:07:27 +02003044static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3045{
3046 int ret;
3047
3048 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3049 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3050 return ret ?: lreq->notify_finish_error;
3051}
3052
Ilya Dryomov922dab62016-05-26 01:15:02 +02003053/*
Ilya Dryomovfbca9632016-04-28 16:07:24 +02003054 * Timeout callback, called every N seconds. When 1 or more OSD
3055 * requests has been active for more than N seconds, we send a keepalive
3056 * (tag + timestamp) to its OSD to ensure any communications channel
3057 * reset is detected.
Sage Weilf24e9982009-10-06 11:31:10 -07003058 */
3059static void handle_timeout(struct work_struct *work)
3060{
3061 struct ceph_osd_client *osdc =
3062 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03003063 struct ceph_options *opts = osdc->client->options;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003064 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003065 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003066 LIST_HEAD(slow_osds);
3067 struct rb_node *n, *p;
Sage Weilf24e9982009-10-06 11:31:10 -07003068
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003069 dout("%s osdc %p\n", __func__, osdc);
3070 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003071
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003072 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003073 * ping osds that are a bit slow. this ensures that if there
3074 * is a break in the TCP connection we will notice, and reopen
3075 * a connection with that osd (from the fault callback).
3076 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003077 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3078 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3079 bool found = false;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003080
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003081 for (p = rb_first(&osd->o_requests); p; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003082 struct ceph_osd_request *req =
3083 rb_entry(p, struct ceph_osd_request, r_node);
3084
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003085 p = rb_next(p); /* abort_request() */
3086
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003087 if (time_before(req->r_stamp, cutoff)) {
3088 dout(" req %p tid %llu on osd%d is laggy\n",
3089 req, req->r_tid, osd->o_osd);
3090 found = true;
3091 }
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003092 if (opts->osd_request_timeout &&
3093 time_before(req->r_start_stamp, expiry_cutoff)) {
3094 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3095 req->r_tid, osd->o_osd);
3096 abort_request(req, -ETIMEDOUT);
3097 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003098 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003099 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3100 struct ceph_osd_linger_request *lreq =
3101 rb_entry(p, struct ceph_osd_linger_request, node);
3102
3103 dout(" lreq %p linger_id %llu is served by osd%d\n",
3104 lreq, lreq->linger_id, osd->o_osd);
3105 found = true;
3106
3107 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02003108 if (lreq->is_watch && lreq->committed && !lreq->last_error)
Ilya Dryomov922dab62016-05-26 01:15:02 +02003109 send_linger_ping(lreq);
3110 mutex_unlock(&lreq->lock);
3111 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003112
3113 if (found)
3114 list_move_tail(&osd->o_keepalive_item, &slow_osds);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003115 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003116
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01003117 if (opts->osd_request_timeout) {
3118 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3119 struct ceph_osd_request *req =
3120 rb_entry(p, struct ceph_osd_request, r_node);
3121
3122 p = rb_next(p); /* abort_request() */
3123
3124 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3125 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3126 req->r_tid, osdc->homeless_osd.o_osd);
3127 abort_request(req, -ETIMEDOUT);
3128 }
3129 }
3130 }
3131
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003132 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3133 maybe_request_map(osdc);
3134
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003135 while (!list_empty(&slow_osds)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003136 struct ceph_osd *osd = list_first_entry(&slow_osds,
3137 struct ceph_osd,
3138 o_keepalive_item);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003139 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07003140 ceph_con_keepalive(&osd->o_con);
3141 }
3142
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003143 up_write(&osdc->lock);
Ilya Dryomovfbca9632016-04-28 16:07:24 +02003144 schedule_delayed_work(&osdc->timeout_work,
3145 osdc->client->options->osd_keepalive_timeout);
Sage Weilf24e9982009-10-06 11:31:10 -07003146}
3147
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003148static void handle_osds_timeout(struct work_struct *work)
3149{
3150 struct ceph_osd_client *osdc =
3151 container_of(work, struct ceph_osd_client,
3152 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03003153 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003154 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003155
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003156 dout("%s osdc %p\n", __func__, osdc);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003157 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003158 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3159 if (time_before(jiffies, osd->lru_ttl))
3160 break;
3161
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003162 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02003163 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003164 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02003165 }
3166
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003167 up_write(&osdc->lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08003168 schedule_delayed_work(&osdc->osds_timeout_work,
3169 round_jiffies_relative(delay));
3170}
3171
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003172static int ceph_oloc_decode(void **p, void *end,
3173 struct ceph_object_locator *oloc)
3174{
3175 u8 struct_v, struct_cv;
3176 u32 len;
3177 void *struct_end;
3178 int ret = 0;
3179
3180 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3181 struct_v = ceph_decode_8(p);
3182 struct_cv = ceph_decode_8(p);
3183 if (struct_v < 3) {
3184 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3185 struct_v, struct_cv);
3186 goto e_inval;
3187 }
3188 if (struct_cv > 6) {
3189 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3190 struct_v, struct_cv);
3191 goto e_inval;
3192 }
3193 len = ceph_decode_32(p);
3194 ceph_decode_need(p, end, len, e_inval);
3195 struct_end = *p + len;
3196
3197 oloc->pool = ceph_decode_64(p);
3198 *p += 4; /* skip preferred */
3199
3200 len = ceph_decode_32(p);
3201 if (len > 0) {
3202 pr_warn("ceph_object_locator::key is set\n");
3203 goto e_inval;
3204 }
3205
3206 if (struct_v >= 5) {
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003207 bool changed = false;
3208
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003209 len = ceph_decode_32(p);
3210 if (len > 0) {
Yan, Zheng30c156d2016-02-14 11:24:31 +08003211 ceph_decode_need(p, end, len, e_inval);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003212 if (!oloc->pool_ns ||
3213 ceph_compare_string(oloc->pool_ns, *p, len))
3214 changed = true;
Yan, Zheng30c156d2016-02-14 11:24:31 +08003215 *p += len;
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003216 } else {
3217 if (oloc->pool_ns)
3218 changed = true;
3219 }
3220 if (changed) {
3221 /* redirect changes namespace */
3222 pr_warn("ceph_object_locator::nspace is changed\n");
3223 goto e_inval;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003224 }
3225 }
3226
3227 if (struct_v >= 6) {
3228 s64 hash = ceph_decode_64(p);
3229 if (hash != -1) {
3230 pr_warn("ceph_object_locator::hash is set\n");
3231 goto e_inval;
3232 }
3233 }
3234
3235 /* skip the rest */
3236 *p = struct_end;
3237out:
3238 return ret;
3239
3240e_inval:
3241 ret = -EINVAL;
3242 goto out;
3243}
3244
3245static int ceph_redirect_decode(void **p, void *end,
3246 struct ceph_request_redirect *redir)
3247{
3248 u8 struct_v, struct_cv;
3249 u32 len;
3250 void *struct_end;
3251 int ret;
3252
3253 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3254 struct_v = ceph_decode_8(p);
3255 struct_cv = ceph_decode_8(p);
3256 if (struct_cv > 1) {
3257 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3258 struct_v, struct_cv);
3259 goto e_inval;
3260 }
3261 len = ceph_decode_32(p);
3262 ceph_decode_need(p, end, len, e_inval);
3263 struct_end = *p + len;
3264
3265 ret = ceph_oloc_decode(p, end, &redir->oloc);
3266 if (ret)
3267 goto out;
3268
3269 len = ceph_decode_32(p);
3270 if (len > 0) {
3271 pr_warn("ceph_request_redirect::object_name is set\n");
3272 goto e_inval;
3273 }
3274
3275 len = ceph_decode_32(p);
3276 *p += len; /* skip osd_instructions */
3277
3278 /* skip the rest */
3279 *p = struct_end;
3280out:
3281 return ret;
3282
3283e_inval:
3284 ret = -EINVAL;
3285 goto out;
3286}
3287
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003288struct MOSDOpReply {
3289 struct ceph_pg pgid;
3290 u64 flags;
3291 int result;
3292 u32 epoch;
3293 int num_ops;
3294 u32 outdata_len[CEPH_OSD_MAX_OPS];
3295 s32 rval[CEPH_OSD_MAX_OPS];
3296 int retry_attempt;
3297 struct ceph_eversion replay_version;
3298 u64 user_version;
3299 struct ceph_request_redirect redirect;
3300};
Sage Weil25845472011-06-03 09:37:09 -07003301
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003302static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
Sage Weilf24e9982009-10-06 11:31:10 -07003303{
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003304 void *p = msg->front.iov_base;
3305 void *const end = p + msg->front.iov_len;
3306 u16 version = le16_to_cpu(msg->hdr.version);
3307 struct ceph_eversion bad_replay_version;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003308 u8 decode_redir;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003309 u32 len;
3310 int ret;
3311 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07003312
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003313 ceph_decode_32_safe(&p, end, len, e_inval);
3314 ceph_decode_need(&p, end, len, e_inval);
3315 p += len; /* skip oid */
Sage Weil1b83bef2013-02-25 16:11:12 -08003316
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003317 ret = ceph_decode_pgid(&p, end, &m->pgid);
3318 if (ret)
3319 return ret;
Sage Weil1b83bef2013-02-25 16:11:12 -08003320
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003321 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3322 ceph_decode_32_safe(&p, end, m->result, e_inval);
3323 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3324 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3325 p += sizeof(bad_replay_version);
3326 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
Sage Weil1b83bef2013-02-25 16:11:12 -08003327
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003328 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3329 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3330 goto e_inval;
Sage Weil1b83bef2013-02-25 16:11:12 -08003331
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003332 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3333 e_inval);
3334 for (i = 0; i < m->num_ops; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -08003335 struct ceph_osd_op *op = p;
Sage Weil1b83bef2013-02-25 16:11:12 -08003336
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003337 m->outdata_len[i] = le32_to_cpu(op->payload_len);
Sage Weil1b83bef2013-02-25 16:11:12 -08003338 p += sizeof(*op);
3339 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003340
3341 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3342 for (i = 0; i < m->num_ops; i++)
3343 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3344
3345 if (version >= 5) {
3346 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3347 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3348 p += sizeof(m->replay_version);
3349 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3350 } else {
3351 m->replay_version = bad_replay_version; /* struct */
3352 m->user_version = le64_to_cpu(m->replay_version.version);
Sage Weil1b83bef2013-02-25 16:11:12 -08003353 }
3354
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003355 if (version >= 6) {
3356 if (version >= 7)
3357 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003358 else
3359 decode_redir = 1;
3360 } else {
3361 decode_redir = 0;
3362 }
3363
3364 if (decode_redir) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003365 ret = ceph_redirect_decode(&p, end, &m->redirect);
3366 if (ret)
3367 return ret;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003368 } else {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003369 ceph_oloc_init(&m->redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003370 }
3371
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003372 return 0;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003373
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003374e_inval:
3375 return -EINVAL;
3376}
3377
3378/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003379 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3380 * specified.
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003381 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003382static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003383{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003384 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003385 struct ceph_osd_request *req;
3386 struct MOSDOpReply m;
3387 u64 tid = le64_to_cpu(msg->hdr.tid);
3388 u32 data_len = 0;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003389 int ret;
3390 int i;
3391
3392 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3393
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003394 down_read(&osdc->lock);
3395 if (!osd_registered(osd)) {
3396 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3397 goto out_unlock_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003398 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003399 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3400
3401 mutex_lock(&osd->lock);
3402 req = lookup_request(&osd->o_requests, tid);
3403 if (!req) {
3404 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3405 goto out_unlock_session;
3406 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003407
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003408 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003409 ret = decode_MOSDOpReply(msg, &m);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003410 m.redirect.oloc.pool_ns = NULL;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003411 if (ret) {
3412 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3413 req->r_tid, ret);
3414 ceph_msg_dump(msg);
3415 goto fail_request;
3416 }
3417 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3418 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3419 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3420 le64_to_cpu(m.replay_version.version), m.user_version);
3421
3422 if (m.retry_attempt >= 0) {
3423 if (m.retry_attempt != req->r_attempts - 1) {
3424 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3425 req, req->r_tid, m.retry_attempt,
3426 req->r_attempts - 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003427 goto out_unlock_session;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003428 }
3429 } else {
3430 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3431 }
3432
3433 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3434 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3435 m.redirect.oloc.pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003436 unlink_request(osd, req);
3437 mutex_unlock(&osd->lock);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003438
Yan, Zheng30c156d2016-02-14 11:24:31 +08003439 /*
3440 * Not ceph_oloc_copy() - changing pool_ns is not
3441 * supported.
3442 */
3443 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003444 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3445 req->r_tid = 0;
3446 __submit_request(req, false);
3447 goto out_unlock_osdc;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003448 }
3449
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003450 if (m.num_ops != req->r_num_ops) {
3451 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3452 req->r_num_ops, req->r_tid);
3453 goto fail_request;
3454 }
3455 for (i = 0; i < req->r_num_ops; i++) {
3456 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3457 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3458 req->r_ops[i].rval = m.rval[i];
3459 req->r_ops[i].outdata_len = m.outdata_len[i];
3460 data_len += m.outdata_len[i];
3461 }
3462 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3463 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3464 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3465 goto fail_request;
3466 }
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003467 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3468 req, req->r_tid, m.result, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07003469
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003470 /*
3471 * Since we only ever request ONDISK, we should only ever get
3472 * one (type of) reply back.
3473 */
3474 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3475 req->r_result = m.result ?: data_len;
3476 finish_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003477 mutex_unlock(&osd->lock);
3478 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003479
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003480 __complete_request(req);
3481 complete_all(&req->r_completion);
3482 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07003483 return;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003484
3485fail_request:
Ilya Dryomov46092452016-04-28 16:07:27 +02003486 complete_request(req, -EIO);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003487out_unlock_session:
3488 mutex_unlock(&osd->lock);
3489out_unlock_osdc:
3490 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003491}
3492
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003493static void set_pool_was_full(struct ceph_osd_client *osdc)
3494{
3495 struct rb_node *n;
3496
3497 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3498 struct ceph_pg_pool_info *pi =
3499 rb_entry(n, struct ceph_pg_pool_info, node);
3500
3501 pi->was_full = __pool_full(pi);
3502 }
3503}
3504
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003505static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
Sage Weilf24e9982009-10-06 11:31:10 -07003506{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003507 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07003508
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003509 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3510 if (!pi)
3511 return false;
Sage Weilf24e9982009-10-06 11:31:10 -07003512
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003513 return pi->was_full && !__pool_full(pi);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003514}
3515
Ilya Dryomov922dab62016-05-26 01:15:02 +02003516static enum calc_target_result
3517recalc_linger_target(struct ceph_osd_linger_request *lreq)
3518{
3519 struct ceph_osd_client *osdc = lreq->osdc;
3520 enum calc_target_result ct_res;
3521
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003522 ct_res = calc_target(osdc, &lreq->t, NULL, true);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003523 if (ct_res == CALC_TARGET_NEED_RESEND) {
3524 struct ceph_osd *osd;
3525
3526 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3527 if (osd != lreq->osd) {
3528 unlink_linger(lreq->osd, lreq);
3529 link_linger(osd, lreq);
3530 }
3531 }
3532
3533 return ct_res;
3534}
3535
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003536/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003537 * Requeue requests whose mapping to an OSD has changed.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003538 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003539static void scan_requests(struct ceph_osd *osd,
3540 bool force_resend,
3541 bool cleared_full,
3542 bool check_pool_cleared_full,
3543 struct rb_root *need_resend,
3544 struct list_head *need_resend_linger)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003545{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003546 struct ceph_osd_client *osdc = osd->o_osdc;
3547 struct rb_node *n;
3548 bool force_resend_writes;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003549
Ilya Dryomov922dab62016-05-26 01:15:02 +02003550 for (n = rb_first(&osd->o_linger_requests); n; ) {
3551 struct ceph_osd_linger_request *lreq =
3552 rb_entry(n, struct ceph_osd_linger_request, node);
3553 enum calc_target_result ct_res;
3554
3555 n = rb_next(n); /* recalc_linger_target() */
3556
3557 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3558 lreq->linger_id);
3559 ct_res = recalc_linger_target(lreq);
3560 switch (ct_res) {
3561 case CALC_TARGET_NO_ACTION:
3562 force_resend_writes = cleared_full ||
3563 (check_pool_cleared_full &&
3564 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3565 if (!force_resend && !force_resend_writes)
3566 break;
3567
3568 /* fall through */
3569 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003570 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003571 /*
3572 * scan_requests() for the previous epoch(s)
3573 * may have already added it to the list, since
3574 * it's not unlinked here.
3575 */
3576 if (list_empty(&lreq->scan_item))
3577 list_add_tail(&lreq->scan_item, need_resend_linger);
3578 break;
3579 case CALC_TARGET_POOL_DNE:
Ilya Dryomova10bcb12017-06-15 16:30:55 +02003580 list_del_init(&lreq->scan_item);
Ilya Dryomov46092452016-04-28 16:07:27 +02003581 check_linger_pool_dne(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003582 break;
3583 }
3584 }
3585
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003586 for (n = rb_first(&osd->o_requests); n; ) {
3587 struct ceph_osd_request *req =
3588 rb_entry(n, struct ceph_osd_request, r_node);
3589 enum calc_target_result ct_res;
Alex Elderab60b162012-12-19 15:52:36 -06003590
Ilya Dryomov46092452016-04-28 16:07:27 +02003591 n = rb_next(n); /* unlink_request(), check_pool_dne() */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003592
3593 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003594 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3595 false);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003596 switch (ct_res) {
3597 case CALC_TARGET_NO_ACTION:
3598 force_resend_writes = cleared_full ||
3599 (check_pool_cleared_full &&
3600 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3601 if (!force_resend &&
3602 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3603 !force_resend_writes))
3604 break;
3605
3606 /* fall through */
3607 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003608 cancel_map_check(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003609 unlink_request(osd, req);
3610 insert_request(need_resend, req);
3611 break;
3612 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003613 check_pool_dne(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003614 break;
Alex Elderab60b162012-12-19 15:52:36 -06003615 }
Sage Weilf24e9982009-10-06 11:31:10 -07003616 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003617}
Sage Weil6f6c7002011-01-17 20:34:08 -08003618
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003619static int handle_one_map(struct ceph_osd_client *osdc,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003620 void *p, void *end, bool incremental,
3621 struct rb_root *need_resend,
3622 struct list_head *need_resend_linger)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003623{
3624 struct ceph_osdmap *newmap;
3625 struct rb_node *n;
3626 bool skipped_map = false;
3627 bool was_full;
3628
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003629 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003630 set_pool_was_full(osdc);
3631
3632 if (incremental)
3633 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3634 else
3635 newmap = ceph_osdmap_decode(&p, end);
3636 if (IS_ERR(newmap))
3637 return PTR_ERR(newmap);
3638
3639 if (newmap != osdc->osdmap) {
3640 /*
3641 * Preserve ->was_full before destroying the old map.
3642 * For pools that weren't in the old map, ->was_full
3643 * should be false.
3644 */
3645 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3646 struct ceph_pg_pool_info *pi =
3647 rb_entry(n, struct ceph_pg_pool_info, node);
3648 struct ceph_pg_pool_info *old_pi;
3649
3650 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3651 if (old_pi)
3652 pi->was_full = old_pi->was_full;
3653 else
3654 WARN_ON(pi->was_full);
3655 }
3656
3657 if (osdc->osdmap->epoch &&
3658 osdc->osdmap->epoch + 1 < newmap->epoch) {
3659 WARN_ON(incremental);
3660 skipped_map = true;
3661 }
3662
3663 ceph_osdmap_destroy(osdc->osdmap);
3664 osdc->osdmap = newmap;
3665 }
3666
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003667 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003668 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3669 need_resend, need_resend_linger);
3670
3671 for (n = rb_first(&osdc->osds); n; ) {
3672 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3673
3674 n = rb_next(n); /* close_osd() */
3675
3676 scan_requests(osd, skipped_map, was_full, true, need_resend,
3677 need_resend_linger);
3678 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3679 memcmp(&osd->o_con.peer_addr,
3680 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3681 sizeof(struct ceph_entity_addr)))
3682 close_osd(osd);
3683 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003684
3685 return 0;
3686}
Sage Weil6f6c7002011-01-17 20:34:08 -08003687
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003688static void kick_requests(struct ceph_osd_client *osdc,
3689 struct rb_root *need_resend,
3690 struct list_head *need_resend_linger)
3691{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003692 struct ceph_osd_linger_request *lreq, *nlreq;
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003693 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003694 struct rb_node *n;
3695
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003696 /* make sure need_resend targets reflect latest map */
3697 for (n = rb_first(need_resend); n; ) {
3698 struct ceph_osd_request *req =
3699 rb_entry(n, struct ceph_osd_request, r_node);
3700
3701 n = rb_next(n);
3702
3703 if (req->r_t.epoch < osdc->osdmap->epoch) {
3704 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3705 if (ct_res == CALC_TARGET_POOL_DNE) {
3706 erase_request(need_resend, req);
3707 check_pool_dne(req);
3708 }
3709 }
3710 }
3711
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003712 for (n = rb_first(need_resend); n; ) {
3713 struct ceph_osd_request *req =
3714 rb_entry(n, struct ceph_osd_request, r_node);
3715 struct ceph_osd *osd;
3716
3717 n = rb_next(n);
3718 erase_request(need_resend, req); /* before link_request() */
3719
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003720 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3721 link_request(osd, req);
3722 if (!req->r_linger) {
3723 if (!osd_homeless(osd) && !req->r_t.paused)
3724 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003725 } else {
3726 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003727 }
3728 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003729
3730 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3731 if (!osd_homeless(lreq->osd))
3732 send_linger(lreq);
3733
3734 list_del_init(&lreq->scan_item);
3735 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003736}
3737
Sage Weilf24e9982009-10-06 11:31:10 -07003738/*
3739 * Process updated osd map.
3740 *
3741 * The message contains any number of incremental and full maps, normally
3742 * indicating some sort of topology change in the cluster. Kick requests
3743 * off to different OSDs as needed.
3744 */
3745void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3746{
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003747 void *p = msg->front.iov_base;
3748 void *const end = p + msg->front.iov_len;
Sage Weilf24e9982009-10-06 11:31:10 -07003749 u32 nr_maps, maplen;
3750 u32 epoch;
Sage Weilf24e9982009-10-06 11:31:10 -07003751 struct ceph_fsid fsid;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003752 struct rb_root need_resend = RB_ROOT;
3753 LIST_HEAD(need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003754 bool handled_incremental = false;
3755 bool was_pauserd, was_pausewr;
3756 bool pauserd, pausewr;
3757 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07003758
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003759 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003760 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003761
3762 /* verify fsid */
3763 ceph_decode_need(&p, end, sizeof(fsid), bad);
3764 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08003765 if (ceph_check_fsid(osdc->client, &fsid) < 0)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003766 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003767
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003768 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3769 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3770 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003771 have_pool_full(osdc);
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08003772
Sage Weilf24e9982009-10-06 11:31:10 -07003773 /* incremental maps */
3774 ceph_decode_32_safe(&p, end, nr_maps, bad);
3775 dout(" %d inc maps\n", nr_maps);
3776 while (nr_maps > 0) {
3777 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003778 epoch = ceph_decode_32(&p);
3779 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003780 ceph_decode_need(&p, end, maplen, bad);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003781 if (osdc->osdmap->epoch &&
3782 osdc->osdmap->epoch + 1 == epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003783 dout("applying incremental map %u len %d\n",
3784 epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003785 err = handle_one_map(osdc, p, p + maplen, true,
3786 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003787 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003788 goto bad;
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003789 handled_incremental = true;
Sage Weilf24e9982009-10-06 11:31:10 -07003790 } else {
3791 dout("ignoring incremental map %u len %d\n",
3792 epoch, maplen);
3793 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003794 p += maplen;
Sage Weilf24e9982009-10-06 11:31:10 -07003795 nr_maps--;
3796 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003797 if (handled_incremental)
Sage Weilf24e9982009-10-06 11:31:10 -07003798 goto done;
3799
3800 /* full maps */
3801 ceph_decode_32_safe(&p, end, nr_maps, bad);
3802 dout(" %d full maps\n", nr_maps);
3803 while (nr_maps) {
3804 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003805 epoch = ceph_decode_32(&p);
3806 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003807 ceph_decode_need(&p, end, maplen, bad);
3808 if (nr_maps > 1) {
3809 dout("skipping non-latest full map %u len %d\n",
3810 epoch, maplen);
Ilya Dryomove5253a72016-04-28 16:07:25 +02003811 } else if (osdc->osdmap->epoch >= epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003812 dout("skipping full map %u len %d, "
3813 "older than our %u\n", epoch, maplen,
3814 osdc->osdmap->epoch);
3815 } else {
3816 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003817 err = handle_one_map(osdc, p, p + maplen, false,
3818 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003819 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003820 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003821 }
3822 p += maplen;
3823 nr_maps--;
3824 }
3825
3826done:
Sage Weilcd634fb2011-05-12 09:29:18 -07003827 /*
3828 * subscribe to subsequent osdmap updates if full to ensure
3829 * we find out when we are no longer full and stop returning
3830 * ENOSPC.
3831 */
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003832 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3833 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3834 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003835 have_pool_full(osdc);
Jeff Layton58eb7932017-04-18 09:21:16 -04003836 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3837 osdc->osdmap->epoch < osdc->epoch_barrier)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003838 maybe_request_map(osdc);
Sage Weilcd634fb2011-05-12 09:29:18 -07003839
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003840 kick_requests(osdc, &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003841
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04003842 ceph_osdc_abort_on_full(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003843 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3844 osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003845 up_write(&osdc->lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07003846 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07003847 return;
3848
3849bad:
3850 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003851 ceph_msg_dump(msg);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003852 up_write(&osdc->lock);
3853}
3854
3855/*
3856 * Resubmit requests pending on the given osd.
3857 */
3858static void kick_osd_requests(struct ceph_osd *osd)
3859{
3860 struct rb_node *n;
3861
Ilya Dryomova02a9462017-06-19 12:18:05 +02003862 clear_backoffs(osd);
3863
Ilya Dryomov922dab62016-05-26 01:15:02 +02003864 for (n = rb_first(&osd->o_requests); n; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003865 struct ceph_osd_request *req =
3866 rb_entry(n, struct ceph_osd_request, r_node);
3867
Ilya Dryomov922dab62016-05-26 01:15:02 +02003868 n = rb_next(n); /* cancel_linger_request() */
3869
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003870 if (!req->r_linger) {
3871 if (!req->r_t.paused)
3872 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003873 } else {
3874 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003875 }
3876 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003877 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3878 struct ceph_osd_linger_request *lreq =
3879 rb_entry(n, struct ceph_osd_linger_request, node);
3880
3881 send_linger(lreq);
3882 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003883}
3884
3885/*
3886 * If the osd connection drops, we need to resubmit all requests.
3887 */
3888static void osd_fault(struct ceph_connection *con)
3889{
3890 struct ceph_osd *osd = con->private;
3891 struct ceph_osd_client *osdc = osd->o_osdc;
3892
3893 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3894
3895 down_write(&osdc->lock);
3896 if (!osd_registered(osd)) {
3897 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3898 goto out_unlock;
3899 }
3900
3901 if (!reopen_osd(osd))
3902 kick_osd_requests(osd);
3903 maybe_request_map(osdc);
3904
3905out_unlock:
3906 up_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003907}
3908
Ilya Dryomova02a9462017-06-19 12:18:05 +02003909struct MOSDBackoff {
3910 struct ceph_spg spgid;
3911 u32 map_epoch;
3912 u8 op;
3913 u64 id;
3914 struct ceph_hobject_id *begin;
3915 struct ceph_hobject_id *end;
3916};
3917
3918static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
3919{
3920 void *p = msg->front.iov_base;
3921 void *const end = p + msg->front.iov_len;
3922 u8 struct_v;
3923 u32 struct_len;
3924 int ret;
3925
3926 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
3927 if (ret)
3928 return ret;
3929
3930 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
3931 if (ret)
3932 return ret;
3933
3934 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
3935 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
3936 ceph_decode_8_safe(&p, end, m->op, e_inval);
3937 ceph_decode_64_safe(&p, end, m->id, e_inval);
3938
3939 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
3940 if (!m->begin)
3941 return -ENOMEM;
3942
3943 ret = decode_hoid(&p, end, m->begin);
3944 if (ret) {
3945 free_hoid(m->begin);
3946 return ret;
3947 }
3948
3949 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
3950 if (!m->end) {
3951 free_hoid(m->begin);
3952 return -ENOMEM;
3953 }
3954
3955 ret = decode_hoid(&p, end, m->end);
3956 if (ret) {
3957 free_hoid(m->begin);
3958 free_hoid(m->end);
3959 return ret;
3960 }
3961
3962 return 0;
3963
3964e_inval:
3965 return -EINVAL;
3966}
3967
3968static struct ceph_msg *create_backoff_message(
3969 const struct ceph_osd_backoff *backoff,
3970 u32 map_epoch)
3971{
3972 struct ceph_msg *msg;
3973 void *p, *end;
3974 int msg_size;
3975
3976 msg_size = CEPH_ENCODING_START_BLK_LEN +
3977 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
3978 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
3979 msg_size += CEPH_ENCODING_START_BLK_LEN +
3980 hoid_encoding_size(backoff->begin);
3981 msg_size += CEPH_ENCODING_START_BLK_LEN +
3982 hoid_encoding_size(backoff->end);
3983
3984 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
3985 if (!msg)
3986 return NULL;
3987
3988 p = msg->front.iov_base;
3989 end = p + msg->front_alloc_len;
3990
3991 encode_spgid(&p, &backoff->spgid);
3992 ceph_encode_32(&p, map_epoch);
3993 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
3994 ceph_encode_64(&p, backoff->id);
3995 encode_hoid(&p, end, backoff->begin);
3996 encode_hoid(&p, end, backoff->end);
3997 BUG_ON(p != end);
3998
3999 msg->front.iov_len = p - msg->front.iov_base;
4000 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4001 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4002
4003 return msg;
4004}
4005
4006static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4007{
4008 struct ceph_spg_mapping *spg;
4009 struct ceph_osd_backoff *backoff;
4010 struct ceph_msg *msg;
4011
4012 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4013 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4014
4015 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4016 if (!spg) {
4017 spg = alloc_spg_mapping();
4018 if (!spg) {
4019 pr_err("%s failed to allocate spg\n", __func__);
4020 return;
4021 }
4022 spg->spgid = m->spgid; /* struct */
4023 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4024 }
4025
4026 backoff = alloc_backoff();
4027 if (!backoff) {
4028 pr_err("%s failed to allocate backoff\n", __func__);
4029 return;
4030 }
4031 backoff->spgid = m->spgid; /* struct */
4032 backoff->id = m->id;
4033 backoff->begin = m->begin;
4034 m->begin = NULL; /* backoff now owns this */
4035 backoff->end = m->end;
4036 m->end = NULL; /* ditto */
4037
4038 insert_backoff(&spg->backoffs, backoff);
4039 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4040
4041 /*
4042 * Ack with original backoff's epoch so that the OSD can
4043 * discard this if there was a PG split.
4044 */
4045 msg = create_backoff_message(backoff, m->map_epoch);
4046 if (!msg) {
4047 pr_err("%s failed to allocate msg\n", __func__);
4048 return;
4049 }
4050 ceph_con_send(&osd->o_con, msg);
4051}
4052
4053static bool target_contained_by(const struct ceph_osd_request_target *t,
4054 const struct ceph_hobject_id *begin,
4055 const struct ceph_hobject_id *end)
4056{
4057 struct ceph_hobject_id hoid;
4058 int cmp;
4059
4060 hoid_fill_from_target(&hoid, t);
4061 cmp = hoid_compare(&hoid, begin);
4062 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4063}
4064
4065static void handle_backoff_unblock(struct ceph_osd *osd,
4066 const struct MOSDBackoff *m)
4067{
4068 struct ceph_spg_mapping *spg;
4069 struct ceph_osd_backoff *backoff;
4070 struct rb_node *n;
4071
4072 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4073 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4074
4075 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4076 if (!backoff) {
4077 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4078 __func__, osd->o_osd, m->spgid.pgid.pool,
4079 m->spgid.pgid.seed, m->spgid.shard, m->id);
4080 return;
4081 }
4082
4083 if (hoid_compare(backoff->begin, m->begin) &&
4084 hoid_compare(backoff->end, m->end)) {
4085 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4086 __func__, osd->o_osd, m->spgid.pgid.pool,
4087 m->spgid.pgid.seed, m->spgid.shard, m->id);
4088 /* unblock it anyway... */
4089 }
4090
4091 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4092 BUG_ON(!spg);
4093
4094 erase_backoff(&spg->backoffs, backoff);
4095 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4096 free_backoff(backoff);
4097
4098 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4099 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4100 free_spg_mapping(spg);
4101 }
4102
4103 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4104 struct ceph_osd_request *req =
4105 rb_entry(n, struct ceph_osd_request, r_node);
4106
4107 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4108 /*
4109 * Match against @m, not @backoff -- the PG may
4110 * have split on the OSD.
4111 */
4112 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4113 /*
4114 * If no other installed backoff applies,
4115 * resend.
4116 */
4117 send_request(req);
4118 }
4119 }
4120 }
4121}
4122
4123static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4124{
4125 struct ceph_osd_client *osdc = osd->o_osdc;
4126 struct MOSDBackoff m;
4127 int ret;
4128
4129 down_read(&osdc->lock);
4130 if (!osd_registered(osd)) {
4131 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4132 up_read(&osdc->lock);
4133 return;
4134 }
4135 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4136
4137 mutex_lock(&osd->lock);
4138 ret = decode_MOSDBackoff(msg, &m);
4139 if (ret) {
4140 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4141 ceph_msg_dump(msg);
4142 goto out_unlock;
4143 }
4144
4145 switch (m.op) {
4146 case CEPH_OSD_BACKOFF_OP_BLOCK:
4147 handle_backoff_block(osd, &m);
4148 break;
4149 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4150 handle_backoff_unblock(osd, &m);
4151 break;
4152 default:
4153 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4154 }
4155
4156 free_hoid(m.begin);
4157 free_hoid(m.end);
4158
4159out_unlock:
4160 mutex_unlock(&osd->lock);
4161 up_read(&osdc->lock);
4162}
4163
Sage Weilf24e9982009-10-06 11:31:10 -07004164/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004165 * Process osd watch notifications
4166 */
Alex Elder3c663bb2013-02-15 11:42:30 -06004167static void handle_watch_notify(struct ceph_osd_client *osdc,
4168 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004169{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004170 void *p = msg->front.iov_base;
4171 void *const end = p + msg->front.iov_len;
4172 struct ceph_osd_linger_request *lreq;
4173 struct linger_work *lwork;
4174 u8 proto_ver, opcode;
4175 u64 cookie, notify_id;
4176 u64 notifier_id = 0;
Ilya Dryomov19079202016-04-28 16:07:27 +02004177 s32 return_code = 0;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004178 void *payload = NULL;
4179 u32 payload_len = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004180
4181 ceph_decode_8_safe(&p, end, proto_ver, bad);
4182 ceph_decode_8_safe(&p, end, opcode, bad);
4183 ceph_decode_64_safe(&p, end, cookie, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004184 p += 8; /* skip ver */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004185 ceph_decode_64_safe(&p, end, notify_id, bad);
4186
Ilya Dryomov922dab62016-05-26 01:15:02 +02004187 if (proto_ver >= 1) {
4188 ceph_decode_32_safe(&p, end, payload_len, bad);
4189 ceph_decode_need(&p, end, payload_len, bad);
4190 payload = p;
4191 p += payload_len;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004192 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004193
4194 if (le16_to_cpu(msg->hdr.version) >= 2)
Ilya Dryomov19079202016-04-28 16:07:27 +02004195 ceph_decode_32_safe(&p, end, return_code, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004196
4197 if (le16_to_cpu(msg->hdr.version) >= 3)
4198 ceph_decode_64_safe(&p, end, notifier_id, bad);
4199
4200 down_read(&osdc->lock);
4201 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4202 if (!lreq) {
4203 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4204 cookie);
4205 goto out_unlock_osdc;
4206 }
4207
4208 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02004209 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4210 opcode, cookie, lreq, lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004211 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4212 if (!lreq->last_error) {
4213 lreq->last_error = -ENOTCONN;
4214 queue_watch_error(lreq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004215 }
Ilya Dryomov19079202016-04-28 16:07:27 +02004216 } else if (!lreq->is_watch) {
4217 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4218 if (lreq->notify_id && lreq->notify_id != notify_id) {
4219 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4220 lreq->notify_id, notify_id);
4221 } else if (!completion_done(&lreq->notify_finish_wait)) {
4222 struct ceph_msg_data *data =
4223 list_first_entry_or_null(&msg->data,
4224 struct ceph_msg_data,
4225 links);
4226
4227 if (data) {
4228 if (lreq->preply_pages) {
4229 WARN_ON(data->type !=
4230 CEPH_MSG_DATA_PAGES);
4231 *lreq->preply_pages = data->pages;
4232 *lreq->preply_len = data->length;
4233 } else {
4234 ceph_release_page_vector(data->pages,
4235 calc_pages_for(0, data->length));
4236 }
4237 }
4238 lreq->notify_finish_error = return_code;
4239 complete_all(&lreq->notify_finish_wait);
4240 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02004241 } else {
4242 /* CEPH_WATCH_EVENT_NOTIFY */
4243 lwork = lwork_alloc(lreq, do_watch_notify);
4244 if (!lwork) {
4245 pr_err("failed to allocate notify-lwork\n");
4246 goto out_unlock_lreq;
4247 }
Ilya Dryomov91883cd2014-09-11 12:18:53 +04004248
Ilya Dryomov922dab62016-05-26 01:15:02 +02004249 lwork->notify.notify_id = notify_id;
4250 lwork->notify.notifier_id = notifier_id;
4251 lwork->notify.payload = payload;
4252 lwork->notify.payload_len = payload_len;
4253 lwork->notify.msg = ceph_msg_get(msg);
4254 lwork_queue(lwork);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004255 }
4256
Ilya Dryomov922dab62016-05-26 01:15:02 +02004257out_unlock_lreq:
4258 mutex_unlock(&lreq->lock);
4259out_unlock_osdc:
4260 up_read(&osdc->lock);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004261 return;
4262
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004263bad:
4264 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004265}
4266
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004267/*
Sage Weilf24e9982009-10-06 11:31:10 -07004268 * Register request, send initial attempt.
4269 */
4270int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4271 struct ceph_osd_request *req,
4272 bool nofail)
4273{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004274 down_read(&osdc->lock);
4275 submit_request(req, false);
4276 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004277
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004278 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07004279}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004280EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07004281
4282/*
Ilya Dryomovc297eb422016-12-02 14:01:55 +01004283 * Unregister a registered request. The request is not completed:
4284 * ->r_result isn't set and __complete_request() isn't called.
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04004285 */
4286void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4287{
4288 struct ceph_osd_client *osdc = req->r_osdc;
4289
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004290 down_write(&osdc->lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004291 if (req->r_osd)
4292 cancel_request(req);
4293 up_write(&osdc->lock);
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04004294}
4295EXPORT_SYMBOL(ceph_osdc_cancel_request);
4296
4297/*
Ilya Dryomov42b06962016-04-28 16:07:26 +02004298 * @timeout: in jiffies, 0 means "wait forever"
4299 */
4300static int wait_request_timeout(struct ceph_osd_request *req,
4301 unsigned long timeout)
4302{
4303 long left;
4304
4305 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Yan, Zheng0e76abf2016-05-13 11:04:33 +08004306 left = wait_for_completion_killable_timeout(&req->r_completion,
Ilya Dryomov42b06962016-04-28 16:07:26 +02004307 ceph_timeout_jiffies(timeout));
4308 if (left <= 0) {
4309 left = left ?: -ETIMEDOUT;
4310 ceph_osdc_cancel_request(req);
Ilya Dryomov42b06962016-04-28 16:07:26 +02004311 } else {
4312 left = req->r_result; /* completed */
4313 }
4314
4315 return left;
4316}
4317
4318/*
Sage Weilf24e9982009-10-06 11:31:10 -07004319 * wait for a request to complete
4320 */
4321int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4322 struct ceph_osd_request *req)
4323{
Ilya Dryomov42b06962016-04-28 16:07:26 +02004324 return wait_request_timeout(req, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07004325}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004326EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07004327
4328/*
4329 * sync - wait for all in-flight requests to flush. avoid starvation.
4330 */
4331void ceph_osdc_sync(struct ceph_osd_client *osdc)
4332{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004333 struct rb_node *n, *p;
4334 u64 last_tid = atomic64_read(&osdc->last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004335
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004336again:
4337 down_read(&osdc->lock);
4338 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4339 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07004340
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004341 mutex_lock(&osd->lock);
4342 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4343 struct ceph_osd_request *req =
4344 rb_entry(p, struct ceph_osd_request, r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07004345
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004346 if (req->r_tid > last_tid)
4347 break;
4348
4349 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4350 continue;
4351
4352 ceph_osdc_get_request(req);
4353 mutex_unlock(&osd->lock);
4354 up_read(&osdc->lock);
4355 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4356 __func__, req, req->r_tid, last_tid);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01004357 wait_for_completion(&req->r_completion);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004358 ceph_osdc_put_request(req);
4359 goto again;
4360 }
4361
4362 mutex_unlock(&osd->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004363 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004364
4365 up_read(&osdc->lock);
4366 dout("%s done last_tid %llu\n", __func__, last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004367}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004368EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07004369
Ilya Dryomov922dab62016-05-26 01:15:02 +02004370static struct ceph_osd_request *
4371alloc_linger_request(struct ceph_osd_linger_request *lreq)
4372{
4373 struct ceph_osd_request *req;
4374
4375 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4376 if (!req)
4377 return NULL;
4378
4379 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4380 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4381
4382 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4383 ceph_osdc_put_request(req);
4384 return NULL;
4385 }
4386
4387 return req;
4388}
4389
4390/*
4391 * Returns a handle, caller owns a ref.
4392 */
4393struct ceph_osd_linger_request *
4394ceph_osdc_watch(struct ceph_osd_client *osdc,
4395 struct ceph_object_id *oid,
4396 struct ceph_object_locator *oloc,
4397 rados_watchcb2_t wcb,
4398 rados_watcherrcb_t errcb,
4399 void *data)
4400{
4401 struct ceph_osd_linger_request *lreq;
4402 int ret;
4403
4404 lreq = linger_alloc(osdc);
4405 if (!lreq)
4406 return ERR_PTR(-ENOMEM);
4407
Ilya Dryomov19079202016-04-28 16:07:27 +02004408 lreq->is_watch = true;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004409 lreq->wcb = wcb;
4410 lreq->errcb = errcb;
4411 lreq->data = data;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004412 lreq->watch_valid_thru = jiffies;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004413
4414 ceph_oid_copy(&lreq->t.base_oid, oid);
4415 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004416 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07004417 ktime_get_real_ts(&lreq->mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004418
4419 lreq->reg_req = alloc_linger_request(lreq);
4420 if (!lreq->reg_req) {
4421 ret = -ENOMEM;
4422 goto err_put_lreq;
4423 }
4424
4425 lreq->ping_req = alloc_linger_request(lreq);
4426 if (!lreq->ping_req) {
4427 ret = -ENOMEM;
4428 goto err_put_lreq;
4429 }
4430
4431 down_write(&osdc->lock);
4432 linger_register(lreq); /* before osd_req_op_* */
4433 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4434 CEPH_OSD_WATCH_OP_WATCH);
4435 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4436 CEPH_OSD_WATCH_OP_PING);
4437 linger_submit(lreq);
4438 up_write(&osdc->lock);
4439
4440 ret = linger_reg_commit_wait(lreq);
4441 if (ret) {
4442 linger_cancel(lreq);
4443 goto err_put_lreq;
4444 }
4445
4446 return lreq;
4447
4448err_put_lreq:
4449 linger_put(lreq);
4450 return ERR_PTR(ret);
4451}
4452EXPORT_SYMBOL(ceph_osdc_watch);
4453
4454/*
4455 * Releases a ref.
4456 *
4457 * Times out after mount_timeout to preserve rbd unmap behaviour
4458 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4459 * with mount_timeout").
4460 */
4461int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4462 struct ceph_osd_linger_request *lreq)
4463{
4464 struct ceph_options *opts = osdc->client->options;
4465 struct ceph_osd_request *req;
4466 int ret;
4467
4468 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4469 if (!req)
4470 return -ENOMEM;
4471
4472 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4473 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004474 req->r_flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07004475 ktime_get_real_ts(&req->r_mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004476 osd_req_op_watch_init(req, 0, lreq->linger_id,
4477 CEPH_OSD_WATCH_OP_UNWATCH);
4478
4479 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4480 if (ret)
4481 goto out_put_req;
4482
4483 ceph_osdc_start_request(osdc, req, false);
4484 linger_cancel(lreq);
4485 linger_put(lreq);
4486 ret = wait_request_timeout(req, opts->mount_timeout);
4487
4488out_put_req:
4489 ceph_osdc_put_request(req);
4490 return ret;
4491}
4492EXPORT_SYMBOL(ceph_osdc_unwatch);
4493
4494static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4495 u64 notify_id, u64 cookie, void *payload,
4496 size_t payload_len)
4497{
4498 struct ceph_osd_req_op *op;
4499 struct ceph_pagelist *pl;
4500 int ret;
4501
4502 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4503
4504 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4505 if (!pl)
4506 return -ENOMEM;
4507
4508 ceph_pagelist_init(pl);
4509 ret = ceph_pagelist_encode_64(pl, notify_id);
4510 ret |= ceph_pagelist_encode_64(pl, cookie);
4511 if (payload) {
4512 ret |= ceph_pagelist_encode_32(pl, payload_len);
4513 ret |= ceph_pagelist_append(pl, payload, payload_len);
4514 } else {
4515 ret |= ceph_pagelist_encode_32(pl, 0);
4516 }
4517 if (ret) {
4518 ceph_pagelist_release(pl);
4519 return -ENOMEM;
4520 }
4521
4522 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4523 op->indata_len = pl->length;
4524 return 0;
4525}
4526
4527int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4528 struct ceph_object_id *oid,
4529 struct ceph_object_locator *oloc,
4530 u64 notify_id,
4531 u64 cookie,
4532 void *payload,
4533 size_t payload_len)
4534{
4535 struct ceph_osd_request *req;
4536 int ret;
4537
4538 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4539 if (!req)
4540 return -ENOMEM;
4541
4542 ceph_oid_copy(&req->r_base_oid, oid);
4543 ceph_oloc_copy(&req->r_base_oloc, oloc);
4544 req->r_flags = CEPH_OSD_FLAG_READ;
4545
4546 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4547 if (ret)
4548 goto out_put_req;
4549
4550 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4551 payload_len);
4552 if (ret)
4553 goto out_put_req;
4554
4555 ceph_osdc_start_request(osdc, req, false);
4556 ret = ceph_osdc_wait_request(osdc, req);
4557
4558out_put_req:
4559 ceph_osdc_put_request(req);
4560 return ret;
4561}
4562EXPORT_SYMBOL(ceph_osdc_notify_ack);
4563
Ilya Dryomov19079202016-04-28 16:07:27 +02004564static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4565 u64 cookie, u32 prot_ver, u32 timeout,
4566 void *payload, size_t payload_len)
4567{
4568 struct ceph_osd_req_op *op;
4569 struct ceph_pagelist *pl;
4570 int ret;
4571
4572 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4573 op->notify.cookie = cookie;
4574
4575 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4576 if (!pl)
4577 return -ENOMEM;
4578
4579 ceph_pagelist_init(pl);
4580 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4581 ret |= ceph_pagelist_encode_32(pl, timeout);
4582 ret |= ceph_pagelist_encode_32(pl, payload_len);
4583 ret |= ceph_pagelist_append(pl, payload, payload_len);
4584 if (ret) {
4585 ceph_pagelist_release(pl);
4586 return -ENOMEM;
4587 }
4588
4589 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4590 op->indata_len = pl->length;
4591 return 0;
4592}
4593
4594/*
4595 * @timeout: in seconds
4596 *
4597 * @preply_{pages,len} are initialized both on success and error.
4598 * The caller is responsible for:
4599 *
4600 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4601 */
4602int ceph_osdc_notify(struct ceph_osd_client *osdc,
4603 struct ceph_object_id *oid,
4604 struct ceph_object_locator *oloc,
4605 void *payload,
4606 size_t payload_len,
4607 u32 timeout,
4608 struct page ***preply_pages,
4609 size_t *preply_len)
4610{
4611 struct ceph_osd_linger_request *lreq;
4612 struct page **pages;
4613 int ret;
4614
4615 WARN_ON(!timeout);
4616 if (preply_pages) {
4617 *preply_pages = NULL;
4618 *preply_len = 0;
4619 }
4620
4621 lreq = linger_alloc(osdc);
4622 if (!lreq)
4623 return -ENOMEM;
4624
4625 lreq->preply_pages = preply_pages;
4626 lreq->preply_len = preply_len;
4627
4628 ceph_oid_copy(&lreq->t.base_oid, oid);
4629 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4630 lreq->t.flags = CEPH_OSD_FLAG_READ;
4631
4632 lreq->reg_req = alloc_linger_request(lreq);
4633 if (!lreq->reg_req) {
4634 ret = -ENOMEM;
4635 goto out_put_lreq;
4636 }
4637
4638 /* for notify_id */
4639 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4640 if (IS_ERR(pages)) {
4641 ret = PTR_ERR(pages);
4642 goto out_put_lreq;
4643 }
4644
4645 down_write(&osdc->lock);
4646 linger_register(lreq); /* before osd_req_op_* */
4647 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4648 timeout, payload, payload_len);
4649 if (ret) {
4650 linger_unregister(lreq);
4651 up_write(&osdc->lock);
4652 ceph_release_page_vector(pages, 1);
4653 goto out_put_lreq;
4654 }
4655 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4656 response_data),
4657 pages, PAGE_SIZE, 0, false, true);
4658 linger_submit(lreq);
4659 up_write(&osdc->lock);
4660
4661 ret = linger_reg_commit_wait(lreq);
4662 if (!ret)
4663 ret = linger_notify_finish_wait(lreq);
4664 else
4665 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4666
4667 linger_cancel(lreq);
4668out_put_lreq:
4669 linger_put(lreq);
4670 return ret;
4671}
4672EXPORT_SYMBOL(ceph_osdc_notify);
4673
Sage Weilf24e9982009-10-06 11:31:10 -07004674/*
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004675 * Return the number of milliseconds since the watch was last
4676 * confirmed, or an error. If there is an error, the watch is no
4677 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4678 */
4679int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4680 struct ceph_osd_linger_request *lreq)
4681{
4682 unsigned long stamp, age;
4683 int ret;
4684
4685 down_read(&osdc->lock);
4686 mutex_lock(&lreq->lock);
4687 stamp = lreq->watch_valid_thru;
4688 if (!list_empty(&lreq->pending_lworks)) {
4689 struct linger_work *lwork =
4690 list_first_entry(&lreq->pending_lworks,
4691 struct linger_work,
4692 pending_item);
4693
4694 if (time_before(lwork->queued_stamp, stamp))
4695 stamp = lwork->queued_stamp;
4696 }
4697 age = jiffies - stamp;
4698 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4699 lreq, lreq->linger_id, age, lreq->last_error);
4700 /* we are truncating to msecs, so return a safe upper bound */
4701 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4702
4703 mutex_unlock(&lreq->lock);
4704 up_read(&osdc->lock);
4705 return ret;
4706}
4707
Douglas Fullera4ed38d2015-07-17 13:18:07 -07004708static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4709{
4710 u8 struct_v;
4711 u32 struct_len;
4712 int ret;
4713
4714 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4715 &struct_v, &struct_len);
4716 if (ret)
4717 return ret;
4718
4719 ceph_decode_copy(p, &item->name, sizeof(item->name));
4720 item->cookie = ceph_decode_64(p);
4721 *p += 4; /* skip timeout_seconds */
4722 if (struct_v >= 2) {
4723 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4724 ceph_decode_addr(&item->addr);
4725 }
4726
4727 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4728 ENTITY_NAME(item->name), item->cookie,
4729 ceph_pr_addr(&item->addr.in_addr));
4730 return 0;
4731}
4732
4733static int decode_watchers(void **p, void *end,
4734 struct ceph_watch_item **watchers,
4735 u32 *num_watchers)
4736{
4737 u8 struct_v;
4738 u32 struct_len;
4739 int i;
4740 int ret;
4741
4742 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4743 &struct_v, &struct_len);
4744 if (ret)
4745 return ret;
4746
4747 *num_watchers = ceph_decode_32(p);
4748 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4749 if (!*watchers)
4750 return -ENOMEM;
4751
4752 for (i = 0; i < *num_watchers; i++) {
4753 ret = decode_watcher(p, end, *watchers + i);
4754 if (ret) {
4755 kfree(*watchers);
4756 return ret;
4757 }
4758 }
4759
4760 return 0;
4761}
4762
4763/*
4764 * On success, the caller is responsible for:
4765 *
4766 * kfree(watchers);
4767 */
4768int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4769 struct ceph_object_id *oid,
4770 struct ceph_object_locator *oloc,
4771 struct ceph_watch_item **watchers,
4772 u32 *num_watchers)
4773{
4774 struct ceph_osd_request *req;
4775 struct page **pages;
4776 int ret;
4777
4778 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4779 if (!req)
4780 return -ENOMEM;
4781
4782 ceph_oid_copy(&req->r_base_oid, oid);
4783 ceph_oloc_copy(&req->r_base_oloc, oloc);
4784 req->r_flags = CEPH_OSD_FLAG_READ;
4785
4786 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4787 if (ret)
4788 goto out_put_req;
4789
4790 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4791 if (IS_ERR(pages)) {
4792 ret = PTR_ERR(pages);
4793 goto out_put_req;
4794 }
4795
4796 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4797 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4798 response_data),
4799 pages, PAGE_SIZE, 0, false, true);
4800
4801 ceph_osdc_start_request(osdc, req, false);
4802 ret = ceph_osdc_wait_request(osdc, req);
4803 if (ret >= 0) {
4804 void *p = page_address(pages[0]);
4805 void *const end = p + req->r_ops[0].outdata_len;
4806
4807 ret = decode_watchers(&p, end, watchers, num_watchers);
4808 }
4809
4810out_put_req:
4811 ceph_osdc_put_request(req);
4812 return ret;
4813}
4814EXPORT_SYMBOL(ceph_osdc_list_watchers);
4815
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004816/*
Josh Durgindd935f42013-08-28 21:43:09 -07004817 * Call all pending notify callbacks - for use after a watch is
4818 * unregistered, to make sure no more callbacks for it will be invoked
4819 */
stephen hemmingerf64794492014-06-10 20:30:13 -07004820void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07004821{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004822 dout("%s osdc %p\n", __func__, osdc);
Josh Durgindd935f42013-08-28 21:43:09 -07004823 flush_workqueue(osdc->notify_wq);
4824}
4825EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4826
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02004827void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4828{
4829 down_read(&osdc->lock);
4830 maybe_request_map(osdc);
4831 up_read(&osdc->lock);
4832}
4833EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
Josh Durgindd935f42013-08-28 21:43:09 -07004834
4835/*
Douglas Fuller428a7152015-06-17 14:49:45 -04004836 * Execute an OSD class method on an object.
4837 *
4838 * @flags: CEPH_OSD_FLAG_*
Ilya Dryomov2544a022017-01-25 18:16:21 +01004839 * @resp_len: in/out param for reply length
Douglas Fuller428a7152015-06-17 14:49:45 -04004840 */
4841int ceph_osdc_call(struct ceph_osd_client *osdc,
4842 struct ceph_object_id *oid,
4843 struct ceph_object_locator *oloc,
4844 const char *class, const char *method,
4845 unsigned int flags,
4846 struct page *req_page, size_t req_len,
4847 struct page *resp_page, size_t *resp_len)
4848{
4849 struct ceph_osd_request *req;
4850 int ret;
4851
Ilya Dryomov2544a022017-01-25 18:16:21 +01004852 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4853 return -E2BIG;
4854
Douglas Fuller428a7152015-06-17 14:49:45 -04004855 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4856 if (!req)
4857 return -ENOMEM;
4858
4859 ceph_oid_copy(&req->r_base_oid, oid);
4860 ceph_oloc_copy(&req->r_base_oloc, oloc);
4861 req->r_flags = flags;
4862
4863 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4864 if (ret)
4865 goto out_put_req;
4866
4867 osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4868 if (req_page)
4869 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4870 0, false, false);
4871 if (resp_page)
4872 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
Ilya Dryomov2544a022017-01-25 18:16:21 +01004873 *resp_len, 0, false, false);
Douglas Fuller428a7152015-06-17 14:49:45 -04004874
4875 ceph_osdc_start_request(osdc, req, false);
4876 ret = ceph_osdc_wait_request(osdc, req);
4877 if (ret >= 0) {
4878 ret = req->r_ops[0].rval;
4879 if (resp_page)
4880 *resp_len = req->r_ops[0].outdata_len;
4881 }
4882
4883out_put_req:
4884 ceph_osdc_put_request(req);
4885 return ret;
4886}
4887EXPORT_SYMBOL(ceph_osdc_call);
4888
4889/*
Sage Weilf24e9982009-10-06 11:31:10 -07004890 * init, shutdown
4891 */
4892int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4893{
4894 int err;
4895
4896 dout("init\n");
4897 osdc->client = client;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004898 init_rwsem(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004899 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004900 INIT_LIST_HEAD(&osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02004901 spin_lock_init(&osdc->osd_lru_lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004902 osd_init(&osdc->homeless_osd);
4903 osdc->homeless_osd.o_osdc = osdc;
4904 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
Ilya Dryomov264048a2016-11-08 15:15:24 +01004905 osdc->last_linger_id = CEPH_LINGER_ID_START;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004906 osdc->linger_requests = RB_ROOT;
Ilya Dryomov46092452016-04-28 16:07:27 +02004907 osdc->map_checks = RB_ROOT;
4908 osdc->linger_map_checks = RB_ROOT;
Sage Weilf24e9982009-10-06 11:31:10 -07004909 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004910 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4911
Sage Weil5f44f142009-11-18 14:52:18 -08004912 err = -ENOMEM;
Ilya Dryomove5253a72016-04-28 16:07:25 +02004913 osdc->osdmap = ceph_osdmap_alloc();
4914 if (!osdc->osdmap)
4915 goto out;
4916
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01004917 osdc->req_mempool = mempool_create_slab_pool(10,
4918 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07004919 if (!osdc->req_mempool)
Ilya Dryomove5253a72016-04-28 16:07:25 +02004920 goto out_map;
Sage Weilf24e9982009-10-06 11:31:10 -07004921
Sage Weild50b4092012-07-09 14:22:34 -07004922 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02004923 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07004924 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08004925 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07004926 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02004927 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08004928 if (err < 0)
4929 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004930
Dan Carpenterdbcae082013-08-15 08:58:59 +03004931 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004932 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03004933 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02004934 goto out_msgpool_reply;
4935
Ilya Dryomovfbca9632016-04-28 16:07:24 +02004936 schedule_delayed_work(&osdc->timeout_work,
4937 osdc->client->options->osd_keepalive_timeout);
Ilya Dryomovb37ee1b2016-04-28 16:07:24 +02004938 schedule_delayed_work(&osdc->osds_timeout_work,
4939 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4940
Sage Weilf24e9982009-10-06 11:31:10 -07004941 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08004942
Ilya Dryomovc172ec52014-01-31 17:49:22 +02004943out_msgpool_reply:
4944 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08004945out_msgpool:
4946 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08004947out_mempool:
4948 mempool_destroy(osdc->req_mempool);
Ilya Dryomove5253a72016-04-28 16:07:25 +02004949out_map:
4950 ceph_osdmap_destroy(osdc->osdmap);
Sage Weil5f44f142009-11-18 14:52:18 -08004951out:
4952 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07004953}
4954
4955void ceph_osdc_stop(struct ceph_osd_client *osdc)
4956{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004957 flush_workqueue(osdc->notify_wq);
4958 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07004959 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004960 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004961
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004962 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004963 while (!RB_EMPTY_ROOT(&osdc->osds)) {
4964 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4965 struct ceph_osd, o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004966 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004967 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004968 up_write(&osdc->lock);
Elena Reshetova02113a02017-03-17 14:10:28 +02004969 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004970 osd_cleanup(&osdc->homeless_osd);
4971
4972 WARN_ON(!list_empty(&osdc->osd_lru));
Ilya Dryomov922dab62016-05-26 01:15:02 +02004973 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
Ilya Dryomov46092452016-04-28 16:07:27 +02004974 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4975 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004976 WARN_ON(atomic_read(&osdc->num_requests));
4977 WARN_ON(atomic_read(&osdc->num_homeless));
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004978
Ilya Dryomove5253a72016-04-28 16:07:25 +02004979 ceph_osdmap_destroy(osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07004980 mempool_destroy(osdc->req_mempool);
4981 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08004982 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07004983}
4984
4985/*
4986 * Read some contiguous pages. If we cross a stripe boundary, shorten
4987 * *plen. Return number of bytes read, or error.
4988 */
4989int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4990 struct ceph_vino vino, struct ceph_file_layout *layout,
4991 u64 off, u64 *plen,
4992 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08004993 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07004994{
4995 struct ceph_osd_request *req;
4996 int rc = 0;
4997
4998 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4999 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08005000 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07005001 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05005002 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06005003 false);
Sage Weil68162822012-09-24 21:01:02 -07005004 if (IS_ERR(req))
5005 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07005006
5007 /* it may be a short read due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05005008 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05005009 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07005010
Alex Eldere0c59482013-03-07 15:38:25 -06005011 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05005012 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07005013
5014 rc = ceph_osdc_start_request(osdc, req, false);
5015 if (!rc)
5016 rc = ceph_osdc_wait_request(osdc, req);
5017
5018 ceph_osdc_put_request(req);
5019 dout("readpages result %d\n", rc);
5020 return rc;
5021}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005022EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07005023
5024/*
5025 * do a synchronous write on N pages
5026 */
5027int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5028 struct ceph_file_layout *layout,
5029 struct ceph_snap_context *snapc,
5030 u64 off, u64 len,
5031 u32 truncate_seq, u64 truncate_size,
5032 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06005033 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07005034{
5035 struct ceph_osd_request *req;
5036 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08005037 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07005038
Yan, Zheng715e4cd2014-11-13 14:40:37 +08005039 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01005040 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05005041 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06005042 true);
Sage Weil68162822012-09-24 21:01:02 -07005043 if (IS_ERR(req))
5044 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07005045
5046 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05005047 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05005048 false, false);
5049 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07005050
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02005051 req->r_mtime = *mtime;
Alex Elder87f979d2013-02-15 11:42:29 -06005052 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07005053 if (!rc)
5054 rc = ceph_osdc_wait_request(osdc, req);
5055
5056 ceph_osdc_put_request(req);
5057 if (rc == 0)
5058 rc = len;
5059 dout("writepages result %d\n", rc);
5060 return rc;
5061}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005062EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07005063
Alex Elder5522ae02013-05-01 12:43:04 -05005064int ceph_osdc_setup(void)
5065{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01005066 size_t size = sizeof(struct ceph_osd_request) +
5067 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5068
Alex Elder5522ae02013-05-01 12:43:04 -05005069 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01005070 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5071 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05005072
5073 return ceph_osd_request_cache ? 0 : -ENOMEM;
5074}
5075EXPORT_SYMBOL(ceph_osdc_setup);
5076
5077void ceph_osdc_cleanup(void)
5078{
5079 BUG_ON(!ceph_osd_request_cache);
5080 kmem_cache_destroy(ceph_osd_request_cache);
5081 ceph_osd_request_cache = NULL;
5082}
5083EXPORT_SYMBOL(ceph_osdc_cleanup);
5084
Sage Weilf24e9982009-10-06 11:31:10 -07005085/*
5086 * handle incoming message
5087 */
5088static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5089{
5090 struct ceph_osd *osd = con->private;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005091 struct ceph_osd_client *osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07005092 int type = le16_to_cpu(msg->hdr.type);
5093
Sage Weilf24e9982009-10-06 11:31:10 -07005094 switch (type) {
5095 case CEPH_MSG_OSD_MAP:
5096 ceph_osdc_handle_map(osdc, msg);
5097 break;
5098 case CEPH_MSG_OSD_OPREPLY:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005099 handle_reply(osd, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07005100 break;
Ilya Dryomova02a9462017-06-19 12:18:05 +02005101 case CEPH_MSG_OSD_BACKOFF:
5102 handle_backoff(osd, msg);
5103 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005104 case CEPH_MSG_WATCH_NOTIFY:
5105 handle_watch_notify(osdc, msg);
5106 break;
Sage Weilf24e9982009-10-06 11:31:10 -07005107
5108 default:
5109 pr_err("received unknown message type %d %s\n", type,
5110 ceph_msg_type_name(type));
5111 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005112
Sage Weilf24e9982009-10-06 11:31:10 -07005113 ceph_msg_put(msg);
5114}
5115
Sage Weil5b3a4db2010-02-19 21:43:23 -08005116/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005117 * Lookup and return message for incoming reply. Don't try to do
5118 * anything about a larger than preallocated data portion of the
5119 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08005120 */
5121static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08005122 struct ceph_msg_header *hdr,
5123 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07005124{
5125 struct ceph_osd *osd = con->private;
5126 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005127 struct ceph_msg *m = NULL;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005128 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02005129 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005130 int data_len = le32_to_cpu(hdr->data_len);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005131 u64 tid = le64_to_cpu(hdr->tid);
Sage Weilf24e9982009-10-06 11:31:10 -07005132
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005133 down_read(&osdc->lock);
5134 if (!osd_registered(osd)) {
5135 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5136 *skip = 1;
5137 goto out_unlock_osdc;
5138 }
5139 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5140
5141 mutex_lock(&osd->lock);
5142 req = lookup_request(&osd->o_requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005143 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01005144 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5145 osd->o_osd, tid);
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005146 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005147 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005148 }
Sage Weilc16e7862010-03-01 13:02:00 -08005149
Alex Elderace6d3a2013-04-01 16:12:14 -05005150 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08005151
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02005152 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005153 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5154 __func__, osd->o_osd, req->r_tid, front_len,
5155 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02005156 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5157 false);
Sage Weila79832f2010-04-01 16:06:19 -07005158 if (!m)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005159 goto out_unlock_session;
Sage Weilc16e7862010-03-01 13:02:00 -08005160 ceph_msg_put(req->r_reply);
5161 req->r_reply = m;
5162 }
Sage Weilc16e7862010-03-01 13:02:00 -08005163
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005164 if (data_len > req->r_reply->data_length) {
5165 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5166 __func__, osd->o_osd, req->r_tid, data_len,
5167 req->r_reply->data_length);
5168 m = NULL;
5169 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005170 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005171 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03005172
5173 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08005174 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08005175
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005176out_unlock_session:
5177 mutex_unlock(&osd->lock);
5178out_unlock_osdc:
5179 up_read(&osdc->lock);
Yehuda Sadeh24504182010-01-08 13:58:34 -08005180 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08005181}
5182
Ilya Dryomov19079202016-04-28 16:07:27 +02005183/*
5184 * TODO: switch to a msg-owned pagelist
5185 */
5186static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5187{
5188 struct ceph_msg *m;
5189 int type = le16_to_cpu(hdr->type);
5190 u32 front_len = le32_to_cpu(hdr->front_len);
5191 u32 data_len = le32_to_cpu(hdr->data_len);
5192
5193 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
5194 if (!m)
5195 return NULL;
5196
5197 if (data_len) {
5198 struct page **pages;
5199 struct ceph_osd_data osd_data;
5200
5201 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5202 GFP_NOIO);
Wei Yongjunc22e8532016-07-30 00:37:57 +00005203 if (IS_ERR(pages)) {
Ilya Dryomov19079202016-04-28 16:07:27 +02005204 ceph_msg_put(m);
5205 return NULL;
5206 }
5207
5208 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5209 false);
5210 ceph_osdc_msg_data_add(m, &osd_data);
5211 }
5212
5213 return m;
5214}
5215
Sage Weil5b3a4db2010-02-19 21:43:23 -08005216static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5217 struct ceph_msg_header *hdr,
5218 int *skip)
5219{
5220 struct ceph_osd *osd = con->private;
5221 int type = le16_to_cpu(hdr->type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005222
Alex Elder1c20f2d2012-06-04 14:43:32 -05005223 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08005224 switch (type) {
5225 case CEPH_MSG_OSD_MAP:
Ilya Dryomova02a9462017-06-19 12:18:05 +02005226 case CEPH_MSG_OSD_BACKOFF:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07005227 case CEPH_MSG_WATCH_NOTIFY:
Ilya Dryomov19079202016-04-28 16:07:27 +02005228 return alloc_msg_with_page_vector(hdr);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005229 case CEPH_MSG_OSD_OPREPLY:
5230 return get_reply(con, hdr, skip);
5231 default:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005232 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5233 osd->o_osd, type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08005234 *skip = 1;
5235 return NULL;
5236 }
Sage Weilf24e9982009-10-06 11:31:10 -07005237}
5238
5239/*
5240 * Wrappers to refcount containing ceph_osd struct
5241 */
5242static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5243{
5244 struct ceph_osd *osd = con->private;
5245 if (get_osd(osd))
5246 return con;
5247 return NULL;
5248}
5249
5250static void put_osd_con(struct ceph_connection *con)
5251{
5252 struct ceph_osd *osd = con->private;
5253 put_osd(osd);
5254}
5255
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005256/*
5257 * authentication
5258 */
Alex Eldera3530df2012-05-16 15:16:39 -05005259/*
5260 * Note: returned pointer is the address of a structure that's
5261 * managed separately. Caller must *not* attempt to free it.
5262 */
5263static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05005264 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005265{
5266 struct ceph_osd *o = con->private;
5267 struct ceph_osd_client *osdc = o->o_osdc;
5268 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05005269 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005270
Alex Elder74f18692012-05-16 15:16:39 -05005271 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02005272 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05005273 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005274 }
Sage Weil27859f92013-03-25 10:26:14 -07005275 if (!auth->authorizer) {
5276 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5277 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005278 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05005279 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07005280 } else {
5281 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07005282 auth);
5283 if (ret)
5284 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005285 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005286 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05005287
Alex Eldera3530df2012-05-16 15:16:39 -05005288 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005289}
5290
5291
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005292static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005293{
5294 struct ceph_osd *o = con->private;
5295 struct ceph_osd_client *osdc = o->o_osdc;
5296 struct ceph_auth_client *ac = osdc->client->monc.auth;
5297
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005298 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005299}
5300
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005301static int invalidate_authorizer(struct ceph_connection *con)
5302{
5303 struct ceph_osd *o = con->private;
5304 struct ceph_osd_client *osdc = o->o_osdc;
5305 struct ceph_auth_client *ac = osdc->client->monc.auth;
5306
Sage Weil27859f92013-03-25 10:26:14 -07005307 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005308 return ceph_monc_validate_auth(&osdc->client->monc);
5309}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005310
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005311static void osd_reencode_message(struct ceph_msg *msg)
5312{
5313 encode_request_finish(msg);
5314}
5315
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005316static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005317{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005318 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005319 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005320
Yan, Zheng33d07332014-11-04 16:33:37 +08005321 return ceph_auth_sign_message(auth, msg);
5322}
5323
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005324static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005325{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005326 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005327 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005328
Yan, Zheng33d07332014-11-04 16:33:37 +08005329 return ceph_auth_check_message_signature(auth, msg);
5330}
5331
Tobias Klauser9e327892010-05-20 10:40:19 +02005332static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07005333 .get = get_osd_con,
5334 .put = put_osd_con,
5335 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005336 .get_authorizer = get_authorizer,
5337 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005338 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07005339 .alloc_msg = alloc_msg,
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02005340 .reencode_message = osd_reencode_message,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005341 .sign_message = osd_sign_message,
5342 .check_message_signature = osd_check_message_signature,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02005343 .fault = osd_fault,
Sage Weilf24e9982009-10-06 11:31:10 -07005344};