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