]>
Commit | Line | Data |
---|---|---|
3d14c5d2 | 1 | #include <linux/ceph/ceph_debug.h> |
f24e9980 | 2 | |
3d14c5d2 | 3 | #include <linux/module.h> |
f24e9980 SW |
4 | #include <linux/err.h> |
5 | #include <linux/highmem.h> | |
6 | #include <linux/mm.h> | |
7 | #include <linux/pagemap.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/uaccess.h> | |
68b4476b YS |
10 | #ifdef CONFIG_BLOCK |
11 | #include <linux/bio.h> | |
12 | #endif | |
f24e9980 | 13 | |
3d14c5d2 YS |
14 | #include <linux/ceph/libceph.h> |
15 | #include <linux/ceph/osd_client.h> | |
16 | #include <linux/ceph/messenger.h> | |
17 | #include <linux/ceph/decode.h> | |
18 | #include <linux/ceph/auth.h> | |
19 | #include <linux/ceph/pagelist.h> | |
f24e9980 | 20 | |
c16e7869 SW |
21 | #define OSD_OP_FRONT_LEN 4096 |
22 | #define OSD_OPREPLY_FRONT_LEN 512 | |
0d59ab81 | 23 | |
9e32789f | 24 | static const struct ceph_connection_operations osd_con_ops; |
f24e9980 | 25 | |
f9d25199 | 26 | static void __send_queued(struct ceph_osd_client *osdc); |
6f6c7006 | 27 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd); |
a40c4f10 YS |
28 | static void __register_request(struct ceph_osd_client *osdc, |
29 | struct ceph_osd_request *req); | |
30 | static void __unregister_linger_request(struct ceph_osd_client *osdc, | |
31 | struct ceph_osd_request *req); | |
56e925b6 SW |
32 | static void __send_request(struct ceph_osd_client *osdc, |
33 | struct ceph_osd_request *req); | |
f24e9980 | 34 | |
68b4476b YS |
35 | static int op_has_extent(int op) |
36 | { | |
37 | return (op == CEPH_OSD_OP_READ || | |
38 | op == CEPH_OSD_OP_WRITE); | |
39 | } | |
40 | ||
f24e9980 SW |
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, |
47a05811 | 67 | struct ceph_osd_req_op *op, u64 *bno) |
f24e9980 | 68 | { |
60e56f13 | 69 | u64 orig_len = *plen; |
60e56f13 AE |
70 | u64 objoff = 0; |
71 | u64 objlen = 0; | |
d63b77f4 | 72 | int r; |
f24e9980 | 73 | |
60e56f13 | 74 | /* object extent? */ |
47a05811 | 75 | r = ceph_calc_file_object_mapping(layout, off, orig_len, bno, |
60e56f13 | 76 | &objoff, &objlen); |
d63b77f4 SW |
77 | if (r < 0) |
78 | return r; | |
60e56f13 AE |
79 | if (objlen < orig_len) { |
80 | *plen = objlen; | |
81 | dout(" skipping last %llu, final file extent %llu~%llu\n", | |
82 | orig_len - *plen, off, *plen); | |
83 | } | |
84 | ||
85 | if (op_has_extent(op->op)) { | |
86 | u32 osize = le32_to_cpu(layout->fl_object_size); | |
87 | op->extent.offset = objoff; | |
88 | op->extent.length = objlen; | |
89 | if (op->extent.truncate_size <= off - objoff) { | |
90 | op->extent.truncate_size = 0; | |
91 | } else { | |
92 | op->extent.truncate_size -= off - objoff; | |
93 | if (op->extent.truncate_size > osize) | |
94 | op->extent.truncate_size = osize; | |
95 | } | |
96 | } | |
60e56f13 AE |
97 | if (op->op == CEPH_OSD_OP_WRITE) |
98 | op->payload_len = *plen; | |
99 | ||
60cf5992 | 100 | dout("calc_layout bno=%llx %llu~%llu\n", *bno, objoff, objlen); |
f24e9980 | 101 | |
3ff5f385 | 102 | return 0; |
f24e9980 SW |
103 | } |
104 | ||
f24e9980 SW |
105 | /* |
106 | * requests | |
107 | */ | |
415e49a9 | 108 | void ceph_osdc_release_request(struct kref *kref) |
f24e9980 | 109 | { |
e0c59487 | 110 | int num_pages; |
415e49a9 SW |
111 | struct ceph_osd_request *req = container_of(kref, |
112 | struct ceph_osd_request, | |
113 | r_kref); | |
114 | ||
115 | if (req->r_request) | |
116 | ceph_msg_put(req->r_request); | |
0d59ab81 | 117 | if (req->r_con_filling_msg) { |
9cbb1d72 AE |
118 | dout("%s revoking msg %p from con %p\n", __func__, |
119 | req->r_reply, req->r_con_filling_msg); | |
8921d114 | 120 | ceph_msg_revoke_incoming(req->r_reply); |
0d47766f | 121 | req->r_con_filling_msg->ops->put(req->r_con_filling_msg); |
9cbb1d72 | 122 | req->r_con_filling_msg = NULL; |
350b1c32 | 123 | } |
ab8cb34a AE |
124 | if (req->r_reply) |
125 | ceph_msg_put(req->r_reply); | |
0fff87ec AE |
126 | |
127 | if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES && | |
e0c59487 AE |
128 | req->r_data_in.own_pages) { |
129 | num_pages = calc_pages_for((u64)req->r_data_in.alignment, | |
130 | (u64)req->r_data_in.length); | |
131 | ceph_release_page_vector(req->r_data_in.pages, num_pages); | |
132 | } | |
0fff87ec | 133 | if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES && |
e0c59487 AE |
134 | req->r_data_out.own_pages) { |
135 | num_pages = calc_pages_for((u64)req->r_data_out.alignment, | |
136 | (u64)req->r_data_out.length); | |
137 | ceph_release_page_vector(req->r_data_out.pages, num_pages); | |
138 | } | |
0fff87ec | 139 | |
415e49a9 | 140 | ceph_put_snap_context(req->r_snapc); |
c885837f | 141 | ceph_pagelist_release(&req->r_trail); |
415e49a9 SW |
142 | if (req->r_mempool) |
143 | mempool_free(req, req->r_osdc->req_mempool); | |
144 | else | |
145 | kfree(req); | |
f24e9980 | 146 | } |
3d14c5d2 | 147 | EXPORT_SYMBOL(ceph_osdc_release_request); |
68b4476b | 148 | |
3499e8a5 | 149 | struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc, |
f24e9980 | 150 | struct ceph_snap_context *snapc, |
1b83bef2 | 151 | unsigned int num_ops, |
3499e8a5 | 152 | bool use_mempool, |
54a54007 | 153 | gfp_t gfp_flags) |
f24e9980 SW |
154 | { |
155 | struct ceph_osd_request *req; | |
156 | struct ceph_msg *msg; | |
1b83bef2 SW |
157 | size_t msg_size; |
158 | ||
159 | msg_size = 4 + 4 + 8 + 8 + 4+8; | |
160 | msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */ | |
161 | msg_size += 1 + 8 + 4 + 4; /* pg_t */ | |
162 | msg_size += 4 + MAX_OBJ_NAME_SIZE; | |
163 | msg_size += 2 + num_ops*sizeof(struct ceph_osd_op); | |
164 | msg_size += 8; /* snapid */ | |
165 | msg_size += 8; /* snap_seq */ | |
166 | msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */ | |
167 | msg_size += 4; | |
f24e9980 SW |
168 | |
169 | if (use_mempool) { | |
3499e8a5 | 170 | req = mempool_alloc(osdc->req_mempool, gfp_flags); |
f24e9980 SW |
171 | memset(req, 0, sizeof(*req)); |
172 | } else { | |
3499e8a5 | 173 | req = kzalloc(sizeof(*req), gfp_flags); |
f24e9980 SW |
174 | } |
175 | if (req == NULL) | |
a79832f2 | 176 | return NULL; |
f24e9980 | 177 | |
f24e9980 SW |
178 | req->r_osdc = osdc; |
179 | req->r_mempool = use_mempool; | |
68b4476b | 180 | |
415e49a9 | 181 | kref_init(&req->r_kref); |
f24e9980 SW |
182 | init_completion(&req->r_completion); |
183 | init_completion(&req->r_safe_completion); | |
a978fa20 | 184 | RB_CLEAR_NODE(&req->r_node); |
f24e9980 | 185 | INIT_LIST_HEAD(&req->r_unsafe_item); |
a40c4f10 YS |
186 | INIT_LIST_HEAD(&req->r_linger_item); |
187 | INIT_LIST_HEAD(&req->r_linger_osd); | |
935b639a | 188 | INIT_LIST_HEAD(&req->r_req_lru_item); |
cd43045c SW |
189 | INIT_LIST_HEAD(&req->r_osd_item); |
190 | ||
c16e7869 SW |
191 | /* create reply message */ |
192 | if (use_mempool) | |
193 | msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0); | |
194 | else | |
195 | msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, | |
b61c2763 | 196 | OSD_OPREPLY_FRONT_LEN, gfp_flags, true); |
a79832f2 | 197 | if (!msg) { |
c16e7869 | 198 | ceph_osdc_put_request(req); |
a79832f2 | 199 | return NULL; |
c16e7869 SW |
200 | } |
201 | req->r_reply = msg; | |
202 | ||
0fff87ec AE |
203 | req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE; |
204 | req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE; | |
c885837f | 205 | ceph_pagelist_init(&req->r_trail); |
d50b409f | 206 | |
c16e7869 | 207 | /* create request message; allow space for oid */ |
f24e9980 | 208 | if (use_mempool) |
8f3bc053 | 209 | msg = ceph_msgpool_get(&osdc->msgpool_op, 0); |
f24e9980 | 210 | else |
b61c2763 | 211 | msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true); |
a79832f2 | 212 | if (!msg) { |
f24e9980 | 213 | ceph_osdc_put_request(req); |
a79832f2 | 214 | return NULL; |
f24e9980 | 215 | } |
68b4476b | 216 | |
f24e9980 | 217 | memset(msg->front.iov_base, 0, msg->front.iov_len); |
3499e8a5 YS |
218 | |
219 | req->r_request = msg; | |
3499e8a5 YS |
220 | |
221 | return req; | |
222 | } | |
3d14c5d2 | 223 | EXPORT_SYMBOL(ceph_osdc_alloc_request); |
3499e8a5 | 224 | |
175face2 | 225 | static u64 osd_req_encode_op(struct ceph_osd_request *req, |
68b4476b YS |
226 | struct ceph_osd_op *dst, |
227 | struct ceph_osd_req_op *src) | |
228 | { | |
175face2 AE |
229 | u64 out_data_len = 0; |
230 | u64 tmp; | |
231 | ||
68b4476b YS |
232 | dst->op = cpu_to_le16(src->op); |
233 | ||
065a68f9 | 234 | switch (src->op) { |
fbfab539 AE |
235 | case CEPH_OSD_OP_STAT: |
236 | break; | |
68b4476b YS |
237 | case CEPH_OSD_OP_READ: |
238 | case CEPH_OSD_OP_WRITE: | |
175face2 AE |
239 | if (src->op == CEPH_OSD_OP_WRITE) |
240 | out_data_len = src->extent.length; | |
241 | dst->extent.offset = cpu_to_le64(src->extent.offset); | |
242 | dst->extent.length = cpu_to_le64(src->extent.length); | |
68b4476b YS |
243 | dst->extent.truncate_size = |
244 | cpu_to_le64(src->extent.truncate_size); | |
245 | dst->extent.truncate_seq = | |
246 | cpu_to_le32(src->extent.truncate_seq); | |
247 | break; | |
ae1533b6 | 248 | case CEPH_OSD_OP_CALL: |
ae1533b6 YS |
249 | dst->cls.class_len = src->cls.class_len; |
250 | dst->cls.method_len = src->cls.method_len; | |
251 | dst->cls.indata_len = cpu_to_le32(src->cls.indata_len); | |
252 | ||
175face2 | 253 | tmp = req->r_trail.length; |
c885837f | 254 | ceph_pagelist_append(&req->r_trail, src->cls.class_name, |
ae1533b6 | 255 | src->cls.class_len); |
c885837f | 256 | ceph_pagelist_append(&req->r_trail, src->cls.method_name, |
ae1533b6 | 257 | src->cls.method_len); |
c885837f | 258 | ceph_pagelist_append(&req->r_trail, src->cls.indata, |
ae1533b6 | 259 | src->cls.indata_len); |
175face2 | 260 | out_data_len = req->r_trail.length - tmp; |
ae1533b6 | 261 | break; |
68b4476b YS |
262 | case CEPH_OSD_OP_STARTSYNC: |
263 | break; | |
a40c4f10 YS |
264 | case CEPH_OSD_OP_NOTIFY_ACK: |
265 | case CEPH_OSD_OP_WATCH: | |
266 | dst->watch.cookie = cpu_to_le64(src->watch.cookie); | |
267 | dst->watch.ver = cpu_to_le64(src->watch.ver); | |
268 | dst->watch.flag = src->watch.flag; | |
269 | break; | |
68b4476b | 270 | default: |
8f63ca2d | 271 | pr_err("unrecognized osd opcode %d\n", src->op); |
68b4476b YS |
272 | WARN_ON(1); |
273 | break; | |
4c46459c AE |
274 | case CEPH_OSD_OP_MAPEXT: |
275 | case CEPH_OSD_OP_MASKTRUNC: | |
276 | case CEPH_OSD_OP_SPARSE_READ: | |
a9f36c3e | 277 | case CEPH_OSD_OP_NOTIFY: |
4c46459c AE |
278 | case CEPH_OSD_OP_ASSERT_VER: |
279 | case CEPH_OSD_OP_WRITEFULL: | |
280 | case CEPH_OSD_OP_TRUNCATE: | |
281 | case CEPH_OSD_OP_ZERO: | |
282 | case CEPH_OSD_OP_DELETE: | |
283 | case CEPH_OSD_OP_APPEND: | |
284 | case CEPH_OSD_OP_SETTRUNC: | |
285 | case CEPH_OSD_OP_TRIMTRUNC: | |
286 | case CEPH_OSD_OP_TMAPUP: | |
287 | case CEPH_OSD_OP_TMAPPUT: | |
288 | case CEPH_OSD_OP_TMAPGET: | |
289 | case CEPH_OSD_OP_CREATE: | |
a9f36c3e | 290 | case CEPH_OSD_OP_ROLLBACK: |
4c46459c AE |
291 | case CEPH_OSD_OP_OMAPGETKEYS: |
292 | case CEPH_OSD_OP_OMAPGETVALS: | |
293 | case CEPH_OSD_OP_OMAPGETHEADER: | |
294 | case CEPH_OSD_OP_OMAPGETVALSBYKEYS: | |
295 | case CEPH_OSD_OP_MODE_RD: | |
296 | case CEPH_OSD_OP_OMAPSETVALS: | |
297 | case CEPH_OSD_OP_OMAPSETHEADER: | |
298 | case CEPH_OSD_OP_OMAPCLEAR: | |
299 | case CEPH_OSD_OP_OMAPRMKEYS: | |
300 | case CEPH_OSD_OP_OMAP_CMP: | |
301 | case CEPH_OSD_OP_CLONERANGE: | |
302 | case CEPH_OSD_OP_ASSERT_SRC_VERSION: | |
303 | case CEPH_OSD_OP_SRC_CMPXATTR: | |
a9f36c3e | 304 | case CEPH_OSD_OP_GETXATTR: |
4c46459c | 305 | case CEPH_OSD_OP_GETXATTRS: |
a9f36c3e AE |
306 | case CEPH_OSD_OP_CMPXATTR: |
307 | case CEPH_OSD_OP_SETXATTR: | |
4c46459c AE |
308 | case CEPH_OSD_OP_SETXATTRS: |
309 | case CEPH_OSD_OP_RESETXATTRS: | |
310 | case CEPH_OSD_OP_RMXATTR: | |
311 | case CEPH_OSD_OP_PULL: | |
312 | case CEPH_OSD_OP_PUSH: | |
313 | case CEPH_OSD_OP_BALANCEREADS: | |
314 | case CEPH_OSD_OP_UNBALANCEREADS: | |
315 | case CEPH_OSD_OP_SCRUB: | |
316 | case CEPH_OSD_OP_SCRUB_RESERVE: | |
317 | case CEPH_OSD_OP_SCRUB_UNRESERVE: | |
318 | case CEPH_OSD_OP_SCRUB_STOP: | |
319 | case CEPH_OSD_OP_SCRUB_MAP: | |
320 | case CEPH_OSD_OP_WRLOCK: | |
321 | case CEPH_OSD_OP_WRUNLOCK: | |
322 | case CEPH_OSD_OP_RDLOCK: | |
323 | case CEPH_OSD_OP_RDUNLOCK: | |
324 | case CEPH_OSD_OP_UPLOCK: | |
325 | case CEPH_OSD_OP_DNLOCK: | |
326 | case CEPH_OSD_OP_PGLS: | |
327 | case CEPH_OSD_OP_PGLS_FILTER: | |
328 | pr_err("unsupported osd opcode %s\n", | |
8f63ca2d | 329 | ceph_osd_op_name(src->op)); |
4c46459c AE |
330 | WARN_ON(1); |
331 | break; | |
68b4476b YS |
332 | } |
333 | dst->payload_len = cpu_to_le32(src->payload_len); | |
175face2 AE |
334 | |
335 | return out_data_len; | |
68b4476b YS |
336 | } |
337 | ||
3499e8a5 YS |
338 | /* |
339 | * build new request AND message | |
340 | * | |
341 | */ | |
342 | void ceph_osdc_build_request(struct ceph_osd_request *req, | |
175face2 | 343 | u64 off, unsigned int num_ops, |
68b4476b | 344 | struct ceph_osd_req_op *src_ops, |
4d6b250b | 345 | struct ceph_snap_context *snapc, u64 snap_id, |
af77f26c | 346 | struct timespec *mtime) |
3499e8a5 YS |
347 | { |
348 | struct ceph_msg *msg = req->r_request; | |
68b4476b | 349 | struct ceph_osd_req_op *src_op; |
3499e8a5 | 350 | void *p; |
1b83bef2 | 351 | size_t msg_size; |
3499e8a5 | 352 | int flags = req->r_flags; |
f44246e3 | 353 | u64 data_len; |
68b4476b | 354 | int i; |
3499e8a5 | 355 | |
1b83bef2 SW |
356 | req->r_num_ops = num_ops; |
357 | req->r_snapid = snap_id; | |
f24e9980 SW |
358 | req->r_snapc = ceph_get_snap_context(snapc); |
359 | ||
1b83bef2 SW |
360 | /* encode request */ |
361 | msg->hdr.version = cpu_to_le16(4); | |
362 | ||
363 | p = msg->front.iov_base; | |
364 | ceph_encode_32(&p, 1); /* client_inc is always 1 */ | |
365 | req->r_request_osdmap_epoch = p; | |
366 | p += 4; | |
367 | req->r_request_flags = p; | |
368 | p += 4; | |
369 | if (req->r_flags & CEPH_OSD_FLAG_WRITE) | |
370 | ceph_encode_timespec(p, mtime); | |
371 | p += sizeof(struct ceph_timespec); | |
372 | req->r_request_reassert_version = p; | |
373 | p += sizeof(struct ceph_eversion); /* will get filled in */ | |
374 | ||
375 | /* oloc */ | |
376 | ceph_encode_8(&p, 4); | |
377 | ceph_encode_8(&p, 4); | |
378 | ceph_encode_32(&p, 8 + 4 + 4); | |
379 | req->r_request_pool = p; | |
380 | p += 8; | |
381 | ceph_encode_32(&p, -1); /* preferred */ | |
382 | ceph_encode_32(&p, 0); /* key len */ | |
383 | ||
384 | ceph_encode_8(&p, 1); | |
385 | req->r_request_pgid = p; | |
386 | p += 8 + 4; | |
387 | ceph_encode_32(&p, -1); /* preferred */ | |
388 | ||
389 | /* oid */ | |
390 | ceph_encode_32(&p, req->r_oid_len); | |
af77f26c | 391 | memcpy(p, req->r_oid, req->r_oid_len); |
1b83bef2 | 392 | dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len); |
af77f26c | 393 | p += req->r_oid_len; |
f24e9980 | 394 | |
175face2 | 395 | /* ops--can imply data */ |
1b83bef2 | 396 | ceph_encode_16(&p, num_ops); |
68b4476b | 397 | src_op = src_ops; |
1b83bef2 | 398 | req->r_request_ops = p; |
175face2 | 399 | data_len = 0; |
1b83bef2 | 400 | for (i = 0; i < num_ops; i++, src_op++) { |
175face2 | 401 | data_len += osd_req_encode_op(req, p, src_op); |
1b83bef2 SW |
402 | p += sizeof(struct ceph_osd_op); |
403 | } | |
68b4476b | 404 | |
1b83bef2 SW |
405 | /* snaps */ |
406 | ceph_encode_64(&p, req->r_snapid); | |
407 | ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0); | |
408 | ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0); | |
409 | if (req->r_snapc) { | |
f24e9980 | 410 | for (i = 0; i < snapc->num_snaps; i++) { |
1b83bef2 | 411 | ceph_encode_64(&p, req->r_snapc->snaps[i]); |
f24e9980 SW |
412 | } |
413 | } | |
414 | ||
1b83bef2 SW |
415 | req->r_request_attempts = p; |
416 | p += 4; | |
417 | ||
175face2 AE |
418 | /* data */ |
419 | if (flags & CEPH_OSD_FLAG_WRITE) | |
68b4476b | 420 | req->r_request->hdr.data_off = cpu_to_le16(off); |
f44246e3 | 421 | req->r_request->hdr.data_len = cpu_to_le32(data_len); |
c5c6b19d | 422 | |
f24e9980 | 423 | BUG_ON(p > msg->front.iov_base + msg->front.iov_len); |
6f863e71 SW |
424 | msg_size = p - msg->front.iov_base; |
425 | msg->front.iov_len = msg_size; | |
426 | msg->hdr.front_len = cpu_to_le32(msg_size); | |
1b83bef2 SW |
427 | |
428 | dout("build_request msg_size was %d num_ops %d\n", (int)msg_size, | |
429 | num_ops); | |
3499e8a5 YS |
430 | return; |
431 | } | |
3d14c5d2 | 432 | EXPORT_SYMBOL(ceph_osdc_build_request); |
3499e8a5 YS |
433 | |
434 | /* | |
435 | * build new request AND message, calculate layout, and adjust file | |
436 | * extent as needed. | |
437 | * | |
438 | * if the file was recently truncated, we include information about its | |
439 | * old and new size so that the object can be updated appropriately. (we | |
440 | * avoid synchronously deleting truncated objects because it's slow.) | |
441 | * | |
442 | * if @do_sync, include a 'startsync' command so that the osd will flush | |
443 | * data quickly. | |
444 | */ | |
445 | struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, | |
446 | struct ceph_file_layout *layout, | |
447 | struct ceph_vino vino, | |
448 | u64 off, u64 *plen, | |
449 | int opcode, int flags, | |
450 | struct ceph_snap_context *snapc, | |
451 | int do_sync, | |
452 | u32 truncate_seq, | |
453 | u64 truncate_size, | |
454 | struct timespec *mtime, | |
153e5167 | 455 | bool use_mempool) |
3499e8a5 | 456 | { |
ae7ca4a3 | 457 | struct ceph_osd_req_op ops[2]; |
68b4476b | 458 | struct ceph_osd_request *req; |
ae7ca4a3 | 459 | unsigned int num_op = 1; |
47a05811 | 460 | u64 bno = 0; |
6816282d | 461 | int r; |
68b4476b | 462 | |
ae7ca4a3 AE |
463 | memset(&ops, 0, sizeof ops); |
464 | ||
68b4476b YS |
465 | ops[0].op = opcode; |
466 | ops[0].extent.truncate_seq = truncate_seq; | |
467 | ops[0].extent.truncate_size = truncate_size; | |
68b4476b YS |
468 | |
469 | if (do_sync) { | |
470 | ops[1].op = CEPH_OSD_OP_STARTSYNC; | |
ae7ca4a3 AE |
471 | num_op++; |
472 | } | |
68b4476b | 473 | |
ae7ca4a3 AE |
474 | req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool, |
475 | GFP_NOFS); | |
4ad12621 | 476 | if (!req) |
6816282d | 477 | return ERR_PTR(-ENOMEM); |
d178a9e7 | 478 | req->r_flags = flags; |
3499e8a5 YS |
479 | |
480 | /* calculate max write size */ | |
60cf5992 | 481 | r = calc_layout(layout, off, plen, ops, &bno); |
3ff5f385 AE |
482 | if (r < 0) { |
483 | ceph_osdc_put_request(req); | |
6816282d | 484 | return ERR_PTR(r); |
3ff5f385 | 485 | } |
3499e8a5 YS |
486 | req->r_file_layout = *layout; /* keep a copy */ |
487 | ||
dbe0fc41 AE |
488 | snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno); |
489 | req->r_oid_len = strlen(req->r_oid); | |
490 | ||
175face2 | 491 | ceph_osdc_build_request(req, off, num_op, ops, |
ae7ca4a3 | 492 | snapc, vino.snap, mtime); |
3499e8a5 | 493 | |
f24e9980 SW |
494 | return req; |
495 | } | |
3d14c5d2 | 496 | EXPORT_SYMBOL(ceph_osdc_new_request); |
f24e9980 SW |
497 | |
498 | /* | |
499 | * We keep osd requests in an rbtree, sorted by ->r_tid. | |
500 | */ | |
501 | static void __insert_request(struct ceph_osd_client *osdc, | |
502 | struct ceph_osd_request *new) | |
503 | { | |
504 | struct rb_node **p = &osdc->requests.rb_node; | |
505 | struct rb_node *parent = NULL; | |
506 | struct ceph_osd_request *req = NULL; | |
507 | ||
508 | while (*p) { | |
509 | parent = *p; | |
510 | req = rb_entry(parent, struct ceph_osd_request, r_node); | |
511 | if (new->r_tid < req->r_tid) | |
512 | p = &(*p)->rb_left; | |
513 | else if (new->r_tid > req->r_tid) | |
514 | p = &(*p)->rb_right; | |
515 | else | |
516 | BUG(); | |
517 | } | |
518 | ||
519 | rb_link_node(&new->r_node, parent, p); | |
520 | rb_insert_color(&new->r_node, &osdc->requests); | |
521 | } | |
522 | ||
523 | static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc, | |
524 | u64 tid) | |
525 | { | |
526 | struct ceph_osd_request *req; | |
527 | struct rb_node *n = osdc->requests.rb_node; | |
528 | ||
529 | while (n) { | |
530 | req = rb_entry(n, struct ceph_osd_request, r_node); | |
531 | if (tid < req->r_tid) | |
532 | n = n->rb_left; | |
533 | else if (tid > req->r_tid) | |
534 | n = n->rb_right; | |
535 | else | |
536 | return req; | |
537 | } | |
538 | return NULL; | |
539 | } | |
540 | ||
541 | static struct ceph_osd_request * | |
542 | __lookup_request_ge(struct ceph_osd_client *osdc, | |
543 | u64 tid) | |
544 | { | |
545 | struct ceph_osd_request *req; | |
546 | struct rb_node *n = osdc->requests.rb_node; | |
547 | ||
548 | while (n) { | |
549 | req = rb_entry(n, struct ceph_osd_request, r_node); | |
550 | if (tid < req->r_tid) { | |
551 | if (!n->rb_left) | |
552 | return req; | |
553 | n = n->rb_left; | |
554 | } else if (tid > req->r_tid) { | |
555 | n = n->rb_right; | |
556 | } else { | |
557 | return req; | |
558 | } | |
559 | } | |
560 | return NULL; | |
561 | } | |
562 | ||
6f6c7006 SW |
563 | /* |
564 | * Resubmit requests pending on the given osd. | |
565 | */ | |
566 | static void __kick_osd_requests(struct ceph_osd_client *osdc, | |
567 | struct ceph_osd *osd) | |
568 | { | |
a40c4f10 | 569 | struct ceph_osd_request *req, *nreq; |
6f6c7006 SW |
570 | int err; |
571 | ||
572 | dout("__kick_osd_requests osd%d\n", osd->o_osd); | |
573 | err = __reset_osd(osdc, osd); | |
685a7555 | 574 | if (err) |
6f6c7006 SW |
575 | return; |
576 | ||
577 | list_for_each_entry(req, &osd->o_requests, r_osd_item) { | |
578 | list_move(&req->r_req_lru_item, &osdc->req_unsent); | |
579 | dout("requeued %p tid %llu osd%d\n", req, req->r_tid, | |
580 | osd->o_osd); | |
a40c4f10 YS |
581 | if (!req->r_linger) |
582 | req->r_flags |= CEPH_OSD_FLAG_RETRY; | |
583 | } | |
584 | ||
585 | list_for_each_entry_safe(req, nreq, &osd->o_linger_requests, | |
586 | r_linger_osd) { | |
77f38e0e SW |
587 | /* |
588 | * reregister request prior to unregistering linger so | |
589 | * that r_osd is preserved. | |
590 | */ | |
591 | BUG_ON(!list_empty(&req->r_req_lru_item)); | |
a40c4f10 | 592 | __register_request(osdc, req); |
77f38e0e SW |
593 | list_add(&req->r_req_lru_item, &osdc->req_unsent); |
594 | list_add(&req->r_osd_item, &req->r_osd->o_requests); | |
595 | __unregister_linger_request(osdc, req); | |
a40c4f10 YS |
596 | dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid, |
597 | osd->o_osd); | |
6f6c7006 SW |
598 | } |
599 | } | |
600 | ||
f24e9980 | 601 | /* |
81b024e7 | 602 | * If the osd connection drops, we need to resubmit all requests. |
f24e9980 SW |
603 | */ |
604 | static void osd_reset(struct ceph_connection *con) | |
605 | { | |
606 | struct ceph_osd *osd = con->private; | |
607 | struct ceph_osd_client *osdc; | |
608 | ||
609 | if (!osd) | |
610 | return; | |
611 | dout("osd_reset osd%d\n", osd->o_osd); | |
612 | osdc = osd->o_osdc; | |
f24e9980 | 613 | down_read(&osdc->map_sem); |
83aff95e SW |
614 | mutex_lock(&osdc->request_mutex); |
615 | __kick_osd_requests(osdc, osd); | |
f9d25199 | 616 | __send_queued(osdc); |
83aff95e | 617 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
618 | up_read(&osdc->map_sem); |
619 | } | |
620 | ||
621 | /* | |
622 | * Track open sessions with osds. | |
623 | */ | |
e10006f8 | 624 | static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum) |
f24e9980 SW |
625 | { |
626 | struct ceph_osd *osd; | |
627 | ||
628 | osd = kzalloc(sizeof(*osd), GFP_NOFS); | |
629 | if (!osd) | |
630 | return NULL; | |
631 | ||
632 | atomic_set(&osd->o_ref, 1); | |
633 | osd->o_osdc = osdc; | |
e10006f8 | 634 | osd->o_osd = onum; |
f407731d | 635 | RB_CLEAR_NODE(&osd->o_node); |
f24e9980 | 636 | INIT_LIST_HEAD(&osd->o_requests); |
a40c4f10 | 637 | INIT_LIST_HEAD(&osd->o_linger_requests); |
f5a2041b | 638 | INIT_LIST_HEAD(&osd->o_osd_lru); |
f24e9980 SW |
639 | osd->o_incarnation = 1; |
640 | ||
b7a9e5dd | 641 | ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr); |
4e7a5dcd | 642 | |
422d2cb8 | 643 | INIT_LIST_HEAD(&osd->o_keepalive_item); |
f24e9980 SW |
644 | return osd; |
645 | } | |
646 | ||
647 | static struct ceph_osd *get_osd(struct ceph_osd *osd) | |
648 | { | |
649 | if (atomic_inc_not_zero(&osd->o_ref)) { | |
650 | dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1, | |
651 | atomic_read(&osd->o_ref)); | |
652 | return osd; | |
653 | } else { | |
654 | dout("get_osd %p FAIL\n", osd); | |
655 | return NULL; | |
656 | } | |
657 | } | |
658 | ||
659 | static void put_osd(struct ceph_osd *osd) | |
660 | { | |
661 | dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref), | |
662 | atomic_read(&osd->o_ref) - 1); | |
a255651d | 663 | if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) { |
79494d1b SW |
664 | struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth; |
665 | ||
a255651d | 666 | if (ac->ops && ac->ops->destroy_authorizer) |
6c4a1915 | 667 | ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer); |
f24e9980 | 668 | kfree(osd); |
79494d1b | 669 | } |
f24e9980 SW |
670 | } |
671 | ||
672 | /* | |
673 | * remove an osd from our map | |
674 | */ | |
f5a2041b | 675 | static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) |
f24e9980 | 676 | { |
f5a2041b | 677 | dout("__remove_osd %p\n", osd); |
f24e9980 SW |
678 | BUG_ON(!list_empty(&osd->o_requests)); |
679 | rb_erase(&osd->o_node, &osdc->osds); | |
f5a2041b | 680 | list_del_init(&osd->o_osd_lru); |
f24e9980 SW |
681 | ceph_con_close(&osd->o_con); |
682 | put_osd(osd); | |
683 | } | |
684 | ||
aca420bc SW |
685 | static void remove_all_osds(struct ceph_osd_client *osdc) |
686 | { | |
048a9d2d | 687 | dout("%s %p\n", __func__, osdc); |
aca420bc SW |
688 | mutex_lock(&osdc->request_mutex); |
689 | while (!RB_EMPTY_ROOT(&osdc->osds)) { | |
690 | struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds), | |
691 | struct ceph_osd, o_node); | |
692 | __remove_osd(osdc, osd); | |
693 | } | |
694 | mutex_unlock(&osdc->request_mutex); | |
695 | } | |
696 | ||
f5a2041b YS |
697 | static void __move_osd_to_lru(struct ceph_osd_client *osdc, |
698 | struct ceph_osd *osd) | |
699 | { | |
700 | dout("__move_osd_to_lru %p\n", osd); | |
701 | BUG_ON(!list_empty(&osd->o_osd_lru)); | |
702 | list_add_tail(&osd->o_osd_lru, &osdc->osd_lru); | |
3d14c5d2 | 703 | osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ; |
f5a2041b YS |
704 | } |
705 | ||
706 | static void __remove_osd_from_lru(struct ceph_osd *osd) | |
707 | { | |
708 | dout("__remove_osd_from_lru %p\n", osd); | |
709 | if (!list_empty(&osd->o_osd_lru)) | |
710 | list_del_init(&osd->o_osd_lru); | |
711 | } | |
712 | ||
aca420bc | 713 | static void remove_old_osds(struct ceph_osd_client *osdc) |
f5a2041b YS |
714 | { |
715 | struct ceph_osd *osd, *nosd; | |
716 | ||
717 | dout("__remove_old_osds %p\n", osdc); | |
718 | mutex_lock(&osdc->request_mutex); | |
719 | list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) { | |
aca420bc | 720 | if (time_before(jiffies, osd->lru_ttl)) |
f5a2041b YS |
721 | break; |
722 | __remove_osd(osdc, osd); | |
723 | } | |
724 | mutex_unlock(&osdc->request_mutex); | |
725 | } | |
726 | ||
f24e9980 SW |
727 | /* |
728 | * reset osd connect | |
729 | */ | |
f5a2041b | 730 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) |
f24e9980 | 731 | { |
c3acb181 | 732 | struct ceph_entity_addr *peer_addr; |
f24e9980 | 733 | |
f5a2041b | 734 | dout("__reset_osd %p osd%d\n", osd, osd->o_osd); |
a40c4f10 YS |
735 | if (list_empty(&osd->o_requests) && |
736 | list_empty(&osd->o_linger_requests)) { | |
f5a2041b | 737 | __remove_osd(osdc, osd); |
c3acb181 AE |
738 | |
739 | return -ENODEV; | |
740 | } | |
741 | ||
742 | peer_addr = &osdc->osdmap->osd_addr[osd->o_osd]; | |
743 | if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) && | |
744 | !ceph_con_opened(&osd->o_con)) { | |
745 | struct ceph_osd_request *req; | |
746 | ||
87b315a5 SW |
747 | dout(" osd addr hasn't changed and connection never opened," |
748 | " letting msgr retry"); | |
749 | /* touch each r_stamp for handle_timeout()'s benfit */ | |
750 | list_for_each_entry(req, &osd->o_requests, r_osd_item) | |
751 | req->r_stamp = jiffies; | |
c3acb181 AE |
752 | |
753 | return -EAGAIN; | |
f24e9980 | 754 | } |
c3acb181 AE |
755 | |
756 | ceph_con_close(&osd->o_con); | |
757 | ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr); | |
758 | osd->o_incarnation++; | |
759 | ||
760 | return 0; | |
f24e9980 SW |
761 | } |
762 | ||
763 | static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new) | |
764 | { | |
765 | struct rb_node **p = &osdc->osds.rb_node; | |
766 | struct rb_node *parent = NULL; | |
767 | struct ceph_osd *osd = NULL; | |
768 | ||
aca420bc | 769 | dout("__insert_osd %p osd%d\n", new, new->o_osd); |
f24e9980 SW |
770 | while (*p) { |
771 | parent = *p; | |
772 | osd = rb_entry(parent, struct ceph_osd, o_node); | |
773 | if (new->o_osd < osd->o_osd) | |
774 | p = &(*p)->rb_left; | |
775 | else if (new->o_osd > osd->o_osd) | |
776 | p = &(*p)->rb_right; | |
777 | else | |
778 | BUG(); | |
779 | } | |
780 | ||
781 | rb_link_node(&new->o_node, parent, p); | |
782 | rb_insert_color(&new->o_node, &osdc->osds); | |
783 | } | |
784 | ||
785 | static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o) | |
786 | { | |
787 | struct ceph_osd *osd; | |
788 | struct rb_node *n = osdc->osds.rb_node; | |
789 | ||
790 | while (n) { | |
791 | osd = rb_entry(n, struct ceph_osd, o_node); | |
792 | if (o < osd->o_osd) | |
793 | n = n->rb_left; | |
794 | else if (o > osd->o_osd) | |
795 | n = n->rb_right; | |
796 | else | |
797 | return osd; | |
798 | } | |
799 | return NULL; | |
800 | } | |
801 | ||
422d2cb8 YS |
802 | static void __schedule_osd_timeout(struct ceph_osd_client *osdc) |
803 | { | |
804 | schedule_delayed_work(&osdc->timeout_work, | |
3d14c5d2 | 805 | osdc->client->options->osd_keepalive_timeout * HZ); |
422d2cb8 YS |
806 | } |
807 | ||
808 | static void __cancel_osd_timeout(struct ceph_osd_client *osdc) | |
809 | { | |
810 | cancel_delayed_work(&osdc->timeout_work); | |
811 | } | |
f24e9980 SW |
812 | |
813 | /* | |
814 | * Register request, assign tid. If this is the first request, set up | |
815 | * the timeout event. | |
816 | */ | |
a40c4f10 YS |
817 | static void __register_request(struct ceph_osd_client *osdc, |
818 | struct ceph_osd_request *req) | |
f24e9980 | 819 | { |
f24e9980 | 820 | req->r_tid = ++osdc->last_tid; |
6df058c0 | 821 | req->r_request->hdr.tid = cpu_to_le64(req->r_tid); |
77f38e0e | 822 | dout("__register_request %p tid %lld\n", req, req->r_tid); |
f24e9980 SW |
823 | __insert_request(osdc, req); |
824 | ceph_osdc_get_request(req); | |
825 | osdc->num_requests++; | |
f24e9980 | 826 | if (osdc->num_requests == 1) { |
422d2cb8 YS |
827 | dout(" first request, scheduling timeout\n"); |
828 | __schedule_osd_timeout(osdc); | |
f24e9980 | 829 | } |
a40c4f10 YS |
830 | } |
831 | ||
832 | static void register_request(struct ceph_osd_client *osdc, | |
833 | struct ceph_osd_request *req) | |
834 | { | |
835 | mutex_lock(&osdc->request_mutex); | |
836 | __register_request(osdc, req); | |
f24e9980 SW |
837 | mutex_unlock(&osdc->request_mutex); |
838 | } | |
839 | ||
840 | /* | |
841 | * called under osdc->request_mutex | |
842 | */ | |
843 | static void __unregister_request(struct ceph_osd_client *osdc, | |
844 | struct ceph_osd_request *req) | |
845 | { | |
35f9f8a0 SW |
846 | if (RB_EMPTY_NODE(&req->r_node)) { |
847 | dout("__unregister_request %p tid %lld not registered\n", | |
848 | req, req->r_tid); | |
849 | return; | |
850 | } | |
851 | ||
f24e9980 SW |
852 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); |
853 | rb_erase(&req->r_node, &osdc->requests); | |
854 | osdc->num_requests--; | |
855 | ||
0ba6478d SW |
856 | if (req->r_osd) { |
857 | /* make sure the original request isn't in flight. */ | |
6740a845 | 858 | ceph_msg_revoke(req->r_request); |
0ba6478d SW |
859 | |
860 | list_del_init(&req->r_osd_item); | |
a40c4f10 YS |
861 | if (list_empty(&req->r_osd->o_requests) && |
862 | list_empty(&req->r_osd->o_linger_requests)) { | |
863 | dout("moving osd to %p lru\n", req->r_osd); | |
f5a2041b | 864 | __move_osd_to_lru(osdc, req->r_osd); |
a40c4f10 | 865 | } |
fbdb9190 | 866 | if (list_empty(&req->r_linger_item)) |
a40c4f10 | 867 | req->r_osd = NULL; |
0ba6478d | 868 | } |
f24e9980 | 869 | |
7d5f2481 | 870 | list_del_init(&req->r_req_lru_item); |
f24e9980 SW |
871 | ceph_osdc_put_request(req); |
872 | ||
422d2cb8 YS |
873 | if (osdc->num_requests == 0) { |
874 | dout(" no requests, canceling timeout\n"); | |
875 | __cancel_osd_timeout(osdc); | |
f24e9980 SW |
876 | } |
877 | } | |
878 | ||
879 | /* | |
880 | * Cancel a previously queued request message | |
881 | */ | |
882 | static void __cancel_request(struct ceph_osd_request *req) | |
883 | { | |
6bc18876 | 884 | if (req->r_sent && req->r_osd) { |
6740a845 | 885 | ceph_msg_revoke(req->r_request); |
f24e9980 SW |
886 | req->r_sent = 0; |
887 | } | |
888 | } | |
889 | ||
a40c4f10 YS |
890 | static void __register_linger_request(struct ceph_osd_client *osdc, |
891 | struct ceph_osd_request *req) | |
892 | { | |
893 | dout("__register_linger_request %p\n", req); | |
894 | list_add_tail(&req->r_linger_item, &osdc->req_linger); | |
6194ea89 SW |
895 | if (req->r_osd) |
896 | list_add_tail(&req->r_linger_osd, | |
897 | &req->r_osd->o_linger_requests); | |
a40c4f10 YS |
898 | } |
899 | ||
900 | static void __unregister_linger_request(struct ceph_osd_client *osdc, | |
901 | struct ceph_osd_request *req) | |
902 | { | |
903 | dout("__unregister_linger_request %p\n", req); | |
61c74035 | 904 | list_del_init(&req->r_linger_item); |
a40c4f10 | 905 | if (req->r_osd) { |
a40c4f10 YS |
906 | list_del_init(&req->r_linger_osd); |
907 | ||
908 | if (list_empty(&req->r_osd->o_requests) && | |
909 | list_empty(&req->r_osd->o_linger_requests)) { | |
910 | dout("moving osd to %p lru\n", req->r_osd); | |
911 | __move_osd_to_lru(osdc, req->r_osd); | |
912 | } | |
fbdb9190 SW |
913 | if (list_empty(&req->r_osd_item)) |
914 | req->r_osd = NULL; | |
a40c4f10 YS |
915 | } |
916 | } | |
917 | ||
918 | void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc, | |
919 | struct ceph_osd_request *req) | |
920 | { | |
921 | mutex_lock(&osdc->request_mutex); | |
922 | if (req->r_linger) { | |
923 | __unregister_linger_request(osdc, req); | |
924 | ceph_osdc_put_request(req); | |
925 | } | |
926 | mutex_unlock(&osdc->request_mutex); | |
927 | } | |
928 | EXPORT_SYMBOL(ceph_osdc_unregister_linger_request); | |
929 | ||
930 | void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc, | |
931 | struct ceph_osd_request *req) | |
932 | { | |
933 | if (!req->r_linger) { | |
934 | dout("set_request_linger %p\n", req); | |
935 | req->r_linger = 1; | |
936 | /* | |
937 | * caller is now responsible for calling | |
938 | * unregister_linger_request | |
939 | */ | |
940 | ceph_osdc_get_request(req); | |
941 | } | |
942 | } | |
943 | EXPORT_SYMBOL(ceph_osdc_set_request_linger); | |
944 | ||
f24e9980 SW |
945 | /* |
946 | * Pick an osd (the first 'up' osd in the pg), allocate the osd struct | |
947 | * (as needed), and set the request r_osd appropriately. If there is | |
25985edc | 948 | * no up osd, set r_osd to NULL. Move the request to the appropriate list |
6f6c7006 | 949 | * (unsent, homeless) or leave on in-flight lru. |
f24e9980 SW |
950 | * |
951 | * Return 0 if unchanged, 1 if changed, or negative on error. | |
952 | * | |
953 | * Caller should hold map_sem for read and request_mutex. | |
954 | */ | |
6f6c7006 | 955 | static int __map_request(struct ceph_osd_client *osdc, |
38d6453c | 956 | struct ceph_osd_request *req, int force_resend) |
f24e9980 | 957 | { |
5b191d99 | 958 | struct ceph_pg pgid; |
d85b7056 SW |
959 | int acting[CEPH_PG_MAX_SIZE]; |
960 | int o = -1, num = 0; | |
f24e9980 | 961 | int err; |
f24e9980 | 962 | |
6f6c7006 | 963 | dout("map_request %p tid %lld\n", req, req->r_tid); |
41766f87 AE |
964 | err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap, |
965 | ceph_file_layout_pg_pool(req->r_file_layout)); | |
6f6c7006 SW |
966 | if (err) { |
967 | list_move(&req->r_req_lru_item, &osdc->req_notarget); | |
f24e9980 | 968 | return err; |
6f6c7006 | 969 | } |
7740a42f SW |
970 | req->r_pgid = pgid; |
971 | ||
d85b7056 SW |
972 | err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting); |
973 | if (err > 0) { | |
974 | o = acting[0]; | |
975 | num = err; | |
976 | } | |
f24e9980 | 977 | |
38d6453c SW |
978 | if ((!force_resend && |
979 | req->r_osd && req->r_osd->o_osd == o && | |
d85b7056 SW |
980 | req->r_sent >= req->r_osd->o_incarnation && |
981 | req->r_num_pg_osds == num && | |
982 | memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) || | |
f24e9980 SW |
983 | (req->r_osd == NULL && o == -1)) |
984 | return 0; /* no change */ | |
985 | ||
5b191d99 SW |
986 | dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n", |
987 | req->r_tid, pgid.pool, pgid.seed, o, | |
f24e9980 SW |
988 | req->r_osd ? req->r_osd->o_osd : -1); |
989 | ||
d85b7056 SW |
990 | /* record full pg acting set */ |
991 | memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num); | |
992 | req->r_num_pg_osds = num; | |
993 | ||
f24e9980 SW |
994 | if (req->r_osd) { |
995 | __cancel_request(req); | |
996 | list_del_init(&req->r_osd_item); | |
f24e9980 SW |
997 | req->r_osd = NULL; |
998 | } | |
999 | ||
1000 | req->r_osd = __lookup_osd(osdc, o); | |
1001 | if (!req->r_osd && o >= 0) { | |
c99eb1c7 | 1002 | err = -ENOMEM; |
e10006f8 | 1003 | req->r_osd = create_osd(osdc, o); |
6f6c7006 SW |
1004 | if (!req->r_osd) { |
1005 | list_move(&req->r_req_lru_item, &osdc->req_notarget); | |
c99eb1c7 | 1006 | goto out; |
6f6c7006 | 1007 | } |
f24e9980 | 1008 | |
6f6c7006 | 1009 | dout("map_request osd %p is osd%d\n", req->r_osd, o); |
f24e9980 SW |
1010 | __insert_osd(osdc, req->r_osd); |
1011 | ||
b7a9e5dd SW |
1012 | ceph_con_open(&req->r_osd->o_con, |
1013 | CEPH_ENTITY_TYPE_OSD, o, | |
1014 | &osdc->osdmap->osd_addr[o]); | |
f24e9980 SW |
1015 | } |
1016 | ||
f5a2041b YS |
1017 | if (req->r_osd) { |
1018 | __remove_osd_from_lru(req->r_osd); | |
f24e9980 | 1019 | list_add(&req->r_osd_item, &req->r_osd->o_requests); |
6f6c7006 SW |
1020 | list_move(&req->r_req_lru_item, &osdc->req_unsent); |
1021 | } else { | |
1022 | list_move(&req->r_req_lru_item, &osdc->req_notarget); | |
f5a2041b | 1023 | } |
d85b7056 | 1024 | err = 1; /* osd or pg changed */ |
f24e9980 SW |
1025 | |
1026 | out: | |
f24e9980 SW |
1027 | return err; |
1028 | } | |
1029 | ||
1030 | /* | |
1031 | * caller should hold map_sem (for read) and request_mutex | |
1032 | */ | |
56e925b6 SW |
1033 | static void __send_request(struct ceph_osd_client *osdc, |
1034 | struct ceph_osd_request *req) | |
f24e9980 | 1035 | { |
1b83bef2 | 1036 | void *p; |
f24e9980 | 1037 | |
1b83bef2 SW |
1038 | dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n", |
1039 | req, req->r_tid, req->r_osd->o_osd, req->r_flags, | |
1040 | (unsigned long long)req->r_pgid.pool, req->r_pgid.seed); | |
1041 | ||
1042 | /* fill in message content that changes each time we send it */ | |
1043 | put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch); | |
1044 | put_unaligned_le32(req->r_flags, req->r_request_flags); | |
1045 | put_unaligned_le64(req->r_pgid.pool, req->r_request_pool); | |
1046 | p = req->r_request_pgid; | |
1047 | ceph_encode_64(&p, req->r_pgid.pool); | |
1048 | ceph_encode_32(&p, req->r_pgid.seed); | |
1049 | put_unaligned_le64(1, req->r_request_attempts); /* FIXME */ | |
1050 | memcpy(req->r_request_reassert_version, &req->r_reassert_version, | |
1051 | sizeof(req->r_reassert_version)); | |
2169aea6 | 1052 | |
3dd72fc0 | 1053 | req->r_stamp = jiffies; |
07a27e22 | 1054 | list_move_tail(&req->r_req_lru_item, &osdc->req_lru); |
f24e9980 SW |
1055 | |
1056 | ceph_msg_get(req->r_request); /* send consumes a ref */ | |
1057 | ceph_con_send(&req->r_osd->o_con, req->r_request); | |
1058 | req->r_sent = req->r_osd->o_incarnation; | |
f24e9980 SW |
1059 | } |
1060 | ||
6f6c7006 SW |
1061 | /* |
1062 | * Send any requests in the queue (req_unsent). | |
1063 | */ | |
f9d25199 | 1064 | static void __send_queued(struct ceph_osd_client *osdc) |
6f6c7006 SW |
1065 | { |
1066 | struct ceph_osd_request *req, *tmp; | |
1067 | ||
f9d25199 AE |
1068 | dout("__send_queued\n"); |
1069 | list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) | |
6f6c7006 | 1070 | __send_request(osdc, req); |
6f6c7006 SW |
1071 | } |
1072 | ||
f24e9980 SW |
1073 | /* |
1074 | * Timeout callback, called every N seconds when 1 or more osd | |
1075 | * requests has been active for more than N seconds. When this | |
1076 | * happens, we ping all OSDs with requests who have timed out to | |
1077 | * ensure any communications channel reset is detected. Reset the | |
1078 | * request timeouts another N seconds in the future as we go. | |
1079 | * Reschedule the timeout event another N seconds in future (unless | |
1080 | * there are no open requests). | |
1081 | */ | |
1082 | static void handle_timeout(struct work_struct *work) | |
1083 | { | |
1084 | struct ceph_osd_client *osdc = | |
1085 | container_of(work, struct ceph_osd_client, timeout_work.work); | |
83aff95e | 1086 | struct ceph_osd_request *req; |
f24e9980 | 1087 | struct ceph_osd *osd; |
422d2cb8 | 1088 | unsigned long keepalive = |
3d14c5d2 | 1089 | osdc->client->options->osd_keepalive_timeout * HZ; |
422d2cb8 | 1090 | struct list_head slow_osds; |
f24e9980 SW |
1091 | dout("timeout\n"); |
1092 | down_read(&osdc->map_sem); | |
1093 | ||
1094 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1095 | ||
1096 | mutex_lock(&osdc->request_mutex); | |
f24e9980 | 1097 | |
422d2cb8 YS |
1098 | /* |
1099 | * ping osds that are a bit slow. this ensures that if there | |
1100 | * is a break in the TCP connection we will notice, and reopen | |
1101 | * a connection with that osd (from the fault callback). | |
1102 | */ | |
1103 | INIT_LIST_HEAD(&slow_osds); | |
1104 | list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) { | |
3dd72fc0 | 1105 | if (time_before(jiffies, req->r_stamp + keepalive)) |
422d2cb8 YS |
1106 | break; |
1107 | ||
1108 | osd = req->r_osd; | |
1109 | BUG_ON(!osd); | |
1110 | dout(" tid %llu is slow, will send keepalive on osd%d\n", | |
f24e9980 | 1111 | req->r_tid, osd->o_osd); |
422d2cb8 YS |
1112 | list_move_tail(&osd->o_keepalive_item, &slow_osds); |
1113 | } | |
1114 | while (!list_empty(&slow_osds)) { | |
1115 | osd = list_entry(slow_osds.next, struct ceph_osd, | |
1116 | o_keepalive_item); | |
1117 | list_del_init(&osd->o_keepalive_item); | |
f24e9980 SW |
1118 | ceph_con_keepalive(&osd->o_con); |
1119 | } | |
1120 | ||
422d2cb8 | 1121 | __schedule_osd_timeout(osdc); |
f9d25199 | 1122 | __send_queued(osdc); |
f24e9980 | 1123 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
1124 | up_read(&osdc->map_sem); |
1125 | } | |
1126 | ||
f5a2041b YS |
1127 | static void handle_osds_timeout(struct work_struct *work) |
1128 | { | |
1129 | struct ceph_osd_client *osdc = | |
1130 | container_of(work, struct ceph_osd_client, | |
1131 | osds_timeout_work.work); | |
1132 | unsigned long delay = | |
3d14c5d2 | 1133 | osdc->client->options->osd_idle_ttl * HZ >> 2; |
f5a2041b YS |
1134 | |
1135 | dout("osds timeout\n"); | |
1136 | down_read(&osdc->map_sem); | |
aca420bc | 1137 | remove_old_osds(osdc); |
f5a2041b YS |
1138 | up_read(&osdc->map_sem); |
1139 | ||
1140 | schedule_delayed_work(&osdc->osds_timeout_work, | |
1141 | round_jiffies_relative(delay)); | |
1142 | } | |
1143 | ||
25845472 SW |
1144 | static void complete_request(struct ceph_osd_request *req) |
1145 | { | |
1146 | if (req->r_safe_callback) | |
1147 | req->r_safe_callback(req, NULL); | |
1148 | complete_all(&req->r_safe_completion); /* fsync waiter */ | |
1149 | } | |
1150 | ||
1b83bef2 SW |
1151 | static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid) |
1152 | { | |
1153 | __u8 v; | |
1154 | ||
1155 | ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad); | |
1156 | v = ceph_decode_8(p); | |
1157 | if (v > 1) { | |
1158 | pr_warning("do not understand pg encoding %d > 1", v); | |
1159 | return -EINVAL; | |
1160 | } | |
1161 | pgid->pool = ceph_decode_64(p); | |
1162 | pgid->seed = ceph_decode_32(p); | |
1163 | *p += 4; | |
1164 | return 0; | |
1165 | ||
1166 | bad: | |
1167 | pr_warning("incomplete pg encoding"); | |
1168 | return -EINVAL; | |
1169 | } | |
1170 | ||
f24e9980 SW |
1171 | /* |
1172 | * handle osd op reply. either call the callback if it is specified, | |
1173 | * or do the completion to wake up the waiting thread. | |
1174 | */ | |
350b1c32 SW |
1175 | static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg, |
1176 | struct ceph_connection *con) | |
f24e9980 | 1177 | { |
1b83bef2 | 1178 | void *p, *end; |
f24e9980 SW |
1179 | struct ceph_osd_request *req; |
1180 | u64 tid; | |
1b83bef2 SW |
1181 | int object_len; |
1182 | int numops, payload_len, flags; | |
0ceed5db | 1183 | s32 result; |
1b83bef2 SW |
1184 | s32 retry_attempt; |
1185 | struct ceph_pg pg; | |
1186 | int err; | |
1187 | u32 reassert_epoch; | |
1188 | u64 reassert_version; | |
1189 | u32 osdmap_epoch; | |
0d5af164 | 1190 | int already_completed; |
1b83bef2 | 1191 | int i; |
f24e9980 | 1192 | |
6df058c0 | 1193 | tid = le64_to_cpu(msg->hdr.tid); |
1b83bef2 SW |
1194 | dout("handle_reply %p tid %llu\n", msg, tid); |
1195 | ||
1196 | p = msg->front.iov_base; | |
1197 | end = p + msg->front.iov_len; | |
1198 | ||
1199 | ceph_decode_need(&p, end, 4, bad); | |
1200 | object_len = ceph_decode_32(&p); | |
1201 | ceph_decode_need(&p, end, object_len, bad); | |
1202 | p += object_len; | |
1203 | ||
1204 | err = __decode_pgid(&p, end, &pg); | |
1205 | if (err) | |
f24e9980 | 1206 | goto bad; |
1b83bef2 SW |
1207 | |
1208 | ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad); | |
1209 | flags = ceph_decode_64(&p); | |
1210 | result = ceph_decode_32(&p); | |
1211 | reassert_epoch = ceph_decode_32(&p); | |
1212 | reassert_version = ceph_decode_64(&p); | |
1213 | osdmap_epoch = ceph_decode_32(&p); | |
1214 | ||
f24e9980 SW |
1215 | /* lookup */ |
1216 | mutex_lock(&osdc->request_mutex); | |
1217 | req = __lookup_request(osdc, tid); | |
1218 | if (req == NULL) { | |
1219 | dout("handle_reply tid %llu dne\n", tid); | |
1220 | mutex_unlock(&osdc->request_mutex); | |
1221 | return; | |
1222 | } | |
1223 | ceph_osdc_get_request(req); | |
1b83bef2 SW |
1224 | |
1225 | dout("handle_reply %p tid %llu req %p result %d\n", msg, tid, | |
1226 | req, result); | |
1227 | ||
1228 | ceph_decode_need(&p, end, 4, bad); | |
1229 | numops = ceph_decode_32(&p); | |
1230 | if (numops > CEPH_OSD_MAX_OP) | |
1231 | goto bad_put; | |
1232 | if (numops != req->r_num_ops) | |
1233 | goto bad_put; | |
1234 | payload_len = 0; | |
1235 | ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad); | |
1236 | for (i = 0; i < numops; i++) { | |
1237 | struct ceph_osd_op *op = p; | |
1238 | int len; | |
1239 | ||
1240 | len = le32_to_cpu(op->payload_len); | |
1241 | req->r_reply_op_len[i] = len; | |
1242 | dout(" op %d has %d bytes\n", i, len); | |
1243 | payload_len += len; | |
1244 | p += sizeof(*op); | |
1245 | } | |
1246 | if (payload_len != le32_to_cpu(msg->hdr.data_len)) { | |
1247 | pr_warning("sum of op payload lens %d != data_len %d", | |
1248 | payload_len, le32_to_cpu(msg->hdr.data_len)); | |
1249 | goto bad_put; | |
1250 | } | |
1251 | ||
1252 | ceph_decode_need(&p, end, 4 + numops * 4, bad); | |
1253 | retry_attempt = ceph_decode_32(&p); | |
1254 | for (i = 0; i < numops; i++) | |
1255 | req->r_reply_op_result[i] = ceph_decode_32(&p); | |
f24e9980 | 1256 | |
350b1c32 | 1257 | /* |
0d59ab81 | 1258 | * if this connection filled our message, drop our reference now, to |
350b1c32 SW |
1259 | * avoid a (safe but slower) revoke later. |
1260 | */ | |
0d59ab81 | 1261 | if (req->r_con_filling_msg == con && req->r_reply == msg) { |
c16e7869 | 1262 | dout(" dropping con_filling_msg ref %p\n", con); |
0d59ab81 | 1263 | req->r_con_filling_msg = NULL; |
0d47766f | 1264 | con->ops->put(con); |
350b1c32 SW |
1265 | } |
1266 | ||
f24e9980 | 1267 | if (!req->r_got_reply) { |
95c96174 | 1268 | unsigned int bytes; |
f24e9980 | 1269 | |
1b83bef2 | 1270 | req->r_result = result; |
f24e9980 SW |
1271 | bytes = le32_to_cpu(msg->hdr.data_len); |
1272 | dout("handle_reply result %d bytes %d\n", req->r_result, | |
1273 | bytes); | |
1274 | if (req->r_result == 0) | |
1275 | req->r_result = bytes; | |
1276 | ||
1277 | /* in case this is a write and we need to replay, */ | |
1b83bef2 SW |
1278 | req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch); |
1279 | req->r_reassert_version.version = cpu_to_le64(reassert_version); | |
f24e9980 SW |
1280 | |
1281 | req->r_got_reply = 1; | |
1282 | } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) { | |
1283 | dout("handle_reply tid %llu dup ack\n", tid); | |
34b43a56 | 1284 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
1285 | goto done; |
1286 | } | |
1287 | ||
1288 | dout("handle_reply tid %llu flags %d\n", tid, flags); | |
1289 | ||
a40c4f10 YS |
1290 | if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK)) |
1291 | __register_linger_request(osdc, req); | |
1292 | ||
f24e9980 | 1293 | /* either this is a read, or we got the safe response */ |
0ceed5db SW |
1294 | if (result < 0 || |
1295 | (flags & CEPH_OSD_FLAG_ONDISK) || | |
f24e9980 SW |
1296 | ((flags & CEPH_OSD_FLAG_WRITE) == 0)) |
1297 | __unregister_request(osdc, req); | |
1298 | ||
0d5af164 AE |
1299 | already_completed = req->r_completed; |
1300 | req->r_completed = 1; | |
f24e9980 | 1301 | mutex_unlock(&osdc->request_mutex); |
0d5af164 AE |
1302 | if (already_completed) |
1303 | goto done; | |
f24e9980 SW |
1304 | |
1305 | if (req->r_callback) | |
1306 | req->r_callback(req, msg); | |
1307 | else | |
03066f23 | 1308 | complete_all(&req->r_completion); |
f24e9980 | 1309 | |
25845472 SW |
1310 | if (flags & CEPH_OSD_FLAG_ONDISK) |
1311 | complete_request(req); | |
f24e9980 SW |
1312 | |
1313 | done: | |
a40c4f10 | 1314 | dout("req=%p req->r_linger=%d\n", req, req->r_linger); |
f24e9980 SW |
1315 | ceph_osdc_put_request(req); |
1316 | return; | |
1317 | ||
1b83bef2 SW |
1318 | bad_put: |
1319 | ceph_osdc_put_request(req); | |
f24e9980 | 1320 | bad: |
1b83bef2 SW |
1321 | pr_err("corrupt osd_op_reply got %d %d\n", |
1322 | (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len)); | |
9ec7cab1 | 1323 | ceph_msg_dump(msg); |
f24e9980 SW |
1324 | } |
1325 | ||
6f6c7006 | 1326 | static void reset_changed_osds(struct ceph_osd_client *osdc) |
f24e9980 | 1327 | { |
f24e9980 | 1328 | struct rb_node *p, *n; |
f24e9980 | 1329 | |
6f6c7006 SW |
1330 | for (p = rb_first(&osdc->osds); p; p = n) { |
1331 | struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node); | |
f24e9980 | 1332 | |
6f6c7006 SW |
1333 | n = rb_next(p); |
1334 | if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || | |
1335 | memcmp(&osd->o_con.peer_addr, | |
1336 | ceph_osd_addr(osdc->osdmap, | |
1337 | osd->o_osd), | |
1338 | sizeof(struct ceph_entity_addr)) != 0) | |
1339 | __reset_osd(osdc, osd); | |
f24e9980 | 1340 | } |
422d2cb8 YS |
1341 | } |
1342 | ||
1343 | /* | |
6f6c7006 SW |
1344 | * Requeue requests whose mapping to an OSD has changed. If requests map to |
1345 | * no osd, request a new map. | |
422d2cb8 | 1346 | * |
e6d50f67 | 1347 | * Caller should hold map_sem for read. |
422d2cb8 | 1348 | */ |
38d6453c | 1349 | static void kick_requests(struct ceph_osd_client *osdc, int force_resend) |
422d2cb8 | 1350 | { |
a40c4f10 | 1351 | struct ceph_osd_request *req, *nreq; |
6f6c7006 SW |
1352 | struct rb_node *p; |
1353 | int needmap = 0; | |
1354 | int err; | |
422d2cb8 | 1355 | |
38d6453c | 1356 | dout("kick_requests %s\n", force_resend ? " (force resend)" : ""); |
422d2cb8 | 1357 | mutex_lock(&osdc->request_mutex); |
6194ea89 | 1358 | for (p = rb_first(&osdc->requests); p; ) { |
6f6c7006 | 1359 | req = rb_entry(p, struct ceph_osd_request, r_node); |
6194ea89 | 1360 | p = rb_next(p); |
ab60b16d AE |
1361 | |
1362 | /* | |
1363 | * For linger requests that have not yet been | |
1364 | * registered, move them to the linger list; they'll | |
1365 | * be sent to the osd in the loop below. Unregister | |
1366 | * the request before re-registering it as a linger | |
1367 | * request to ensure the __map_request() below | |
1368 | * will decide it needs to be sent. | |
1369 | */ | |
1370 | if (req->r_linger && list_empty(&req->r_linger_item)) { | |
1371 | dout("%p tid %llu restart on osd%d\n", | |
1372 | req, req->r_tid, | |
1373 | req->r_osd ? req->r_osd->o_osd : -1); | |
1374 | __unregister_request(osdc, req); | |
1375 | __register_linger_request(osdc, req); | |
1376 | continue; | |
1377 | } | |
1378 | ||
38d6453c | 1379 | err = __map_request(osdc, req, force_resend); |
6f6c7006 SW |
1380 | if (err < 0) |
1381 | continue; /* error */ | |
1382 | if (req->r_osd == NULL) { | |
1383 | dout("%p tid %llu maps to no osd\n", req, req->r_tid); | |
1384 | needmap++; /* request a newer map */ | |
1385 | } else if (err > 0) { | |
6194ea89 SW |
1386 | if (!req->r_linger) { |
1387 | dout("%p tid %llu requeued on osd%d\n", req, | |
1388 | req->r_tid, | |
1389 | req->r_osd ? req->r_osd->o_osd : -1); | |
a40c4f10 | 1390 | req->r_flags |= CEPH_OSD_FLAG_RETRY; |
6194ea89 SW |
1391 | } |
1392 | } | |
a40c4f10 YS |
1393 | } |
1394 | ||
1395 | list_for_each_entry_safe(req, nreq, &osdc->req_linger, | |
1396 | r_linger_item) { | |
1397 | dout("linger req=%p req->r_osd=%p\n", req, req->r_osd); | |
1398 | ||
38d6453c | 1399 | err = __map_request(osdc, req, force_resend); |
ab60b16d | 1400 | dout("__map_request returned %d\n", err); |
a40c4f10 YS |
1401 | if (err == 0) |
1402 | continue; /* no change and no osd was specified */ | |
1403 | if (err < 0) | |
1404 | continue; /* hrm! */ | |
1405 | if (req->r_osd == NULL) { | |
1406 | dout("tid %llu maps to no valid osd\n", req->r_tid); | |
1407 | needmap++; /* request a newer map */ | |
1408 | continue; | |
6f6c7006 | 1409 | } |
a40c4f10 YS |
1410 | |
1411 | dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid, | |
1412 | req->r_osd ? req->r_osd->o_osd : -1); | |
a40c4f10 | 1413 | __register_request(osdc, req); |
c89ce05e | 1414 | __unregister_linger_request(osdc, req); |
6f6c7006 | 1415 | } |
f24e9980 SW |
1416 | mutex_unlock(&osdc->request_mutex); |
1417 | ||
1418 | if (needmap) { | |
1419 | dout("%d requests for down osds, need new map\n", needmap); | |
1420 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1421 | } | |
e6d50f67 | 1422 | reset_changed_osds(osdc); |
422d2cb8 | 1423 | } |
6f6c7006 SW |
1424 | |
1425 | ||
f24e9980 SW |
1426 | /* |
1427 | * Process updated osd map. | |
1428 | * | |
1429 | * The message contains any number of incremental and full maps, normally | |
1430 | * indicating some sort of topology change in the cluster. Kick requests | |
1431 | * off to different OSDs as needed. | |
1432 | */ | |
1433 | void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) | |
1434 | { | |
1435 | void *p, *end, *next; | |
1436 | u32 nr_maps, maplen; | |
1437 | u32 epoch; | |
1438 | struct ceph_osdmap *newmap = NULL, *oldmap; | |
1439 | int err; | |
1440 | struct ceph_fsid fsid; | |
1441 | ||
1442 | dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0); | |
1443 | p = msg->front.iov_base; | |
1444 | end = p + msg->front.iov_len; | |
1445 | ||
1446 | /* verify fsid */ | |
1447 | ceph_decode_need(&p, end, sizeof(fsid), bad); | |
1448 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | |
0743304d SW |
1449 | if (ceph_check_fsid(osdc->client, &fsid) < 0) |
1450 | return; | |
f24e9980 SW |
1451 | |
1452 | down_write(&osdc->map_sem); | |
1453 | ||
1454 | /* incremental maps */ | |
1455 | ceph_decode_32_safe(&p, end, nr_maps, bad); | |
1456 | dout(" %d inc maps\n", nr_maps); | |
1457 | while (nr_maps > 0) { | |
1458 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | |
c89136ea SW |
1459 | epoch = ceph_decode_32(&p); |
1460 | maplen = ceph_decode_32(&p); | |
f24e9980 SW |
1461 | ceph_decode_need(&p, end, maplen, bad); |
1462 | next = p + maplen; | |
1463 | if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) { | |
1464 | dout("applying incremental map %u len %d\n", | |
1465 | epoch, maplen); | |
1466 | newmap = osdmap_apply_incremental(&p, next, | |
1467 | osdc->osdmap, | |
15d9882c | 1468 | &osdc->client->msgr); |
f24e9980 SW |
1469 | if (IS_ERR(newmap)) { |
1470 | err = PTR_ERR(newmap); | |
1471 | goto bad; | |
1472 | } | |
30dc6381 | 1473 | BUG_ON(!newmap); |
f24e9980 SW |
1474 | if (newmap != osdc->osdmap) { |
1475 | ceph_osdmap_destroy(osdc->osdmap); | |
1476 | osdc->osdmap = newmap; | |
1477 | } | |
38d6453c | 1478 | kick_requests(osdc, 0); |
f24e9980 SW |
1479 | } else { |
1480 | dout("ignoring incremental map %u len %d\n", | |
1481 | epoch, maplen); | |
1482 | } | |
1483 | p = next; | |
1484 | nr_maps--; | |
1485 | } | |
1486 | if (newmap) | |
1487 | goto done; | |
1488 | ||
1489 | /* full maps */ | |
1490 | ceph_decode_32_safe(&p, end, nr_maps, bad); | |
1491 | dout(" %d full maps\n", nr_maps); | |
1492 | while (nr_maps) { | |
1493 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | |
c89136ea SW |
1494 | epoch = ceph_decode_32(&p); |
1495 | maplen = ceph_decode_32(&p); | |
f24e9980 SW |
1496 | ceph_decode_need(&p, end, maplen, bad); |
1497 | if (nr_maps > 1) { | |
1498 | dout("skipping non-latest full map %u len %d\n", | |
1499 | epoch, maplen); | |
1500 | } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) { | |
1501 | dout("skipping full map %u len %d, " | |
1502 | "older than our %u\n", epoch, maplen, | |
1503 | osdc->osdmap->epoch); | |
1504 | } else { | |
38d6453c SW |
1505 | int skipped_map = 0; |
1506 | ||
f24e9980 SW |
1507 | dout("taking full map %u len %d\n", epoch, maplen); |
1508 | newmap = osdmap_decode(&p, p+maplen); | |
1509 | if (IS_ERR(newmap)) { | |
1510 | err = PTR_ERR(newmap); | |
1511 | goto bad; | |
1512 | } | |
30dc6381 | 1513 | BUG_ON(!newmap); |
f24e9980 SW |
1514 | oldmap = osdc->osdmap; |
1515 | osdc->osdmap = newmap; | |
38d6453c SW |
1516 | if (oldmap) { |
1517 | if (oldmap->epoch + 1 < newmap->epoch) | |
1518 | skipped_map = 1; | |
f24e9980 | 1519 | ceph_osdmap_destroy(oldmap); |
38d6453c SW |
1520 | } |
1521 | kick_requests(osdc, skipped_map); | |
f24e9980 SW |
1522 | } |
1523 | p += maplen; | |
1524 | nr_maps--; | |
1525 | } | |
1526 | ||
1527 | done: | |
1528 | downgrade_write(&osdc->map_sem); | |
1529 | ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch); | |
cd634fb6 SW |
1530 | |
1531 | /* | |
1532 | * subscribe to subsequent osdmap updates if full to ensure | |
1533 | * we find out when we are no longer full and stop returning | |
1534 | * ENOSPC. | |
1535 | */ | |
1536 | if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) | |
1537 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1538 | ||
f9d25199 AE |
1539 | mutex_lock(&osdc->request_mutex); |
1540 | __send_queued(osdc); | |
1541 | mutex_unlock(&osdc->request_mutex); | |
f24e9980 | 1542 | up_read(&osdc->map_sem); |
03066f23 | 1543 | wake_up_all(&osdc->client->auth_wq); |
f24e9980 SW |
1544 | return; |
1545 | ||
1546 | bad: | |
1547 | pr_err("osdc handle_map corrupt msg\n"); | |
9ec7cab1 | 1548 | ceph_msg_dump(msg); |
f24e9980 SW |
1549 | up_write(&osdc->map_sem); |
1550 | return; | |
1551 | } | |
1552 | ||
a40c4f10 YS |
1553 | /* |
1554 | * watch/notify callback event infrastructure | |
1555 | * | |
1556 | * These callbacks are used both for watch and notify operations. | |
1557 | */ | |
1558 | static void __release_event(struct kref *kref) | |
1559 | { | |
1560 | struct ceph_osd_event *event = | |
1561 | container_of(kref, struct ceph_osd_event, kref); | |
1562 | ||
1563 | dout("__release_event %p\n", event); | |
1564 | kfree(event); | |
1565 | } | |
1566 | ||
1567 | static void get_event(struct ceph_osd_event *event) | |
1568 | { | |
1569 | kref_get(&event->kref); | |
1570 | } | |
1571 | ||
1572 | void ceph_osdc_put_event(struct ceph_osd_event *event) | |
1573 | { | |
1574 | kref_put(&event->kref, __release_event); | |
1575 | } | |
1576 | EXPORT_SYMBOL(ceph_osdc_put_event); | |
1577 | ||
1578 | static void __insert_event(struct ceph_osd_client *osdc, | |
1579 | struct ceph_osd_event *new) | |
1580 | { | |
1581 | struct rb_node **p = &osdc->event_tree.rb_node; | |
1582 | struct rb_node *parent = NULL; | |
1583 | struct ceph_osd_event *event = NULL; | |
1584 | ||
1585 | while (*p) { | |
1586 | parent = *p; | |
1587 | event = rb_entry(parent, struct ceph_osd_event, node); | |
1588 | if (new->cookie < event->cookie) | |
1589 | p = &(*p)->rb_left; | |
1590 | else if (new->cookie > event->cookie) | |
1591 | p = &(*p)->rb_right; | |
1592 | else | |
1593 | BUG(); | |
1594 | } | |
1595 | ||
1596 | rb_link_node(&new->node, parent, p); | |
1597 | rb_insert_color(&new->node, &osdc->event_tree); | |
1598 | } | |
1599 | ||
1600 | static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc, | |
1601 | u64 cookie) | |
1602 | { | |
1603 | struct rb_node **p = &osdc->event_tree.rb_node; | |
1604 | struct rb_node *parent = NULL; | |
1605 | struct ceph_osd_event *event = NULL; | |
1606 | ||
1607 | while (*p) { | |
1608 | parent = *p; | |
1609 | event = rb_entry(parent, struct ceph_osd_event, node); | |
1610 | if (cookie < event->cookie) | |
1611 | p = &(*p)->rb_left; | |
1612 | else if (cookie > event->cookie) | |
1613 | p = &(*p)->rb_right; | |
1614 | else | |
1615 | return event; | |
1616 | } | |
1617 | return NULL; | |
1618 | } | |
1619 | ||
1620 | static void __remove_event(struct ceph_osd_event *event) | |
1621 | { | |
1622 | struct ceph_osd_client *osdc = event->osdc; | |
1623 | ||
1624 | if (!RB_EMPTY_NODE(&event->node)) { | |
1625 | dout("__remove_event removed %p\n", event); | |
1626 | rb_erase(&event->node, &osdc->event_tree); | |
1627 | ceph_osdc_put_event(event); | |
1628 | } else { | |
1629 | dout("__remove_event didn't remove %p\n", event); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | int ceph_osdc_create_event(struct ceph_osd_client *osdc, | |
1634 | void (*event_cb)(u64, u64, u8, void *), | |
3c663bbd | 1635 | void *data, struct ceph_osd_event **pevent) |
a40c4f10 YS |
1636 | { |
1637 | struct ceph_osd_event *event; | |
1638 | ||
1639 | event = kmalloc(sizeof(*event), GFP_NOIO); | |
1640 | if (!event) | |
1641 | return -ENOMEM; | |
1642 | ||
1643 | dout("create_event %p\n", event); | |
1644 | event->cb = event_cb; | |
3c663bbd | 1645 | event->one_shot = 0; |
a40c4f10 YS |
1646 | event->data = data; |
1647 | event->osdc = osdc; | |
1648 | INIT_LIST_HEAD(&event->osd_node); | |
3ee5234d | 1649 | RB_CLEAR_NODE(&event->node); |
a40c4f10 YS |
1650 | kref_init(&event->kref); /* one ref for us */ |
1651 | kref_get(&event->kref); /* one ref for the caller */ | |
a40c4f10 YS |
1652 | |
1653 | spin_lock(&osdc->event_lock); | |
1654 | event->cookie = ++osdc->event_count; | |
1655 | __insert_event(osdc, event); | |
1656 | spin_unlock(&osdc->event_lock); | |
1657 | ||
1658 | *pevent = event; | |
1659 | return 0; | |
1660 | } | |
1661 | EXPORT_SYMBOL(ceph_osdc_create_event); | |
1662 | ||
1663 | void ceph_osdc_cancel_event(struct ceph_osd_event *event) | |
1664 | { | |
1665 | struct ceph_osd_client *osdc = event->osdc; | |
1666 | ||
1667 | dout("cancel_event %p\n", event); | |
1668 | spin_lock(&osdc->event_lock); | |
1669 | __remove_event(event); | |
1670 | spin_unlock(&osdc->event_lock); | |
1671 | ceph_osdc_put_event(event); /* caller's */ | |
1672 | } | |
1673 | EXPORT_SYMBOL(ceph_osdc_cancel_event); | |
1674 | ||
1675 | ||
1676 | static void do_event_work(struct work_struct *work) | |
1677 | { | |
1678 | struct ceph_osd_event_work *event_work = | |
1679 | container_of(work, struct ceph_osd_event_work, work); | |
1680 | struct ceph_osd_event *event = event_work->event; | |
1681 | u64 ver = event_work->ver; | |
1682 | u64 notify_id = event_work->notify_id; | |
1683 | u8 opcode = event_work->opcode; | |
1684 | ||
1685 | dout("do_event_work completing %p\n", event); | |
1686 | event->cb(ver, notify_id, opcode, event->data); | |
a40c4f10 YS |
1687 | dout("do_event_work completed %p\n", event); |
1688 | ceph_osdc_put_event(event); | |
1689 | kfree(event_work); | |
1690 | } | |
1691 | ||
1692 | ||
1693 | /* | |
1694 | * Process osd watch notifications | |
1695 | */ | |
3c663bbd AE |
1696 | static void handle_watch_notify(struct ceph_osd_client *osdc, |
1697 | struct ceph_msg *msg) | |
a40c4f10 YS |
1698 | { |
1699 | void *p, *end; | |
1700 | u8 proto_ver; | |
1701 | u64 cookie, ver, notify_id; | |
1702 | u8 opcode; | |
1703 | struct ceph_osd_event *event; | |
1704 | struct ceph_osd_event_work *event_work; | |
1705 | ||
1706 | p = msg->front.iov_base; | |
1707 | end = p + msg->front.iov_len; | |
1708 | ||
1709 | ceph_decode_8_safe(&p, end, proto_ver, bad); | |
1710 | ceph_decode_8_safe(&p, end, opcode, bad); | |
1711 | ceph_decode_64_safe(&p, end, cookie, bad); | |
1712 | ceph_decode_64_safe(&p, end, ver, bad); | |
1713 | ceph_decode_64_safe(&p, end, notify_id, bad); | |
1714 | ||
1715 | spin_lock(&osdc->event_lock); | |
1716 | event = __find_event(osdc, cookie); | |
1717 | if (event) { | |
3c663bbd | 1718 | BUG_ON(event->one_shot); |
a40c4f10 | 1719 | get_event(event); |
a40c4f10 YS |
1720 | } |
1721 | spin_unlock(&osdc->event_lock); | |
1722 | dout("handle_watch_notify cookie %lld ver %lld event %p\n", | |
1723 | cookie, ver, event); | |
1724 | if (event) { | |
1725 | event_work = kmalloc(sizeof(*event_work), GFP_NOIO); | |
a40c4f10 YS |
1726 | if (!event_work) { |
1727 | dout("ERROR: could not allocate event_work\n"); | |
1728 | goto done_err; | |
1729 | } | |
6b0ae409 | 1730 | INIT_WORK(&event_work->work, do_event_work); |
a40c4f10 YS |
1731 | event_work->event = event; |
1732 | event_work->ver = ver; | |
1733 | event_work->notify_id = notify_id; | |
1734 | event_work->opcode = opcode; | |
1735 | if (!queue_work(osdc->notify_wq, &event_work->work)) { | |
1736 | dout("WARNING: failed to queue notify event work\n"); | |
1737 | goto done_err; | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | return; | |
1742 | ||
1743 | done_err: | |
a40c4f10 YS |
1744 | ceph_osdc_put_event(event); |
1745 | return; | |
1746 | ||
1747 | bad: | |
1748 | pr_err("osdc handle_watch_notify corrupt msg\n"); | |
1749 | return; | |
1750 | } | |
1751 | ||
70636773 AE |
1752 | static void ceph_osdc_msg_data_set(struct ceph_msg *msg, |
1753 | struct ceph_osd_data *osd_data) | |
f24e9980 | 1754 | { |
0fff87ec | 1755 | if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) { |
f1baeb2b | 1756 | BUG_ON(osd_data->length > (u64) SIZE_MAX); |
ebf18f47 | 1757 | if (osd_data->length) |
70636773 AE |
1758 | ceph_msg_data_set_pages(msg, osd_data->pages, |
1759 | osd_data->length, osd_data->alignment); | |
68b4476b | 1760 | #ifdef CONFIG_BLOCK |
0fff87ec | 1761 | } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) { |
70636773 | 1762 | ceph_msg_data_set_bio(msg, osd_data->bio); |
68b4476b | 1763 | #endif |
2ac2b7a6 | 1764 | } else { |
0fff87ec | 1765 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE); |
2ac2b7a6 | 1766 | } |
70636773 AE |
1767 | } |
1768 | ||
1769 | /* | |
1770 | * Register request, send initial attempt. | |
1771 | */ | |
1772 | int ceph_osdc_start_request(struct ceph_osd_client *osdc, | |
1773 | struct ceph_osd_request *req, | |
1774 | bool nofail) | |
1775 | { | |
1776 | int rc = 0; | |
1777 | ||
1778 | /* Set up response incoming data and request outgoing data fields */ | |
1779 | ||
1780 | ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in); | |
1781 | ceph_osdc_msg_data_set(req->r_request, &req->r_data_out); | |
ebf18f47 AE |
1782 | if (req->r_trail.length) |
1783 | ceph_msg_data_set_trail(req->r_request, &req->r_trail); | |
f24e9980 SW |
1784 | |
1785 | register_request(osdc, req); | |
1786 | ||
1787 | down_read(&osdc->map_sem); | |
1788 | mutex_lock(&osdc->request_mutex); | |
c1ea8823 SW |
1789 | /* |
1790 | * a racing kick_requests() may have sent the message for us | |
1791 | * while we dropped request_mutex above, so only send now if | |
1792 | * the request still han't been touched yet. | |
1793 | */ | |
1794 | if (req->r_sent == 0) { | |
38d6453c | 1795 | rc = __map_request(osdc, req, 0); |
9d6fcb08 SW |
1796 | if (rc < 0) { |
1797 | if (nofail) { | |
1798 | dout("osdc_start_request failed map, " | |
1799 | " will retry %lld\n", req->r_tid); | |
1800 | rc = 0; | |
1801 | } | |
234af26f | 1802 | goto out_unlock; |
9d6fcb08 | 1803 | } |
6f6c7006 SW |
1804 | if (req->r_osd == NULL) { |
1805 | dout("send_request %p no up osds in pg\n", req); | |
1806 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
1807 | } else { | |
56e925b6 | 1808 | __send_request(osdc, req); |
f24e9980 | 1809 | } |
56e925b6 | 1810 | rc = 0; |
f24e9980 | 1811 | } |
234af26f DC |
1812 | |
1813 | out_unlock: | |
f24e9980 SW |
1814 | mutex_unlock(&osdc->request_mutex); |
1815 | up_read(&osdc->map_sem); | |
1816 | return rc; | |
1817 | } | |
3d14c5d2 | 1818 | EXPORT_SYMBOL(ceph_osdc_start_request); |
f24e9980 SW |
1819 | |
1820 | /* | |
1821 | * wait for a request to complete | |
1822 | */ | |
1823 | int ceph_osdc_wait_request(struct ceph_osd_client *osdc, | |
1824 | struct ceph_osd_request *req) | |
1825 | { | |
1826 | int rc; | |
1827 | ||
1828 | rc = wait_for_completion_interruptible(&req->r_completion); | |
1829 | if (rc < 0) { | |
1830 | mutex_lock(&osdc->request_mutex); | |
1831 | __cancel_request(req); | |
529cfcc4 | 1832 | __unregister_request(osdc, req); |
f24e9980 | 1833 | mutex_unlock(&osdc->request_mutex); |
25845472 | 1834 | complete_request(req); |
529cfcc4 | 1835 | dout("wait_request tid %llu canceled/timed out\n", req->r_tid); |
f24e9980 SW |
1836 | return rc; |
1837 | } | |
1838 | ||
1839 | dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result); | |
1840 | return req->r_result; | |
1841 | } | |
3d14c5d2 | 1842 | EXPORT_SYMBOL(ceph_osdc_wait_request); |
f24e9980 SW |
1843 | |
1844 | /* | |
1845 | * sync - wait for all in-flight requests to flush. avoid starvation. | |
1846 | */ | |
1847 | void ceph_osdc_sync(struct ceph_osd_client *osdc) | |
1848 | { | |
1849 | struct ceph_osd_request *req; | |
1850 | u64 last_tid, next_tid = 0; | |
1851 | ||
1852 | mutex_lock(&osdc->request_mutex); | |
1853 | last_tid = osdc->last_tid; | |
1854 | while (1) { | |
1855 | req = __lookup_request_ge(osdc, next_tid); | |
1856 | if (!req) | |
1857 | break; | |
1858 | if (req->r_tid > last_tid) | |
1859 | break; | |
1860 | ||
1861 | next_tid = req->r_tid + 1; | |
1862 | if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0) | |
1863 | continue; | |
1864 | ||
1865 | ceph_osdc_get_request(req); | |
1866 | mutex_unlock(&osdc->request_mutex); | |
1867 | dout("sync waiting on tid %llu (last is %llu)\n", | |
1868 | req->r_tid, last_tid); | |
1869 | wait_for_completion(&req->r_safe_completion); | |
1870 | mutex_lock(&osdc->request_mutex); | |
1871 | ceph_osdc_put_request(req); | |
1872 | } | |
1873 | mutex_unlock(&osdc->request_mutex); | |
1874 | dout("sync done (thru tid %llu)\n", last_tid); | |
1875 | } | |
3d14c5d2 | 1876 | EXPORT_SYMBOL(ceph_osdc_sync); |
f24e9980 SW |
1877 | |
1878 | /* | |
1879 | * init, shutdown | |
1880 | */ | |
1881 | int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) | |
1882 | { | |
1883 | int err; | |
1884 | ||
1885 | dout("init\n"); | |
1886 | osdc->client = client; | |
1887 | osdc->osdmap = NULL; | |
1888 | init_rwsem(&osdc->map_sem); | |
1889 | init_completion(&osdc->map_waiters); | |
1890 | osdc->last_requested_map = 0; | |
1891 | mutex_init(&osdc->request_mutex); | |
f24e9980 SW |
1892 | osdc->last_tid = 0; |
1893 | osdc->osds = RB_ROOT; | |
f5a2041b | 1894 | INIT_LIST_HEAD(&osdc->osd_lru); |
f24e9980 | 1895 | osdc->requests = RB_ROOT; |
422d2cb8 | 1896 | INIT_LIST_HEAD(&osdc->req_lru); |
6f6c7006 SW |
1897 | INIT_LIST_HEAD(&osdc->req_unsent); |
1898 | INIT_LIST_HEAD(&osdc->req_notarget); | |
a40c4f10 | 1899 | INIT_LIST_HEAD(&osdc->req_linger); |
f24e9980 SW |
1900 | osdc->num_requests = 0; |
1901 | INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); | |
f5a2041b | 1902 | INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); |
a40c4f10 YS |
1903 | spin_lock_init(&osdc->event_lock); |
1904 | osdc->event_tree = RB_ROOT; | |
1905 | osdc->event_count = 0; | |
f5a2041b YS |
1906 | |
1907 | schedule_delayed_work(&osdc->osds_timeout_work, | |
3d14c5d2 | 1908 | round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ)); |
f24e9980 | 1909 | |
5f44f142 | 1910 | err = -ENOMEM; |
f24e9980 SW |
1911 | osdc->req_mempool = mempool_create_kmalloc_pool(10, |
1912 | sizeof(struct ceph_osd_request)); | |
1913 | if (!osdc->req_mempool) | |
5f44f142 | 1914 | goto out; |
f24e9980 | 1915 | |
d50b409f SW |
1916 | err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP, |
1917 | OSD_OP_FRONT_LEN, 10, true, | |
4f48280e | 1918 | "osd_op"); |
f24e9980 | 1919 | if (err < 0) |
5f44f142 | 1920 | goto out_mempool; |
d50b409f | 1921 | err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY, |
4f48280e SW |
1922 | OSD_OPREPLY_FRONT_LEN, 10, true, |
1923 | "osd_op_reply"); | |
c16e7869 SW |
1924 | if (err < 0) |
1925 | goto out_msgpool; | |
a40c4f10 YS |
1926 | |
1927 | osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify"); | |
1928 | if (IS_ERR(osdc->notify_wq)) { | |
1929 | err = PTR_ERR(osdc->notify_wq); | |
1930 | osdc->notify_wq = NULL; | |
1931 | goto out_msgpool; | |
1932 | } | |
f24e9980 | 1933 | return 0; |
5f44f142 | 1934 | |
c16e7869 SW |
1935 | out_msgpool: |
1936 | ceph_msgpool_destroy(&osdc->msgpool_op); | |
5f44f142 SW |
1937 | out_mempool: |
1938 | mempool_destroy(osdc->req_mempool); | |
1939 | out: | |
1940 | return err; | |
f24e9980 SW |
1941 | } |
1942 | ||
1943 | void ceph_osdc_stop(struct ceph_osd_client *osdc) | |
1944 | { | |
a40c4f10 YS |
1945 | flush_workqueue(osdc->notify_wq); |
1946 | destroy_workqueue(osdc->notify_wq); | |
f24e9980 | 1947 | cancel_delayed_work_sync(&osdc->timeout_work); |
f5a2041b | 1948 | cancel_delayed_work_sync(&osdc->osds_timeout_work); |
f24e9980 SW |
1949 | if (osdc->osdmap) { |
1950 | ceph_osdmap_destroy(osdc->osdmap); | |
1951 | osdc->osdmap = NULL; | |
1952 | } | |
aca420bc | 1953 | remove_all_osds(osdc); |
f24e9980 SW |
1954 | mempool_destroy(osdc->req_mempool); |
1955 | ceph_msgpool_destroy(&osdc->msgpool_op); | |
c16e7869 | 1956 | ceph_msgpool_destroy(&osdc->msgpool_op_reply); |
f24e9980 SW |
1957 | } |
1958 | ||
1959 | /* | |
1960 | * Read some contiguous pages. If we cross a stripe boundary, shorten | |
1961 | * *plen. Return number of bytes read, or error. | |
1962 | */ | |
1963 | int ceph_osdc_readpages(struct ceph_osd_client *osdc, | |
1964 | struct ceph_vino vino, struct ceph_file_layout *layout, | |
1965 | u64 off, u64 *plen, | |
1966 | u32 truncate_seq, u64 truncate_size, | |
b7495fc2 | 1967 | struct page **pages, int num_pages, int page_align) |
f24e9980 SW |
1968 | { |
1969 | struct ceph_osd_request *req; | |
0fff87ec | 1970 | struct ceph_osd_data *osd_data; |
f24e9980 SW |
1971 | int rc = 0; |
1972 | ||
1973 | dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino, | |
1974 | vino.snap, off, *plen); | |
1975 | req = ceph_osdc_new_request(osdc, layout, vino, off, plen, | |
1976 | CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, | |
1977 | NULL, 0, truncate_seq, truncate_size, NULL, | |
153e5167 | 1978 | false); |
6816282d SW |
1979 | if (IS_ERR(req)) |
1980 | return PTR_ERR(req); | |
f24e9980 SW |
1981 | |
1982 | /* it may be a short read due to an object boundary */ | |
0fff87ec AE |
1983 | |
1984 | osd_data = &req->r_data_in; | |
1985 | osd_data->type = CEPH_OSD_DATA_TYPE_PAGES; | |
1986 | osd_data->pages = pages; | |
e0c59487 | 1987 | osd_data->length = *plen; |
0fff87ec | 1988 | osd_data->alignment = page_align; |
f24e9980 | 1989 | |
e0c59487 AE |
1990 | dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n", |
1991 | off, *plen, osd_data->length, page_align); | |
f24e9980 SW |
1992 | |
1993 | rc = ceph_osdc_start_request(osdc, req, false); | |
1994 | if (!rc) | |
1995 | rc = ceph_osdc_wait_request(osdc, req); | |
1996 | ||
1997 | ceph_osdc_put_request(req); | |
1998 | dout("readpages result %d\n", rc); | |
1999 | return rc; | |
2000 | } | |
3d14c5d2 | 2001 | EXPORT_SYMBOL(ceph_osdc_readpages); |
f24e9980 SW |
2002 | |
2003 | /* | |
2004 | * do a synchronous write on N pages | |
2005 | */ | |
2006 | int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino, | |
2007 | struct ceph_file_layout *layout, | |
2008 | struct ceph_snap_context *snapc, | |
2009 | u64 off, u64 len, | |
2010 | u32 truncate_seq, u64 truncate_size, | |
2011 | struct timespec *mtime, | |
24808826 | 2012 | struct page **pages, int num_pages) |
f24e9980 SW |
2013 | { |
2014 | struct ceph_osd_request *req; | |
0fff87ec | 2015 | struct ceph_osd_data *osd_data; |
f24e9980 | 2016 | int rc = 0; |
b7495fc2 | 2017 | int page_align = off & ~PAGE_MASK; |
f24e9980 SW |
2018 | |
2019 | BUG_ON(vino.snap != CEPH_NOSNAP); | |
2020 | req = ceph_osdc_new_request(osdc, layout, vino, off, &len, | |
2021 | CEPH_OSD_OP_WRITE, | |
24808826 | 2022 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, |
fbf8685f | 2023 | snapc, 0, |
f24e9980 | 2024 | truncate_seq, truncate_size, mtime, |
153e5167 | 2025 | true); |
6816282d SW |
2026 | if (IS_ERR(req)) |
2027 | return PTR_ERR(req); | |
f24e9980 SW |
2028 | |
2029 | /* it may be a short write due to an object boundary */ | |
0fff87ec AE |
2030 | osd_data = &req->r_data_out; |
2031 | osd_data->type = CEPH_OSD_DATA_TYPE_PAGES; | |
2032 | osd_data->pages = pages; | |
e0c59487 | 2033 | osd_data->length = len; |
0fff87ec | 2034 | osd_data->alignment = page_align; |
e0c59487 | 2035 | dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length); |
f24e9980 | 2036 | |
87f979d3 | 2037 | rc = ceph_osdc_start_request(osdc, req, true); |
f24e9980 SW |
2038 | if (!rc) |
2039 | rc = ceph_osdc_wait_request(osdc, req); | |
2040 | ||
2041 | ceph_osdc_put_request(req); | |
2042 | if (rc == 0) | |
2043 | rc = len; | |
2044 | dout("writepages result %d\n", rc); | |
2045 | return rc; | |
2046 | } | |
3d14c5d2 | 2047 | EXPORT_SYMBOL(ceph_osdc_writepages); |
f24e9980 SW |
2048 | |
2049 | /* | |
2050 | * handle incoming message | |
2051 | */ | |
2052 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | |
2053 | { | |
2054 | struct ceph_osd *osd = con->private; | |
32c895e7 | 2055 | struct ceph_osd_client *osdc; |
f24e9980 SW |
2056 | int type = le16_to_cpu(msg->hdr.type); |
2057 | ||
2058 | if (!osd) | |
4a32f93d | 2059 | goto out; |
32c895e7 | 2060 | osdc = osd->o_osdc; |
f24e9980 SW |
2061 | |
2062 | switch (type) { | |
2063 | case CEPH_MSG_OSD_MAP: | |
2064 | ceph_osdc_handle_map(osdc, msg); | |
2065 | break; | |
2066 | case CEPH_MSG_OSD_OPREPLY: | |
350b1c32 | 2067 | handle_reply(osdc, msg, con); |
f24e9980 | 2068 | break; |
a40c4f10 YS |
2069 | case CEPH_MSG_WATCH_NOTIFY: |
2070 | handle_watch_notify(osdc, msg); | |
2071 | break; | |
f24e9980 SW |
2072 | |
2073 | default: | |
2074 | pr_err("received unknown message type %d %s\n", type, | |
2075 | ceph_msg_type_name(type)); | |
2076 | } | |
4a32f93d | 2077 | out: |
f24e9980 SW |
2078 | ceph_msg_put(msg); |
2079 | } | |
2080 | ||
5b3a4db3 | 2081 | /* |
21b667f6 SW |
2082 | * lookup and return message for incoming reply. set up reply message |
2083 | * pages. | |
5b3a4db3 SW |
2084 | */ |
2085 | static struct ceph_msg *get_reply(struct ceph_connection *con, | |
2450418c YS |
2086 | struct ceph_msg_header *hdr, |
2087 | int *skip) | |
f24e9980 SW |
2088 | { |
2089 | struct ceph_osd *osd = con->private; | |
2090 | struct ceph_osd_client *osdc = osd->o_osdc; | |
2450418c | 2091 | struct ceph_msg *m; |
0547a9b3 | 2092 | struct ceph_osd_request *req; |
5b3a4db3 SW |
2093 | int front = le32_to_cpu(hdr->front_len); |
2094 | int data_len = le32_to_cpu(hdr->data_len); | |
0547a9b3 | 2095 | u64 tid; |
f24e9980 | 2096 | |
0547a9b3 YS |
2097 | tid = le64_to_cpu(hdr->tid); |
2098 | mutex_lock(&osdc->request_mutex); | |
2099 | req = __lookup_request(osdc, tid); | |
2100 | if (!req) { | |
2101 | *skip = 1; | |
2102 | m = NULL; | |
756a16a5 SW |
2103 | dout("get_reply unknown tid %llu from osd%d\n", tid, |
2104 | osd->o_osd); | |
0547a9b3 YS |
2105 | goto out; |
2106 | } | |
c16e7869 SW |
2107 | |
2108 | if (req->r_con_filling_msg) { | |
8921d114 | 2109 | dout("%s revoking msg %p from old con %p\n", __func__, |
c16e7869 | 2110 | req->r_reply, req->r_con_filling_msg); |
8921d114 | 2111 | ceph_msg_revoke_incoming(req->r_reply); |
0d47766f | 2112 | req->r_con_filling_msg->ops->put(req->r_con_filling_msg); |
6f46cb29 | 2113 | req->r_con_filling_msg = NULL; |
0547a9b3 YS |
2114 | } |
2115 | ||
c16e7869 SW |
2116 | if (front > req->r_reply->front.iov_len) { |
2117 | pr_warning("get_reply front %d > preallocated %d\n", | |
2118 | front, (int)req->r_reply->front.iov_len); | |
b61c2763 | 2119 | m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false); |
a79832f2 | 2120 | if (!m) |
c16e7869 SW |
2121 | goto out; |
2122 | ceph_msg_put(req->r_reply); | |
2123 | req->r_reply = m; | |
2124 | } | |
2125 | m = ceph_msg_get(req->r_reply); | |
2126 | ||
0547a9b3 | 2127 | if (data_len > 0) { |
0fff87ec AE |
2128 | struct ceph_osd_data *osd_data = &req->r_data_in; |
2129 | ||
2130 | if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) { | |
0fff87ec | 2131 | if (osd_data->pages && |
e0c59487 | 2132 | unlikely(osd_data->length < data_len)) { |
2ac2b7a6 | 2133 | |
e0c59487 AE |
2134 | pr_warning("tid %lld reply has %d bytes " |
2135 | "we had only %llu bytes ready\n", | |
2136 | tid, data_len, osd_data->length); | |
2ac2b7a6 AE |
2137 | *skip = 1; |
2138 | ceph_msg_put(m); | |
2139 | m = NULL; | |
2140 | goto out; | |
2141 | } | |
2ac2b7a6 | 2142 | } |
0547a9b3 | 2143 | } |
5b3a4db3 | 2144 | *skip = 0; |
0d47766f | 2145 | req->r_con_filling_msg = con->ops->get(con); |
c16e7869 | 2146 | dout("get_reply tid %lld %p\n", tid, m); |
0547a9b3 YS |
2147 | |
2148 | out: | |
2149 | mutex_unlock(&osdc->request_mutex); | |
2450418c | 2150 | return m; |
5b3a4db3 SW |
2151 | |
2152 | } | |
2153 | ||
2154 | static struct ceph_msg *alloc_msg(struct ceph_connection *con, | |
2155 | struct ceph_msg_header *hdr, | |
2156 | int *skip) | |
2157 | { | |
2158 | struct ceph_osd *osd = con->private; | |
2159 | int type = le16_to_cpu(hdr->type); | |
2160 | int front = le32_to_cpu(hdr->front_len); | |
2161 | ||
1c20f2d2 | 2162 | *skip = 0; |
5b3a4db3 SW |
2163 | switch (type) { |
2164 | case CEPH_MSG_OSD_MAP: | |
a40c4f10 | 2165 | case CEPH_MSG_WATCH_NOTIFY: |
b61c2763 | 2166 | return ceph_msg_new(type, front, GFP_NOFS, false); |
5b3a4db3 SW |
2167 | case CEPH_MSG_OSD_OPREPLY: |
2168 | return get_reply(con, hdr, skip); | |
2169 | default: | |
2170 | pr_info("alloc_msg unexpected msg type %d from osd%d\n", type, | |
2171 | osd->o_osd); | |
2172 | *skip = 1; | |
2173 | return NULL; | |
2174 | } | |
f24e9980 SW |
2175 | } |
2176 | ||
2177 | /* | |
2178 | * Wrappers to refcount containing ceph_osd struct | |
2179 | */ | |
2180 | static struct ceph_connection *get_osd_con(struct ceph_connection *con) | |
2181 | { | |
2182 | struct ceph_osd *osd = con->private; | |
2183 | if (get_osd(osd)) | |
2184 | return con; | |
2185 | return NULL; | |
2186 | } | |
2187 | ||
2188 | static void put_osd_con(struct ceph_connection *con) | |
2189 | { | |
2190 | struct ceph_osd *osd = con->private; | |
2191 | put_osd(osd); | |
2192 | } | |
2193 | ||
4e7a5dcd SW |
2194 | /* |
2195 | * authentication | |
2196 | */ | |
a3530df3 AE |
2197 | /* |
2198 | * Note: returned pointer is the address of a structure that's | |
2199 | * managed separately. Caller must *not* attempt to free it. | |
2200 | */ | |
2201 | static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, | |
8f43fb53 | 2202 | int *proto, int force_new) |
4e7a5dcd SW |
2203 | { |
2204 | struct ceph_osd *o = con->private; | |
2205 | struct ceph_osd_client *osdc = o->o_osdc; | |
2206 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
74f1869f | 2207 | struct ceph_auth_handshake *auth = &o->o_auth; |
4e7a5dcd | 2208 | |
74f1869f | 2209 | if (force_new && auth->authorizer) { |
a255651d AE |
2210 | if (ac->ops && ac->ops->destroy_authorizer) |
2211 | ac->ops->destroy_authorizer(ac, auth->authorizer); | |
74f1869f AE |
2212 | auth->authorizer = NULL; |
2213 | } | |
a255651d | 2214 | if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) { |
a3530df3 AE |
2215 | int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD, |
2216 | auth); | |
4e7a5dcd | 2217 | if (ret) |
a3530df3 | 2218 | return ERR_PTR(ret); |
4e7a5dcd | 2219 | } |
4e7a5dcd | 2220 | *proto = ac->protocol; |
74f1869f | 2221 | |
a3530df3 | 2222 | return auth; |
4e7a5dcd SW |
2223 | } |
2224 | ||
2225 | ||
2226 | static int verify_authorizer_reply(struct ceph_connection *con, int len) | |
2227 | { | |
2228 | struct ceph_osd *o = con->private; | |
2229 | struct ceph_osd_client *osdc = o->o_osdc; | |
2230 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
2231 | ||
a255651d AE |
2232 | /* |
2233 | * XXX If ac->ops or ac->ops->verify_authorizer_reply is null, | |
2234 | * XXX which do we do: succeed or fail? | |
2235 | */ | |
6c4a1915 | 2236 | return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len); |
4e7a5dcd SW |
2237 | } |
2238 | ||
9bd2e6f8 SW |
2239 | static int invalidate_authorizer(struct ceph_connection *con) |
2240 | { | |
2241 | struct ceph_osd *o = con->private; | |
2242 | struct ceph_osd_client *osdc = o->o_osdc; | |
2243 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
2244 | ||
a255651d | 2245 | if (ac->ops && ac->ops->invalidate_authorizer) |
9bd2e6f8 SW |
2246 | ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); |
2247 | ||
2248 | return ceph_monc_validate_auth(&osdc->client->monc); | |
2249 | } | |
4e7a5dcd | 2250 | |
9e32789f | 2251 | static const struct ceph_connection_operations osd_con_ops = { |
f24e9980 SW |
2252 | .get = get_osd_con, |
2253 | .put = put_osd_con, | |
2254 | .dispatch = dispatch, | |
4e7a5dcd SW |
2255 | .get_authorizer = get_authorizer, |
2256 | .verify_authorizer_reply = verify_authorizer_reply, | |
9bd2e6f8 | 2257 | .invalidate_authorizer = invalidate_authorizer, |
f24e9980 | 2258 | .alloc_msg = alloc_msg, |
81b024e7 | 2259 | .fault = osd_reset, |
f24e9980 | 2260 | }; |