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