Commit | Line | Data |
---|---|---|
a4ce40a9 | 1 | |
3d14c5d2 | 2 | #include <linux/ceph/ceph_debug.h> |
f24e9980 | 3 | |
3d14c5d2 | 4 | #include <linux/module.h> |
f24e9980 SW |
5 | #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> | |
68b4476b YS |
11 | #ifdef CONFIG_BLOCK |
12 | #include <linux/bio.h> | |
13 | #endif | |
f24e9980 | 14 | |
3d14c5d2 YS |
15 | #include <linux/ceph/libceph.h> |
16 | #include <linux/ceph/osd_client.h> | |
17 | #include <linux/ceph/messenger.h> | |
18 | #include <linux/ceph/decode.h> | |
19 | #include <linux/ceph/auth.h> | |
20 | #include <linux/ceph/pagelist.h> | |
f24e9980 | 21 | |
c16e7869 | 22 | #define OSD_OPREPLY_FRONT_LEN 512 |
0d59ab81 | 23 | |
5522ae0b AE |
24 | static struct kmem_cache *ceph_osd_request_cache; |
25 | ||
9e32789f | 26 | static const struct ceph_connection_operations osd_con_ops; |
f24e9980 | 27 | |
f9d25199 | 28 | static void __send_queued(struct ceph_osd_client *osdc); |
6f6c7006 | 29 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd); |
a40c4f10 YS |
30 | static void __register_request(struct ceph_osd_client *osdc, |
31 | struct ceph_osd_request *req); | |
2cc6128a ID |
32 | static void __unregister_request(struct ceph_osd_client *osdc, |
33 | struct ceph_osd_request *req); | |
a40c4f10 YS |
34 | static void __unregister_linger_request(struct ceph_osd_client *osdc, |
35 | struct ceph_osd_request *req); | |
2cc6128a | 36 | static void __enqueue_request(struct ceph_osd_request *req); |
56e925b6 SW |
37 | static void __send_request(struct ceph_osd_client *osdc, |
38 | struct ceph_osd_request *req); | |
f24e9980 SW |
39 | |
40 | /* | |
41 | * Implement client access to distributed object storage cluster. | |
42 | * | |
43 | * All data objects are stored within a cluster/cloud of OSDs, or | |
44 | * "object storage devices." (Note that Ceph OSDs have _nothing_ to | |
45 | * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply | |
46 | * remote daemons serving up and coordinating consistent and safe | |
47 | * access to storage. | |
48 | * | |
49 | * Cluster membership and the mapping of data objects onto storage devices | |
50 | * are described by the osd map. | |
51 | * | |
52 | * We keep track of pending OSD requests (read, write), resubmit | |
53 | * requests to different OSDs when the cluster topology/data layout | |
54 | * change, or retry the affected requests when the communications | |
55 | * channel with an OSD is reset. | |
56 | */ | |
57 | ||
58 | /* | |
59 | * calculate the mapping of a file extent onto an object, and fill out the | |
60 | * request accordingly. shorten extent as necessary if it crosses an | |
61 | * object boundary. | |
62 | * | |
63 | * fill osd op in request message. | |
64 | */ | |
dbe0fc41 | 65 | static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen, |
a19dadfb | 66 | u64 *objnum, u64 *objoff, u64 *objlen) |
f24e9980 | 67 | { |
60e56f13 | 68 | u64 orig_len = *plen; |
d63b77f4 | 69 | int r; |
f24e9980 | 70 | |
60e56f13 | 71 | /* object extent? */ |
75d1c941 AE |
72 | r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum, |
73 | objoff, objlen); | |
d63b77f4 SW |
74 | if (r < 0) |
75 | return r; | |
75d1c941 AE |
76 | if (*objlen < orig_len) { |
77 | *plen = *objlen; | |
60e56f13 AE |
78 | dout(" skipping last %llu, final file extent %llu~%llu\n", |
79 | orig_len - *plen, off, *plen); | |
80 | } | |
81 | ||
75d1c941 | 82 | dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen); |
f24e9980 | 83 | |
3ff5f385 | 84 | return 0; |
f24e9980 SW |
85 | } |
86 | ||
c54d47bf AE |
87 | static void ceph_osd_data_init(struct ceph_osd_data *osd_data) |
88 | { | |
89 | memset(osd_data, 0, sizeof (*osd_data)); | |
90 | osd_data->type = CEPH_OSD_DATA_TYPE_NONE; | |
91 | } | |
92 | ||
a4ce40a9 | 93 | static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data, |
43bfe5de AE |
94 | struct page **pages, u64 length, u32 alignment, |
95 | bool pages_from_pool, bool own_pages) | |
96 | { | |
97 | osd_data->type = CEPH_OSD_DATA_TYPE_PAGES; | |
98 | osd_data->pages = pages; | |
99 | osd_data->length = length; | |
100 | osd_data->alignment = alignment; | |
101 | osd_data->pages_from_pool = pages_from_pool; | |
102 | osd_data->own_pages = own_pages; | |
103 | } | |
43bfe5de | 104 | |
a4ce40a9 | 105 | static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data, |
43bfe5de AE |
106 | struct ceph_pagelist *pagelist) |
107 | { | |
108 | osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST; | |
109 | osd_data->pagelist = pagelist; | |
110 | } | |
43bfe5de AE |
111 | |
112 | #ifdef CONFIG_BLOCK | |
a4ce40a9 | 113 | static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data, |
43bfe5de AE |
114 | struct bio *bio, size_t bio_length) |
115 | { | |
116 | osd_data->type = CEPH_OSD_DATA_TYPE_BIO; | |
117 | osd_data->bio = bio; | |
118 | osd_data->bio_length = bio_length; | |
119 | } | |
43bfe5de AE |
120 | #endif /* CONFIG_BLOCK */ |
121 | ||
8a703a38 IC |
122 | #define osd_req_op_data(oreq, whch, typ, fld) \ |
123 | ({ \ | |
124 | struct ceph_osd_request *__oreq = (oreq); \ | |
125 | unsigned int __whch = (whch); \ | |
126 | BUG_ON(__whch >= __oreq->r_num_ops); \ | |
127 | &__oreq->r_ops[__whch].typ.fld; \ | |
128 | }) | |
863c7eb5 | 129 | |
49719778 AE |
130 | static struct ceph_osd_data * |
131 | osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which) | |
132 | { | |
133 | BUG_ON(which >= osd_req->r_num_ops); | |
134 | ||
135 | return &osd_req->r_ops[which].raw_data_in; | |
136 | } | |
137 | ||
a4ce40a9 AE |
138 | struct ceph_osd_data * |
139 | osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req, | |
406e2c9f | 140 | unsigned int which) |
a4ce40a9 | 141 | { |
863c7eb5 | 142 | return osd_req_op_data(osd_req, which, extent, osd_data); |
a4ce40a9 AE |
143 | } |
144 | EXPORT_SYMBOL(osd_req_op_extent_osd_data); | |
145 | ||
a4ce40a9 AE |
146 | struct ceph_osd_data * |
147 | osd_req_op_cls_response_data(struct ceph_osd_request *osd_req, | |
148 | unsigned int which) | |
149 | { | |
863c7eb5 | 150 | return osd_req_op_data(osd_req, which, cls, response_data); |
a4ce40a9 AE |
151 | } |
152 | EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */ | |
153 | ||
49719778 AE |
154 | void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req, |
155 | unsigned int which, struct page **pages, | |
156 | u64 length, u32 alignment, | |
157 | bool pages_from_pool, bool own_pages) | |
158 | { | |
159 | struct ceph_osd_data *osd_data; | |
160 | ||
161 | osd_data = osd_req_op_raw_data_in(osd_req, which); | |
162 | ceph_osd_data_pages_init(osd_data, pages, length, alignment, | |
163 | pages_from_pool, own_pages); | |
164 | } | |
165 | EXPORT_SYMBOL(osd_req_op_raw_data_in_pages); | |
166 | ||
a4ce40a9 | 167 | void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req, |
406e2c9f AE |
168 | unsigned int which, struct page **pages, |
169 | u64 length, u32 alignment, | |
a4ce40a9 AE |
170 | bool pages_from_pool, bool own_pages) |
171 | { | |
172 | struct ceph_osd_data *osd_data; | |
173 | ||
863c7eb5 | 174 | osd_data = osd_req_op_data(osd_req, which, extent, osd_data); |
a4ce40a9 AE |
175 | ceph_osd_data_pages_init(osd_data, pages, length, alignment, |
176 | pages_from_pool, own_pages); | |
a4ce40a9 AE |
177 | } |
178 | EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages); | |
179 | ||
180 | void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req, | |
406e2c9f | 181 | unsigned int which, struct ceph_pagelist *pagelist) |
a4ce40a9 AE |
182 | { |
183 | struct ceph_osd_data *osd_data; | |
184 | ||
863c7eb5 | 185 | osd_data = osd_req_op_data(osd_req, which, extent, osd_data); |
a4ce40a9 | 186 | ceph_osd_data_pagelist_init(osd_data, pagelist); |
a4ce40a9 AE |
187 | } |
188 | EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist); | |
189 | ||
190 | #ifdef CONFIG_BLOCK | |
191 | void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req, | |
406e2c9f | 192 | unsigned int which, struct bio *bio, size_t bio_length) |
a4ce40a9 AE |
193 | { |
194 | struct ceph_osd_data *osd_data; | |
863c7eb5 AE |
195 | |
196 | osd_data = osd_req_op_data(osd_req, which, extent, osd_data); | |
a4ce40a9 | 197 | ceph_osd_data_bio_init(osd_data, bio, bio_length); |
a4ce40a9 AE |
198 | } |
199 | EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio); | |
200 | #endif /* CONFIG_BLOCK */ | |
201 | ||
202 | static void osd_req_op_cls_request_info_pagelist( | |
203 | struct ceph_osd_request *osd_req, | |
204 | unsigned int which, struct ceph_pagelist *pagelist) | |
205 | { | |
206 | struct ceph_osd_data *osd_data; | |
207 | ||
863c7eb5 | 208 | osd_data = osd_req_op_data(osd_req, which, cls, request_info); |
a4ce40a9 | 209 | ceph_osd_data_pagelist_init(osd_data, pagelist); |
a4ce40a9 AE |
210 | } |
211 | ||
04017e29 AE |
212 | void osd_req_op_cls_request_data_pagelist( |
213 | struct ceph_osd_request *osd_req, | |
214 | unsigned int which, struct ceph_pagelist *pagelist) | |
215 | { | |
216 | struct ceph_osd_data *osd_data; | |
217 | ||
863c7eb5 | 218 | osd_data = osd_req_op_data(osd_req, which, cls, request_data); |
04017e29 AE |
219 | ceph_osd_data_pagelist_init(osd_data, pagelist); |
220 | } | |
221 | EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist); | |
222 | ||
6c57b554 AE |
223 | void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req, |
224 | unsigned int which, struct page **pages, u64 length, | |
225 | u32 alignment, bool pages_from_pool, bool own_pages) | |
226 | { | |
227 | struct ceph_osd_data *osd_data; | |
228 | ||
229 | osd_data = osd_req_op_data(osd_req, which, cls, request_data); | |
230 | ceph_osd_data_pages_init(osd_data, pages, length, alignment, | |
231 | pages_from_pool, own_pages); | |
232 | } | |
233 | EXPORT_SYMBOL(osd_req_op_cls_request_data_pages); | |
234 | ||
a4ce40a9 AE |
235 | void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req, |
236 | unsigned int which, struct page **pages, u64 length, | |
237 | u32 alignment, bool pages_from_pool, bool own_pages) | |
238 | { | |
239 | struct ceph_osd_data *osd_data; | |
240 | ||
863c7eb5 | 241 | osd_data = osd_req_op_data(osd_req, which, cls, response_data); |
a4ce40a9 AE |
242 | ceph_osd_data_pages_init(osd_data, pages, length, alignment, |
243 | pages_from_pool, own_pages); | |
a4ce40a9 AE |
244 | } |
245 | EXPORT_SYMBOL(osd_req_op_cls_response_data_pages); | |
246 | ||
23c08a9c AE |
247 | static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data) |
248 | { | |
249 | switch (osd_data->type) { | |
250 | case CEPH_OSD_DATA_TYPE_NONE: | |
251 | return 0; | |
252 | case CEPH_OSD_DATA_TYPE_PAGES: | |
253 | return osd_data->length; | |
254 | case CEPH_OSD_DATA_TYPE_PAGELIST: | |
255 | return (u64)osd_data->pagelist->length; | |
256 | #ifdef CONFIG_BLOCK | |
257 | case CEPH_OSD_DATA_TYPE_BIO: | |
258 | return (u64)osd_data->bio_length; | |
259 | #endif /* CONFIG_BLOCK */ | |
260 | default: | |
261 | WARN(true, "unrecognized data type %d\n", (int)osd_data->type); | |
262 | return 0; | |
263 | } | |
264 | } | |
265 | ||
c54d47bf AE |
266 | static void ceph_osd_data_release(struct ceph_osd_data *osd_data) |
267 | { | |
5476492f | 268 | if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) { |
c54d47bf AE |
269 | int num_pages; |
270 | ||
271 | num_pages = calc_pages_for((u64)osd_data->alignment, | |
272 | (u64)osd_data->length); | |
273 | ceph_release_page_vector(osd_data->pages, num_pages); | |
274 | } | |
5476492f AE |
275 | ceph_osd_data_init(osd_data); |
276 | } | |
277 | ||
278 | static void osd_req_op_data_release(struct ceph_osd_request *osd_req, | |
279 | unsigned int which) | |
280 | { | |
281 | struct ceph_osd_req_op *op; | |
282 | ||
283 | BUG_ON(which >= osd_req->r_num_ops); | |
284 | op = &osd_req->r_ops[which]; | |
285 | ||
286 | switch (op->op) { | |
287 | case CEPH_OSD_OP_READ: | |
288 | case CEPH_OSD_OP_WRITE: | |
e30b7577 | 289 | case CEPH_OSD_OP_WRITEFULL: |
5476492f AE |
290 | ceph_osd_data_release(&op->extent.osd_data); |
291 | break; | |
292 | case CEPH_OSD_OP_CALL: | |
293 | ceph_osd_data_release(&op->cls.request_info); | |
04017e29 | 294 | ceph_osd_data_release(&op->cls.request_data); |
5476492f AE |
295 | ceph_osd_data_release(&op->cls.response_data); |
296 | break; | |
d74b50be YZ |
297 | case CEPH_OSD_OP_SETXATTR: |
298 | case CEPH_OSD_OP_CMPXATTR: | |
299 | ceph_osd_data_release(&op->xattr.osd_data); | |
300 | break; | |
66ba609f YZ |
301 | case CEPH_OSD_OP_STAT: |
302 | ceph_osd_data_release(&op->raw_data_in); | |
303 | break; | |
5476492f AE |
304 | default: |
305 | break; | |
306 | } | |
c54d47bf AE |
307 | } |
308 | ||
f24e9980 SW |
309 | /* |
310 | * requests | |
311 | */ | |
9e94af20 | 312 | static void ceph_osdc_release_request(struct kref *kref) |
f24e9980 | 313 | { |
9e94af20 ID |
314 | struct ceph_osd_request *req = container_of(kref, |
315 | struct ceph_osd_request, r_kref); | |
5476492f | 316 | unsigned int which; |
415e49a9 | 317 | |
9e94af20 ID |
318 | dout("%s %p (r_request %p r_reply %p)\n", __func__, req, |
319 | req->r_request, req->r_reply); | |
6562d661 ID |
320 | WARN_ON(!RB_EMPTY_NODE(&req->r_node)); |
321 | WARN_ON(!list_empty(&req->r_req_lru_item)); | |
322 | WARN_ON(!list_empty(&req->r_osd_item)); | |
323 | WARN_ON(!list_empty(&req->r_linger_item)); | |
324 | WARN_ON(!list_empty(&req->r_linger_osd_item)); | |
325 | WARN_ON(req->r_osd); | |
9e94af20 | 326 | |
415e49a9 SW |
327 | if (req->r_request) |
328 | ceph_msg_put(req->r_request); | |
ace6d3a9 | 329 | if (req->r_reply) { |
8921d114 | 330 | ceph_msg_revoke_incoming(req->r_reply); |
ab8cb34a | 331 | ceph_msg_put(req->r_reply); |
ace6d3a9 | 332 | } |
0fff87ec | 333 | |
5476492f AE |
334 | for (which = 0; which < req->r_num_ops; which++) |
335 | osd_req_op_data_release(req, which); | |
0fff87ec | 336 | |
415e49a9 SW |
337 | ceph_put_snap_context(req->r_snapc); |
338 | if (req->r_mempool) | |
339 | mempool_free(req, req->r_osdc->req_mempool); | |
3f1af42a | 340 | else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS) |
5522ae0b | 341 | kmem_cache_free(ceph_osd_request_cache, req); |
3f1af42a ID |
342 | else |
343 | kfree(req); | |
f24e9980 | 344 | } |
9e94af20 ID |
345 | |
346 | void ceph_osdc_get_request(struct ceph_osd_request *req) | |
347 | { | |
348 | dout("%s %p (was %d)\n", __func__, req, | |
349 | atomic_read(&req->r_kref.refcount)); | |
350 | kref_get(&req->r_kref); | |
351 | } | |
352 | EXPORT_SYMBOL(ceph_osdc_get_request); | |
353 | ||
354 | void ceph_osdc_put_request(struct ceph_osd_request *req) | |
355 | { | |
3ed97d63 ID |
356 | if (req) { |
357 | dout("%s %p (was %d)\n", __func__, req, | |
358 | atomic_read(&req->r_kref.refcount)); | |
359 | kref_put(&req->r_kref, ceph_osdc_release_request); | |
360 | } | |
9e94af20 ID |
361 | } |
362 | EXPORT_SYMBOL(ceph_osdc_put_request); | |
68b4476b | 363 | |
3499e8a5 | 364 | struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc, |
f24e9980 | 365 | struct ceph_snap_context *snapc, |
1b83bef2 | 366 | unsigned int num_ops, |
3499e8a5 | 367 | bool use_mempool, |
54a54007 | 368 | gfp_t gfp_flags) |
f24e9980 SW |
369 | { |
370 | struct ceph_osd_request *req; | |
1b83bef2 | 371 | |
f24e9980 | 372 | if (use_mempool) { |
3f1af42a | 373 | BUG_ON(num_ops > CEPH_OSD_SLAB_OPS); |
3499e8a5 | 374 | req = mempool_alloc(osdc->req_mempool, gfp_flags); |
3f1af42a ID |
375 | } else if (num_ops <= CEPH_OSD_SLAB_OPS) { |
376 | req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags); | |
f24e9980 | 377 | } else { |
3f1af42a ID |
378 | BUG_ON(num_ops > CEPH_OSD_MAX_OPS); |
379 | req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]), | |
380 | gfp_flags); | |
f24e9980 | 381 | } |
3f1af42a | 382 | if (unlikely(!req)) |
a79832f2 | 383 | return NULL; |
f24e9980 | 384 | |
3f1af42a ID |
385 | /* req only, each op is zeroed in _osd_req_op_init() */ |
386 | memset(req, 0, sizeof(*req)); | |
387 | ||
f24e9980 SW |
388 | req->r_osdc = osdc; |
389 | req->r_mempool = use_mempool; | |
79528734 | 390 | req->r_num_ops = num_ops; |
84127282 ID |
391 | req->r_snapid = CEPH_NOSNAP; |
392 | req->r_snapc = ceph_get_snap_context(snapc); | |
68b4476b | 393 | |
415e49a9 | 394 | kref_init(&req->r_kref); |
f24e9980 SW |
395 | init_completion(&req->r_completion); |
396 | init_completion(&req->r_safe_completion); | |
a978fa20 | 397 | RB_CLEAR_NODE(&req->r_node); |
f24e9980 | 398 | INIT_LIST_HEAD(&req->r_unsafe_item); |
a40c4f10 | 399 | INIT_LIST_HEAD(&req->r_linger_item); |
1d0326b1 | 400 | INIT_LIST_HEAD(&req->r_linger_osd_item); |
935b639a | 401 | INIT_LIST_HEAD(&req->r_req_lru_item); |
cd43045c SW |
402 | INIT_LIST_HEAD(&req->r_osd_item); |
403 | ||
3c972c95 | 404 | req->r_base_oloc.pool = -1; |
205ee118 | 405 | req->r_target_oloc.pool = -1; |
22116525 | 406 | |
13d1ad16 ID |
407 | dout("%s req %p\n", __func__, req); |
408 | return req; | |
409 | } | |
410 | EXPORT_SYMBOL(ceph_osdc_alloc_request); | |
3f1af42a | 411 | |
13d1ad16 ID |
412 | int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp) |
413 | { | |
414 | struct ceph_osd_client *osdc = req->r_osdc; | |
415 | struct ceph_msg *msg; | |
416 | int msg_size; | |
c16e7869 | 417 | |
13d1ad16 | 418 | /* create request message */ |
ae458f5a ID |
419 | msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */ |
420 | msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */ | |
421 | msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */ | |
422 | msg_size += 1 + 8 + 4 + 4; /* pgid */ | |
13d1ad16 ID |
423 | msg_size += 4 + req->r_base_oid.name_len; /* oid */ |
424 | msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op); | |
ae458f5a ID |
425 | msg_size += 8; /* snapid */ |
426 | msg_size += 8; /* snap_seq */ | |
13d1ad16 | 427 | msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0); |
ae458f5a ID |
428 | msg_size += 4; /* retry_attempt */ |
429 | ||
13d1ad16 | 430 | if (req->r_mempool) |
8f3bc053 | 431 | msg = ceph_msgpool_get(&osdc->msgpool_op, 0); |
f24e9980 | 432 | else |
13d1ad16 ID |
433 | msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true); |
434 | if (!msg) | |
435 | return -ENOMEM; | |
68b4476b | 436 | |
f24e9980 | 437 | memset(msg->front.iov_base, 0, msg->front.iov_len); |
3499e8a5 | 438 | req->r_request = msg; |
3499e8a5 | 439 | |
13d1ad16 ID |
440 | /* create reply message */ |
441 | msg_size = OSD_OPREPLY_FRONT_LEN; | |
711da55d ID |
442 | msg_size += req->r_base_oid.name_len; |
443 | msg_size += req->r_num_ops * sizeof(struct ceph_osd_op); | |
13d1ad16 ID |
444 | |
445 | if (req->r_mempool) | |
446 | msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0); | |
447 | else | |
448 | msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true); | |
449 | if (!msg) | |
450 | return -ENOMEM; | |
451 | ||
452 | req->r_reply = msg; | |
453 | ||
454 | return 0; | |
3499e8a5 | 455 | } |
13d1ad16 | 456 | EXPORT_SYMBOL(ceph_osdc_alloc_messages); |
3499e8a5 | 457 | |
a8dd0a37 | 458 | static bool osd_req_opcode_valid(u16 opcode) |
68b4476b | 459 | { |
a8dd0a37 | 460 | switch (opcode) { |
70b5bfa3 ID |
461 | #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true; |
462 | __CEPH_FORALL_OSD_OPS(GENERATE_CASE) | |
463 | #undef GENERATE_CASE | |
a8dd0a37 AE |
464 | default: |
465 | return false; | |
466 | } | |
467 | } | |
468 | ||
33803f33 AE |
469 | /* |
470 | * This is an osd op init function for opcodes that have no data or | |
471 | * other information associated with them. It also serves as a | |
472 | * common init routine for all the other init functions, below. | |
473 | */ | |
c99d2d4a | 474 | static struct ceph_osd_req_op * |
49719778 | 475 | _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which, |
144cba14 | 476 | u16 opcode, u32 flags) |
33803f33 | 477 | { |
c99d2d4a AE |
478 | struct ceph_osd_req_op *op; |
479 | ||
480 | BUG_ON(which >= osd_req->r_num_ops); | |
33803f33 AE |
481 | BUG_ON(!osd_req_opcode_valid(opcode)); |
482 | ||
c99d2d4a | 483 | op = &osd_req->r_ops[which]; |
33803f33 | 484 | memset(op, 0, sizeof (*op)); |
33803f33 | 485 | op->op = opcode; |
144cba14 | 486 | op->flags = flags; |
c99d2d4a AE |
487 | |
488 | return op; | |
33803f33 AE |
489 | } |
490 | ||
49719778 | 491 | void osd_req_op_init(struct ceph_osd_request *osd_req, |
144cba14 | 492 | unsigned int which, u16 opcode, u32 flags) |
49719778 | 493 | { |
144cba14 | 494 | (void)_osd_req_op_init(osd_req, which, opcode, flags); |
49719778 AE |
495 | } |
496 | EXPORT_SYMBOL(osd_req_op_init); | |
497 | ||
c99d2d4a AE |
498 | void osd_req_op_extent_init(struct ceph_osd_request *osd_req, |
499 | unsigned int which, u16 opcode, | |
33803f33 AE |
500 | u64 offset, u64 length, |
501 | u64 truncate_size, u32 truncate_seq) | |
502 | { | |
144cba14 YZ |
503 | struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, |
504 | opcode, 0); | |
33803f33 AE |
505 | size_t payload_len = 0; |
506 | ||
ad7a60de | 507 | BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE && |
e30b7577 ID |
508 | opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO && |
509 | opcode != CEPH_OSD_OP_TRUNCATE); | |
33803f33 | 510 | |
33803f33 AE |
511 | op->extent.offset = offset; |
512 | op->extent.length = length; | |
513 | op->extent.truncate_size = truncate_size; | |
514 | op->extent.truncate_seq = truncate_seq; | |
e30b7577 | 515 | if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL) |
33803f33 AE |
516 | payload_len += length; |
517 | ||
de2aa102 | 518 | op->indata_len = payload_len; |
33803f33 AE |
519 | } |
520 | EXPORT_SYMBOL(osd_req_op_extent_init); | |
521 | ||
c99d2d4a AE |
522 | void osd_req_op_extent_update(struct ceph_osd_request *osd_req, |
523 | unsigned int which, u64 length) | |
e5975c7c | 524 | { |
c99d2d4a AE |
525 | struct ceph_osd_req_op *op; |
526 | u64 previous; | |
527 | ||
528 | BUG_ON(which >= osd_req->r_num_ops); | |
529 | op = &osd_req->r_ops[which]; | |
530 | previous = op->extent.length; | |
e5975c7c AE |
531 | |
532 | if (length == previous) | |
533 | return; /* Nothing to do */ | |
534 | BUG_ON(length > previous); | |
535 | ||
536 | op->extent.length = length; | |
de2aa102 | 537 | op->indata_len -= previous - length; |
e5975c7c AE |
538 | } |
539 | EXPORT_SYMBOL(osd_req_op_extent_update); | |
540 | ||
2c63f49a YZ |
541 | void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req, |
542 | unsigned int which, u64 offset_inc) | |
543 | { | |
544 | struct ceph_osd_req_op *op, *prev_op; | |
545 | ||
546 | BUG_ON(which + 1 >= osd_req->r_num_ops); | |
547 | ||
548 | prev_op = &osd_req->r_ops[which]; | |
549 | op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags); | |
550 | /* dup previous one */ | |
551 | op->indata_len = prev_op->indata_len; | |
552 | op->outdata_len = prev_op->outdata_len; | |
553 | op->extent = prev_op->extent; | |
554 | /* adjust offset */ | |
555 | op->extent.offset += offset_inc; | |
556 | op->extent.length -= offset_inc; | |
557 | ||
558 | if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL) | |
559 | op->indata_len -= offset_inc; | |
560 | } | |
561 | EXPORT_SYMBOL(osd_req_op_extent_dup_last); | |
562 | ||
c99d2d4a | 563 | void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which, |
04017e29 | 564 | u16 opcode, const char *class, const char *method) |
33803f33 | 565 | { |
144cba14 YZ |
566 | struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, |
567 | opcode, 0); | |
5f562df5 | 568 | struct ceph_pagelist *pagelist; |
33803f33 AE |
569 | size_t payload_len = 0; |
570 | size_t size; | |
571 | ||
572 | BUG_ON(opcode != CEPH_OSD_OP_CALL); | |
573 | ||
5f562df5 AE |
574 | pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS); |
575 | BUG_ON(!pagelist); | |
576 | ceph_pagelist_init(pagelist); | |
577 | ||
33803f33 AE |
578 | op->cls.class_name = class; |
579 | size = strlen(class); | |
580 | BUG_ON(size > (size_t) U8_MAX); | |
581 | op->cls.class_len = size; | |
5f562df5 | 582 | ceph_pagelist_append(pagelist, class, size); |
33803f33 AE |
583 | payload_len += size; |
584 | ||
585 | op->cls.method_name = method; | |
586 | size = strlen(method); | |
587 | BUG_ON(size > (size_t) U8_MAX); | |
588 | op->cls.method_len = size; | |
5f562df5 | 589 | ceph_pagelist_append(pagelist, method, size); |
33803f33 AE |
590 | payload_len += size; |
591 | ||
a4ce40a9 | 592 | osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist); |
5f562df5 | 593 | |
33803f33 AE |
594 | op->cls.argc = 0; /* currently unused */ |
595 | ||
de2aa102 | 596 | op->indata_len = payload_len; |
33803f33 AE |
597 | } |
598 | EXPORT_SYMBOL(osd_req_op_cls_init); | |
8c042b0d | 599 | |
d74b50be YZ |
600 | int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which, |
601 | u16 opcode, const char *name, const void *value, | |
602 | size_t size, u8 cmp_op, u8 cmp_mode) | |
603 | { | |
144cba14 YZ |
604 | struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, |
605 | opcode, 0); | |
d74b50be YZ |
606 | struct ceph_pagelist *pagelist; |
607 | size_t payload_len; | |
608 | ||
609 | BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR); | |
610 | ||
611 | pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS); | |
612 | if (!pagelist) | |
613 | return -ENOMEM; | |
614 | ||
615 | ceph_pagelist_init(pagelist); | |
616 | ||
617 | payload_len = strlen(name); | |
618 | op->xattr.name_len = payload_len; | |
619 | ceph_pagelist_append(pagelist, name, payload_len); | |
620 | ||
621 | op->xattr.value_len = size; | |
622 | ceph_pagelist_append(pagelist, value, size); | |
623 | payload_len += size; | |
624 | ||
625 | op->xattr.cmp_op = cmp_op; | |
626 | op->xattr.cmp_mode = cmp_mode; | |
627 | ||
628 | ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist); | |
de2aa102 | 629 | op->indata_len = payload_len; |
d74b50be YZ |
630 | return 0; |
631 | } | |
632 | EXPORT_SYMBOL(osd_req_op_xattr_init); | |
633 | ||
c99d2d4a AE |
634 | void osd_req_op_watch_init(struct ceph_osd_request *osd_req, |
635 | unsigned int which, u16 opcode, | |
33803f33 AE |
636 | u64 cookie, u64 version, int flag) |
637 | { | |
144cba14 YZ |
638 | struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, |
639 | opcode, 0); | |
33803f33 | 640 | |
c99d2d4a | 641 | BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH); |
33803f33 AE |
642 | |
643 | op->watch.cookie = cookie; | |
9ef1ee5a | 644 | op->watch.ver = version; |
33803f33 | 645 | if (opcode == CEPH_OSD_OP_WATCH && flag) |
c99d2d4a | 646 | op->watch.flag = (u8)1; |
33803f33 AE |
647 | } |
648 | EXPORT_SYMBOL(osd_req_op_watch_init); | |
649 | ||
c647b8a8 ID |
650 | void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req, |
651 | unsigned int which, | |
652 | u64 expected_object_size, | |
653 | u64 expected_write_size) | |
654 | { | |
655 | struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, | |
144cba14 YZ |
656 | CEPH_OSD_OP_SETALLOCHINT, |
657 | 0); | |
c647b8a8 ID |
658 | |
659 | op->alloc_hint.expected_object_size = expected_object_size; | |
660 | op->alloc_hint.expected_write_size = expected_write_size; | |
661 | ||
662 | /* | |
663 | * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed | |
664 | * not worth a feature bit. Set FAILOK per-op flag to make | |
665 | * sure older osds don't trip over an unsupported opcode. | |
666 | */ | |
667 | op->flags |= CEPH_OSD_OP_FLAG_FAILOK; | |
668 | } | |
669 | EXPORT_SYMBOL(osd_req_op_alloc_hint_init); | |
670 | ||
90af3602 | 671 | static void ceph_osdc_msg_data_add(struct ceph_msg *msg, |
ec9123c5 AE |
672 | struct ceph_osd_data *osd_data) |
673 | { | |
674 | u64 length = ceph_osd_data_length(osd_data); | |
675 | ||
676 | if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) { | |
677 | BUG_ON(length > (u64) SIZE_MAX); | |
678 | if (length) | |
90af3602 | 679 | ceph_msg_data_add_pages(msg, osd_data->pages, |
ec9123c5 AE |
680 | length, osd_data->alignment); |
681 | } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) { | |
682 | BUG_ON(!length); | |
90af3602 | 683 | ceph_msg_data_add_pagelist(msg, osd_data->pagelist); |
ec9123c5 AE |
684 | #ifdef CONFIG_BLOCK |
685 | } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) { | |
90af3602 | 686 | ceph_msg_data_add_bio(msg, osd_data->bio, length); |
ec9123c5 AE |
687 | #endif |
688 | } else { | |
689 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE); | |
690 | } | |
691 | } | |
692 | ||
a8dd0a37 | 693 | static u64 osd_req_encode_op(struct ceph_osd_request *req, |
79528734 | 694 | struct ceph_osd_op *dst, unsigned int which) |
a8dd0a37 | 695 | { |
79528734 | 696 | struct ceph_osd_req_op *src; |
04017e29 | 697 | struct ceph_osd_data *osd_data; |
54d50649 | 698 | u64 request_data_len = 0; |
04017e29 | 699 | u64 data_length; |
a8dd0a37 | 700 | |
79528734 AE |
701 | BUG_ON(which >= req->r_num_ops); |
702 | src = &req->r_ops[which]; | |
a8dd0a37 AE |
703 | if (WARN_ON(!osd_req_opcode_valid(src->op))) { |
704 | pr_err("unrecognized osd opcode %d\n", src->op); | |
705 | ||
706 | return 0; | |
707 | } | |
708 | ||
709 | switch (src->op) { | |
710 | case CEPH_OSD_OP_STAT: | |
49719778 AE |
711 | osd_data = &src->raw_data_in; |
712 | ceph_osdc_msg_data_add(req->r_reply, osd_data); | |
a8dd0a37 AE |
713 | break; |
714 | case CEPH_OSD_OP_READ: | |
715 | case CEPH_OSD_OP_WRITE: | |
e30b7577 | 716 | case CEPH_OSD_OP_WRITEFULL: |
ad7a60de | 717 | case CEPH_OSD_OP_ZERO: |
ad7a60de | 718 | case CEPH_OSD_OP_TRUNCATE: |
e30b7577 ID |
719 | if (src->op == CEPH_OSD_OP_WRITE || |
720 | src->op == CEPH_OSD_OP_WRITEFULL) | |
54d50649 | 721 | request_data_len = src->extent.length; |
a8dd0a37 AE |
722 | dst->extent.offset = cpu_to_le64(src->extent.offset); |
723 | dst->extent.length = cpu_to_le64(src->extent.length); | |
724 | dst->extent.truncate_size = | |
725 | cpu_to_le64(src->extent.truncate_size); | |
726 | dst->extent.truncate_seq = | |
727 | cpu_to_le32(src->extent.truncate_seq); | |
04017e29 | 728 | osd_data = &src->extent.osd_data; |
e30b7577 ID |
729 | if (src->op == CEPH_OSD_OP_WRITE || |
730 | src->op == CEPH_OSD_OP_WRITEFULL) | |
04017e29 | 731 | ceph_osdc_msg_data_add(req->r_request, osd_data); |
5476492f | 732 | else |
04017e29 | 733 | ceph_osdc_msg_data_add(req->r_reply, osd_data); |
a8dd0a37 AE |
734 | break; |
735 | case CEPH_OSD_OP_CALL: | |
a8dd0a37 AE |
736 | dst->cls.class_len = src->cls.class_len; |
737 | dst->cls.method_len = src->cls.method_len; | |
04017e29 AE |
738 | osd_data = &src->cls.request_info; |
739 | ceph_osdc_msg_data_add(req->r_request, osd_data); | |
740 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST); | |
741 | request_data_len = osd_data->pagelist->length; | |
742 | ||
743 | osd_data = &src->cls.request_data; | |
744 | data_length = ceph_osd_data_length(osd_data); | |
745 | if (data_length) { | |
746 | BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE); | |
747 | dst->cls.indata_len = cpu_to_le32(data_length); | |
748 | ceph_osdc_msg_data_add(req->r_request, osd_data); | |
de2aa102 | 749 | src->indata_len += data_length; |
04017e29 AE |
750 | request_data_len += data_length; |
751 | } | |
752 | osd_data = &src->cls.response_data; | |
753 | ceph_osdc_msg_data_add(req->r_reply, osd_data); | |
a8dd0a37 AE |
754 | break; |
755 | case CEPH_OSD_OP_STARTSYNC: | |
756 | break; | |
757 | case CEPH_OSD_OP_NOTIFY_ACK: | |
758 | case CEPH_OSD_OP_WATCH: | |
759 | dst->watch.cookie = cpu_to_le64(src->watch.cookie); | |
760 | dst->watch.ver = cpu_to_le64(src->watch.ver); | |
761 | dst->watch.flag = src->watch.flag; | |
762 | break; | |
c647b8a8 ID |
763 | case CEPH_OSD_OP_SETALLOCHINT: |
764 | dst->alloc_hint.expected_object_size = | |
765 | cpu_to_le64(src->alloc_hint.expected_object_size); | |
766 | dst->alloc_hint.expected_write_size = | |
767 | cpu_to_le64(src->alloc_hint.expected_write_size); | |
768 | break; | |
d74b50be YZ |
769 | case CEPH_OSD_OP_SETXATTR: |
770 | case CEPH_OSD_OP_CMPXATTR: | |
771 | dst->xattr.name_len = cpu_to_le32(src->xattr.name_len); | |
772 | dst->xattr.value_len = cpu_to_le32(src->xattr.value_len); | |
773 | dst->xattr.cmp_op = src->xattr.cmp_op; | |
774 | dst->xattr.cmp_mode = src->xattr.cmp_mode; | |
775 | osd_data = &src->xattr.osd_data; | |
776 | ceph_osdc_msg_data_add(req->r_request, osd_data); | |
777 | request_data_len = osd_data->pagelist->length; | |
778 | break; | |
864e9197 YZ |
779 | case CEPH_OSD_OP_CREATE: |
780 | case CEPH_OSD_OP_DELETE: | |
781 | break; | |
a8dd0a37 | 782 | default: |
4c46459c | 783 | pr_err("unsupported osd opcode %s\n", |
8f63ca2d | 784 | ceph_osd_op_name(src->op)); |
4c46459c | 785 | WARN_ON(1); |
a8dd0a37 AE |
786 | |
787 | return 0; | |
68b4476b | 788 | } |
7b25bf5f | 789 | |
a8dd0a37 | 790 | dst->op = cpu_to_le16(src->op); |
7b25bf5f | 791 | dst->flags = cpu_to_le32(src->flags); |
de2aa102 | 792 | dst->payload_len = cpu_to_le32(src->indata_len); |
175face2 | 793 | |
54d50649 | 794 | return request_data_len; |
68b4476b YS |
795 | } |
796 | ||
3499e8a5 YS |
797 | /* |
798 | * build new request AND message, calculate layout, and adjust file | |
799 | * extent as needed. | |
800 | * | |
801 | * if the file was recently truncated, we include information about its | |
802 | * old and new size so that the object can be updated appropriately. (we | |
803 | * avoid synchronously deleting truncated objects because it's slow.) | |
804 | * | |
805 | * if @do_sync, include a 'startsync' command so that the osd will flush | |
806 | * data quickly. | |
807 | */ | |
808 | struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, | |
809 | struct ceph_file_layout *layout, | |
810 | struct ceph_vino vino, | |
715e4cd4 YZ |
811 | u64 off, u64 *plen, |
812 | unsigned int which, int num_ops, | |
3499e8a5 YS |
813 | int opcode, int flags, |
814 | struct ceph_snap_context *snapc, | |
3499e8a5 YS |
815 | u32 truncate_seq, |
816 | u64 truncate_size, | |
153e5167 | 817 | bool use_mempool) |
3499e8a5 | 818 | { |
68b4476b | 819 | struct ceph_osd_request *req; |
75d1c941 AE |
820 | u64 objnum = 0; |
821 | u64 objoff = 0; | |
822 | u64 objlen = 0; | |
6816282d | 823 | int r; |
68b4476b | 824 | |
ad7a60de | 825 | BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE && |
864e9197 YZ |
826 | opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE && |
827 | opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE); | |
68b4476b | 828 | |
acead002 | 829 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool, |
ae7ca4a3 | 830 | GFP_NOFS); |
13d1ad16 ID |
831 | if (!req) { |
832 | r = -ENOMEM; | |
833 | goto fail; | |
834 | } | |
79528734 | 835 | |
d178a9e7 | 836 | req->r_flags = flags; |
3499e8a5 YS |
837 | |
838 | /* calculate max write size */ | |
a19dadfb | 839 | r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen); |
13d1ad16 ID |
840 | if (r) |
841 | goto fail; | |
a19dadfb | 842 | |
864e9197 | 843 | if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) { |
144cba14 | 844 | osd_req_op_init(req, which, opcode, 0); |
864e9197 YZ |
845 | } else { |
846 | u32 object_size = le32_to_cpu(layout->fl_object_size); | |
847 | u32 object_base = off - objoff; | |
848 | if (!(truncate_seq == 1 && truncate_size == -1ULL)) { | |
849 | if (truncate_size <= object_base) { | |
850 | truncate_size = 0; | |
851 | } else { | |
852 | truncate_size -= object_base; | |
853 | if (truncate_size > object_size) | |
854 | truncate_size = object_size; | |
855 | } | |
ccca4e37 | 856 | } |
715e4cd4 | 857 | osd_req_op_extent_init(req, which, opcode, objoff, objlen, |
864e9197 YZ |
858 | truncate_size, truncate_seq); |
859 | } | |
d18d1e28 | 860 | |
3c972c95 | 861 | req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout); |
3499e8a5 | 862 | |
3c972c95 | 863 | snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name), |
4295f221 | 864 | "%llx.%08llx", vino.ino, objnum); |
3c972c95 | 865 | req->r_base_oid.name_len = strlen(req->r_base_oid.name); |
dbe0fc41 | 866 | |
13d1ad16 ID |
867 | r = ceph_osdc_alloc_messages(req, GFP_NOFS); |
868 | if (r) | |
869 | goto fail; | |
870 | ||
f24e9980 | 871 | return req; |
13d1ad16 ID |
872 | |
873 | fail: | |
874 | ceph_osdc_put_request(req); | |
875 | return ERR_PTR(r); | |
f24e9980 | 876 | } |
3d14c5d2 | 877 | EXPORT_SYMBOL(ceph_osdc_new_request); |
f24e9980 SW |
878 | |
879 | /* | |
880 | * We keep osd requests in an rbtree, sorted by ->r_tid. | |
881 | */ | |
882 | static void __insert_request(struct ceph_osd_client *osdc, | |
883 | struct ceph_osd_request *new) | |
884 | { | |
885 | struct rb_node **p = &osdc->requests.rb_node; | |
886 | struct rb_node *parent = NULL; | |
887 | struct ceph_osd_request *req = NULL; | |
888 | ||
889 | while (*p) { | |
890 | parent = *p; | |
891 | req = rb_entry(parent, struct ceph_osd_request, r_node); | |
892 | if (new->r_tid < req->r_tid) | |
893 | p = &(*p)->rb_left; | |
894 | else if (new->r_tid > req->r_tid) | |
895 | p = &(*p)->rb_right; | |
896 | else | |
897 | BUG(); | |
898 | } | |
899 | ||
900 | rb_link_node(&new->r_node, parent, p); | |
901 | rb_insert_color(&new->r_node, &osdc->requests); | |
902 | } | |
903 | ||
904 | static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc, | |
905 | u64 tid) | |
906 | { | |
907 | struct ceph_osd_request *req; | |
908 | struct rb_node *n = osdc->requests.rb_node; | |
909 | ||
910 | while (n) { | |
911 | req = rb_entry(n, struct ceph_osd_request, r_node); | |
912 | if (tid < req->r_tid) | |
913 | n = n->rb_left; | |
914 | else if (tid > req->r_tid) | |
915 | n = n->rb_right; | |
916 | else | |
917 | return req; | |
918 | } | |
919 | return NULL; | |
920 | } | |
921 | ||
922 | static struct ceph_osd_request * | |
923 | __lookup_request_ge(struct ceph_osd_client *osdc, | |
924 | u64 tid) | |
925 | { | |
926 | struct ceph_osd_request *req; | |
927 | struct rb_node *n = osdc->requests.rb_node; | |
928 | ||
929 | while (n) { | |
930 | req = rb_entry(n, struct ceph_osd_request, r_node); | |
931 | if (tid < req->r_tid) { | |
932 | if (!n->rb_left) | |
933 | return req; | |
934 | n = n->rb_left; | |
935 | } else if (tid > req->r_tid) { | |
936 | n = n->rb_right; | |
937 | } else { | |
938 | return req; | |
939 | } | |
940 | } | |
941 | return NULL; | |
942 | } | |
943 | ||
2cc6128a ID |
944 | static void __kick_linger_request(struct ceph_osd_request *req) |
945 | { | |
946 | struct ceph_osd_client *osdc = req->r_osdc; | |
947 | struct ceph_osd *osd = req->r_osd; | |
948 | ||
949 | /* | |
950 | * Linger requests need to be resent with a new tid to avoid | |
951 | * the dup op detection logic on the OSDs. Achieve this with | |
952 | * a re-register dance instead of open-coding. | |
953 | */ | |
954 | ceph_osdc_get_request(req); | |
955 | if (!list_empty(&req->r_linger_item)) | |
956 | __unregister_linger_request(osdc, req); | |
957 | else | |
958 | __unregister_request(osdc, req); | |
959 | __register_request(osdc, req); | |
960 | ceph_osdc_put_request(req); | |
961 | ||
962 | /* | |
963 | * Unless request has been registered as both normal and | |
964 | * lingering, __unregister{,_linger}_request clears r_osd. | |
965 | * However, here we need to preserve r_osd to make sure we | |
966 | * requeue on the same OSD. | |
967 | */ | |
968 | WARN_ON(req->r_osd || !osd); | |
969 | req->r_osd = osd; | |
970 | ||
971 | dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid); | |
972 | __enqueue_request(req); | |
973 | } | |
974 | ||
6f6c7006 SW |
975 | /* |
976 | * Resubmit requests pending on the given osd. | |
977 | */ | |
978 | static void __kick_osd_requests(struct ceph_osd_client *osdc, | |
979 | struct ceph_osd *osd) | |
980 | { | |
a40c4f10 | 981 | struct ceph_osd_request *req, *nreq; |
e02493c0 | 982 | LIST_HEAD(resend); |
2cc6128a | 983 | LIST_HEAD(resend_linger); |
6f6c7006 SW |
984 | int err; |
985 | ||
2cc6128a | 986 | dout("%s osd%d\n", __func__, osd->o_osd); |
6f6c7006 | 987 | err = __reset_osd(osdc, osd); |
685a7555 | 988 | if (err) |
6f6c7006 | 989 | return; |
2cc6128a | 990 | |
e02493c0 AE |
991 | /* |
992 | * Build up a list of requests to resend by traversing the | |
993 | * osd's list of requests. Requests for a given object are | |
994 | * sent in tid order, and that is also the order they're | |
995 | * kept on this list. Therefore all requests that are in | |
996 | * flight will be found first, followed by all requests that | |
997 | * have not yet been sent. And to resend requests while | |
998 | * preserving this order we will want to put any sent | |
999 | * requests back on the front of the osd client's unsent | |
1000 | * list. | |
1001 | * | |
1002 | * So we build a separate ordered list of already-sent | |
1003 | * requests for the affected osd and splice it onto the | |
1004 | * front of the osd client's unsent list. Once we've seen a | |
1005 | * request that has not yet been sent we're done. Those | |
1006 | * requests are already sitting right where they belong. | |
1007 | */ | |
6f6c7006 | 1008 | list_for_each_entry(req, &osd->o_requests, r_osd_item) { |
e02493c0 AE |
1009 | if (!req->r_sent) |
1010 | break; | |
2cc6128a ID |
1011 | |
1012 | if (!req->r_linger) { | |
1013 | dout("%s requeueing %p tid %llu\n", __func__, req, | |
1014 | req->r_tid); | |
1015 | list_move_tail(&req->r_req_lru_item, &resend); | |
a40c4f10 | 1016 | req->r_flags |= CEPH_OSD_FLAG_RETRY; |
2cc6128a ID |
1017 | } else { |
1018 | list_move_tail(&req->r_req_lru_item, &resend_linger); | |
1019 | } | |
a40c4f10 | 1020 | } |
e02493c0 | 1021 | list_splice(&resend, &osdc->req_unsent); |
a40c4f10 | 1022 | |
e02493c0 | 1023 | /* |
2cc6128a ID |
1024 | * Both registered and not yet registered linger requests are |
1025 | * enqueued with a new tid on the same OSD. We add/move them | |
1026 | * to req_unsent/o_requests at the end to keep things in tid | |
1027 | * order. | |
e02493c0 | 1028 | */ |
a40c4f10 | 1029 | list_for_each_entry_safe(req, nreq, &osd->o_linger_requests, |
1d0326b1 | 1030 | r_linger_osd_item) { |
2cc6128a ID |
1031 | WARN_ON(!list_empty(&req->r_req_lru_item)); |
1032 | __kick_linger_request(req); | |
6f6c7006 | 1033 | } |
2cc6128a ID |
1034 | |
1035 | list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item) | |
1036 | __kick_linger_request(req); | |
6f6c7006 SW |
1037 | } |
1038 | ||
f24e9980 | 1039 | /* |
81b024e7 | 1040 | * If the osd connection drops, we need to resubmit all requests. |
f24e9980 SW |
1041 | */ |
1042 | static void osd_reset(struct ceph_connection *con) | |
1043 | { | |
1044 | struct ceph_osd *osd = con->private; | |
1045 | struct ceph_osd_client *osdc; | |
1046 | ||
1047 | if (!osd) | |
1048 | return; | |
1049 | dout("osd_reset osd%d\n", osd->o_osd); | |
1050 | osdc = osd->o_osdc; | |
f24e9980 | 1051 | down_read(&osdc->map_sem); |
83aff95e SW |
1052 | mutex_lock(&osdc->request_mutex); |
1053 | __kick_osd_requests(osdc, osd); | |
f9d25199 | 1054 | __send_queued(osdc); |
83aff95e | 1055 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
1056 | up_read(&osdc->map_sem); |
1057 | } | |
1058 | ||
1059 | /* | |
1060 | * Track open sessions with osds. | |
1061 | */ | |
e10006f8 | 1062 | static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum) |
f24e9980 SW |
1063 | { |
1064 | struct ceph_osd *osd; | |
1065 | ||
1066 | osd = kzalloc(sizeof(*osd), GFP_NOFS); | |
1067 | if (!osd) | |
1068 | return NULL; | |
1069 | ||
1070 | atomic_set(&osd->o_ref, 1); | |
1071 | osd->o_osdc = osdc; | |
e10006f8 | 1072 | osd->o_osd = onum; |
f407731d | 1073 | RB_CLEAR_NODE(&osd->o_node); |
f24e9980 | 1074 | INIT_LIST_HEAD(&osd->o_requests); |
a40c4f10 | 1075 | INIT_LIST_HEAD(&osd->o_linger_requests); |
f5a2041b | 1076 | INIT_LIST_HEAD(&osd->o_osd_lru); |
f24e9980 SW |
1077 | osd->o_incarnation = 1; |
1078 | ||
b7a9e5dd | 1079 | ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr); |
4e7a5dcd | 1080 | |
422d2cb8 | 1081 | INIT_LIST_HEAD(&osd->o_keepalive_item); |
f24e9980 SW |
1082 | return osd; |
1083 | } | |
1084 | ||
1085 | static struct ceph_osd *get_osd(struct ceph_osd *osd) | |
1086 | { | |
1087 | if (atomic_inc_not_zero(&osd->o_ref)) { | |
1088 | dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1, | |
1089 | atomic_read(&osd->o_ref)); | |
1090 | return osd; | |
1091 | } else { | |
1092 | dout("get_osd %p FAIL\n", osd); | |
1093 | return NULL; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | static void put_osd(struct ceph_osd *osd) | |
1098 | { | |
1099 | dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref), | |
1100 | atomic_read(&osd->o_ref) - 1); | |
b28ec2f3 | 1101 | if (atomic_dec_and_test(&osd->o_ref)) { |
b28ec2f3 | 1102 | if (osd->o_auth.authorizer) |
6c1ea260 | 1103 | ceph_auth_destroy_authorizer(osd->o_auth.authorizer); |
f24e9980 | 1104 | kfree(osd); |
79494d1b | 1105 | } |
f24e9980 SW |
1106 | } |
1107 | ||
1108 | /* | |
1109 | * remove an osd from our map | |
1110 | */ | |
f5a2041b | 1111 | static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) |
f24e9980 | 1112 | { |
7eb71e03 | 1113 | dout("%s %p osd%d\n", __func__, osd, osd->o_osd); |
cc9f1f51 ID |
1114 | WARN_ON(!list_empty(&osd->o_requests)); |
1115 | WARN_ON(!list_empty(&osd->o_linger_requests)); | |
7c6e6fc5 | 1116 | |
f5a2041b | 1117 | list_del_init(&osd->o_osd_lru); |
7eb71e03 ID |
1118 | rb_erase(&osd->o_node, &osdc->osds); |
1119 | RB_CLEAR_NODE(&osd->o_node); | |
1120 | } | |
1121 | ||
1122 | static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) | |
1123 | { | |
1124 | dout("%s %p osd%d\n", __func__, osd, osd->o_osd); | |
1125 | ||
1126 | if (!RB_EMPTY_NODE(&osd->o_node)) { | |
1127 | ceph_con_close(&osd->o_con); | |
1128 | __remove_osd(osdc, osd); | |
1129 | put_osd(osd); | |
1130 | } | |
f24e9980 SW |
1131 | } |
1132 | ||
aca420bc SW |
1133 | static void remove_all_osds(struct ceph_osd_client *osdc) |
1134 | { | |
048a9d2d | 1135 | dout("%s %p\n", __func__, osdc); |
aca420bc SW |
1136 | mutex_lock(&osdc->request_mutex); |
1137 | while (!RB_EMPTY_ROOT(&osdc->osds)) { | |
1138 | struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds), | |
1139 | struct ceph_osd, o_node); | |
7eb71e03 | 1140 | remove_osd(osdc, osd); |
aca420bc SW |
1141 | } |
1142 | mutex_unlock(&osdc->request_mutex); | |
1143 | } | |
1144 | ||
f5a2041b YS |
1145 | static void __move_osd_to_lru(struct ceph_osd_client *osdc, |
1146 | struct ceph_osd *osd) | |
1147 | { | |
bbf37ec3 | 1148 | dout("%s %p\n", __func__, osd); |
f5a2041b | 1149 | BUG_ON(!list_empty(&osd->o_osd_lru)); |
bbf37ec3 | 1150 | |
f5a2041b | 1151 | list_add_tail(&osd->o_osd_lru, &osdc->osd_lru); |
a319bf56 | 1152 | osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl; |
f5a2041b YS |
1153 | } |
1154 | ||
bbf37ec3 ID |
1155 | static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc, |
1156 | struct ceph_osd *osd) | |
1157 | { | |
1158 | dout("%s %p\n", __func__, osd); | |
1159 | ||
1160 | if (list_empty(&osd->o_requests) && | |
1161 | list_empty(&osd->o_linger_requests)) | |
1162 | __move_osd_to_lru(osdc, osd); | |
1163 | } | |
1164 | ||
f5a2041b YS |
1165 | static void __remove_osd_from_lru(struct ceph_osd *osd) |
1166 | { | |
1167 | dout("__remove_osd_from_lru %p\n", osd); | |
1168 | if (!list_empty(&osd->o_osd_lru)) | |
1169 | list_del_init(&osd->o_osd_lru); | |
1170 | } | |
1171 | ||
aca420bc | 1172 | static void remove_old_osds(struct ceph_osd_client *osdc) |
f5a2041b YS |
1173 | { |
1174 | struct ceph_osd *osd, *nosd; | |
1175 | ||
1176 | dout("__remove_old_osds %p\n", osdc); | |
1177 | mutex_lock(&osdc->request_mutex); | |
1178 | list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) { | |
aca420bc | 1179 | if (time_before(jiffies, osd->lru_ttl)) |
f5a2041b | 1180 | break; |
7eb71e03 | 1181 | remove_osd(osdc, osd); |
f5a2041b YS |
1182 | } |
1183 | mutex_unlock(&osdc->request_mutex); | |
1184 | } | |
1185 | ||
f24e9980 SW |
1186 | /* |
1187 | * reset osd connect | |
1188 | */ | |
f5a2041b | 1189 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) |
f24e9980 | 1190 | { |
c3acb181 | 1191 | struct ceph_entity_addr *peer_addr; |
f24e9980 | 1192 | |
f5a2041b | 1193 | dout("__reset_osd %p osd%d\n", osd, osd->o_osd); |
a40c4f10 YS |
1194 | if (list_empty(&osd->o_requests) && |
1195 | list_empty(&osd->o_linger_requests)) { | |
7eb71e03 | 1196 | remove_osd(osdc, osd); |
c3acb181 AE |
1197 | return -ENODEV; |
1198 | } | |
1199 | ||
1200 | peer_addr = &osdc->osdmap->osd_addr[osd->o_osd]; | |
1201 | if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) && | |
1202 | !ceph_con_opened(&osd->o_con)) { | |
1203 | struct ceph_osd_request *req; | |
1204 | ||
0b4af2e8 ID |
1205 | dout("osd addr hasn't changed and connection never opened, " |
1206 | "letting msgr retry\n"); | |
87b315a5 SW |
1207 | /* touch each r_stamp for handle_timeout()'s benfit */ |
1208 | list_for_each_entry(req, &osd->o_requests, r_osd_item) | |
1209 | req->r_stamp = jiffies; | |
c3acb181 AE |
1210 | |
1211 | return -EAGAIN; | |
f24e9980 | 1212 | } |
c3acb181 AE |
1213 | |
1214 | ceph_con_close(&osd->o_con); | |
1215 | ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr); | |
1216 | osd->o_incarnation++; | |
1217 | ||
1218 | return 0; | |
f24e9980 SW |
1219 | } |
1220 | ||
1221 | static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new) | |
1222 | { | |
1223 | struct rb_node **p = &osdc->osds.rb_node; | |
1224 | struct rb_node *parent = NULL; | |
1225 | struct ceph_osd *osd = NULL; | |
1226 | ||
aca420bc | 1227 | dout("__insert_osd %p osd%d\n", new, new->o_osd); |
f24e9980 SW |
1228 | while (*p) { |
1229 | parent = *p; | |
1230 | osd = rb_entry(parent, struct ceph_osd, o_node); | |
1231 | if (new->o_osd < osd->o_osd) | |
1232 | p = &(*p)->rb_left; | |
1233 | else if (new->o_osd > osd->o_osd) | |
1234 | p = &(*p)->rb_right; | |
1235 | else | |
1236 | BUG(); | |
1237 | } | |
1238 | ||
1239 | rb_link_node(&new->o_node, parent, p); | |
1240 | rb_insert_color(&new->o_node, &osdc->osds); | |
1241 | } | |
1242 | ||
1243 | static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o) | |
1244 | { | |
1245 | struct ceph_osd *osd; | |
1246 | struct rb_node *n = osdc->osds.rb_node; | |
1247 | ||
1248 | while (n) { | |
1249 | osd = rb_entry(n, struct ceph_osd, o_node); | |
1250 | if (o < osd->o_osd) | |
1251 | n = n->rb_left; | |
1252 | else if (o > osd->o_osd) | |
1253 | n = n->rb_right; | |
1254 | else | |
1255 | return osd; | |
1256 | } | |
1257 | return NULL; | |
1258 | } | |
1259 | ||
422d2cb8 YS |
1260 | static void __schedule_osd_timeout(struct ceph_osd_client *osdc) |
1261 | { | |
1262 | schedule_delayed_work(&osdc->timeout_work, | |
a319bf56 | 1263 | osdc->client->options->osd_keepalive_timeout); |
422d2cb8 YS |
1264 | } |
1265 | ||
1266 | static void __cancel_osd_timeout(struct ceph_osd_client *osdc) | |
1267 | { | |
1268 | cancel_delayed_work(&osdc->timeout_work); | |
1269 | } | |
f24e9980 SW |
1270 | |
1271 | /* | |
1272 | * Register request, assign tid. If this is the first request, set up | |
1273 | * the timeout event. | |
1274 | */ | |
a40c4f10 YS |
1275 | static void __register_request(struct ceph_osd_client *osdc, |
1276 | struct ceph_osd_request *req) | |
f24e9980 | 1277 | { |
f24e9980 | 1278 | req->r_tid = ++osdc->last_tid; |
6df058c0 | 1279 | req->r_request->hdr.tid = cpu_to_le64(req->r_tid); |
77f38e0e | 1280 | dout("__register_request %p tid %lld\n", req, req->r_tid); |
f24e9980 SW |
1281 | __insert_request(osdc, req); |
1282 | ceph_osdc_get_request(req); | |
1283 | osdc->num_requests++; | |
f24e9980 | 1284 | if (osdc->num_requests == 1) { |
422d2cb8 YS |
1285 | dout(" first request, scheduling timeout\n"); |
1286 | __schedule_osd_timeout(osdc); | |
f24e9980 | 1287 | } |
a40c4f10 YS |
1288 | } |
1289 | ||
f24e9980 SW |
1290 | /* |
1291 | * called under osdc->request_mutex | |
1292 | */ | |
1293 | static void __unregister_request(struct ceph_osd_client *osdc, | |
1294 | struct ceph_osd_request *req) | |
1295 | { | |
35f9f8a0 SW |
1296 | if (RB_EMPTY_NODE(&req->r_node)) { |
1297 | dout("__unregister_request %p tid %lld not registered\n", | |
1298 | req, req->r_tid); | |
1299 | return; | |
1300 | } | |
1301 | ||
f24e9980 SW |
1302 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); |
1303 | rb_erase(&req->r_node, &osdc->requests); | |
6562d661 | 1304 | RB_CLEAR_NODE(&req->r_node); |
f24e9980 SW |
1305 | osdc->num_requests--; |
1306 | ||
0ba6478d SW |
1307 | if (req->r_osd) { |
1308 | /* make sure the original request isn't in flight. */ | |
6740a845 | 1309 | ceph_msg_revoke(req->r_request); |
0ba6478d SW |
1310 | |
1311 | list_del_init(&req->r_osd_item); | |
bbf37ec3 | 1312 | maybe_move_osd_to_lru(osdc, req->r_osd); |
4f23409e | 1313 | if (list_empty(&req->r_linger_osd_item)) |
a40c4f10 | 1314 | req->r_osd = NULL; |
0ba6478d | 1315 | } |
f24e9980 | 1316 | |
7d5f2481 | 1317 | list_del_init(&req->r_req_lru_item); |
f24e9980 SW |
1318 | ceph_osdc_put_request(req); |
1319 | ||
422d2cb8 YS |
1320 | if (osdc->num_requests == 0) { |
1321 | dout(" no requests, canceling timeout\n"); | |
1322 | __cancel_osd_timeout(osdc); | |
f24e9980 SW |
1323 | } |
1324 | } | |
1325 | ||
1326 | /* | |
1327 | * Cancel a previously queued request message | |
1328 | */ | |
1329 | static void __cancel_request(struct ceph_osd_request *req) | |
1330 | { | |
6bc18876 | 1331 | if (req->r_sent && req->r_osd) { |
6740a845 | 1332 | ceph_msg_revoke(req->r_request); |
f24e9980 SW |
1333 | req->r_sent = 0; |
1334 | } | |
1335 | } | |
1336 | ||
a40c4f10 YS |
1337 | static void __register_linger_request(struct ceph_osd_client *osdc, |
1338 | struct ceph_osd_request *req) | |
1339 | { | |
af593064 ID |
1340 | dout("%s %p tid %llu\n", __func__, req, req->r_tid); |
1341 | WARN_ON(!req->r_linger); | |
1342 | ||
96e4dac6 | 1343 | ceph_osdc_get_request(req); |
a40c4f10 | 1344 | list_add_tail(&req->r_linger_item, &osdc->req_linger); |
6194ea89 | 1345 | if (req->r_osd) |
1d0326b1 | 1346 | list_add_tail(&req->r_linger_osd_item, |
6194ea89 | 1347 | &req->r_osd->o_linger_requests); |
a40c4f10 YS |
1348 | } |
1349 | ||
1350 | static void __unregister_linger_request(struct ceph_osd_client *osdc, | |
1351 | struct ceph_osd_request *req) | |
1352 | { | |
af593064 ID |
1353 | WARN_ON(!req->r_linger); |
1354 | ||
1355 | if (list_empty(&req->r_linger_item)) { | |
1356 | dout("%s %p tid %llu not registered\n", __func__, req, | |
1357 | req->r_tid); | |
1358 | return; | |
1359 | } | |
1360 | ||
1361 | dout("%s %p tid %llu\n", __func__, req, req->r_tid); | |
61c74035 | 1362 | list_del_init(&req->r_linger_item); |
af593064 | 1363 | |
a40c4f10 | 1364 | if (req->r_osd) { |
1d0326b1 | 1365 | list_del_init(&req->r_linger_osd_item); |
bbf37ec3 | 1366 | maybe_move_osd_to_lru(osdc, req->r_osd); |
fbdb9190 SW |
1367 | if (list_empty(&req->r_osd_item)) |
1368 | req->r_osd = NULL; | |
a40c4f10 | 1369 | } |
96e4dac6 | 1370 | ceph_osdc_put_request(req); |
a40c4f10 YS |
1371 | } |
1372 | ||
a40c4f10 YS |
1373 | void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc, |
1374 | struct ceph_osd_request *req) | |
1375 | { | |
1376 | if (!req->r_linger) { | |
1377 | dout("set_request_linger %p\n", req); | |
1378 | req->r_linger = 1; | |
a40c4f10 YS |
1379 | } |
1380 | } | |
1381 | EXPORT_SYMBOL(ceph_osdc_set_request_linger); | |
1382 | ||
d29adb34 JD |
1383 | /* |
1384 | * Returns whether a request should be blocked from being sent | |
1385 | * based on the current osdmap and osd_client settings. | |
1386 | * | |
1387 | * Caller should hold map_sem for read. | |
1388 | */ | |
1389 | static bool __req_should_be_paused(struct ceph_osd_client *osdc, | |
1390 | struct ceph_osd_request *req) | |
1391 | { | |
1392 | bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD); | |
1393 | bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) || | |
1394 | ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL); | |
1395 | return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) || | |
1396 | (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr); | |
1397 | } | |
1398 | ||
17a13e40 ID |
1399 | /* |
1400 | * Calculate mapping of a request to a PG. Takes tiering into account. | |
1401 | */ | |
1402 | static int __calc_request_pg(struct ceph_osdmap *osdmap, | |
1403 | struct ceph_osd_request *req, | |
1404 | struct ceph_pg *pg_out) | |
1405 | { | |
205ee118 ID |
1406 | bool need_check_tiering; |
1407 | ||
1408 | need_check_tiering = false; | |
1409 | if (req->r_target_oloc.pool == -1) { | |
1410 | req->r_target_oloc = req->r_base_oloc; /* struct */ | |
1411 | need_check_tiering = true; | |
1412 | } | |
1413 | if (req->r_target_oid.name_len == 0) { | |
1414 | ceph_oid_copy(&req->r_target_oid, &req->r_base_oid); | |
1415 | need_check_tiering = true; | |
1416 | } | |
1417 | ||
1418 | if (need_check_tiering && | |
1419 | (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) { | |
17a13e40 ID |
1420 | struct ceph_pg_pool_info *pi; |
1421 | ||
205ee118 | 1422 | pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool); |
17a13e40 ID |
1423 | if (pi) { |
1424 | if ((req->r_flags & CEPH_OSD_FLAG_READ) && | |
1425 | pi->read_tier >= 0) | |
205ee118 | 1426 | req->r_target_oloc.pool = pi->read_tier; |
17a13e40 ID |
1427 | if ((req->r_flags & CEPH_OSD_FLAG_WRITE) && |
1428 | pi->write_tier >= 0) | |
205ee118 | 1429 | req->r_target_oloc.pool = pi->write_tier; |
17a13e40 ID |
1430 | } |
1431 | /* !pi is caught in ceph_oloc_oid_to_pg() */ | |
1432 | } | |
1433 | ||
205ee118 ID |
1434 | return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc, |
1435 | &req->r_target_oid, pg_out); | |
17a13e40 ID |
1436 | } |
1437 | ||
f671b581 ID |
1438 | static void __enqueue_request(struct ceph_osd_request *req) |
1439 | { | |
1440 | struct ceph_osd_client *osdc = req->r_osdc; | |
1441 | ||
1442 | dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid, | |
1443 | req->r_osd ? req->r_osd->o_osd : -1); | |
1444 | ||
1445 | if (req->r_osd) { | |
1446 | __remove_osd_from_lru(req->r_osd); | |
1447 | list_add_tail(&req->r_osd_item, &req->r_osd->o_requests); | |
1448 | list_move_tail(&req->r_req_lru_item, &osdc->req_unsent); | |
1449 | } else { | |
1450 | list_move_tail(&req->r_req_lru_item, &osdc->req_notarget); | |
1451 | } | |
1452 | } | |
1453 | ||
f24e9980 SW |
1454 | /* |
1455 | * Pick an osd (the first 'up' osd in the pg), allocate the osd struct | |
1456 | * (as needed), and set the request r_osd appropriately. If there is | |
25985edc | 1457 | * no up osd, set r_osd to NULL. Move the request to the appropriate list |
6f6c7006 | 1458 | * (unsent, homeless) or leave on in-flight lru. |
f24e9980 SW |
1459 | * |
1460 | * Return 0 if unchanged, 1 if changed, or negative on error. | |
1461 | * | |
1462 | * Caller should hold map_sem for read and request_mutex. | |
1463 | */ | |
6f6c7006 | 1464 | static int __map_request(struct ceph_osd_client *osdc, |
38d6453c | 1465 | struct ceph_osd_request *req, int force_resend) |
f24e9980 | 1466 | { |
5b191d99 | 1467 | struct ceph_pg pgid; |
d85b7056 | 1468 | int acting[CEPH_PG_MAX_SIZE]; |
8008ab10 | 1469 | int num, o; |
f24e9980 | 1470 | int err; |
d29adb34 | 1471 | bool was_paused; |
f24e9980 | 1472 | |
6f6c7006 | 1473 | dout("map_request %p tid %lld\n", req, req->r_tid); |
17a13e40 ID |
1474 | |
1475 | err = __calc_request_pg(osdc->osdmap, req, &pgid); | |
6f6c7006 SW |
1476 | if (err) { |
1477 | list_move(&req->r_req_lru_item, &osdc->req_notarget); | |
f24e9980 | 1478 | return err; |
6f6c7006 | 1479 | } |
7740a42f SW |
1480 | req->r_pgid = pgid; |
1481 | ||
8008ab10 ID |
1482 | num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o); |
1483 | if (num < 0) | |
1484 | num = 0; | |
f24e9980 | 1485 | |
d29adb34 JD |
1486 | was_paused = req->r_paused; |
1487 | req->r_paused = __req_should_be_paused(osdc, req); | |
1488 | if (was_paused && !req->r_paused) | |
1489 | force_resend = 1; | |
1490 | ||
38d6453c SW |
1491 | if ((!force_resend && |
1492 | req->r_osd && req->r_osd->o_osd == o && | |
d85b7056 SW |
1493 | req->r_sent >= req->r_osd->o_incarnation && |
1494 | req->r_num_pg_osds == num && | |
1495 | memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) || | |
d29adb34 JD |
1496 | (req->r_osd == NULL && o == -1) || |
1497 | req->r_paused) | |
f24e9980 SW |
1498 | return 0; /* no change */ |
1499 | ||
5b191d99 SW |
1500 | dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n", |
1501 | req->r_tid, pgid.pool, pgid.seed, o, | |
f24e9980 SW |
1502 | req->r_osd ? req->r_osd->o_osd : -1); |
1503 | ||
d85b7056 SW |
1504 | /* record full pg acting set */ |
1505 | memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num); | |
1506 | req->r_num_pg_osds = num; | |
1507 | ||
f24e9980 SW |
1508 | if (req->r_osd) { |
1509 | __cancel_request(req); | |
1510 | list_del_init(&req->r_osd_item); | |
a390de02 | 1511 | list_del_init(&req->r_linger_osd_item); |
f24e9980 SW |
1512 | req->r_osd = NULL; |
1513 | } | |
1514 | ||
1515 | req->r_osd = __lookup_osd(osdc, o); | |
1516 | if (!req->r_osd && o >= 0) { | |
c99eb1c7 | 1517 | err = -ENOMEM; |
e10006f8 | 1518 | req->r_osd = create_osd(osdc, o); |
6f6c7006 SW |
1519 | if (!req->r_osd) { |
1520 | list_move(&req->r_req_lru_item, &osdc->req_notarget); | |
c99eb1c7 | 1521 | goto out; |
6f6c7006 | 1522 | } |
f24e9980 | 1523 | |
6f6c7006 | 1524 | dout("map_request osd %p is osd%d\n", req->r_osd, o); |
f24e9980 SW |
1525 | __insert_osd(osdc, req->r_osd); |
1526 | ||
b7a9e5dd SW |
1527 | ceph_con_open(&req->r_osd->o_con, |
1528 | CEPH_ENTITY_TYPE_OSD, o, | |
1529 | &osdc->osdmap->osd_addr[o]); | |
f24e9980 SW |
1530 | } |
1531 | ||
f671b581 | 1532 | __enqueue_request(req); |
d85b7056 | 1533 | err = 1; /* osd or pg changed */ |
f24e9980 SW |
1534 | |
1535 | out: | |
f24e9980 SW |
1536 | return err; |
1537 | } | |
1538 | ||
1539 | /* | |
1540 | * caller should hold map_sem (for read) and request_mutex | |
1541 | */ | |
56e925b6 SW |
1542 | static void __send_request(struct ceph_osd_client *osdc, |
1543 | struct ceph_osd_request *req) | |
f24e9980 | 1544 | { |
1b83bef2 | 1545 | void *p; |
f24e9980 | 1546 | |
1b83bef2 SW |
1547 | dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n", |
1548 | req, req->r_tid, req->r_osd->o_osd, req->r_flags, | |
1549 | (unsigned long long)req->r_pgid.pool, req->r_pgid.seed); | |
1550 | ||
1551 | /* fill in message content that changes each time we send it */ | |
1552 | put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch); | |
1553 | put_unaligned_le32(req->r_flags, req->r_request_flags); | |
205ee118 | 1554 | put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool); |
1b83bef2 SW |
1555 | p = req->r_request_pgid; |
1556 | ceph_encode_64(&p, req->r_pgid.pool); | |
1557 | ceph_encode_32(&p, req->r_pgid.seed); | |
1558 | put_unaligned_le64(1, req->r_request_attempts); /* FIXME */ | |
1559 | memcpy(req->r_request_reassert_version, &req->r_reassert_version, | |
1560 | sizeof(req->r_reassert_version)); | |
2169aea6 | 1561 | |
3dd72fc0 | 1562 | req->r_stamp = jiffies; |
07a27e22 | 1563 | list_move_tail(&req->r_req_lru_item, &osdc->req_lru); |
f24e9980 SW |
1564 | |
1565 | ceph_msg_get(req->r_request); /* send consumes a ref */ | |
26be8808 | 1566 | |
f24e9980 | 1567 | req->r_sent = req->r_osd->o_incarnation; |
26be8808 AE |
1568 | |
1569 | ceph_con_send(&req->r_osd->o_con, req->r_request); | |
f24e9980 SW |
1570 | } |
1571 | ||
6f6c7006 SW |
1572 | /* |
1573 | * Send any requests in the queue (req_unsent). | |
1574 | */ | |
f9d25199 | 1575 | static void __send_queued(struct ceph_osd_client *osdc) |
6f6c7006 SW |
1576 | { |
1577 | struct ceph_osd_request *req, *tmp; | |
1578 | ||
f9d25199 AE |
1579 | dout("__send_queued\n"); |
1580 | list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) | |
6f6c7006 | 1581 | __send_request(osdc, req); |
6f6c7006 SW |
1582 | } |
1583 | ||
0bbfdfe8 ID |
1584 | /* |
1585 | * Caller should hold map_sem for read and request_mutex. | |
1586 | */ | |
1587 | static int __ceph_osdc_start_request(struct ceph_osd_client *osdc, | |
1588 | struct ceph_osd_request *req, | |
1589 | bool nofail) | |
1590 | { | |
1591 | int rc; | |
1592 | ||
1593 | __register_request(osdc, req); | |
1594 | req->r_sent = 0; | |
1595 | req->r_got_reply = 0; | |
1596 | rc = __map_request(osdc, req, 0); | |
1597 | if (rc < 0) { | |
1598 | if (nofail) { | |
1599 | dout("osdc_start_request failed map, " | |
1600 | " will retry %lld\n", req->r_tid); | |
1601 | rc = 0; | |
1602 | } else { | |
1603 | __unregister_request(osdc, req); | |
1604 | } | |
1605 | return rc; | |
1606 | } | |
1607 | ||
1608 | if (req->r_osd == NULL) { | |
1609 | dout("send_request %p no up osds in pg\n", req); | |
1610 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1611 | } else { | |
1612 | __send_queued(osdc); | |
1613 | } | |
1614 | ||
1615 | return 0; | |
1616 | } | |
1617 | ||
f24e9980 SW |
1618 | /* |
1619 | * Timeout callback, called every N seconds when 1 or more osd | |
1620 | * requests has been active for more than N seconds. When this | |
1621 | * happens, we ping all OSDs with requests who have timed out to | |
1622 | * ensure any communications channel reset is detected. Reset the | |
1623 | * request timeouts another N seconds in the future as we go. | |
1624 | * Reschedule the timeout event another N seconds in future (unless | |
1625 | * there are no open requests). | |
1626 | */ | |
1627 | static void handle_timeout(struct work_struct *work) | |
1628 | { | |
1629 | struct ceph_osd_client *osdc = | |
1630 | container_of(work, struct ceph_osd_client, timeout_work.work); | |
a319bf56 | 1631 | struct ceph_options *opts = osdc->client->options; |
83aff95e | 1632 | struct ceph_osd_request *req; |
f24e9980 | 1633 | struct ceph_osd *osd; |
422d2cb8 | 1634 | struct list_head slow_osds; |
f24e9980 SW |
1635 | dout("timeout\n"); |
1636 | down_read(&osdc->map_sem); | |
1637 | ||
1638 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1639 | ||
1640 | mutex_lock(&osdc->request_mutex); | |
f24e9980 | 1641 | |
422d2cb8 YS |
1642 | /* |
1643 | * ping osds that are a bit slow. this ensures that if there | |
1644 | * is a break in the TCP connection we will notice, and reopen | |
1645 | * a connection with that osd (from the fault callback). | |
1646 | */ | |
1647 | INIT_LIST_HEAD(&slow_osds); | |
1648 | list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) { | |
a319bf56 ID |
1649 | if (time_before(jiffies, |
1650 | req->r_stamp + opts->osd_keepalive_timeout)) | |
422d2cb8 YS |
1651 | break; |
1652 | ||
1653 | osd = req->r_osd; | |
1654 | BUG_ON(!osd); | |
1655 | dout(" tid %llu is slow, will send keepalive on osd%d\n", | |
f24e9980 | 1656 | req->r_tid, osd->o_osd); |
422d2cb8 YS |
1657 | list_move_tail(&osd->o_keepalive_item, &slow_osds); |
1658 | } | |
1659 | while (!list_empty(&slow_osds)) { | |
1660 | osd = list_entry(slow_osds.next, struct ceph_osd, | |
1661 | o_keepalive_item); | |
1662 | list_del_init(&osd->o_keepalive_item); | |
f24e9980 SW |
1663 | ceph_con_keepalive(&osd->o_con); |
1664 | } | |
1665 | ||
422d2cb8 | 1666 | __schedule_osd_timeout(osdc); |
f9d25199 | 1667 | __send_queued(osdc); |
f24e9980 | 1668 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
1669 | up_read(&osdc->map_sem); |
1670 | } | |
1671 | ||
f5a2041b YS |
1672 | static void handle_osds_timeout(struct work_struct *work) |
1673 | { | |
1674 | struct ceph_osd_client *osdc = | |
1675 | container_of(work, struct ceph_osd_client, | |
1676 | osds_timeout_work.work); | |
a319bf56 | 1677 | unsigned long delay = osdc->client->options->osd_idle_ttl / 4; |
f5a2041b YS |
1678 | |
1679 | dout("osds timeout\n"); | |
1680 | down_read(&osdc->map_sem); | |
aca420bc | 1681 | remove_old_osds(osdc); |
f5a2041b YS |
1682 | up_read(&osdc->map_sem); |
1683 | ||
1684 | schedule_delayed_work(&osdc->osds_timeout_work, | |
1685 | round_jiffies_relative(delay)); | |
1686 | } | |
1687 | ||
205ee118 ID |
1688 | static int ceph_oloc_decode(void **p, void *end, |
1689 | struct ceph_object_locator *oloc) | |
1690 | { | |
1691 | u8 struct_v, struct_cv; | |
1692 | u32 len; | |
1693 | void *struct_end; | |
1694 | int ret = 0; | |
1695 | ||
1696 | ceph_decode_need(p, end, 1 + 1 + 4, e_inval); | |
1697 | struct_v = ceph_decode_8(p); | |
1698 | struct_cv = ceph_decode_8(p); | |
1699 | if (struct_v < 3) { | |
1700 | pr_warn("got v %d < 3 cv %d of ceph_object_locator\n", | |
1701 | struct_v, struct_cv); | |
1702 | goto e_inval; | |
1703 | } | |
1704 | if (struct_cv > 6) { | |
1705 | pr_warn("got v %d cv %d > 6 of ceph_object_locator\n", | |
1706 | struct_v, struct_cv); | |
1707 | goto e_inval; | |
1708 | } | |
1709 | len = ceph_decode_32(p); | |
1710 | ceph_decode_need(p, end, len, e_inval); | |
1711 | struct_end = *p + len; | |
1712 | ||
1713 | oloc->pool = ceph_decode_64(p); | |
1714 | *p += 4; /* skip preferred */ | |
1715 | ||
1716 | len = ceph_decode_32(p); | |
1717 | if (len > 0) { | |
1718 | pr_warn("ceph_object_locator::key is set\n"); | |
1719 | goto e_inval; | |
1720 | } | |
1721 | ||
1722 | if (struct_v >= 5) { | |
1723 | len = ceph_decode_32(p); | |
1724 | if (len > 0) { | |
1725 | pr_warn("ceph_object_locator::nspace is set\n"); | |
1726 | goto e_inval; | |
1727 | } | |
1728 | } | |
1729 | ||
1730 | if (struct_v >= 6) { | |
1731 | s64 hash = ceph_decode_64(p); | |
1732 | if (hash != -1) { | |
1733 | pr_warn("ceph_object_locator::hash is set\n"); | |
1734 | goto e_inval; | |
1735 | } | |
1736 | } | |
1737 | ||
1738 | /* skip the rest */ | |
1739 | *p = struct_end; | |
1740 | out: | |
1741 | return ret; | |
1742 | ||
1743 | e_inval: | |
1744 | ret = -EINVAL; | |
1745 | goto out; | |
1746 | } | |
1747 | ||
1748 | static int ceph_redirect_decode(void **p, void *end, | |
1749 | struct ceph_request_redirect *redir) | |
1750 | { | |
1751 | u8 struct_v, struct_cv; | |
1752 | u32 len; | |
1753 | void *struct_end; | |
1754 | int ret; | |
1755 | ||
1756 | ceph_decode_need(p, end, 1 + 1 + 4, e_inval); | |
1757 | struct_v = ceph_decode_8(p); | |
1758 | struct_cv = ceph_decode_8(p); | |
1759 | if (struct_cv > 1) { | |
1760 | pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n", | |
1761 | struct_v, struct_cv); | |
1762 | goto e_inval; | |
1763 | } | |
1764 | len = ceph_decode_32(p); | |
1765 | ceph_decode_need(p, end, len, e_inval); | |
1766 | struct_end = *p + len; | |
1767 | ||
1768 | ret = ceph_oloc_decode(p, end, &redir->oloc); | |
1769 | if (ret) | |
1770 | goto out; | |
1771 | ||
1772 | len = ceph_decode_32(p); | |
1773 | if (len > 0) { | |
1774 | pr_warn("ceph_request_redirect::object_name is set\n"); | |
1775 | goto e_inval; | |
1776 | } | |
1777 | ||
1778 | len = ceph_decode_32(p); | |
1779 | *p += len; /* skip osd_instructions */ | |
1780 | ||
1781 | /* skip the rest */ | |
1782 | *p = struct_end; | |
1783 | out: | |
1784 | return ret; | |
1785 | ||
1786 | e_inval: | |
1787 | ret = -EINVAL; | |
1788 | goto out; | |
1789 | } | |
1790 | ||
25845472 SW |
1791 | static void complete_request(struct ceph_osd_request *req) |
1792 | { | |
25845472 SW |
1793 | complete_all(&req->r_safe_completion); /* fsync waiter */ |
1794 | } | |
1795 | ||
f24e9980 SW |
1796 | /* |
1797 | * handle osd op reply. either call the callback if it is specified, | |
1798 | * or do the completion to wake up the waiting thread. | |
1799 | */ | |
70cf052d | 1800 | static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg) |
f24e9980 | 1801 | { |
1b83bef2 | 1802 | void *p, *end; |
f24e9980 | 1803 | struct ceph_osd_request *req; |
205ee118 | 1804 | struct ceph_request_redirect redir; |
f24e9980 | 1805 | u64 tid; |
1b83bef2 | 1806 | int object_len; |
79528734 AE |
1807 | unsigned int numops; |
1808 | int payload_len, flags; | |
0ceed5db | 1809 | s32 result; |
1b83bef2 SW |
1810 | s32 retry_attempt; |
1811 | struct ceph_pg pg; | |
1812 | int err; | |
1813 | u32 reassert_epoch; | |
1814 | u64 reassert_version; | |
1815 | u32 osdmap_epoch; | |
0d5af164 | 1816 | int already_completed; |
9fc6e064 | 1817 | u32 bytes; |
b0b31a8f | 1818 | u8 decode_redir; |
79528734 | 1819 | unsigned int i; |
f24e9980 | 1820 | |
6df058c0 | 1821 | tid = le64_to_cpu(msg->hdr.tid); |
1b83bef2 SW |
1822 | dout("handle_reply %p tid %llu\n", msg, tid); |
1823 | ||
1824 | p = msg->front.iov_base; | |
1825 | end = p + msg->front.iov_len; | |
1826 | ||
1827 | ceph_decode_need(&p, end, 4, bad); | |
1828 | object_len = ceph_decode_32(&p); | |
1829 | ceph_decode_need(&p, end, object_len, bad); | |
1830 | p += object_len; | |
1831 | ||
ef4859d6 | 1832 | err = ceph_decode_pgid(&p, end, &pg); |
1b83bef2 | 1833 | if (err) |
f24e9980 | 1834 | goto bad; |
1b83bef2 SW |
1835 | |
1836 | ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad); | |
1837 | flags = ceph_decode_64(&p); | |
1838 | result = ceph_decode_32(&p); | |
1839 | reassert_epoch = ceph_decode_32(&p); | |
1840 | reassert_version = ceph_decode_64(&p); | |
1841 | osdmap_epoch = ceph_decode_32(&p); | |
1842 | ||
f24e9980 | 1843 | /* lookup */ |
ff513ace | 1844 | down_read(&osdc->map_sem); |
f24e9980 SW |
1845 | mutex_lock(&osdc->request_mutex); |
1846 | req = __lookup_request(osdc, tid); | |
1847 | if (req == NULL) { | |
1848 | dout("handle_reply tid %llu dne\n", tid); | |
8058fd45 | 1849 | goto bad_mutex; |
f24e9980 SW |
1850 | } |
1851 | ceph_osdc_get_request(req); | |
1b83bef2 SW |
1852 | |
1853 | dout("handle_reply %p tid %llu req %p result %d\n", msg, tid, | |
1854 | req, result); | |
1855 | ||
18741196 | 1856 | ceph_decode_need(&p, end, 4, bad_put); |
1b83bef2 | 1857 | numops = ceph_decode_32(&p); |
3f1af42a | 1858 | if (numops > CEPH_OSD_MAX_OPS) |
1b83bef2 SW |
1859 | goto bad_put; |
1860 | if (numops != req->r_num_ops) | |
1861 | goto bad_put; | |
1862 | payload_len = 0; | |
18741196 | 1863 | ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put); |
1b83bef2 SW |
1864 | for (i = 0; i < numops; i++) { |
1865 | struct ceph_osd_op *op = p; | |
1866 | int len; | |
1867 | ||
1868 | len = le32_to_cpu(op->payload_len); | |
7665d85b | 1869 | req->r_ops[i].outdata_len = len; |
1b83bef2 SW |
1870 | dout(" op %d has %d bytes\n", i, len); |
1871 | payload_len += len; | |
1872 | p += sizeof(*op); | |
1873 | } | |
9fc6e064 AE |
1874 | bytes = le32_to_cpu(msg->hdr.data_len); |
1875 | if (payload_len != bytes) { | |
b9a67899 JP |
1876 | pr_warn("sum of op payload lens %d != data_len %d\n", |
1877 | payload_len, bytes); | |
1b83bef2 SW |
1878 | goto bad_put; |
1879 | } | |
1880 | ||
18741196 | 1881 | ceph_decode_need(&p, end, 4 + numops * 4, bad_put); |
1b83bef2 SW |
1882 | retry_attempt = ceph_decode_32(&p); |
1883 | for (i = 0; i < numops; i++) | |
7665d85b | 1884 | req->r_ops[i].rval = ceph_decode_32(&p); |
f24e9980 | 1885 | |
205ee118 ID |
1886 | if (le16_to_cpu(msg->hdr.version) >= 6) { |
1887 | p += 8 + 4; /* skip replay_version */ | |
1888 | p += 8; /* skip user_version */ | |
eb845ff1 | 1889 | |
b0b31a8f ID |
1890 | if (le16_to_cpu(msg->hdr.version) >= 7) |
1891 | ceph_decode_8_safe(&p, end, decode_redir, bad_put); | |
1892 | else | |
1893 | decode_redir = 1; | |
1894 | } else { | |
1895 | decode_redir = 0; | |
1896 | } | |
1897 | ||
1898 | if (decode_redir) { | |
205ee118 ID |
1899 | err = ceph_redirect_decode(&p, end, &redir); |
1900 | if (err) | |
1901 | goto bad_put; | |
1902 | } else { | |
1903 | redir.oloc.pool = -1; | |
1904 | } | |
f24e9980 | 1905 | |
205ee118 ID |
1906 | if (redir.oloc.pool != -1) { |
1907 | dout("redirect pool %lld\n", redir.oloc.pool); | |
1908 | ||
1909 | __unregister_request(osdc, req); | |
205ee118 ID |
1910 | |
1911 | req->r_target_oloc = redir.oloc; /* struct */ | |
1912 | ||
1913 | /* | |
1914 | * Start redirect requests with nofail=true. If | |
1915 | * mapping fails, request will end up on the notarget | |
1916 | * list, waiting for the new osdmap (which can take | |
1917 | * a while), even though the original request mapped | |
1918 | * successfully. In the future we might want to follow | |
1919 | * original request's nofail setting here. | |
1920 | */ | |
ff513ace | 1921 | err = __ceph_osdc_start_request(osdc, req, true); |
205ee118 ID |
1922 | BUG_ON(err); |
1923 | ||
ff513ace | 1924 | goto out_unlock; |
205ee118 ID |
1925 | } |
1926 | ||
1927 | already_completed = req->r_got_reply; | |
1928 | if (!req->r_got_reply) { | |
1b83bef2 | 1929 | req->r_result = result; |
f24e9980 SW |
1930 | dout("handle_reply result %d bytes %d\n", req->r_result, |
1931 | bytes); | |
1932 | if (req->r_result == 0) | |
1933 | req->r_result = bytes; | |
1934 | ||
1935 | /* in case this is a write and we need to replay, */ | |
1b83bef2 SW |
1936 | req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch); |
1937 | req->r_reassert_version.version = cpu_to_le64(reassert_version); | |
f24e9980 SW |
1938 | |
1939 | req->r_got_reply = 1; | |
1940 | } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) { | |
1941 | dout("handle_reply tid %llu dup ack\n", tid); | |
ff513ace | 1942 | goto out_unlock; |
f24e9980 SW |
1943 | } |
1944 | ||
1945 | dout("handle_reply tid %llu flags %d\n", tid, flags); | |
1946 | ||
a40c4f10 YS |
1947 | if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK)) |
1948 | __register_linger_request(osdc, req); | |
1949 | ||
f24e9980 | 1950 | /* either this is a read, or we got the safe response */ |
0ceed5db SW |
1951 | if (result < 0 || |
1952 | (flags & CEPH_OSD_FLAG_ONDISK) || | |
f24e9980 SW |
1953 | ((flags & CEPH_OSD_FLAG_WRITE) == 0)) |
1954 | __unregister_request(osdc, req); | |
1955 | ||
1956 | mutex_unlock(&osdc->request_mutex); | |
ff513ace | 1957 | up_read(&osdc->map_sem); |
f24e9980 | 1958 | |
eb845ff1 | 1959 | if (!already_completed) { |
61c5d6bf YZ |
1960 | if (req->r_unsafe_callback && |
1961 | result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK)) | |
1962 | req->r_unsafe_callback(req, true); | |
eb845ff1 YZ |
1963 | if (req->r_callback) |
1964 | req->r_callback(req, msg); | |
1965 | else | |
1966 | complete_all(&req->r_completion); | |
1967 | } | |
f24e9980 | 1968 | |
61c5d6bf YZ |
1969 | if (flags & CEPH_OSD_FLAG_ONDISK) { |
1970 | if (req->r_unsafe_callback && already_completed) | |
1971 | req->r_unsafe_callback(req, false); | |
25845472 | 1972 | complete_request(req); |
61c5d6bf | 1973 | } |
f24e9980 | 1974 | |
ff513ace | 1975 | out: |
a40c4f10 | 1976 | dout("req=%p req->r_linger=%d\n", req, req->r_linger); |
f24e9980 SW |
1977 | ceph_osdc_put_request(req); |
1978 | return; | |
ff513ace ID |
1979 | out_unlock: |
1980 | mutex_unlock(&osdc->request_mutex); | |
1981 | up_read(&osdc->map_sem); | |
1982 | goto out; | |
f24e9980 | 1983 | |
1b83bef2 | 1984 | bad_put: |
37c89bde LW |
1985 | req->r_result = -EIO; |
1986 | __unregister_request(osdc, req); | |
1987 | if (req->r_callback) | |
1988 | req->r_callback(req, msg); | |
1989 | else | |
1990 | complete_all(&req->r_completion); | |
1991 | complete_request(req); | |
1b83bef2 | 1992 | ceph_osdc_put_request(req); |
8058fd45 AE |
1993 | bad_mutex: |
1994 | mutex_unlock(&osdc->request_mutex); | |
ff513ace | 1995 | up_read(&osdc->map_sem); |
f24e9980 | 1996 | bad: |
1b83bef2 SW |
1997 | pr_err("corrupt osd_op_reply got %d %d\n", |
1998 | (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len)); | |
9ec7cab1 | 1999 | ceph_msg_dump(msg); |
f24e9980 SW |
2000 | } |
2001 | ||
6f6c7006 | 2002 | static void reset_changed_osds(struct ceph_osd_client *osdc) |
f24e9980 | 2003 | { |
f24e9980 | 2004 | struct rb_node *p, *n; |
f24e9980 | 2005 | |
7eb71e03 | 2006 | dout("%s %p\n", __func__, osdc); |
6f6c7006 SW |
2007 | for (p = rb_first(&osdc->osds); p; p = n) { |
2008 | struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node); | |
f24e9980 | 2009 | |
6f6c7006 SW |
2010 | n = rb_next(p); |
2011 | if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || | |
2012 | memcmp(&osd->o_con.peer_addr, | |
2013 | ceph_osd_addr(osdc->osdmap, | |
2014 | osd->o_osd), | |
2015 | sizeof(struct ceph_entity_addr)) != 0) | |
2016 | __reset_osd(osdc, osd); | |
f24e9980 | 2017 | } |
422d2cb8 YS |
2018 | } |
2019 | ||
2020 | /* | |
6f6c7006 SW |
2021 | * Requeue requests whose mapping to an OSD has changed. If requests map to |
2022 | * no osd, request a new map. | |
422d2cb8 | 2023 | * |
e6d50f67 | 2024 | * Caller should hold map_sem for read. |
422d2cb8 | 2025 | */ |
9a1ea2db JD |
2026 | static void kick_requests(struct ceph_osd_client *osdc, bool force_resend, |
2027 | bool force_resend_writes) | |
422d2cb8 | 2028 | { |
a40c4f10 | 2029 | struct ceph_osd_request *req, *nreq; |
6f6c7006 SW |
2030 | struct rb_node *p; |
2031 | int needmap = 0; | |
2032 | int err; | |
9a1ea2db | 2033 | bool force_resend_req; |
422d2cb8 | 2034 | |
9a1ea2db JD |
2035 | dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "", |
2036 | force_resend_writes ? " (force resend writes)" : ""); | |
422d2cb8 | 2037 | mutex_lock(&osdc->request_mutex); |
6194ea89 | 2038 | for (p = rb_first(&osdc->requests); p; ) { |
6f6c7006 | 2039 | req = rb_entry(p, struct ceph_osd_request, r_node); |
6194ea89 | 2040 | p = rb_next(p); |
ab60b16d AE |
2041 | |
2042 | /* | |
2043 | * For linger requests that have not yet been | |
2044 | * registered, move them to the linger list; they'll | |
2045 | * be sent to the osd in the loop below. Unregister | |
2046 | * the request before re-registering it as a linger | |
2047 | * request to ensure the __map_request() below | |
2048 | * will decide it needs to be sent. | |
2049 | */ | |
2050 | if (req->r_linger && list_empty(&req->r_linger_item)) { | |
2051 | dout("%p tid %llu restart on osd%d\n", | |
2052 | req, req->r_tid, | |
2053 | req->r_osd ? req->r_osd->o_osd : -1); | |
96e4dac6 | 2054 | ceph_osdc_get_request(req); |
ab60b16d AE |
2055 | __unregister_request(osdc, req); |
2056 | __register_linger_request(osdc, req); | |
96e4dac6 | 2057 | ceph_osdc_put_request(req); |
ab60b16d AE |
2058 | continue; |
2059 | } | |
2060 | ||
9a1ea2db JD |
2061 | force_resend_req = force_resend || |
2062 | (force_resend_writes && | |
2063 | req->r_flags & CEPH_OSD_FLAG_WRITE); | |
2064 | err = __map_request(osdc, req, force_resend_req); | |
6f6c7006 SW |
2065 | if (err < 0) |
2066 | continue; /* error */ | |
2067 | if (req->r_osd == NULL) { | |
2068 | dout("%p tid %llu maps to no osd\n", req, req->r_tid); | |
2069 | needmap++; /* request a newer map */ | |
2070 | } else if (err > 0) { | |
6194ea89 SW |
2071 | if (!req->r_linger) { |
2072 | dout("%p tid %llu requeued on osd%d\n", req, | |
2073 | req->r_tid, | |
2074 | req->r_osd ? req->r_osd->o_osd : -1); | |
a40c4f10 | 2075 | req->r_flags |= CEPH_OSD_FLAG_RETRY; |
6194ea89 SW |
2076 | } |
2077 | } | |
a40c4f10 YS |
2078 | } |
2079 | ||
2080 | list_for_each_entry_safe(req, nreq, &osdc->req_linger, | |
2081 | r_linger_item) { | |
2082 | dout("linger req=%p req->r_osd=%p\n", req, req->r_osd); | |
2083 | ||
9a1ea2db JD |
2084 | err = __map_request(osdc, req, |
2085 | force_resend || force_resend_writes); | |
ab60b16d | 2086 | dout("__map_request returned %d\n", err); |
a40c4f10 YS |
2087 | if (err < 0) |
2088 | continue; /* hrm! */ | |
b0494532 ID |
2089 | if (req->r_osd == NULL || err > 0) { |
2090 | if (req->r_osd == NULL) { | |
2091 | dout("lingering %p tid %llu maps to no osd\n", | |
2092 | req, req->r_tid); | |
2093 | /* | |
2094 | * A homeless lingering request makes | |
2095 | * no sense, as it's job is to keep | |
2096 | * a particular OSD connection open. | |
2097 | * Request a newer map and kick the | |
2098 | * request, knowing that it won't be | |
2099 | * resent until we actually get a map | |
2100 | * that can tell us where to send it. | |
2101 | */ | |
2102 | needmap++; | |
2103 | } | |
a40c4f10 | 2104 | |
b0494532 ID |
2105 | dout("kicking lingering %p tid %llu osd%d\n", req, |
2106 | req->r_tid, req->r_osd ? req->r_osd->o_osd : -1); | |
2107 | __register_request(osdc, req); | |
2108 | __unregister_linger_request(osdc, req); | |
2109 | } | |
6f6c7006 | 2110 | } |
14d2f38d | 2111 | reset_changed_osds(osdc); |
f24e9980 SW |
2112 | mutex_unlock(&osdc->request_mutex); |
2113 | ||
2114 | if (needmap) { | |
2115 | dout("%d requests for down osds, need new map\n", needmap); | |
2116 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
2117 | } | |
422d2cb8 | 2118 | } |
6f6c7006 SW |
2119 | |
2120 | ||
f24e9980 SW |
2121 | /* |
2122 | * Process updated osd map. | |
2123 | * | |
2124 | * The message contains any number of incremental and full maps, normally | |
2125 | * indicating some sort of topology change in the cluster. Kick requests | |
2126 | * off to different OSDs as needed. | |
2127 | */ | |
2128 | void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) | |
2129 | { | |
2130 | void *p, *end, *next; | |
2131 | u32 nr_maps, maplen; | |
2132 | u32 epoch; | |
2133 | struct ceph_osdmap *newmap = NULL, *oldmap; | |
2134 | int err; | |
2135 | struct ceph_fsid fsid; | |
9a1ea2db | 2136 | bool was_full; |
f24e9980 SW |
2137 | |
2138 | dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0); | |
2139 | p = msg->front.iov_base; | |
2140 | end = p + msg->front.iov_len; | |
2141 | ||
2142 | /* verify fsid */ | |
2143 | ceph_decode_need(&p, end, sizeof(fsid), bad); | |
2144 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | |
0743304d SW |
2145 | if (ceph_check_fsid(osdc->client, &fsid) < 0) |
2146 | return; | |
f24e9980 SW |
2147 | |
2148 | down_write(&osdc->map_sem); | |
2149 | ||
9a1ea2db JD |
2150 | was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL); |
2151 | ||
f24e9980 SW |
2152 | /* incremental maps */ |
2153 | ceph_decode_32_safe(&p, end, nr_maps, bad); | |
2154 | dout(" %d inc maps\n", nr_maps); | |
2155 | while (nr_maps > 0) { | |
2156 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | |
c89136ea SW |
2157 | epoch = ceph_decode_32(&p); |
2158 | maplen = ceph_decode_32(&p); | |
f24e9980 SW |
2159 | ceph_decode_need(&p, end, maplen, bad); |
2160 | next = p + maplen; | |
2161 | if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) { | |
2162 | dout("applying incremental map %u len %d\n", | |
2163 | epoch, maplen); | |
2164 | newmap = osdmap_apply_incremental(&p, next, | |
2165 | osdc->osdmap, | |
15d9882c | 2166 | &osdc->client->msgr); |
f24e9980 SW |
2167 | if (IS_ERR(newmap)) { |
2168 | err = PTR_ERR(newmap); | |
2169 | goto bad; | |
2170 | } | |
30dc6381 | 2171 | BUG_ON(!newmap); |
f24e9980 SW |
2172 | if (newmap != osdc->osdmap) { |
2173 | ceph_osdmap_destroy(osdc->osdmap); | |
2174 | osdc->osdmap = newmap; | |
2175 | } | |
9a1ea2db JD |
2176 | was_full = was_full || |
2177 | ceph_osdmap_flag(osdc->osdmap, | |
2178 | CEPH_OSDMAP_FULL); | |
2179 | kick_requests(osdc, 0, was_full); | |
f24e9980 SW |
2180 | } else { |
2181 | dout("ignoring incremental map %u len %d\n", | |
2182 | epoch, maplen); | |
2183 | } | |
2184 | p = next; | |
2185 | nr_maps--; | |
2186 | } | |
2187 | if (newmap) | |
2188 | goto done; | |
2189 | ||
2190 | /* full maps */ | |
2191 | ceph_decode_32_safe(&p, end, nr_maps, bad); | |
2192 | dout(" %d full maps\n", nr_maps); | |
2193 | while (nr_maps) { | |
2194 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | |
c89136ea SW |
2195 | epoch = ceph_decode_32(&p); |
2196 | maplen = ceph_decode_32(&p); | |
f24e9980 SW |
2197 | ceph_decode_need(&p, end, maplen, bad); |
2198 | if (nr_maps > 1) { | |
2199 | dout("skipping non-latest full map %u len %d\n", | |
2200 | epoch, maplen); | |
2201 | } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) { | |
2202 | dout("skipping full map %u len %d, " | |
2203 | "older than our %u\n", epoch, maplen, | |
2204 | osdc->osdmap->epoch); | |
2205 | } else { | |
38d6453c SW |
2206 | int skipped_map = 0; |
2207 | ||
f24e9980 | 2208 | dout("taking full map %u len %d\n", epoch, maplen); |
a2505d63 | 2209 | newmap = ceph_osdmap_decode(&p, p+maplen); |
f24e9980 SW |
2210 | if (IS_ERR(newmap)) { |
2211 | err = PTR_ERR(newmap); | |
2212 | goto bad; | |
2213 | } | |
30dc6381 | 2214 | BUG_ON(!newmap); |
f24e9980 SW |
2215 | oldmap = osdc->osdmap; |
2216 | osdc->osdmap = newmap; | |
38d6453c SW |
2217 | if (oldmap) { |
2218 | if (oldmap->epoch + 1 < newmap->epoch) | |
2219 | skipped_map = 1; | |
f24e9980 | 2220 | ceph_osdmap_destroy(oldmap); |
38d6453c | 2221 | } |
9a1ea2db JD |
2222 | was_full = was_full || |
2223 | ceph_osdmap_flag(osdc->osdmap, | |
2224 | CEPH_OSDMAP_FULL); | |
2225 | kick_requests(osdc, skipped_map, was_full); | |
f24e9980 SW |
2226 | } |
2227 | p += maplen; | |
2228 | nr_maps--; | |
2229 | } | |
2230 | ||
b72e19b9 DC |
2231 | if (!osdc->osdmap) |
2232 | goto bad; | |
f24e9980 SW |
2233 | done: |
2234 | downgrade_write(&osdc->map_sem); | |
82dcabad ID |
2235 | ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP, |
2236 | osdc->osdmap->epoch); | |
cd634fb6 SW |
2237 | |
2238 | /* | |
2239 | * subscribe to subsequent osdmap updates if full to ensure | |
2240 | * we find out when we are no longer full and stop returning | |
2241 | * ENOSPC. | |
2242 | */ | |
d29adb34 JD |
2243 | if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) || |
2244 | ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) || | |
2245 | ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR)) | |
cd634fb6 SW |
2246 | ceph_monc_request_next_osdmap(&osdc->client->monc); |
2247 | ||
f9d25199 AE |
2248 | mutex_lock(&osdc->request_mutex); |
2249 | __send_queued(osdc); | |
2250 | mutex_unlock(&osdc->request_mutex); | |
f24e9980 | 2251 | up_read(&osdc->map_sem); |
03066f23 | 2252 | wake_up_all(&osdc->client->auth_wq); |
f24e9980 SW |
2253 | return; |
2254 | ||
2255 | bad: | |
2256 | pr_err("osdc handle_map corrupt msg\n"); | |
9ec7cab1 | 2257 | ceph_msg_dump(msg); |
f24e9980 | 2258 | up_write(&osdc->map_sem); |
f24e9980 SW |
2259 | } |
2260 | ||
a40c4f10 YS |
2261 | /* |
2262 | * watch/notify callback event infrastructure | |
2263 | * | |
2264 | * These callbacks are used both for watch and notify operations. | |
2265 | */ | |
2266 | static void __release_event(struct kref *kref) | |
2267 | { | |
2268 | struct ceph_osd_event *event = | |
2269 | container_of(kref, struct ceph_osd_event, kref); | |
2270 | ||
2271 | dout("__release_event %p\n", event); | |
2272 | kfree(event); | |
2273 | } | |
2274 | ||
2275 | static void get_event(struct ceph_osd_event *event) | |
2276 | { | |
2277 | kref_get(&event->kref); | |
2278 | } | |
2279 | ||
2280 | void ceph_osdc_put_event(struct ceph_osd_event *event) | |
2281 | { | |
2282 | kref_put(&event->kref, __release_event); | |
2283 | } | |
2284 | EXPORT_SYMBOL(ceph_osdc_put_event); | |
2285 | ||
2286 | static void __insert_event(struct ceph_osd_client *osdc, | |
2287 | struct ceph_osd_event *new) | |
2288 | { | |
2289 | struct rb_node **p = &osdc->event_tree.rb_node; | |
2290 | struct rb_node *parent = NULL; | |
2291 | struct ceph_osd_event *event = NULL; | |
2292 | ||
2293 | while (*p) { | |
2294 | parent = *p; | |
2295 | event = rb_entry(parent, struct ceph_osd_event, node); | |
2296 | if (new->cookie < event->cookie) | |
2297 | p = &(*p)->rb_left; | |
2298 | else if (new->cookie > event->cookie) | |
2299 | p = &(*p)->rb_right; | |
2300 | else | |
2301 | BUG(); | |
2302 | } | |
2303 | ||
2304 | rb_link_node(&new->node, parent, p); | |
2305 | rb_insert_color(&new->node, &osdc->event_tree); | |
2306 | } | |
2307 | ||
2308 | static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc, | |
2309 | u64 cookie) | |
2310 | { | |
2311 | struct rb_node **p = &osdc->event_tree.rb_node; | |
2312 | struct rb_node *parent = NULL; | |
2313 | struct ceph_osd_event *event = NULL; | |
2314 | ||
2315 | while (*p) { | |
2316 | parent = *p; | |
2317 | event = rb_entry(parent, struct ceph_osd_event, node); | |
2318 | if (cookie < event->cookie) | |
2319 | p = &(*p)->rb_left; | |
2320 | else if (cookie > event->cookie) | |
2321 | p = &(*p)->rb_right; | |
2322 | else | |
2323 | return event; | |
2324 | } | |
2325 | return NULL; | |
2326 | } | |
2327 | ||
2328 | static void __remove_event(struct ceph_osd_event *event) | |
2329 | { | |
2330 | struct ceph_osd_client *osdc = event->osdc; | |
2331 | ||
2332 | if (!RB_EMPTY_NODE(&event->node)) { | |
2333 | dout("__remove_event removed %p\n", event); | |
2334 | rb_erase(&event->node, &osdc->event_tree); | |
2335 | ceph_osdc_put_event(event); | |
2336 | } else { | |
2337 | dout("__remove_event didn't remove %p\n", event); | |
2338 | } | |
2339 | } | |
2340 | ||
2341 | int ceph_osdc_create_event(struct ceph_osd_client *osdc, | |
2342 | void (*event_cb)(u64, u64, u8, void *), | |
3c663bbd | 2343 | void *data, struct ceph_osd_event **pevent) |
a40c4f10 YS |
2344 | { |
2345 | struct ceph_osd_event *event; | |
2346 | ||
2347 | event = kmalloc(sizeof(*event), GFP_NOIO); | |
2348 | if (!event) | |
2349 | return -ENOMEM; | |
2350 | ||
2351 | dout("create_event %p\n", event); | |
2352 | event->cb = event_cb; | |
3c663bbd | 2353 | event->one_shot = 0; |
a40c4f10 YS |
2354 | event->data = data; |
2355 | event->osdc = osdc; | |
2356 | INIT_LIST_HEAD(&event->osd_node); | |
3ee5234d | 2357 | RB_CLEAR_NODE(&event->node); |
a40c4f10 YS |
2358 | kref_init(&event->kref); /* one ref for us */ |
2359 | kref_get(&event->kref); /* one ref for the caller */ | |
a40c4f10 YS |
2360 | |
2361 | spin_lock(&osdc->event_lock); | |
2362 | event->cookie = ++osdc->event_count; | |
2363 | __insert_event(osdc, event); | |
2364 | spin_unlock(&osdc->event_lock); | |
2365 | ||
2366 | *pevent = event; | |
2367 | return 0; | |
2368 | } | |
2369 | EXPORT_SYMBOL(ceph_osdc_create_event); | |
2370 | ||
2371 | void ceph_osdc_cancel_event(struct ceph_osd_event *event) | |
2372 | { | |
2373 | struct ceph_osd_client *osdc = event->osdc; | |
2374 | ||
2375 | dout("cancel_event %p\n", event); | |
2376 | spin_lock(&osdc->event_lock); | |
2377 | __remove_event(event); | |
2378 | spin_unlock(&osdc->event_lock); | |
2379 | ceph_osdc_put_event(event); /* caller's */ | |
2380 | } | |
2381 | EXPORT_SYMBOL(ceph_osdc_cancel_event); | |
2382 | ||
2383 | ||
2384 | static void do_event_work(struct work_struct *work) | |
2385 | { | |
2386 | struct ceph_osd_event_work *event_work = | |
2387 | container_of(work, struct ceph_osd_event_work, work); | |
2388 | struct ceph_osd_event *event = event_work->event; | |
2389 | u64 ver = event_work->ver; | |
2390 | u64 notify_id = event_work->notify_id; | |
2391 | u8 opcode = event_work->opcode; | |
2392 | ||
2393 | dout("do_event_work completing %p\n", event); | |
2394 | event->cb(ver, notify_id, opcode, event->data); | |
a40c4f10 YS |
2395 | dout("do_event_work completed %p\n", event); |
2396 | ceph_osdc_put_event(event); | |
2397 | kfree(event_work); | |
2398 | } | |
2399 | ||
2400 | ||
2401 | /* | |
2402 | * Process osd watch notifications | |
2403 | */ | |
3c663bbd AE |
2404 | static void handle_watch_notify(struct ceph_osd_client *osdc, |
2405 | struct ceph_msg *msg) | |
a40c4f10 YS |
2406 | { |
2407 | void *p, *end; | |
2408 | u8 proto_ver; | |
2409 | u64 cookie, ver, notify_id; | |
2410 | u8 opcode; | |
2411 | struct ceph_osd_event *event; | |
2412 | struct ceph_osd_event_work *event_work; | |
2413 | ||
2414 | p = msg->front.iov_base; | |
2415 | end = p + msg->front.iov_len; | |
2416 | ||
2417 | ceph_decode_8_safe(&p, end, proto_ver, bad); | |
2418 | ceph_decode_8_safe(&p, end, opcode, bad); | |
2419 | ceph_decode_64_safe(&p, end, cookie, bad); | |
2420 | ceph_decode_64_safe(&p, end, ver, bad); | |
2421 | ceph_decode_64_safe(&p, end, notify_id, bad); | |
2422 | ||
2423 | spin_lock(&osdc->event_lock); | |
2424 | event = __find_event(osdc, cookie); | |
2425 | if (event) { | |
3c663bbd | 2426 | BUG_ON(event->one_shot); |
a40c4f10 | 2427 | get_event(event); |
a40c4f10 YS |
2428 | } |
2429 | spin_unlock(&osdc->event_lock); | |
2430 | dout("handle_watch_notify cookie %lld ver %lld event %p\n", | |
2431 | cookie, ver, event); | |
2432 | if (event) { | |
2433 | event_work = kmalloc(sizeof(*event_work), GFP_NOIO); | |
a40c4f10 | 2434 | if (!event_work) { |
91883cd2 ID |
2435 | pr_err("couldn't allocate event_work\n"); |
2436 | ceph_osdc_put_event(event); | |
2437 | return; | |
a40c4f10 | 2438 | } |
6b0ae409 | 2439 | INIT_WORK(&event_work->work, do_event_work); |
a40c4f10 YS |
2440 | event_work->event = event; |
2441 | event_work->ver = ver; | |
2442 | event_work->notify_id = notify_id; | |
2443 | event_work->opcode = opcode; | |
a40c4f10 | 2444 | |
91883cd2 ID |
2445 | queue_work(osdc->notify_wq, &event_work->work); |
2446 | } | |
a40c4f10 | 2447 | |
a40c4f10 YS |
2448 | return; |
2449 | ||
2450 | bad: | |
2451 | pr_err("osdc handle_watch_notify corrupt msg\n"); | |
a40c4f10 YS |
2452 | } |
2453 | ||
e65550fd AE |
2454 | /* |
2455 | * build new request AND message | |
2456 | * | |
2457 | */ | |
2458 | void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off, | |
2459 | struct ceph_snap_context *snapc, u64 snap_id, | |
2460 | struct timespec *mtime) | |
2461 | { | |
2462 | struct ceph_msg *msg = req->r_request; | |
2463 | void *p; | |
2464 | size_t msg_size; | |
2465 | int flags = req->r_flags; | |
2466 | u64 data_len; | |
2467 | unsigned int i; | |
2468 | ||
2469 | req->r_snapid = snap_id; | |
84127282 | 2470 | WARN_ON(snapc != req->r_snapc); |
e65550fd AE |
2471 | |
2472 | /* encode request */ | |
2473 | msg->hdr.version = cpu_to_le16(4); | |
2474 | ||
2475 | p = msg->front.iov_base; | |
2476 | ceph_encode_32(&p, 1); /* client_inc is always 1 */ | |
2477 | req->r_request_osdmap_epoch = p; | |
2478 | p += 4; | |
2479 | req->r_request_flags = p; | |
2480 | p += 4; | |
2481 | if (req->r_flags & CEPH_OSD_FLAG_WRITE) | |
2482 | ceph_encode_timespec(p, mtime); | |
2483 | p += sizeof(struct ceph_timespec); | |
2484 | req->r_request_reassert_version = p; | |
2485 | p += sizeof(struct ceph_eversion); /* will get filled in */ | |
2486 | ||
2487 | /* oloc */ | |
2488 | ceph_encode_8(&p, 4); | |
2489 | ceph_encode_8(&p, 4); | |
2490 | ceph_encode_32(&p, 8 + 4 + 4); | |
2491 | req->r_request_pool = p; | |
2492 | p += 8; | |
2493 | ceph_encode_32(&p, -1); /* preferred */ | |
2494 | ceph_encode_32(&p, 0); /* key len */ | |
2495 | ||
2496 | ceph_encode_8(&p, 1); | |
2497 | req->r_request_pgid = p; | |
2498 | p += 8 + 4; | |
2499 | ceph_encode_32(&p, -1); /* preferred */ | |
2500 | ||
2501 | /* oid */ | |
3c972c95 ID |
2502 | ceph_encode_32(&p, req->r_base_oid.name_len); |
2503 | memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len); | |
2504 | dout("oid '%.*s' len %d\n", req->r_base_oid.name_len, | |
2505 | req->r_base_oid.name, req->r_base_oid.name_len); | |
2506 | p += req->r_base_oid.name_len; | |
e65550fd AE |
2507 | |
2508 | /* ops--can imply data */ | |
2509 | ceph_encode_16(&p, (u16)req->r_num_ops); | |
2510 | data_len = 0; | |
2511 | for (i = 0; i < req->r_num_ops; i++) { | |
2512 | data_len += osd_req_encode_op(req, p, i); | |
2513 | p += sizeof(struct ceph_osd_op); | |
2514 | } | |
2515 | ||
2516 | /* snaps */ | |
2517 | ceph_encode_64(&p, req->r_snapid); | |
2518 | ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0); | |
2519 | ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0); | |
2520 | if (req->r_snapc) { | |
84127282 | 2521 | for (i = 0; i < req->r_snapc->num_snaps; i++) { |
e65550fd AE |
2522 | ceph_encode_64(&p, req->r_snapc->snaps[i]); |
2523 | } | |
2524 | } | |
2525 | ||
2526 | req->r_request_attempts = p; | |
2527 | p += 4; | |
2528 | ||
2529 | /* data */ | |
2530 | if (flags & CEPH_OSD_FLAG_WRITE) { | |
2531 | u16 data_off; | |
2532 | ||
2533 | /* | |
2534 | * The header "data_off" is a hint to the receiver | |
2535 | * allowing it to align received data into its | |
2536 | * buffers such that there's no need to re-copy | |
2537 | * it before writing it to disk (direct I/O). | |
2538 | */ | |
2539 | data_off = (u16) (off & 0xffff); | |
2540 | req->r_request->hdr.data_off = cpu_to_le16(data_off); | |
2541 | } | |
2542 | req->r_request->hdr.data_len = cpu_to_le32(data_len); | |
2543 | ||
2544 | BUG_ON(p > msg->front.iov_base + msg->front.iov_len); | |
2545 | msg_size = p - msg->front.iov_base; | |
2546 | msg->front.iov_len = msg_size; | |
2547 | msg->hdr.front_len = cpu_to_le32(msg_size); | |
2548 | ||
2549 | dout("build_request msg_size was %d\n", (int)msg_size); | |
2550 | } | |
2551 | EXPORT_SYMBOL(ceph_osdc_build_request); | |
2552 | ||
70636773 AE |
2553 | /* |
2554 | * Register request, send initial attempt. | |
2555 | */ | |
2556 | int ceph_osdc_start_request(struct ceph_osd_client *osdc, | |
2557 | struct ceph_osd_request *req, | |
2558 | bool nofail) | |
2559 | { | |
0bbfdfe8 | 2560 | int rc; |
70636773 | 2561 | |
f24e9980 SW |
2562 | down_read(&osdc->map_sem); |
2563 | mutex_lock(&osdc->request_mutex); | |
0bbfdfe8 ID |
2564 | |
2565 | rc = __ceph_osdc_start_request(osdc, req, nofail); | |
2566 | ||
f24e9980 SW |
2567 | mutex_unlock(&osdc->request_mutex); |
2568 | up_read(&osdc->map_sem); | |
0bbfdfe8 | 2569 | |
f24e9980 SW |
2570 | return rc; |
2571 | } | |
3d14c5d2 | 2572 | EXPORT_SYMBOL(ceph_osdc_start_request); |
f24e9980 | 2573 | |
c9f9b93d ID |
2574 | /* |
2575 | * Unregister a registered request. The request is not completed (i.e. | |
2576 | * no callbacks or wakeups) - higher layers are supposed to know what | |
2577 | * they are canceling. | |
2578 | */ | |
2579 | void ceph_osdc_cancel_request(struct ceph_osd_request *req) | |
2580 | { | |
2581 | struct ceph_osd_client *osdc = req->r_osdc; | |
2582 | ||
2583 | mutex_lock(&osdc->request_mutex); | |
2584 | if (req->r_linger) | |
2585 | __unregister_linger_request(osdc, req); | |
2586 | __unregister_request(osdc, req); | |
2587 | mutex_unlock(&osdc->request_mutex); | |
2588 | ||
2589 | dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid); | |
2590 | } | |
2591 | EXPORT_SYMBOL(ceph_osdc_cancel_request); | |
2592 | ||
f24e9980 SW |
2593 | /* |
2594 | * wait for a request to complete | |
2595 | */ | |
2596 | int ceph_osdc_wait_request(struct ceph_osd_client *osdc, | |
2597 | struct ceph_osd_request *req) | |
2598 | { | |
2599 | int rc; | |
2600 | ||
c9f9b93d ID |
2601 | dout("%s %p tid %llu\n", __func__, req, req->r_tid); |
2602 | ||
f24e9980 SW |
2603 | rc = wait_for_completion_interruptible(&req->r_completion); |
2604 | if (rc < 0) { | |
c9f9b93d ID |
2605 | dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid); |
2606 | ceph_osdc_cancel_request(req); | |
25845472 | 2607 | complete_request(req); |
f24e9980 SW |
2608 | return rc; |
2609 | } | |
2610 | ||
c9f9b93d ID |
2611 | dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid, |
2612 | req->r_result); | |
f24e9980 SW |
2613 | return req->r_result; |
2614 | } | |
3d14c5d2 | 2615 | EXPORT_SYMBOL(ceph_osdc_wait_request); |
f24e9980 SW |
2616 | |
2617 | /* | |
2618 | * sync - wait for all in-flight requests to flush. avoid starvation. | |
2619 | */ | |
2620 | void ceph_osdc_sync(struct ceph_osd_client *osdc) | |
2621 | { | |
2622 | struct ceph_osd_request *req; | |
2623 | u64 last_tid, next_tid = 0; | |
2624 | ||
2625 | mutex_lock(&osdc->request_mutex); | |
2626 | last_tid = osdc->last_tid; | |
2627 | while (1) { | |
2628 | req = __lookup_request_ge(osdc, next_tid); | |
2629 | if (!req) | |
2630 | break; | |
2631 | if (req->r_tid > last_tid) | |
2632 | break; | |
2633 | ||
2634 | next_tid = req->r_tid + 1; | |
2635 | if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0) | |
2636 | continue; | |
2637 | ||
2638 | ceph_osdc_get_request(req); | |
2639 | mutex_unlock(&osdc->request_mutex); | |
2640 | dout("sync waiting on tid %llu (last is %llu)\n", | |
2641 | req->r_tid, last_tid); | |
2642 | wait_for_completion(&req->r_safe_completion); | |
2643 | mutex_lock(&osdc->request_mutex); | |
2644 | ceph_osdc_put_request(req); | |
2645 | } | |
2646 | mutex_unlock(&osdc->request_mutex); | |
2647 | dout("sync done (thru tid %llu)\n", last_tid); | |
2648 | } | |
3d14c5d2 | 2649 | EXPORT_SYMBOL(ceph_osdc_sync); |
f24e9980 | 2650 | |
dd935f44 JD |
2651 | /* |
2652 | * Call all pending notify callbacks - for use after a watch is | |
2653 | * unregistered, to make sure no more callbacks for it will be invoked | |
2654 | */ | |
f6479449 | 2655 | void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc) |
dd935f44 JD |
2656 | { |
2657 | flush_workqueue(osdc->notify_wq); | |
2658 | } | |
2659 | EXPORT_SYMBOL(ceph_osdc_flush_notifies); | |
2660 | ||
2661 | ||
f24e9980 SW |
2662 | /* |
2663 | * init, shutdown | |
2664 | */ | |
2665 | int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) | |
2666 | { | |
2667 | int err; | |
2668 | ||
2669 | dout("init\n"); | |
2670 | osdc->client = client; | |
2671 | osdc->osdmap = NULL; | |
2672 | init_rwsem(&osdc->map_sem); | |
2673 | init_completion(&osdc->map_waiters); | |
2674 | osdc->last_requested_map = 0; | |
2675 | mutex_init(&osdc->request_mutex); | |
f24e9980 SW |
2676 | osdc->last_tid = 0; |
2677 | osdc->osds = RB_ROOT; | |
f5a2041b | 2678 | INIT_LIST_HEAD(&osdc->osd_lru); |
f24e9980 | 2679 | osdc->requests = RB_ROOT; |
422d2cb8 | 2680 | INIT_LIST_HEAD(&osdc->req_lru); |
6f6c7006 SW |
2681 | INIT_LIST_HEAD(&osdc->req_unsent); |
2682 | INIT_LIST_HEAD(&osdc->req_notarget); | |
a40c4f10 | 2683 | INIT_LIST_HEAD(&osdc->req_linger); |
f24e9980 SW |
2684 | osdc->num_requests = 0; |
2685 | INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); | |
f5a2041b | 2686 | INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); |
a40c4f10 YS |
2687 | spin_lock_init(&osdc->event_lock); |
2688 | osdc->event_tree = RB_ROOT; | |
2689 | osdc->event_count = 0; | |
f5a2041b YS |
2690 | |
2691 | schedule_delayed_work(&osdc->osds_timeout_work, | |
a319bf56 | 2692 | round_jiffies_relative(osdc->client->options->osd_idle_ttl)); |
f24e9980 | 2693 | |
5f44f142 | 2694 | err = -ENOMEM; |
9e767adb ID |
2695 | osdc->req_mempool = mempool_create_slab_pool(10, |
2696 | ceph_osd_request_cache); | |
f24e9980 | 2697 | if (!osdc->req_mempool) |
5f44f142 | 2698 | goto out; |
f24e9980 | 2699 | |
d50b409f | 2700 | err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP, |
711da55d | 2701 | PAGE_SIZE, 10, true, "osd_op"); |
f24e9980 | 2702 | if (err < 0) |
5f44f142 | 2703 | goto out_mempool; |
d50b409f | 2704 | err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY, |
711da55d | 2705 | PAGE_SIZE, 10, true, "osd_op_reply"); |
c16e7869 SW |
2706 | if (err < 0) |
2707 | goto out_msgpool; | |
a40c4f10 | 2708 | |
dbcae088 | 2709 | err = -ENOMEM; |
a40c4f10 | 2710 | osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify"); |
dbcae088 | 2711 | if (!osdc->notify_wq) |
c172ec5c ID |
2712 | goto out_msgpool_reply; |
2713 | ||
f24e9980 | 2714 | return 0; |
5f44f142 | 2715 | |
c172ec5c ID |
2716 | out_msgpool_reply: |
2717 | ceph_msgpool_destroy(&osdc->msgpool_op_reply); | |
c16e7869 SW |
2718 | out_msgpool: |
2719 | ceph_msgpool_destroy(&osdc->msgpool_op); | |
5f44f142 SW |
2720 | out_mempool: |
2721 | mempool_destroy(osdc->req_mempool); | |
2722 | out: | |
2723 | return err; | |
f24e9980 SW |
2724 | } |
2725 | ||
2726 | void ceph_osdc_stop(struct ceph_osd_client *osdc) | |
2727 | { | |
a40c4f10 YS |
2728 | flush_workqueue(osdc->notify_wq); |
2729 | destroy_workqueue(osdc->notify_wq); | |
f24e9980 | 2730 | cancel_delayed_work_sync(&osdc->timeout_work); |
f5a2041b | 2731 | cancel_delayed_work_sync(&osdc->osds_timeout_work); |
f24e9980 SW |
2732 | if (osdc->osdmap) { |
2733 | ceph_osdmap_destroy(osdc->osdmap); | |
2734 | osdc->osdmap = NULL; | |
2735 | } | |
aca420bc | 2736 | remove_all_osds(osdc); |
f24e9980 SW |
2737 | mempool_destroy(osdc->req_mempool); |
2738 | ceph_msgpool_destroy(&osdc->msgpool_op); | |
c16e7869 | 2739 | ceph_msgpool_destroy(&osdc->msgpool_op_reply); |
f24e9980 SW |
2740 | } |
2741 | ||
2742 | /* | |
2743 | * Read some contiguous pages. If we cross a stripe boundary, shorten | |
2744 | * *plen. Return number of bytes read, or error. | |
2745 | */ | |
2746 | int ceph_osdc_readpages(struct ceph_osd_client *osdc, | |
2747 | struct ceph_vino vino, struct ceph_file_layout *layout, | |
2748 | u64 off, u64 *plen, | |
2749 | u32 truncate_seq, u64 truncate_size, | |
b7495fc2 | 2750 | struct page **pages, int num_pages, int page_align) |
f24e9980 SW |
2751 | { |
2752 | struct ceph_osd_request *req; | |
2753 | int rc = 0; | |
2754 | ||
2755 | dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino, | |
2756 | vino.snap, off, *plen); | |
715e4cd4 | 2757 | req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1, |
f24e9980 | 2758 | CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, |
acead002 | 2759 | NULL, truncate_seq, truncate_size, |
153e5167 | 2760 | false); |
6816282d SW |
2761 | if (IS_ERR(req)) |
2762 | return PTR_ERR(req); | |
f24e9980 SW |
2763 | |
2764 | /* it may be a short read due to an object boundary */ | |
0fff87ec | 2765 | |
406e2c9f | 2766 | osd_req_op_extent_osd_data_pages(req, 0, |
a4ce40a9 | 2767 | pages, *plen, page_align, false, false); |
f24e9980 | 2768 | |
e0c59487 | 2769 | dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n", |
43bfe5de | 2770 | off, *plen, *plen, page_align); |
f24e9980 | 2771 | |
79528734 | 2772 | ceph_osdc_build_request(req, off, NULL, vino.snap, NULL); |
02ee07d3 | 2773 | |
f24e9980 SW |
2774 | rc = ceph_osdc_start_request(osdc, req, false); |
2775 | if (!rc) | |
2776 | rc = ceph_osdc_wait_request(osdc, req); | |
2777 | ||
2778 | ceph_osdc_put_request(req); | |
2779 | dout("readpages result %d\n", rc); | |
2780 | return rc; | |
2781 | } | |
3d14c5d2 | 2782 | EXPORT_SYMBOL(ceph_osdc_readpages); |
f24e9980 SW |
2783 | |
2784 | /* | |
2785 | * do a synchronous write on N pages | |
2786 | */ | |
2787 | int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino, | |
2788 | struct ceph_file_layout *layout, | |
2789 | struct ceph_snap_context *snapc, | |
2790 | u64 off, u64 len, | |
2791 | u32 truncate_seq, u64 truncate_size, | |
2792 | struct timespec *mtime, | |
24808826 | 2793 | struct page **pages, int num_pages) |
f24e9980 SW |
2794 | { |
2795 | struct ceph_osd_request *req; | |
2796 | int rc = 0; | |
b7495fc2 | 2797 | int page_align = off & ~PAGE_MASK; |
f24e9980 | 2798 | |
acead002 | 2799 | BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */ |
715e4cd4 | 2800 | req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1, |
f24e9980 | 2801 | CEPH_OSD_OP_WRITE, |
24808826 | 2802 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, |
acead002 | 2803 | snapc, truncate_seq, truncate_size, |
153e5167 | 2804 | true); |
6816282d SW |
2805 | if (IS_ERR(req)) |
2806 | return PTR_ERR(req); | |
f24e9980 SW |
2807 | |
2808 | /* it may be a short write due to an object boundary */ | |
406e2c9f | 2809 | osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align, |
43bfe5de AE |
2810 | false, false); |
2811 | dout("writepages %llu~%llu (%llu bytes)\n", off, len, len); | |
f24e9980 | 2812 | |
79528734 | 2813 | ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime); |
02ee07d3 | 2814 | |
87f979d3 | 2815 | rc = ceph_osdc_start_request(osdc, req, true); |
f24e9980 SW |
2816 | if (!rc) |
2817 | rc = ceph_osdc_wait_request(osdc, req); | |
2818 | ||
2819 | ceph_osdc_put_request(req); | |
2820 | if (rc == 0) | |
2821 | rc = len; | |
2822 | dout("writepages result %d\n", rc); | |
2823 | return rc; | |
2824 | } | |
3d14c5d2 | 2825 | EXPORT_SYMBOL(ceph_osdc_writepages); |
f24e9980 | 2826 | |
5522ae0b AE |
2827 | int ceph_osdc_setup(void) |
2828 | { | |
3f1af42a ID |
2829 | size_t size = sizeof(struct ceph_osd_request) + |
2830 | CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op); | |
2831 | ||
5522ae0b | 2832 | BUG_ON(ceph_osd_request_cache); |
3f1af42a ID |
2833 | ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size, |
2834 | 0, 0, NULL); | |
5522ae0b AE |
2835 | |
2836 | return ceph_osd_request_cache ? 0 : -ENOMEM; | |
2837 | } | |
2838 | EXPORT_SYMBOL(ceph_osdc_setup); | |
2839 | ||
2840 | void ceph_osdc_cleanup(void) | |
2841 | { | |
2842 | BUG_ON(!ceph_osd_request_cache); | |
2843 | kmem_cache_destroy(ceph_osd_request_cache); | |
2844 | ceph_osd_request_cache = NULL; | |
2845 | } | |
2846 | EXPORT_SYMBOL(ceph_osdc_cleanup); | |
2847 | ||
f24e9980 SW |
2848 | /* |
2849 | * handle incoming message | |
2850 | */ | |
2851 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | |
2852 | { | |
2853 | struct ceph_osd *osd = con->private; | |
32c895e7 | 2854 | struct ceph_osd_client *osdc; |
f24e9980 SW |
2855 | int type = le16_to_cpu(msg->hdr.type); |
2856 | ||
2857 | if (!osd) | |
4a32f93d | 2858 | goto out; |
32c895e7 | 2859 | osdc = osd->o_osdc; |
f24e9980 SW |
2860 | |
2861 | switch (type) { | |
2862 | case CEPH_MSG_OSD_MAP: | |
2863 | ceph_osdc_handle_map(osdc, msg); | |
2864 | break; | |
2865 | case CEPH_MSG_OSD_OPREPLY: | |
70cf052d | 2866 | handle_reply(osdc, msg); |
f24e9980 | 2867 | break; |
a40c4f10 YS |
2868 | case CEPH_MSG_WATCH_NOTIFY: |
2869 | handle_watch_notify(osdc, msg); | |
2870 | break; | |
f24e9980 SW |
2871 | |
2872 | default: | |
2873 | pr_err("received unknown message type %d %s\n", type, | |
2874 | ceph_msg_type_name(type)); | |
2875 | } | |
4a32f93d | 2876 | out: |
f24e9980 SW |
2877 | ceph_msg_put(msg); |
2878 | } | |
2879 | ||
5b3a4db3 | 2880 | /* |
d15f9d69 ID |
2881 | * Lookup and return message for incoming reply. Don't try to do |
2882 | * anything about a larger than preallocated data portion of the | |
2883 | * message at the moment - for now, just skip the message. | |
5b3a4db3 SW |
2884 | */ |
2885 | static struct ceph_msg *get_reply(struct ceph_connection *con, | |
2450418c YS |
2886 | struct ceph_msg_header *hdr, |
2887 | int *skip) | |
f24e9980 SW |
2888 | { |
2889 | struct ceph_osd *osd = con->private; | |
2890 | struct ceph_osd_client *osdc = osd->o_osdc; | |
2450418c | 2891 | struct ceph_msg *m; |
0547a9b3 | 2892 | struct ceph_osd_request *req; |
3f0a4ac5 | 2893 | int front_len = le32_to_cpu(hdr->front_len); |
5b3a4db3 | 2894 | int data_len = le32_to_cpu(hdr->data_len); |
0547a9b3 | 2895 | u64 tid; |
f24e9980 | 2896 | |
0547a9b3 YS |
2897 | tid = le64_to_cpu(hdr->tid); |
2898 | mutex_lock(&osdc->request_mutex); | |
2899 | req = __lookup_request(osdc, tid); | |
2900 | if (!req) { | |
cd8140c6 ID |
2901 | dout("%s osd%d tid %llu unknown, skipping\n", __func__, |
2902 | osd->o_osd, tid); | |
0547a9b3 | 2903 | m = NULL; |
d15f9d69 | 2904 | *skip = 1; |
0547a9b3 YS |
2905 | goto out; |
2906 | } | |
c16e7869 | 2907 | |
ace6d3a9 | 2908 | ceph_msg_revoke_incoming(req->r_reply); |
0547a9b3 | 2909 | |
f2be82b0 | 2910 | if (front_len > req->r_reply->front_alloc_len) { |
d15f9d69 ID |
2911 | pr_warn("%s osd%d tid %llu front %d > preallocated %d\n", |
2912 | __func__, osd->o_osd, req->r_tid, front_len, | |
2913 | req->r_reply->front_alloc_len); | |
3f0a4ac5 ID |
2914 | m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS, |
2915 | false); | |
a79832f2 | 2916 | if (!m) |
c16e7869 SW |
2917 | goto out; |
2918 | ceph_msg_put(req->r_reply); | |
2919 | req->r_reply = m; | |
2920 | } | |
0fff87ec | 2921 | |
d15f9d69 ID |
2922 | if (data_len > req->r_reply->data_length) { |
2923 | pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n", | |
2924 | __func__, osd->o_osd, req->r_tid, data_len, | |
2925 | req->r_reply->data_length); | |
2926 | m = NULL; | |
2927 | *skip = 1; | |
2928 | goto out; | |
0547a9b3 | 2929 | } |
d15f9d69 ID |
2930 | |
2931 | m = ceph_msg_get(req->r_reply); | |
c16e7869 | 2932 | dout("get_reply tid %lld %p\n", tid, m); |
0547a9b3 YS |
2933 | |
2934 | out: | |
2935 | mutex_unlock(&osdc->request_mutex); | |
2450418c | 2936 | return m; |
5b3a4db3 SW |
2937 | } |
2938 | ||
2939 | static struct ceph_msg *alloc_msg(struct ceph_connection *con, | |
2940 | struct ceph_msg_header *hdr, | |
2941 | int *skip) | |
2942 | { | |
2943 | struct ceph_osd *osd = con->private; | |
2944 | int type = le16_to_cpu(hdr->type); | |
2945 | int front = le32_to_cpu(hdr->front_len); | |
2946 | ||
1c20f2d2 | 2947 | *skip = 0; |
5b3a4db3 SW |
2948 | switch (type) { |
2949 | case CEPH_MSG_OSD_MAP: | |
a40c4f10 | 2950 | case CEPH_MSG_WATCH_NOTIFY: |
b61c2763 | 2951 | return ceph_msg_new(type, front, GFP_NOFS, false); |
5b3a4db3 SW |
2952 | case CEPH_MSG_OSD_OPREPLY: |
2953 | return get_reply(con, hdr, skip); | |
2954 | default: | |
2955 | pr_info("alloc_msg unexpected msg type %d from osd%d\n", type, | |
2956 | osd->o_osd); | |
2957 | *skip = 1; | |
2958 | return NULL; | |
2959 | } | |
f24e9980 SW |
2960 | } |
2961 | ||
2962 | /* | |
2963 | * Wrappers to refcount containing ceph_osd struct | |
2964 | */ | |
2965 | static struct ceph_connection *get_osd_con(struct ceph_connection *con) | |
2966 | { | |
2967 | struct ceph_osd *osd = con->private; | |
2968 | if (get_osd(osd)) | |
2969 | return con; | |
2970 | return NULL; | |
2971 | } | |
2972 | ||
2973 | static void put_osd_con(struct ceph_connection *con) | |
2974 | { | |
2975 | struct ceph_osd *osd = con->private; | |
2976 | put_osd(osd); | |
2977 | } | |
2978 | ||
4e7a5dcd SW |
2979 | /* |
2980 | * authentication | |
2981 | */ | |
a3530df3 AE |
2982 | /* |
2983 | * Note: returned pointer is the address of a structure that's | |
2984 | * managed separately. Caller must *not* attempt to free it. | |
2985 | */ | |
2986 | static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, | |
8f43fb53 | 2987 | int *proto, int force_new) |
4e7a5dcd SW |
2988 | { |
2989 | struct ceph_osd *o = con->private; | |
2990 | struct ceph_osd_client *osdc = o->o_osdc; | |
2991 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
74f1869f | 2992 | struct ceph_auth_handshake *auth = &o->o_auth; |
4e7a5dcd | 2993 | |
74f1869f | 2994 | if (force_new && auth->authorizer) { |
6c1ea260 | 2995 | ceph_auth_destroy_authorizer(auth->authorizer); |
74f1869f AE |
2996 | auth->authorizer = NULL; |
2997 | } | |
27859f97 SW |
2998 | if (!auth->authorizer) { |
2999 | int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD, | |
3000 | auth); | |
4e7a5dcd | 3001 | if (ret) |
a3530df3 | 3002 | return ERR_PTR(ret); |
27859f97 SW |
3003 | } else { |
3004 | int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD, | |
0bed9b5c SW |
3005 | auth); |
3006 | if (ret) | |
3007 | return ERR_PTR(ret); | |
4e7a5dcd | 3008 | } |
4e7a5dcd | 3009 | *proto = ac->protocol; |
74f1869f | 3010 | |
a3530df3 | 3011 | return auth; |
4e7a5dcd SW |
3012 | } |
3013 | ||
3014 | ||
3015 | static int verify_authorizer_reply(struct ceph_connection *con, int len) | |
3016 | { | |
3017 | struct ceph_osd *o = con->private; | |
3018 | struct ceph_osd_client *osdc = o->o_osdc; | |
3019 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
3020 | ||
27859f97 | 3021 | return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len); |
4e7a5dcd SW |
3022 | } |
3023 | ||
9bd2e6f8 SW |
3024 | static int invalidate_authorizer(struct ceph_connection *con) |
3025 | { | |
3026 | struct ceph_osd *o = con->private; | |
3027 | struct ceph_osd_client *osdc = o->o_osdc; | |
3028 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
3029 | ||
27859f97 | 3030 | ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); |
9bd2e6f8 SW |
3031 | return ceph_monc_validate_auth(&osdc->client->monc); |
3032 | } | |
4e7a5dcd | 3033 | |
79dbd1ba | 3034 | static int osd_sign_message(struct ceph_msg *msg) |
33d07337 | 3035 | { |
79dbd1ba | 3036 | struct ceph_osd *o = msg->con->private; |
33d07337 | 3037 | struct ceph_auth_handshake *auth = &o->o_auth; |
79dbd1ba | 3038 | |
33d07337 YZ |
3039 | return ceph_auth_sign_message(auth, msg); |
3040 | } | |
3041 | ||
79dbd1ba | 3042 | static int osd_check_message_signature(struct ceph_msg *msg) |
33d07337 | 3043 | { |
79dbd1ba | 3044 | struct ceph_osd *o = msg->con->private; |
33d07337 | 3045 | struct ceph_auth_handshake *auth = &o->o_auth; |
79dbd1ba | 3046 | |
33d07337 YZ |
3047 | return ceph_auth_check_message_signature(auth, msg); |
3048 | } | |
3049 | ||
9e32789f | 3050 | static const struct ceph_connection_operations osd_con_ops = { |
f24e9980 SW |
3051 | .get = get_osd_con, |
3052 | .put = put_osd_con, | |
3053 | .dispatch = dispatch, | |
4e7a5dcd SW |
3054 | .get_authorizer = get_authorizer, |
3055 | .verify_authorizer_reply = verify_authorizer_reply, | |
9bd2e6f8 | 3056 | .invalidate_authorizer = invalidate_authorizer, |
f24e9980 | 3057 | .alloc_msg = alloc_msg, |
79dbd1ba ID |
3058 | .sign_message = osd_sign_message, |
3059 | .check_message_signature = osd_check_message_signature, | |
81b024e7 | 3060 | .fault = osd_reset, |
f24e9980 | 3061 | }; |