blob: 924f07c36ddbc2864c7428723766d0a64729c7c4 [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
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/libceph.h>
16#include <linux/ceph/osd_client.h>
17#include <linux/ceph/messenger.h>
18#include <linux/ceph/decode.h>
19#include <linux/ceph/auth.h>
20#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070021
Sage Weilc16e7862010-03-01 13:02:00 -080022#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080023
Alex Elder5522ae02013-05-01 12:43:04 -050024static struct kmem_cache *ceph_osd_request_cache;
25
Tobias Klauser9e327892010-05-20 10:40:19 +020026static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070027
Sage Weilf24e9982009-10-06 11:31:10 -070028/*
29 * Implement client access to distributed object storage cluster.
30 *
31 * All data objects are stored within a cluster/cloud of OSDs, or
32 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
33 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
34 * remote daemons serving up and coordinating consistent and safe
35 * access to storage.
36 *
37 * Cluster membership and the mapping of data objects onto storage devices
38 * are described by the osd map.
39 *
40 * We keep track of pending OSD requests (read, write), resubmit
41 * requests to different OSDs when the cluster topology/data layout
42 * change, or retry the affected requests when the communications
43 * channel with an OSD is reset.
44 */
45
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020046static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
47static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
Ilya Dryomov922dab62016-05-26 01:15:02 +020048static void link_linger(struct ceph_osd *osd,
49 struct ceph_osd_linger_request *lreq);
50static void unlink_linger(struct ceph_osd *osd,
51 struct ceph_osd_linger_request *lreq);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020052
53#if 1
54static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
55{
56 bool wrlocked = true;
57
58 if (unlikely(down_read_trylock(sem))) {
59 wrlocked = false;
60 up_read(sem);
61 }
62
63 return wrlocked;
64}
65static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
66{
67 WARN_ON(!rwsem_is_locked(&osdc->lock));
68}
69static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
70{
71 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
72}
73static inline void verify_osd_locked(struct ceph_osd *osd)
74{
75 struct ceph_osd_client *osdc = osd->o_osdc;
76
77 WARN_ON(!(mutex_is_locked(&osd->lock) &&
78 rwsem_is_locked(&osdc->lock)) &&
79 !rwsem_is_wrlocked(&osdc->lock));
80}
Ilya Dryomov922dab62016-05-26 01:15:02 +020081static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
82{
83 WARN_ON(!mutex_is_locked(&lreq->lock));
84}
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020085#else
86static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
87static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
88static inline void verify_osd_locked(struct ceph_osd *osd) { }
Ilya Dryomov922dab62016-05-26 01:15:02 +020089static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020090#endif
91
Sage Weilf24e9982009-10-06 11:31:10 -070092/*
93 * calculate the mapping of a file extent onto an object, and fill out the
94 * request accordingly. shorten extent as necessary if it crosses an
95 * object boundary.
96 *
97 * fill osd op in request message.
98 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060099static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -0500100 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -0700101{
Alex Elder60e56f12013-02-15 11:42:29 -0600102 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -0700103 int r;
Sage Weilf24e9982009-10-06 11:31:10 -0700104
Alex Elder60e56f12013-02-15 11:42:29 -0600105 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -0500106 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
107 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -0700108 if (r < 0)
109 return r;
Alex Elder75d1c942013-03-13 20:50:00 -0500110 if (*objlen < orig_len) {
111 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -0600112 dout(" skipping last %llu, final file extent %llu~%llu\n",
113 orig_len - *plen, off, *plen);
114 }
115
Alex Elder75d1c942013-03-13 20:50:00 -0500116 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -0700117
Alex Elder3ff5f382013-02-15 22:10:17 -0600118 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700119}
120
Alex Elderc54d47b2013-04-03 01:28:57 -0500121static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
122{
123 memset(osd_data, 0, sizeof (*osd_data));
124 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
125}
126
Alex Eldera4ce40a2013-04-05 01:27:12 -0500127static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500128 struct page **pages, u64 length, u32 alignment,
129 bool pages_from_pool, bool own_pages)
130{
131 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
132 osd_data->pages = pages;
133 osd_data->length = length;
134 osd_data->alignment = alignment;
135 osd_data->pages_from_pool = pages_from_pool;
136 osd_data->own_pages = own_pages;
137}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500138
Alex Eldera4ce40a2013-04-05 01:27:12 -0500139static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500140 struct ceph_pagelist *pagelist)
141{
142 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
143 osd_data->pagelist = pagelist;
144}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500145
146#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500147static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500148 struct bio *bio, size_t bio_length)
149{
150 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
151 osd_data->bio = bio;
152 osd_data->bio_length = bio_length;
153}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500154#endif /* CONFIG_BLOCK */
155
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300156#define osd_req_op_data(oreq, whch, typ, fld) \
157({ \
158 struct ceph_osd_request *__oreq = (oreq); \
159 unsigned int __whch = (whch); \
160 BUG_ON(__whch >= __oreq->r_num_ops); \
161 &__oreq->r_ops[__whch].typ.fld; \
162})
Alex Elder863c7eb2013-04-15 14:50:36 -0500163
Alex Elder49719772013-02-11 12:33:24 -0600164static struct ceph_osd_data *
165osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
166{
167 BUG_ON(which >= osd_req->r_num_ops);
168
169 return &osd_req->r_ops[which].raw_data_in;
170}
171
Alex Eldera4ce40a2013-04-05 01:27:12 -0500172struct ceph_osd_data *
173osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500174 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500175{
Alex Elder863c7eb2013-04-15 14:50:36 -0500176 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500177}
178EXPORT_SYMBOL(osd_req_op_extent_osd_data);
179
Alex Elder49719772013-02-11 12:33:24 -0600180void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
181 unsigned int which, struct page **pages,
182 u64 length, u32 alignment,
183 bool pages_from_pool, bool own_pages)
184{
185 struct ceph_osd_data *osd_data;
186
187 osd_data = osd_req_op_raw_data_in(osd_req, which);
188 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
189 pages_from_pool, own_pages);
190}
191EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
192
Alex Eldera4ce40a2013-04-05 01:27:12 -0500193void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500194 unsigned int which, struct page **pages,
195 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500196 bool pages_from_pool, bool own_pages)
197{
198 struct ceph_osd_data *osd_data;
199
Alex Elder863c7eb2013-04-15 14:50:36 -0500200 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500201 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
202 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500203}
204EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
205
206void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500207 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500208{
209 struct ceph_osd_data *osd_data;
210
Alex Elder863c7eb2013-04-15 14:50:36 -0500211 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500212 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500213}
214EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
215
216#ifdef CONFIG_BLOCK
217void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500218 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500219{
220 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500221
222 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500223 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500224}
225EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
226#endif /* CONFIG_BLOCK */
227
228static void osd_req_op_cls_request_info_pagelist(
229 struct ceph_osd_request *osd_req,
230 unsigned int which, struct ceph_pagelist *pagelist)
231{
232 struct ceph_osd_data *osd_data;
233
Alex Elder863c7eb2013-04-15 14:50:36 -0500234 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500235 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236}
237
Alex Elder04017e22013-04-05 14:46:02 -0500238void osd_req_op_cls_request_data_pagelist(
239 struct ceph_osd_request *osd_req,
240 unsigned int which, struct ceph_pagelist *pagelist)
241{
242 struct ceph_osd_data *osd_data;
243
Alex Elder863c7eb2013-04-15 14:50:36 -0500244 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500245 ceph_osd_data_pagelist_init(osd_data, pagelist);
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200246 osd_req->r_ops[which].cls.indata_len += pagelist->length;
247 osd_req->r_ops[which].indata_len += pagelist->length;
Alex Elder04017e22013-04-05 14:46:02 -0500248}
249EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
250
Alex Elder6c57b552013-04-19 15:34:49 -0500251void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
252 unsigned int which, struct page **pages, u64 length,
253 u32 alignment, bool pages_from_pool, bool own_pages)
254{
255 struct ceph_osd_data *osd_data;
256
257 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
258 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
259 pages_from_pool, own_pages);
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200260 osd_req->r_ops[which].cls.indata_len += length;
261 osd_req->r_ops[which].indata_len += length;
Alex Elder6c57b552013-04-19 15:34:49 -0500262}
263EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
264
Alex Eldera4ce40a2013-04-05 01:27:12 -0500265void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
266 unsigned int which, struct page **pages, u64 length,
267 u32 alignment, bool pages_from_pool, bool own_pages)
268{
269 struct ceph_osd_data *osd_data;
270
Alex Elder863c7eb2013-04-15 14:50:36 -0500271 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500272 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
273 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500274}
275EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
276
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500277static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
278{
279 switch (osd_data->type) {
280 case CEPH_OSD_DATA_TYPE_NONE:
281 return 0;
282 case CEPH_OSD_DATA_TYPE_PAGES:
283 return osd_data->length;
284 case CEPH_OSD_DATA_TYPE_PAGELIST:
285 return (u64)osd_data->pagelist->length;
286#ifdef CONFIG_BLOCK
287 case CEPH_OSD_DATA_TYPE_BIO:
288 return (u64)osd_data->bio_length;
289#endif /* CONFIG_BLOCK */
290 default:
291 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
292 return 0;
293 }
294}
295
Alex Elderc54d47b2013-04-03 01:28:57 -0500296static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
297{
Alex Elder54764922013-04-05 01:27:12 -0500298 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500299 int num_pages;
300
301 num_pages = calc_pages_for((u64)osd_data->alignment,
302 (u64)osd_data->length);
303 ceph_release_page_vector(osd_data->pages, num_pages);
304 }
Alex Elder54764922013-04-05 01:27:12 -0500305 ceph_osd_data_init(osd_data);
306}
307
308static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
309 unsigned int which)
310{
311 struct ceph_osd_req_op *op;
312
313 BUG_ON(which >= osd_req->r_num_ops);
314 op = &osd_req->r_ops[which];
315
316 switch (op->op) {
317 case CEPH_OSD_OP_READ:
318 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200319 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500320 ceph_osd_data_release(&op->extent.osd_data);
321 break;
322 case CEPH_OSD_OP_CALL:
323 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500324 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500325 ceph_osd_data_release(&op->cls.response_data);
326 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800327 case CEPH_OSD_OP_SETXATTR:
328 case CEPH_OSD_OP_CMPXATTR:
329 ceph_osd_data_release(&op->xattr.osd_data);
330 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800331 case CEPH_OSD_OP_STAT:
332 ceph_osd_data_release(&op->raw_data_in);
333 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200334 case CEPH_OSD_OP_NOTIFY_ACK:
335 ceph_osd_data_release(&op->notify_ack.request_data);
336 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200337 case CEPH_OSD_OP_NOTIFY:
338 ceph_osd_data_release(&op->notify.request_data);
339 ceph_osd_data_release(&op->notify.response_data);
340 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700341 case CEPH_OSD_OP_LIST_WATCHERS:
342 ceph_osd_data_release(&op->list_watchers.response_data);
343 break;
Alex Elder54764922013-04-05 01:27:12 -0500344 default:
345 break;
346 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500347}
348
Sage Weilf24e9982009-10-06 11:31:10 -0700349/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200350 * Assumes @t is zero-initialized.
351 */
352static void target_init(struct ceph_osd_request_target *t)
353{
354 ceph_oid_init(&t->base_oid);
355 ceph_oloc_init(&t->base_oloc);
356 ceph_oid_init(&t->target_oid);
357 ceph_oloc_init(&t->target_oloc);
358
359 ceph_osds_init(&t->acting);
360 ceph_osds_init(&t->up);
361 t->size = -1;
362 t->min_size = -1;
363
364 t->osd = CEPH_HOMELESS_OSD;
365}
366
Ilya Dryomov922dab62016-05-26 01:15:02 +0200367static void target_copy(struct ceph_osd_request_target *dest,
368 const struct ceph_osd_request_target *src)
369{
370 ceph_oid_copy(&dest->base_oid, &src->base_oid);
371 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
372 ceph_oid_copy(&dest->target_oid, &src->target_oid);
373 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
374
375 dest->pgid = src->pgid; /* struct */
376 dest->pg_num = src->pg_num;
377 dest->pg_num_mask = src->pg_num_mask;
378 ceph_osds_copy(&dest->acting, &src->acting);
379 ceph_osds_copy(&dest->up, &src->up);
380 dest->size = src->size;
381 dest->min_size = src->min_size;
382 dest->sort_bitwise = src->sort_bitwise;
383
384 dest->flags = src->flags;
385 dest->paused = src->paused;
386
387 dest->osd = src->osd;
388}
389
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200390static void target_destroy(struct ceph_osd_request_target *t)
391{
392 ceph_oid_destroy(&t->base_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800393 ceph_oloc_destroy(&t->base_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200394 ceph_oid_destroy(&t->target_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800395 ceph_oloc_destroy(&t->target_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200396}
397
398/*
Sage Weilf24e9982009-10-06 11:31:10 -0700399 * requests
400 */
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200401static void request_release_checks(struct ceph_osd_request *req)
402{
403 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
Ilya Dryomov46092452016-04-28 16:07:27 +0200404 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200405 WARN_ON(!list_empty(&req->r_unsafe_item));
406 WARN_ON(req->r_osd);
407}
408
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400409static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700410{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400411 struct ceph_osd_request *req = container_of(kref,
412 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500413 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800414
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400415 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
416 req->r_request, req->r_reply);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200417 request_release_checks(req);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400418
Sage Weil415e49a2009-12-07 13:37:03 -0800419 if (req->r_request)
420 ceph_msg_put(req->r_request);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200421 if (req->r_reply)
Alex Elderab8cb342012-06-04 14:43:32 -0500422 ceph_msg_put(req->r_reply);
Alex Elder0fff87e2013-02-14 12:16:43 -0600423
Alex Elder54764922013-04-05 01:27:12 -0500424 for (which = 0; which < req->r_num_ops; which++)
425 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600426
Ilya Dryomova66dd382016-04-28 16:07:23 +0200427 target_destroy(&req->r_t);
Sage Weil415e49a2009-12-07 13:37:03 -0800428 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200429
Sage Weil415e49a2009-12-07 13:37:03 -0800430 if (req->r_mempool)
431 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100432 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500433 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100434 else
435 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700436}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400437
438void ceph_osdc_get_request(struct ceph_osd_request *req)
439{
440 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100441 kref_read(&req->r_kref));
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400442 kref_get(&req->r_kref);
443}
444EXPORT_SYMBOL(ceph_osdc_get_request);
445
446void ceph_osdc_put_request(struct ceph_osd_request *req)
447{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200448 if (req) {
449 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100450 kref_read(&req->r_kref));
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200451 kref_put(&req->r_kref, ceph_osdc_release_request);
452 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400453}
454EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700455
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200456static void request_init(struct ceph_osd_request *req)
457{
458 /* req only, each op is zeroed in _osd_req_op_init() */
459 memset(req, 0, sizeof(*req));
460
461 kref_init(&req->r_kref);
462 init_completion(&req->r_completion);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200463 RB_CLEAR_NODE(&req->r_node);
Ilya Dryomov46092452016-04-28 16:07:27 +0200464 RB_CLEAR_NODE(&req->r_mc_node);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200465 INIT_LIST_HEAD(&req->r_unsafe_item);
466
467 target_init(&req->r_t);
468}
469
Ilya Dryomov922dab62016-05-26 01:15:02 +0200470/*
471 * This is ugly, but it allows us to reuse linger registration and ping
472 * requests, keeping the structure of the code around send_linger{_ping}()
473 * reasonable. Setting up a min_nr=2 mempool for each linger request
474 * and dealing with copying ops (this blasts req only, watch op remains
475 * intact) isn't any better.
476 */
477static void request_reinit(struct ceph_osd_request *req)
478{
479 struct ceph_osd_client *osdc = req->r_osdc;
480 bool mempool = req->r_mempool;
481 unsigned int num_ops = req->r_num_ops;
482 u64 snapid = req->r_snapid;
483 struct ceph_snap_context *snapc = req->r_snapc;
484 bool linger = req->r_linger;
485 struct ceph_msg *request_msg = req->r_request;
486 struct ceph_msg *reply_msg = req->r_reply;
487
488 dout("%s req %p\n", __func__, req);
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100489 WARN_ON(kref_read(&req->r_kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200490 request_release_checks(req);
491
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100492 WARN_ON(kref_read(&request_msg->kref) != 1);
493 WARN_ON(kref_read(&reply_msg->kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200494 target_destroy(&req->r_t);
495
496 request_init(req);
497 req->r_osdc = osdc;
498 req->r_mempool = mempool;
499 req->r_num_ops = num_ops;
500 req->r_snapid = snapid;
501 req->r_snapc = snapc;
502 req->r_linger = linger;
503 req->r_request = request_msg;
504 req->r_reply = reply_msg;
505}
506
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700507struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700508 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800509 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700510 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600511 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700512{
513 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700514
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700515 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100516 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700517 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100518 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
519 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700520 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100521 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
522 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
523 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700524 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100525 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700526 return NULL;
527
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200528 request_init(req);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700529 req->r_osdc = osdc;
530 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500531 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200532 req->r_snapid = CEPH_NOSNAP;
533 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700534
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200535 dout("%s req %p\n", __func__, req);
536 return req;
537}
538EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100539
Yan, Zheng30c156d2016-02-14 11:24:31 +0800540static int ceph_oloc_encoding_size(struct ceph_object_locator *oloc)
541{
542 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
543}
544
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200545int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
546{
547 struct ceph_osd_client *osdc = req->r_osdc;
548 struct ceph_msg *msg;
549 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700550
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200551 WARN_ON(ceph_oid_empty(&req->r_base_oid));
Yan, Zheng30c156d2016-02-14 11:24:31 +0800552 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200553
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200554 /* create request message */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100555 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
556 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
Yan, Zheng30c156d2016-02-14 11:24:31 +0800557 msg_size += CEPH_ENCODING_START_BLK_LEN +
558 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100559 msg_size += 1 + 8 + 4 + 4; /* pgid */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200560 msg_size += 4 + req->r_base_oid.name_len; /* oid */
561 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100562 msg_size += 8; /* snapid */
563 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200564 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100565 msg_size += 4; /* retry_attempt */
566
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200567 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700568 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
569 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200570 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
571 if (!msg)
572 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700573
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700574 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700575 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700576
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200577 /* create reply message */
578 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200579 msg_size += req->r_base_oid.name_len;
580 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200581
582 if (req->r_mempool)
583 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
584 else
585 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
586 if (!msg)
587 return -ENOMEM;
588
589 req->r_reply = msg;
590
591 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700592}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200593EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700594
Alex Eldera8dd0a32013-03-13 20:50:00 -0500595static bool osd_req_opcode_valid(u16 opcode)
596{
597 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400598#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
599__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
600#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500601 default:
602 return false;
603 }
604}
605
Alex Elder33803f32013-03-13 20:50:00 -0500606/*
607 * This is an osd op init function for opcodes that have no data or
608 * other information associated with them. It also serves as a
609 * common init routine for all the other init functions, below.
610 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500611static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600612_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800613 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500614{
Alex Elderc99d2d42013-04-05 01:27:11 -0500615 struct ceph_osd_req_op *op;
616
617 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500618 BUG_ON(!osd_req_opcode_valid(opcode));
619
Alex Elderc99d2d42013-04-05 01:27:11 -0500620 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500621 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500622 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800623 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500624
625 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500626}
627
Alex Elder49719772013-02-11 12:33:24 -0600628void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800629 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600630{
Yan, Zheng144cba12015-04-27 11:09:54 +0800631 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600632}
633EXPORT_SYMBOL(osd_req_op_init);
634
Alex Elderc99d2d42013-04-05 01:27:11 -0500635void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
636 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500637 u64 offset, u64 length,
638 u64 truncate_size, u32 truncate_seq)
639{
Yan, Zheng144cba12015-04-27 11:09:54 +0800640 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
641 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500642 size_t payload_len = 0;
643
Li Wangad7a60d2013-08-15 11:51:44 +0800644 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200645 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
646 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500647
Alex Elder33803f32013-03-13 20:50:00 -0500648 op->extent.offset = offset;
649 op->extent.length = length;
650 op->extent.truncate_size = truncate_size;
651 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200652 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500653 payload_len += length;
654
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100655 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500656}
657EXPORT_SYMBOL(osd_req_op_extent_init);
658
Alex Elderc99d2d42013-04-05 01:27:11 -0500659void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
660 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500661{
Alex Elderc99d2d42013-04-05 01:27:11 -0500662 struct ceph_osd_req_op *op;
663 u64 previous;
664
665 BUG_ON(which >= osd_req->r_num_ops);
666 op = &osd_req->r_ops[which];
667 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500668
669 if (length == previous)
670 return; /* Nothing to do */
671 BUG_ON(length > previous);
672
673 op->extent.length = length;
Yan, Zhengd641df82017-01-19 11:21:29 +0800674 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
675 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500676}
677EXPORT_SYMBOL(osd_req_op_extent_update);
678
Yan, Zheng2c63f492016-01-07 17:32:54 +0800679void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
680 unsigned int which, u64 offset_inc)
681{
682 struct ceph_osd_req_op *op, *prev_op;
683
684 BUG_ON(which + 1 >= osd_req->r_num_ops);
685
686 prev_op = &osd_req->r_ops[which];
687 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
688 /* dup previous one */
689 op->indata_len = prev_op->indata_len;
690 op->outdata_len = prev_op->outdata_len;
691 op->extent = prev_op->extent;
692 /* adjust offset */
693 op->extent.offset += offset_inc;
694 op->extent.length -= offset_inc;
695
696 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
697 op->indata_len -= offset_inc;
698}
699EXPORT_SYMBOL(osd_req_op_extent_dup_last);
700
Alex Elderc99d2d42013-04-05 01:27:11 -0500701void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500702 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500703{
Yan, Zheng144cba12015-04-27 11:09:54 +0800704 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
705 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500706 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500707 size_t payload_len = 0;
708 size_t size;
709
710 BUG_ON(opcode != CEPH_OSD_OP_CALL);
711
Alex Elder5f562df2013-04-05 01:27:12 -0500712 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
713 BUG_ON(!pagelist);
714 ceph_pagelist_init(pagelist);
715
Alex Elder33803f32013-03-13 20:50:00 -0500716 op->cls.class_name = class;
717 size = strlen(class);
718 BUG_ON(size > (size_t) U8_MAX);
719 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500720 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500721 payload_len += size;
722
723 op->cls.method_name = method;
724 size = strlen(method);
725 BUG_ON(size > (size_t) U8_MAX);
726 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500727 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500728 payload_len += size;
729
Alex Eldera4ce40a2013-04-05 01:27:12 -0500730 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500731
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100732 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500733}
734EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500735
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800736int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
737 u16 opcode, const char *name, const void *value,
738 size_t size, u8 cmp_op, u8 cmp_mode)
739{
Yan, Zheng144cba12015-04-27 11:09:54 +0800740 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
741 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800742 struct ceph_pagelist *pagelist;
743 size_t payload_len;
744
745 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
746
747 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
748 if (!pagelist)
749 return -ENOMEM;
750
751 ceph_pagelist_init(pagelist);
752
753 payload_len = strlen(name);
754 op->xattr.name_len = payload_len;
755 ceph_pagelist_append(pagelist, name, payload_len);
756
757 op->xattr.value_len = size;
758 ceph_pagelist_append(pagelist, value, size);
759 payload_len += size;
760
761 op->xattr.cmp_op = cmp_op;
762 op->xattr.cmp_mode = cmp_mode;
763
764 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100765 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800766 return 0;
767}
768EXPORT_SYMBOL(osd_req_op_xattr_init);
769
Ilya Dryomov922dab62016-05-26 01:15:02 +0200770/*
771 * @watch_opcode: CEPH_OSD_WATCH_OP_*
772 */
773static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
774 u64 cookie, u8 watch_opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500775{
Ilya Dryomov922dab62016-05-26 01:15:02 +0200776 struct ceph_osd_req_op *op;
Alex Elder33803f32013-03-13 20:50:00 -0500777
Ilya Dryomov922dab62016-05-26 01:15:02 +0200778 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500779 op->watch.cookie = cookie;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200780 op->watch.op = watch_opcode;
781 op->watch.gen = 0;
Alex Elder33803f32013-03-13 20:50:00 -0500782}
Alex Elder33803f32013-03-13 20:50:00 -0500783
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200784void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
785 unsigned int which,
786 u64 expected_object_size,
787 u64 expected_write_size)
788{
789 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800790 CEPH_OSD_OP_SETALLOCHINT,
791 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200792
793 op->alloc_hint.expected_object_size = expected_object_size;
794 op->alloc_hint.expected_write_size = expected_write_size;
795
796 /*
797 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
798 * not worth a feature bit. Set FAILOK per-op flag to make
799 * sure older osds don't trip over an unsupported opcode.
800 */
801 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
802}
803EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
804
Alex Elder90af3602013-04-05 14:46:01 -0500805static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500806 struct ceph_osd_data *osd_data)
807{
808 u64 length = ceph_osd_data_length(osd_data);
809
810 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
811 BUG_ON(length > (u64) SIZE_MAX);
812 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500813 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500814 length, osd_data->alignment);
815 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
816 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500817 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500818#ifdef CONFIG_BLOCK
819 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500820 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500821#endif
822 } else {
823 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
824 }
825}
826
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200827static u32 osd_req_encode_op(struct ceph_osd_op *dst,
828 const struct ceph_osd_req_op *src)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700829{
Alex Eldera8dd0a32013-03-13 20:50:00 -0500830 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
831 pr_err("unrecognized osd opcode %d\n", src->op);
832
833 return 0;
834 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700835
Alex Elder065a68f2012-04-20 15:49:43 -0500836 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600837 case CEPH_OSD_OP_STAT:
838 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700839 case CEPH_OSD_OP_READ:
840 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200841 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800842 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800843 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600844 dst->extent.offset = cpu_to_le64(src->extent.offset);
845 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700846 dst->extent.truncate_size =
847 cpu_to_le64(src->extent.truncate_size);
848 dst->extent.truncate_seq =
849 cpu_to_le32(src->extent.truncate_seq);
850 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700851 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700852 dst->cls.class_len = src->cls.class_len;
853 dst->cls.method_len = src->cls.method_len;
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200854 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700855 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700856 case CEPH_OSD_OP_STARTSYNC:
857 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700858 case CEPH_OSD_OP_WATCH:
859 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200860 dst->watch.ver = cpu_to_le64(0);
861 dst->watch.op = src->watch.op;
862 dst->watch.gen = cpu_to_le32(src->watch.gen);
863 break;
864 case CEPH_OSD_OP_NOTIFY_ACK:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700865 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200866 case CEPH_OSD_OP_NOTIFY:
867 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
868 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700869 case CEPH_OSD_OP_LIST_WATCHERS:
870 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200871 case CEPH_OSD_OP_SETALLOCHINT:
872 dst->alloc_hint.expected_object_size =
873 cpu_to_le64(src->alloc_hint.expected_object_size);
874 dst->alloc_hint.expected_write_size =
875 cpu_to_le64(src->alloc_hint.expected_write_size);
876 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800877 case CEPH_OSD_OP_SETXATTR:
878 case CEPH_OSD_OP_CMPXATTR:
879 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
880 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
881 dst->xattr.cmp_op = src->xattr.cmp_op;
882 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800883 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800884 case CEPH_OSD_OP_CREATE:
885 case CEPH_OSD_OP_DELETE:
886 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700887 default:
Alex Elder4c464592013-02-15 11:42:30 -0600888 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600889 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600890 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500891
892 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700893 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200894
Alex Eldera8dd0a32013-03-13 20:50:00 -0500895 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200896 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100897 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600898
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200899 return src->indata_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700900}
901
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700902/*
Sage Weilf24e9982009-10-06 11:31:10 -0700903 * build new request AND message, calculate layout, and adjust file
904 * extent as needed.
905 *
906 * if the file was recently truncated, we include information about its
907 * old and new size so that the object can be updated appropriately. (we
908 * avoid synchronously deleting truncated objects because it's slow.)
909 *
910 * if @do_sync, include a 'startsync' command so that the osd will flush
911 * data quickly.
912 */
913struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
914 struct ceph_file_layout *layout,
915 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800916 u64 off, u64 *plen,
917 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700918 int opcode, int flags,
919 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700920 u32 truncate_seq,
921 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600922 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700923{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700924 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500925 u64 objnum = 0;
926 u64 objoff = 0;
927 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700928 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700929
Li Wangad7a60d2013-08-15 11:51:44 +0800930 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800931 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
932 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700933
Alex Elderacead002013-03-14 14:09:05 -0500934 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600935 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200936 if (!req) {
937 r = -ENOMEM;
938 goto fail;
939 }
Alex Elder79528732013-04-03 21:32:51 -0500940
Sage Weilf24e9982009-10-06 11:31:10 -0700941 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500942 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200943 if (r)
944 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500945
Yan, Zheng864e9192014-11-13 10:47:25 +0800946 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800947 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800948 } else {
Yan, Zheng76271512016-02-03 21:24:49 +0800949 u32 object_size = layout->object_size;
Yan, Zheng864e9192014-11-13 10:47:25 +0800950 u32 object_base = off - objoff;
951 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
952 if (truncate_size <= object_base) {
953 truncate_size = 0;
954 } else {
955 truncate_size -= object_base;
956 if (truncate_size > object_size)
957 truncate_size = object_size;
958 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800959 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800960 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800961 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500962 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500963
Jeff Laytona1f40202017-04-04 08:39:37 -0400964 req->r_abort_on_full = true;
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200965 req->r_flags = flags;
Yan, Zheng76271512016-02-03 21:24:49 +0800966 req->r_base_oloc.pool = layout->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +0800967 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200968 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600969
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200970 req->r_snapid = vino.snap;
971 if (flags & CEPH_OSD_FLAG_WRITE)
972 req->r_data_offset = off;
973
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200974 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
975 if (r)
976 goto fail;
977
Sage Weilf24e9982009-10-06 11:31:10 -0700978 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200979
980fail:
981 ceph_osdc_put_request(req);
982 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700983}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700984EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700985
986/*
987 * We keep osd requests in an rbtree, sorted by ->r_tid.
988 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200989DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Ilya Dryomov46092452016-04-28 16:07:27 +0200990DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
Sage Weilf24e9982009-10-06 11:31:10 -0700991
Ilya Dryomov0247a0c2016-04-28 16:07:25 +0200992static bool osd_homeless(struct ceph_osd *osd)
993{
994 return osd->o_osd == CEPH_HOMELESS_OSD;
995}
996
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200997static bool osd_registered(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700998{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200999 verify_osdc_locked(osd->o_osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001000
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001001 return !RB_EMPTY_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001002}
1003
1004/*
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001005 * Assumes @osd is zero-initialized.
1006 */
1007static void osd_init(struct ceph_osd *osd)
1008{
Elena Reshetova02113a02017-03-17 14:10:28 +02001009 refcount_set(&osd->o_ref, 1);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001010 RB_CLEAR_NODE(&osd->o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001011 osd->o_requests = RB_ROOT;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001012 osd->o_linger_requests = RB_ROOT;
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001013 INIT_LIST_HEAD(&osd->o_osd_lru);
1014 INIT_LIST_HEAD(&osd->o_keepalive_item);
1015 osd->o_incarnation = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001016 mutex_init(&osd->lock);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001017}
1018
1019static void osd_cleanup(struct ceph_osd *osd)
1020{
1021 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001022 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001023 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001024 WARN_ON(!list_empty(&osd->o_osd_lru));
1025 WARN_ON(!list_empty(&osd->o_keepalive_item));
1026
1027 if (osd->o_auth.authorizer) {
1028 WARN_ON(osd_homeless(osd));
1029 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1030 }
1031}
1032
1033/*
Sage Weilf24e9982009-10-06 11:31:10 -07001034 * Track open sessions with osds.
1035 */
Alex Eldere10006f2012-05-26 23:26:43 -05001036static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001037{
1038 struct ceph_osd *osd;
1039
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001040 WARN_ON(onum == CEPH_HOMELESS_OSD);
1041
Ilya Dryomov7a28f592016-04-28 16:07:25 +02001042 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001043 osd_init(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001044 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001045 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -07001046
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001047 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001048
Sage Weilf24e9982009-10-06 11:31:10 -07001049 return osd;
1050}
1051
1052static struct ceph_osd *get_osd(struct ceph_osd *osd)
1053{
Elena Reshetova02113a02017-03-17 14:10:28 +02001054 if (refcount_inc_not_zero(&osd->o_ref)) {
1055 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1056 refcount_read(&osd->o_ref));
Sage Weilf24e9982009-10-06 11:31:10 -07001057 return osd;
1058 } else {
1059 dout("get_osd %p FAIL\n", osd);
1060 return NULL;
1061 }
1062}
1063
1064static void put_osd(struct ceph_osd *osd)
1065{
Elena Reshetova02113a02017-03-17 14:10:28 +02001066 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1067 refcount_read(&osd->o_ref) - 1);
1068 if (refcount_dec_and_test(&osd->o_ref)) {
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001069 osd_cleanup(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001070 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001071 }
Sage Weilf24e9982009-10-06 11:31:10 -07001072}
1073
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001074DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1075
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001076static void __move_osd_to_lru(struct ceph_osd *osd)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001077{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001078 struct ceph_osd_client *osdc = osd->o_osdc;
1079
1080 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001081 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001082
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001083 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001084 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001085 spin_unlock(&osdc->osd_lru_lock);
1086
Ilya Dryomova319bf52015-05-15 12:02:17 +03001087 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001088}
1089
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001090static void maybe_move_osd_to_lru(struct ceph_osd *osd)
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001091{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001092 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001093 RB_EMPTY_ROOT(&osd->o_linger_requests))
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001094 __move_osd_to_lru(osd);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001095}
1096
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001097static void __remove_osd_from_lru(struct ceph_osd *osd)
1098{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001099 struct ceph_osd_client *osdc = osd->o_osdc;
1100
1101 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1102
1103 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001104 if (!list_empty(&osd->o_osd_lru))
1105 list_del_init(&osd->o_osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001106 spin_unlock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001107}
1108
Sage Weilf24e9982009-10-06 11:31:10 -07001109/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001110 * Close the connection and assign any leftover requests to the
1111 * homeless session.
1112 */
1113static void close_osd(struct ceph_osd *osd)
1114{
1115 struct ceph_osd_client *osdc = osd->o_osdc;
1116 struct rb_node *n;
1117
1118 verify_osdc_wrlocked(osdc);
1119 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1120
1121 ceph_con_close(&osd->o_con);
1122
1123 for (n = rb_first(&osd->o_requests); n; ) {
1124 struct ceph_osd_request *req =
1125 rb_entry(n, struct ceph_osd_request, r_node);
1126
1127 n = rb_next(n); /* unlink_request() */
1128
1129 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1130 unlink_request(osd, req);
1131 link_request(&osdc->homeless_osd, req);
1132 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02001133 for (n = rb_first(&osd->o_linger_requests); n; ) {
1134 struct ceph_osd_linger_request *lreq =
1135 rb_entry(n, struct ceph_osd_linger_request, node);
1136
1137 n = rb_next(n); /* unlink_linger() */
1138
1139 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1140 lreq->linger_id);
1141 unlink_linger(osd, lreq);
1142 link_linger(&osdc->homeless_osd, lreq);
1143 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001144
1145 __remove_osd_from_lru(osd);
1146 erase_osd(&osdc->osds, osd);
1147 put_osd(osd);
1148}
1149
1150/*
Sage Weilf24e9982009-10-06 11:31:10 -07001151 * reset osd connect
1152 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001153static int reopen_osd(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001154{
Alex Elderc3acb182012-12-07 09:57:58 -06001155 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001156
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001157 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1158
1159 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001160 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001161 close_osd(osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001162 return -ENODEV;
1163 }
1164
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001165 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
Alex Elderc3acb182012-12-07 09:57:58 -06001166 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1167 !ceph_con_opened(&osd->o_con)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001168 struct rb_node *n;
Alex Elderc3acb182012-12-07 09:57:58 -06001169
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001170 dout("osd addr hasn't changed and connection never opened, "
1171 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001172 /* touch each r_stamp for handle_timeout()'s benfit */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001173 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1174 struct ceph_osd_request *req =
1175 rb_entry(n, struct ceph_osd_request, r_node);
Sage Weil87b315a2010-03-22 14:51:18 -07001176 req->r_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001177 }
Alex Elderc3acb182012-12-07 09:57:58 -06001178
1179 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001180 }
Alex Elderc3acb182012-12-07 09:57:58 -06001181
1182 ceph_con_close(&osd->o_con);
1183 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1184 osd->o_incarnation++;
1185
1186 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001187}
1188
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001189static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1190 bool wrlocked)
Sage Weilf24e9982009-10-06 11:31:10 -07001191{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001192 struct ceph_osd *osd;
1193
1194 if (wrlocked)
1195 verify_osdc_wrlocked(osdc);
1196 else
1197 verify_osdc_locked(osdc);
1198
1199 if (o != CEPH_HOMELESS_OSD)
1200 osd = lookup_osd(&osdc->osds, o);
1201 else
1202 osd = &osdc->homeless_osd;
1203 if (!osd) {
1204 if (!wrlocked)
1205 return ERR_PTR(-EAGAIN);
1206
1207 osd = create_osd(osdc, o);
1208 insert_osd(&osdc->osds, osd);
1209 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1210 &osdc->osdmap->osd_addr[osd->o_osd]);
1211 }
1212
1213 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1214 return osd;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001215}
1216
Sage Weilf24e9982009-10-06 11:31:10 -07001217/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001218 * Create request <-> OSD session relation.
1219 *
1220 * @req has to be assigned a tid, @osd may be homeless.
Sage Weilf24e9982009-10-06 11:31:10 -07001221 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001222static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001223{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001224 verify_osd_locked(osd);
1225 WARN_ON(!req->r_tid || req->r_osd);
1226 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1227 req, req->r_tid);
Sage Weil35f9f8a2012-05-16 15:16:38 -05001228
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001229 if (!osd_homeless(osd))
1230 __remove_osd_from_lru(osd);
1231 else
1232 atomic_inc(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001233
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001234 get_osd(osd);
1235 insert_request(&osd->o_requests, req);
1236 req->r_osd = osd;
Sage Weilf24e9982009-10-06 11:31:10 -07001237}
1238
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001239static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001240{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001241 verify_osd_locked(osd);
1242 WARN_ON(req->r_osd != osd);
1243 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1244 req, req->r_tid);
1245
1246 req->r_osd = NULL;
1247 erase_request(&osd->o_requests, req);
1248 put_osd(osd);
1249
1250 if (!osd_homeless(osd))
1251 maybe_move_osd_to_lru(osd);
1252 else
1253 atomic_dec(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001254}
1255
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001256static bool __pool_full(struct ceph_pg_pool_info *pi)
1257{
1258 return pi->flags & CEPH_POOL_FLAG_FULL;
1259}
1260
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001261static bool have_pool_full(struct ceph_osd_client *osdc)
1262{
1263 struct rb_node *n;
1264
1265 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1266 struct ceph_pg_pool_info *pi =
1267 rb_entry(n, struct ceph_pg_pool_info, node);
1268
1269 if (__pool_full(pi))
1270 return true;
1271 }
1272
1273 return false;
1274}
1275
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001276static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1277{
1278 struct ceph_pg_pool_info *pi;
1279
1280 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1281 if (!pi)
1282 return false;
1283
1284 return __pool_full(pi);
1285}
1286
Sage Weilf24e9982009-10-06 11:31:10 -07001287/*
Josh Durgind29adb32013-12-02 19:11:48 -08001288 * Returns whether a request should be blocked from being sent
1289 * based on the current osdmap and osd_client settings.
Josh Durgind29adb32013-12-02 19:11:48 -08001290 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001291static bool target_should_be_paused(struct ceph_osd_client *osdc,
1292 const struct ceph_osd_request_target *t,
1293 struct ceph_pg_pool_info *pi)
1294{
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001295 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1296 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1297 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001298 __pool_full(pi);
1299
1300 WARN_ON(pi->id != t->base_oloc.pool);
Jeff Layton58eb7932017-04-18 09:21:16 -04001301 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1302 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1303 (osdc->osdmap->epoch < osdc->epoch_barrier);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001304}
1305
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001306enum calc_target_result {
1307 CALC_TARGET_NO_ACTION = 0,
1308 CALC_TARGET_NEED_RESEND,
1309 CALC_TARGET_POOL_DNE,
1310};
1311
1312static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1313 struct ceph_osd_request_target *t,
1314 u32 *last_force_resend,
1315 bool any_change)
1316{
1317 struct ceph_pg_pool_info *pi;
1318 struct ceph_pg pgid, last_pgid;
1319 struct ceph_osds up, acting;
1320 bool force_resend = false;
1321 bool need_check_tiering = false;
1322 bool need_resend = false;
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001323 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001324 enum calc_target_result ct_res;
1325 int ret;
1326
1327 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1328 if (!pi) {
1329 t->osd = CEPH_HOMELESS_OSD;
1330 ct_res = CALC_TARGET_POOL_DNE;
1331 goto out;
1332 }
1333
1334 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1335 if (last_force_resend &&
1336 *last_force_resend < pi->last_force_request_resend) {
1337 *last_force_resend = pi->last_force_request_resend;
1338 force_resend = true;
1339 } else if (!last_force_resend) {
1340 force_resend = true;
1341 }
1342 }
1343 if (ceph_oid_empty(&t->target_oid) || force_resend) {
1344 ceph_oid_copy(&t->target_oid, &t->base_oid);
1345 need_check_tiering = true;
1346 }
1347 if (ceph_oloc_empty(&t->target_oloc) || force_resend) {
1348 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1349 need_check_tiering = true;
1350 }
1351
1352 if (need_check_tiering &&
1353 (t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1354 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1355 t->target_oloc.pool = pi->read_tier;
1356 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1357 t->target_oloc.pool = pi->write_tier;
1358 }
1359
1360 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1361 &t->target_oloc, &pgid);
1362 if (ret) {
1363 WARN_ON(ret != -ENOENT);
1364 t->osd = CEPH_HOMELESS_OSD;
1365 ct_res = CALC_TARGET_POOL_DNE;
1366 goto out;
1367 }
1368 last_pgid.pool = pgid.pool;
1369 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1370
1371 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1372 if (any_change &&
1373 ceph_is_new_interval(&t->acting,
1374 &acting,
1375 &t->up,
1376 &up,
1377 t->size,
1378 pi->size,
1379 t->min_size,
1380 pi->min_size,
1381 t->pg_num,
1382 pi->pg_num,
1383 t->sort_bitwise,
1384 sort_bitwise,
1385 &last_pgid))
1386 force_resend = true;
1387
1388 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1389 t->paused = false;
1390 need_resend = true;
1391 }
1392
1393 if (ceph_pg_compare(&t->pgid, &pgid) ||
1394 ceph_osds_changed(&t->acting, &acting, any_change) ||
1395 force_resend) {
1396 t->pgid = pgid; /* struct */
1397 ceph_osds_copy(&t->acting, &acting);
1398 ceph_osds_copy(&t->up, &up);
1399 t->size = pi->size;
1400 t->min_size = pi->min_size;
1401 t->pg_num = pi->pg_num;
1402 t->pg_num_mask = pi->pg_num_mask;
1403 t->sort_bitwise = sort_bitwise;
1404
1405 t->osd = acting.primary;
1406 need_resend = true;
1407 }
1408
1409 ct_res = need_resend ? CALC_TARGET_NEED_RESEND : CALC_TARGET_NO_ACTION;
1410out:
1411 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1412 return ct_res;
1413}
1414
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001415static void setup_request_data(struct ceph_osd_request *req,
1416 struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001417{
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001418 u32 data_len = 0;
1419 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001420
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001421 if (!list_empty(&msg->data))
1422 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001423
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001424 WARN_ON(msg->data_length);
1425 for (i = 0; i < req->r_num_ops; i++) {
1426 struct ceph_osd_req_op *op = &req->r_ops[i];
1427
1428 switch (op->op) {
1429 /* request */
1430 case CEPH_OSD_OP_WRITE:
1431 case CEPH_OSD_OP_WRITEFULL:
1432 WARN_ON(op->indata_len != op->extent.length);
1433 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1434 break;
1435 case CEPH_OSD_OP_SETXATTR:
1436 case CEPH_OSD_OP_CMPXATTR:
1437 WARN_ON(op->indata_len != op->xattr.name_len +
1438 op->xattr.value_len);
1439 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1440 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001441 case CEPH_OSD_OP_NOTIFY_ACK:
1442 ceph_osdc_msg_data_add(msg,
1443 &op->notify_ack.request_data);
1444 break;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001445
1446 /* reply */
1447 case CEPH_OSD_OP_STAT:
1448 ceph_osdc_msg_data_add(req->r_reply,
1449 &op->raw_data_in);
1450 break;
1451 case CEPH_OSD_OP_READ:
1452 ceph_osdc_msg_data_add(req->r_reply,
1453 &op->extent.osd_data);
1454 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001455 case CEPH_OSD_OP_LIST_WATCHERS:
1456 ceph_osdc_msg_data_add(req->r_reply,
1457 &op->list_watchers.response_data);
1458 break;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001459
1460 /* both */
1461 case CEPH_OSD_OP_CALL:
1462 WARN_ON(op->indata_len != op->cls.class_len +
1463 op->cls.method_len +
1464 op->cls.indata_len);
1465 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1466 /* optional, can be NONE */
1467 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1468 /* optional, can be NONE */
1469 ceph_osdc_msg_data_add(req->r_reply,
1470 &op->cls.response_data);
1471 break;
Ilya Dryomov19079202016-04-28 16:07:27 +02001472 case CEPH_OSD_OP_NOTIFY:
1473 ceph_osdc_msg_data_add(msg,
1474 &op->notify.request_data);
1475 ceph_osdc_msg_data_add(req->r_reply,
1476 &op->notify.response_data);
1477 break;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001478 }
1479
1480 data_len += op->indata_len;
1481 }
1482
1483 WARN_ON(data_len != msg->data_length);
1484}
1485
1486static void encode_request(struct ceph_osd_request *req, struct ceph_msg *msg)
1487{
1488 void *p = msg->front.iov_base;
1489 void *const end = p + msg->front_alloc_len;
1490 u32 data_len = 0;
1491 int i;
1492
1493 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1494 /* snapshots aren't writeable */
1495 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1496 } else {
1497 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1498 req->r_data_offset || req->r_snapc);
1499 }
1500
1501 setup_request_data(req, msg);
1502
1503 ceph_encode_32(&p, 1); /* client_inc, always 1 */
1504 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1505 ceph_encode_32(&p, req->r_flags);
1506 ceph_encode_timespec(p, &req->r_mtime);
1507 p += sizeof(struct ceph_timespec);
Jeff Laytonaa26d662017-04-04 08:39:36 -04001508
1509 /* reassert_version */
1510 memset(p, 0, sizeof(struct ceph_eversion));
1511 p += sizeof(struct ceph_eversion);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001512
1513 /* oloc */
Yan, Zheng30c156d2016-02-14 11:24:31 +08001514 ceph_start_encoding(&p, 5, 4,
1515 ceph_oloc_encoding_size(&req->r_t.target_oloc));
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001516 ceph_encode_64(&p, req->r_t.target_oloc.pool);
1517 ceph_encode_32(&p, -1); /* preferred */
1518 ceph_encode_32(&p, 0); /* key len */
Yan, Zheng30c156d2016-02-14 11:24:31 +08001519 if (req->r_t.target_oloc.pool_ns)
1520 ceph_encode_string(&p, end, req->r_t.target_oloc.pool_ns->str,
1521 req->r_t.target_oloc.pool_ns->len);
1522 else
1523 ceph_encode_32(&p, 0);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001524
1525 /* pgid */
1526 ceph_encode_8(&p, 1);
Ilya Dryomova66dd382016-04-28 16:07:23 +02001527 ceph_encode_64(&p, req->r_t.pgid.pool);
1528 ceph_encode_32(&p, req->r_t.pgid.seed);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001529 ceph_encode_32(&p, -1); /* preferred */
Sage Weil2169aea2013-02-25 16:13:08 -08001530
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001531 /* oid */
1532 ceph_encode_32(&p, req->r_t.target_oid.name_len);
1533 memcpy(p, req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1534 p += req->r_t.target_oid.name_len;
1535
1536 /* ops, can imply data */
1537 ceph_encode_16(&p, req->r_num_ops);
1538 for (i = 0; i < req->r_num_ops; i++) {
1539 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1540 p += sizeof(struct ceph_osd_op);
1541 }
1542
1543 ceph_encode_64(&p, req->r_snapid); /* snapid */
1544 if (req->r_snapc) {
1545 ceph_encode_64(&p, req->r_snapc->seq);
1546 ceph_encode_32(&p, req->r_snapc->num_snaps);
1547 for (i = 0; i < req->r_snapc->num_snaps; i++)
1548 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1549 } else {
1550 ceph_encode_64(&p, 0); /* snap_seq */
1551 ceph_encode_32(&p, 0); /* snaps len */
1552 }
1553
1554 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1555
1556 BUG_ON(p > end);
1557 msg->front.iov_len = p - msg->front.iov_base;
1558 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1559 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1560 msg->hdr.data_len = cpu_to_le32(data_len);
1561 /*
1562 * The header "data_off" is a hint to the receiver allowing it
1563 * to align received data into its buffers such that there's no
1564 * need to re-copy it before writing it to disk (direct I/O).
1565 */
1566 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1567
Ilya Dryomov4a3262b2016-05-30 18:33:32 +02001568 dout("%s req %p oid %s oid_len %d front %zu data %u\n", __func__,
1569 req, req->r_t.target_oid.name, req->r_t.target_oid.name_len,
1570 msg->front.iov_len, data_len);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001571}
1572
1573/*
1574 * @req has to be assigned a tid and registered.
1575 */
1576static void send_request(struct ceph_osd_request *req)
1577{
1578 struct ceph_osd *osd = req->r_osd;
1579
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001580 verify_osd_locked(osd);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001581 WARN_ON(osd->o_osd != req->r_t.osd);
1582
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001583 /*
1584 * We may have a previously queued request message hanging
1585 * around. Cancel it to avoid corrupting the msgr.
1586 */
1587 if (req->r_sent)
1588 ceph_msg_revoke(req->r_request);
1589
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001590 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1591 if (req->r_attempts)
1592 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1593 else
1594 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1595
1596 encode_request(req, req->r_request);
1597
1598 dout("%s req %p tid %llu to pg %llu.%x osd%d flags 0x%x attempt %d\n",
1599 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
1600 req->r_t.osd, req->r_flags, req->r_attempts);
1601
1602 req->r_t.paused = false;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001603 req->r_stamp = jiffies;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001604 req->r_attempts++;
Sage Weilf24e9982009-10-06 11:31:10 -07001605
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001606 req->r_sent = osd->o_incarnation;
1607 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1608 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
Sage Weilf24e9982009-10-06 11:31:10 -07001609}
1610
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001611static void maybe_request_map(struct ceph_osd_client *osdc)
1612{
1613 bool continuous = false;
1614
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001615 verify_osdc_locked(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001616 WARN_ON(!osdc->osdmap->epoch);
1617
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001618 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1619 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
1620 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001621 dout("%s osdc %p continuous\n", __func__, osdc);
1622 continuous = true;
1623 } else {
1624 dout("%s osdc %p onetime\n", __func__, osdc);
1625 }
1626
1627 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1628 osdc->osdmap->epoch + 1, continuous))
1629 ceph_monc_renew_subs(&osdc->client->monc);
1630}
1631
Jeff Laytona1f40202017-04-04 08:39:37 -04001632static void complete_request(struct ceph_osd_request *req, int err);
Ilya Dryomov46092452016-04-28 16:07:27 +02001633static void send_map_check(struct ceph_osd_request *req);
1634
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001635static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001636{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001637 struct ceph_osd_client *osdc = req->r_osdc;
1638 struct ceph_osd *osd;
Ilya Dryomov46092452016-04-28 16:07:27 +02001639 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001640 bool need_send = false;
1641 bool promoted = false;
Jeff Laytona1f40202017-04-04 08:39:37 -04001642 bool need_abort = false;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001643
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001644 WARN_ON(req->r_tid);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001645 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
1646
1647again:
Ilya Dryomov46092452016-04-28 16:07:27 +02001648 ct_res = calc_target(osdc, &req->r_t, &req->r_last_force_resend, false);
1649 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
1650 goto promote;
1651
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001652 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
1653 if (IS_ERR(osd)) {
1654 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
1655 goto promote;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001656 }
1657
Jeff Layton58eb7932017-04-18 09:21:16 -04001658 if (osdc->osdmap->epoch < osdc->epoch_barrier) {
1659 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
1660 osdc->epoch_barrier);
1661 req->r_t.paused = true;
1662 maybe_request_map(osdc);
1663 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1664 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001665 dout("req %p pausewr\n", req);
1666 req->r_t.paused = true;
1667 maybe_request_map(osdc);
1668 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001669 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001670 dout("req %p pauserd\n", req);
1671 req->r_t.paused = true;
1672 maybe_request_map(osdc);
1673 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1674 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
1675 CEPH_OSD_FLAG_FULL_FORCE)) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001676 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001677 pool_full(osdc, req->r_t.base_oloc.pool))) {
1678 dout("req %p full/pool_full\n", req);
1679 pr_warn_ratelimited("FULL or reached pool quota\n");
1680 req->r_t.paused = true;
1681 maybe_request_map(osdc);
Jeff Laytona1f40202017-04-04 08:39:37 -04001682 if (req->r_abort_on_full)
1683 need_abort = true;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001684 } else if (!osd_homeless(osd)) {
1685 need_send = true;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001686 } else {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001687 maybe_request_map(osdc);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001688 }
1689
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001690 mutex_lock(&osd->lock);
1691 /*
1692 * Assign the tid atomically with send_request() to protect
1693 * multiple writes to the same object from racing with each
1694 * other, resulting in out of order ops on the OSDs.
1695 */
1696 req->r_tid = atomic64_inc_return(&osdc->last_tid);
1697 link_request(osd, req);
1698 if (need_send)
1699 send_request(req);
Jeff Laytona1f40202017-04-04 08:39:37 -04001700 else if (need_abort)
1701 complete_request(req, -ENOSPC);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001702 mutex_unlock(&osd->lock);
1703
Ilya Dryomov46092452016-04-28 16:07:27 +02001704 if (ct_res == CALC_TARGET_POOL_DNE)
1705 send_map_check(req);
1706
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001707 if (promoted)
1708 downgrade_write(&osdc->lock);
1709 return;
1710
1711promote:
1712 up_read(&osdc->lock);
1713 down_write(&osdc->lock);
1714 wrlocked = true;
1715 promoted = true;
1716 goto again;
1717}
1718
1719static void account_request(struct ceph_osd_request *req)
1720{
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001721 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001722 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001723
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001724 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001725 atomic_inc(&req->r_osdc->num_requests);
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01001726
1727 req->r_start_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001728}
1729
1730static void submit_request(struct ceph_osd_request *req, bool wrlocked)
1731{
1732 ceph_osdc_get_request(req);
1733 account_request(req);
1734 __submit_request(req, wrlocked);
1735}
1736
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001737static void finish_request(struct ceph_osd_request *req)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001738{
1739 struct ceph_osd_client *osdc = req->r_osdc;
1740 struct ceph_osd *osd = req->r_osd;
1741
1742 verify_osd_locked(osd);
1743 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1744
Ilya Dryomov46092452016-04-28 16:07:27 +02001745 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001746 unlink_request(osd, req);
1747 atomic_dec(&osdc->num_requests);
1748
1749 /*
1750 * If an OSD has failed or returned and a request has been sent
1751 * twice, it's possible to get a reply and end up here while the
1752 * request message is queued for delivery. We will ignore the
1753 * reply, so not a big deal, but better to try and catch it.
1754 */
1755 ceph_msg_revoke(req->r_request);
1756 ceph_msg_revoke_incoming(req->r_reply);
1757}
1758
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001759static void __complete_request(struct ceph_osd_request *req)
1760{
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001761 if (req->r_callback) {
1762 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
1763 req->r_tid, req->r_callback, req->r_result);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001764 req->r_callback(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001765 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001766}
1767
Ilya Dryomov46092452016-04-28 16:07:27 +02001768/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001769 * This is open-coded in handle_reply().
Ilya Dryomov46092452016-04-28 16:07:27 +02001770 */
1771static void complete_request(struct ceph_osd_request *req, int err)
1772{
1773 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1774
1775 req->r_result = err;
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001776 finish_request(req);
Ilya Dryomov46092452016-04-28 16:07:27 +02001777 __complete_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001778 complete_all(&req->r_completion);
Ilya Dryomov46092452016-04-28 16:07:27 +02001779 ceph_osdc_put_request(req);
1780}
1781
1782static void cancel_map_check(struct ceph_osd_request *req)
1783{
1784 struct ceph_osd_client *osdc = req->r_osdc;
1785 struct ceph_osd_request *lookup_req;
1786
1787 verify_osdc_wrlocked(osdc);
1788
1789 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1790 if (!lookup_req)
1791 return;
1792
1793 WARN_ON(lookup_req != req);
1794 erase_request_mc(&osdc->map_checks, req);
1795 ceph_osdc_put_request(req);
1796}
1797
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001798static void cancel_request(struct ceph_osd_request *req)
1799{
1800 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1801
Ilya Dryomov46092452016-04-28 16:07:27 +02001802 cancel_map_check(req);
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001803 finish_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001804 complete_all(&req->r_completion);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01001805 ceph_osdc_put_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001806}
1807
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01001808static void abort_request(struct ceph_osd_request *req, int err)
1809{
1810 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1811
1812 cancel_map_check(req);
1813 complete_request(req, err);
1814}
1815
Jeff Layton58eb7932017-04-18 09:21:16 -04001816static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
1817{
1818 if (likely(eb > osdc->epoch_barrier)) {
1819 dout("updating epoch_barrier from %u to %u\n",
1820 osdc->epoch_barrier, eb);
1821 osdc->epoch_barrier = eb;
1822 /* Request map if we're not to the barrier yet */
1823 if (eb > osdc->osdmap->epoch)
1824 maybe_request_map(osdc);
1825 }
1826}
1827
1828void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
1829{
1830 down_read(&osdc->lock);
1831 if (unlikely(eb > osdc->epoch_barrier)) {
1832 up_read(&osdc->lock);
1833 down_write(&osdc->lock);
1834 update_epoch_barrier(osdc, eb);
1835 up_write(&osdc->lock);
1836 } else {
1837 up_read(&osdc->lock);
1838 }
1839}
1840EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
1841
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04001842/*
1843 * Drop all pending requests that are stalled waiting on a full condition to
Jeff Layton58eb7932017-04-18 09:21:16 -04001844 * clear, and complete them with ENOSPC as the return code. Set the
1845 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
1846 * cancelled.
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04001847 */
1848static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
1849{
1850 struct rb_node *n;
Jeff Layton58eb7932017-04-18 09:21:16 -04001851 bool victims = false;
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04001852
1853 dout("enter abort_on_full\n");
1854
1855 if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
1856 goto out;
1857
Jeff Layton58eb7932017-04-18 09:21:16 -04001858 /* Scan list and see if there is anything to abort */
1859 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1860 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1861 struct rb_node *m;
1862
1863 m = rb_first(&osd->o_requests);
1864 while (m) {
1865 struct ceph_osd_request *req = rb_entry(m,
1866 struct ceph_osd_request, r_node);
1867 m = rb_next(m);
1868
1869 if (req->r_abort_on_full) {
1870 victims = true;
1871 break;
1872 }
1873 }
1874 if (victims)
1875 break;
1876 }
1877
1878 if (!victims)
1879 goto out;
1880
1881 /*
1882 * Update the barrier to current epoch if it's behind that point,
1883 * since we know we have some calls to be aborted in the tree.
1884 */
1885 update_epoch_barrier(osdc, osdc->osdmap->epoch);
1886
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04001887 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1888 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1889 struct rb_node *m;
1890
1891 m = rb_first(&osd->o_requests);
1892 while (m) {
1893 struct ceph_osd_request *req = rb_entry(m,
1894 struct ceph_osd_request, r_node);
1895 m = rb_next(m);
1896
1897 if (req->r_abort_on_full &&
1898 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1899 pool_full(osdc, req->r_t.target_oloc.pool)))
1900 abort_request(req, -ENOSPC);
1901 }
1902 }
1903out:
Jeff Layton58eb7932017-04-18 09:21:16 -04001904 dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04001905}
1906
Ilya Dryomov46092452016-04-28 16:07:27 +02001907static void check_pool_dne(struct ceph_osd_request *req)
1908{
1909 struct ceph_osd_client *osdc = req->r_osdc;
1910 struct ceph_osdmap *map = osdc->osdmap;
1911
1912 verify_osdc_wrlocked(osdc);
1913 WARN_ON(!map->epoch);
1914
1915 if (req->r_attempts) {
1916 /*
1917 * We sent a request earlier, which means that
1918 * previously the pool existed, and now it does not
1919 * (i.e., it was deleted).
1920 */
1921 req->r_map_dne_bound = map->epoch;
1922 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
1923 req->r_tid);
1924 } else {
1925 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
1926 req, req->r_tid, req->r_map_dne_bound, map->epoch);
1927 }
1928
1929 if (req->r_map_dne_bound) {
1930 if (map->epoch >= req->r_map_dne_bound) {
1931 /* we had a new enough map */
1932 pr_info_ratelimited("tid %llu pool does not exist\n",
1933 req->r_tid);
1934 complete_request(req, -ENOENT);
1935 }
1936 } else {
1937 send_map_check(req);
1938 }
1939}
1940
1941static void map_check_cb(struct ceph_mon_generic_request *greq)
1942{
1943 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
1944 struct ceph_osd_request *req;
1945 u64 tid = greq->private_data;
1946
1947 WARN_ON(greq->result || !greq->u.newest);
1948
1949 down_write(&osdc->lock);
1950 req = lookup_request_mc(&osdc->map_checks, tid);
1951 if (!req) {
1952 dout("%s tid %llu dne\n", __func__, tid);
1953 goto out_unlock;
1954 }
1955
1956 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
1957 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
1958 if (!req->r_map_dne_bound)
1959 req->r_map_dne_bound = greq->u.newest;
1960 erase_request_mc(&osdc->map_checks, req);
1961 check_pool_dne(req);
1962
1963 ceph_osdc_put_request(req);
1964out_unlock:
1965 up_write(&osdc->lock);
1966}
1967
1968static void send_map_check(struct ceph_osd_request *req)
1969{
1970 struct ceph_osd_client *osdc = req->r_osdc;
1971 struct ceph_osd_request *lookup_req;
1972 int ret;
1973
1974 verify_osdc_wrlocked(osdc);
1975
1976 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1977 if (lookup_req) {
1978 WARN_ON(lookup_req != req);
1979 return;
1980 }
1981
1982 ceph_osdc_get_request(req);
1983 insert_request_mc(&osdc->map_checks, req);
1984 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
1985 map_check_cb, req->r_tid);
1986 WARN_ON(ret);
1987}
1988
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001989/*
Ilya Dryomov922dab62016-05-26 01:15:02 +02001990 * lingering requests, watch/notify v2 infrastructure
1991 */
1992static void linger_release(struct kref *kref)
1993{
1994 struct ceph_osd_linger_request *lreq =
1995 container_of(kref, struct ceph_osd_linger_request, kref);
1996
1997 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
1998 lreq->reg_req, lreq->ping_req);
1999 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2000 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
Ilya Dryomov46092452016-04-28 16:07:27 +02002001 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002002 WARN_ON(!list_empty(&lreq->scan_item));
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002003 WARN_ON(!list_empty(&lreq->pending_lworks));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002004 WARN_ON(lreq->osd);
2005
2006 if (lreq->reg_req)
2007 ceph_osdc_put_request(lreq->reg_req);
2008 if (lreq->ping_req)
2009 ceph_osdc_put_request(lreq->ping_req);
2010 target_destroy(&lreq->t);
2011 kfree(lreq);
2012}
2013
2014static void linger_put(struct ceph_osd_linger_request *lreq)
2015{
2016 if (lreq)
2017 kref_put(&lreq->kref, linger_release);
2018}
2019
2020static struct ceph_osd_linger_request *
2021linger_get(struct ceph_osd_linger_request *lreq)
2022{
2023 kref_get(&lreq->kref);
2024 return lreq;
2025}
2026
2027static struct ceph_osd_linger_request *
2028linger_alloc(struct ceph_osd_client *osdc)
2029{
2030 struct ceph_osd_linger_request *lreq;
2031
2032 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2033 if (!lreq)
2034 return NULL;
2035
2036 kref_init(&lreq->kref);
2037 mutex_init(&lreq->lock);
2038 RB_CLEAR_NODE(&lreq->node);
2039 RB_CLEAR_NODE(&lreq->osdc_node);
Ilya Dryomov46092452016-04-28 16:07:27 +02002040 RB_CLEAR_NODE(&lreq->mc_node);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002041 INIT_LIST_HEAD(&lreq->scan_item);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002042 INIT_LIST_HEAD(&lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002043 init_completion(&lreq->reg_commit_wait);
Ilya Dryomov19079202016-04-28 16:07:27 +02002044 init_completion(&lreq->notify_finish_wait);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002045
2046 lreq->osdc = osdc;
2047 target_init(&lreq->t);
2048
2049 dout("%s lreq %p\n", __func__, lreq);
2050 return lreq;
2051}
2052
2053DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2054DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02002055DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002056
2057/*
2058 * Create linger request <-> OSD session relation.
2059 *
2060 * @lreq has to be registered, @osd may be homeless.
2061 */
2062static void link_linger(struct ceph_osd *osd,
2063 struct ceph_osd_linger_request *lreq)
2064{
2065 verify_osd_locked(osd);
2066 WARN_ON(!lreq->linger_id || lreq->osd);
2067 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2068 osd->o_osd, lreq, lreq->linger_id);
2069
2070 if (!osd_homeless(osd))
2071 __remove_osd_from_lru(osd);
2072 else
2073 atomic_inc(&osd->o_osdc->num_homeless);
2074
2075 get_osd(osd);
2076 insert_linger(&osd->o_linger_requests, lreq);
2077 lreq->osd = osd;
2078}
2079
2080static void unlink_linger(struct ceph_osd *osd,
2081 struct ceph_osd_linger_request *lreq)
2082{
2083 verify_osd_locked(osd);
2084 WARN_ON(lreq->osd != osd);
2085 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2086 osd->o_osd, lreq, lreq->linger_id);
2087
2088 lreq->osd = NULL;
2089 erase_linger(&osd->o_linger_requests, lreq);
2090 put_osd(osd);
2091
2092 if (!osd_homeless(osd))
2093 maybe_move_osd_to_lru(osd);
2094 else
2095 atomic_dec(&osd->o_osdc->num_homeless);
2096}
2097
2098static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2099{
2100 verify_osdc_locked(lreq->osdc);
2101
2102 return !RB_EMPTY_NODE(&lreq->osdc_node);
2103}
2104
2105static bool linger_registered(struct ceph_osd_linger_request *lreq)
2106{
2107 struct ceph_osd_client *osdc = lreq->osdc;
2108 bool registered;
2109
2110 down_read(&osdc->lock);
2111 registered = __linger_registered(lreq);
2112 up_read(&osdc->lock);
2113
2114 return registered;
2115}
2116
2117static void linger_register(struct ceph_osd_linger_request *lreq)
2118{
2119 struct ceph_osd_client *osdc = lreq->osdc;
2120
2121 verify_osdc_wrlocked(osdc);
2122 WARN_ON(lreq->linger_id);
2123
2124 linger_get(lreq);
2125 lreq->linger_id = ++osdc->last_linger_id;
2126 insert_linger_osdc(&osdc->linger_requests, lreq);
2127}
2128
2129static void linger_unregister(struct ceph_osd_linger_request *lreq)
2130{
2131 struct ceph_osd_client *osdc = lreq->osdc;
2132
2133 verify_osdc_wrlocked(osdc);
2134
2135 erase_linger_osdc(&osdc->linger_requests, lreq);
2136 linger_put(lreq);
2137}
2138
2139static void cancel_linger_request(struct ceph_osd_request *req)
2140{
2141 struct ceph_osd_linger_request *lreq = req->r_priv;
2142
2143 WARN_ON(!req->r_linger);
2144 cancel_request(req);
2145 linger_put(lreq);
2146}
2147
2148struct linger_work {
2149 struct work_struct work;
2150 struct ceph_osd_linger_request *lreq;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002151 struct list_head pending_item;
2152 unsigned long queued_stamp;
Ilya Dryomov922dab62016-05-26 01:15:02 +02002153
2154 union {
2155 struct {
2156 u64 notify_id;
2157 u64 notifier_id;
2158 void *payload; /* points into @msg front */
2159 size_t payload_len;
2160
2161 struct ceph_msg *msg; /* for ceph_msg_put() */
2162 } notify;
2163 struct {
2164 int err;
2165 } error;
2166 };
2167};
2168
2169static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2170 work_func_t workfn)
2171{
2172 struct linger_work *lwork;
2173
2174 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2175 if (!lwork)
2176 return NULL;
2177
2178 INIT_WORK(&lwork->work, workfn);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002179 INIT_LIST_HEAD(&lwork->pending_item);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002180 lwork->lreq = linger_get(lreq);
2181
2182 return lwork;
2183}
2184
2185static void lwork_free(struct linger_work *lwork)
2186{
2187 struct ceph_osd_linger_request *lreq = lwork->lreq;
2188
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002189 mutex_lock(&lreq->lock);
2190 list_del(&lwork->pending_item);
2191 mutex_unlock(&lreq->lock);
2192
Ilya Dryomov922dab62016-05-26 01:15:02 +02002193 linger_put(lreq);
2194 kfree(lwork);
2195}
2196
2197static void lwork_queue(struct linger_work *lwork)
2198{
2199 struct ceph_osd_linger_request *lreq = lwork->lreq;
2200 struct ceph_osd_client *osdc = lreq->osdc;
2201
2202 verify_lreq_locked(lreq);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002203 WARN_ON(!list_empty(&lwork->pending_item));
2204
2205 lwork->queued_stamp = jiffies;
2206 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002207 queue_work(osdc->notify_wq, &lwork->work);
2208}
2209
2210static void do_watch_notify(struct work_struct *w)
2211{
2212 struct linger_work *lwork = container_of(w, struct linger_work, work);
2213 struct ceph_osd_linger_request *lreq = lwork->lreq;
2214
2215 if (!linger_registered(lreq)) {
2216 dout("%s lreq %p not registered\n", __func__, lreq);
2217 goto out;
2218 }
2219
Ilya Dryomov19079202016-04-28 16:07:27 +02002220 WARN_ON(!lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002221 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2222 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2223 lwork->notify.payload_len);
2224 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2225 lwork->notify.notifier_id, lwork->notify.payload,
2226 lwork->notify.payload_len);
2227
2228out:
2229 ceph_msg_put(lwork->notify.msg);
2230 lwork_free(lwork);
2231}
2232
2233static void do_watch_error(struct work_struct *w)
2234{
2235 struct linger_work *lwork = container_of(w, struct linger_work, work);
2236 struct ceph_osd_linger_request *lreq = lwork->lreq;
2237
2238 if (!linger_registered(lreq)) {
2239 dout("%s lreq %p not registered\n", __func__, lreq);
2240 goto out;
2241 }
2242
2243 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2244 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2245
2246out:
2247 lwork_free(lwork);
2248}
2249
2250static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2251{
2252 struct linger_work *lwork;
2253
2254 lwork = lwork_alloc(lreq, do_watch_error);
2255 if (!lwork) {
2256 pr_err("failed to allocate error-lwork\n");
2257 return;
2258 }
2259
2260 lwork->error.err = lreq->last_error;
2261 lwork_queue(lwork);
2262}
2263
2264static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2265 int result)
2266{
2267 if (!completion_done(&lreq->reg_commit_wait)) {
2268 lreq->reg_commit_error = (result <= 0 ? result : 0);
2269 complete_all(&lreq->reg_commit_wait);
2270 }
2271}
2272
2273static void linger_commit_cb(struct ceph_osd_request *req)
2274{
2275 struct ceph_osd_linger_request *lreq = req->r_priv;
2276
2277 mutex_lock(&lreq->lock);
2278 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2279 lreq->linger_id, req->r_result);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002280 linger_reg_commit_complete(lreq, req->r_result);
2281 lreq->committed = true;
2282
Ilya Dryomov19079202016-04-28 16:07:27 +02002283 if (!lreq->is_watch) {
2284 struct ceph_osd_data *osd_data =
2285 osd_req_op_data(req, 0, notify, response_data);
2286 void *p = page_address(osd_data->pages[0]);
2287
2288 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2289 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2290
2291 /* make note of the notify_id */
2292 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2293 lreq->notify_id = ceph_decode_64(&p);
2294 dout("lreq %p notify_id %llu\n", lreq,
2295 lreq->notify_id);
2296 } else {
2297 dout("lreq %p no notify_id\n", lreq);
2298 }
2299 }
2300
Ilya Dryomov922dab62016-05-26 01:15:02 +02002301 mutex_unlock(&lreq->lock);
2302 linger_put(lreq);
2303}
2304
2305static int normalize_watch_error(int err)
2306{
2307 /*
2308 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2309 * notification and a failure to reconnect because we raced with
2310 * the delete appear the same to the user.
2311 */
2312 if (err == -ENOENT)
2313 err = -ENOTCONN;
2314
2315 return err;
2316}
2317
2318static void linger_reconnect_cb(struct ceph_osd_request *req)
2319{
2320 struct ceph_osd_linger_request *lreq = req->r_priv;
2321
2322 mutex_lock(&lreq->lock);
2323 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2324 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2325 if (req->r_result < 0) {
2326 if (!lreq->last_error) {
2327 lreq->last_error = normalize_watch_error(req->r_result);
2328 queue_watch_error(lreq);
2329 }
2330 }
2331
2332 mutex_unlock(&lreq->lock);
2333 linger_put(lreq);
2334}
2335
2336static void send_linger(struct ceph_osd_linger_request *lreq)
2337{
2338 struct ceph_osd_request *req = lreq->reg_req;
2339 struct ceph_osd_req_op *op = &req->r_ops[0];
2340
2341 verify_osdc_wrlocked(req->r_osdc);
2342 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2343
2344 if (req->r_osd)
2345 cancel_linger_request(req);
2346
2347 request_reinit(req);
2348 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2349 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2350 req->r_flags = lreq->t.flags;
2351 req->r_mtime = lreq->mtime;
2352
2353 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002354 if (lreq->is_watch && lreq->committed) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002355 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2356 op->watch.cookie != lreq->linger_id);
2357 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2358 op->watch.gen = ++lreq->register_gen;
2359 dout("lreq %p reconnect register_gen %u\n", lreq,
2360 op->watch.gen);
2361 req->r_callback = linger_reconnect_cb;
2362 } else {
Ilya Dryomov19079202016-04-28 16:07:27 +02002363 if (!lreq->is_watch)
2364 lreq->notify_id = 0;
2365 else
2366 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002367 dout("lreq %p register\n", lreq);
2368 req->r_callback = linger_commit_cb;
2369 }
2370 mutex_unlock(&lreq->lock);
2371
2372 req->r_priv = linger_get(lreq);
2373 req->r_linger = true;
2374
2375 submit_request(req, true);
2376}
2377
2378static void linger_ping_cb(struct ceph_osd_request *req)
2379{
2380 struct ceph_osd_linger_request *lreq = req->r_priv;
2381
2382 mutex_lock(&lreq->lock);
2383 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2384 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2385 lreq->last_error);
2386 if (lreq->register_gen == req->r_ops[0].watch.gen) {
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002387 if (!req->r_result) {
2388 lreq->watch_valid_thru = lreq->ping_sent;
2389 } else if (!lreq->last_error) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002390 lreq->last_error = normalize_watch_error(req->r_result);
2391 queue_watch_error(lreq);
2392 }
2393 } else {
2394 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2395 lreq->register_gen, req->r_ops[0].watch.gen);
2396 }
2397
2398 mutex_unlock(&lreq->lock);
2399 linger_put(lreq);
2400}
2401
2402static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2403{
2404 struct ceph_osd_client *osdc = lreq->osdc;
2405 struct ceph_osd_request *req = lreq->ping_req;
2406 struct ceph_osd_req_op *op = &req->r_ops[0];
2407
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002408 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002409 dout("%s PAUSERD\n", __func__);
2410 return;
2411 }
2412
2413 lreq->ping_sent = jiffies;
2414 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2415 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2416 lreq->register_gen);
2417
2418 if (req->r_osd)
2419 cancel_linger_request(req);
2420
2421 request_reinit(req);
2422 target_copy(&req->r_t, &lreq->t);
2423
2424 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2425 op->watch.cookie != lreq->linger_id ||
2426 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2427 op->watch.gen = lreq->register_gen;
2428 req->r_callback = linger_ping_cb;
2429 req->r_priv = linger_get(lreq);
2430 req->r_linger = true;
2431
2432 ceph_osdc_get_request(req);
2433 account_request(req);
2434 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2435 link_request(lreq->osd, req);
2436 send_request(req);
2437}
2438
2439static void linger_submit(struct ceph_osd_linger_request *lreq)
2440{
2441 struct ceph_osd_client *osdc = lreq->osdc;
2442 struct ceph_osd *osd;
2443
2444 calc_target(osdc, &lreq->t, &lreq->last_force_resend, false);
2445 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2446 link_linger(osd, lreq);
2447
2448 send_linger(lreq);
2449}
2450
Ilya Dryomov46092452016-04-28 16:07:27 +02002451static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2452{
2453 struct ceph_osd_client *osdc = lreq->osdc;
2454 struct ceph_osd_linger_request *lookup_lreq;
2455
2456 verify_osdc_wrlocked(osdc);
2457
2458 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2459 lreq->linger_id);
2460 if (!lookup_lreq)
2461 return;
2462
2463 WARN_ON(lookup_lreq != lreq);
2464 erase_linger_mc(&osdc->linger_map_checks, lreq);
2465 linger_put(lreq);
2466}
2467
Ilya Dryomov922dab62016-05-26 01:15:02 +02002468/*
2469 * @lreq has to be both registered and linked.
2470 */
2471static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2472{
Ilya Dryomov19079202016-04-28 16:07:27 +02002473 if (lreq->is_watch && lreq->ping_req->r_osd)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002474 cancel_linger_request(lreq->ping_req);
2475 if (lreq->reg_req->r_osd)
2476 cancel_linger_request(lreq->reg_req);
Ilya Dryomov46092452016-04-28 16:07:27 +02002477 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002478 unlink_linger(lreq->osd, lreq);
2479 linger_unregister(lreq);
2480}
2481
2482static void linger_cancel(struct ceph_osd_linger_request *lreq)
2483{
2484 struct ceph_osd_client *osdc = lreq->osdc;
2485
2486 down_write(&osdc->lock);
2487 if (__linger_registered(lreq))
2488 __linger_cancel(lreq);
2489 up_write(&osdc->lock);
2490}
2491
Ilya Dryomov46092452016-04-28 16:07:27 +02002492static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2493
2494static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2495{
2496 struct ceph_osd_client *osdc = lreq->osdc;
2497 struct ceph_osdmap *map = osdc->osdmap;
2498
2499 verify_osdc_wrlocked(osdc);
2500 WARN_ON(!map->epoch);
2501
2502 if (lreq->register_gen) {
2503 lreq->map_dne_bound = map->epoch;
2504 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2505 lreq, lreq->linger_id);
2506 } else {
2507 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2508 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2509 map->epoch);
2510 }
2511
2512 if (lreq->map_dne_bound) {
2513 if (map->epoch >= lreq->map_dne_bound) {
2514 /* we had a new enough map */
2515 pr_info("linger_id %llu pool does not exist\n",
2516 lreq->linger_id);
2517 linger_reg_commit_complete(lreq, -ENOENT);
2518 __linger_cancel(lreq);
2519 }
2520 } else {
2521 send_linger_map_check(lreq);
2522 }
2523}
2524
2525static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2526{
2527 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2528 struct ceph_osd_linger_request *lreq;
2529 u64 linger_id = greq->private_data;
2530
2531 WARN_ON(greq->result || !greq->u.newest);
2532
2533 down_write(&osdc->lock);
2534 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2535 if (!lreq) {
2536 dout("%s linger_id %llu dne\n", __func__, linger_id);
2537 goto out_unlock;
2538 }
2539
2540 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
2541 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2542 greq->u.newest);
2543 if (!lreq->map_dne_bound)
2544 lreq->map_dne_bound = greq->u.newest;
2545 erase_linger_mc(&osdc->linger_map_checks, lreq);
2546 check_linger_pool_dne(lreq);
2547
2548 linger_put(lreq);
2549out_unlock:
2550 up_write(&osdc->lock);
2551}
2552
2553static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
2554{
2555 struct ceph_osd_client *osdc = lreq->osdc;
2556 struct ceph_osd_linger_request *lookup_lreq;
2557 int ret;
2558
2559 verify_osdc_wrlocked(osdc);
2560
2561 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2562 lreq->linger_id);
2563 if (lookup_lreq) {
2564 WARN_ON(lookup_lreq != lreq);
2565 return;
2566 }
2567
2568 linger_get(lreq);
2569 insert_linger_mc(&osdc->linger_map_checks, lreq);
2570 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2571 linger_map_check_cb, lreq->linger_id);
2572 WARN_ON(ret);
2573}
2574
Ilya Dryomov922dab62016-05-26 01:15:02 +02002575static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
2576{
2577 int ret;
2578
2579 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2580 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
2581 return ret ?: lreq->reg_commit_error;
2582}
2583
Ilya Dryomov19079202016-04-28 16:07:27 +02002584static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
2585{
2586 int ret;
2587
2588 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2589 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
2590 return ret ?: lreq->notify_finish_error;
2591}
2592
Ilya Dryomov922dab62016-05-26 01:15:02 +02002593/*
Ilya Dryomovfbca9632016-04-28 16:07:24 +02002594 * Timeout callback, called every N seconds. When 1 or more OSD
2595 * requests has been active for more than N seconds, we send a keepalive
2596 * (tag + timestamp) to its OSD to ensure any communications channel
2597 * reset is detected.
Sage Weilf24e9982009-10-06 11:31:10 -07002598 */
2599static void handle_timeout(struct work_struct *work)
2600{
2601 struct ceph_osd_client *osdc =
2602 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03002603 struct ceph_options *opts = osdc->client->options;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002604 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002605 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002606 LIST_HEAD(slow_osds);
2607 struct rb_node *n, *p;
Sage Weilf24e9982009-10-06 11:31:10 -07002608
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002609 dout("%s osdc %p\n", __func__, osdc);
2610 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07002611
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002612 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002613 * ping osds that are a bit slow. this ensures that if there
2614 * is a break in the TCP connection we will notice, and reopen
2615 * a connection with that osd (from the fault callback).
2616 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002617 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2618 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2619 bool found = false;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002620
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002621 for (p = rb_first(&osd->o_requests); p; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002622 struct ceph_osd_request *req =
2623 rb_entry(p, struct ceph_osd_request, r_node);
2624
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002625 p = rb_next(p); /* abort_request() */
2626
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002627 if (time_before(req->r_stamp, cutoff)) {
2628 dout(" req %p tid %llu on osd%d is laggy\n",
2629 req, req->r_tid, osd->o_osd);
2630 found = true;
2631 }
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002632 if (opts->osd_request_timeout &&
2633 time_before(req->r_start_stamp, expiry_cutoff)) {
2634 pr_err_ratelimited("tid %llu on osd%d timeout\n",
2635 req->r_tid, osd->o_osd);
2636 abort_request(req, -ETIMEDOUT);
2637 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002638 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02002639 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
2640 struct ceph_osd_linger_request *lreq =
2641 rb_entry(p, struct ceph_osd_linger_request, node);
2642
2643 dout(" lreq %p linger_id %llu is served by osd%d\n",
2644 lreq, lreq->linger_id, osd->o_osd);
2645 found = true;
2646
2647 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002648 if (lreq->is_watch && lreq->committed && !lreq->last_error)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002649 send_linger_ping(lreq);
2650 mutex_unlock(&lreq->lock);
2651 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002652
2653 if (found)
2654 list_move_tail(&osd->o_keepalive_item, &slow_osds);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002655 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002656
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002657 if (opts->osd_request_timeout) {
2658 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
2659 struct ceph_osd_request *req =
2660 rb_entry(p, struct ceph_osd_request, r_node);
2661
2662 p = rb_next(p); /* abort_request() */
2663
2664 if (time_before(req->r_start_stamp, expiry_cutoff)) {
2665 pr_err_ratelimited("tid %llu on osd%d timeout\n",
2666 req->r_tid, osdc->homeless_osd.o_osd);
2667 abort_request(req, -ETIMEDOUT);
2668 }
2669 }
2670 }
2671
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002672 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
2673 maybe_request_map(osdc);
2674
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002675 while (!list_empty(&slow_osds)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002676 struct ceph_osd *osd = list_first_entry(&slow_osds,
2677 struct ceph_osd,
2678 o_keepalive_item);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002679 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07002680 ceph_con_keepalive(&osd->o_con);
2681 }
2682
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002683 up_write(&osdc->lock);
Ilya Dryomovfbca9632016-04-28 16:07:24 +02002684 schedule_delayed_work(&osdc->timeout_work,
2685 osdc->client->options->osd_keepalive_timeout);
Sage Weilf24e9982009-10-06 11:31:10 -07002686}
2687
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002688static void handle_osds_timeout(struct work_struct *work)
2689{
2690 struct ceph_osd_client *osdc =
2691 container_of(work, struct ceph_osd_client,
2692 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03002693 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002694 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002695
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002696 dout("%s osdc %p\n", __func__, osdc);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002697 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002698 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
2699 if (time_before(jiffies, osd->lru_ttl))
2700 break;
2701
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002702 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002703 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002704 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002705 }
2706
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002707 up_write(&osdc->lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002708 schedule_delayed_work(&osdc->osds_timeout_work,
2709 round_jiffies_relative(delay));
2710}
2711
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002712static int ceph_oloc_decode(void **p, void *end,
2713 struct ceph_object_locator *oloc)
2714{
2715 u8 struct_v, struct_cv;
2716 u32 len;
2717 void *struct_end;
2718 int ret = 0;
2719
2720 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2721 struct_v = ceph_decode_8(p);
2722 struct_cv = ceph_decode_8(p);
2723 if (struct_v < 3) {
2724 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
2725 struct_v, struct_cv);
2726 goto e_inval;
2727 }
2728 if (struct_cv > 6) {
2729 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
2730 struct_v, struct_cv);
2731 goto e_inval;
2732 }
2733 len = ceph_decode_32(p);
2734 ceph_decode_need(p, end, len, e_inval);
2735 struct_end = *p + len;
2736
2737 oloc->pool = ceph_decode_64(p);
2738 *p += 4; /* skip preferred */
2739
2740 len = ceph_decode_32(p);
2741 if (len > 0) {
2742 pr_warn("ceph_object_locator::key is set\n");
2743 goto e_inval;
2744 }
2745
2746 if (struct_v >= 5) {
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002747 bool changed = false;
2748
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002749 len = ceph_decode_32(p);
2750 if (len > 0) {
Yan, Zheng30c156d2016-02-14 11:24:31 +08002751 ceph_decode_need(p, end, len, e_inval);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002752 if (!oloc->pool_ns ||
2753 ceph_compare_string(oloc->pool_ns, *p, len))
2754 changed = true;
Yan, Zheng30c156d2016-02-14 11:24:31 +08002755 *p += len;
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002756 } else {
2757 if (oloc->pool_ns)
2758 changed = true;
2759 }
2760 if (changed) {
2761 /* redirect changes namespace */
2762 pr_warn("ceph_object_locator::nspace is changed\n");
2763 goto e_inval;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002764 }
2765 }
2766
2767 if (struct_v >= 6) {
2768 s64 hash = ceph_decode_64(p);
2769 if (hash != -1) {
2770 pr_warn("ceph_object_locator::hash is set\n");
2771 goto e_inval;
2772 }
2773 }
2774
2775 /* skip the rest */
2776 *p = struct_end;
2777out:
2778 return ret;
2779
2780e_inval:
2781 ret = -EINVAL;
2782 goto out;
2783}
2784
2785static int ceph_redirect_decode(void **p, void *end,
2786 struct ceph_request_redirect *redir)
2787{
2788 u8 struct_v, struct_cv;
2789 u32 len;
2790 void *struct_end;
2791 int ret;
2792
2793 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2794 struct_v = ceph_decode_8(p);
2795 struct_cv = ceph_decode_8(p);
2796 if (struct_cv > 1) {
2797 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
2798 struct_v, struct_cv);
2799 goto e_inval;
2800 }
2801 len = ceph_decode_32(p);
2802 ceph_decode_need(p, end, len, e_inval);
2803 struct_end = *p + len;
2804
2805 ret = ceph_oloc_decode(p, end, &redir->oloc);
2806 if (ret)
2807 goto out;
2808
2809 len = ceph_decode_32(p);
2810 if (len > 0) {
2811 pr_warn("ceph_request_redirect::object_name is set\n");
2812 goto e_inval;
2813 }
2814
2815 len = ceph_decode_32(p);
2816 *p += len; /* skip osd_instructions */
2817
2818 /* skip the rest */
2819 *p = struct_end;
2820out:
2821 return ret;
2822
2823e_inval:
2824 ret = -EINVAL;
2825 goto out;
2826}
2827
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002828struct MOSDOpReply {
2829 struct ceph_pg pgid;
2830 u64 flags;
2831 int result;
2832 u32 epoch;
2833 int num_ops;
2834 u32 outdata_len[CEPH_OSD_MAX_OPS];
2835 s32 rval[CEPH_OSD_MAX_OPS];
2836 int retry_attempt;
2837 struct ceph_eversion replay_version;
2838 u64 user_version;
2839 struct ceph_request_redirect redirect;
2840};
Sage Weil25845472011-06-03 09:37:09 -07002841
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002842static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
Sage Weilf24e9982009-10-06 11:31:10 -07002843{
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002844 void *p = msg->front.iov_base;
2845 void *const end = p + msg->front.iov_len;
2846 u16 version = le16_to_cpu(msg->hdr.version);
2847 struct ceph_eversion bad_replay_version;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01002848 u8 decode_redir;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002849 u32 len;
2850 int ret;
2851 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07002852
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002853 ceph_decode_32_safe(&p, end, len, e_inval);
2854 ceph_decode_need(&p, end, len, e_inval);
2855 p += len; /* skip oid */
Sage Weil1b83bef2013-02-25 16:11:12 -08002856
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002857 ret = ceph_decode_pgid(&p, end, &m->pgid);
2858 if (ret)
2859 return ret;
Sage Weil1b83bef2013-02-25 16:11:12 -08002860
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002861 ceph_decode_64_safe(&p, end, m->flags, e_inval);
2862 ceph_decode_32_safe(&p, end, m->result, e_inval);
2863 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
2864 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
2865 p += sizeof(bad_replay_version);
2866 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
Sage Weil1b83bef2013-02-25 16:11:12 -08002867
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002868 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
2869 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
2870 goto e_inval;
Sage Weil1b83bef2013-02-25 16:11:12 -08002871
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002872 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
2873 e_inval);
2874 for (i = 0; i < m->num_ops; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -08002875 struct ceph_osd_op *op = p;
Sage Weil1b83bef2013-02-25 16:11:12 -08002876
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002877 m->outdata_len[i] = le32_to_cpu(op->payload_len);
Sage Weil1b83bef2013-02-25 16:11:12 -08002878 p += sizeof(*op);
2879 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002880
2881 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
2882 for (i = 0; i < m->num_ops; i++)
2883 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
2884
2885 if (version >= 5) {
2886 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
2887 memcpy(&m->replay_version, p, sizeof(m->replay_version));
2888 p += sizeof(m->replay_version);
2889 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
2890 } else {
2891 m->replay_version = bad_replay_version; /* struct */
2892 m->user_version = le64_to_cpu(m->replay_version.version);
Sage Weil1b83bef2013-02-25 16:11:12 -08002893 }
2894
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002895 if (version >= 6) {
2896 if (version >= 7)
2897 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01002898 else
2899 decode_redir = 1;
2900 } else {
2901 decode_redir = 0;
2902 }
2903
2904 if (decode_redir) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002905 ret = ceph_redirect_decode(&p, end, &m->redirect);
2906 if (ret)
2907 return ret;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002908 } else {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002909 ceph_oloc_init(&m->redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002910 }
2911
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002912 return 0;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002913
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002914e_inval:
2915 return -EINVAL;
2916}
2917
2918/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01002919 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
2920 * specified.
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002921 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002922static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002923{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002924 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002925 struct ceph_osd_request *req;
2926 struct MOSDOpReply m;
2927 u64 tid = le64_to_cpu(msg->hdr.tid);
2928 u32 data_len = 0;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002929 int ret;
2930 int i;
2931
2932 dout("%s msg %p tid %llu\n", __func__, msg, tid);
2933
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002934 down_read(&osdc->lock);
2935 if (!osd_registered(osd)) {
2936 dout("%s osd%d unknown\n", __func__, osd->o_osd);
2937 goto out_unlock_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002938 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002939 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
2940
2941 mutex_lock(&osd->lock);
2942 req = lookup_request(&osd->o_requests, tid);
2943 if (!req) {
2944 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
2945 goto out_unlock_session;
2946 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002947
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002948 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002949 ret = decode_MOSDOpReply(msg, &m);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002950 m.redirect.oloc.pool_ns = NULL;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002951 if (ret) {
2952 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
2953 req->r_tid, ret);
2954 ceph_msg_dump(msg);
2955 goto fail_request;
2956 }
2957 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
2958 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
2959 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
2960 le64_to_cpu(m.replay_version.version), m.user_version);
2961
2962 if (m.retry_attempt >= 0) {
2963 if (m.retry_attempt != req->r_attempts - 1) {
2964 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
2965 req, req->r_tid, m.retry_attempt,
2966 req->r_attempts - 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002967 goto out_unlock_session;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002968 }
2969 } else {
2970 WARN_ON(1); /* MOSDOpReply v4 is assumed */
2971 }
2972
2973 if (!ceph_oloc_empty(&m.redirect.oloc)) {
2974 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
2975 m.redirect.oloc.pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002976 unlink_request(osd, req);
2977 mutex_unlock(&osd->lock);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002978
Yan, Zheng30c156d2016-02-14 11:24:31 +08002979 /*
2980 * Not ceph_oloc_copy() - changing pool_ns is not
2981 * supported.
2982 */
2983 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002984 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
2985 req->r_tid = 0;
2986 __submit_request(req, false);
2987 goto out_unlock_osdc;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002988 }
2989
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002990 if (m.num_ops != req->r_num_ops) {
2991 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
2992 req->r_num_ops, req->r_tid);
2993 goto fail_request;
2994 }
2995 for (i = 0; i < req->r_num_ops; i++) {
2996 dout(" req %p tid %llu op %d rval %d len %u\n", req,
2997 req->r_tid, i, m.rval[i], m.outdata_len[i]);
2998 req->r_ops[i].rval = m.rval[i];
2999 req->r_ops[i].outdata_len = m.outdata_len[i];
3000 data_len += m.outdata_len[i];
3001 }
3002 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3003 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3004 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3005 goto fail_request;
3006 }
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003007 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3008 req, req->r_tid, m.result, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07003009
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003010 /*
3011 * Since we only ever request ONDISK, we should only ever get
3012 * one (type of) reply back.
3013 */
3014 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3015 req->r_result = m.result ?: data_len;
3016 finish_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003017 mutex_unlock(&osd->lock);
3018 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003019
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003020 __complete_request(req);
3021 complete_all(&req->r_completion);
3022 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07003023 return;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003024
3025fail_request:
Ilya Dryomov46092452016-04-28 16:07:27 +02003026 complete_request(req, -EIO);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003027out_unlock_session:
3028 mutex_unlock(&osd->lock);
3029out_unlock_osdc:
3030 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003031}
3032
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003033static void set_pool_was_full(struct ceph_osd_client *osdc)
3034{
3035 struct rb_node *n;
3036
3037 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3038 struct ceph_pg_pool_info *pi =
3039 rb_entry(n, struct ceph_pg_pool_info, node);
3040
3041 pi->was_full = __pool_full(pi);
3042 }
3043}
3044
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003045static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
Sage Weilf24e9982009-10-06 11:31:10 -07003046{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003047 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07003048
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003049 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3050 if (!pi)
3051 return false;
Sage Weilf24e9982009-10-06 11:31:10 -07003052
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003053 return pi->was_full && !__pool_full(pi);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003054}
3055
Ilya Dryomov922dab62016-05-26 01:15:02 +02003056static enum calc_target_result
3057recalc_linger_target(struct ceph_osd_linger_request *lreq)
3058{
3059 struct ceph_osd_client *osdc = lreq->osdc;
3060 enum calc_target_result ct_res;
3061
3062 ct_res = calc_target(osdc, &lreq->t, &lreq->last_force_resend, true);
3063 if (ct_res == CALC_TARGET_NEED_RESEND) {
3064 struct ceph_osd *osd;
3065
3066 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3067 if (osd != lreq->osd) {
3068 unlink_linger(lreq->osd, lreq);
3069 link_linger(osd, lreq);
3070 }
3071 }
3072
3073 return ct_res;
3074}
3075
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003076/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003077 * Requeue requests whose mapping to an OSD has changed.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003078 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003079static void scan_requests(struct ceph_osd *osd,
3080 bool force_resend,
3081 bool cleared_full,
3082 bool check_pool_cleared_full,
3083 struct rb_root *need_resend,
3084 struct list_head *need_resend_linger)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003085{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003086 struct ceph_osd_client *osdc = osd->o_osdc;
3087 struct rb_node *n;
3088 bool force_resend_writes;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003089
Ilya Dryomov922dab62016-05-26 01:15:02 +02003090 for (n = rb_first(&osd->o_linger_requests); n; ) {
3091 struct ceph_osd_linger_request *lreq =
3092 rb_entry(n, struct ceph_osd_linger_request, node);
3093 enum calc_target_result ct_res;
3094
3095 n = rb_next(n); /* recalc_linger_target() */
3096
3097 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3098 lreq->linger_id);
3099 ct_res = recalc_linger_target(lreq);
3100 switch (ct_res) {
3101 case CALC_TARGET_NO_ACTION:
3102 force_resend_writes = cleared_full ||
3103 (check_pool_cleared_full &&
3104 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3105 if (!force_resend && !force_resend_writes)
3106 break;
3107
3108 /* fall through */
3109 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003110 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003111 /*
3112 * scan_requests() for the previous epoch(s)
3113 * may have already added it to the list, since
3114 * it's not unlinked here.
3115 */
3116 if (list_empty(&lreq->scan_item))
3117 list_add_tail(&lreq->scan_item, need_resend_linger);
3118 break;
3119 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003120 check_linger_pool_dne(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003121 break;
3122 }
3123 }
3124
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003125 for (n = rb_first(&osd->o_requests); n; ) {
3126 struct ceph_osd_request *req =
3127 rb_entry(n, struct ceph_osd_request, r_node);
3128 enum calc_target_result ct_res;
Alex Elderab60b162012-12-19 15:52:36 -06003129
Ilya Dryomov46092452016-04-28 16:07:27 +02003130 n = rb_next(n); /* unlink_request(), check_pool_dne() */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003131
3132 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3133 ct_res = calc_target(osdc, &req->r_t,
3134 &req->r_last_force_resend, false);
3135 switch (ct_res) {
3136 case CALC_TARGET_NO_ACTION:
3137 force_resend_writes = cleared_full ||
3138 (check_pool_cleared_full &&
3139 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3140 if (!force_resend &&
3141 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3142 !force_resend_writes))
3143 break;
3144
3145 /* fall through */
3146 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003147 cancel_map_check(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003148 unlink_request(osd, req);
3149 insert_request(need_resend, req);
3150 break;
3151 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003152 check_pool_dne(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003153 break;
Alex Elderab60b162012-12-19 15:52:36 -06003154 }
Sage Weilf24e9982009-10-06 11:31:10 -07003155 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003156}
Sage Weil6f6c7002011-01-17 20:34:08 -08003157
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003158static int handle_one_map(struct ceph_osd_client *osdc,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003159 void *p, void *end, bool incremental,
3160 struct rb_root *need_resend,
3161 struct list_head *need_resend_linger)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003162{
3163 struct ceph_osdmap *newmap;
3164 struct rb_node *n;
3165 bool skipped_map = false;
3166 bool was_full;
3167
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003168 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003169 set_pool_was_full(osdc);
3170
3171 if (incremental)
3172 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3173 else
3174 newmap = ceph_osdmap_decode(&p, end);
3175 if (IS_ERR(newmap))
3176 return PTR_ERR(newmap);
3177
3178 if (newmap != osdc->osdmap) {
3179 /*
3180 * Preserve ->was_full before destroying the old map.
3181 * For pools that weren't in the old map, ->was_full
3182 * should be false.
3183 */
3184 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3185 struct ceph_pg_pool_info *pi =
3186 rb_entry(n, struct ceph_pg_pool_info, node);
3187 struct ceph_pg_pool_info *old_pi;
3188
3189 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3190 if (old_pi)
3191 pi->was_full = old_pi->was_full;
3192 else
3193 WARN_ON(pi->was_full);
3194 }
3195
3196 if (osdc->osdmap->epoch &&
3197 osdc->osdmap->epoch + 1 < newmap->epoch) {
3198 WARN_ON(incremental);
3199 skipped_map = true;
3200 }
3201
3202 ceph_osdmap_destroy(osdc->osdmap);
3203 osdc->osdmap = newmap;
3204 }
3205
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003206 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003207 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3208 need_resend, need_resend_linger);
3209
3210 for (n = rb_first(&osdc->osds); n; ) {
3211 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3212
3213 n = rb_next(n); /* close_osd() */
3214
3215 scan_requests(osd, skipped_map, was_full, true, need_resend,
3216 need_resend_linger);
3217 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3218 memcmp(&osd->o_con.peer_addr,
3219 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3220 sizeof(struct ceph_entity_addr)))
3221 close_osd(osd);
3222 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003223
3224 return 0;
3225}
Sage Weil6f6c7002011-01-17 20:34:08 -08003226
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003227static void kick_requests(struct ceph_osd_client *osdc,
3228 struct rb_root *need_resend,
3229 struct list_head *need_resend_linger)
3230{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003231 struct ceph_osd_linger_request *lreq, *nlreq;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003232 struct rb_node *n;
3233
3234 for (n = rb_first(need_resend); n; ) {
3235 struct ceph_osd_request *req =
3236 rb_entry(n, struct ceph_osd_request, r_node);
3237 struct ceph_osd *osd;
3238
3239 n = rb_next(n);
3240 erase_request(need_resend, req); /* before link_request() */
3241
3242 WARN_ON(req->r_osd);
3243 calc_target(osdc, &req->r_t, NULL, false);
3244 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3245 link_request(osd, req);
3246 if (!req->r_linger) {
3247 if (!osd_homeless(osd) && !req->r_t.paused)
3248 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003249 } else {
3250 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003251 }
3252 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003253
3254 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3255 if (!osd_homeless(lreq->osd))
3256 send_linger(lreq);
3257
3258 list_del_init(&lreq->scan_item);
3259 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003260}
3261
Sage Weilf24e9982009-10-06 11:31:10 -07003262/*
3263 * Process updated osd map.
3264 *
3265 * The message contains any number of incremental and full maps, normally
3266 * indicating some sort of topology change in the cluster. Kick requests
3267 * off to different OSDs as needed.
3268 */
3269void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3270{
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003271 void *p = msg->front.iov_base;
3272 void *const end = p + msg->front.iov_len;
Sage Weilf24e9982009-10-06 11:31:10 -07003273 u32 nr_maps, maplen;
3274 u32 epoch;
Sage Weilf24e9982009-10-06 11:31:10 -07003275 struct ceph_fsid fsid;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003276 struct rb_root need_resend = RB_ROOT;
3277 LIST_HEAD(need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003278 bool handled_incremental = false;
3279 bool was_pauserd, was_pausewr;
3280 bool pauserd, pausewr;
3281 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07003282
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003283 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003284 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003285
3286 /* verify fsid */
3287 ceph_decode_need(&p, end, sizeof(fsid), bad);
3288 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08003289 if (ceph_check_fsid(osdc->client, &fsid) < 0)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003290 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003291
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003292 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3293 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3294 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003295 have_pool_full(osdc);
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08003296
Sage Weilf24e9982009-10-06 11:31:10 -07003297 /* incremental maps */
3298 ceph_decode_32_safe(&p, end, nr_maps, bad);
3299 dout(" %d inc maps\n", nr_maps);
3300 while (nr_maps > 0) {
3301 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003302 epoch = ceph_decode_32(&p);
3303 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003304 ceph_decode_need(&p, end, maplen, bad);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003305 if (osdc->osdmap->epoch &&
3306 osdc->osdmap->epoch + 1 == epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003307 dout("applying incremental map %u len %d\n",
3308 epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003309 err = handle_one_map(osdc, p, p + maplen, true,
3310 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003311 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003312 goto bad;
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003313 handled_incremental = true;
Sage Weilf24e9982009-10-06 11:31:10 -07003314 } else {
3315 dout("ignoring incremental map %u len %d\n",
3316 epoch, maplen);
3317 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003318 p += maplen;
Sage Weilf24e9982009-10-06 11:31:10 -07003319 nr_maps--;
3320 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003321 if (handled_incremental)
Sage Weilf24e9982009-10-06 11:31:10 -07003322 goto done;
3323
3324 /* full maps */
3325 ceph_decode_32_safe(&p, end, nr_maps, bad);
3326 dout(" %d full maps\n", nr_maps);
3327 while (nr_maps) {
3328 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003329 epoch = ceph_decode_32(&p);
3330 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003331 ceph_decode_need(&p, end, maplen, bad);
3332 if (nr_maps > 1) {
3333 dout("skipping non-latest full map %u len %d\n",
3334 epoch, maplen);
Ilya Dryomove5253a72016-04-28 16:07:25 +02003335 } else if (osdc->osdmap->epoch >= epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003336 dout("skipping full map %u len %d, "
3337 "older than our %u\n", epoch, maplen,
3338 osdc->osdmap->epoch);
3339 } else {
3340 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003341 err = handle_one_map(osdc, p, p + maplen, false,
3342 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003343 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003344 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003345 }
3346 p += maplen;
3347 nr_maps--;
3348 }
3349
3350done:
Sage Weilcd634fb2011-05-12 09:29:18 -07003351 /*
3352 * subscribe to subsequent osdmap updates if full to ensure
3353 * we find out when we are no longer full and stop returning
3354 * ENOSPC.
3355 */
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003356 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3357 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3358 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003359 have_pool_full(osdc);
Jeff Layton58eb7932017-04-18 09:21:16 -04003360 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3361 osdc->osdmap->epoch < osdc->epoch_barrier)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003362 maybe_request_map(osdc);
Sage Weilcd634fb2011-05-12 09:29:18 -07003363
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003364 kick_requests(osdc, &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003365
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04003366 ceph_osdc_abort_on_full(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003367 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3368 osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003369 up_write(&osdc->lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07003370 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07003371 return;
3372
3373bad:
3374 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003375 ceph_msg_dump(msg);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003376 up_write(&osdc->lock);
3377}
3378
3379/*
3380 * Resubmit requests pending on the given osd.
3381 */
3382static void kick_osd_requests(struct ceph_osd *osd)
3383{
3384 struct rb_node *n;
3385
Ilya Dryomov922dab62016-05-26 01:15:02 +02003386 for (n = rb_first(&osd->o_requests); n; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003387 struct ceph_osd_request *req =
3388 rb_entry(n, struct ceph_osd_request, r_node);
3389
Ilya Dryomov922dab62016-05-26 01:15:02 +02003390 n = rb_next(n); /* cancel_linger_request() */
3391
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003392 if (!req->r_linger) {
3393 if (!req->r_t.paused)
3394 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003395 } else {
3396 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003397 }
3398 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003399 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3400 struct ceph_osd_linger_request *lreq =
3401 rb_entry(n, struct ceph_osd_linger_request, node);
3402
3403 send_linger(lreq);
3404 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003405}
3406
3407/*
3408 * If the osd connection drops, we need to resubmit all requests.
3409 */
3410static void osd_fault(struct ceph_connection *con)
3411{
3412 struct ceph_osd *osd = con->private;
3413 struct ceph_osd_client *osdc = osd->o_osdc;
3414
3415 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3416
3417 down_write(&osdc->lock);
3418 if (!osd_registered(osd)) {
3419 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3420 goto out_unlock;
3421 }
3422
3423 if (!reopen_osd(osd))
3424 kick_osd_requests(osd);
3425 maybe_request_map(osdc);
3426
3427out_unlock:
3428 up_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003429}
3430
Sage Weilf24e9982009-10-06 11:31:10 -07003431/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003432 * Process osd watch notifications
3433 */
Alex Elder3c663bb2013-02-15 11:42:30 -06003434static void handle_watch_notify(struct ceph_osd_client *osdc,
3435 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003436{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003437 void *p = msg->front.iov_base;
3438 void *const end = p + msg->front.iov_len;
3439 struct ceph_osd_linger_request *lreq;
3440 struct linger_work *lwork;
3441 u8 proto_ver, opcode;
3442 u64 cookie, notify_id;
3443 u64 notifier_id = 0;
Ilya Dryomov19079202016-04-28 16:07:27 +02003444 s32 return_code = 0;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003445 void *payload = NULL;
3446 u32 payload_len = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003447
3448 ceph_decode_8_safe(&p, end, proto_ver, bad);
3449 ceph_decode_8_safe(&p, end, opcode, bad);
3450 ceph_decode_64_safe(&p, end, cookie, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003451 p += 8; /* skip ver */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003452 ceph_decode_64_safe(&p, end, notify_id, bad);
3453
Ilya Dryomov922dab62016-05-26 01:15:02 +02003454 if (proto_ver >= 1) {
3455 ceph_decode_32_safe(&p, end, payload_len, bad);
3456 ceph_decode_need(&p, end, payload_len, bad);
3457 payload = p;
3458 p += payload_len;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003459 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003460
3461 if (le16_to_cpu(msg->hdr.version) >= 2)
Ilya Dryomov19079202016-04-28 16:07:27 +02003462 ceph_decode_32_safe(&p, end, return_code, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003463
3464 if (le16_to_cpu(msg->hdr.version) >= 3)
3465 ceph_decode_64_safe(&p, end, notifier_id, bad);
3466
3467 down_read(&osdc->lock);
3468 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
3469 if (!lreq) {
3470 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
3471 cookie);
3472 goto out_unlock_osdc;
3473 }
3474
3475 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02003476 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
3477 opcode, cookie, lreq, lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003478 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
3479 if (!lreq->last_error) {
3480 lreq->last_error = -ENOTCONN;
3481 queue_watch_error(lreq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003482 }
Ilya Dryomov19079202016-04-28 16:07:27 +02003483 } else if (!lreq->is_watch) {
3484 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
3485 if (lreq->notify_id && lreq->notify_id != notify_id) {
3486 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
3487 lreq->notify_id, notify_id);
3488 } else if (!completion_done(&lreq->notify_finish_wait)) {
3489 struct ceph_msg_data *data =
3490 list_first_entry_or_null(&msg->data,
3491 struct ceph_msg_data,
3492 links);
3493
3494 if (data) {
3495 if (lreq->preply_pages) {
3496 WARN_ON(data->type !=
3497 CEPH_MSG_DATA_PAGES);
3498 *lreq->preply_pages = data->pages;
3499 *lreq->preply_len = data->length;
3500 } else {
3501 ceph_release_page_vector(data->pages,
3502 calc_pages_for(0, data->length));
3503 }
3504 }
3505 lreq->notify_finish_error = return_code;
3506 complete_all(&lreq->notify_finish_wait);
3507 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003508 } else {
3509 /* CEPH_WATCH_EVENT_NOTIFY */
3510 lwork = lwork_alloc(lreq, do_watch_notify);
3511 if (!lwork) {
3512 pr_err("failed to allocate notify-lwork\n");
3513 goto out_unlock_lreq;
3514 }
Ilya Dryomov91883cd2014-09-11 12:18:53 +04003515
Ilya Dryomov922dab62016-05-26 01:15:02 +02003516 lwork->notify.notify_id = notify_id;
3517 lwork->notify.notifier_id = notifier_id;
3518 lwork->notify.payload = payload;
3519 lwork->notify.payload_len = payload_len;
3520 lwork->notify.msg = ceph_msg_get(msg);
3521 lwork_queue(lwork);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003522 }
3523
Ilya Dryomov922dab62016-05-26 01:15:02 +02003524out_unlock_lreq:
3525 mutex_unlock(&lreq->lock);
3526out_unlock_osdc:
3527 up_read(&osdc->lock);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003528 return;
3529
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003530bad:
3531 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003532}
3533
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003534/*
Sage Weilf24e9982009-10-06 11:31:10 -07003535 * Register request, send initial attempt.
3536 */
3537int ceph_osdc_start_request(struct ceph_osd_client *osdc,
3538 struct ceph_osd_request *req,
3539 bool nofail)
3540{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003541 down_read(&osdc->lock);
3542 submit_request(req, false);
3543 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003544
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003545 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07003546}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003547EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07003548
3549/*
Ilya Dryomovc297eb422016-12-02 14:01:55 +01003550 * Unregister a registered request. The request is not completed:
3551 * ->r_result isn't set and __complete_request() isn't called.
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04003552 */
3553void ceph_osdc_cancel_request(struct ceph_osd_request *req)
3554{
3555 struct ceph_osd_client *osdc = req->r_osdc;
3556
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003557 down_write(&osdc->lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003558 if (req->r_osd)
3559 cancel_request(req);
3560 up_write(&osdc->lock);
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04003561}
3562EXPORT_SYMBOL(ceph_osdc_cancel_request);
3563
3564/*
Ilya Dryomov42b06962016-04-28 16:07:26 +02003565 * @timeout: in jiffies, 0 means "wait forever"
3566 */
3567static int wait_request_timeout(struct ceph_osd_request *req,
3568 unsigned long timeout)
3569{
3570 long left;
3571
3572 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Yan, Zheng0e76abf2016-05-13 11:04:33 +08003573 left = wait_for_completion_killable_timeout(&req->r_completion,
Ilya Dryomov42b06962016-04-28 16:07:26 +02003574 ceph_timeout_jiffies(timeout));
3575 if (left <= 0) {
3576 left = left ?: -ETIMEDOUT;
3577 ceph_osdc_cancel_request(req);
Ilya Dryomov42b06962016-04-28 16:07:26 +02003578 } else {
3579 left = req->r_result; /* completed */
3580 }
3581
3582 return left;
3583}
3584
3585/*
Sage Weilf24e9982009-10-06 11:31:10 -07003586 * wait for a request to complete
3587 */
3588int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
3589 struct ceph_osd_request *req)
3590{
Ilya Dryomov42b06962016-04-28 16:07:26 +02003591 return wait_request_timeout(req, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07003592}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003593EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07003594
3595/*
3596 * sync - wait for all in-flight requests to flush. avoid starvation.
3597 */
3598void ceph_osdc_sync(struct ceph_osd_client *osdc)
3599{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003600 struct rb_node *n, *p;
3601 u64 last_tid = atomic64_read(&osdc->last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07003602
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003603again:
3604 down_read(&osdc->lock);
3605 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3606 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07003607
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003608 mutex_lock(&osd->lock);
3609 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
3610 struct ceph_osd_request *req =
3611 rb_entry(p, struct ceph_osd_request, r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07003612
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003613 if (req->r_tid > last_tid)
3614 break;
3615
3616 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
3617 continue;
3618
3619 ceph_osdc_get_request(req);
3620 mutex_unlock(&osd->lock);
3621 up_read(&osdc->lock);
3622 dout("%s waiting on req %p tid %llu last_tid %llu\n",
3623 __func__, req, req->r_tid, last_tid);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003624 wait_for_completion(&req->r_completion);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003625 ceph_osdc_put_request(req);
3626 goto again;
3627 }
3628
3629 mutex_unlock(&osd->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003630 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003631
3632 up_read(&osdc->lock);
3633 dout("%s done last_tid %llu\n", __func__, last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07003634}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003635EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07003636
Ilya Dryomov922dab62016-05-26 01:15:02 +02003637static struct ceph_osd_request *
3638alloc_linger_request(struct ceph_osd_linger_request *lreq)
3639{
3640 struct ceph_osd_request *req;
3641
3642 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
3643 if (!req)
3644 return NULL;
3645
3646 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3647 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3648
3649 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
3650 ceph_osdc_put_request(req);
3651 return NULL;
3652 }
3653
3654 return req;
3655}
3656
3657/*
3658 * Returns a handle, caller owns a ref.
3659 */
3660struct ceph_osd_linger_request *
3661ceph_osdc_watch(struct ceph_osd_client *osdc,
3662 struct ceph_object_id *oid,
3663 struct ceph_object_locator *oloc,
3664 rados_watchcb2_t wcb,
3665 rados_watcherrcb_t errcb,
3666 void *data)
3667{
3668 struct ceph_osd_linger_request *lreq;
3669 int ret;
3670
3671 lreq = linger_alloc(osdc);
3672 if (!lreq)
3673 return ERR_PTR(-ENOMEM);
3674
Ilya Dryomov19079202016-04-28 16:07:27 +02003675 lreq->is_watch = true;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003676 lreq->wcb = wcb;
3677 lreq->errcb = errcb;
3678 lreq->data = data;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02003679 lreq->watch_valid_thru = jiffies;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003680
3681 ceph_oid_copy(&lreq->t.base_oid, oid);
3682 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01003683 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07003684 ktime_get_real_ts(&lreq->mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003685
3686 lreq->reg_req = alloc_linger_request(lreq);
3687 if (!lreq->reg_req) {
3688 ret = -ENOMEM;
3689 goto err_put_lreq;
3690 }
3691
3692 lreq->ping_req = alloc_linger_request(lreq);
3693 if (!lreq->ping_req) {
3694 ret = -ENOMEM;
3695 goto err_put_lreq;
3696 }
3697
3698 down_write(&osdc->lock);
3699 linger_register(lreq); /* before osd_req_op_* */
3700 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
3701 CEPH_OSD_WATCH_OP_WATCH);
3702 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
3703 CEPH_OSD_WATCH_OP_PING);
3704 linger_submit(lreq);
3705 up_write(&osdc->lock);
3706
3707 ret = linger_reg_commit_wait(lreq);
3708 if (ret) {
3709 linger_cancel(lreq);
3710 goto err_put_lreq;
3711 }
3712
3713 return lreq;
3714
3715err_put_lreq:
3716 linger_put(lreq);
3717 return ERR_PTR(ret);
3718}
3719EXPORT_SYMBOL(ceph_osdc_watch);
3720
3721/*
3722 * Releases a ref.
3723 *
3724 * Times out after mount_timeout to preserve rbd unmap behaviour
3725 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
3726 * with mount_timeout").
3727 */
3728int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
3729 struct ceph_osd_linger_request *lreq)
3730{
3731 struct ceph_options *opts = osdc->client->options;
3732 struct ceph_osd_request *req;
3733 int ret;
3734
3735 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3736 if (!req)
3737 return -ENOMEM;
3738
3739 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3740 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01003741 req->r_flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07003742 ktime_get_real_ts(&req->r_mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003743 osd_req_op_watch_init(req, 0, lreq->linger_id,
3744 CEPH_OSD_WATCH_OP_UNWATCH);
3745
3746 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3747 if (ret)
3748 goto out_put_req;
3749
3750 ceph_osdc_start_request(osdc, req, false);
3751 linger_cancel(lreq);
3752 linger_put(lreq);
3753 ret = wait_request_timeout(req, opts->mount_timeout);
3754
3755out_put_req:
3756 ceph_osdc_put_request(req);
3757 return ret;
3758}
3759EXPORT_SYMBOL(ceph_osdc_unwatch);
3760
3761static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
3762 u64 notify_id, u64 cookie, void *payload,
3763 size_t payload_len)
3764{
3765 struct ceph_osd_req_op *op;
3766 struct ceph_pagelist *pl;
3767 int ret;
3768
3769 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
3770
3771 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3772 if (!pl)
3773 return -ENOMEM;
3774
3775 ceph_pagelist_init(pl);
3776 ret = ceph_pagelist_encode_64(pl, notify_id);
3777 ret |= ceph_pagelist_encode_64(pl, cookie);
3778 if (payload) {
3779 ret |= ceph_pagelist_encode_32(pl, payload_len);
3780 ret |= ceph_pagelist_append(pl, payload, payload_len);
3781 } else {
3782 ret |= ceph_pagelist_encode_32(pl, 0);
3783 }
3784 if (ret) {
3785 ceph_pagelist_release(pl);
3786 return -ENOMEM;
3787 }
3788
3789 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
3790 op->indata_len = pl->length;
3791 return 0;
3792}
3793
3794int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
3795 struct ceph_object_id *oid,
3796 struct ceph_object_locator *oloc,
3797 u64 notify_id,
3798 u64 cookie,
3799 void *payload,
3800 size_t payload_len)
3801{
3802 struct ceph_osd_request *req;
3803 int ret;
3804
3805 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3806 if (!req)
3807 return -ENOMEM;
3808
3809 ceph_oid_copy(&req->r_base_oid, oid);
3810 ceph_oloc_copy(&req->r_base_oloc, oloc);
3811 req->r_flags = CEPH_OSD_FLAG_READ;
3812
3813 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3814 if (ret)
3815 goto out_put_req;
3816
3817 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
3818 payload_len);
3819 if (ret)
3820 goto out_put_req;
3821
3822 ceph_osdc_start_request(osdc, req, false);
3823 ret = ceph_osdc_wait_request(osdc, req);
3824
3825out_put_req:
3826 ceph_osdc_put_request(req);
3827 return ret;
3828}
3829EXPORT_SYMBOL(ceph_osdc_notify_ack);
3830
Ilya Dryomov19079202016-04-28 16:07:27 +02003831static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
3832 u64 cookie, u32 prot_ver, u32 timeout,
3833 void *payload, size_t payload_len)
3834{
3835 struct ceph_osd_req_op *op;
3836 struct ceph_pagelist *pl;
3837 int ret;
3838
3839 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
3840 op->notify.cookie = cookie;
3841
3842 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3843 if (!pl)
3844 return -ENOMEM;
3845
3846 ceph_pagelist_init(pl);
3847 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
3848 ret |= ceph_pagelist_encode_32(pl, timeout);
3849 ret |= ceph_pagelist_encode_32(pl, payload_len);
3850 ret |= ceph_pagelist_append(pl, payload, payload_len);
3851 if (ret) {
3852 ceph_pagelist_release(pl);
3853 return -ENOMEM;
3854 }
3855
3856 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
3857 op->indata_len = pl->length;
3858 return 0;
3859}
3860
3861/*
3862 * @timeout: in seconds
3863 *
3864 * @preply_{pages,len} are initialized both on success and error.
3865 * The caller is responsible for:
3866 *
3867 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
3868 */
3869int ceph_osdc_notify(struct ceph_osd_client *osdc,
3870 struct ceph_object_id *oid,
3871 struct ceph_object_locator *oloc,
3872 void *payload,
3873 size_t payload_len,
3874 u32 timeout,
3875 struct page ***preply_pages,
3876 size_t *preply_len)
3877{
3878 struct ceph_osd_linger_request *lreq;
3879 struct page **pages;
3880 int ret;
3881
3882 WARN_ON(!timeout);
3883 if (preply_pages) {
3884 *preply_pages = NULL;
3885 *preply_len = 0;
3886 }
3887
3888 lreq = linger_alloc(osdc);
3889 if (!lreq)
3890 return -ENOMEM;
3891
3892 lreq->preply_pages = preply_pages;
3893 lreq->preply_len = preply_len;
3894
3895 ceph_oid_copy(&lreq->t.base_oid, oid);
3896 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3897 lreq->t.flags = CEPH_OSD_FLAG_READ;
3898
3899 lreq->reg_req = alloc_linger_request(lreq);
3900 if (!lreq->reg_req) {
3901 ret = -ENOMEM;
3902 goto out_put_lreq;
3903 }
3904
3905 /* for notify_id */
3906 pages = ceph_alloc_page_vector(1, GFP_NOIO);
3907 if (IS_ERR(pages)) {
3908 ret = PTR_ERR(pages);
3909 goto out_put_lreq;
3910 }
3911
3912 down_write(&osdc->lock);
3913 linger_register(lreq); /* before osd_req_op_* */
3914 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
3915 timeout, payload, payload_len);
3916 if (ret) {
3917 linger_unregister(lreq);
3918 up_write(&osdc->lock);
3919 ceph_release_page_vector(pages, 1);
3920 goto out_put_lreq;
3921 }
3922 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
3923 response_data),
3924 pages, PAGE_SIZE, 0, false, true);
3925 linger_submit(lreq);
3926 up_write(&osdc->lock);
3927
3928 ret = linger_reg_commit_wait(lreq);
3929 if (!ret)
3930 ret = linger_notify_finish_wait(lreq);
3931 else
3932 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
3933
3934 linger_cancel(lreq);
3935out_put_lreq:
3936 linger_put(lreq);
3937 return ret;
3938}
3939EXPORT_SYMBOL(ceph_osdc_notify);
3940
Sage Weilf24e9982009-10-06 11:31:10 -07003941/*
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02003942 * Return the number of milliseconds since the watch was last
3943 * confirmed, or an error. If there is an error, the watch is no
3944 * longer valid, and should be destroyed with ceph_osdc_unwatch().
3945 */
3946int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
3947 struct ceph_osd_linger_request *lreq)
3948{
3949 unsigned long stamp, age;
3950 int ret;
3951
3952 down_read(&osdc->lock);
3953 mutex_lock(&lreq->lock);
3954 stamp = lreq->watch_valid_thru;
3955 if (!list_empty(&lreq->pending_lworks)) {
3956 struct linger_work *lwork =
3957 list_first_entry(&lreq->pending_lworks,
3958 struct linger_work,
3959 pending_item);
3960
3961 if (time_before(lwork->queued_stamp, stamp))
3962 stamp = lwork->queued_stamp;
3963 }
3964 age = jiffies - stamp;
3965 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
3966 lreq, lreq->linger_id, age, lreq->last_error);
3967 /* we are truncating to msecs, so return a safe upper bound */
3968 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
3969
3970 mutex_unlock(&lreq->lock);
3971 up_read(&osdc->lock);
3972 return ret;
3973}
3974
Douglas Fullera4ed38d2015-07-17 13:18:07 -07003975static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
3976{
3977 u8 struct_v;
3978 u32 struct_len;
3979 int ret;
3980
3981 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
3982 &struct_v, &struct_len);
3983 if (ret)
3984 return ret;
3985
3986 ceph_decode_copy(p, &item->name, sizeof(item->name));
3987 item->cookie = ceph_decode_64(p);
3988 *p += 4; /* skip timeout_seconds */
3989 if (struct_v >= 2) {
3990 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
3991 ceph_decode_addr(&item->addr);
3992 }
3993
3994 dout("%s %s%llu cookie %llu addr %s\n", __func__,
3995 ENTITY_NAME(item->name), item->cookie,
3996 ceph_pr_addr(&item->addr.in_addr));
3997 return 0;
3998}
3999
4000static int decode_watchers(void **p, void *end,
4001 struct ceph_watch_item **watchers,
4002 u32 *num_watchers)
4003{
4004 u8 struct_v;
4005 u32 struct_len;
4006 int i;
4007 int ret;
4008
4009 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4010 &struct_v, &struct_len);
4011 if (ret)
4012 return ret;
4013
4014 *num_watchers = ceph_decode_32(p);
4015 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4016 if (!*watchers)
4017 return -ENOMEM;
4018
4019 for (i = 0; i < *num_watchers; i++) {
4020 ret = decode_watcher(p, end, *watchers + i);
4021 if (ret) {
4022 kfree(*watchers);
4023 return ret;
4024 }
4025 }
4026
4027 return 0;
4028}
4029
4030/*
4031 * On success, the caller is responsible for:
4032 *
4033 * kfree(watchers);
4034 */
4035int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4036 struct ceph_object_id *oid,
4037 struct ceph_object_locator *oloc,
4038 struct ceph_watch_item **watchers,
4039 u32 *num_watchers)
4040{
4041 struct ceph_osd_request *req;
4042 struct page **pages;
4043 int ret;
4044
4045 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4046 if (!req)
4047 return -ENOMEM;
4048
4049 ceph_oid_copy(&req->r_base_oid, oid);
4050 ceph_oloc_copy(&req->r_base_oloc, oloc);
4051 req->r_flags = CEPH_OSD_FLAG_READ;
4052
4053 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4054 if (ret)
4055 goto out_put_req;
4056
4057 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4058 if (IS_ERR(pages)) {
4059 ret = PTR_ERR(pages);
4060 goto out_put_req;
4061 }
4062
4063 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4064 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4065 response_data),
4066 pages, PAGE_SIZE, 0, false, true);
4067
4068 ceph_osdc_start_request(osdc, req, false);
4069 ret = ceph_osdc_wait_request(osdc, req);
4070 if (ret >= 0) {
4071 void *p = page_address(pages[0]);
4072 void *const end = p + req->r_ops[0].outdata_len;
4073
4074 ret = decode_watchers(&p, end, watchers, num_watchers);
4075 }
4076
4077out_put_req:
4078 ceph_osdc_put_request(req);
4079 return ret;
4080}
4081EXPORT_SYMBOL(ceph_osdc_list_watchers);
4082
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004083/*
Josh Durgindd935f42013-08-28 21:43:09 -07004084 * Call all pending notify callbacks - for use after a watch is
4085 * unregistered, to make sure no more callbacks for it will be invoked
4086 */
stephen hemmingerf64794492014-06-10 20:30:13 -07004087void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07004088{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004089 dout("%s osdc %p\n", __func__, osdc);
Josh Durgindd935f42013-08-28 21:43:09 -07004090 flush_workqueue(osdc->notify_wq);
4091}
4092EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4093
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02004094void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4095{
4096 down_read(&osdc->lock);
4097 maybe_request_map(osdc);
4098 up_read(&osdc->lock);
4099}
4100EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
Josh Durgindd935f42013-08-28 21:43:09 -07004101
4102/*
Douglas Fuller428a7152015-06-17 14:49:45 -04004103 * Execute an OSD class method on an object.
4104 *
4105 * @flags: CEPH_OSD_FLAG_*
Ilya Dryomov2544a022017-01-25 18:16:21 +01004106 * @resp_len: in/out param for reply length
Douglas Fuller428a7152015-06-17 14:49:45 -04004107 */
4108int ceph_osdc_call(struct ceph_osd_client *osdc,
4109 struct ceph_object_id *oid,
4110 struct ceph_object_locator *oloc,
4111 const char *class, const char *method,
4112 unsigned int flags,
4113 struct page *req_page, size_t req_len,
4114 struct page *resp_page, size_t *resp_len)
4115{
4116 struct ceph_osd_request *req;
4117 int ret;
4118
Ilya Dryomov2544a022017-01-25 18:16:21 +01004119 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4120 return -E2BIG;
4121
Douglas Fuller428a7152015-06-17 14:49:45 -04004122 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4123 if (!req)
4124 return -ENOMEM;
4125
4126 ceph_oid_copy(&req->r_base_oid, oid);
4127 ceph_oloc_copy(&req->r_base_oloc, oloc);
4128 req->r_flags = flags;
4129
4130 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4131 if (ret)
4132 goto out_put_req;
4133
4134 osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4135 if (req_page)
4136 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4137 0, false, false);
4138 if (resp_page)
4139 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
Ilya Dryomov2544a022017-01-25 18:16:21 +01004140 *resp_len, 0, false, false);
Douglas Fuller428a7152015-06-17 14:49:45 -04004141
4142 ceph_osdc_start_request(osdc, req, false);
4143 ret = ceph_osdc_wait_request(osdc, req);
4144 if (ret >= 0) {
4145 ret = req->r_ops[0].rval;
4146 if (resp_page)
4147 *resp_len = req->r_ops[0].outdata_len;
4148 }
4149
4150out_put_req:
4151 ceph_osdc_put_request(req);
4152 return ret;
4153}
4154EXPORT_SYMBOL(ceph_osdc_call);
4155
4156/*
Sage Weilf24e9982009-10-06 11:31:10 -07004157 * init, shutdown
4158 */
4159int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4160{
4161 int err;
4162
4163 dout("init\n");
4164 osdc->client = client;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004165 init_rwsem(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004166 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004167 INIT_LIST_HEAD(&osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02004168 spin_lock_init(&osdc->osd_lru_lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004169 osd_init(&osdc->homeless_osd);
4170 osdc->homeless_osd.o_osdc = osdc;
4171 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
Ilya Dryomov264048a2016-11-08 15:15:24 +01004172 osdc->last_linger_id = CEPH_LINGER_ID_START;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004173 osdc->linger_requests = RB_ROOT;
Ilya Dryomov46092452016-04-28 16:07:27 +02004174 osdc->map_checks = RB_ROOT;
4175 osdc->linger_map_checks = RB_ROOT;
Sage Weilf24e9982009-10-06 11:31:10 -07004176 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004177 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4178
Sage Weil5f44f142009-11-18 14:52:18 -08004179 err = -ENOMEM;
Ilya Dryomove5253a72016-04-28 16:07:25 +02004180 osdc->osdmap = ceph_osdmap_alloc();
4181 if (!osdc->osdmap)
4182 goto out;
4183
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01004184 osdc->req_mempool = mempool_create_slab_pool(10,
4185 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07004186 if (!osdc->req_mempool)
Ilya Dryomove5253a72016-04-28 16:07:25 +02004187 goto out_map;
Sage Weilf24e9982009-10-06 11:31:10 -07004188
Sage Weild50b4092012-07-09 14:22:34 -07004189 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02004190 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07004191 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08004192 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07004193 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02004194 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08004195 if (err < 0)
4196 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004197
Dan Carpenterdbcae082013-08-15 08:58:59 +03004198 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004199 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03004200 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02004201 goto out_msgpool_reply;
4202
Ilya Dryomovfbca9632016-04-28 16:07:24 +02004203 schedule_delayed_work(&osdc->timeout_work,
4204 osdc->client->options->osd_keepalive_timeout);
Ilya Dryomovb37ee1b2016-04-28 16:07:24 +02004205 schedule_delayed_work(&osdc->osds_timeout_work,
4206 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4207
Sage Weilf24e9982009-10-06 11:31:10 -07004208 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08004209
Ilya Dryomovc172ec52014-01-31 17:49:22 +02004210out_msgpool_reply:
4211 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08004212out_msgpool:
4213 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08004214out_mempool:
4215 mempool_destroy(osdc->req_mempool);
Ilya Dryomove5253a72016-04-28 16:07:25 +02004216out_map:
4217 ceph_osdmap_destroy(osdc->osdmap);
Sage Weil5f44f142009-11-18 14:52:18 -08004218out:
4219 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07004220}
4221
4222void ceph_osdc_stop(struct ceph_osd_client *osdc)
4223{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004224 flush_workqueue(osdc->notify_wq);
4225 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07004226 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004227 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004228
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004229 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004230 while (!RB_EMPTY_ROOT(&osdc->osds)) {
4231 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4232 struct ceph_osd, o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004233 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004234 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004235 up_write(&osdc->lock);
Elena Reshetova02113a02017-03-17 14:10:28 +02004236 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004237 osd_cleanup(&osdc->homeless_osd);
4238
4239 WARN_ON(!list_empty(&osdc->osd_lru));
Ilya Dryomov922dab62016-05-26 01:15:02 +02004240 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
Ilya Dryomov46092452016-04-28 16:07:27 +02004241 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4242 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004243 WARN_ON(atomic_read(&osdc->num_requests));
4244 WARN_ON(atomic_read(&osdc->num_homeless));
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004245
Ilya Dryomove5253a72016-04-28 16:07:25 +02004246 ceph_osdmap_destroy(osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07004247 mempool_destroy(osdc->req_mempool);
4248 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08004249 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07004250}
4251
4252/*
4253 * Read some contiguous pages. If we cross a stripe boundary, shorten
4254 * *plen. Return number of bytes read, or error.
4255 */
4256int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4257 struct ceph_vino vino, struct ceph_file_layout *layout,
4258 u64 off, u64 *plen,
4259 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08004260 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07004261{
4262 struct ceph_osd_request *req;
4263 int rc = 0;
4264
4265 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4266 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08004267 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07004268 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05004269 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06004270 false);
Sage Weil68162822012-09-24 21:01:02 -07004271 if (IS_ERR(req))
4272 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07004273
4274 /* it may be a short read due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05004275 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05004276 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07004277
Alex Eldere0c59482013-03-07 15:38:25 -06004278 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05004279 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07004280
4281 rc = ceph_osdc_start_request(osdc, req, false);
4282 if (!rc)
4283 rc = ceph_osdc_wait_request(osdc, req);
4284
4285 ceph_osdc_put_request(req);
4286 dout("readpages result %d\n", rc);
4287 return rc;
4288}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004289EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07004290
4291/*
4292 * do a synchronous write on N pages
4293 */
4294int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
4295 struct ceph_file_layout *layout,
4296 struct ceph_snap_context *snapc,
4297 u64 off, u64 len,
4298 u32 truncate_seq, u64 truncate_size,
4299 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06004300 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07004301{
4302 struct ceph_osd_request *req;
4303 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08004304 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07004305
Yan, Zheng715e4cd2014-11-13 14:40:37 +08004306 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004307 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05004308 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06004309 true);
Sage Weil68162822012-09-24 21:01:02 -07004310 if (IS_ERR(req))
4311 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07004312
4313 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05004314 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05004315 false, false);
4316 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07004317
Ilya Dryomovbb873b52016-05-26 00:29:52 +02004318 req->r_mtime = *mtime;
Alex Elder87f979d2013-02-15 11:42:29 -06004319 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07004320 if (!rc)
4321 rc = ceph_osdc_wait_request(osdc, req);
4322
4323 ceph_osdc_put_request(req);
4324 if (rc == 0)
4325 rc = len;
4326 dout("writepages result %d\n", rc);
4327 return rc;
4328}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004329EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07004330
Alex Elder5522ae02013-05-01 12:43:04 -05004331int ceph_osdc_setup(void)
4332{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01004333 size_t size = sizeof(struct ceph_osd_request) +
4334 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
4335
Alex Elder5522ae02013-05-01 12:43:04 -05004336 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01004337 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
4338 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05004339
4340 return ceph_osd_request_cache ? 0 : -ENOMEM;
4341}
4342EXPORT_SYMBOL(ceph_osdc_setup);
4343
4344void ceph_osdc_cleanup(void)
4345{
4346 BUG_ON(!ceph_osd_request_cache);
4347 kmem_cache_destroy(ceph_osd_request_cache);
4348 ceph_osd_request_cache = NULL;
4349}
4350EXPORT_SYMBOL(ceph_osdc_cleanup);
4351
Sage Weilf24e9982009-10-06 11:31:10 -07004352/*
4353 * handle incoming message
4354 */
4355static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4356{
4357 struct ceph_osd *osd = con->private;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004358 struct ceph_osd_client *osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07004359 int type = le16_to_cpu(msg->hdr.type);
4360
Sage Weilf24e9982009-10-06 11:31:10 -07004361 switch (type) {
4362 case CEPH_MSG_OSD_MAP:
4363 ceph_osdc_handle_map(osdc, msg);
4364 break;
4365 case CEPH_MSG_OSD_OPREPLY:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004366 handle_reply(osd, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07004367 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004368 case CEPH_MSG_WATCH_NOTIFY:
4369 handle_watch_notify(osdc, msg);
4370 break;
Sage Weilf24e9982009-10-06 11:31:10 -07004371
4372 default:
4373 pr_err("received unknown message type %d %s\n", type,
4374 ceph_msg_type_name(type));
4375 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004376
Sage Weilf24e9982009-10-06 11:31:10 -07004377 ceph_msg_put(msg);
4378}
4379
Sage Weil5b3a4db2010-02-19 21:43:23 -08004380/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004381 * Lookup and return message for incoming reply. Don't try to do
4382 * anything about a larger than preallocated data portion of the
4383 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08004384 */
4385static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08004386 struct ceph_msg_header *hdr,
4387 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07004388{
4389 struct ceph_osd *osd = con->private;
4390 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004391 struct ceph_msg *m = NULL;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004392 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02004393 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004394 int data_len = le32_to_cpu(hdr->data_len);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004395 u64 tid = le64_to_cpu(hdr->tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004396
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004397 down_read(&osdc->lock);
4398 if (!osd_registered(osd)) {
4399 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
4400 *skip = 1;
4401 goto out_unlock_osdc;
4402 }
4403 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
4404
4405 mutex_lock(&osd->lock);
4406 req = lookup_request(&osd->o_requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004407 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01004408 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
4409 osd->o_osd, tid);
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004410 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004411 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004412 }
Sage Weilc16e7862010-03-01 13:02:00 -08004413
Alex Elderace6d3a2013-04-01 16:12:14 -05004414 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08004415
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02004416 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004417 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
4418 __func__, osd->o_osd, req->r_tid, front_len,
4419 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02004420 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
4421 false);
Sage Weila79832f2010-04-01 16:06:19 -07004422 if (!m)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004423 goto out_unlock_session;
Sage Weilc16e7862010-03-01 13:02:00 -08004424 ceph_msg_put(req->r_reply);
4425 req->r_reply = m;
4426 }
Sage Weilc16e7862010-03-01 13:02:00 -08004427
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004428 if (data_len > req->r_reply->data_length) {
4429 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
4430 __func__, osd->o_osd, req->r_tid, data_len,
4431 req->r_reply->data_length);
4432 m = NULL;
4433 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004434 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004435 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004436
4437 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08004438 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004439
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004440out_unlock_session:
4441 mutex_unlock(&osd->lock);
4442out_unlock_osdc:
4443 up_read(&osdc->lock);
Yehuda Sadeh24504182010-01-08 13:58:34 -08004444 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08004445}
4446
Ilya Dryomov19079202016-04-28 16:07:27 +02004447/*
4448 * TODO: switch to a msg-owned pagelist
4449 */
4450static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
4451{
4452 struct ceph_msg *m;
4453 int type = le16_to_cpu(hdr->type);
4454 u32 front_len = le32_to_cpu(hdr->front_len);
4455 u32 data_len = le32_to_cpu(hdr->data_len);
4456
4457 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
4458 if (!m)
4459 return NULL;
4460
4461 if (data_len) {
4462 struct page **pages;
4463 struct ceph_osd_data osd_data;
4464
4465 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
4466 GFP_NOIO);
Wei Yongjunc22e8532016-07-30 00:37:57 +00004467 if (IS_ERR(pages)) {
Ilya Dryomov19079202016-04-28 16:07:27 +02004468 ceph_msg_put(m);
4469 return NULL;
4470 }
4471
4472 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
4473 false);
4474 ceph_osdc_msg_data_add(m, &osd_data);
4475 }
4476
4477 return m;
4478}
4479
Sage Weil5b3a4db2010-02-19 21:43:23 -08004480static struct ceph_msg *alloc_msg(struct ceph_connection *con,
4481 struct ceph_msg_header *hdr,
4482 int *skip)
4483{
4484 struct ceph_osd *osd = con->private;
4485 int type = le16_to_cpu(hdr->type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004486
Alex Elder1c20f2d2012-06-04 14:43:32 -05004487 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08004488 switch (type) {
4489 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004490 case CEPH_MSG_WATCH_NOTIFY:
Ilya Dryomov19079202016-04-28 16:07:27 +02004491 return alloc_msg_with_page_vector(hdr);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004492 case CEPH_MSG_OSD_OPREPLY:
4493 return get_reply(con, hdr, skip);
4494 default:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004495 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
4496 osd->o_osd, type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004497 *skip = 1;
4498 return NULL;
4499 }
Sage Weilf24e9982009-10-06 11:31:10 -07004500}
4501
4502/*
4503 * Wrappers to refcount containing ceph_osd struct
4504 */
4505static struct ceph_connection *get_osd_con(struct ceph_connection *con)
4506{
4507 struct ceph_osd *osd = con->private;
4508 if (get_osd(osd))
4509 return con;
4510 return NULL;
4511}
4512
4513static void put_osd_con(struct ceph_connection *con)
4514{
4515 struct ceph_osd *osd = con->private;
4516 put_osd(osd);
4517}
4518
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004519/*
4520 * authentication
4521 */
Alex Eldera3530df2012-05-16 15:16:39 -05004522/*
4523 * Note: returned pointer is the address of a structure that's
4524 * managed separately. Caller must *not* attempt to free it.
4525 */
4526static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004527 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004528{
4529 struct ceph_osd *o = con->private;
4530 struct ceph_osd_client *osdc = o->o_osdc;
4531 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004532 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004533
Alex Elder74f18692012-05-16 15:16:39 -05004534 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004535 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004536 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004537 }
Sage Weil27859f92013-03-25 10:26:14 -07004538 if (!auth->authorizer) {
4539 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4540 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004541 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004542 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004543 } else {
4544 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07004545 auth);
4546 if (ret)
4547 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004548 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004549 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004550
Alex Eldera3530df2012-05-16 15:16:39 -05004551 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004552}
4553
4554
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004555static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004556{
4557 struct ceph_osd *o = con->private;
4558 struct ceph_osd_client *osdc = o->o_osdc;
4559 struct ceph_auth_client *ac = osdc->client->monc.auth;
4560
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004561 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004562}
4563
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004564static int invalidate_authorizer(struct ceph_connection *con)
4565{
4566 struct ceph_osd *o = con->private;
4567 struct ceph_osd_client *osdc = o->o_osdc;
4568 struct ceph_auth_client *ac = osdc->client->monc.auth;
4569
Sage Weil27859f92013-03-25 10:26:14 -07004570 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004571 return ceph_monc_validate_auth(&osdc->client->monc);
4572}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004573
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004574static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004575{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004576 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004577 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004578
Yan, Zheng33d07332014-11-04 16:33:37 +08004579 return ceph_auth_sign_message(auth, msg);
4580}
4581
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004582static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004583{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004584 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004585 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004586
Yan, Zheng33d07332014-11-04 16:33:37 +08004587 return ceph_auth_check_message_signature(auth, msg);
4588}
4589
Tobias Klauser9e327892010-05-20 10:40:19 +02004590static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07004591 .get = get_osd_con,
4592 .put = put_osd_con,
4593 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004594 .get_authorizer = get_authorizer,
4595 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004596 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07004597 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004598 .sign_message = osd_sign_message,
4599 .check_message_signature = osd_check_message_signature,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004600 .fault = osd_fault,
Sage Weilf24e9982009-10-06 11:31:10 -07004601};