blob: ce0055dc7544826e36651ec3adce1f218426f87f [file] [log] [blame]
Alex Eldera4ce40a2013-04-05 01:27:12 -05001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07005#include <linux/err.h>
6#include <linux/highmem.h>
7#include <linux/mm.h>
8#include <linux/pagemap.h>
9#include <linux/slab.h>
10#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070011#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
Sage Weilf24e9982009-10-06 11:31:10 -070014
Ilya Dryomov8cb441c2017-06-15 16:30:54 +020015#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070016#include <linux/ceph/libceph.h>
17#include <linux/ceph/osd_client.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/auth.h>
21#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070022
Sage Weilc16e7862010-03-01 13:02:00 -080023#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080024
Alex Elder5522ae02013-05-01 12:43:04 -050025static struct kmem_cache *ceph_osd_request_cache;
26
Tobias Klauser9e327892010-05-20 10:40:19 +020027static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070028
Sage Weilf24e9982009-10-06 11:31:10 -070029/*
30 * Implement client access to distributed object storage cluster.
31 *
32 * All data objects are stored within a cluster/cloud of OSDs, or
33 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
34 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
35 * remote daemons serving up and coordinating consistent and safe
36 * access to storage.
37 *
38 * Cluster membership and the mapping of data objects onto storage devices
39 * are described by the osd map.
40 *
41 * We keep track of pending OSD requests (read, write), resubmit
42 * requests to different OSDs when the cluster topology/data layout
43 * change, or retry the affected requests when the communications
44 * channel with an OSD is reset.
45 */
46
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020047static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
48static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
Ilya Dryomov922dab62016-05-26 01:15:02 +020049static void link_linger(struct ceph_osd *osd,
50 struct ceph_osd_linger_request *lreq);
51static void unlink_linger(struct ceph_osd *osd,
52 struct ceph_osd_linger_request *lreq);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020053
54#if 1
55static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
56{
57 bool wrlocked = true;
58
59 if (unlikely(down_read_trylock(sem))) {
60 wrlocked = false;
61 up_read(sem);
62 }
63
64 return wrlocked;
65}
66static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
67{
68 WARN_ON(!rwsem_is_locked(&osdc->lock));
69}
70static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
71{
72 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
73}
74static inline void verify_osd_locked(struct ceph_osd *osd)
75{
76 struct ceph_osd_client *osdc = osd->o_osdc;
77
78 WARN_ON(!(mutex_is_locked(&osd->lock) &&
79 rwsem_is_locked(&osdc->lock)) &&
80 !rwsem_is_wrlocked(&osdc->lock));
81}
Ilya Dryomov922dab62016-05-26 01:15:02 +020082static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
83{
84 WARN_ON(!mutex_is_locked(&lreq->lock));
85}
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020086#else
87static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
88static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
89static inline void verify_osd_locked(struct ceph_osd *osd) { }
Ilya Dryomov922dab62016-05-26 01:15:02 +020090static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020091#endif
92
Sage Weilf24e9982009-10-06 11:31:10 -070093/*
94 * calculate the mapping of a file extent onto an object, and fill out the
95 * request accordingly. shorten extent as necessary if it crosses an
96 * object boundary.
97 *
98 * fill osd op in request message.
99 */
Alex Elderdbe0fc42013-02-15 22:10:17 -0600100static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -0500101 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -0700102{
Alex Elder60e56f12013-02-15 11:42:29 -0600103 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -0700104 int r;
Sage Weilf24e9982009-10-06 11:31:10 -0700105
Alex Elder60e56f12013-02-15 11:42:29 -0600106 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -0500107 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
108 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -0700109 if (r < 0)
110 return r;
Alex Elder75d1c942013-03-13 20:50:00 -0500111 if (*objlen < orig_len) {
112 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -0600113 dout(" skipping last %llu, final file extent %llu~%llu\n",
114 orig_len - *plen, off, *plen);
115 }
116
Alex Elder75d1c942013-03-13 20:50:00 -0500117 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -0700118
Alex Elder3ff5f382013-02-15 22:10:17 -0600119 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700120}
121
Alex Elderc54d47b2013-04-03 01:28:57 -0500122static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
123{
124 memset(osd_data, 0, sizeof (*osd_data));
125 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
126}
127
Alex Eldera4ce40a2013-04-05 01:27:12 -0500128static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500129 struct page **pages, u64 length, u32 alignment,
130 bool pages_from_pool, bool own_pages)
131{
132 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
133 osd_data->pages = pages;
134 osd_data->length = length;
135 osd_data->alignment = alignment;
136 osd_data->pages_from_pool = pages_from_pool;
137 osd_data->own_pages = own_pages;
138}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500139
Alex Eldera4ce40a2013-04-05 01:27:12 -0500140static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500141 struct ceph_pagelist *pagelist)
142{
143 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
144 osd_data->pagelist = pagelist;
145}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500146
147#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500148static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500149 struct bio *bio, size_t bio_length)
150{
151 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
152 osd_data->bio = bio;
153 osd_data->bio_length = bio_length;
154}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500155#endif /* CONFIG_BLOCK */
156
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300157#define osd_req_op_data(oreq, whch, typ, fld) \
158({ \
159 struct ceph_osd_request *__oreq = (oreq); \
160 unsigned int __whch = (whch); \
161 BUG_ON(__whch >= __oreq->r_num_ops); \
162 &__oreq->r_ops[__whch].typ.fld; \
163})
Alex Elder863c7eb2013-04-15 14:50:36 -0500164
Alex Elder49719772013-02-11 12:33:24 -0600165static struct ceph_osd_data *
166osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
167{
168 BUG_ON(which >= osd_req->r_num_ops);
169
170 return &osd_req->r_ops[which].raw_data_in;
171}
172
Alex Eldera4ce40a2013-04-05 01:27:12 -0500173struct ceph_osd_data *
174osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500175 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500176{
Alex Elder863c7eb2013-04-15 14:50:36 -0500177 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500178}
179EXPORT_SYMBOL(osd_req_op_extent_osd_data);
180
Alex Elder49719772013-02-11 12:33:24 -0600181void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
182 unsigned int which, struct page **pages,
183 u64 length, u32 alignment,
184 bool pages_from_pool, bool own_pages)
185{
186 struct ceph_osd_data *osd_data;
187
188 osd_data = osd_req_op_raw_data_in(osd_req, which);
189 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
190 pages_from_pool, own_pages);
191}
192EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
193
Alex Eldera4ce40a2013-04-05 01:27:12 -0500194void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500195 unsigned int which, struct page **pages,
196 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500197 bool pages_from_pool, bool own_pages)
198{
199 struct ceph_osd_data *osd_data;
200
Alex Elder863c7eb2013-04-15 14:50:36 -0500201 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500202 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
203 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500204}
205EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
206
207void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500208 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500209{
210 struct ceph_osd_data *osd_data;
211
Alex Elder863c7eb2013-04-15 14:50:36 -0500212 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500213 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500214}
215EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
216
217#ifdef CONFIG_BLOCK
218void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500219 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500220{
221 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500222
223 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500224 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500225}
226EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
227#endif /* CONFIG_BLOCK */
228
229static void osd_req_op_cls_request_info_pagelist(
230 struct ceph_osd_request *osd_req,
231 unsigned int which, struct ceph_pagelist *pagelist)
232{
233 struct ceph_osd_data *osd_data;
234
Alex Elder863c7eb2013-04-15 14:50:36 -0500235 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500237}
238
Alex Elder04017e22013-04-05 14:46:02 -0500239void osd_req_op_cls_request_data_pagelist(
240 struct ceph_osd_request *osd_req,
241 unsigned int which, struct ceph_pagelist *pagelist)
242{
243 struct ceph_osd_data *osd_data;
244
Alex Elder863c7eb2013-04-15 14:50:36 -0500245 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500246 ceph_osd_data_pagelist_init(osd_data, pagelist);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200247 osd_req->r_ops[which].cls.indata_len += pagelist->length;
248 osd_req->r_ops[which].indata_len += pagelist->length;
Alex Elder04017e22013-04-05 14:46:02 -0500249}
250EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
251
Alex Elder6c57b552013-04-19 15:34:49 -0500252void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
253 unsigned int which, struct page **pages, u64 length,
254 u32 alignment, bool pages_from_pool, bool own_pages)
255{
256 struct ceph_osd_data *osd_data;
257
258 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
259 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
260 pages_from_pool, own_pages);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200261 osd_req->r_ops[which].cls.indata_len += length;
262 osd_req->r_ops[which].indata_len += length;
Alex Elder6c57b552013-04-19 15:34:49 -0500263}
264EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
265
Alex Eldera4ce40a2013-04-05 01:27:12 -0500266void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
267 unsigned int which, struct page **pages, u64 length,
268 u32 alignment, bool pages_from_pool, bool own_pages)
269{
270 struct ceph_osd_data *osd_data;
271
Alex Elder863c7eb2013-04-15 14:50:36 -0500272 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500273 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
274 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500275}
276EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
277
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500278static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
279{
280 switch (osd_data->type) {
281 case CEPH_OSD_DATA_TYPE_NONE:
282 return 0;
283 case CEPH_OSD_DATA_TYPE_PAGES:
284 return osd_data->length;
285 case CEPH_OSD_DATA_TYPE_PAGELIST:
286 return (u64)osd_data->pagelist->length;
287#ifdef CONFIG_BLOCK
288 case CEPH_OSD_DATA_TYPE_BIO:
289 return (u64)osd_data->bio_length;
290#endif /* CONFIG_BLOCK */
291 default:
292 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
293 return 0;
294 }
295}
296
Alex Elderc54d47b2013-04-03 01:28:57 -0500297static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
298{
Alex Elder54764922013-04-05 01:27:12 -0500299 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500300 int num_pages;
301
302 num_pages = calc_pages_for((u64)osd_data->alignment,
303 (u64)osd_data->length);
304 ceph_release_page_vector(osd_data->pages, num_pages);
305 }
Alex Elder54764922013-04-05 01:27:12 -0500306 ceph_osd_data_init(osd_data);
307}
308
309static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
310 unsigned int which)
311{
312 struct ceph_osd_req_op *op;
313
314 BUG_ON(which >= osd_req->r_num_ops);
315 op = &osd_req->r_ops[which];
316
317 switch (op->op) {
318 case CEPH_OSD_OP_READ:
319 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200320 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500321 ceph_osd_data_release(&op->extent.osd_data);
322 break;
323 case CEPH_OSD_OP_CALL:
324 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500325 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500326 ceph_osd_data_release(&op->cls.response_data);
327 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800328 case CEPH_OSD_OP_SETXATTR:
329 case CEPH_OSD_OP_CMPXATTR:
330 ceph_osd_data_release(&op->xattr.osd_data);
331 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800332 case CEPH_OSD_OP_STAT:
333 ceph_osd_data_release(&op->raw_data_in);
334 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200335 case CEPH_OSD_OP_NOTIFY_ACK:
336 ceph_osd_data_release(&op->notify_ack.request_data);
337 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200338 case CEPH_OSD_OP_NOTIFY:
339 ceph_osd_data_release(&op->notify.request_data);
340 ceph_osd_data_release(&op->notify.response_data);
341 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700342 case CEPH_OSD_OP_LIST_WATCHERS:
343 ceph_osd_data_release(&op->list_watchers.response_data);
344 break;
Alex Elder54764922013-04-05 01:27:12 -0500345 default:
346 break;
347 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500348}
349
Sage Weilf24e9982009-10-06 11:31:10 -0700350/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200351 * Assumes @t is zero-initialized.
352 */
353static void target_init(struct ceph_osd_request_target *t)
354{
355 ceph_oid_init(&t->base_oid);
356 ceph_oloc_init(&t->base_oloc);
357 ceph_oid_init(&t->target_oid);
358 ceph_oloc_init(&t->target_oloc);
359
360 ceph_osds_init(&t->acting);
361 ceph_osds_init(&t->up);
362 t->size = -1;
363 t->min_size = -1;
364
365 t->osd = CEPH_HOMELESS_OSD;
366}
367
Ilya Dryomov922dab62016-05-26 01:15:02 +0200368static void target_copy(struct ceph_osd_request_target *dest,
369 const struct ceph_osd_request_target *src)
370{
371 ceph_oid_copy(&dest->base_oid, &src->base_oid);
372 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
373 ceph_oid_copy(&dest->target_oid, &src->target_oid);
374 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
375
376 dest->pgid = src->pgid; /* struct */
Ilya Dryomovdc98ff72017-06-15 16:30:53 +0200377 dest->spgid = src->spgid; /* struct */
Ilya Dryomov922dab62016-05-26 01:15:02 +0200378 dest->pg_num = src->pg_num;
379 dest->pg_num_mask = src->pg_num_mask;
380 ceph_osds_copy(&dest->acting, &src->acting);
381 ceph_osds_copy(&dest->up, &src->up);
382 dest->size = src->size;
383 dest->min_size = src->min_size;
384 dest->sort_bitwise = src->sort_bitwise;
385
386 dest->flags = src->flags;
387 dest->paused = src->paused;
388
Ilya Dryomov04c7d782017-06-15 16:30:55 +0200389 dest->epoch = src->epoch;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +0200390 dest->last_force_resend = src->last_force_resend;
391
Ilya Dryomov922dab62016-05-26 01:15:02 +0200392 dest->osd = src->osd;
393}
394
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200395static void target_destroy(struct ceph_osd_request_target *t)
396{
397 ceph_oid_destroy(&t->base_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800398 ceph_oloc_destroy(&t->base_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200399 ceph_oid_destroy(&t->target_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800400 ceph_oloc_destroy(&t->target_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200401}
402
403/*
Sage Weilf24e9982009-10-06 11:31:10 -0700404 * requests
405 */
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200406static void request_release_checks(struct ceph_osd_request *req)
407{
408 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
Ilya Dryomov46092452016-04-28 16:07:27 +0200409 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200410 WARN_ON(!list_empty(&req->r_unsafe_item));
411 WARN_ON(req->r_osd);
412}
413
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400414static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700415{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400416 struct ceph_osd_request *req = container_of(kref,
417 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500418 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800419
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400420 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
421 req->r_request, req->r_reply);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200422 request_release_checks(req);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400423
Sage Weil415e49a2009-12-07 13:37:03 -0800424 if (req->r_request)
425 ceph_msg_put(req->r_request);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200426 if (req->r_reply)
Alex Elderab8cb342012-06-04 14:43:32 -0500427 ceph_msg_put(req->r_reply);
Alex Elder0fff87e2013-02-14 12:16:43 -0600428
Alex Elder54764922013-04-05 01:27:12 -0500429 for (which = 0; which < req->r_num_ops; which++)
430 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600431
Ilya Dryomova66dd382016-04-28 16:07:23 +0200432 target_destroy(&req->r_t);
Sage Weil415e49a2009-12-07 13:37:03 -0800433 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200434
Sage Weil415e49a2009-12-07 13:37:03 -0800435 if (req->r_mempool)
436 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100437 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500438 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100439 else
440 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700441}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400442
443void ceph_osdc_get_request(struct ceph_osd_request *req)
444{
445 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100446 kref_read(&req->r_kref));
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400447 kref_get(&req->r_kref);
448}
449EXPORT_SYMBOL(ceph_osdc_get_request);
450
451void ceph_osdc_put_request(struct ceph_osd_request *req)
452{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200453 if (req) {
454 dout("%s %p (was %d)\n", __func__, req,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100455 kref_read(&req->r_kref));
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200456 kref_put(&req->r_kref, ceph_osdc_release_request);
457 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400458}
459EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700460
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200461static void request_init(struct ceph_osd_request *req)
462{
463 /* req only, each op is zeroed in _osd_req_op_init() */
464 memset(req, 0, sizeof(*req));
465
466 kref_init(&req->r_kref);
467 init_completion(&req->r_completion);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200468 RB_CLEAR_NODE(&req->r_node);
Ilya Dryomov46092452016-04-28 16:07:27 +0200469 RB_CLEAR_NODE(&req->r_mc_node);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200470 INIT_LIST_HEAD(&req->r_unsafe_item);
471
472 target_init(&req->r_t);
473}
474
Ilya Dryomov922dab62016-05-26 01:15:02 +0200475/*
476 * This is ugly, but it allows us to reuse linger registration and ping
477 * requests, keeping the structure of the code around send_linger{_ping}()
478 * reasonable. Setting up a min_nr=2 mempool for each linger request
479 * and dealing with copying ops (this blasts req only, watch op remains
480 * intact) isn't any better.
481 */
482static void request_reinit(struct ceph_osd_request *req)
483{
484 struct ceph_osd_client *osdc = req->r_osdc;
485 bool mempool = req->r_mempool;
486 unsigned int num_ops = req->r_num_ops;
487 u64 snapid = req->r_snapid;
488 struct ceph_snap_context *snapc = req->r_snapc;
489 bool linger = req->r_linger;
490 struct ceph_msg *request_msg = req->r_request;
491 struct ceph_msg *reply_msg = req->r_reply;
492
493 dout("%s req %p\n", __func__, req);
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100494 WARN_ON(kref_read(&req->r_kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200495 request_release_checks(req);
496
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100497 WARN_ON(kref_read(&request_msg->kref) != 1);
498 WARN_ON(kref_read(&reply_msg->kref) != 1);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200499 target_destroy(&req->r_t);
500
501 request_init(req);
502 req->r_osdc = osdc;
503 req->r_mempool = mempool;
504 req->r_num_ops = num_ops;
505 req->r_snapid = snapid;
506 req->r_snapc = snapc;
507 req->r_linger = linger;
508 req->r_request = request_msg;
509 req->r_reply = reply_msg;
510}
511
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700512struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700513 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800514 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700515 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600516 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700517{
518 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700519
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700520 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100521 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700522 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100523 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
524 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700525 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100526 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
527 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
528 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700529 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100530 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700531 return NULL;
532
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200533 request_init(req);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700534 req->r_osdc = osdc;
535 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500536 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200537 req->r_snapid = CEPH_NOSNAP;
538 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700539
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200540 dout("%s req %p\n", __func__, req);
541 return req;
542}
543EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100544
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +0200545static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
Yan, Zheng30c156d2016-02-14 11:24:31 +0800546{
547 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
548}
549
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200550int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
551{
552 struct ceph_osd_client *osdc = req->r_osdc;
553 struct ceph_msg *msg;
554 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700555
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200556 WARN_ON(ceph_oid_empty(&req->r_base_oid));
Yan, Zheng30c156d2016-02-14 11:24:31 +0800557 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200558
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200559 /* create request message */
Ilya Dryomov8cb441c2017-06-15 16:30:54 +0200560 msg_size = CEPH_ENCODING_START_BLK_LEN +
561 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
562 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
563 msg_size += CEPH_ENCODING_START_BLK_LEN +
564 sizeof(struct ceph_osd_reqid); /* reqid */
565 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
566 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
Yan, Zheng30c156d2016-02-14 11:24:31 +0800567 msg_size += CEPH_ENCODING_START_BLK_LEN +
568 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200569 msg_size += 4 + req->r_base_oid.name_len; /* oid */
570 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100571 msg_size += 8; /* snapid */
572 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200573 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +0200574 msg_size += 4 + 8; /* retry_attempt, features */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100575
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200576 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700577 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
578 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200579 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
580 if (!msg)
581 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700582
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700583 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700584 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700585
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200586 /* create reply message */
587 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200588 msg_size += req->r_base_oid.name_len;
589 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200590
591 if (req->r_mempool)
592 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
593 else
594 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
595 if (!msg)
596 return -ENOMEM;
597
598 req->r_reply = msg;
599
600 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700601}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200602EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700603
Alex Eldera8dd0a32013-03-13 20:50:00 -0500604static bool osd_req_opcode_valid(u16 opcode)
605{
606 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400607#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
608__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
609#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500610 default:
611 return false;
612 }
613}
614
Alex Elder33803f32013-03-13 20:50:00 -0500615/*
616 * This is an osd op init function for opcodes that have no data or
617 * other information associated with them. It also serves as a
618 * common init routine for all the other init functions, below.
619 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500620static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600621_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800622 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500623{
Alex Elderc99d2d42013-04-05 01:27:11 -0500624 struct ceph_osd_req_op *op;
625
626 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500627 BUG_ON(!osd_req_opcode_valid(opcode));
628
Alex Elderc99d2d42013-04-05 01:27:11 -0500629 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500630 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500631 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800632 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500633
634 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500635}
636
Alex Elder49719772013-02-11 12:33:24 -0600637void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800638 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600639{
Yan, Zheng144cba12015-04-27 11:09:54 +0800640 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600641}
642EXPORT_SYMBOL(osd_req_op_init);
643
Alex Elderc99d2d42013-04-05 01:27:11 -0500644void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
645 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500646 u64 offset, u64 length,
647 u64 truncate_size, u32 truncate_seq)
648{
Yan, Zheng144cba12015-04-27 11:09:54 +0800649 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
650 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500651 size_t payload_len = 0;
652
Li Wangad7a60d2013-08-15 11:51:44 +0800653 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200654 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
655 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500656
Alex Elder33803f32013-03-13 20:50:00 -0500657 op->extent.offset = offset;
658 op->extent.length = length;
659 op->extent.truncate_size = truncate_size;
660 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200661 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500662 payload_len += length;
663
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100664 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500665}
666EXPORT_SYMBOL(osd_req_op_extent_init);
667
Alex Elderc99d2d42013-04-05 01:27:11 -0500668void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
669 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500670{
Alex Elderc99d2d42013-04-05 01:27:11 -0500671 struct ceph_osd_req_op *op;
672 u64 previous;
673
674 BUG_ON(which >= osd_req->r_num_ops);
675 op = &osd_req->r_ops[which];
676 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500677
678 if (length == previous)
679 return; /* Nothing to do */
680 BUG_ON(length > previous);
681
682 op->extent.length = length;
Yan, Zhengd641df82017-01-19 11:21:29 +0800683 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
684 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500685}
686EXPORT_SYMBOL(osd_req_op_extent_update);
687
Yan, Zheng2c63f492016-01-07 17:32:54 +0800688void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
689 unsigned int which, u64 offset_inc)
690{
691 struct ceph_osd_req_op *op, *prev_op;
692
693 BUG_ON(which + 1 >= osd_req->r_num_ops);
694
695 prev_op = &osd_req->r_ops[which];
696 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
697 /* dup previous one */
698 op->indata_len = prev_op->indata_len;
699 op->outdata_len = prev_op->outdata_len;
700 op->extent = prev_op->extent;
701 /* adjust offset */
702 op->extent.offset += offset_inc;
703 op->extent.length -= offset_inc;
704
705 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
706 op->indata_len -= offset_inc;
707}
708EXPORT_SYMBOL(osd_req_op_extent_dup_last);
709
Alex Elderc99d2d42013-04-05 01:27:11 -0500710void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500711 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500712{
Yan, Zheng144cba12015-04-27 11:09:54 +0800713 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
714 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500715 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500716 size_t payload_len = 0;
717 size_t size;
718
719 BUG_ON(opcode != CEPH_OSD_OP_CALL);
720
Alex Elder5f562df2013-04-05 01:27:12 -0500721 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
722 BUG_ON(!pagelist);
723 ceph_pagelist_init(pagelist);
724
Alex Elder33803f32013-03-13 20:50:00 -0500725 op->cls.class_name = class;
726 size = strlen(class);
727 BUG_ON(size > (size_t) U8_MAX);
728 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500729 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500730 payload_len += size;
731
732 op->cls.method_name = method;
733 size = strlen(method);
734 BUG_ON(size > (size_t) U8_MAX);
735 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500736 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500737 payload_len += size;
738
Alex Eldera4ce40a2013-04-05 01:27:12 -0500739 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500740
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100741 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500742}
743EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500744
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800745int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
746 u16 opcode, const char *name, const void *value,
747 size_t size, u8 cmp_op, u8 cmp_mode)
748{
Yan, Zheng144cba12015-04-27 11:09:54 +0800749 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
750 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800751 struct ceph_pagelist *pagelist;
752 size_t payload_len;
753
754 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
755
756 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
757 if (!pagelist)
758 return -ENOMEM;
759
760 ceph_pagelist_init(pagelist);
761
762 payload_len = strlen(name);
763 op->xattr.name_len = payload_len;
764 ceph_pagelist_append(pagelist, name, payload_len);
765
766 op->xattr.value_len = size;
767 ceph_pagelist_append(pagelist, value, size);
768 payload_len += size;
769
770 op->xattr.cmp_op = cmp_op;
771 op->xattr.cmp_mode = cmp_mode;
772
773 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100774 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800775 return 0;
776}
777EXPORT_SYMBOL(osd_req_op_xattr_init);
778
Ilya Dryomov922dab62016-05-26 01:15:02 +0200779/*
780 * @watch_opcode: CEPH_OSD_WATCH_OP_*
781 */
782static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
783 u64 cookie, u8 watch_opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500784{
Ilya Dryomov922dab62016-05-26 01:15:02 +0200785 struct ceph_osd_req_op *op;
Alex Elder33803f32013-03-13 20:50:00 -0500786
Ilya Dryomov922dab62016-05-26 01:15:02 +0200787 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500788 op->watch.cookie = cookie;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200789 op->watch.op = watch_opcode;
790 op->watch.gen = 0;
Alex Elder33803f32013-03-13 20:50:00 -0500791}
Alex Elder33803f32013-03-13 20:50:00 -0500792
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200793void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
794 unsigned int which,
795 u64 expected_object_size,
796 u64 expected_write_size)
797{
798 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800799 CEPH_OSD_OP_SETALLOCHINT,
800 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200801
802 op->alloc_hint.expected_object_size = expected_object_size;
803 op->alloc_hint.expected_write_size = expected_write_size;
804
805 /*
806 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
807 * not worth a feature bit. Set FAILOK per-op flag to make
808 * sure older osds don't trip over an unsupported opcode.
809 */
810 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
811}
812EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
813
Alex Elder90af3602013-04-05 14:46:01 -0500814static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500815 struct ceph_osd_data *osd_data)
816{
817 u64 length = ceph_osd_data_length(osd_data);
818
819 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
820 BUG_ON(length > (u64) SIZE_MAX);
821 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500822 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500823 length, osd_data->alignment);
824 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
825 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500826 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500827#ifdef CONFIG_BLOCK
828 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500829 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500830#endif
831 } else {
832 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
833 }
834}
835
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200836static u32 osd_req_encode_op(struct ceph_osd_op *dst,
837 const struct ceph_osd_req_op *src)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700838{
Alex Eldera8dd0a32013-03-13 20:50:00 -0500839 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
840 pr_err("unrecognized osd opcode %d\n", src->op);
841
842 return 0;
843 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700844
Alex Elder065a68f2012-04-20 15:49:43 -0500845 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600846 case CEPH_OSD_OP_STAT:
847 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700848 case CEPH_OSD_OP_READ:
849 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200850 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800851 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800852 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600853 dst->extent.offset = cpu_to_le64(src->extent.offset);
854 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700855 dst->extent.truncate_size =
856 cpu_to_le64(src->extent.truncate_size);
857 dst->extent.truncate_seq =
858 cpu_to_le32(src->extent.truncate_seq);
859 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700860 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700861 dst->cls.class_len = src->cls.class_len;
862 dst->cls.method_len = src->cls.method_len;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200863 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700864 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700865 case CEPH_OSD_OP_STARTSYNC:
866 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700867 case CEPH_OSD_OP_WATCH:
868 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200869 dst->watch.ver = cpu_to_le64(0);
870 dst->watch.op = src->watch.op;
871 dst->watch.gen = cpu_to_le32(src->watch.gen);
872 break;
873 case CEPH_OSD_OP_NOTIFY_ACK:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700874 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200875 case CEPH_OSD_OP_NOTIFY:
876 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
877 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700878 case CEPH_OSD_OP_LIST_WATCHERS:
879 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200880 case CEPH_OSD_OP_SETALLOCHINT:
881 dst->alloc_hint.expected_object_size =
882 cpu_to_le64(src->alloc_hint.expected_object_size);
883 dst->alloc_hint.expected_write_size =
884 cpu_to_le64(src->alloc_hint.expected_write_size);
885 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800886 case CEPH_OSD_OP_SETXATTR:
887 case CEPH_OSD_OP_CMPXATTR:
888 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
889 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
890 dst->xattr.cmp_op = src->xattr.cmp_op;
891 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800892 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800893 case CEPH_OSD_OP_CREATE:
894 case CEPH_OSD_OP_DELETE:
895 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700896 default:
Alex Elder4c464592013-02-15 11:42:30 -0600897 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600898 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600899 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500900
901 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700902 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200903
Alex Eldera8dd0a32013-03-13 20:50:00 -0500904 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200905 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100906 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600907
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200908 return src->indata_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700909}
910
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700911/*
Sage Weilf24e9982009-10-06 11:31:10 -0700912 * build new request AND message, calculate layout, and adjust file
913 * extent as needed.
914 *
915 * if the file was recently truncated, we include information about its
916 * old and new size so that the object can be updated appropriately. (we
917 * avoid synchronously deleting truncated objects because it's slow.)
918 *
919 * if @do_sync, include a 'startsync' command so that the osd will flush
920 * data quickly.
921 */
922struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
923 struct ceph_file_layout *layout,
924 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800925 u64 off, u64 *plen,
926 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700927 int opcode, int flags,
928 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700929 u32 truncate_seq,
930 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600931 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700932{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700933 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500934 u64 objnum = 0;
935 u64 objoff = 0;
936 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700937 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700938
Li Wangad7a60d2013-08-15 11:51:44 +0800939 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800940 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
941 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700942
Alex Elderacead002013-03-14 14:09:05 -0500943 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600944 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200945 if (!req) {
946 r = -ENOMEM;
947 goto fail;
948 }
Alex Elder79528732013-04-03 21:32:51 -0500949
Sage Weilf24e9982009-10-06 11:31:10 -0700950 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500951 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200952 if (r)
953 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500954
Yan, Zheng864e9192014-11-13 10:47:25 +0800955 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800956 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800957 } else {
Yan, Zheng76271512016-02-03 21:24:49 +0800958 u32 object_size = layout->object_size;
Yan, Zheng864e9192014-11-13 10:47:25 +0800959 u32 object_base = off - objoff;
960 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
961 if (truncate_size <= object_base) {
962 truncate_size = 0;
963 } else {
964 truncate_size -= object_base;
965 if (truncate_size > object_size)
966 truncate_size = object_size;
967 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800968 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800969 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800970 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500971 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500972
Jeff Laytona1f40202017-04-04 08:39:37 -0400973 req->r_abort_on_full = true;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200974 req->r_flags = flags;
Yan, Zheng76271512016-02-03 21:24:49 +0800975 req->r_base_oloc.pool = layout->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +0800976 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200977 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600978
Ilya Dryomovbb873b5392016-05-26 00:29:52 +0200979 req->r_snapid = vino.snap;
980 if (flags & CEPH_OSD_FLAG_WRITE)
981 req->r_data_offset = off;
982
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200983 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
984 if (r)
985 goto fail;
986
Sage Weilf24e9982009-10-06 11:31:10 -0700987 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200988
989fail:
990 ceph_osdc_put_request(req);
991 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700992}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700993EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700994
995/*
996 * We keep osd requests in an rbtree, sorted by ->r_tid.
997 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200998DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Ilya Dryomov46092452016-04-28 16:07:27 +0200999DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
Sage Weilf24e9982009-10-06 11:31:10 -07001000
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001001static bool osd_homeless(struct ceph_osd *osd)
1002{
1003 return osd->o_osd == CEPH_HOMELESS_OSD;
1004}
1005
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001006static bool osd_registered(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001007{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001008 verify_osdc_locked(osd->o_osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001009
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001010 return !RB_EMPTY_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001011}
1012
1013/*
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001014 * Assumes @osd is zero-initialized.
1015 */
1016static void osd_init(struct ceph_osd *osd)
1017{
Elena Reshetova02113a02017-03-17 14:10:28 +02001018 refcount_set(&osd->o_ref, 1);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001019 RB_CLEAR_NODE(&osd->o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001020 osd->o_requests = RB_ROOT;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001021 osd->o_linger_requests = RB_ROOT;
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001022 INIT_LIST_HEAD(&osd->o_osd_lru);
1023 INIT_LIST_HEAD(&osd->o_keepalive_item);
1024 osd->o_incarnation = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001025 mutex_init(&osd->lock);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001026}
1027
1028static void osd_cleanup(struct ceph_osd *osd)
1029{
1030 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001031 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001032 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001033 WARN_ON(!list_empty(&osd->o_osd_lru));
1034 WARN_ON(!list_empty(&osd->o_keepalive_item));
1035
1036 if (osd->o_auth.authorizer) {
1037 WARN_ON(osd_homeless(osd));
1038 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1039 }
1040}
1041
1042/*
Sage Weilf24e9982009-10-06 11:31:10 -07001043 * Track open sessions with osds.
1044 */
Alex Eldere10006f2012-05-26 23:26:43 -05001045static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001046{
1047 struct ceph_osd *osd;
1048
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001049 WARN_ON(onum == CEPH_HOMELESS_OSD);
1050
Ilya Dryomov7a28f592016-04-28 16:07:25 +02001051 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001052 osd_init(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001053 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001054 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -07001055
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001056 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001057
Sage Weilf24e9982009-10-06 11:31:10 -07001058 return osd;
1059}
1060
1061static struct ceph_osd *get_osd(struct ceph_osd *osd)
1062{
Elena Reshetova02113a02017-03-17 14:10:28 +02001063 if (refcount_inc_not_zero(&osd->o_ref)) {
1064 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1065 refcount_read(&osd->o_ref));
Sage Weilf24e9982009-10-06 11:31:10 -07001066 return osd;
1067 } else {
1068 dout("get_osd %p FAIL\n", osd);
1069 return NULL;
1070 }
1071}
1072
1073static void put_osd(struct ceph_osd *osd)
1074{
Elena Reshetova02113a02017-03-17 14:10:28 +02001075 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1076 refcount_read(&osd->o_ref) - 1);
1077 if (refcount_dec_and_test(&osd->o_ref)) {
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001078 osd_cleanup(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001079 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001080 }
Sage Weilf24e9982009-10-06 11:31:10 -07001081}
1082
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001083DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1084
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001085static void __move_osd_to_lru(struct ceph_osd *osd)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001086{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001087 struct ceph_osd_client *osdc = osd->o_osdc;
1088
1089 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001090 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001091
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001092 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001093 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001094 spin_unlock(&osdc->osd_lru_lock);
1095
Ilya Dryomova319bf52015-05-15 12:02:17 +03001096 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001097}
1098
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001099static void maybe_move_osd_to_lru(struct ceph_osd *osd)
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001100{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001101 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001102 RB_EMPTY_ROOT(&osd->o_linger_requests))
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001103 __move_osd_to_lru(osd);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001104}
1105
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001106static void __remove_osd_from_lru(struct ceph_osd *osd)
1107{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001108 struct ceph_osd_client *osdc = osd->o_osdc;
1109
1110 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1111
1112 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001113 if (!list_empty(&osd->o_osd_lru))
1114 list_del_init(&osd->o_osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001115 spin_unlock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001116}
1117
Sage Weilf24e9982009-10-06 11:31:10 -07001118/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001119 * Close the connection and assign any leftover requests to the
1120 * homeless session.
1121 */
1122static void close_osd(struct ceph_osd *osd)
1123{
1124 struct ceph_osd_client *osdc = osd->o_osdc;
1125 struct rb_node *n;
1126
1127 verify_osdc_wrlocked(osdc);
1128 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1129
1130 ceph_con_close(&osd->o_con);
1131
1132 for (n = rb_first(&osd->o_requests); n; ) {
1133 struct ceph_osd_request *req =
1134 rb_entry(n, struct ceph_osd_request, r_node);
1135
1136 n = rb_next(n); /* unlink_request() */
1137
1138 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1139 unlink_request(osd, req);
1140 link_request(&osdc->homeless_osd, req);
1141 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02001142 for (n = rb_first(&osd->o_linger_requests); n; ) {
1143 struct ceph_osd_linger_request *lreq =
1144 rb_entry(n, struct ceph_osd_linger_request, node);
1145
1146 n = rb_next(n); /* unlink_linger() */
1147
1148 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1149 lreq->linger_id);
1150 unlink_linger(osd, lreq);
1151 link_linger(&osdc->homeless_osd, lreq);
1152 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001153
1154 __remove_osd_from_lru(osd);
1155 erase_osd(&osdc->osds, osd);
1156 put_osd(osd);
1157}
1158
1159/*
Sage Weilf24e9982009-10-06 11:31:10 -07001160 * reset osd connect
1161 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001162static int reopen_osd(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001163{
Alex Elderc3acb182012-12-07 09:57:58 -06001164 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001165
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001166 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1167
1168 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001169 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001170 close_osd(osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001171 return -ENODEV;
1172 }
1173
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001174 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
Alex Elderc3acb182012-12-07 09:57:58 -06001175 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1176 !ceph_con_opened(&osd->o_con)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001177 struct rb_node *n;
Alex Elderc3acb182012-12-07 09:57:58 -06001178
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001179 dout("osd addr hasn't changed and connection never opened, "
1180 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001181 /* touch each r_stamp for handle_timeout()'s benfit */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001182 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1183 struct ceph_osd_request *req =
1184 rb_entry(n, struct ceph_osd_request, r_node);
Sage Weil87b315a2010-03-22 14:51:18 -07001185 req->r_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001186 }
Alex Elderc3acb182012-12-07 09:57:58 -06001187
1188 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001189 }
Alex Elderc3acb182012-12-07 09:57:58 -06001190
1191 ceph_con_close(&osd->o_con);
1192 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1193 osd->o_incarnation++;
1194
1195 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001196}
1197
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001198static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1199 bool wrlocked)
Sage Weilf24e9982009-10-06 11:31:10 -07001200{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001201 struct ceph_osd *osd;
1202
1203 if (wrlocked)
1204 verify_osdc_wrlocked(osdc);
1205 else
1206 verify_osdc_locked(osdc);
1207
1208 if (o != CEPH_HOMELESS_OSD)
1209 osd = lookup_osd(&osdc->osds, o);
1210 else
1211 osd = &osdc->homeless_osd;
1212 if (!osd) {
1213 if (!wrlocked)
1214 return ERR_PTR(-EAGAIN);
1215
1216 osd = create_osd(osdc, o);
1217 insert_osd(&osdc->osds, osd);
1218 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1219 &osdc->osdmap->osd_addr[osd->o_osd]);
1220 }
1221
1222 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1223 return osd;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001224}
1225
Sage Weilf24e9982009-10-06 11:31:10 -07001226/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001227 * Create request <-> OSD session relation.
1228 *
1229 * @req has to be assigned a tid, @osd may be homeless.
Sage Weilf24e9982009-10-06 11:31:10 -07001230 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001231static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001232{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001233 verify_osd_locked(osd);
1234 WARN_ON(!req->r_tid || req->r_osd);
1235 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1236 req, req->r_tid);
Sage Weil35f9f8a2012-05-16 15:16:38 -05001237
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001238 if (!osd_homeless(osd))
1239 __remove_osd_from_lru(osd);
1240 else
1241 atomic_inc(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001242
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001243 get_osd(osd);
1244 insert_request(&osd->o_requests, req);
1245 req->r_osd = osd;
Sage Weilf24e9982009-10-06 11:31:10 -07001246}
1247
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001248static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001249{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001250 verify_osd_locked(osd);
1251 WARN_ON(req->r_osd != osd);
1252 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1253 req, req->r_tid);
1254
1255 req->r_osd = NULL;
1256 erase_request(&osd->o_requests, req);
1257 put_osd(osd);
1258
1259 if (!osd_homeless(osd))
1260 maybe_move_osd_to_lru(osd);
1261 else
1262 atomic_dec(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001263}
1264
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001265static bool __pool_full(struct ceph_pg_pool_info *pi)
1266{
1267 return pi->flags & CEPH_POOL_FLAG_FULL;
1268}
1269
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001270static bool have_pool_full(struct ceph_osd_client *osdc)
1271{
1272 struct rb_node *n;
1273
1274 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1275 struct ceph_pg_pool_info *pi =
1276 rb_entry(n, struct ceph_pg_pool_info, node);
1277
1278 if (__pool_full(pi))
1279 return true;
1280 }
1281
1282 return false;
1283}
1284
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001285static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1286{
1287 struct ceph_pg_pool_info *pi;
1288
1289 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1290 if (!pi)
1291 return false;
1292
1293 return __pool_full(pi);
1294}
1295
Sage Weilf24e9982009-10-06 11:31:10 -07001296/*
Josh Durgind29adb32013-12-02 19:11:48 -08001297 * Returns whether a request should be blocked from being sent
1298 * based on the current osdmap and osd_client settings.
Josh Durgind29adb32013-12-02 19:11:48 -08001299 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001300static bool target_should_be_paused(struct ceph_osd_client *osdc,
1301 const struct ceph_osd_request_target *t,
1302 struct ceph_pg_pool_info *pi)
1303{
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001304 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1305 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1306 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001307 __pool_full(pi);
1308
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001309 WARN_ON(pi->id != t->target_oloc.pool);
Jeff Layton58eb7932017-04-18 09:21:16 -04001310 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1311 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1312 (osdc->osdmap->epoch < osdc->epoch_barrier);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001313}
1314
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001315enum calc_target_result {
1316 CALC_TARGET_NO_ACTION = 0,
1317 CALC_TARGET_NEED_RESEND,
1318 CALC_TARGET_POOL_DNE,
1319};
1320
1321static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1322 struct ceph_osd_request_target *t,
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001323 struct ceph_connection *con,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001324 bool any_change)
1325{
1326 struct ceph_pg_pool_info *pi;
1327 struct ceph_pg pgid, last_pgid;
1328 struct ceph_osds up, acting;
1329 bool force_resend = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001330 bool unpaused = false;
1331 bool legacy_change;
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001332 bool split = false;
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001333 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001334 enum calc_target_result ct_res;
1335 int ret;
1336
Ilya Dryomov04c7d782017-06-15 16:30:55 +02001337 t->epoch = osdc->osdmap->epoch;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001338 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1339 if (!pi) {
1340 t->osd = CEPH_HOMELESS_OSD;
1341 ct_res = CALC_TARGET_POOL_DNE;
1342 goto out;
1343 }
1344
1345 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001346 if (t->last_force_resend < pi->last_force_request_resend) {
1347 t->last_force_resend = pi->last_force_request_resend;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001348 force_resend = true;
Ilya Dryomovdc93e0e2017-06-05 14:45:00 +02001349 } else if (t->last_force_resend == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001350 force_resend = true;
1351 }
1352 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001353
Ilya Dryomovdb098ec2017-06-15 16:30:55 +02001354 /* apply tiering */
1355 ceph_oid_copy(&t->target_oid, &t->base_oid);
1356 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1357 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001358 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1359 t->target_oloc.pool = pi->read_tier;
1360 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1361 t->target_oloc.pool = pi->write_tier;
Ilya Dryomov6d637a52017-06-15 16:30:55 +02001362
1363 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1364 if (!pi) {
1365 t->osd = CEPH_HOMELESS_OSD;
1366 ct_res = CALC_TARGET_POOL_DNE;
1367 goto out;
1368 }
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001369 }
1370
1371 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1372 &t->target_oloc, &pgid);
1373 if (ret) {
1374 WARN_ON(ret != -ENOENT);
1375 t->osd = CEPH_HOMELESS_OSD;
1376 ct_res = CALC_TARGET_POOL_DNE;
1377 goto out;
1378 }
1379 last_pgid.pool = pgid.pool;
1380 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1381
1382 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1383 if (any_change &&
1384 ceph_is_new_interval(&t->acting,
1385 &acting,
1386 &t->up,
1387 &up,
1388 t->size,
1389 pi->size,
1390 t->min_size,
1391 pi->min_size,
1392 t->pg_num,
1393 pi->pg_num,
1394 t->sort_bitwise,
1395 sort_bitwise,
1396 &last_pgid))
1397 force_resend = true;
1398
1399 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1400 t->paused = false;
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001401 unpaused = true;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001402 }
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001403 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1404 ceph_osds_changed(&t->acting, &acting, any_change);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001405 if (t->pg_num)
1406 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001407
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001408 if (legacy_change || force_resend || split) {
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001409 t->pgid = pgid; /* struct */
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02001410 ceph_pg_to_primary_shard(osdc->osdmap, &pgid, &t->spgid);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001411 ceph_osds_copy(&t->acting, &acting);
1412 ceph_osds_copy(&t->up, &up);
1413 t->size = pi->size;
1414 t->min_size = pi->min_size;
1415 t->pg_num = pi->pg_num;
1416 t->pg_num_mask = pi->pg_num_mask;
1417 t->sort_bitwise = sort_bitwise;
1418
1419 t->osd = acting.primary;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001420 }
1421
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001422 if (unpaused || legacy_change || force_resend ||
1423 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1424 RESEND_ON_SPLIT)))
Ilya Dryomov84ed45d2017-06-15 16:30:54 +02001425 ct_res = CALC_TARGET_NEED_RESEND;
1426 else
1427 ct_res = CALC_TARGET_NO_ACTION;
1428
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001429out:
1430 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1431 return ct_res;
1432}
1433
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001434static void setup_request_data(struct ceph_osd_request *req,
1435 struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001436{
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001437 u32 data_len = 0;
1438 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001439
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001440 if (!list_empty(&msg->data))
1441 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001442
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001443 WARN_ON(msg->data_length);
1444 for (i = 0; i < req->r_num_ops; i++) {
1445 struct ceph_osd_req_op *op = &req->r_ops[i];
1446
1447 switch (op->op) {
1448 /* request */
1449 case CEPH_OSD_OP_WRITE:
1450 case CEPH_OSD_OP_WRITEFULL:
1451 WARN_ON(op->indata_len != op->extent.length);
1452 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1453 break;
1454 case CEPH_OSD_OP_SETXATTR:
1455 case CEPH_OSD_OP_CMPXATTR:
1456 WARN_ON(op->indata_len != op->xattr.name_len +
1457 op->xattr.value_len);
1458 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1459 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001460 case CEPH_OSD_OP_NOTIFY_ACK:
1461 ceph_osdc_msg_data_add(msg,
1462 &op->notify_ack.request_data);
1463 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001464
1465 /* reply */
1466 case CEPH_OSD_OP_STAT:
1467 ceph_osdc_msg_data_add(req->r_reply,
1468 &op->raw_data_in);
1469 break;
1470 case CEPH_OSD_OP_READ:
1471 ceph_osdc_msg_data_add(req->r_reply,
1472 &op->extent.osd_data);
1473 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001474 case CEPH_OSD_OP_LIST_WATCHERS:
1475 ceph_osdc_msg_data_add(req->r_reply,
1476 &op->list_watchers.response_data);
1477 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001478
1479 /* both */
1480 case CEPH_OSD_OP_CALL:
1481 WARN_ON(op->indata_len != op->cls.class_len +
1482 op->cls.method_len +
1483 op->cls.indata_len);
1484 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1485 /* optional, can be NONE */
1486 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1487 /* optional, can be NONE */
1488 ceph_osdc_msg_data_add(req->r_reply,
1489 &op->cls.response_data);
1490 break;
Ilya Dryomov19079202016-04-28 16:07:27 +02001491 case CEPH_OSD_OP_NOTIFY:
1492 ceph_osdc_msg_data_add(msg,
1493 &op->notify.request_data);
1494 ceph_osdc_msg_data_add(req->r_reply,
1495 &op->notify.response_data);
1496 break;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001497 }
1498
1499 data_len += op->indata_len;
1500 }
1501
1502 WARN_ON(data_len != msg->data_length);
1503}
1504
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001505static void encode_pgid(void **p, const struct ceph_pg *pgid)
1506{
1507 ceph_encode_8(p, 1);
1508 ceph_encode_64(p, pgid->pool);
1509 ceph_encode_32(p, pgid->seed);
1510 ceph_encode_32(p, -1); /* preferred */
1511}
1512
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001513static void encode_spgid(void **p, const struct ceph_spg *spgid)
1514{
1515 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1516 encode_pgid(p, &spgid->pgid);
1517 ceph_encode_8(p, spgid->shard);
1518}
1519
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001520static void encode_oloc(void **p, void *end,
1521 const struct ceph_object_locator *oloc)
1522{
1523 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1524 ceph_encode_64(p, oloc->pool);
1525 ceph_encode_32(p, -1); /* preferred */
1526 ceph_encode_32(p, 0); /* key len */
1527 if (oloc->pool_ns)
1528 ceph_encode_string(p, end, oloc->pool_ns->str,
1529 oloc->pool_ns->len);
1530 else
1531 ceph_encode_32(p, 0);
1532}
1533
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001534static void encode_request_partial(struct ceph_osd_request *req,
1535 struct ceph_msg *msg)
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001536{
1537 void *p = msg->front.iov_base;
1538 void *const end = p + msg->front_alloc_len;
1539 u32 data_len = 0;
1540 int i;
1541
1542 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1543 /* snapshots aren't writeable */
1544 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1545 } else {
1546 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1547 req->r_data_offset || req->r_snapc);
1548 }
1549
1550 setup_request_data(req, msg);
1551
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001552 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1553 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001554 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1555 ceph_encode_32(&p, req->r_flags);
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001556
1557 /* reqid */
1558 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1559 memset(p, 0, sizeof(struct ceph_osd_reqid));
1560 p += sizeof(struct ceph_osd_reqid);
1561
1562 /* trace */
1563 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1564 p += sizeof(struct ceph_blkin_trace_info);
1565
1566 ceph_encode_32(&p, 0); /* client_inc, always 0 */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001567 ceph_encode_timespec(p, &req->r_mtime);
1568 p += sizeof(struct ceph_timespec);
Jeff Laytonaa26d662017-04-04 08:39:36 -04001569
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001570 encode_oloc(&p, end, &req->r_t.target_oloc);
Ilya Dryomov2e59ffd2017-06-15 16:30:53 +02001571 ceph_encode_string(&p, end, req->r_t.target_oid.name,
1572 req->r_t.target_oid.name_len);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001573
1574 /* ops, can imply data */
1575 ceph_encode_16(&p, req->r_num_ops);
1576 for (i = 0; i < req->r_num_ops; i++) {
1577 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1578 p += sizeof(struct ceph_osd_op);
1579 }
1580
1581 ceph_encode_64(&p, req->r_snapid); /* snapid */
1582 if (req->r_snapc) {
1583 ceph_encode_64(&p, req->r_snapc->seq);
1584 ceph_encode_32(&p, req->r_snapc->num_snaps);
1585 for (i = 0; i < req->r_snapc->num_snaps; i++)
1586 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1587 } else {
1588 ceph_encode_64(&p, 0); /* snap_seq */
1589 ceph_encode_32(&p, 0); /* snaps len */
1590 }
1591
1592 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001593 BUG_ON(p != end - 8); /* space for features */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001594
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001595 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
1596 /* front_len is finalized in encode_request_finish() */
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001597 msg->hdr.data_len = cpu_to_le32(data_len);
1598 /*
1599 * The header "data_off" is a hint to the receiver allowing it
1600 * to align received data into its buffers such that there's no
1601 * need to re-copy it before writing it to disk (direct I/O).
1602 */
1603 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1604
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001605 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
1606 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1607}
1608
1609static void encode_request_finish(struct ceph_msg *msg)
1610{
1611 void *p = msg->front.iov_base;
1612 void *const end = p + msg->front_alloc_len;
1613
1614 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
1615 /* luminous OSD -- encode features and be done */
1616 p = end - 8;
1617 ceph_encode_64(&p, msg->con->peer_features);
1618 } else {
1619 struct {
1620 char spgid[CEPH_ENCODING_START_BLK_LEN +
1621 CEPH_PGID_ENCODING_LEN + 1];
1622 __le32 hash;
1623 __le32 epoch;
1624 __le32 flags;
1625 char reqid[CEPH_ENCODING_START_BLK_LEN +
1626 sizeof(struct ceph_osd_reqid)];
1627 char trace[sizeof(struct ceph_blkin_trace_info)];
1628 __le32 client_inc;
1629 struct ceph_timespec mtime;
1630 } __packed head;
1631 struct ceph_pg pgid;
1632 void *oloc, *oid, *tail;
1633 int oloc_len, oid_len, tail_len;
1634 int len;
1635
1636 /*
1637 * Pre-luminous OSD -- reencode v8 into v4 using @head
1638 * as a temporary buffer. Encode the raw PG; the rest
1639 * is just a matter of moving oloc, oid and tail blobs
1640 * around.
1641 */
1642 memcpy(&head, p, sizeof(head));
1643 p += sizeof(head);
1644
1645 oloc = p;
1646 p += CEPH_ENCODING_START_BLK_LEN;
1647 pgid.pool = ceph_decode_64(&p);
1648 p += 4 + 4; /* preferred, key len */
1649 len = ceph_decode_32(&p);
1650 p += len; /* nspace */
1651 oloc_len = p - oloc;
1652
1653 oid = p;
1654 len = ceph_decode_32(&p);
1655 p += len;
1656 oid_len = p - oid;
1657
1658 tail = p;
1659 tail_len = (end - p) - 8;
1660
1661 p = msg->front.iov_base;
1662 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
1663 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
1664 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
1665 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
1666
1667 /* reassert_version */
1668 memset(p, 0, sizeof(struct ceph_eversion));
1669 p += sizeof(struct ceph_eversion);
1670
1671 BUG_ON(p >= oloc);
1672 memmove(p, oloc, oloc_len);
1673 p += oloc_len;
1674
1675 pgid.seed = le32_to_cpu(head.hash);
1676 encode_pgid(&p, &pgid); /* raw pg */
1677
1678 BUG_ON(p >= oid);
1679 memmove(p, oid, oid_len);
1680 p += oid_len;
1681
1682 /* tail -- ops, snapid, snapc, retry_attempt */
1683 BUG_ON(p >= tail);
1684 memmove(p, tail, tail_len);
1685 p += tail_len;
1686
1687 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1688 }
1689
1690 BUG_ON(p > end);
1691 msg->front.iov_len = p - msg->front.iov_base;
1692 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1693
1694 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
1695 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
1696 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
1697 le16_to_cpu(msg->hdr.version));
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001698}
1699
1700/*
1701 * @req has to be assigned a tid and registered.
1702 */
1703static void send_request(struct ceph_osd_request *req)
1704{
1705 struct ceph_osd *osd = req->r_osd;
1706
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001707 verify_osd_locked(osd);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001708 WARN_ON(osd->o_osd != req->r_t.osd);
1709
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001710 /*
1711 * We may have a previously queued request message hanging
1712 * around. Cancel it to avoid corrupting the msgr.
1713 */
1714 if (req->r_sent)
1715 ceph_msg_revoke(req->r_request);
1716
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001717 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1718 if (req->r_attempts)
1719 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1720 else
1721 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1722
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02001723 encode_request_partial(req, req->r_request);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001724
Ilya Dryomov04c7d782017-06-15 16:30:55 +02001725 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001726 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02001727 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
Ilya Dryomov04c7d782017-06-15 16:30:55 +02001728 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
1729 req->r_attempts);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001730
1731 req->r_t.paused = false;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001732 req->r_stamp = jiffies;
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001733 req->r_attempts++;
Sage Weilf24e9982009-10-06 11:31:10 -07001734
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001735 req->r_sent = osd->o_incarnation;
1736 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1737 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
Sage Weilf24e9982009-10-06 11:31:10 -07001738}
1739
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001740static void maybe_request_map(struct ceph_osd_client *osdc)
1741{
1742 bool continuous = false;
1743
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001744 verify_osdc_locked(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001745 WARN_ON(!osdc->osdmap->epoch);
1746
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001747 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1748 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
1749 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001750 dout("%s osdc %p continuous\n", __func__, osdc);
1751 continuous = true;
1752 } else {
1753 dout("%s osdc %p onetime\n", __func__, osdc);
1754 }
1755
1756 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1757 osdc->osdmap->epoch + 1, continuous))
1758 ceph_monc_renew_subs(&osdc->client->monc);
1759}
1760
Jeff Laytona1f40202017-04-04 08:39:37 -04001761static void complete_request(struct ceph_osd_request *req, int err);
Ilya Dryomov46092452016-04-28 16:07:27 +02001762static void send_map_check(struct ceph_osd_request *req);
1763
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001764static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001765{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001766 struct ceph_osd_client *osdc = req->r_osdc;
1767 struct ceph_osd *osd;
Ilya Dryomov46092452016-04-28 16:07:27 +02001768 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001769 bool need_send = false;
1770 bool promoted = false;
Jeff Laytona1f40202017-04-04 08:39:37 -04001771 bool need_abort = false;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001772
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001773 WARN_ON(req->r_tid);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001774 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
1775
1776again:
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001777 ct_res = calc_target(osdc, &req->r_t, NULL, false);
Ilya Dryomov46092452016-04-28 16:07:27 +02001778 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
1779 goto promote;
1780
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001781 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
1782 if (IS_ERR(osd)) {
1783 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
1784 goto promote;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001785 }
1786
Jeff Layton58eb7932017-04-18 09:21:16 -04001787 if (osdc->osdmap->epoch < osdc->epoch_barrier) {
1788 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
1789 osdc->epoch_barrier);
1790 req->r_t.paused = true;
1791 maybe_request_map(osdc);
1792 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1793 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001794 dout("req %p pausewr\n", req);
1795 req->r_t.paused = true;
1796 maybe_request_map(osdc);
1797 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001798 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001799 dout("req %p pauserd\n", req);
1800 req->r_t.paused = true;
1801 maybe_request_map(osdc);
1802 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1803 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
1804 CEPH_OSD_FLAG_FULL_FORCE)) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001805 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001806 pool_full(osdc, req->r_t.base_oloc.pool))) {
1807 dout("req %p full/pool_full\n", req);
1808 pr_warn_ratelimited("FULL or reached pool quota\n");
1809 req->r_t.paused = true;
1810 maybe_request_map(osdc);
Jeff Laytona1f40202017-04-04 08:39:37 -04001811 if (req->r_abort_on_full)
1812 need_abort = true;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001813 } else if (!osd_homeless(osd)) {
1814 need_send = true;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001815 } else {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001816 maybe_request_map(osdc);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001817 }
1818
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001819 mutex_lock(&osd->lock);
1820 /*
1821 * Assign the tid atomically with send_request() to protect
1822 * multiple writes to the same object from racing with each
1823 * other, resulting in out of order ops on the OSDs.
1824 */
1825 req->r_tid = atomic64_inc_return(&osdc->last_tid);
1826 link_request(osd, req);
1827 if (need_send)
1828 send_request(req);
Jeff Laytona1f40202017-04-04 08:39:37 -04001829 else if (need_abort)
1830 complete_request(req, -ENOSPC);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001831 mutex_unlock(&osd->lock);
1832
Ilya Dryomov46092452016-04-28 16:07:27 +02001833 if (ct_res == CALC_TARGET_POOL_DNE)
1834 send_map_check(req);
1835
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001836 if (promoted)
1837 downgrade_write(&osdc->lock);
1838 return;
1839
1840promote:
1841 up_read(&osdc->lock);
1842 down_write(&osdc->lock);
1843 wrlocked = true;
1844 promoted = true;
1845 goto again;
1846}
1847
1848static void account_request(struct ceph_osd_request *req)
1849{
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001850 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001851 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001852
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001853 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001854 atomic_inc(&req->r_osdc->num_requests);
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01001855
1856 req->r_start_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001857}
1858
1859static void submit_request(struct ceph_osd_request *req, bool wrlocked)
1860{
1861 ceph_osdc_get_request(req);
1862 account_request(req);
1863 __submit_request(req, wrlocked);
1864}
1865
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001866static void finish_request(struct ceph_osd_request *req)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001867{
1868 struct ceph_osd_client *osdc = req->r_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001869
Ilya Dryomov46092452016-04-28 16:07:27 +02001870 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
Ilya Dryomov04c7d782017-06-15 16:30:55 +02001871 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1872
1873 if (req->r_osd)
1874 unlink_request(req->r_osd, req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001875 atomic_dec(&osdc->num_requests);
1876
1877 /*
1878 * If an OSD has failed or returned and a request has been sent
1879 * twice, it's possible to get a reply and end up here while the
1880 * request message is queued for delivery. We will ignore the
1881 * reply, so not a big deal, but better to try and catch it.
1882 */
1883 ceph_msg_revoke(req->r_request);
1884 ceph_msg_revoke_incoming(req->r_reply);
1885}
1886
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001887static void __complete_request(struct ceph_osd_request *req)
1888{
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001889 if (req->r_callback) {
1890 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
1891 req->r_tid, req->r_callback, req->r_result);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001892 req->r_callback(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001893 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001894}
1895
Ilya Dryomov46092452016-04-28 16:07:27 +02001896/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001897 * This is open-coded in handle_reply().
Ilya Dryomov46092452016-04-28 16:07:27 +02001898 */
1899static void complete_request(struct ceph_osd_request *req, int err)
1900{
1901 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1902
1903 req->r_result = err;
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001904 finish_request(req);
Ilya Dryomov46092452016-04-28 16:07:27 +02001905 __complete_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001906 complete_all(&req->r_completion);
Ilya Dryomov46092452016-04-28 16:07:27 +02001907 ceph_osdc_put_request(req);
1908}
1909
1910static void cancel_map_check(struct ceph_osd_request *req)
1911{
1912 struct ceph_osd_client *osdc = req->r_osdc;
1913 struct ceph_osd_request *lookup_req;
1914
1915 verify_osdc_wrlocked(osdc);
1916
1917 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1918 if (!lookup_req)
1919 return;
1920
1921 WARN_ON(lookup_req != req);
1922 erase_request_mc(&osdc->map_checks, req);
1923 ceph_osdc_put_request(req);
1924}
1925
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001926static void cancel_request(struct ceph_osd_request *req)
1927{
1928 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1929
Ilya Dryomov46092452016-04-28 16:07:27 +02001930 cancel_map_check(req);
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001931 finish_request(req);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01001932 complete_all(&req->r_completion);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01001933 ceph_osdc_put_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001934}
1935
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01001936static void abort_request(struct ceph_osd_request *req, int err)
1937{
1938 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1939
1940 cancel_map_check(req);
1941 complete_request(req, err);
1942}
1943
Jeff Layton58eb7932017-04-18 09:21:16 -04001944static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
1945{
1946 if (likely(eb > osdc->epoch_barrier)) {
1947 dout("updating epoch_barrier from %u to %u\n",
1948 osdc->epoch_barrier, eb);
1949 osdc->epoch_barrier = eb;
1950 /* Request map if we're not to the barrier yet */
1951 if (eb > osdc->osdmap->epoch)
1952 maybe_request_map(osdc);
1953 }
1954}
1955
1956void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
1957{
1958 down_read(&osdc->lock);
1959 if (unlikely(eb > osdc->epoch_barrier)) {
1960 up_read(&osdc->lock);
1961 down_write(&osdc->lock);
1962 update_epoch_barrier(osdc, eb);
1963 up_write(&osdc->lock);
1964 } else {
1965 up_read(&osdc->lock);
1966 }
1967}
1968EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
1969
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04001970/*
1971 * Drop all pending requests that are stalled waiting on a full condition to
Jeff Layton58eb7932017-04-18 09:21:16 -04001972 * clear, and complete them with ENOSPC as the return code. Set the
1973 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
1974 * cancelled.
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04001975 */
1976static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
1977{
1978 struct rb_node *n;
Jeff Layton58eb7932017-04-18 09:21:16 -04001979 bool victims = false;
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04001980
1981 dout("enter abort_on_full\n");
1982
1983 if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
1984 goto out;
1985
Jeff Layton58eb7932017-04-18 09:21:16 -04001986 /* Scan list and see if there is anything to abort */
1987 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1988 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1989 struct rb_node *m;
1990
1991 m = rb_first(&osd->o_requests);
1992 while (m) {
1993 struct ceph_osd_request *req = rb_entry(m,
1994 struct ceph_osd_request, r_node);
1995 m = rb_next(m);
1996
1997 if (req->r_abort_on_full) {
1998 victims = true;
1999 break;
2000 }
2001 }
2002 if (victims)
2003 break;
2004 }
2005
2006 if (!victims)
2007 goto out;
2008
2009 /*
2010 * Update the barrier to current epoch if it's behind that point,
2011 * since we know we have some calls to be aborted in the tree.
2012 */
2013 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2014
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002015 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2016 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2017 struct rb_node *m;
2018
2019 m = rb_first(&osd->o_requests);
2020 while (m) {
2021 struct ceph_osd_request *req = rb_entry(m,
2022 struct ceph_osd_request, r_node);
2023 m = rb_next(m);
2024
2025 if (req->r_abort_on_full &&
2026 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2027 pool_full(osdc, req->r_t.target_oloc.pool)))
2028 abort_request(req, -ENOSPC);
2029 }
2030 }
2031out:
Jeff Layton58eb7932017-04-18 09:21:16 -04002032 dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04002033}
2034
Ilya Dryomov46092452016-04-28 16:07:27 +02002035static void check_pool_dne(struct ceph_osd_request *req)
2036{
2037 struct ceph_osd_client *osdc = req->r_osdc;
2038 struct ceph_osdmap *map = osdc->osdmap;
2039
2040 verify_osdc_wrlocked(osdc);
2041 WARN_ON(!map->epoch);
2042
2043 if (req->r_attempts) {
2044 /*
2045 * We sent a request earlier, which means that
2046 * previously the pool existed, and now it does not
2047 * (i.e., it was deleted).
2048 */
2049 req->r_map_dne_bound = map->epoch;
2050 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2051 req->r_tid);
2052 } else {
2053 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2054 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2055 }
2056
2057 if (req->r_map_dne_bound) {
2058 if (map->epoch >= req->r_map_dne_bound) {
2059 /* we had a new enough map */
2060 pr_info_ratelimited("tid %llu pool does not exist\n",
2061 req->r_tid);
2062 complete_request(req, -ENOENT);
2063 }
2064 } else {
2065 send_map_check(req);
2066 }
2067}
2068
2069static void map_check_cb(struct ceph_mon_generic_request *greq)
2070{
2071 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2072 struct ceph_osd_request *req;
2073 u64 tid = greq->private_data;
2074
2075 WARN_ON(greq->result || !greq->u.newest);
2076
2077 down_write(&osdc->lock);
2078 req = lookup_request_mc(&osdc->map_checks, tid);
2079 if (!req) {
2080 dout("%s tid %llu dne\n", __func__, tid);
2081 goto out_unlock;
2082 }
2083
2084 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2085 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2086 if (!req->r_map_dne_bound)
2087 req->r_map_dne_bound = greq->u.newest;
2088 erase_request_mc(&osdc->map_checks, req);
2089 check_pool_dne(req);
2090
2091 ceph_osdc_put_request(req);
2092out_unlock:
2093 up_write(&osdc->lock);
2094}
2095
2096static void send_map_check(struct ceph_osd_request *req)
2097{
2098 struct ceph_osd_client *osdc = req->r_osdc;
2099 struct ceph_osd_request *lookup_req;
2100 int ret;
2101
2102 verify_osdc_wrlocked(osdc);
2103
2104 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2105 if (lookup_req) {
2106 WARN_ON(lookup_req != req);
2107 return;
2108 }
2109
2110 ceph_osdc_get_request(req);
2111 insert_request_mc(&osdc->map_checks, req);
2112 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2113 map_check_cb, req->r_tid);
2114 WARN_ON(ret);
2115}
2116
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002117/*
Ilya Dryomov922dab62016-05-26 01:15:02 +02002118 * lingering requests, watch/notify v2 infrastructure
2119 */
2120static void linger_release(struct kref *kref)
2121{
2122 struct ceph_osd_linger_request *lreq =
2123 container_of(kref, struct ceph_osd_linger_request, kref);
2124
2125 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2126 lreq->reg_req, lreq->ping_req);
2127 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2128 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
Ilya Dryomov46092452016-04-28 16:07:27 +02002129 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002130 WARN_ON(!list_empty(&lreq->scan_item));
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002131 WARN_ON(!list_empty(&lreq->pending_lworks));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002132 WARN_ON(lreq->osd);
2133
2134 if (lreq->reg_req)
2135 ceph_osdc_put_request(lreq->reg_req);
2136 if (lreq->ping_req)
2137 ceph_osdc_put_request(lreq->ping_req);
2138 target_destroy(&lreq->t);
2139 kfree(lreq);
2140}
2141
2142static void linger_put(struct ceph_osd_linger_request *lreq)
2143{
2144 if (lreq)
2145 kref_put(&lreq->kref, linger_release);
2146}
2147
2148static struct ceph_osd_linger_request *
2149linger_get(struct ceph_osd_linger_request *lreq)
2150{
2151 kref_get(&lreq->kref);
2152 return lreq;
2153}
2154
2155static struct ceph_osd_linger_request *
2156linger_alloc(struct ceph_osd_client *osdc)
2157{
2158 struct ceph_osd_linger_request *lreq;
2159
2160 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2161 if (!lreq)
2162 return NULL;
2163
2164 kref_init(&lreq->kref);
2165 mutex_init(&lreq->lock);
2166 RB_CLEAR_NODE(&lreq->node);
2167 RB_CLEAR_NODE(&lreq->osdc_node);
Ilya Dryomov46092452016-04-28 16:07:27 +02002168 RB_CLEAR_NODE(&lreq->mc_node);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002169 INIT_LIST_HEAD(&lreq->scan_item);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002170 INIT_LIST_HEAD(&lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002171 init_completion(&lreq->reg_commit_wait);
Ilya Dryomov19079202016-04-28 16:07:27 +02002172 init_completion(&lreq->notify_finish_wait);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002173
2174 lreq->osdc = osdc;
2175 target_init(&lreq->t);
2176
2177 dout("%s lreq %p\n", __func__, lreq);
2178 return lreq;
2179}
2180
2181DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2182DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02002183DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002184
2185/*
2186 * Create linger request <-> OSD session relation.
2187 *
2188 * @lreq has to be registered, @osd may be homeless.
2189 */
2190static void link_linger(struct ceph_osd *osd,
2191 struct ceph_osd_linger_request *lreq)
2192{
2193 verify_osd_locked(osd);
2194 WARN_ON(!lreq->linger_id || lreq->osd);
2195 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2196 osd->o_osd, lreq, lreq->linger_id);
2197
2198 if (!osd_homeless(osd))
2199 __remove_osd_from_lru(osd);
2200 else
2201 atomic_inc(&osd->o_osdc->num_homeless);
2202
2203 get_osd(osd);
2204 insert_linger(&osd->o_linger_requests, lreq);
2205 lreq->osd = osd;
2206}
2207
2208static void unlink_linger(struct ceph_osd *osd,
2209 struct ceph_osd_linger_request *lreq)
2210{
2211 verify_osd_locked(osd);
2212 WARN_ON(lreq->osd != osd);
2213 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2214 osd->o_osd, lreq, lreq->linger_id);
2215
2216 lreq->osd = NULL;
2217 erase_linger(&osd->o_linger_requests, lreq);
2218 put_osd(osd);
2219
2220 if (!osd_homeless(osd))
2221 maybe_move_osd_to_lru(osd);
2222 else
2223 atomic_dec(&osd->o_osdc->num_homeless);
2224}
2225
2226static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2227{
2228 verify_osdc_locked(lreq->osdc);
2229
2230 return !RB_EMPTY_NODE(&lreq->osdc_node);
2231}
2232
2233static bool linger_registered(struct ceph_osd_linger_request *lreq)
2234{
2235 struct ceph_osd_client *osdc = lreq->osdc;
2236 bool registered;
2237
2238 down_read(&osdc->lock);
2239 registered = __linger_registered(lreq);
2240 up_read(&osdc->lock);
2241
2242 return registered;
2243}
2244
2245static void linger_register(struct ceph_osd_linger_request *lreq)
2246{
2247 struct ceph_osd_client *osdc = lreq->osdc;
2248
2249 verify_osdc_wrlocked(osdc);
2250 WARN_ON(lreq->linger_id);
2251
2252 linger_get(lreq);
2253 lreq->linger_id = ++osdc->last_linger_id;
2254 insert_linger_osdc(&osdc->linger_requests, lreq);
2255}
2256
2257static void linger_unregister(struct ceph_osd_linger_request *lreq)
2258{
2259 struct ceph_osd_client *osdc = lreq->osdc;
2260
2261 verify_osdc_wrlocked(osdc);
2262
2263 erase_linger_osdc(&osdc->linger_requests, lreq);
2264 linger_put(lreq);
2265}
2266
2267static void cancel_linger_request(struct ceph_osd_request *req)
2268{
2269 struct ceph_osd_linger_request *lreq = req->r_priv;
2270
2271 WARN_ON(!req->r_linger);
2272 cancel_request(req);
2273 linger_put(lreq);
2274}
2275
2276struct linger_work {
2277 struct work_struct work;
2278 struct ceph_osd_linger_request *lreq;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002279 struct list_head pending_item;
2280 unsigned long queued_stamp;
Ilya Dryomov922dab62016-05-26 01:15:02 +02002281
2282 union {
2283 struct {
2284 u64 notify_id;
2285 u64 notifier_id;
2286 void *payload; /* points into @msg front */
2287 size_t payload_len;
2288
2289 struct ceph_msg *msg; /* for ceph_msg_put() */
2290 } notify;
2291 struct {
2292 int err;
2293 } error;
2294 };
2295};
2296
2297static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2298 work_func_t workfn)
2299{
2300 struct linger_work *lwork;
2301
2302 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2303 if (!lwork)
2304 return NULL;
2305
2306 INIT_WORK(&lwork->work, workfn);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002307 INIT_LIST_HEAD(&lwork->pending_item);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002308 lwork->lreq = linger_get(lreq);
2309
2310 return lwork;
2311}
2312
2313static void lwork_free(struct linger_work *lwork)
2314{
2315 struct ceph_osd_linger_request *lreq = lwork->lreq;
2316
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002317 mutex_lock(&lreq->lock);
2318 list_del(&lwork->pending_item);
2319 mutex_unlock(&lreq->lock);
2320
Ilya Dryomov922dab62016-05-26 01:15:02 +02002321 linger_put(lreq);
2322 kfree(lwork);
2323}
2324
2325static void lwork_queue(struct linger_work *lwork)
2326{
2327 struct ceph_osd_linger_request *lreq = lwork->lreq;
2328 struct ceph_osd_client *osdc = lreq->osdc;
2329
2330 verify_lreq_locked(lreq);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002331 WARN_ON(!list_empty(&lwork->pending_item));
2332
2333 lwork->queued_stamp = jiffies;
2334 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002335 queue_work(osdc->notify_wq, &lwork->work);
2336}
2337
2338static void do_watch_notify(struct work_struct *w)
2339{
2340 struct linger_work *lwork = container_of(w, struct linger_work, work);
2341 struct ceph_osd_linger_request *lreq = lwork->lreq;
2342
2343 if (!linger_registered(lreq)) {
2344 dout("%s lreq %p not registered\n", __func__, lreq);
2345 goto out;
2346 }
2347
Ilya Dryomov19079202016-04-28 16:07:27 +02002348 WARN_ON(!lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002349 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2350 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2351 lwork->notify.payload_len);
2352 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2353 lwork->notify.notifier_id, lwork->notify.payload,
2354 lwork->notify.payload_len);
2355
2356out:
2357 ceph_msg_put(lwork->notify.msg);
2358 lwork_free(lwork);
2359}
2360
2361static void do_watch_error(struct work_struct *w)
2362{
2363 struct linger_work *lwork = container_of(w, struct linger_work, work);
2364 struct ceph_osd_linger_request *lreq = lwork->lreq;
2365
2366 if (!linger_registered(lreq)) {
2367 dout("%s lreq %p not registered\n", __func__, lreq);
2368 goto out;
2369 }
2370
2371 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2372 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2373
2374out:
2375 lwork_free(lwork);
2376}
2377
2378static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2379{
2380 struct linger_work *lwork;
2381
2382 lwork = lwork_alloc(lreq, do_watch_error);
2383 if (!lwork) {
2384 pr_err("failed to allocate error-lwork\n");
2385 return;
2386 }
2387
2388 lwork->error.err = lreq->last_error;
2389 lwork_queue(lwork);
2390}
2391
2392static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2393 int result)
2394{
2395 if (!completion_done(&lreq->reg_commit_wait)) {
2396 lreq->reg_commit_error = (result <= 0 ? result : 0);
2397 complete_all(&lreq->reg_commit_wait);
2398 }
2399}
2400
2401static void linger_commit_cb(struct ceph_osd_request *req)
2402{
2403 struct ceph_osd_linger_request *lreq = req->r_priv;
2404
2405 mutex_lock(&lreq->lock);
2406 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2407 lreq->linger_id, req->r_result);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002408 linger_reg_commit_complete(lreq, req->r_result);
2409 lreq->committed = true;
2410
Ilya Dryomov19079202016-04-28 16:07:27 +02002411 if (!lreq->is_watch) {
2412 struct ceph_osd_data *osd_data =
2413 osd_req_op_data(req, 0, notify, response_data);
2414 void *p = page_address(osd_data->pages[0]);
2415
2416 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2417 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2418
2419 /* make note of the notify_id */
2420 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2421 lreq->notify_id = ceph_decode_64(&p);
2422 dout("lreq %p notify_id %llu\n", lreq,
2423 lreq->notify_id);
2424 } else {
2425 dout("lreq %p no notify_id\n", lreq);
2426 }
2427 }
2428
Ilya Dryomov922dab62016-05-26 01:15:02 +02002429 mutex_unlock(&lreq->lock);
2430 linger_put(lreq);
2431}
2432
2433static int normalize_watch_error(int err)
2434{
2435 /*
2436 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2437 * notification and a failure to reconnect because we raced with
2438 * the delete appear the same to the user.
2439 */
2440 if (err == -ENOENT)
2441 err = -ENOTCONN;
2442
2443 return err;
2444}
2445
2446static void linger_reconnect_cb(struct ceph_osd_request *req)
2447{
2448 struct ceph_osd_linger_request *lreq = req->r_priv;
2449
2450 mutex_lock(&lreq->lock);
2451 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2452 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2453 if (req->r_result < 0) {
2454 if (!lreq->last_error) {
2455 lreq->last_error = normalize_watch_error(req->r_result);
2456 queue_watch_error(lreq);
2457 }
2458 }
2459
2460 mutex_unlock(&lreq->lock);
2461 linger_put(lreq);
2462}
2463
2464static void send_linger(struct ceph_osd_linger_request *lreq)
2465{
2466 struct ceph_osd_request *req = lreq->reg_req;
2467 struct ceph_osd_req_op *op = &req->r_ops[0];
2468
2469 verify_osdc_wrlocked(req->r_osdc);
2470 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2471
2472 if (req->r_osd)
2473 cancel_linger_request(req);
2474
2475 request_reinit(req);
2476 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2477 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2478 req->r_flags = lreq->t.flags;
2479 req->r_mtime = lreq->mtime;
2480
2481 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002482 if (lreq->is_watch && lreq->committed) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002483 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2484 op->watch.cookie != lreq->linger_id);
2485 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2486 op->watch.gen = ++lreq->register_gen;
2487 dout("lreq %p reconnect register_gen %u\n", lreq,
2488 op->watch.gen);
2489 req->r_callback = linger_reconnect_cb;
2490 } else {
Ilya Dryomov19079202016-04-28 16:07:27 +02002491 if (!lreq->is_watch)
2492 lreq->notify_id = 0;
2493 else
2494 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002495 dout("lreq %p register\n", lreq);
2496 req->r_callback = linger_commit_cb;
2497 }
2498 mutex_unlock(&lreq->lock);
2499
2500 req->r_priv = linger_get(lreq);
2501 req->r_linger = true;
2502
2503 submit_request(req, true);
2504}
2505
2506static void linger_ping_cb(struct ceph_osd_request *req)
2507{
2508 struct ceph_osd_linger_request *lreq = req->r_priv;
2509
2510 mutex_lock(&lreq->lock);
2511 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2512 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2513 lreq->last_error);
2514 if (lreq->register_gen == req->r_ops[0].watch.gen) {
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002515 if (!req->r_result) {
2516 lreq->watch_valid_thru = lreq->ping_sent;
2517 } else if (!lreq->last_error) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002518 lreq->last_error = normalize_watch_error(req->r_result);
2519 queue_watch_error(lreq);
2520 }
2521 } else {
2522 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2523 lreq->register_gen, req->r_ops[0].watch.gen);
2524 }
2525
2526 mutex_unlock(&lreq->lock);
2527 linger_put(lreq);
2528}
2529
2530static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2531{
2532 struct ceph_osd_client *osdc = lreq->osdc;
2533 struct ceph_osd_request *req = lreq->ping_req;
2534 struct ceph_osd_req_op *op = &req->r_ops[0];
2535
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002536 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002537 dout("%s PAUSERD\n", __func__);
2538 return;
2539 }
2540
2541 lreq->ping_sent = jiffies;
2542 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2543 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2544 lreq->register_gen);
2545
2546 if (req->r_osd)
2547 cancel_linger_request(req);
2548
2549 request_reinit(req);
2550 target_copy(&req->r_t, &lreq->t);
2551
2552 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2553 op->watch.cookie != lreq->linger_id ||
2554 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2555 op->watch.gen = lreq->register_gen;
2556 req->r_callback = linger_ping_cb;
2557 req->r_priv = linger_get(lreq);
2558 req->r_linger = true;
2559
2560 ceph_osdc_get_request(req);
2561 account_request(req);
2562 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2563 link_request(lreq->osd, req);
2564 send_request(req);
2565}
2566
2567static void linger_submit(struct ceph_osd_linger_request *lreq)
2568{
2569 struct ceph_osd_client *osdc = lreq->osdc;
2570 struct ceph_osd *osd;
2571
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002572 calc_target(osdc, &lreq->t, NULL, false);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002573 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2574 link_linger(osd, lreq);
2575
2576 send_linger(lreq);
2577}
2578
Ilya Dryomov46092452016-04-28 16:07:27 +02002579static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2580{
2581 struct ceph_osd_client *osdc = lreq->osdc;
2582 struct ceph_osd_linger_request *lookup_lreq;
2583
2584 verify_osdc_wrlocked(osdc);
2585
2586 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2587 lreq->linger_id);
2588 if (!lookup_lreq)
2589 return;
2590
2591 WARN_ON(lookup_lreq != lreq);
2592 erase_linger_mc(&osdc->linger_map_checks, lreq);
2593 linger_put(lreq);
2594}
2595
Ilya Dryomov922dab62016-05-26 01:15:02 +02002596/*
2597 * @lreq has to be both registered and linked.
2598 */
2599static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2600{
Ilya Dryomov19079202016-04-28 16:07:27 +02002601 if (lreq->is_watch && lreq->ping_req->r_osd)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002602 cancel_linger_request(lreq->ping_req);
2603 if (lreq->reg_req->r_osd)
2604 cancel_linger_request(lreq->reg_req);
Ilya Dryomov46092452016-04-28 16:07:27 +02002605 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002606 unlink_linger(lreq->osd, lreq);
2607 linger_unregister(lreq);
2608}
2609
2610static void linger_cancel(struct ceph_osd_linger_request *lreq)
2611{
2612 struct ceph_osd_client *osdc = lreq->osdc;
2613
2614 down_write(&osdc->lock);
2615 if (__linger_registered(lreq))
2616 __linger_cancel(lreq);
2617 up_write(&osdc->lock);
2618}
2619
Ilya Dryomov46092452016-04-28 16:07:27 +02002620static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2621
2622static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2623{
2624 struct ceph_osd_client *osdc = lreq->osdc;
2625 struct ceph_osdmap *map = osdc->osdmap;
2626
2627 verify_osdc_wrlocked(osdc);
2628 WARN_ON(!map->epoch);
2629
2630 if (lreq->register_gen) {
2631 lreq->map_dne_bound = map->epoch;
2632 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2633 lreq, lreq->linger_id);
2634 } else {
2635 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2636 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2637 map->epoch);
2638 }
2639
2640 if (lreq->map_dne_bound) {
2641 if (map->epoch >= lreq->map_dne_bound) {
2642 /* we had a new enough map */
2643 pr_info("linger_id %llu pool does not exist\n",
2644 lreq->linger_id);
2645 linger_reg_commit_complete(lreq, -ENOENT);
2646 __linger_cancel(lreq);
2647 }
2648 } else {
2649 send_linger_map_check(lreq);
2650 }
2651}
2652
2653static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2654{
2655 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2656 struct ceph_osd_linger_request *lreq;
2657 u64 linger_id = greq->private_data;
2658
2659 WARN_ON(greq->result || !greq->u.newest);
2660
2661 down_write(&osdc->lock);
2662 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2663 if (!lreq) {
2664 dout("%s linger_id %llu dne\n", __func__, linger_id);
2665 goto out_unlock;
2666 }
2667
2668 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
2669 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2670 greq->u.newest);
2671 if (!lreq->map_dne_bound)
2672 lreq->map_dne_bound = greq->u.newest;
2673 erase_linger_mc(&osdc->linger_map_checks, lreq);
2674 check_linger_pool_dne(lreq);
2675
2676 linger_put(lreq);
2677out_unlock:
2678 up_write(&osdc->lock);
2679}
2680
2681static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
2682{
2683 struct ceph_osd_client *osdc = lreq->osdc;
2684 struct ceph_osd_linger_request *lookup_lreq;
2685 int ret;
2686
2687 verify_osdc_wrlocked(osdc);
2688
2689 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2690 lreq->linger_id);
2691 if (lookup_lreq) {
2692 WARN_ON(lookup_lreq != lreq);
2693 return;
2694 }
2695
2696 linger_get(lreq);
2697 insert_linger_mc(&osdc->linger_map_checks, lreq);
2698 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2699 linger_map_check_cb, lreq->linger_id);
2700 WARN_ON(ret);
2701}
2702
Ilya Dryomov922dab62016-05-26 01:15:02 +02002703static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
2704{
2705 int ret;
2706
2707 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2708 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
2709 return ret ?: lreq->reg_commit_error;
2710}
2711
Ilya Dryomov19079202016-04-28 16:07:27 +02002712static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
2713{
2714 int ret;
2715
2716 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2717 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
2718 return ret ?: lreq->notify_finish_error;
2719}
2720
Ilya Dryomov922dab62016-05-26 01:15:02 +02002721/*
Ilya Dryomovfbca9632016-04-28 16:07:24 +02002722 * Timeout callback, called every N seconds. When 1 or more OSD
2723 * requests has been active for more than N seconds, we send a keepalive
2724 * (tag + timestamp) to its OSD to ensure any communications channel
2725 * reset is detected.
Sage Weilf24e9982009-10-06 11:31:10 -07002726 */
2727static void handle_timeout(struct work_struct *work)
2728{
2729 struct ceph_osd_client *osdc =
2730 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03002731 struct ceph_options *opts = osdc->client->options;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002732 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002733 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002734 LIST_HEAD(slow_osds);
2735 struct rb_node *n, *p;
Sage Weilf24e9982009-10-06 11:31:10 -07002736
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002737 dout("%s osdc %p\n", __func__, osdc);
2738 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07002739
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002740 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002741 * ping osds that are a bit slow. this ensures that if there
2742 * is a break in the TCP connection we will notice, and reopen
2743 * a connection with that osd (from the fault callback).
2744 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002745 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2746 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2747 bool found = false;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002748
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002749 for (p = rb_first(&osd->o_requests); p; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002750 struct ceph_osd_request *req =
2751 rb_entry(p, struct ceph_osd_request, r_node);
2752
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002753 p = rb_next(p); /* abort_request() */
2754
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002755 if (time_before(req->r_stamp, cutoff)) {
2756 dout(" req %p tid %llu on osd%d is laggy\n",
2757 req, req->r_tid, osd->o_osd);
2758 found = true;
2759 }
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002760 if (opts->osd_request_timeout &&
2761 time_before(req->r_start_stamp, expiry_cutoff)) {
2762 pr_err_ratelimited("tid %llu on osd%d timeout\n",
2763 req->r_tid, osd->o_osd);
2764 abort_request(req, -ETIMEDOUT);
2765 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002766 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02002767 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
2768 struct ceph_osd_linger_request *lreq =
2769 rb_entry(p, struct ceph_osd_linger_request, node);
2770
2771 dout(" lreq %p linger_id %llu is served by osd%d\n",
2772 lreq, lreq->linger_id, osd->o_osd);
2773 found = true;
2774
2775 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002776 if (lreq->is_watch && lreq->committed && !lreq->last_error)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002777 send_linger_ping(lreq);
2778 mutex_unlock(&lreq->lock);
2779 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002780
2781 if (found)
2782 list_move_tail(&osd->o_keepalive_item, &slow_osds);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002783 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002784
Ilya Dryomov7cc5e382017-02-12 17:11:07 +01002785 if (opts->osd_request_timeout) {
2786 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
2787 struct ceph_osd_request *req =
2788 rb_entry(p, struct ceph_osd_request, r_node);
2789
2790 p = rb_next(p); /* abort_request() */
2791
2792 if (time_before(req->r_start_stamp, expiry_cutoff)) {
2793 pr_err_ratelimited("tid %llu on osd%d timeout\n",
2794 req->r_tid, osdc->homeless_osd.o_osd);
2795 abort_request(req, -ETIMEDOUT);
2796 }
2797 }
2798 }
2799
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002800 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
2801 maybe_request_map(osdc);
2802
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002803 while (!list_empty(&slow_osds)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002804 struct ceph_osd *osd = list_first_entry(&slow_osds,
2805 struct ceph_osd,
2806 o_keepalive_item);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002807 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07002808 ceph_con_keepalive(&osd->o_con);
2809 }
2810
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002811 up_write(&osdc->lock);
Ilya Dryomovfbca9632016-04-28 16:07:24 +02002812 schedule_delayed_work(&osdc->timeout_work,
2813 osdc->client->options->osd_keepalive_timeout);
Sage Weilf24e9982009-10-06 11:31:10 -07002814}
2815
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002816static void handle_osds_timeout(struct work_struct *work)
2817{
2818 struct ceph_osd_client *osdc =
2819 container_of(work, struct ceph_osd_client,
2820 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03002821 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002822 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002823
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002824 dout("%s osdc %p\n", __func__, osdc);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002825 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002826 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
2827 if (time_before(jiffies, osd->lru_ttl))
2828 break;
2829
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002830 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002831 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002832 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002833 }
2834
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002835 up_write(&osdc->lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002836 schedule_delayed_work(&osdc->osds_timeout_work,
2837 round_jiffies_relative(delay));
2838}
2839
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002840static int ceph_oloc_decode(void **p, void *end,
2841 struct ceph_object_locator *oloc)
2842{
2843 u8 struct_v, struct_cv;
2844 u32 len;
2845 void *struct_end;
2846 int ret = 0;
2847
2848 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2849 struct_v = ceph_decode_8(p);
2850 struct_cv = ceph_decode_8(p);
2851 if (struct_v < 3) {
2852 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
2853 struct_v, struct_cv);
2854 goto e_inval;
2855 }
2856 if (struct_cv > 6) {
2857 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
2858 struct_v, struct_cv);
2859 goto e_inval;
2860 }
2861 len = ceph_decode_32(p);
2862 ceph_decode_need(p, end, len, e_inval);
2863 struct_end = *p + len;
2864
2865 oloc->pool = ceph_decode_64(p);
2866 *p += 4; /* skip preferred */
2867
2868 len = ceph_decode_32(p);
2869 if (len > 0) {
2870 pr_warn("ceph_object_locator::key is set\n");
2871 goto e_inval;
2872 }
2873
2874 if (struct_v >= 5) {
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002875 bool changed = false;
2876
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002877 len = ceph_decode_32(p);
2878 if (len > 0) {
Yan, Zheng30c156d2016-02-14 11:24:31 +08002879 ceph_decode_need(p, end, len, e_inval);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002880 if (!oloc->pool_ns ||
2881 ceph_compare_string(oloc->pool_ns, *p, len))
2882 changed = true;
Yan, Zheng30c156d2016-02-14 11:24:31 +08002883 *p += len;
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002884 } else {
2885 if (oloc->pool_ns)
2886 changed = true;
2887 }
2888 if (changed) {
2889 /* redirect changes namespace */
2890 pr_warn("ceph_object_locator::nspace is changed\n");
2891 goto e_inval;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002892 }
2893 }
2894
2895 if (struct_v >= 6) {
2896 s64 hash = ceph_decode_64(p);
2897 if (hash != -1) {
2898 pr_warn("ceph_object_locator::hash is set\n");
2899 goto e_inval;
2900 }
2901 }
2902
2903 /* skip the rest */
2904 *p = struct_end;
2905out:
2906 return ret;
2907
2908e_inval:
2909 ret = -EINVAL;
2910 goto out;
2911}
2912
2913static int ceph_redirect_decode(void **p, void *end,
2914 struct ceph_request_redirect *redir)
2915{
2916 u8 struct_v, struct_cv;
2917 u32 len;
2918 void *struct_end;
2919 int ret;
2920
2921 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2922 struct_v = ceph_decode_8(p);
2923 struct_cv = ceph_decode_8(p);
2924 if (struct_cv > 1) {
2925 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
2926 struct_v, struct_cv);
2927 goto e_inval;
2928 }
2929 len = ceph_decode_32(p);
2930 ceph_decode_need(p, end, len, e_inval);
2931 struct_end = *p + len;
2932
2933 ret = ceph_oloc_decode(p, end, &redir->oloc);
2934 if (ret)
2935 goto out;
2936
2937 len = ceph_decode_32(p);
2938 if (len > 0) {
2939 pr_warn("ceph_request_redirect::object_name is set\n");
2940 goto e_inval;
2941 }
2942
2943 len = ceph_decode_32(p);
2944 *p += len; /* skip osd_instructions */
2945
2946 /* skip the rest */
2947 *p = struct_end;
2948out:
2949 return ret;
2950
2951e_inval:
2952 ret = -EINVAL;
2953 goto out;
2954}
2955
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002956struct MOSDOpReply {
2957 struct ceph_pg pgid;
2958 u64 flags;
2959 int result;
2960 u32 epoch;
2961 int num_ops;
2962 u32 outdata_len[CEPH_OSD_MAX_OPS];
2963 s32 rval[CEPH_OSD_MAX_OPS];
2964 int retry_attempt;
2965 struct ceph_eversion replay_version;
2966 u64 user_version;
2967 struct ceph_request_redirect redirect;
2968};
Sage Weil25845472011-06-03 09:37:09 -07002969
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002970static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
Sage Weilf24e9982009-10-06 11:31:10 -07002971{
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002972 void *p = msg->front.iov_base;
2973 void *const end = p + msg->front.iov_len;
2974 u16 version = le16_to_cpu(msg->hdr.version);
2975 struct ceph_eversion bad_replay_version;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01002976 u8 decode_redir;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002977 u32 len;
2978 int ret;
2979 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07002980
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002981 ceph_decode_32_safe(&p, end, len, e_inval);
2982 ceph_decode_need(&p, end, len, e_inval);
2983 p += len; /* skip oid */
Sage Weil1b83bef2013-02-25 16:11:12 -08002984
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002985 ret = ceph_decode_pgid(&p, end, &m->pgid);
2986 if (ret)
2987 return ret;
Sage Weil1b83bef2013-02-25 16:11:12 -08002988
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002989 ceph_decode_64_safe(&p, end, m->flags, e_inval);
2990 ceph_decode_32_safe(&p, end, m->result, e_inval);
2991 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
2992 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
2993 p += sizeof(bad_replay_version);
2994 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
Sage Weil1b83bef2013-02-25 16:11:12 -08002995
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002996 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
2997 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
2998 goto e_inval;
Sage Weil1b83bef2013-02-25 16:11:12 -08002999
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003000 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3001 e_inval);
3002 for (i = 0; i < m->num_ops; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -08003003 struct ceph_osd_op *op = p;
Sage Weil1b83bef2013-02-25 16:11:12 -08003004
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003005 m->outdata_len[i] = le32_to_cpu(op->payload_len);
Sage Weil1b83bef2013-02-25 16:11:12 -08003006 p += sizeof(*op);
3007 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003008
3009 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3010 for (i = 0; i < m->num_ops; i++)
3011 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3012
3013 if (version >= 5) {
3014 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3015 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3016 p += sizeof(m->replay_version);
3017 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3018 } else {
3019 m->replay_version = bad_replay_version; /* struct */
3020 m->user_version = le64_to_cpu(m->replay_version.version);
Sage Weil1b83bef2013-02-25 16:11:12 -08003021 }
3022
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003023 if (version >= 6) {
3024 if (version >= 7)
3025 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01003026 else
3027 decode_redir = 1;
3028 } else {
3029 decode_redir = 0;
3030 }
3031
3032 if (decode_redir) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003033 ret = ceph_redirect_decode(&p, end, &m->redirect);
3034 if (ret)
3035 return ret;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003036 } else {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003037 ceph_oloc_init(&m->redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003038 }
3039
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003040 return 0;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003041
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003042e_inval:
3043 return -EINVAL;
3044}
3045
3046/*
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003047 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3048 * specified.
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003049 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003050static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003051{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003052 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003053 struct ceph_osd_request *req;
3054 struct MOSDOpReply m;
3055 u64 tid = le64_to_cpu(msg->hdr.tid);
3056 u32 data_len = 0;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003057 int ret;
3058 int i;
3059
3060 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3061
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003062 down_read(&osdc->lock);
3063 if (!osd_registered(osd)) {
3064 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3065 goto out_unlock_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003066 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003067 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3068
3069 mutex_lock(&osd->lock);
3070 req = lookup_request(&osd->o_requests, tid);
3071 if (!req) {
3072 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3073 goto out_unlock_session;
3074 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003075
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003076 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003077 ret = decode_MOSDOpReply(msg, &m);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08003078 m.redirect.oloc.pool_ns = NULL;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003079 if (ret) {
3080 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3081 req->r_tid, ret);
3082 ceph_msg_dump(msg);
3083 goto fail_request;
3084 }
3085 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3086 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3087 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3088 le64_to_cpu(m.replay_version.version), m.user_version);
3089
3090 if (m.retry_attempt >= 0) {
3091 if (m.retry_attempt != req->r_attempts - 1) {
3092 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3093 req, req->r_tid, m.retry_attempt,
3094 req->r_attempts - 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003095 goto out_unlock_session;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003096 }
3097 } else {
3098 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3099 }
3100
3101 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3102 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3103 m.redirect.oloc.pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003104 unlink_request(osd, req);
3105 mutex_unlock(&osd->lock);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003106
Yan, Zheng30c156d2016-02-14 11:24:31 +08003107 /*
3108 * Not ceph_oloc_copy() - changing pool_ns is not
3109 * supported.
3110 */
3111 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003112 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3113 req->r_tid = 0;
3114 __submit_request(req, false);
3115 goto out_unlock_osdc;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02003116 }
3117
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003118 if (m.num_ops != req->r_num_ops) {
3119 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3120 req->r_num_ops, req->r_tid);
3121 goto fail_request;
3122 }
3123 for (i = 0; i < req->r_num_ops; i++) {
3124 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3125 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3126 req->r_ops[i].rval = m.rval[i];
3127 req->r_ops[i].outdata_len = m.outdata_len[i];
3128 data_len += m.outdata_len[i];
3129 }
3130 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3131 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3132 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3133 goto fail_request;
3134 }
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003135 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3136 req, req->r_tid, m.result, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07003137
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003138 /*
3139 * Since we only ever request ONDISK, we should only ever get
3140 * one (type of) reply back.
3141 */
3142 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3143 req->r_result = m.result ?: data_len;
3144 finish_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003145 mutex_unlock(&osd->lock);
3146 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003147
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003148 __complete_request(req);
3149 complete_all(&req->r_completion);
3150 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07003151 return;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02003152
3153fail_request:
Ilya Dryomov46092452016-04-28 16:07:27 +02003154 complete_request(req, -EIO);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003155out_unlock_session:
3156 mutex_unlock(&osd->lock);
3157out_unlock_osdc:
3158 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003159}
3160
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003161static void set_pool_was_full(struct ceph_osd_client *osdc)
3162{
3163 struct rb_node *n;
3164
3165 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3166 struct ceph_pg_pool_info *pi =
3167 rb_entry(n, struct ceph_pg_pool_info, node);
3168
3169 pi->was_full = __pool_full(pi);
3170 }
3171}
3172
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003173static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
Sage Weilf24e9982009-10-06 11:31:10 -07003174{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003175 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07003176
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003177 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3178 if (!pi)
3179 return false;
Sage Weilf24e9982009-10-06 11:31:10 -07003180
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003181 return pi->was_full && !__pool_full(pi);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003182}
3183
Ilya Dryomov922dab62016-05-26 01:15:02 +02003184static enum calc_target_result
3185recalc_linger_target(struct ceph_osd_linger_request *lreq)
3186{
3187 struct ceph_osd_client *osdc = lreq->osdc;
3188 enum calc_target_result ct_res;
3189
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003190 ct_res = calc_target(osdc, &lreq->t, NULL, true);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003191 if (ct_res == CALC_TARGET_NEED_RESEND) {
3192 struct ceph_osd *osd;
3193
3194 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3195 if (osd != lreq->osd) {
3196 unlink_linger(lreq->osd, lreq);
3197 link_linger(osd, lreq);
3198 }
3199 }
3200
3201 return ct_res;
3202}
3203
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003204/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003205 * Requeue requests whose mapping to an OSD has changed.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003206 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003207static void scan_requests(struct ceph_osd *osd,
3208 bool force_resend,
3209 bool cleared_full,
3210 bool check_pool_cleared_full,
3211 struct rb_root *need_resend,
3212 struct list_head *need_resend_linger)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003213{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003214 struct ceph_osd_client *osdc = osd->o_osdc;
3215 struct rb_node *n;
3216 bool force_resend_writes;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003217
Ilya Dryomov922dab62016-05-26 01:15:02 +02003218 for (n = rb_first(&osd->o_linger_requests); n; ) {
3219 struct ceph_osd_linger_request *lreq =
3220 rb_entry(n, struct ceph_osd_linger_request, node);
3221 enum calc_target_result ct_res;
3222
3223 n = rb_next(n); /* recalc_linger_target() */
3224
3225 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3226 lreq->linger_id);
3227 ct_res = recalc_linger_target(lreq);
3228 switch (ct_res) {
3229 case CALC_TARGET_NO_ACTION:
3230 force_resend_writes = cleared_full ||
3231 (check_pool_cleared_full &&
3232 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3233 if (!force_resend && !force_resend_writes)
3234 break;
3235
3236 /* fall through */
3237 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003238 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003239 /*
3240 * scan_requests() for the previous epoch(s)
3241 * may have already added it to the list, since
3242 * it's not unlinked here.
3243 */
3244 if (list_empty(&lreq->scan_item))
3245 list_add_tail(&lreq->scan_item, need_resend_linger);
3246 break;
3247 case CALC_TARGET_POOL_DNE:
Ilya Dryomova10bcb12017-06-15 16:30:55 +02003248 list_del_init(&lreq->scan_item);
Ilya Dryomov46092452016-04-28 16:07:27 +02003249 check_linger_pool_dne(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003250 break;
3251 }
3252 }
3253
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003254 for (n = rb_first(&osd->o_requests); n; ) {
3255 struct ceph_osd_request *req =
3256 rb_entry(n, struct ceph_osd_request, r_node);
3257 enum calc_target_result ct_res;
Alex Elderab60b162012-12-19 15:52:36 -06003258
Ilya Dryomov46092452016-04-28 16:07:27 +02003259 n = rb_next(n); /* unlink_request(), check_pool_dne() */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003260
3261 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Ilya Dryomov7de030d2017-06-15 16:30:54 +02003262 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3263 false);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003264 switch (ct_res) {
3265 case CALC_TARGET_NO_ACTION:
3266 force_resend_writes = cleared_full ||
3267 (check_pool_cleared_full &&
3268 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3269 if (!force_resend &&
3270 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3271 !force_resend_writes))
3272 break;
3273
3274 /* fall through */
3275 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003276 cancel_map_check(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003277 unlink_request(osd, req);
3278 insert_request(need_resend, req);
3279 break;
3280 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003281 check_pool_dne(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003282 break;
Alex Elderab60b162012-12-19 15:52:36 -06003283 }
Sage Weilf24e9982009-10-06 11:31:10 -07003284 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003285}
Sage Weil6f6c7002011-01-17 20:34:08 -08003286
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003287static int handle_one_map(struct ceph_osd_client *osdc,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003288 void *p, void *end, bool incremental,
3289 struct rb_root *need_resend,
3290 struct list_head *need_resend_linger)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003291{
3292 struct ceph_osdmap *newmap;
3293 struct rb_node *n;
3294 bool skipped_map = false;
3295 bool was_full;
3296
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003297 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003298 set_pool_was_full(osdc);
3299
3300 if (incremental)
3301 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3302 else
3303 newmap = ceph_osdmap_decode(&p, end);
3304 if (IS_ERR(newmap))
3305 return PTR_ERR(newmap);
3306
3307 if (newmap != osdc->osdmap) {
3308 /*
3309 * Preserve ->was_full before destroying the old map.
3310 * For pools that weren't in the old map, ->was_full
3311 * should be false.
3312 */
3313 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3314 struct ceph_pg_pool_info *pi =
3315 rb_entry(n, struct ceph_pg_pool_info, node);
3316 struct ceph_pg_pool_info *old_pi;
3317
3318 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3319 if (old_pi)
3320 pi->was_full = old_pi->was_full;
3321 else
3322 WARN_ON(pi->was_full);
3323 }
3324
3325 if (osdc->osdmap->epoch &&
3326 osdc->osdmap->epoch + 1 < newmap->epoch) {
3327 WARN_ON(incremental);
3328 skipped_map = true;
3329 }
3330
3331 ceph_osdmap_destroy(osdc->osdmap);
3332 osdc->osdmap = newmap;
3333 }
3334
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003335 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003336 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3337 need_resend, need_resend_linger);
3338
3339 for (n = rb_first(&osdc->osds); n; ) {
3340 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3341
3342 n = rb_next(n); /* close_osd() */
3343
3344 scan_requests(osd, skipped_map, was_full, true, need_resend,
3345 need_resend_linger);
3346 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3347 memcmp(&osd->o_con.peer_addr,
3348 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3349 sizeof(struct ceph_entity_addr)))
3350 close_osd(osd);
3351 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003352
3353 return 0;
3354}
Sage Weil6f6c7002011-01-17 20:34:08 -08003355
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003356static void kick_requests(struct ceph_osd_client *osdc,
3357 struct rb_root *need_resend,
3358 struct list_head *need_resend_linger)
3359{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003360 struct ceph_osd_linger_request *lreq, *nlreq;
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003361 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003362 struct rb_node *n;
3363
Ilya Dryomov04c7d782017-06-15 16:30:55 +02003364 /* make sure need_resend targets reflect latest map */
3365 for (n = rb_first(need_resend); n; ) {
3366 struct ceph_osd_request *req =
3367 rb_entry(n, struct ceph_osd_request, r_node);
3368
3369 n = rb_next(n);
3370
3371 if (req->r_t.epoch < osdc->osdmap->epoch) {
3372 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3373 if (ct_res == CALC_TARGET_POOL_DNE) {
3374 erase_request(need_resend, req);
3375 check_pool_dne(req);
3376 }
3377 }
3378 }
3379
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003380 for (n = rb_first(need_resend); n; ) {
3381 struct ceph_osd_request *req =
3382 rb_entry(n, struct ceph_osd_request, r_node);
3383 struct ceph_osd *osd;
3384
3385 n = rb_next(n);
3386 erase_request(need_resend, req); /* before link_request() */
3387
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003388 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3389 link_request(osd, req);
3390 if (!req->r_linger) {
3391 if (!osd_homeless(osd) && !req->r_t.paused)
3392 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003393 } else {
3394 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003395 }
3396 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003397
3398 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3399 if (!osd_homeless(lreq->osd))
3400 send_linger(lreq);
3401
3402 list_del_init(&lreq->scan_item);
3403 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003404}
3405
Sage Weilf24e9982009-10-06 11:31:10 -07003406/*
3407 * Process updated osd map.
3408 *
3409 * The message contains any number of incremental and full maps, normally
3410 * indicating some sort of topology change in the cluster. Kick requests
3411 * off to different OSDs as needed.
3412 */
3413void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3414{
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003415 void *p = msg->front.iov_base;
3416 void *const end = p + msg->front.iov_len;
Sage Weilf24e9982009-10-06 11:31:10 -07003417 u32 nr_maps, maplen;
3418 u32 epoch;
Sage Weilf24e9982009-10-06 11:31:10 -07003419 struct ceph_fsid fsid;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003420 struct rb_root need_resend = RB_ROOT;
3421 LIST_HEAD(need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003422 bool handled_incremental = false;
3423 bool was_pauserd, was_pausewr;
3424 bool pauserd, pausewr;
3425 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07003426
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003427 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003428 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003429
3430 /* verify fsid */
3431 ceph_decode_need(&p, end, sizeof(fsid), bad);
3432 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08003433 if (ceph_check_fsid(osdc->client, &fsid) < 0)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003434 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003435
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003436 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3437 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3438 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003439 have_pool_full(osdc);
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08003440
Sage Weilf24e9982009-10-06 11:31:10 -07003441 /* incremental maps */
3442 ceph_decode_32_safe(&p, end, nr_maps, bad);
3443 dout(" %d inc maps\n", nr_maps);
3444 while (nr_maps > 0) {
3445 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003446 epoch = ceph_decode_32(&p);
3447 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003448 ceph_decode_need(&p, end, maplen, bad);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003449 if (osdc->osdmap->epoch &&
3450 osdc->osdmap->epoch + 1 == epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003451 dout("applying incremental map %u len %d\n",
3452 epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003453 err = handle_one_map(osdc, p, p + maplen, true,
3454 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003455 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003456 goto bad;
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003457 handled_incremental = true;
Sage Weilf24e9982009-10-06 11:31:10 -07003458 } else {
3459 dout("ignoring incremental map %u len %d\n",
3460 epoch, maplen);
3461 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003462 p += maplen;
Sage Weilf24e9982009-10-06 11:31:10 -07003463 nr_maps--;
3464 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003465 if (handled_incremental)
Sage Weilf24e9982009-10-06 11:31:10 -07003466 goto done;
3467
3468 /* full maps */
3469 ceph_decode_32_safe(&p, end, nr_maps, bad);
3470 dout(" %d full maps\n", nr_maps);
3471 while (nr_maps) {
3472 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003473 epoch = ceph_decode_32(&p);
3474 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003475 ceph_decode_need(&p, end, maplen, bad);
3476 if (nr_maps > 1) {
3477 dout("skipping non-latest full map %u len %d\n",
3478 epoch, maplen);
Ilya Dryomove5253a72016-04-28 16:07:25 +02003479 } else if (osdc->osdmap->epoch >= epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003480 dout("skipping full map %u len %d, "
3481 "older than our %u\n", epoch, maplen,
3482 osdc->osdmap->epoch);
3483 } else {
3484 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003485 err = handle_one_map(osdc, p, p + maplen, false,
3486 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003487 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003488 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003489 }
3490 p += maplen;
3491 nr_maps--;
3492 }
3493
3494done:
Sage Weilcd634fb2011-05-12 09:29:18 -07003495 /*
3496 * subscribe to subsequent osdmap updates if full to ensure
3497 * we find out when we are no longer full and stop returning
3498 * ENOSPC.
3499 */
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003500 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3501 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3502 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003503 have_pool_full(osdc);
Jeff Layton58eb7932017-04-18 09:21:16 -04003504 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3505 osdc->osdmap->epoch < osdc->epoch_barrier)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003506 maybe_request_map(osdc);
Sage Weilcd634fb2011-05-12 09:29:18 -07003507
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003508 kick_requests(osdc, &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003509
Jeff Laytonfc36d0a2017-04-04 08:39:39 -04003510 ceph_osdc_abort_on_full(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003511 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3512 osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003513 up_write(&osdc->lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07003514 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07003515 return;
3516
3517bad:
3518 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003519 ceph_msg_dump(msg);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003520 up_write(&osdc->lock);
3521}
3522
3523/*
3524 * Resubmit requests pending on the given osd.
3525 */
3526static void kick_osd_requests(struct ceph_osd *osd)
3527{
3528 struct rb_node *n;
3529
Ilya Dryomov922dab62016-05-26 01:15:02 +02003530 for (n = rb_first(&osd->o_requests); n; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003531 struct ceph_osd_request *req =
3532 rb_entry(n, struct ceph_osd_request, r_node);
3533
Ilya Dryomov922dab62016-05-26 01:15:02 +02003534 n = rb_next(n); /* cancel_linger_request() */
3535
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003536 if (!req->r_linger) {
3537 if (!req->r_t.paused)
3538 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003539 } else {
3540 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003541 }
3542 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003543 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3544 struct ceph_osd_linger_request *lreq =
3545 rb_entry(n, struct ceph_osd_linger_request, node);
3546
3547 send_linger(lreq);
3548 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003549}
3550
3551/*
3552 * If the osd connection drops, we need to resubmit all requests.
3553 */
3554static void osd_fault(struct ceph_connection *con)
3555{
3556 struct ceph_osd *osd = con->private;
3557 struct ceph_osd_client *osdc = osd->o_osdc;
3558
3559 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3560
3561 down_write(&osdc->lock);
3562 if (!osd_registered(osd)) {
3563 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3564 goto out_unlock;
3565 }
3566
3567 if (!reopen_osd(osd))
3568 kick_osd_requests(osd);
3569 maybe_request_map(osdc);
3570
3571out_unlock:
3572 up_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003573}
3574
Sage Weilf24e9982009-10-06 11:31:10 -07003575/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003576 * Process osd watch notifications
3577 */
Alex Elder3c663bb2013-02-15 11:42:30 -06003578static void handle_watch_notify(struct ceph_osd_client *osdc,
3579 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003580{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003581 void *p = msg->front.iov_base;
3582 void *const end = p + msg->front.iov_len;
3583 struct ceph_osd_linger_request *lreq;
3584 struct linger_work *lwork;
3585 u8 proto_ver, opcode;
3586 u64 cookie, notify_id;
3587 u64 notifier_id = 0;
Ilya Dryomov19079202016-04-28 16:07:27 +02003588 s32 return_code = 0;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003589 void *payload = NULL;
3590 u32 payload_len = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003591
3592 ceph_decode_8_safe(&p, end, proto_ver, bad);
3593 ceph_decode_8_safe(&p, end, opcode, bad);
3594 ceph_decode_64_safe(&p, end, cookie, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003595 p += 8; /* skip ver */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003596 ceph_decode_64_safe(&p, end, notify_id, bad);
3597
Ilya Dryomov922dab62016-05-26 01:15:02 +02003598 if (proto_ver >= 1) {
3599 ceph_decode_32_safe(&p, end, payload_len, bad);
3600 ceph_decode_need(&p, end, payload_len, bad);
3601 payload = p;
3602 p += payload_len;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003603 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003604
3605 if (le16_to_cpu(msg->hdr.version) >= 2)
Ilya Dryomov19079202016-04-28 16:07:27 +02003606 ceph_decode_32_safe(&p, end, return_code, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003607
3608 if (le16_to_cpu(msg->hdr.version) >= 3)
3609 ceph_decode_64_safe(&p, end, notifier_id, bad);
3610
3611 down_read(&osdc->lock);
3612 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
3613 if (!lreq) {
3614 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
3615 cookie);
3616 goto out_unlock_osdc;
3617 }
3618
3619 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02003620 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
3621 opcode, cookie, lreq, lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003622 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
3623 if (!lreq->last_error) {
3624 lreq->last_error = -ENOTCONN;
3625 queue_watch_error(lreq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003626 }
Ilya Dryomov19079202016-04-28 16:07:27 +02003627 } else if (!lreq->is_watch) {
3628 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
3629 if (lreq->notify_id && lreq->notify_id != notify_id) {
3630 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
3631 lreq->notify_id, notify_id);
3632 } else if (!completion_done(&lreq->notify_finish_wait)) {
3633 struct ceph_msg_data *data =
3634 list_first_entry_or_null(&msg->data,
3635 struct ceph_msg_data,
3636 links);
3637
3638 if (data) {
3639 if (lreq->preply_pages) {
3640 WARN_ON(data->type !=
3641 CEPH_MSG_DATA_PAGES);
3642 *lreq->preply_pages = data->pages;
3643 *lreq->preply_len = data->length;
3644 } else {
3645 ceph_release_page_vector(data->pages,
3646 calc_pages_for(0, data->length));
3647 }
3648 }
3649 lreq->notify_finish_error = return_code;
3650 complete_all(&lreq->notify_finish_wait);
3651 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003652 } else {
3653 /* CEPH_WATCH_EVENT_NOTIFY */
3654 lwork = lwork_alloc(lreq, do_watch_notify);
3655 if (!lwork) {
3656 pr_err("failed to allocate notify-lwork\n");
3657 goto out_unlock_lreq;
3658 }
Ilya Dryomov91883cd2014-09-11 12:18:53 +04003659
Ilya Dryomov922dab62016-05-26 01:15:02 +02003660 lwork->notify.notify_id = notify_id;
3661 lwork->notify.notifier_id = notifier_id;
3662 lwork->notify.payload = payload;
3663 lwork->notify.payload_len = payload_len;
3664 lwork->notify.msg = ceph_msg_get(msg);
3665 lwork_queue(lwork);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003666 }
3667
Ilya Dryomov922dab62016-05-26 01:15:02 +02003668out_unlock_lreq:
3669 mutex_unlock(&lreq->lock);
3670out_unlock_osdc:
3671 up_read(&osdc->lock);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003672 return;
3673
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003674bad:
3675 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003676}
3677
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003678/*
Sage Weilf24e9982009-10-06 11:31:10 -07003679 * Register request, send initial attempt.
3680 */
3681int ceph_osdc_start_request(struct ceph_osd_client *osdc,
3682 struct ceph_osd_request *req,
3683 bool nofail)
3684{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003685 down_read(&osdc->lock);
3686 submit_request(req, false);
3687 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003688
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003689 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07003690}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003691EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07003692
3693/*
Ilya Dryomovc297eb422016-12-02 14:01:55 +01003694 * Unregister a registered request. The request is not completed:
3695 * ->r_result isn't set and __complete_request() isn't called.
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04003696 */
3697void ceph_osdc_cancel_request(struct ceph_osd_request *req)
3698{
3699 struct ceph_osd_client *osdc = req->r_osdc;
3700
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003701 down_write(&osdc->lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003702 if (req->r_osd)
3703 cancel_request(req);
3704 up_write(&osdc->lock);
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04003705}
3706EXPORT_SYMBOL(ceph_osdc_cancel_request);
3707
3708/*
Ilya Dryomov42b06962016-04-28 16:07:26 +02003709 * @timeout: in jiffies, 0 means "wait forever"
3710 */
3711static int wait_request_timeout(struct ceph_osd_request *req,
3712 unsigned long timeout)
3713{
3714 long left;
3715
3716 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Yan, Zheng0e76abf2016-05-13 11:04:33 +08003717 left = wait_for_completion_killable_timeout(&req->r_completion,
Ilya Dryomov42b06962016-04-28 16:07:26 +02003718 ceph_timeout_jiffies(timeout));
3719 if (left <= 0) {
3720 left = left ?: -ETIMEDOUT;
3721 ceph_osdc_cancel_request(req);
Ilya Dryomov42b06962016-04-28 16:07:26 +02003722 } else {
3723 left = req->r_result; /* completed */
3724 }
3725
3726 return left;
3727}
3728
3729/*
Sage Weilf24e9982009-10-06 11:31:10 -07003730 * wait for a request to complete
3731 */
3732int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
3733 struct ceph_osd_request *req)
3734{
Ilya Dryomov42b06962016-04-28 16:07:26 +02003735 return wait_request_timeout(req, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07003736}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003737EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07003738
3739/*
3740 * sync - wait for all in-flight requests to flush. avoid starvation.
3741 */
3742void ceph_osdc_sync(struct ceph_osd_client *osdc)
3743{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003744 struct rb_node *n, *p;
3745 u64 last_tid = atomic64_read(&osdc->last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07003746
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003747again:
3748 down_read(&osdc->lock);
3749 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3750 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07003751
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003752 mutex_lock(&osd->lock);
3753 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
3754 struct ceph_osd_request *req =
3755 rb_entry(p, struct ceph_osd_request, r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07003756
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003757 if (req->r_tid > last_tid)
3758 break;
3759
3760 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
3761 continue;
3762
3763 ceph_osdc_get_request(req);
3764 mutex_unlock(&osd->lock);
3765 up_read(&osdc->lock);
3766 dout("%s waiting on req %p tid %llu last_tid %llu\n",
3767 __func__, req, req->r_tid, last_tid);
Ilya Dryomovb18b9552017-02-11 18:46:08 +01003768 wait_for_completion(&req->r_completion);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003769 ceph_osdc_put_request(req);
3770 goto again;
3771 }
3772
3773 mutex_unlock(&osd->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003774 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003775
3776 up_read(&osdc->lock);
3777 dout("%s done last_tid %llu\n", __func__, last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07003778}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003779EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07003780
Ilya Dryomov922dab62016-05-26 01:15:02 +02003781static struct ceph_osd_request *
3782alloc_linger_request(struct ceph_osd_linger_request *lreq)
3783{
3784 struct ceph_osd_request *req;
3785
3786 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
3787 if (!req)
3788 return NULL;
3789
3790 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3791 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3792
3793 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
3794 ceph_osdc_put_request(req);
3795 return NULL;
3796 }
3797
3798 return req;
3799}
3800
3801/*
3802 * Returns a handle, caller owns a ref.
3803 */
3804struct ceph_osd_linger_request *
3805ceph_osdc_watch(struct ceph_osd_client *osdc,
3806 struct ceph_object_id *oid,
3807 struct ceph_object_locator *oloc,
3808 rados_watchcb2_t wcb,
3809 rados_watcherrcb_t errcb,
3810 void *data)
3811{
3812 struct ceph_osd_linger_request *lreq;
3813 int ret;
3814
3815 lreq = linger_alloc(osdc);
3816 if (!lreq)
3817 return ERR_PTR(-ENOMEM);
3818
Ilya Dryomov19079202016-04-28 16:07:27 +02003819 lreq->is_watch = true;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003820 lreq->wcb = wcb;
3821 lreq->errcb = errcb;
3822 lreq->data = data;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02003823 lreq->watch_valid_thru = jiffies;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003824
3825 ceph_oid_copy(&lreq->t.base_oid, oid);
3826 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01003827 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07003828 ktime_get_real_ts(&lreq->mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003829
3830 lreq->reg_req = alloc_linger_request(lreq);
3831 if (!lreq->reg_req) {
3832 ret = -ENOMEM;
3833 goto err_put_lreq;
3834 }
3835
3836 lreq->ping_req = alloc_linger_request(lreq);
3837 if (!lreq->ping_req) {
3838 ret = -ENOMEM;
3839 goto err_put_lreq;
3840 }
3841
3842 down_write(&osdc->lock);
3843 linger_register(lreq); /* before osd_req_op_* */
3844 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
3845 CEPH_OSD_WATCH_OP_WATCH);
3846 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
3847 CEPH_OSD_WATCH_OP_PING);
3848 linger_submit(lreq);
3849 up_write(&osdc->lock);
3850
3851 ret = linger_reg_commit_wait(lreq);
3852 if (ret) {
3853 linger_cancel(lreq);
3854 goto err_put_lreq;
3855 }
3856
3857 return lreq;
3858
3859err_put_lreq:
3860 linger_put(lreq);
3861 return ERR_PTR(ret);
3862}
3863EXPORT_SYMBOL(ceph_osdc_watch);
3864
3865/*
3866 * Releases a ref.
3867 *
3868 * Times out after mount_timeout to preserve rbd unmap behaviour
3869 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
3870 * with mount_timeout").
3871 */
3872int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
3873 struct ceph_osd_linger_request *lreq)
3874{
3875 struct ceph_options *opts = osdc->client->options;
3876 struct ceph_osd_request *req;
3877 int ret;
3878
3879 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3880 if (!req)
3881 return -ENOMEM;
3882
3883 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3884 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
Ilya Dryomov54ea0042017-02-11 18:48:41 +01003885 req->r_flags = CEPH_OSD_FLAG_WRITE;
Deepa Dinamani1134e092017-05-08 15:59:19 -07003886 ktime_get_real_ts(&req->r_mtime);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003887 osd_req_op_watch_init(req, 0, lreq->linger_id,
3888 CEPH_OSD_WATCH_OP_UNWATCH);
3889
3890 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3891 if (ret)
3892 goto out_put_req;
3893
3894 ceph_osdc_start_request(osdc, req, false);
3895 linger_cancel(lreq);
3896 linger_put(lreq);
3897 ret = wait_request_timeout(req, opts->mount_timeout);
3898
3899out_put_req:
3900 ceph_osdc_put_request(req);
3901 return ret;
3902}
3903EXPORT_SYMBOL(ceph_osdc_unwatch);
3904
3905static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
3906 u64 notify_id, u64 cookie, void *payload,
3907 size_t payload_len)
3908{
3909 struct ceph_osd_req_op *op;
3910 struct ceph_pagelist *pl;
3911 int ret;
3912
3913 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
3914
3915 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3916 if (!pl)
3917 return -ENOMEM;
3918
3919 ceph_pagelist_init(pl);
3920 ret = ceph_pagelist_encode_64(pl, notify_id);
3921 ret |= ceph_pagelist_encode_64(pl, cookie);
3922 if (payload) {
3923 ret |= ceph_pagelist_encode_32(pl, payload_len);
3924 ret |= ceph_pagelist_append(pl, payload, payload_len);
3925 } else {
3926 ret |= ceph_pagelist_encode_32(pl, 0);
3927 }
3928 if (ret) {
3929 ceph_pagelist_release(pl);
3930 return -ENOMEM;
3931 }
3932
3933 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
3934 op->indata_len = pl->length;
3935 return 0;
3936}
3937
3938int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
3939 struct ceph_object_id *oid,
3940 struct ceph_object_locator *oloc,
3941 u64 notify_id,
3942 u64 cookie,
3943 void *payload,
3944 size_t payload_len)
3945{
3946 struct ceph_osd_request *req;
3947 int ret;
3948
3949 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3950 if (!req)
3951 return -ENOMEM;
3952
3953 ceph_oid_copy(&req->r_base_oid, oid);
3954 ceph_oloc_copy(&req->r_base_oloc, oloc);
3955 req->r_flags = CEPH_OSD_FLAG_READ;
3956
3957 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3958 if (ret)
3959 goto out_put_req;
3960
3961 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
3962 payload_len);
3963 if (ret)
3964 goto out_put_req;
3965
3966 ceph_osdc_start_request(osdc, req, false);
3967 ret = ceph_osdc_wait_request(osdc, req);
3968
3969out_put_req:
3970 ceph_osdc_put_request(req);
3971 return ret;
3972}
3973EXPORT_SYMBOL(ceph_osdc_notify_ack);
3974
Ilya Dryomov19079202016-04-28 16:07:27 +02003975static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
3976 u64 cookie, u32 prot_ver, u32 timeout,
3977 void *payload, size_t payload_len)
3978{
3979 struct ceph_osd_req_op *op;
3980 struct ceph_pagelist *pl;
3981 int ret;
3982
3983 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
3984 op->notify.cookie = cookie;
3985
3986 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3987 if (!pl)
3988 return -ENOMEM;
3989
3990 ceph_pagelist_init(pl);
3991 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
3992 ret |= ceph_pagelist_encode_32(pl, timeout);
3993 ret |= ceph_pagelist_encode_32(pl, payload_len);
3994 ret |= ceph_pagelist_append(pl, payload, payload_len);
3995 if (ret) {
3996 ceph_pagelist_release(pl);
3997 return -ENOMEM;
3998 }
3999
4000 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4001 op->indata_len = pl->length;
4002 return 0;
4003}
4004
4005/*
4006 * @timeout: in seconds
4007 *
4008 * @preply_{pages,len} are initialized both on success and error.
4009 * The caller is responsible for:
4010 *
4011 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4012 */
4013int ceph_osdc_notify(struct ceph_osd_client *osdc,
4014 struct ceph_object_id *oid,
4015 struct ceph_object_locator *oloc,
4016 void *payload,
4017 size_t payload_len,
4018 u32 timeout,
4019 struct page ***preply_pages,
4020 size_t *preply_len)
4021{
4022 struct ceph_osd_linger_request *lreq;
4023 struct page **pages;
4024 int ret;
4025
4026 WARN_ON(!timeout);
4027 if (preply_pages) {
4028 *preply_pages = NULL;
4029 *preply_len = 0;
4030 }
4031
4032 lreq = linger_alloc(osdc);
4033 if (!lreq)
4034 return -ENOMEM;
4035
4036 lreq->preply_pages = preply_pages;
4037 lreq->preply_len = preply_len;
4038
4039 ceph_oid_copy(&lreq->t.base_oid, oid);
4040 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4041 lreq->t.flags = CEPH_OSD_FLAG_READ;
4042
4043 lreq->reg_req = alloc_linger_request(lreq);
4044 if (!lreq->reg_req) {
4045 ret = -ENOMEM;
4046 goto out_put_lreq;
4047 }
4048
4049 /* for notify_id */
4050 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4051 if (IS_ERR(pages)) {
4052 ret = PTR_ERR(pages);
4053 goto out_put_lreq;
4054 }
4055
4056 down_write(&osdc->lock);
4057 linger_register(lreq); /* before osd_req_op_* */
4058 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4059 timeout, payload, payload_len);
4060 if (ret) {
4061 linger_unregister(lreq);
4062 up_write(&osdc->lock);
4063 ceph_release_page_vector(pages, 1);
4064 goto out_put_lreq;
4065 }
4066 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4067 response_data),
4068 pages, PAGE_SIZE, 0, false, true);
4069 linger_submit(lreq);
4070 up_write(&osdc->lock);
4071
4072 ret = linger_reg_commit_wait(lreq);
4073 if (!ret)
4074 ret = linger_notify_finish_wait(lreq);
4075 else
4076 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4077
4078 linger_cancel(lreq);
4079out_put_lreq:
4080 linger_put(lreq);
4081 return ret;
4082}
4083EXPORT_SYMBOL(ceph_osdc_notify);
4084
Sage Weilf24e9982009-10-06 11:31:10 -07004085/*
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004086 * Return the number of milliseconds since the watch was last
4087 * confirmed, or an error. If there is an error, the watch is no
4088 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4089 */
4090int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4091 struct ceph_osd_linger_request *lreq)
4092{
4093 unsigned long stamp, age;
4094 int ret;
4095
4096 down_read(&osdc->lock);
4097 mutex_lock(&lreq->lock);
4098 stamp = lreq->watch_valid_thru;
4099 if (!list_empty(&lreq->pending_lworks)) {
4100 struct linger_work *lwork =
4101 list_first_entry(&lreq->pending_lworks,
4102 struct linger_work,
4103 pending_item);
4104
4105 if (time_before(lwork->queued_stamp, stamp))
4106 stamp = lwork->queued_stamp;
4107 }
4108 age = jiffies - stamp;
4109 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4110 lreq, lreq->linger_id, age, lreq->last_error);
4111 /* we are truncating to msecs, so return a safe upper bound */
4112 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4113
4114 mutex_unlock(&lreq->lock);
4115 up_read(&osdc->lock);
4116 return ret;
4117}
4118
Douglas Fullera4ed38d2015-07-17 13:18:07 -07004119static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4120{
4121 u8 struct_v;
4122 u32 struct_len;
4123 int ret;
4124
4125 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4126 &struct_v, &struct_len);
4127 if (ret)
4128 return ret;
4129
4130 ceph_decode_copy(p, &item->name, sizeof(item->name));
4131 item->cookie = ceph_decode_64(p);
4132 *p += 4; /* skip timeout_seconds */
4133 if (struct_v >= 2) {
4134 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4135 ceph_decode_addr(&item->addr);
4136 }
4137
4138 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4139 ENTITY_NAME(item->name), item->cookie,
4140 ceph_pr_addr(&item->addr.in_addr));
4141 return 0;
4142}
4143
4144static int decode_watchers(void **p, void *end,
4145 struct ceph_watch_item **watchers,
4146 u32 *num_watchers)
4147{
4148 u8 struct_v;
4149 u32 struct_len;
4150 int i;
4151 int ret;
4152
4153 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4154 &struct_v, &struct_len);
4155 if (ret)
4156 return ret;
4157
4158 *num_watchers = ceph_decode_32(p);
4159 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4160 if (!*watchers)
4161 return -ENOMEM;
4162
4163 for (i = 0; i < *num_watchers; i++) {
4164 ret = decode_watcher(p, end, *watchers + i);
4165 if (ret) {
4166 kfree(*watchers);
4167 return ret;
4168 }
4169 }
4170
4171 return 0;
4172}
4173
4174/*
4175 * On success, the caller is responsible for:
4176 *
4177 * kfree(watchers);
4178 */
4179int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4180 struct ceph_object_id *oid,
4181 struct ceph_object_locator *oloc,
4182 struct ceph_watch_item **watchers,
4183 u32 *num_watchers)
4184{
4185 struct ceph_osd_request *req;
4186 struct page **pages;
4187 int ret;
4188
4189 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4190 if (!req)
4191 return -ENOMEM;
4192
4193 ceph_oid_copy(&req->r_base_oid, oid);
4194 ceph_oloc_copy(&req->r_base_oloc, oloc);
4195 req->r_flags = CEPH_OSD_FLAG_READ;
4196
4197 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4198 if (ret)
4199 goto out_put_req;
4200
4201 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4202 if (IS_ERR(pages)) {
4203 ret = PTR_ERR(pages);
4204 goto out_put_req;
4205 }
4206
4207 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4208 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4209 response_data),
4210 pages, PAGE_SIZE, 0, false, true);
4211
4212 ceph_osdc_start_request(osdc, req, false);
4213 ret = ceph_osdc_wait_request(osdc, req);
4214 if (ret >= 0) {
4215 void *p = page_address(pages[0]);
4216 void *const end = p + req->r_ops[0].outdata_len;
4217
4218 ret = decode_watchers(&p, end, watchers, num_watchers);
4219 }
4220
4221out_put_req:
4222 ceph_osdc_put_request(req);
4223 return ret;
4224}
4225EXPORT_SYMBOL(ceph_osdc_list_watchers);
4226
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004227/*
Josh Durgindd935f42013-08-28 21:43:09 -07004228 * Call all pending notify callbacks - for use after a watch is
4229 * unregistered, to make sure no more callbacks for it will be invoked
4230 */
stephen hemmingerf64794492014-06-10 20:30:13 -07004231void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07004232{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004233 dout("%s osdc %p\n", __func__, osdc);
Josh Durgindd935f42013-08-28 21:43:09 -07004234 flush_workqueue(osdc->notify_wq);
4235}
4236EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4237
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02004238void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4239{
4240 down_read(&osdc->lock);
4241 maybe_request_map(osdc);
4242 up_read(&osdc->lock);
4243}
4244EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
Josh Durgindd935f42013-08-28 21:43:09 -07004245
4246/*
Douglas Fuller428a7152015-06-17 14:49:45 -04004247 * Execute an OSD class method on an object.
4248 *
4249 * @flags: CEPH_OSD_FLAG_*
Ilya Dryomov2544a022017-01-25 18:16:21 +01004250 * @resp_len: in/out param for reply length
Douglas Fuller428a7152015-06-17 14:49:45 -04004251 */
4252int ceph_osdc_call(struct ceph_osd_client *osdc,
4253 struct ceph_object_id *oid,
4254 struct ceph_object_locator *oloc,
4255 const char *class, const char *method,
4256 unsigned int flags,
4257 struct page *req_page, size_t req_len,
4258 struct page *resp_page, size_t *resp_len)
4259{
4260 struct ceph_osd_request *req;
4261 int ret;
4262
Ilya Dryomov2544a022017-01-25 18:16:21 +01004263 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4264 return -E2BIG;
4265
Douglas Fuller428a7152015-06-17 14:49:45 -04004266 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4267 if (!req)
4268 return -ENOMEM;
4269
4270 ceph_oid_copy(&req->r_base_oid, oid);
4271 ceph_oloc_copy(&req->r_base_oloc, oloc);
4272 req->r_flags = flags;
4273
4274 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4275 if (ret)
4276 goto out_put_req;
4277
4278 osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4279 if (req_page)
4280 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4281 0, false, false);
4282 if (resp_page)
4283 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
Ilya Dryomov2544a022017-01-25 18:16:21 +01004284 *resp_len, 0, false, false);
Douglas Fuller428a7152015-06-17 14:49:45 -04004285
4286 ceph_osdc_start_request(osdc, req, false);
4287 ret = ceph_osdc_wait_request(osdc, req);
4288 if (ret >= 0) {
4289 ret = req->r_ops[0].rval;
4290 if (resp_page)
4291 *resp_len = req->r_ops[0].outdata_len;
4292 }
4293
4294out_put_req:
4295 ceph_osdc_put_request(req);
4296 return ret;
4297}
4298EXPORT_SYMBOL(ceph_osdc_call);
4299
4300/*
Sage Weilf24e9982009-10-06 11:31:10 -07004301 * init, shutdown
4302 */
4303int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4304{
4305 int err;
4306
4307 dout("init\n");
4308 osdc->client = client;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004309 init_rwsem(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004310 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004311 INIT_LIST_HEAD(&osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02004312 spin_lock_init(&osdc->osd_lru_lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004313 osd_init(&osdc->homeless_osd);
4314 osdc->homeless_osd.o_osdc = osdc;
4315 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
Ilya Dryomov264048a2016-11-08 15:15:24 +01004316 osdc->last_linger_id = CEPH_LINGER_ID_START;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004317 osdc->linger_requests = RB_ROOT;
Ilya Dryomov46092452016-04-28 16:07:27 +02004318 osdc->map_checks = RB_ROOT;
4319 osdc->linger_map_checks = RB_ROOT;
Sage Weilf24e9982009-10-06 11:31:10 -07004320 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004321 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4322
Sage Weil5f44f142009-11-18 14:52:18 -08004323 err = -ENOMEM;
Ilya Dryomove5253a72016-04-28 16:07:25 +02004324 osdc->osdmap = ceph_osdmap_alloc();
4325 if (!osdc->osdmap)
4326 goto out;
4327
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01004328 osdc->req_mempool = mempool_create_slab_pool(10,
4329 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07004330 if (!osdc->req_mempool)
Ilya Dryomove5253a72016-04-28 16:07:25 +02004331 goto out_map;
Sage Weilf24e9982009-10-06 11:31:10 -07004332
Sage Weild50b4092012-07-09 14:22:34 -07004333 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02004334 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07004335 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08004336 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07004337 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02004338 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08004339 if (err < 0)
4340 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004341
Dan Carpenterdbcae082013-08-15 08:58:59 +03004342 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004343 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03004344 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02004345 goto out_msgpool_reply;
4346
Ilya Dryomovfbca9632016-04-28 16:07:24 +02004347 schedule_delayed_work(&osdc->timeout_work,
4348 osdc->client->options->osd_keepalive_timeout);
Ilya Dryomovb37ee1b2016-04-28 16:07:24 +02004349 schedule_delayed_work(&osdc->osds_timeout_work,
4350 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4351
Sage Weilf24e9982009-10-06 11:31:10 -07004352 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08004353
Ilya Dryomovc172ec52014-01-31 17:49:22 +02004354out_msgpool_reply:
4355 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08004356out_msgpool:
4357 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08004358out_mempool:
4359 mempool_destroy(osdc->req_mempool);
Ilya Dryomove5253a72016-04-28 16:07:25 +02004360out_map:
4361 ceph_osdmap_destroy(osdc->osdmap);
Sage Weil5f44f142009-11-18 14:52:18 -08004362out:
4363 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07004364}
4365
4366void ceph_osdc_stop(struct ceph_osd_client *osdc)
4367{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004368 flush_workqueue(osdc->notify_wq);
4369 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07004370 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004371 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004372
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004373 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004374 while (!RB_EMPTY_ROOT(&osdc->osds)) {
4375 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4376 struct ceph_osd, o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004377 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004378 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004379 up_write(&osdc->lock);
Elena Reshetova02113a02017-03-17 14:10:28 +02004380 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004381 osd_cleanup(&osdc->homeless_osd);
4382
4383 WARN_ON(!list_empty(&osdc->osd_lru));
Ilya Dryomov922dab62016-05-26 01:15:02 +02004384 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
Ilya Dryomov46092452016-04-28 16:07:27 +02004385 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4386 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004387 WARN_ON(atomic_read(&osdc->num_requests));
4388 WARN_ON(atomic_read(&osdc->num_homeless));
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004389
Ilya Dryomove5253a72016-04-28 16:07:25 +02004390 ceph_osdmap_destroy(osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07004391 mempool_destroy(osdc->req_mempool);
4392 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08004393 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07004394}
4395
4396/*
4397 * Read some contiguous pages. If we cross a stripe boundary, shorten
4398 * *plen. Return number of bytes read, or error.
4399 */
4400int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4401 struct ceph_vino vino, struct ceph_file_layout *layout,
4402 u64 off, u64 *plen,
4403 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08004404 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07004405{
4406 struct ceph_osd_request *req;
4407 int rc = 0;
4408
4409 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4410 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08004411 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07004412 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05004413 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06004414 false);
Sage Weil68162822012-09-24 21:01:02 -07004415 if (IS_ERR(req))
4416 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07004417
4418 /* it may be a short read due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05004419 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05004420 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07004421
Alex Eldere0c59482013-03-07 15:38:25 -06004422 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05004423 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07004424
4425 rc = ceph_osdc_start_request(osdc, req, false);
4426 if (!rc)
4427 rc = ceph_osdc_wait_request(osdc, req);
4428
4429 ceph_osdc_put_request(req);
4430 dout("readpages result %d\n", rc);
4431 return rc;
4432}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004433EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07004434
4435/*
4436 * do a synchronous write on N pages
4437 */
4438int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
4439 struct ceph_file_layout *layout,
4440 struct ceph_snap_context *snapc,
4441 u64 off, u64 len,
4442 u32 truncate_seq, u64 truncate_size,
4443 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06004444 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07004445{
4446 struct ceph_osd_request *req;
4447 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08004448 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07004449
Yan, Zheng715e4cd2014-11-13 14:40:37 +08004450 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01004451 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05004452 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06004453 true);
Sage Weil68162822012-09-24 21:01:02 -07004454 if (IS_ERR(req))
4455 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07004456
4457 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05004458 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05004459 false, false);
4460 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07004461
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02004462 req->r_mtime = *mtime;
Alex Elder87f979d2013-02-15 11:42:29 -06004463 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07004464 if (!rc)
4465 rc = ceph_osdc_wait_request(osdc, req);
4466
4467 ceph_osdc_put_request(req);
4468 if (rc == 0)
4469 rc = len;
4470 dout("writepages result %d\n", rc);
4471 return rc;
4472}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004473EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07004474
Alex Elder5522ae02013-05-01 12:43:04 -05004475int ceph_osdc_setup(void)
4476{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01004477 size_t size = sizeof(struct ceph_osd_request) +
4478 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
4479
Alex Elder5522ae02013-05-01 12:43:04 -05004480 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01004481 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
4482 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05004483
4484 return ceph_osd_request_cache ? 0 : -ENOMEM;
4485}
4486EXPORT_SYMBOL(ceph_osdc_setup);
4487
4488void ceph_osdc_cleanup(void)
4489{
4490 BUG_ON(!ceph_osd_request_cache);
4491 kmem_cache_destroy(ceph_osd_request_cache);
4492 ceph_osd_request_cache = NULL;
4493}
4494EXPORT_SYMBOL(ceph_osdc_cleanup);
4495
Sage Weilf24e9982009-10-06 11:31:10 -07004496/*
4497 * handle incoming message
4498 */
4499static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4500{
4501 struct ceph_osd *osd = con->private;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004502 struct ceph_osd_client *osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07004503 int type = le16_to_cpu(msg->hdr.type);
4504
Sage Weilf24e9982009-10-06 11:31:10 -07004505 switch (type) {
4506 case CEPH_MSG_OSD_MAP:
4507 ceph_osdc_handle_map(osdc, msg);
4508 break;
4509 case CEPH_MSG_OSD_OPREPLY:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004510 handle_reply(osd, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07004511 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004512 case CEPH_MSG_WATCH_NOTIFY:
4513 handle_watch_notify(osdc, msg);
4514 break;
Sage Weilf24e9982009-10-06 11:31:10 -07004515
4516 default:
4517 pr_err("received unknown message type %d %s\n", type,
4518 ceph_msg_type_name(type));
4519 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004520
Sage Weilf24e9982009-10-06 11:31:10 -07004521 ceph_msg_put(msg);
4522}
4523
Sage Weil5b3a4db2010-02-19 21:43:23 -08004524/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004525 * Lookup and return message for incoming reply. Don't try to do
4526 * anything about a larger than preallocated data portion of the
4527 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08004528 */
4529static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08004530 struct ceph_msg_header *hdr,
4531 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07004532{
4533 struct ceph_osd *osd = con->private;
4534 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004535 struct ceph_msg *m = NULL;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004536 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02004537 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004538 int data_len = le32_to_cpu(hdr->data_len);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004539 u64 tid = le64_to_cpu(hdr->tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004540
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004541 down_read(&osdc->lock);
4542 if (!osd_registered(osd)) {
4543 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
4544 *skip = 1;
4545 goto out_unlock_osdc;
4546 }
4547 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
4548
4549 mutex_lock(&osd->lock);
4550 req = lookup_request(&osd->o_requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004551 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01004552 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
4553 osd->o_osd, tid);
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004554 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004555 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004556 }
Sage Weilc16e7862010-03-01 13:02:00 -08004557
Alex Elderace6d3a2013-04-01 16:12:14 -05004558 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08004559
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02004560 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004561 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
4562 __func__, osd->o_osd, req->r_tid, front_len,
4563 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02004564 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
4565 false);
Sage Weila79832f2010-04-01 16:06:19 -07004566 if (!m)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004567 goto out_unlock_session;
Sage Weilc16e7862010-03-01 13:02:00 -08004568 ceph_msg_put(req->r_reply);
4569 req->r_reply = m;
4570 }
Sage Weilc16e7862010-03-01 13:02:00 -08004571
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004572 if (data_len > req->r_reply->data_length) {
4573 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
4574 __func__, osd->o_osd, req->r_tid, data_len,
4575 req->r_reply->data_length);
4576 m = NULL;
4577 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004578 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004579 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004580
4581 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08004582 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004583
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004584out_unlock_session:
4585 mutex_unlock(&osd->lock);
4586out_unlock_osdc:
4587 up_read(&osdc->lock);
Yehuda Sadeh24504182010-01-08 13:58:34 -08004588 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08004589}
4590
Ilya Dryomov19079202016-04-28 16:07:27 +02004591/*
4592 * TODO: switch to a msg-owned pagelist
4593 */
4594static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
4595{
4596 struct ceph_msg *m;
4597 int type = le16_to_cpu(hdr->type);
4598 u32 front_len = le32_to_cpu(hdr->front_len);
4599 u32 data_len = le32_to_cpu(hdr->data_len);
4600
4601 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
4602 if (!m)
4603 return NULL;
4604
4605 if (data_len) {
4606 struct page **pages;
4607 struct ceph_osd_data osd_data;
4608
4609 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
4610 GFP_NOIO);
Wei Yongjunc22e8532016-07-30 00:37:57 +00004611 if (IS_ERR(pages)) {
Ilya Dryomov19079202016-04-28 16:07:27 +02004612 ceph_msg_put(m);
4613 return NULL;
4614 }
4615
4616 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
4617 false);
4618 ceph_osdc_msg_data_add(m, &osd_data);
4619 }
4620
4621 return m;
4622}
4623
Sage Weil5b3a4db2010-02-19 21:43:23 -08004624static struct ceph_msg *alloc_msg(struct ceph_connection *con,
4625 struct ceph_msg_header *hdr,
4626 int *skip)
4627{
4628 struct ceph_osd *osd = con->private;
4629 int type = le16_to_cpu(hdr->type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004630
Alex Elder1c20f2d2012-06-04 14:43:32 -05004631 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08004632 switch (type) {
4633 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004634 case CEPH_MSG_WATCH_NOTIFY:
Ilya Dryomov19079202016-04-28 16:07:27 +02004635 return alloc_msg_with_page_vector(hdr);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004636 case CEPH_MSG_OSD_OPREPLY:
4637 return get_reply(con, hdr, skip);
4638 default:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004639 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
4640 osd->o_osd, type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004641 *skip = 1;
4642 return NULL;
4643 }
Sage Weilf24e9982009-10-06 11:31:10 -07004644}
4645
4646/*
4647 * Wrappers to refcount containing ceph_osd struct
4648 */
4649static struct ceph_connection *get_osd_con(struct ceph_connection *con)
4650{
4651 struct ceph_osd *osd = con->private;
4652 if (get_osd(osd))
4653 return con;
4654 return NULL;
4655}
4656
4657static void put_osd_con(struct ceph_connection *con)
4658{
4659 struct ceph_osd *osd = con->private;
4660 put_osd(osd);
4661}
4662
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004663/*
4664 * authentication
4665 */
Alex Eldera3530df2012-05-16 15:16:39 -05004666/*
4667 * Note: returned pointer is the address of a structure that's
4668 * managed separately. Caller must *not* attempt to free it.
4669 */
4670static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004671 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004672{
4673 struct ceph_osd *o = con->private;
4674 struct ceph_osd_client *osdc = o->o_osdc;
4675 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004676 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004677
Alex Elder74f18692012-05-16 15:16:39 -05004678 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004679 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004680 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004681 }
Sage Weil27859f92013-03-25 10:26:14 -07004682 if (!auth->authorizer) {
4683 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4684 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004685 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004686 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004687 } else {
4688 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07004689 auth);
4690 if (ret)
4691 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004692 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004693 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004694
Alex Eldera3530df2012-05-16 15:16:39 -05004695 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004696}
4697
4698
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004699static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004700{
4701 struct ceph_osd *o = con->private;
4702 struct ceph_osd_client *osdc = o->o_osdc;
4703 struct ceph_auth_client *ac = osdc->client->monc.auth;
4704
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004705 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004706}
4707
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004708static int invalidate_authorizer(struct ceph_connection *con)
4709{
4710 struct ceph_osd *o = con->private;
4711 struct ceph_osd_client *osdc = o->o_osdc;
4712 struct ceph_auth_client *ac = osdc->client->monc.auth;
4713
Sage Weil27859f92013-03-25 10:26:14 -07004714 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004715 return ceph_monc_validate_auth(&osdc->client->monc);
4716}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004717
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02004718static void osd_reencode_message(struct ceph_msg *msg)
4719{
4720 encode_request_finish(msg);
4721}
4722
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004723static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004724{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004725 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004726 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004727
Yan, Zheng33d07332014-11-04 16:33:37 +08004728 return ceph_auth_sign_message(auth, msg);
4729}
4730
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004731static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004732{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004733 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004734 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004735
Yan, Zheng33d07332014-11-04 16:33:37 +08004736 return ceph_auth_check_message_signature(auth, msg);
4737}
4738
Tobias Klauser9e327892010-05-20 10:40:19 +02004739static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07004740 .get = get_osd_con,
4741 .put = put_osd_con,
4742 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004743 .get_authorizer = get_authorizer,
4744 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004745 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07004746 .alloc_msg = alloc_msg,
Ilya Dryomov8cb441c2017-06-15 16:30:54 +02004747 .reencode_message = osd_reencode_message,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004748 .sign_message = osd_sign_message,
4749 .check_message_signature = osd_check_message_signature,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004750 .fault = osd_fault,
Sage Weilf24e9982009-10-06 11:31:10 -07004751};