PageRenderTime 78ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/net/ceph/osd_client.c

http://github.com/mirrors/linux
C | 5594 lines | 4352 code | 848 blank | 394 comment | 581 complexity | 677125c21d0d3db4d8aba12fbc13c94e MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/module.h>
  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>
  10. #ifdef CONFIG_BLOCK
  11. #include <linux/bio.h>
  12. #endif
  13. #include <linux/ceph/ceph_features.h>
  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>
  20. #include <linux/ceph/striper.h>
  21. #define OSD_OPREPLY_FRONT_LEN 512
  22. static struct kmem_cache *ceph_osd_request_cache;
  23. static const struct ceph_connection_operations osd_con_ops;
  24. /*
  25. * Implement client access to distributed object storage cluster.
  26. *
  27. * All data objects are stored within a cluster/cloud of OSDs, or
  28. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  29. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  30. * remote daemons serving up and coordinating consistent and safe
  31. * access to storage.
  32. *
  33. * Cluster membership and the mapping of data objects onto storage devices
  34. * are described by the osd map.
  35. *
  36. * We keep track of pending OSD requests (read, write), resubmit
  37. * requests to different OSDs when the cluster topology/data layout
  38. * change, or retry the affected requests when the communications
  39. * channel with an OSD is reset.
  40. */
  41. static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  42. static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  43. static void link_linger(struct ceph_osd *osd,
  44. struct ceph_osd_linger_request *lreq);
  45. static void unlink_linger(struct ceph_osd *osd,
  46. struct ceph_osd_linger_request *lreq);
  47. static void clear_backoffs(struct ceph_osd *osd);
  48. #if 1
  49. static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
  50. {
  51. bool wrlocked = true;
  52. if (unlikely(down_read_trylock(sem))) {
  53. wrlocked = false;
  54. up_read(sem);
  55. }
  56. return wrlocked;
  57. }
  58. static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
  59. {
  60. WARN_ON(!rwsem_is_locked(&osdc->lock));
  61. }
  62. static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
  63. {
  64. WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
  65. }
  66. static inline void verify_osd_locked(struct ceph_osd *osd)
  67. {
  68. struct ceph_osd_client *osdc = osd->o_osdc;
  69. WARN_ON(!(mutex_is_locked(&osd->lock) &&
  70. rwsem_is_locked(&osdc->lock)) &&
  71. !rwsem_is_wrlocked(&osdc->lock));
  72. }
  73. static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
  74. {
  75. WARN_ON(!mutex_is_locked(&lreq->lock));
  76. }
  77. #else
  78. static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
  79. static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
  80. static inline void verify_osd_locked(struct ceph_osd *osd) { }
  81. static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
  82. #endif
  83. /*
  84. * calculate the mapping of a file extent onto an object, and fill out the
  85. * request accordingly. shorten extent as necessary if it crosses an
  86. * object boundary.
  87. *
  88. * fill osd op in request message.
  89. */
  90. static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
  91. u64 *objnum, u64 *objoff, u64 *objlen)
  92. {
  93. u64 orig_len = *plen;
  94. u32 xlen;
  95. /* object extent? */
  96. ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
  97. objoff, &xlen);
  98. *objlen = xlen;
  99. if (*objlen < orig_len) {
  100. *plen = *objlen;
  101. dout(" skipping last %llu, final file extent %llu~%llu\n",
  102. orig_len - *plen, off, *plen);
  103. }
  104. dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
  105. return 0;
  106. }
  107. static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
  108. {
  109. memset(osd_data, 0, sizeof (*osd_data));
  110. osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
  111. }
  112. /*
  113. * Consumes @pages if @own_pages is true.
  114. */
  115. static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
  116. struct page **pages, u64 length, u32 alignment,
  117. bool pages_from_pool, bool own_pages)
  118. {
  119. osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
  120. osd_data->pages = pages;
  121. osd_data->length = length;
  122. osd_data->alignment = alignment;
  123. osd_data->pages_from_pool = pages_from_pool;
  124. osd_data->own_pages = own_pages;
  125. }
  126. /*
  127. * Consumes a ref on @pagelist.
  128. */
  129. static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
  130. struct ceph_pagelist *pagelist)
  131. {
  132. osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
  133. osd_data->pagelist = pagelist;
  134. }
  135. #ifdef CONFIG_BLOCK
  136. static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
  137. struct ceph_bio_iter *bio_pos,
  138. u32 bio_length)
  139. {
  140. osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
  141. osd_data->bio_pos = *bio_pos;
  142. osd_data->bio_length = bio_length;
  143. }
  144. #endif /* CONFIG_BLOCK */
  145. static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
  146. struct ceph_bvec_iter *bvec_pos,
  147. u32 num_bvecs)
  148. {
  149. osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
  150. osd_data->bvec_pos = *bvec_pos;
  151. osd_data->num_bvecs = num_bvecs;
  152. }
  153. static struct ceph_osd_data *
  154. osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
  155. {
  156. BUG_ON(which >= osd_req->r_num_ops);
  157. return &osd_req->r_ops[which].raw_data_in;
  158. }
  159. struct ceph_osd_data *
  160. osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
  161. unsigned int which)
  162. {
  163. return osd_req_op_data(osd_req, which, extent, osd_data);
  164. }
  165. EXPORT_SYMBOL(osd_req_op_extent_osd_data);
  166. void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
  167. unsigned int which, struct page **pages,
  168. u64 length, u32 alignment,
  169. bool pages_from_pool, bool own_pages)
  170. {
  171. struct ceph_osd_data *osd_data;
  172. osd_data = osd_req_op_raw_data_in(osd_req, which);
  173. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  174. pages_from_pool, own_pages);
  175. }
  176. EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
  177. void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
  178. unsigned int which, struct page **pages,
  179. u64 length, u32 alignment,
  180. bool pages_from_pool, bool own_pages)
  181. {
  182. struct ceph_osd_data *osd_data;
  183. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  184. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  185. pages_from_pool, own_pages);
  186. }
  187. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
  188. void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
  189. unsigned int which, struct ceph_pagelist *pagelist)
  190. {
  191. struct ceph_osd_data *osd_data;
  192. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  193. ceph_osd_data_pagelist_init(osd_data, pagelist);
  194. }
  195. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
  196. #ifdef CONFIG_BLOCK
  197. void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
  198. unsigned int which,
  199. struct ceph_bio_iter *bio_pos,
  200. u32 bio_length)
  201. {
  202. struct ceph_osd_data *osd_data;
  203. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  204. ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
  205. }
  206. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
  207. #endif /* CONFIG_BLOCK */
  208. void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
  209. unsigned int which,
  210. struct bio_vec *bvecs, u32 num_bvecs,
  211. u32 bytes)
  212. {
  213. struct ceph_osd_data *osd_data;
  214. struct ceph_bvec_iter it = {
  215. .bvecs = bvecs,
  216. .iter = { .bi_size = bytes },
  217. };
  218. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  219. ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
  220. }
  221. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
  222. void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
  223. unsigned int which,
  224. struct ceph_bvec_iter *bvec_pos)
  225. {
  226. struct ceph_osd_data *osd_data;
  227. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  228. ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
  229. }
  230. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
  231. static void osd_req_op_cls_request_info_pagelist(
  232. struct ceph_osd_request *osd_req,
  233. unsigned int which, struct ceph_pagelist *pagelist)
  234. {
  235. struct ceph_osd_data *osd_data;
  236. osd_data = osd_req_op_data(osd_req, which, cls, request_info);
  237. ceph_osd_data_pagelist_init(osd_data, pagelist);
  238. }
  239. void osd_req_op_cls_request_data_pagelist(
  240. struct ceph_osd_request *osd_req,
  241. unsigned int which, struct ceph_pagelist *pagelist)
  242. {
  243. struct ceph_osd_data *osd_data;
  244. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  245. ceph_osd_data_pagelist_init(osd_data, pagelist);
  246. osd_req->r_ops[which].cls.indata_len += pagelist->length;
  247. osd_req->r_ops[which].indata_len += pagelist->length;
  248. }
  249. EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
  250. void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
  251. unsigned int which, struct page **pages, u64 length,
  252. u32 alignment, bool pages_from_pool, bool own_pages)
  253. {
  254. struct ceph_osd_data *osd_data;
  255. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  256. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  257. pages_from_pool, own_pages);
  258. osd_req->r_ops[which].cls.indata_len += length;
  259. osd_req->r_ops[which].indata_len += length;
  260. }
  261. EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
  262. void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
  263. unsigned int which,
  264. struct bio_vec *bvecs, u32 num_bvecs,
  265. u32 bytes)
  266. {
  267. struct ceph_osd_data *osd_data;
  268. struct ceph_bvec_iter it = {
  269. .bvecs = bvecs,
  270. .iter = { .bi_size = bytes },
  271. };
  272. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  273. ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
  274. osd_req->r_ops[which].cls.indata_len += bytes;
  275. osd_req->r_ops[which].indata_len += bytes;
  276. }
  277. EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
  278. void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
  279. unsigned int which, struct page **pages, u64 length,
  280. u32 alignment, bool pages_from_pool, bool own_pages)
  281. {
  282. struct ceph_osd_data *osd_data;
  283. osd_data = osd_req_op_data(osd_req, which, cls, response_data);
  284. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  285. pages_from_pool, own_pages);
  286. }
  287. EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
  288. static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
  289. {
  290. switch (osd_data->type) {
  291. case CEPH_OSD_DATA_TYPE_NONE:
  292. return 0;
  293. case CEPH_OSD_DATA_TYPE_PAGES:
  294. return osd_data->length;
  295. case CEPH_OSD_DATA_TYPE_PAGELIST:
  296. return (u64)osd_data->pagelist->length;
  297. #ifdef CONFIG_BLOCK
  298. case CEPH_OSD_DATA_TYPE_BIO:
  299. return (u64)osd_data->bio_length;
  300. #endif /* CONFIG_BLOCK */
  301. case CEPH_OSD_DATA_TYPE_BVECS:
  302. return osd_data->bvec_pos.iter.bi_size;
  303. default:
  304. WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
  305. return 0;
  306. }
  307. }
  308. static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
  309. {
  310. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
  311. int num_pages;
  312. num_pages = calc_pages_for((u64)osd_data->alignment,
  313. (u64)osd_data->length);
  314. ceph_release_page_vector(osd_data->pages, num_pages);
  315. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
  316. ceph_pagelist_release(osd_data->pagelist);
  317. }
  318. ceph_osd_data_init(osd_data);
  319. }
  320. static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
  321. unsigned int which)
  322. {
  323. struct ceph_osd_req_op *op;
  324. BUG_ON(which >= osd_req->r_num_ops);
  325. op = &osd_req->r_ops[which];
  326. switch (op->op) {
  327. case CEPH_OSD_OP_READ:
  328. case CEPH_OSD_OP_WRITE:
  329. case CEPH_OSD_OP_WRITEFULL:
  330. ceph_osd_data_release(&op->extent.osd_data);
  331. break;
  332. case CEPH_OSD_OP_CALL:
  333. ceph_osd_data_release(&op->cls.request_info);
  334. ceph_osd_data_release(&op->cls.request_data);
  335. ceph_osd_data_release(&op->cls.response_data);
  336. break;
  337. case CEPH_OSD_OP_SETXATTR:
  338. case CEPH_OSD_OP_CMPXATTR:
  339. ceph_osd_data_release(&op->xattr.osd_data);
  340. break;
  341. case CEPH_OSD_OP_STAT:
  342. ceph_osd_data_release(&op->raw_data_in);
  343. break;
  344. case CEPH_OSD_OP_NOTIFY_ACK:
  345. ceph_osd_data_release(&op->notify_ack.request_data);
  346. break;
  347. case CEPH_OSD_OP_NOTIFY:
  348. ceph_osd_data_release(&op->notify.request_data);
  349. ceph_osd_data_release(&op->notify.response_data);
  350. break;
  351. case CEPH_OSD_OP_LIST_WATCHERS:
  352. ceph_osd_data_release(&op->list_watchers.response_data);
  353. break;
  354. case CEPH_OSD_OP_COPY_FROM2:
  355. ceph_osd_data_release(&op->copy_from.osd_data);
  356. break;
  357. default:
  358. break;
  359. }
  360. }
  361. /*
  362. * Assumes @t is zero-initialized.
  363. */
  364. static void target_init(struct ceph_osd_request_target *t)
  365. {
  366. ceph_oid_init(&t->base_oid);
  367. ceph_oloc_init(&t->base_oloc);
  368. ceph_oid_init(&t->target_oid);
  369. ceph_oloc_init(&t->target_oloc);
  370. ceph_osds_init(&t->acting);
  371. ceph_osds_init(&t->up);
  372. t->size = -1;
  373. t->min_size = -1;
  374. t->osd = CEPH_HOMELESS_OSD;
  375. }
  376. static void target_copy(struct ceph_osd_request_target *dest,
  377. const struct ceph_osd_request_target *src)
  378. {
  379. ceph_oid_copy(&dest->base_oid, &src->base_oid);
  380. ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
  381. ceph_oid_copy(&dest->target_oid, &src->target_oid);
  382. ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
  383. dest->pgid = src->pgid; /* struct */
  384. dest->spgid = src->spgid; /* struct */
  385. dest->pg_num = src->pg_num;
  386. dest->pg_num_mask = src->pg_num_mask;
  387. ceph_osds_copy(&dest->acting, &src->acting);
  388. ceph_osds_copy(&dest->up, &src->up);
  389. dest->size = src->size;
  390. dest->min_size = src->min_size;
  391. dest->sort_bitwise = src->sort_bitwise;
  392. dest->flags = src->flags;
  393. dest->paused = src->paused;
  394. dest->epoch = src->epoch;
  395. dest->last_force_resend = src->last_force_resend;
  396. dest->osd = src->osd;
  397. }
  398. static void target_destroy(struct ceph_osd_request_target *t)
  399. {
  400. ceph_oid_destroy(&t->base_oid);
  401. ceph_oloc_destroy(&t->base_oloc);
  402. ceph_oid_destroy(&t->target_oid);
  403. ceph_oloc_destroy(&t->target_oloc);
  404. }
  405. /*
  406. * requests
  407. */
  408. static void request_release_checks(struct ceph_osd_request *req)
  409. {
  410. WARN_ON(!RB_EMPTY_NODE(&req->r_node));
  411. WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
  412. WARN_ON(!list_empty(&req->r_private_item));
  413. WARN_ON(req->r_osd);
  414. }
  415. static void ceph_osdc_release_request(struct kref *kref)
  416. {
  417. struct ceph_osd_request *req = container_of(kref,
  418. struct ceph_osd_request, r_kref);
  419. unsigned int which;
  420. dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
  421. req->r_request, req->r_reply);
  422. request_release_checks(req);
  423. if (req->r_request)
  424. ceph_msg_put(req->r_request);
  425. if (req->r_reply)
  426. ceph_msg_put(req->r_reply);
  427. for (which = 0; which < req->r_num_ops; which++)
  428. osd_req_op_data_release(req, which);
  429. target_destroy(&req->r_t);
  430. ceph_put_snap_context(req->r_snapc);
  431. if (req->r_mempool)
  432. mempool_free(req, req->r_osdc->req_mempool);
  433. else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
  434. kmem_cache_free(ceph_osd_request_cache, req);
  435. else
  436. kfree(req);
  437. }
  438. void ceph_osdc_get_request(struct ceph_osd_request *req)
  439. {
  440. dout("%s %p (was %d)\n", __func__, req,
  441. kref_read(&req->r_kref));
  442. kref_get(&req->r_kref);
  443. }
  444. EXPORT_SYMBOL(ceph_osdc_get_request);
  445. void ceph_osdc_put_request(struct ceph_osd_request *req)
  446. {
  447. if (req) {
  448. dout("%s %p (was %d)\n", __func__, req,
  449. kref_read(&req->r_kref));
  450. kref_put(&req->r_kref, ceph_osdc_release_request);
  451. }
  452. }
  453. EXPORT_SYMBOL(ceph_osdc_put_request);
  454. static void request_init(struct ceph_osd_request *req)
  455. {
  456. /* req only, each op is zeroed in _osd_req_op_init() */
  457. memset(req, 0, sizeof(*req));
  458. kref_init(&req->r_kref);
  459. init_completion(&req->r_completion);
  460. RB_CLEAR_NODE(&req->r_node);
  461. RB_CLEAR_NODE(&req->r_mc_node);
  462. INIT_LIST_HEAD(&req->r_private_item);
  463. target_init(&req->r_t);
  464. }
  465. /*
  466. * This is ugly, but it allows us to reuse linger registration and ping
  467. * requests, keeping the structure of the code around send_linger{_ping}()
  468. * reasonable. Setting up a min_nr=2 mempool for each linger request
  469. * and dealing with copying ops (this blasts req only, watch op remains
  470. * intact) isn't any better.
  471. */
  472. static void request_reinit(struct ceph_osd_request *req)
  473. {
  474. struct ceph_osd_client *osdc = req->r_osdc;
  475. bool mempool = req->r_mempool;
  476. unsigned int num_ops = req->r_num_ops;
  477. u64 snapid = req->r_snapid;
  478. struct ceph_snap_context *snapc = req->r_snapc;
  479. bool linger = req->r_linger;
  480. struct ceph_msg *request_msg = req->r_request;
  481. struct ceph_msg *reply_msg = req->r_reply;
  482. dout("%s req %p\n", __func__, req);
  483. WARN_ON(kref_read(&req->r_kref) != 1);
  484. request_release_checks(req);
  485. WARN_ON(kref_read(&request_msg->kref) != 1);
  486. WARN_ON(kref_read(&reply_msg->kref) != 1);
  487. target_destroy(&req->r_t);
  488. request_init(req);
  489. req->r_osdc = osdc;
  490. req->r_mempool = mempool;
  491. req->r_num_ops = num_ops;
  492. req->r_snapid = snapid;
  493. req->r_snapc = snapc;
  494. req->r_linger = linger;
  495. req->r_request = request_msg;
  496. req->r_reply = reply_msg;
  497. }
  498. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  499. struct ceph_snap_context *snapc,
  500. unsigned int num_ops,
  501. bool use_mempool,
  502. gfp_t gfp_flags)
  503. {
  504. struct ceph_osd_request *req;
  505. if (use_mempool) {
  506. BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
  507. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  508. } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
  509. req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
  510. } else {
  511. BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
  512. req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
  513. }
  514. if (unlikely(!req))
  515. return NULL;
  516. request_init(req);
  517. req->r_osdc = osdc;
  518. req->r_mempool = use_mempool;
  519. req->r_num_ops = num_ops;
  520. req->r_snapid = CEPH_NOSNAP;
  521. req->r_snapc = ceph_get_snap_context(snapc);
  522. dout("%s req %p\n", __func__, req);
  523. return req;
  524. }
  525. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  526. static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
  527. {
  528. return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
  529. }
  530. static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
  531. int num_request_data_items,
  532. int num_reply_data_items)
  533. {
  534. struct ceph_osd_client *osdc = req->r_osdc;
  535. struct ceph_msg *msg;
  536. int msg_size;
  537. WARN_ON(req->r_request || req->r_reply);
  538. WARN_ON(ceph_oid_empty(&req->r_base_oid));
  539. WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
  540. /* create request message */
  541. msg_size = CEPH_ENCODING_START_BLK_LEN +
  542. CEPH_PGID_ENCODING_LEN + 1; /* spgid */
  543. msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
  544. msg_size += CEPH_ENCODING_START_BLK_LEN +
  545. sizeof(struct ceph_osd_reqid); /* reqid */
  546. msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
  547. msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
  548. msg_size += CEPH_ENCODING_START_BLK_LEN +
  549. ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
  550. msg_size += 4 + req->r_base_oid.name_len; /* oid */
  551. msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
  552. msg_size += 8; /* snapid */
  553. msg_size += 8; /* snap_seq */
  554. msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
  555. msg_size += 4 + 8; /* retry_attempt, features */
  556. if (req->r_mempool)
  557. msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
  558. num_request_data_items);
  559. else
  560. msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
  561. num_request_data_items, gfp, true);
  562. if (!msg)
  563. return -ENOMEM;
  564. memset(msg->front.iov_base, 0, msg->front.iov_len);
  565. req->r_request = msg;
  566. /* create reply message */
  567. msg_size = OSD_OPREPLY_FRONT_LEN;
  568. msg_size += req->r_base_oid.name_len;
  569. msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
  570. if (req->r_mempool)
  571. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
  572. num_reply_data_items);
  573. else
  574. msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
  575. num_reply_data_items, gfp, true);
  576. if (!msg)
  577. return -ENOMEM;
  578. req->r_reply = msg;
  579. return 0;
  580. }
  581. static bool osd_req_opcode_valid(u16 opcode)
  582. {
  583. switch (opcode) {
  584. #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
  585. __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
  586. #undef GENERATE_CASE
  587. default:
  588. return false;
  589. }
  590. }
  591. static void get_num_data_items(struct ceph_osd_request *req,
  592. int *num_request_data_items,
  593. int *num_reply_data_items)
  594. {
  595. struct ceph_osd_req_op *op;
  596. *num_request_data_items = 0;
  597. *num_reply_data_items = 0;
  598. for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
  599. switch (op->op) {
  600. /* request */
  601. case CEPH_OSD_OP_WRITE:
  602. case CEPH_OSD_OP_WRITEFULL:
  603. case CEPH_OSD_OP_SETXATTR:
  604. case CEPH_OSD_OP_CMPXATTR:
  605. case CEPH_OSD_OP_NOTIFY_ACK:
  606. case CEPH_OSD_OP_COPY_FROM2:
  607. *num_request_data_items += 1;
  608. break;
  609. /* reply */
  610. case CEPH_OSD_OP_STAT:
  611. case CEPH_OSD_OP_READ:
  612. case CEPH_OSD_OP_LIST_WATCHERS:
  613. *num_reply_data_items += 1;
  614. break;
  615. /* both */
  616. case CEPH_OSD_OP_NOTIFY:
  617. *num_request_data_items += 1;
  618. *num_reply_data_items += 1;
  619. break;
  620. case CEPH_OSD_OP_CALL:
  621. *num_request_data_items += 2;
  622. *num_reply_data_items += 1;
  623. break;
  624. default:
  625. WARN_ON(!osd_req_opcode_valid(op->op));
  626. break;
  627. }
  628. }
  629. }
  630. /*
  631. * oid, oloc and OSD op opcode(s) must be filled in before this function
  632. * is called.
  633. */
  634. int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
  635. {
  636. int num_request_data_items, num_reply_data_items;
  637. get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
  638. return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
  639. num_reply_data_items);
  640. }
  641. EXPORT_SYMBOL(ceph_osdc_alloc_messages);
  642. /*
  643. * This is an osd op init function for opcodes that have no data or
  644. * other information associated with them. It also serves as a
  645. * common init routine for all the other init functions, below.
  646. */
  647. static struct ceph_osd_req_op *
  648. _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
  649. u16 opcode, u32 flags)
  650. {
  651. struct ceph_osd_req_op *op;
  652. BUG_ON(which >= osd_req->r_num_ops);
  653. BUG_ON(!osd_req_opcode_valid(opcode));
  654. op = &osd_req->r_ops[which];
  655. memset(op, 0, sizeof (*op));
  656. op->op = opcode;
  657. op->flags = flags;
  658. return op;
  659. }
  660. void osd_req_op_init(struct ceph_osd_request *osd_req,
  661. unsigned int which, u16 opcode, u32 flags)
  662. {
  663. (void)_osd_req_op_init(osd_req, which, opcode, flags);
  664. }
  665. EXPORT_SYMBOL(osd_req_op_init);
  666. void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
  667. unsigned int which, u16 opcode,
  668. u64 offset, u64 length,
  669. u64 truncate_size, u32 truncate_seq)
  670. {
  671. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
  672. opcode, 0);
  673. size_t payload_len = 0;
  674. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
  675. opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
  676. opcode != CEPH_OSD_OP_TRUNCATE);
  677. op->extent.offset = offset;
  678. op->extent.length = length;
  679. op->extent.truncate_size = truncate_size;
  680. op->extent.truncate_seq = truncate_seq;
  681. if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
  682. payload_len += length;
  683. op->indata_len = payload_len;
  684. }
  685. EXPORT_SYMBOL(osd_req_op_extent_init);
  686. void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
  687. unsigned int which, u64 length)
  688. {
  689. struct ceph_osd_req_op *op;
  690. u64 previous;
  691. BUG_ON(which >= osd_req->r_num_ops);
  692. op = &osd_req->r_ops[which];
  693. previous = op->extent.length;
  694. if (length == previous)
  695. return; /* Nothing to do */
  696. BUG_ON(length > previous);
  697. op->extent.length = length;
  698. if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
  699. op->indata_len -= previous - length;
  700. }
  701. EXPORT_SYMBOL(osd_req_op_extent_update);
  702. void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
  703. unsigned int which, u64 offset_inc)
  704. {
  705. struct ceph_osd_req_op *op, *prev_op;
  706. BUG_ON(which + 1 >= osd_req->r_num_ops);
  707. prev_op = &osd_req->r_ops[which];
  708. op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
  709. /* dup previous one */
  710. op->indata_len = prev_op->indata_len;
  711. op->outdata_len = prev_op->outdata_len;
  712. op->extent = prev_op->extent;
  713. /* adjust offset */
  714. op->extent.offset += offset_inc;
  715. op->extent.length -= offset_inc;
  716. if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
  717. op->indata_len -= offset_inc;
  718. }
  719. EXPORT_SYMBOL(osd_req_op_extent_dup_last);
  720. int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
  721. const char *class, const char *method)
  722. {
  723. struct ceph_osd_req_op *op;
  724. struct ceph_pagelist *pagelist;
  725. size_t payload_len = 0;
  726. size_t size;
  727. int ret;
  728. op = _osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
  729. pagelist = ceph_pagelist_alloc(GFP_NOFS);
  730. if (!pagelist)
  731. return -ENOMEM;
  732. op->cls.class_name = class;
  733. size = strlen(class);
  734. BUG_ON(size > (size_t) U8_MAX);
  735. op->cls.class_len = size;
  736. ret = ceph_pagelist_append(pagelist, class, size);
  737. if (ret)
  738. goto err_pagelist_free;
  739. payload_len += size;
  740. op->cls.method_name = method;
  741. size = strlen(method);
  742. BUG_ON(size > (size_t) U8_MAX);
  743. op->cls.method_len = size;
  744. ret = ceph_pagelist_append(pagelist, method, size);
  745. if (ret)
  746. goto err_pagelist_free;
  747. payload_len += size;
  748. osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
  749. op->indata_len = payload_len;
  750. return 0;
  751. err_pagelist_free:
  752. ceph_pagelist_release(pagelist);
  753. return ret;
  754. }
  755. EXPORT_SYMBOL(osd_req_op_cls_init);
  756. int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
  757. u16 opcode, const char *name, const void *value,
  758. size_t size, u8 cmp_op, u8 cmp_mode)
  759. {
  760. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
  761. opcode, 0);
  762. struct ceph_pagelist *pagelist;
  763. size_t payload_len;
  764. int ret;
  765. BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
  766. pagelist = ceph_pagelist_alloc(GFP_NOFS);
  767. if (!pagelist)
  768. return -ENOMEM;
  769. payload_len = strlen(name);
  770. op->xattr.name_len = payload_len;
  771. ret = ceph_pagelist_append(pagelist, name, payload_len);
  772. if (ret)
  773. goto err_pagelist_free;
  774. op->xattr.value_len = size;
  775. ret = ceph_pagelist_append(pagelist, value, size);
  776. if (ret)
  777. goto err_pagelist_free;
  778. payload_len += size;
  779. op->xattr.cmp_op = cmp_op;
  780. op->xattr.cmp_mode = cmp_mode;
  781. ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
  782. op->indata_len = payload_len;
  783. return 0;
  784. err_pagelist_free:
  785. ceph_pagelist_release(pagelist);
  786. return ret;
  787. }
  788. EXPORT_SYMBOL(osd_req_op_xattr_init);
  789. /*
  790. * @watch_opcode: CEPH_OSD_WATCH_OP_*
  791. */
  792. static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
  793. u64 cookie, u8 watch_opcode)
  794. {
  795. struct ceph_osd_req_op *op;
  796. op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
  797. op->watch.cookie = cookie;
  798. op->watch.op = watch_opcode;
  799. op->watch.gen = 0;
  800. }
  801. void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
  802. unsigned int which,
  803. u64 expected_object_size,
  804. u64 expected_write_size)
  805. {
  806. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
  807. CEPH_OSD_OP_SETALLOCHINT,
  808. 0);
  809. op->alloc_hint.expected_object_size = expected_object_size;
  810. op->alloc_hint.expected_write_size = expected_write_size;
  811. /*
  812. * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
  813. * not worth a feature bit. Set FAILOK per-op flag to make
  814. * sure older osds don't trip over an unsupported opcode.
  815. */
  816. op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
  817. }
  818. EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
  819. static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
  820. struct ceph_osd_data *osd_data)
  821. {
  822. u64 length = ceph_osd_data_length(osd_data);
  823. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  824. BUG_ON(length > (u64) SIZE_MAX);
  825. if (length)
  826. ceph_msg_data_add_pages(msg, osd_data->pages,
  827. length, osd_data->alignment, false);
  828. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
  829. BUG_ON(!length);
  830. ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
  831. #ifdef CONFIG_BLOCK
  832. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
  833. ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
  834. #endif
  835. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
  836. ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
  837. } else {
  838. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
  839. }
  840. }
  841. static u32 osd_req_encode_op(struct ceph_osd_op *dst,
  842. const struct ceph_osd_req_op *src)
  843. {
  844. switch (src->op) {
  845. case CEPH_OSD_OP_STAT:
  846. break;
  847. case CEPH_OSD_OP_READ:
  848. case CEPH_OSD_OP_WRITE:
  849. case CEPH_OSD_OP_WRITEFULL:
  850. case CEPH_OSD_OP_ZERO:
  851. case CEPH_OSD_OP_TRUNCATE:
  852. dst->extent.offset = cpu_to_le64(src->extent.offset);
  853. dst->extent.length = cpu_to_le64(src->extent.length);
  854. dst->extent.truncate_size =
  855. cpu_to_le64(src->extent.truncate_size);
  856. dst->extent.truncate_seq =
  857. cpu_to_le32(src->extent.truncate_seq);
  858. break;
  859. case CEPH_OSD_OP_CALL:
  860. dst->cls.class_len = src->cls.class_len;
  861. dst->cls.method_len = src->cls.method_len;
  862. dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
  863. break;
  864. case CEPH_OSD_OP_WATCH:
  865. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  866. dst->watch.ver = cpu_to_le64(0);
  867. dst->watch.op = src->watch.op;
  868. dst->watch.gen = cpu_to_le32(src->watch.gen);
  869. break;
  870. case CEPH_OSD_OP_NOTIFY_ACK:
  871. break;
  872. case CEPH_OSD_OP_NOTIFY:
  873. dst->notify.cookie = cpu_to_le64(src->notify.cookie);
  874. break;
  875. case CEPH_OSD_OP_LIST_WATCHERS:
  876. break;
  877. case CEPH_OSD_OP_SETALLOCHINT:
  878. dst->alloc_hint.expected_object_size =
  879. cpu_to_le64(src->alloc_hint.expected_object_size);
  880. dst->alloc_hint.expected_write_size =
  881. cpu_to_le64(src->alloc_hint.expected_write_size);
  882. break;
  883. case CEPH_OSD_OP_SETXATTR:
  884. case CEPH_OSD_OP_CMPXATTR:
  885. dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
  886. dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
  887. dst->xattr.cmp_op = src->xattr.cmp_op;
  888. dst->xattr.cmp_mode = src->xattr.cmp_mode;
  889. break;
  890. case CEPH_OSD_OP_CREATE:
  891. case CEPH_OSD_OP_DELETE:
  892. break;
  893. case CEPH_OSD_OP_COPY_FROM2:
  894. dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
  895. dst->copy_from.src_version =
  896. cpu_to_le64(src->copy_from.src_version);
  897. dst->copy_from.flags = src->copy_from.flags;
  898. dst->copy_from.src_fadvise_flags =
  899. cpu_to_le32(src->copy_from.src_fadvise_flags);
  900. break;
  901. default:
  902. pr_err("unsupported osd opcode %s\n",
  903. ceph_osd_op_name(src->op));
  904. WARN_ON(1);
  905. return 0;
  906. }
  907. dst->op = cpu_to_le16(src->op);
  908. dst->flags = cpu_to_le32(src->flags);
  909. dst->payload_len = cpu_to_le32(src->indata_len);
  910. return src->indata_len;
  911. }
  912. /*
  913. * build new request AND message, calculate layout, and adjust file
  914. * extent as needed.
  915. *
  916. * if the file was recently truncated, we include information about its
  917. * old and new size so that the object can be updated appropriately. (we
  918. * avoid synchronously deleting truncated objects because it's slow.)
  919. */
  920. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  921. struct ceph_file_layout *layout,
  922. struct ceph_vino vino,
  923. u64 off, u64 *plen,
  924. unsigned int which, int num_ops,
  925. int opcode, int flags,
  926. struct ceph_snap_context *snapc,
  927. u32 truncate_seq,
  928. u64 truncate_size,
  929. bool use_mempool)
  930. {
  931. struct ceph_osd_request *req;
  932. u64 objnum = 0;
  933. u64 objoff = 0;
  934. u64 objlen = 0;
  935. int r;
  936. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
  937. opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
  938. opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
  939. req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
  940. GFP_NOFS);
  941. if (!req) {
  942. r = -ENOMEM;
  943. goto fail;
  944. }
  945. /* calculate max write size */
  946. r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
  947. if (r)
  948. goto fail;
  949. if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
  950. osd_req_op_init(req, which, opcode, 0);
  951. } else {
  952. u32 object_size = layout->object_size;
  953. u32 object_base = off - objoff;
  954. if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
  955. if (truncate_size <= object_base) {
  956. truncate_size = 0;
  957. } else {
  958. truncate_size -= object_base;
  959. if (truncate_size > object_size)
  960. truncate_size = object_size;
  961. }
  962. }
  963. osd_req_op_extent_init(req, which, opcode, objoff, objlen,
  964. truncate_size, truncate_seq);
  965. }
  966. req->r_flags = flags;
  967. req->r_base_oloc.pool = layout->pool_id;
  968. req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
  969. ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
  970. req->r_snapid = vino.snap;
  971. if (flags & CEPH_OSD_FLAG_WRITE)
  972. req->r_data_offset = off;
  973. if (num_ops > 1)
  974. /*
  975. * This is a special case for ceph_writepages_start(), but it
  976. * also covers ceph_uninline_data(). If more multi-op request
  977. * use cases emerge, we will need a separate helper.
  978. */
  979. r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
  980. else
  981. r = ceph_osdc_alloc_messages(req, GFP_NOFS);
  982. if (r)
  983. goto fail;
  984. return req;
  985. fail:
  986. ceph_osdc_put_request(req);
  987. return ERR_PTR(r);
  988. }
  989. EXPORT_SYMBOL(ceph_osdc_new_request);
  990. /*
  991. * We keep osd requests in an rbtree, sorted by ->r_tid.
  992. */
  993. DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
  994. DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
  995. /*
  996. * Call @fn on each OSD request as long as @fn returns 0.
  997. */
  998. static void for_each_request(struct ceph_osd_client *osdc,
  999. int (*fn)(struct ceph_osd_request *req, void *arg),
  1000. void *arg)
  1001. {
  1002. struct rb_node *n, *p;
  1003. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  1004. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  1005. for (p = rb_first(&osd->o_requests); p; ) {
  1006. struct ceph_osd_request *req =
  1007. rb_entry(p, struct ceph_osd_request, r_node);
  1008. p = rb_next(p);
  1009. if (fn(req, arg))
  1010. return;
  1011. }
  1012. }
  1013. for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
  1014. struct ceph_osd_request *req =
  1015. rb_entry(p, struct ceph_osd_request, r_node);
  1016. p = rb_next(p);
  1017. if (fn(req, arg))
  1018. return;
  1019. }
  1020. }
  1021. static bool osd_homeless(struct ceph_osd *osd)
  1022. {
  1023. return osd->o_osd == CEPH_HOMELESS_OSD;
  1024. }
  1025. static bool osd_registered(struct ceph_osd *osd)
  1026. {
  1027. verify_osdc_locked(osd->o_osdc);
  1028. return !RB_EMPTY_NODE(&osd->o_node);
  1029. }
  1030. /*
  1031. * Assumes @osd is zero-initialized.
  1032. */
  1033. static void osd_init(struct ceph_osd *osd)
  1034. {
  1035. refcount_set(&osd->o_ref, 1);
  1036. RB_CLEAR_NODE(&osd->o_node);
  1037. osd->o_requests = RB_ROOT;
  1038. osd->o_linger_requests = RB_ROOT;
  1039. osd->o_backoff_mappings = RB_ROOT;
  1040. osd->o_backoffs_by_id = RB_ROOT;
  1041. INIT_LIST_HEAD(&osd->o_osd_lru);
  1042. INIT_LIST_HEAD(&osd->o_keepalive_item);
  1043. osd->o_incarnation = 1;
  1044. mutex_init(&osd->lock);
  1045. }
  1046. static void osd_cleanup(struct ceph_osd *osd)
  1047. {
  1048. WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
  1049. WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
  1050. WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
  1051. WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
  1052. WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
  1053. WARN_ON(!list_empty(&osd->o_osd_lru));
  1054. WARN_ON(!list_empty(&osd->o_keepalive_item));
  1055. if (osd->o_auth.authorizer) {
  1056. WARN_ON(osd_homeless(osd));
  1057. ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
  1058. }
  1059. }
  1060. /*
  1061. * Track open sessions with osds.
  1062. */
  1063. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
  1064. {
  1065. struct ceph_osd *osd;
  1066. WARN_ON(onum == CEPH_HOMELESS_OSD);
  1067. osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
  1068. osd_init(osd);
  1069. osd->o_osdc = osdc;
  1070. osd->o_osd = onum;
  1071. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  1072. return osd;
  1073. }
  1074. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  1075. {
  1076. if (refcount_inc_not_zero(&osd->o_ref)) {
  1077. dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
  1078. refcount_read(&osd->o_ref));
  1079. return osd;
  1080. } else {
  1081. dout("get_osd %p FAIL\n", osd);
  1082. return NULL;
  1083. }
  1084. }
  1085. static void put_osd(struct ceph_osd *osd)
  1086. {
  1087. dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
  1088. refcount_read(&osd->o_ref) - 1);
  1089. if (refcount_dec_and_test(&osd->o_ref)) {
  1090. osd_cleanup(osd);
  1091. kfree(osd);
  1092. }
  1093. }
  1094. DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
  1095. static void __move_osd_to_lru(struct ceph_osd *osd)
  1096. {
  1097. struct ceph_osd_client *osdc = osd->o_osdc;
  1098. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1099. BUG_ON(!list_empty(&osd->o_osd_lru));
  1100. spin_lock(&osdc->osd_lru_lock);
  1101. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  1102. spin_unlock(&osdc->osd_lru_lock);
  1103. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
  1104. }
  1105. static void maybe_move_osd_to_lru(struct ceph_osd *osd)
  1106. {
  1107. if (RB_EMPTY_ROOT(&osd->o_requests) &&
  1108. RB_EMPTY_ROOT(&osd->o_linger_requests))
  1109. __move_osd_to_lru(osd);
  1110. }
  1111. static void __remove_osd_from_lru(struct ceph_osd *osd)
  1112. {
  1113. struct ceph_osd_client *osdc = osd->o_osdc;
  1114. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1115. spin_lock(&osdc->osd_lru_lock);
  1116. if (!list_empty(&osd->o_osd_lru))
  1117. list_del_init(&osd->o_osd_lru);
  1118. spin_unlock(&osdc->osd_lru_lock);
  1119. }
  1120. /*
  1121. * Close the connection and assign any leftover requests to the
  1122. * homeless session.
  1123. */
  1124. static void close_osd(struct ceph_osd *osd)
  1125. {
  1126. struct ceph_osd_client *osdc = osd->o_osdc;
  1127. struct rb_node *n;
  1128. verify_osdc_wrlocked(osdc);
  1129. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1130. ceph_con_close(&osd->o_con);
  1131. for (n = rb_first(&osd->o_requests); n; ) {
  1132. struct ceph_osd_request *req =
  1133. rb_entry(n, struct ceph_osd_request, r_node);
  1134. n = rb_next(n); /* unlink_request() */
  1135. dout(" reassigning req %p tid %llu\n", req, req->r_tid);
  1136. unlink_request(osd, req);
  1137. link_request(&osdc->homeless_osd, req);
  1138. }
  1139. for (n = rb_first(&osd->o_linger_requests); n; ) {
  1140. struct ceph_osd_linger_request *lreq =
  1141. rb_entry(n, struct ceph_osd_linger_request, node);
  1142. n = rb_next(n); /* unlink_linger() */
  1143. dout(" reassigning lreq %p linger_id %llu\n", lreq,
  1144. lreq->linger_id);
  1145. unlink_linger(osd, lreq);
  1146. link_linger(&osdc->homeless_osd, lreq);
  1147. }
  1148. clear_backoffs(osd);
  1149. __remove_osd_from_lru(osd);
  1150. erase_osd(&osdc->osds, osd);
  1151. put_osd(osd);
  1152. }
  1153. /*
  1154. * reset osd connect
  1155. */
  1156. static int reopen_osd(struct ceph_osd *osd)
  1157. {
  1158. struct ceph_entity_addr *peer_addr;
  1159. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1160. if (RB_EMPTY_ROOT(&osd->o_requests) &&
  1161. RB_EMPTY_ROOT(&osd->o_linger_requests)) {
  1162. close_osd(osd);
  1163. return -ENODEV;
  1164. }
  1165. peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
  1166. if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
  1167. !ceph_con_opened(&osd->o_con)) {
  1168. struct rb_node *n;
  1169. dout("osd addr hasn't changed and connection never opened, "
  1170. "letting msgr retry\n");
  1171. /* touch each r_stamp for handle_timeout()'s benfit */
  1172. for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
  1173. struct ceph_osd_request *req =
  1174. rb_entry(n, struct ceph_osd_request, r_node);
  1175. req->r_stamp = jiffies;
  1176. }
  1177. return -EAGAIN;
  1178. }
  1179. ceph_con_close(&osd->o_con);
  1180. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
  1181. osd->o_incarnation++;
  1182. return 0;
  1183. }
  1184. static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
  1185. bool wrlocked)
  1186. {
  1187. struct ceph_osd *osd;
  1188. if (wrlocked)
  1189. verify_osdc_wrlocked(osdc);
  1190. else
  1191. verify_osdc_locked(osdc);
  1192. if (o != CEPH_HOMELESS_OSD)
  1193. osd = lookup_osd(&osdc->osds, o);
  1194. else
  1195. osd = &osdc->homeless_osd;
  1196. if (!osd) {
  1197. if (!wrlocked)
  1198. return ERR_PTR(-EAGAIN);
  1199. osd = create_osd(osdc, o);
  1200. insert_osd(&osdc->osds, osd);
  1201. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
  1202. &osdc->osdmap->osd_addr[osd->o_osd]);
  1203. }
  1204. dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
  1205. return osd;
  1206. }
  1207. /*
  1208. * Create request <-> OSD session relation.
  1209. *
  1210. * @req has to be assigned a tid, @osd may be homeless.
  1211. */
  1212. static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
  1213. {
  1214. verify_osd_locked(osd);
  1215. WARN_ON(!req->r_tid || req->r_osd);
  1216. dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
  1217. req, req->r_tid);
  1218. if (!osd_homeless(osd))
  1219. __remove_osd_from_lru(osd);
  1220. else
  1221. atomic_inc(&osd->o_osdc->num_homeless);
  1222. get_osd(osd);
  1223. insert_request(&osd->o_requests, req);
  1224. req->r_osd = osd;
  1225. }
  1226. static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
  1227. {
  1228. verify_osd_locked(osd);
  1229. WARN_ON(req->r_osd != osd);
  1230. dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
  1231. req, req->r_tid);
  1232. req->r_osd = NULL;
  1233. erase_request(&osd->o_requests, req);
  1234. put_osd(osd);
  1235. if (!osd_homeless(osd))
  1236. maybe_move_osd_to_lru(osd);
  1237. else
  1238. atomic_dec(&osd->o_osdc->num_homeless);
  1239. }
  1240. static bool __pool_full(struct ceph_pg_pool_info *pi)
  1241. {
  1242. return pi->flags & CEPH_POOL_FLAG_FULL;
  1243. }
  1244. static bool have_pool_full(struct ceph_osd_client *osdc)
  1245. {
  1246. struct rb_node *n;
  1247. for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
  1248. struct ceph_pg_pool_info *pi =
  1249. rb_entry(n, struct ceph_pg_pool_info, node);
  1250. if (__pool_full(pi))
  1251. return true;
  1252. }
  1253. return false;
  1254. }
  1255. static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
  1256. {
  1257. struct ceph_pg_pool_info *pi;
  1258. pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
  1259. if (!pi)
  1260. return false;
  1261. return __pool_full(pi);
  1262. }
  1263. /*
  1264. * Returns whether a request should be blocked from being sent
  1265. * based on the current osdmap and osd_client settings.
  1266. */
  1267. static bool target_should_be_paused(struct ceph_osd_client *osdc,
  1268. const struct ceph_osd_request_target *t,
  1269. struct ceph_pg_pool_info *pi)
  1270. {
  1271. bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  1272. bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  1273. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  1274. __pool_full(pi);
  1275. WARN_ON(pi->id != t->target_oloc.pool);
  1276. return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
  1277. ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
  1278. (osdc->osdmap->epoch < osdc->epoch_barrier);
  1279. }
  1280. enum calc_target_result {
  1281. CALC_TARGET_NO_ACTION = 0,
  1282. CALC_TARGET_NEED_RESEND,
  1283. CALC_TARGET_POOL_DNE,
  1284. };
  1285. static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
  1286. struct ceph_osd_request_target *t,
  1287. bool any_change)
  1288. {
  1289. struct ceph_pg_pool_info *pi;
  1290. struct ceph_pg pgid, last_pgid;
  1291. struct ceph_osds up, acting;
  1292. bool force_resend = false;
  1293. bool unpaused = false;
  1294. bool legacy_change = false;
  1295. bool split = false;
  1296. bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
  1297. bool recovery_deletes = ceph_osdmap_flag(osdc,
  1298. CEPH_OSDMAP_RECOVERY_DELETES);
  1299. enum calc_target_result ct_res;
  1300. t->epoch = osdc->osdmap->epoch;
  1301. pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
  1302. if (!pi) {
  1303. t->osd = CEPH_HOMELESS_OSD;
  1304. ct_res = CALC_TARGET_POOL_DNE;
  1305. goto out;
  1306. }
  1307. if (osdc->osdmap->epoch == pi->last_force_request_resend) {
  1308. if (t->last_force_resend < pi->last_force_request_resend) {
  1309. t->last_force_resend = pi->last_force_request_resend;
  1310. force_resend = true;
  1311. } else if (t->last_force_resend == 0) {
  1312. force_resend = true;
  1313. }
  1314. }
  1315. /* apply tiering */
  1316. ceph_oid_copy(&t->target_oid, &t->base_oid);
  1317. ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
  1318. if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
  1319. if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
  1320. t->target_oloc.pool = pi->read_tier;
  1321. if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
  1322. t->target_oloc.pool = pi->write_tier;
  1323. pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
  1324. if (!pi) {
  1325. t->osd = CEPH_HOMELESS_OSD;
  1326. ct_res = CALC_TARGET_POOL_DNE;
  1327. goto out;
  1328. }
  1329. }
  1330. __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
  1331. last_pgid.pool = pgid.pool;
  1332. last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
  1333. ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
  1334. if (any_change &&
  1335. ceph_is_new_interval(&t->acting,
  1336. &acting,
  1337. &t->up,
  1338. &up,
  1339. t->size,
  1340. pi->size,
  1341. t->min_size,
  1342. pi->min_size,
  1343. t->pg_num,
  1344. pi->pg_num,
  1345. t->sort_bitwise,
  1346. sort_bitwise,
  1347. t->recovery_deletes,
  1348. recovery_deletes,
  1349. &last_pgid))
  1350. force_resend = true;
  1351. if (t->paused && !target_should_be_paused(osdc, t, pi)) {
  1352. t->paused = false;
  1353. unpaused = true;
  1354. }
  1355. legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
  1356. ceph_osds_changed(&t->acting, &acting, any_change);
  1357. if (t->pg_num)
  1358. split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
  1359. if (legacy_change || force_resend || split) {
  1360. t->pgid = pgid; /* struct */
  1361. ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
  1362. ceph_osds_copy(&t->acting, &acting);
  1363. ceph_osds_copy(&t->up, &up);
  1364. t->size = pi->size;
  1365. t->min_size = pi->min_size;
  1366. t->pg_num = pi->pg_num;
  1367. t->pg_num_mask = pi->pg_num_mask;
  1368. t->sort_bitwise = sort_bitwise;
  1369. t->recovery_deletes = recovery_deletes;
  1370. t->osd = acting.primary;
  1371. }
  1372. if (unpaused || legacy_change || force_resend || split)
  1373. ct_res = CALC_TARGET_NEED_RESEND;
  1374. else
  1375. ct_res = CALC_TARGET_NO_ACTION;
  1376. out:
  1377. dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
  1378. legacy_change, force_resend, split, ct_res, t->osd);
  1379. return ct_res;
  1380. }
  1381. static struct ceph_spg_mapping *alloc_spg_mapping(void)
  1382. {
  1383. struct ceph_spg_mapping *spg;
  1384. spg = kmalloc(sizeof(*spg), GFP_NOIO);
  1385. if (!spg)
  1386. return NULL;
  1387. RB_CLEAR_NODE(&spg->node);
  1388. spg->backoffs = RB_ROOT;
  1389. return spg;
  1390. }
  1391. static void free_spg_mapping(struct ceph_spg_mapping *spg)
  1392. {
  1393. WARN_ON(!RB_EMPTY_NODE(&spg->node));
  1394. WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
  1395. kfree(spg);
  1396. }
  1397. /*
  1398. * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
  1399. * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
  1400. * defined only within a specific spgid; it does not pass anything to
  1401. * children on split, or to another primary.
  1402. */
  1403. DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
  1404. RB_BYPTR, const struct ceph_spg *, node)
  1405. static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
  1406. {
  1407. return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
  1408. }
  1409. static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
  1410. void **pkey, size_t *pkey_len)
  1411. {
  1412. if (hoid->key_len) {
  1413. *pkey = hoid->key;
  1414. *pkey_len = hoid->key_len;
  1415. } else {
  1416. *pkey = hoid->oid;
  1417. *pkey_len = hoid->oid_len;
  1418. }
  1419. }
  1420. static int compare_names(const void *name1, size_t name1_len,
  1421. const void *name2, size_t name2_len)
  1422. {
  1423. int ret;
  1424. ret = memcmp(name1, name2, min(name1_len, name2_len));
  1425. if (!ret) {
  1426. if (name1_len < name2_len)
  1427. ret = -1;
  1428. else if (name1_len > name2_len)
  1429. ret = 1;
  1430. }
  1431. return ret;
  1432. }
  1433. static int hoid_compare(const struct ceph_hobject_id *lhs,
  1434. const struct ceph_hobject_id *rhs)
  1435. {
  1436. void *effective_key1, *effective_key2;
  1437. size_t effective_key1_len, effective_key2_len;
  1438. int ret;
  1439. if (lhs->is_max < rhs->is_max)
  1440. return -1;
  1441. if (lhs->is_max > rhs->is_max)
  1442. return 1;
  1443. if (lhs->pool < rhs->pool)
  1444. return -1;
  1445. if (lhs->pool > rhs->pool)
  1446. return 1;
  1447. if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
  1448. return -1;
  1449. if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
  1450. return 1;
  1451. ret = compare_names(lhs->nspace, lhs->nspace_len,
  1452. rhs->nspace, rhs->nspace_len);
  1453. if (ret)
  1454. return ret;
  1455. hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
  1456. hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
  1457. ret = compare_names(effective_key1, effective_key1_len,
  1458. effective_key2, effective_key2_len);
  1459. if (ret)
  1460. return ret;
  1461. ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
  1462. if (ret)
  1463. return ret;
  1464. if (lhs->snapid < rhs->snapid)
  1465. return -1;
  1466. if (lhs->snapid > rhs->snapid)
  1467. return 1;
  1468. return 0;
  1469. }
  1470. /*
  1471. * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
  1472. * compat stuff here.
  1473. *
  1474. * Assumes @hoid is zero-initialized.
  1475. */
  1476. static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
  1477. {
  1478. u8 struct_v;
  1479. u32 struct_len;
  1480. int ret;
  1481. ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
  1482. &struct_len);
  1483. if (ret)
  1484. return ret;
  1485. if (struct_v < 4) {
  1486. pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
  1487. goto e_inval;
  1488. }
  1489. hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
  1490. GFP_NOIO);
  1491. if (IS_ERR(hoid->key)) {
  1492. ret = PTR_ERR(hoid->key);
  1493. hoid->key = NULL;
  1494. return ret;
  1495. }
  1496. hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
  1497. GFP_NOIO);
  1498. if (IS_ERR(hoid->oid)) {
  1499. ret = PTR_ERR(hoid->oid);
  1500. hoid->oid = NULL;
  1501. return ret;
  1502. }
  1503. ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
  1504. ceph_decode_32_safe(p, end, hoid->hash, e_inval);
  1505. ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
  1506. hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
  1507. GFP_NOIO);
  1508. if (IS_ERR(hoid->nspace)) {
  1509. ret = PTR_ERR(hoid->nspace);
  1510. hoid->nspace = NULL;
  1511. return ret;
  1512. }
  1513. ceph_decode_64_safe(p, end, hoid->pool, e_inval);
  1514. ceph_hoid_build_hash_cache(hoid);
  1515. return 0;
  1516. e_inval:
  1517. return -EINVAL;
  1518. }
  1519. static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
  1520. {
  1521. return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
  1522. 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
  1523. }
  1524. static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
  1525. {
  1526. ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
  1527. ceph_encode_string(p, end, hoid->key, hoid->key_len);
  1528. ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
  1529. ceph_encode_64(p, hoid->snapid);
  1530. ceph_encode_32(p, hoid->hash);
  1531. ceph_encode_8(p, hoid->is_max);
  1532. ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
  1533. ceph_encode_64(p, hoid->pool);
  1534. }
  1535. static void free_hoid(struct ceph_hobject_id *hoid)
  1536. {
  1537. if (hoid) {
  1538. kfree(hoid->key);
  1539. kfree(hoid->oid);
  1540. kfree(hoid->nspace);
  1541. kfree(hoid);
  1542. }
  1543. }
  1544. static struct ceph_osd_backoff *alloc_backoff(void)
  1545. {
  1546. struct ceph_osd_backoff *backoff;
  1547. backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
  1548. if (!backoff)
  1549. return NULL;
  1550. RB_CLEAR_NODE(&backoff->spg_node);
  1551. RB_CLEAR_NODE(&backoff->id_node);
  1552. return backoff;
  1553. }
  1554. static void free_backoff(struct ceph_osd_backoff *backoff)
  1555. {
  1556. WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
  1557. WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
  1558. free_hoid(backoff->begin);
  1559. free_hoid(backoff->end);
  1560. kfree(backoff);
  1561. }
  1562. /*
  1563. * Within a specific spgid, backoffs are managed by ->begin hoid.
  1564. */
  1565. DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
  1566. RB_BYVAL, spg_node);
  1567. static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
  1568. const struct ceph_hobject_id *hoid)
  1569. {
  1570. struct rb_node *n = root->rb_node;
  1571. while (n) {
  1572. struct ceph_osd_backoff *cur =
  1573. rb_entry(n, struct ceph_osd_backoff, spg_node);
  1574. int cmp;
  1575. cmp = hoid_compare(hoid, cur->begin);
  1576. if (cmp < 0) {
  1577. n = n->rb_left;
  1578. } else if (cmp > 0) {
  1579. if (hoid_compare(hoid, cur->end) < 0)
  1580. return cur;
  1581. n = n->rb_right;
  1582. } else {
  1583. return cur;
  1584. }
  1585. }
  1586. return NULL;
  1587. }
  1588. /*
  1589. * Each backoff has a unique id within its OSD session.
  1590. */
  1591. DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
  1592. static void clear_backoffs(struct ceph_osd *osd)
  1593. {
  1594. while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
  1595. struct ceph_spg_mapping *spg =
  1596. rb_entry(rb_first(&osd->o_backoff_mappings),
  1597. struct ceph_spg_mapping, node);
  1598. while (!RB_EMPTY_ROOT(&spg->backoffs)) {
  1599. struct ceph_osd_backoff *backoff =
  1600. rb_entry(rb_first(&spg->backoffs),
  1601. struct ceph_osd_backoff, spg_node);
  1602. erase_backoff(&spg->backoffs, backoff);
  1603. erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  1604. free_backoff(backoff);
  1605. }
  1606. erase_spg_mapping(&osd->o_backoff_mappings, spg);
  1607. free_spg_mapping(spg);
  1608. }
  1609. }
  1610. /*
  1611. * Set up a temporary, non-owning view into @t.
  1612. */
  1613. static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
  1614. const struct ceph_osd_request_target *t)
  1615. {
  1616. hoid->key = NULL;
  1617. hoid->key_len = 0;
  1618. hoid->oid = t->target_oid.name;
  1619. hoid->oid_len = t->target_oid.name_len;
  1620. hoid->snapid = CEPH_NOSNAP;
  1621. hoid->hash = t->pgid.seed;
  1622. hoid->is_max = false;
  1623. if (t->target_oloc.pool_ns) {
  1624. hoid->nspace = t->target_oloc.pool_ns->str;
  1625. hoid->nspace_len = t->target_oloc.pool_ns->len;
  1626. } else {
  1627. hoid->nspace = NULL;
  1628. hoid->nspace_len = 0;
  1629. }
  1630. hoid->pool = t->target_oloc.pool;
  1631. ceph_hoid_build_hash_cache(hoid);
  1632. }
  1633. static bool should_plug_request(struct ceph_osd_request *req)
  1634. {
  1635. struct ceph_osd *osd = req->r_osd;
  1636. struct ceph_spg_mapping *spg;
  1637. struct ceph_osd_backoff *backoff;
  1638. struct ceph_hobject_id hoid;
  1639. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
  1640. if (!spg)
  1641. return false;
  1642. hoid_fill_from_target(&hoid, &req->r_t);
  1643. backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
  1644. if (!backoff)
  1645. return false;
  1646. dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
  1647. __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
  1648. backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
  1649. return true;
  1650. }
  1651. /*
  1652. * Keep get_num_data_items() in sync with this function.
  1653. */
  1654. static void setup_request_data(struct ceph_osd_request *req)
  1655. {
  1656. struct ceph_msg *request_msg = req->r_request;
  1657. struct ceph_msg *reply_msg = req->r_reply;
  1658. struct ceph_osd_req_op *op;
  1659. if (req->r_request->num_data_items || req->r_reply->num_data_items)
  1660. return;
  1661. WARN_ON(request_msg->data_length || reply_msg->data_length);
  1662. for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
  1663. switch (op->op) {
  1664. /* request */
  1665. case CEPH_OSD_OP_WRITE:
  1666. case CEPH_OSD_OP_WRITEFULL:
  1667. WARN_ON(op->indata_len != op->extent.length);
  1668. ceph_osdc_msg_data_add(request_msg,
  1669. &op->extent.osd_data);
  1670. break;
  1671. case CEPH_OSD_OP_SETXATTR:
  1672. case CEPH_OSD_OP_CMPXATTR:
  1673. WARN_ON(op->indata_len != op->xattr.name_len +
  1674. op->xattr.value_len);
  1675. ceph_osdc_msg_data_add(request_msg,
  1676. &op->xattr.osd_data);
  1677. break;
  1678. case CEPH_OSD_OP_NOTIFY_ACK:
  1679. ceph_osdc_msg_data_add(request_msg,
  1680. &op->notify_ack.request_data);
  1681. break;
  1682. case CEPH_OSD_OP_COPY_FROM2:
  1683. ceph_osdc_msg_data_add(request_msg,
  1684. &op->copy_from.osd_data);
  1685. break;
  1686. /* reply */
  1687. case CEPH_OSD_OP_STAT:
  1688. ceph_osdc_msg_data_add(reply_msg,
  1689. &op->raw_data_in);
  1690. break;
  1691. case CEPH_OSD_OP_READ:
  1692. ceph_osdc_msg_data_add(reply_msg,
  1693. &op->extent.osd_data);
  1694. break;
  1695. case CEPH_OSD_OP_LIST_WATCHERS:
  1696. ceph_osdc_msg_data_add(reply_msg,
  1697. &op->list_watchers.response_data);
  1698. break;
  1699. /* both */
  1700. case CEPH_OSD_OP_CALL:
  1701. WARN_ON(op->indata_len != op->cls.class_len +
  1702. op->cls.method_len +
  1703. op->cls.indata_len);
  1704. ceph_osdc_msg_data_add(request_msg,
  1705. &op->cls.request_info);
  1706. /* optional, can be NONE */
  1707. ceph_osdc_msg_data_add(request_msg,
  1708. &op->cls.request_data);
  1709. /* optional, can be NONE */
  1710. ceph_osdc_msg_data_add(reply_msg,
  1711. &op->cls.response_data);
  1712. break;
  1713. case CEPH_OSD_OP_NOTIFY:
  1714. ceph_osdc_msg_data_add(request_msg,
  1715. &op->notify.request_data);
  1716. ceph_osdc_msg_data_add(reply_msg,
  1717. &op->notify.response_data);
  1718. break;
  1719. }
  1720. }
  1721. }
  1722. static void encode_pgid(void **p, const struct ceph_pg *pgid)
  1723. {
  1724. ceph_encode_8(p, 1);
  1725. ceph_encode_64(p, pgid->pool);
  1726. ceph_encode_32(p, pgid->seed);
  1727. ceph_encode_32(p, -1); /* preferred */
  1728. }
  1729. static void encode_spgid(void **p, const struct ceph_spg *spgid)
  1730. {
  1731. ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
  1732. encode_pgid(p, &spgid->pgid);
  1733. ceph_encode_8(p, spgid->shard);
  1734. }
  1735. static void encode_oloc(void **p, void *end,
  1736. const struct ceph_object_locator *oloc)
  1737. {
  1738. ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
  1739. ceph_encode_64(p, oloc->pool);
  1740. ceph_encode_32(p, -1); /* preferred */
  1741. ceph_encode_32(p, 0); /* key len */
  1742. if (oloc->pool_ns)
  1743. ceph_encode_string(p, end, oloc->pool_ns->str,
  1744. oloc->pool_ns->len);
  1745. else
  1746. ceph_encode_32(p, 0);
  1747. }
  1748. static void encode_request_partial(struct ceph_osd_request *req,
  1749. struct ceph_msg *msg)
  1750. {
  1751. void *p = msg->front.iov_base;
  1752. void *const end = p + msg->front_alloc_len;
  1753. u32 data_len = 0;
  1754. int i;
  1755. if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
  1756. /* snapshots aren't writeable */
  1757. WARN_ON(req->r_snapid != CEPH_NOSNAP);
  1758. } else {
  1759. WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
  1760. req->r_data_offset || req->r_snapc);
  1761. }
  1762. setup_request_data(req);
  1763. encode_spgid(&p, &req->r_t.spgid); /* actual spg */
  1764. ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
  1765. ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
  1766. ceph_encode_32(&p, req->r_flags);
  1767. /* reqid */
  1768. ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
  1769. memset(p, 0, sizeof(struct ceph_osd_reqid));
  1770. p += sizeof(struct ceph_osd_reqid);
  1771. /* trace */
  1772. memset(p, 0, sizeof(struct ceph_blkin_trace_info));
  1773. p += sizeof(struct ceph_blkin_trace_info);
  1774. ceph_encode_32(&p, 0); /* client_inc, always 0 */
  1775. ceph_encode_timespec64(p, &req->r_mtime);
  1776. p += sizeof(struct ceph_timespec);
  1777. encode_oloc(&p, end, &req->r_t.target_oloc);
  1778. ceph_encode_string(&p, end, req->r_t.target_oid.name,
  1779. req->r_t.target_oid.name_len);
  1780. /* ops, can imply data */
  1781. ceph_encode_16(&p, req->r_num_ops);
  1782. for (i = 0; i < req->r_num_ops; i++) {
  1783. data_len += osd_req_encode_op(p, &req->r_ops[i]);
  1784. p += sizeof(struct ceph_osd_op);
  1785. }
  1786. ceph_encode_64(&p, req->r_snapid); /* snapid */
  1787. if (req->r_snapc) {
  1788. ceph_encode_64(&p, req->r_snapc->seq);
  1789. ceph_encode_32(&p, req->r_snapc->num_snaps);
  1790. for (i = 0; i < req->r_snapc->num_snaps; i++)
  1791. ceph_encode_64(&p, req->r_snapc->snaps[i]);
  1792. } else {
  1793. ceph_encode_64(&p, 0); /* snap_seq */
  1794. ceph_encode_32(&p, 0); /* snaps len */
  1795. }
  1796. ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
  1797. BUG_ON(p > end - 8); /* space for features */
  1798. msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
  1799. /* front_len is finalized in encode_request_finish() */
  1800. msg->front.iov_len = p - msg->front.iov_base;
  1801. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1802. msg->hdr.data_len = cpu_to_le32(data_len);
  1803. /*
  1804. * The header "data_off" is a hint to the receiver allowing it
  1805. * to align received data into its buffers such that there's no
  1806. * need to re-copy it before writing it to disk (direct I/O).
  1807. */
  1808. msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
  1809. dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
  1810. req->r_t.target_oid.name, req->r_t.target_oid.name_len);
  1811. }
  1812. static void encode_request_finish(struct ceph_msg *msg)
  1813. {
  1814. void *p = msg->front.iov_base;
  1815. void *const partial_end = p + msg->front.iov_len;
  1816. void *const end = p + msg->front_alloc_len;
  1817. if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
  1818. /* luminous OSD -- encode features and be done */
  1819. p = partial_end;
  1820. ceph_encode_64(&p, msg->con->peer_features);
  1821. } else {
  1822. struct {
  1823. char spgid[CEPH_ENCODING_START_BLK_LEN +
  1824. CEPH_PGID_ENCODING_LEN + 1];
  1825. __le32 hash;
  1826. __le32 epoch;
  1827. __le32 flags;
  1828. char reqid[CEPH_ENCODING_START_BLK_LEN +
  1829. sizeof(struct ceph_osd_reqid)];
  1830. char trace[sizeof(struct ceph_blkin_trace_info)];
  1831. __le32 client_inc;
  1832. struct ceph_timespec mtime;
  1833. } __packed head;
  1834. struct ceph_pg pgid;
  1835. void *oloc, *oid, *tail;
  1836. int oloc_len, oid_len, tail_len;
  1837. int len;
  1838. /*
  1839. * Pre-luminous OSD -- reencode v8 into v4 using @head
  1840. * as a temporary buffer. Encode the raw PG; the rest
  1841. * is just a matter of moving oloc, oid and tail blobs
  1842. * around.
  1843. */
  1844. memcpy(&head, p, sizeof(head));
  1845. p += sizeof(head);
  1846. oloc = p;
  1847. p += CEPH_ENCODING_START_BLK_LEN;
  1848. pgid.pool = ceph_decode_64(&p);
  1849. p += 4 + 4; /* preferred, key len */
  1850. len = ceph_decode_32(&p);
  1851. p += len; /* nspace */
  1852. oloc_len = p - oloc;
  1853. oid = p;
  1854. len = ceph_decode_32(&p);
  1855. p += len;
  1856. oid_len = p - oid;
  1857. tail = p;
  1858. tail_len = partial_end - p;
  1859. p = msg->front.iov_base;
  1860. ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
  1861. ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
  1862. ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
  1863. ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
  1864. /* reassert_version */
  1865. memset(p, 0, sizeof(struct ceph_eversion));
  1866. p += sizeof(struct ceph_eversion);
  1867. BUG_ON(p >= oloc);
  1868. memmove(p, oloc, oloc_len);
  1869. p += oloc_len;
  1870. pgid.seed = le32_to_cpu(head.hash);
  1871. encode_pgid(&p, &pgid); /* raw pg */
  1872. BUG_ON(p >= oid);
  1873. memmove(p, oid, oid_len);
  1874. p += oid_len;
  1875. /* tail -- ops, snapid, snapc, retry_attempt */
  1876. BUG_ON(p >= tail);
  1877. memmove(p, tail, tail_len);
  1878. p += tail_len;
  1879. msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
  1880. }
  1881. BUG_ON(p > end);
  1882. msg->front.iov_len = p - msg->front.iov_base;
  1883. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1884. dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
  1885. le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
  1886. le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
  1887. le16_to_cpu(msg->hdr.version));
  1888. }
  1889. /*
  1890. * @req has to be assigned a tid and registered.
  1891. */
  1892. static void send_request(struct ceph_osd_request *req)
  1893. {
  1894. struct ceph_osd *osd = req->r_osd;
  1895. verify_osd_locked(osd);
  1896. WARN_ON(osd->o_osd != req->r_t.osd);
  1897. /* backoff? */
  1898. if (should_plug_request(req))
  1899. return;
  1900. /*
  1901. * We may have a previously queued request message hanging
  1902. * around. Cancel it to avoid corrupting the msgr.
  1903. */
  1904. if (req->r_sent)
  1905. ceph_msg_revoke(req->r_request);
  1906. req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
  1907. if (req->r_attempts)
  1908. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1909. else
  1910. WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
  1911. encode_request_partial(req, req->r_request);
  1912. dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
  1913. __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
  1914. req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
  1915. req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
  1916. req->r_attempts);
  1917. req->r_t.paused = false;
  1918. req->r_stamp = jiffies;
  1919. req->r_attempts++;
  1920. req->r_sent = osd->o_incarnation;
  1921. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  1922. ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
  1923. }
  1924. static void maybe_request_map(struct ceph_osd_client *osdc)
  1925. {
  1926. bool continuous = false;
  1927. verify_osdc_locked(osdc);
  1928. WARN_ON(!osdc->osdmap->epoch);
  1929. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  1930. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
  1931. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
  1932. dout("%s osdc %p continuous\n", __func__, osdc);
  1933. continuous = true;
  1934. } else {
  1935. dout("%s osdc %p onetime\n", __func__, osdc);
  1936. }
  1937. if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
  1938. osdc->osdmap->epoch + 1, continuous))
  1939. ceph_monc_renew_subs(&osdc->client->monc);
  1940. }
  1941. static void complete_request(struct ceph_osd_request *req, int err);
  1942. static void send_map_check(struct ceph_osd_request *req);
  1943. static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
  1944. {
  1945. struct ceph_osd_client *osdc = req->r_osdc;
  1946. struct ceph_osd *osd;
  1947. enum calc_target_result ct_res;
  1948. int err = 0;
  1949. bool need_send = false;
  1950. bool promoted = false;
  1951. WARN_ON(req->r_tid);
  1952. dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
  1953. again:
  1954. ct_res = calc_target(osdc, &req->r_t, false);
  1955. if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
  1956. goto promote;
  1957. osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
  1958. if (IS_ERR(osd)) {
  1959. WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
  1960. goto promote;
  1961. }
  1962. if (osdc->abort_err) {
  1963. dout("req %p abort_err %d\n", req, osdc->abort_err);
  1964. err = osdc->abort_err;
  1965. } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
  1966. dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
  1967. osdc->epoch_barrier);
  1968. req->r_t.paused = true;
  1969. maybe_request_map(osdc);
  1970. } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  1971. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
  1972. dout("req %p pausewr\n", req);
  1973. req->r_t.paused = true;
  1974. maybe_request_map(osdc);
  1975. } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
  1976. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
  1977. dout("req %p pauserd\n", req);
  1978. req->r_t.paused = true;
  1979. maybe_request_map(osdc);
  1980. } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  1981. !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
  1982. CEPH_OSD_FLAG_FULL_FORCE)) &&
  1983. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  1984. pool_full(osdc, req->r_t.base_oloc.pool))) {
  1985. dout("req %p full/pool_full\n", req);
  1986. if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
  1987. err = -ENOSPC;
  1988. } else {
  1989. pr_warn_ratelimited("FULL or reached pool quota\n");
  1990. req->r_t.paused = true;
  1991. maybe_request_map(osdc);
  1992. }
  1993. } else if (!osd_homeless(osd)) {
  1994. need_send = true;
  1995. } else {
  1996. maybe_request_map(osdc);
  1997. }
  1998. mutex_lock(&osd->lock);
  1999. /*
  2000. * Assign the tid atomically with send_request() to protect
  2001. * multiple writes to the same object from racing with each
  2002. * other, resulting in out of order ops on the OSDs.
  2003. */
  2004. req->r_tid = atomic64_inc_return(&osdc->last_tid);
  2005. link_request(osd, req);
  2006. if (need_send)
  2007. send_request(req);
  2008. else if (err)
  2009. complete_request(req, err);
  2010. mutex_unlock(&osd->lock);
  2011. if (!err && ct_res == CALC_TARGET_POOL_DNE)
  2012. send_map_check(req);
  2013. if (promoted)
  2014. downgrade_write(&osdc->lock);
  2015. return;
  2016. promote:
  2017. up_read(&osdc->lock);
  2018. down_write(&osdc->lock);
  2019. wrlocked = true;
  2020. promoted = true;
  2021. goto again;
  2022. }
  2023. static void account_request(struct ceph_osd_request *req)
  2024. {
  2025. WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
  2026. WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
  2027. req->r_flags |= CEPH_OSD_FLAG_ONDISK;
  2028. atomic_inc(&req->r_osdc->num_requests);
  2029. req->r_start_stamp = jiffies;
  2030. }
  2031. static void submit_request(struct ceph_osd_request *req, bool wrlocked)
  2032. {
  2033. ceph_osdc_get_request(req);
  2034. account_request(req);
  2035. __submit_request(req, wrlocked);
  2036. }
  2037. static void finish_request(struct ceph_osd_request *req)
  2038. {
  2039. struct ceph_osd_client *osdc = req->r_osdc;
  2040. WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
  2041. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  2042. if (req->r_osd)
  2043. unlink_request(req->r_osd, req);
  2044. atomic_dec(&osdc->num_requests);
  2045. /*
  2046. * If an OSD has failed or returned and a request has been sent
  2047. * twice, it's possible to get a reply and end up here while the
  2048. * request message is queued for delivery. We will ignore the
  2049. * reply, so not a big deal, but better to try and catch it.
  2050. */
  2051. ceph_msg_revoke(req->r_request);
  2052. ceph_msg_revoke_incoming(req->r_reply);
  2053. }
  2054. static void __complete_request(struct ceph_osd_request *req)
  2055. {
  2056. dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
  2057. req->r_tid, req->r_callback, req->r_result);
  2058. if (req->r_callback)
  2059. req->r_callback(req);
  2060. complete_all(&req->r_completion);
  2061. ceph_osdc_put_request(req);
  2062. }
  2063. static void complete_request_workfn(struct work_struct *work)
  2064. {
  2065. struct ceph_osd_request *req =
  2066. container_of(work, struct ceph_osd_request, r_complete_work);
  2067. __complete_request(req);
  2068. }
  2069. /*
  2070. * This is open-coded in handle_reply().
  2071. */
  2072. static void complete_request(struct ceph_osd_request *req, int err)
  2073. {
  2074. dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
  2075. req->r_result = err;
  2076. finish_request(req);
  2077. INIT_WORK(&req->r_complete_work, complete_request_workfn);
  2078. queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
  2079. }
  2080. static void cancel_map_check(struct ceph_osd_request *req)
  2081. {
  2082. struct ceph_osd_client *osdc = req->r_osdc;
  2083. struct ceph_osd_request *lookup_req;
  2084. verify_osdc_wrlocked(osdc);
  2085. lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
  2086. if (!lookup_req)
  2087. return;
  2088. WARN_ON(lookup_req != req);
  2089. erase_request_mc(&osdc->map_checks, req);
  2090. ceph_osdc_put_request(req);
  2091. }
  2092. static void cancel_request(struct ceph_osd_request *req)
  2093. {
  2094. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  2095. cancel_map_check(req);
  2096. finish_request(req);
  2097. complete_all(&req->r_completion);
  2098. ceph_osdc_put_request(req);
  2099. }
  2100. static void abort_request(struct ceph_osd_request *req, int err)
  2101. {
  2102. dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
  2103. cancel_map_check(req);
  2104. complete_request(req, err);
  2105. }
  2106. static int abort_fn(struct ceph_osd_request *req, void *arg)
  2107. {
  2108. int err = *(int *)arg;
  2109. abort_request(req, err);
  2110. return 0; /* continue iteration */
  2111. }
  2112. /*
  2113. * Abort all in-flight requests with @err and arrange for all future
  2114. * requests to be failed immediately.
  2115. */
  2116. void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
  2117. {
  2118. dout("%s osdc %p err %d\n", __func__, osdc, err);
  2119. down_write(&osdc->lock);
  2120. for_each_request(osdc, abort_fn, &err);
  2121. osdc->abort_err = err;
  2122. up_write(&osdc->lock);
  2123. }
  2124. EXPORT_SYMBOL(ceph_osdc_abort_requests);
  2125. void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
  2126. {
  2127. down_write(&osdc->lock);
  2128. osdc->abort_err = 0;
  2129. up_write(&osdc->lock);
  2130. }
  2131. EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
  2132. static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
  2133. {
  2134. if (likely(eb > osdc->epoch_barrier)) {
  2135. dout("updating epoch_barrier from %u to %u\n",
  2136. osdc->epoch_barrier, eb);
  2137. osdc->epoch_barrier = eb;
  2138. /* Request map if we're not to the barrier yet */
  2139. if (eb > osdc->osdmap->epoch)
  2140. maybe_request_map(osdc);
  2141. }
  2142. }
  2143. void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
  2144. {
  2145. down_read(&osdc->lock);
  2146. if (unlikely(eb > osdc->epoch_barrier)) {
  2147. up_read(&osdc->lock);
  2148. down_write(&osdc->lock);
  2149. update_epoch_barrier(osdc, eb);
  2150. up_write(&osdc->lock);
  2151. } else {
  2152. up_read(&osdc->lock);
  2153. }
  2154. }
  2155. EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
  2156. /*
  2157. * We can end up releasing caps as a result of abort_request().
  2158. * In that case, we probably want to ensure that the cap release message
  2159. * has an updated epoch barrier in it, so set the epoch barrier prior to
  2160. * aborting the first request.
  2161. */
  2162. static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
  2163. {
  2164. struct ceph_osd_client *osdc = req->r_osdc;
  2165. bool *victims = arg;
  2166. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  2167. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  2168. pool_full(osdc, req->r_t.base_oloc.pool))) {
  2169. if (!*victims) {
  2170. update_epoch_barrier(osdc, osdc->osdmap->epoch);
  2171. *victims = true;
  2172. }
  2173. abort_request(req, -ENOSPC);
  2174. }
  2175. return 0; /* continue iteration */
  2176. }
  2177. /*
  2178. * Drop all pending requests that are stalled waiting on a full condition to
  2179. * clear, and complete them with ENOSPC as the return code. Set the
  2180. * osdc->epoch_barrier to the latest map epoch that we've seen if any were
  2181. * cancelled.
  2182. */
  2183. static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
  2184. {
  2185. bool victims = false;
  2186. if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
  2187. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
  2188. for_each_request(osdc, abort_on_full_fn, &victims);
  2189. }
  2190. static void check_pool_dne(struct ceph_osd_request *req)
  2191. {
  2192. struct ceph_osd_client *osdc = req->r_osdc;
  2193. struct ceph_osdmap *map = osdc->osdmap;
  2194. verify_osdc_wrlocked(osdc);
  2195. WARN_ON(!map->epoch);
  2196. if (req->r_attempts) {
  2197. /*
  2198. * We sent a request earlier, which means that
  2199. * previously the pool existed, and now it does not
  2200. * (i.e., it was deleted).
  2201. */
  2202. req->r_map_dne_bound = map->epoch;
  2203. dout("%s req %p tid %llu pool disappeared\n", __func__, req,
  2204. req->r_tid);
  2205. } else {
  2206. dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
  2207. req, req->r_tid, req->r_map_dne_bound, map->epoch);
  2208. }
  2209. if (req->r_map_dne_bound) {
  2210. if (map->epoch >= req->r_map_dne_bound) {
  2211. /* we had a new enough map */
  2212. pr_info_ratelimited("tid %llu pool does not exist\n",
  2213. req->r_tid);
  2214. complete_request(req, -ENOENT);
  2215. }
  2216. } else {
  2217. send_map_check(req);
  2218. }
  2219. }
  2220. static void map_check_cb(struct ceph_mon_generic_request *greq)
  2221. {
  2222. struct ceph_osd_client *osdc = &greq->monc->client->osdc;
  2223. struct ceph_osd_request *req;
  2224. u64 tid = greq->private_data;
  2225. WARN_ON(greq->result || !greq->u.newest);
  2226. down_write(&osdc->lock);
  2227. req = lookup_request_mc(&osdc->map_checks, tid);
  2228. if (!req) {
  2229. dout("%s tid %llu dne\n", __func__, tid);
  2230. goto out_unlock;
  2231. }
  2232. dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
  2233. req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
  2234. if (!req->r_map_dne_bound)
  2235. req->r_map_dne_bound = greq->u.newest;
  2236. erase_request_mc(&osdc->map_checks, req);
  2237. check_pool_dne(req);
  2238. ceph_osdc_put_request(req);
  2239. out_unlock:
  2240. up_write(&osdc->lock);
  2241. }
  2242. static void send_map_check(struct ceph_osd_request *req)
  2243. {
  2244. struct ceph_osd_client *osdc = req->r_osdc;
  2245. struct ceph_osd_request *lookup_req;
  2246. int ret;
  2247. verify_osdc_wrlocked(osdc);
  2248. lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
  2249. if (lookup_req) {
  2250. WARN_ON(lookup_req != req);
  2251. return;
  2252. }
  2253. ceph_osdc_get_request(req);
  2254. insert_request_mc(&osdc->map_checks, req);
  2255. ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
  2256. map_check_cb, req->r_tid);
  2257. WARN_ON(ret);
  2258. }
  2259. /*
  2260. * lingering requests, watch/notify v2 infrastructure
  2261. */
  2262. static void linger_release(struct kref *kref)
  2263. {
  2264. struct ceph_osd_linger_request *lreq =
  2265. container_of(kref, struct ceph_osd_linger_request, kref);
  2266. dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
  2267. lreq->reg_req, lreq->ping_req);
  2268. WARN_ON(!RB_EMPTY_NODE(&lreq->node));
  2269. WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
  2270. WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
  2271. WARN_ON(!list_empty(&lreq->scan_item));
  2272. WARN_ON(!list_empty(&lreq->pending_lworks));
  2273. WARN_ON(lreq->osd);
  2274. if (lreq->reg_req)
  2275. ceph_osdc_put_request(lreq->reg_req);
  2276. if (lreq->ping_req)
  2277. ceph_osdc_put_request(lreq->ping_req);
  2278. target_destroy(&lreq->t);
  2279. kfree(lreq);
  2280. }
  2281. static void linger_put(struct ceph_osd_linger_request *lreq)
  2282. {
  2283. if (lreq)
  2284. kref_put(&lreq->kref, linger_release);
  2285. }
  2286. static struct ceph_osd_linger_request *
  2287. linger_get(struct ceph_osd_linger_request *lreq)
  2288. {
  2289. kref_get(&lreq->kref);
  2290. return lreq;
  2291. }
  2292. static struct ceph_osd_linger_request *
  2293. linger_alloc(struct ceph_osd_client *osdc)
  2294. {
  2295. struct ceph_osd_linger_request *lreq;
  2296. lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
  2297. if (!lreq)
  2298. return NULL;
  2299. kref_init(&lreq->kref);
  2300. mutex_init(&lreq->lock);
  2301. RB_CLEAR_NODE(&lreq->node);
  2302. RB_CLEAR_NODE(&lreq->osdc_node);
  2303. RB_CLEAR_NODE(&lreq->mc_node);
  2304. INIT_LIST_HEAD(&lreq->scan_item);
  2305. INIT_LIST_HEAD(&lreq->pending_lworks);
  2306. init_completion(&lreq->reg_commit_wait);
  2307. init_completion(&lreq->notify_finish_wait);
  2308. lreq->osdc = osdc;
  2309. target_init(&lreq->t);
  2310. dout("%s lreq %p\n", __func__, lreq);
  2311. return lreq;
  2312. }
  2313. DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
  2314. DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
  2315. DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
  2316. /*
  2317. * Create linger request <-> OSD session relation.
  2318. *
  2319. * @lreq has to be registered, @osd may be homeless.
  2320. */
  2321. static void link_linger(struct ceph_osd *osd,
  2322. struct ceph_osd_linger_request *lreq)
  2323. {
  2324. verify_osd_locked(osd);
  2325. WARN_ON(!lreq->linger_id || lreq->osd);
  2326. dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
  2327. osd->o_osd, lreq, lreq->linger_id);
  2328. if (!osd_homeless(osd))
  2329. __remove_osd_from_lru(osd);
  2330. else
  2331. atomic_inc(&osd->o_osdc->num_homeless);
  2332. get_osd(osd);
  2333. insert_linger(&osd->o_linger_requests, lreq);
  2334. lreq->osd = osd;
  2335. }
  2336. static void unlink_linger(struct ceph_osd *osd,
  2337. struct ceph_osd_linger_request *lreq)
  2338. {
  2339. verify_osd_locked(osd);
  2340. WARN_ON(lreq->osd != osd);
  2341. dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
  2342. osd->o_osd, lreq, lreq->linger_id);
  2343. lreq->osd = NULL;
  2344. erase_linger(&osd->o_linger_requests, lreq);
  2345. put_osd(osd);
  2346. if (!osd_homeless(osd))
  2347. maybe_move_osd_to_lru(osd);
  2348. else
  2349. atomic_dec(&osd->o_osdc->num_homeless);
  2350. }
  2351. static bool __linger_registered(struct ceph_osd_linger_request *lreq)
  2352. {
  2353. verify_osdc_locked(lreq->osdc);
  2354. return !RB_EMPTY_NODE(&lreq->osdc_node);
  2355. }
  2356. static bool linger_registered(struct ceph_osd_linger_request *lreq)
  2357. {
  2358. struct ceph_osd_client *osdc = lreq->osdc;
  2359. bool registered;
  2360. down_read(&osdc->lock);
  2361. registered = __linger_registered(lreq);
  2362. up_read(&osdc->lock);
  2363. return registered;
  2364. }
  2365. static void linger_register(struct ceph_osd_linger_request *lreq)
  2366. {
  2367. struct ceph_osd_client *osdc = lreq->osdc;
  2368. verify_osdc_wrlocked(osdc);
  2369. WARN_ON(lreq->linger_id);
  2370. linger_get(lreq);
  2371. lreq->linger_id = ++osdc->last_linger_id;
  2372. insert_linger_osdc(&osdc->linger_requests, lreq);
  2373. }
  2374. static void linger_unregister(struct ceph_osd_linger_request *lreq)
  2375. {
  2376. struct ceph_osd_client *osdc = lreq->osdc;
  2377. verify_osdc_wrlocked(osdc);
  2378. erase_linger_osdc(&osdc->linger_requests, lreq);
  2379. linger_put(lreq);
  2380. }
  2381. static void cancel_linger_request(struct ceph_osd_request *req)
  2382. {
  2383. struct ceph_osd_linger_request *lreq = req->r_priv;
  2384. WARN_ON(!req->r_linger);
  2385. cancel_request(req);
  2386. linger_put(lreq);
  2387. }
  2388. struct linger_work {
  2389. struct work_struct work;
  2390. struct ceph_osd_linger_request *lreq;
  2391. struct list_head pending_item;
  2392. unsigned long queued_stamp;
  2393. union {
  2394. struct {
  2395. u64 notify_id;
  2396. u64 notifier_id;
  2397. void *payload; /* points into @msg front */
  2398. size_t payload_len;
  2399. struct ceph_msg *msg; /* for ceph_msg_put() */
  2400. } notify;
  2401. struct {
  2402. int err;
  2403. } error;
  2404. };
  2405. };
  2406. static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
  2407. work_func_t workfn)
  2408. {
  2409. struct linger_work *lwork;
  2410. lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
  2411. if (!lwork)
  2412. return NULL;
  2413. INIT_WORK(&lwork->work, workfn);
  2414. INIT_LIST_HEAD(&lwork->pending_item);
  2415. lwork->lreq = linger_get(lreq);
  2416. return lwork;
  2417. }
  2418. static void lwork_free(struct linger_work *lwork)
  2419. {
  2420. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2421. mutex_lock(&lreq->lock);
  2422. list_del(&lwork->pending_item);
  2423. mutex_unlock(&lreq->lock);
  2424. linger_put(lreq);
  2425. kfree(lwork);
  2426. }
  2427. static void lwork_queue(struct linger_work *lwork)
  2428. {
  2429. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2430. struct ceph_osd_client *osdc = lreq->osdc;
  2431. verify_lreq_locked(lreq);
  2432. WARN_ON(!list_empty(&lwork->pending_item));
  2433. lwork->queued_stamp = jiffies;
  2434. list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
  2435. queue_work(osdc->notify_wq, &lwork->work);
  2436. }
  2437. static void do_watch_notify(struct work_struct *w)
  2438. {
  2439. struct linger_work *lwork = container_of(w, struct linger_work, work);
  2440. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2441. if (!linger_registered(lreq)) {
  2442. dout("%s lreq %p not registered\n", __func__, lreq);
  2443. goto out;
  2444. }
  2445. WARN_ON(!lreq->is_watch);
  2446. dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
  2447. __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
  2448. lwork->notify.payload_len);
  2449. lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
  2450. lwork->notify.notifier_id, lwork->notify.payload,
  2451. lwork->notify.payload_len);
  2452. out:
  2453. ceph_msg_put(lwork->notify.msg);
  2454. lwork_free(lwork);
  2455. }
  2456. static void do_watch_error(struct work_struct *w)
  2457. {
  2458. struct linger_work *lwork = container_of(w, struct linger_work, work);
  2459. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2460. if (!linger_registered(lreq)) {
  2461. dout("%s lreq %p not registered\n", __func__, lreq);
  2462. goto out;
  2463. }
  2464. dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
  2465. lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
  2466. out:
  2467. lwork_free(lwork);
  2468. }
  2469. static void queue_watch_error(struct ceph_osd_linger_request *lreq)
  2470. {
  2471. struct linger_work *lwork;
  2472. lwork = lwork_alloc(lreq, do_watch_error);
  2473. if (!lwork) {
  2474. pr_err("failed to allocate error-lwork\n");
  2475. return;
  2476. }
  2477. lwork->error.err = lreq->last_error;
  2478. lwork_queue(lwork);
  2479. }
  2480. static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
  2481. int result)
  2482. {
  2483. if (!completion_done(&lreq->reg_commit_wait)) {
  2484. lreq->reg_commit_error = (result <= 0 ? result : 0);
  2485. complete_all(&lreq->reg_commit_wait);
  2486. }
  2487. }
  2488. static void linger_commit_cb(struct ceph_osd_request *req)
  2489. {
  2490. struct ceph_osd_linger_request *lreq = req->r_priv;
  2491. mutex_lock(&lreq->lock);
  2492. dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
  2493. lreq->linger_id, req->r_result);
  2494. linger_reg_commit_complete(lreq, req->r_result);
  2495. lreq->committed = true;
  2496. if (!lreq->is_watch) {
  2497. struct ceph_osd_data *osd_data =
  2498. osd_req_op_data(req, 0, notify, response_data);
  2499. void *p = page_address(osd_data->pages[0]);
  2500. WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
  2501. osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
  2502. /* make note of the notify_id */
  2503. if (req->r_ops[0].outdata_len >= sizeof(u64)) {
  2504. lreq->notify_id = ceph_decode_64(&p);
  2505. dout("lreq %p notify_id %llu\n", lreq,
  2506. lreq->notify_id);
  2507. } else {
  2508. dout("lreq %p no notify_id\n", lreq);
  2509. }
  2510. }
  2511. mutex_unlock(&lreq->lock);
  2512. linger_put(lreq);
  2513. }
  2514. static int normalize_watch_error(int err)
  2515. {
  2516. /*
  2517. * Translate ENOENT -> ENOTCONN so that a delete->disconnection
  2518. * notification and a failure to reconnect because we raced with
  2519. * the delete appear the same to the user.
  2520. */
  2521. if (err == -ENOENT)
  2522. err = -ENOTCONN;
  2523. return err;
  2524. }
  2525. static void linger_reconnect_cb(struct ceph_osd_request *req)
  2526. {
  2527. struct ceph_osd_linger_request *lreq = req->r_priv;
  2528. mutex_lock(&lreq->lock);
  2529. dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
  2530. lreq, lreq->linger_id, req->r_result, lreq->last_error);
  2531. if (req->r_result < 0) {
  2532. if (!lreq->last_error) {
  2533. lreq->last_error = normalize_watch_error(req->r_result);
  2534. queue_watch_error(lreq);
  2535. }
  2536. }
  2537. mutex_unlock(&lreq->lock);
  2538. linger_put(lreq);
  2539. }
  2540. static void send_linger(struct ceph_osd_linger_request *lreq)
  2541. {
  2542. struct ceph_osd_request *req = lreq->reg_req;
  2543. struct ceph_osd_req_op *op = &req->r_ops[0];
  2544. verify_osdc_wrlocked(req->r_osdc);
  2545. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2546. if (req->r_osd)
  2547. cancel_linger_request(req);
  2548. request_reinit(req);
  2549. ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
  2550. ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
  2551. req->r_flags = lreq->t.flags;
  2552. req->r_mtime = lreq->mtime;
  2553. mutex_lock(&lreq->lock);
  2554. if (lreq->is_watch && lreq->committed) {
  2555. WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
  2556. op->watch.cookie != lreq->linger_id);
  2557. op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
  2558. op->watch.gen = ++lreq->register_gen;
  2559. dout("lreq %p reconnect register_gen %u\n", lreq,
  2560. op->watch.gen);
  2561. req->r_callback = linger_reconnect_cb;
  2562. } else {
  2563. if (!lreq->is_watch)
  2564. lreq->notify_id = 0;
  2565. else
  2566. WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
  2567. dout("lreq %p register\n", lreq);
  2568. req->r_callback = linger_commit_cb;
  2569. }
  2570. mutex_unlock(&lreq->lock);
  2571. req->r_priv = linger_get(lreq);
  2572. req->r_linger = true;
  2573. submit_request(req, true);
  2574. }
  2575. static void linger_ping_cb(struct ceph_osd_request *req)
  2576. {
  2577. struct ceph_osd_linger_request *lreq = req->r_priv;
  2578. mutex_lock(&lreq->lock);
  2579. dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
  2580. __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
  2581. lreq->last_error);
  2582. if (lreq->register_gen == req->r_ops[0].watch.gen) {
  2583. if (!req->r_result) {
  2584. lreq->watch_valid_thru = lreq->ping_sent;
  2585. } else if (!lreq->last_error) {
  2586. lreq->last_error = normalize_watch_error(req->r_result);
  2587. queue_watch_error(lreq);
  2588. }
  2589. } else {
  2590. dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
  2591. lreq->register_gen, req->r_ops[0].watch.gen);
  2592. }
  2593. mutex_unlock(&lreq->lock);
  2594. linger_put(lreq);
  2595. }
  2596. static void send_linger_ping(struct ceph_osd_linger_request *lreq)
  2597. {
  2598. struct ceph_osd_client *osdc = lreq->osdc;
  2599. struct ceph_osd_request *req = lreq->ping_req;
  2600. struct ceph_osd_req_op *op = &req->r_ops[0];
  2601. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
  2602. dout("%s PAUSERD\n", __func__);
  2603. return;
  2604. }
  2605. lreq->ping_sent = jiffies;
  2606. dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
  2607. __func__, lreq, lreq->linger_id, lreq->ping_sent,
  2608. lreq->register_gen);
  2609. if (req->r_osd)
  2610. cancel_linger_request(req);
  2611. request_reinit(req);
  2612. target_copy(&req->r_t, &lreq->t);
  2613. WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
  2614. op->watch.cookie != lreq->linger_id ||
  2615. op->watch.op != CEPH_OSD_WATCH_OP_PING);
  2616. op->watch.gen = lreq->register_gen;
  2617. req->r_callback = linger_ping_cb;
  2618. req->r_priv = linger_get(lreq);
  2619. req->r_linger = true;
  2620. ceph_osdc_get_request(req);
  2621. account_request(req);
  2622. req->r_tid = atomic64_inc_return(&osdc->last_tid);
  2623. link_request(lreq->osd, req);
  2624. send_request(req);
  2625. }
  2626. static void linger_submit(struct ceph_osd_linger_request *lreq)
  2627. {
  2628. struct ceph_osd_client *osdc = lreq->osdc;
  2629. struct ceph_osd *osd;
  2630. down_write(&osdc->lock);
  2631. linger_register(lreq);
  2632. if (lreq->is_watch) {
  2633. lreq->reg_req->r_ops[0].watch.cookie = lreq->linger_id;
  2634. lreq->ping_req->r_ops[0].watch.cookie = lreq->linger_id;
  2635. } else {
  2636. lreq->reg_req->r_ops[0].notify.cookie = lreq->linger_id;
  2637. }
  2638. calc_target(osdc, &lreq->t, false);
  2639. osd = lookup_create_osd(osdc, lreq->t.osd, true);
  2640. link_linger(osd, lreq);
  2641. send_linger(lreq);
  2642. up_write(&osdc->lock);
  2643. }
  2644. static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
  2645. {
  2646. struct ceph_osd_client *osdc = lreq->osdc;
  2647. struct ceph_osd_linger_request *lookup_lreq;
  2648. verify_osdc_wrlocked(osdc);
  2649. lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
  2650. lreq->linger_id);
  2651. if (!lookup_lreq)
  2652. return;
  2653. WARN_ON(lookup_lreq != lreq);
  2654. erase_linger_mc(&osdc->linger_map_checks, lreq);
  2655. linger_put(lreq);
  2656. }
  2657. /*
  2658. * @lreq has to be both registered and linked.
  2659. */
  2660. static void __linger_cancel(struct ceph_osd_linger_request *lreq)
  2661. {
  2662. if (lreq->is_watch && lreq->ping_req->r_osd)
  2663. cancel_linger_request(lreq->ping_req);
  2664. if (lreq->reg_req->r_osd)
  2665. cancel_linger_request(lreq->reg_req);
  2666. cancel_linger_map_check(lreq);
  2667. unlink_linger(lreq->osd, lreq);
  2668. linger_unregister(lreq);
  2669. }
  2670. static void linger_cancel(struct ceph_osd_linger_request *lreq)
  2671. {
  2672. struct ceph_osd_client *osdc = lreq->osdc;
  2673. down_write(&osdc->lock);
  2674. if (__linger_registered(lreq))
  2675. __linger_cancel(lreq);
  2676. up_write(&osdc->lock);
  2677. }
  2678. static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
  2679. static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
  2680. {
  2681. struct ceph_osd_client *osdc = lreq->osdc;
  2682. struct ceph_osdmap *map = osdc->osdmap;
  2683. verify_osdc_wrlocked(osdc);
  2684. WARN_ON(!map->epoch);
  2685. if (lreq->register_gen) {
  2686. lreq->map_dne_bound = map->epoch;
  2687. dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
  2688. lreq, lreq->linger_id);
  2689. } else {
  2690. dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
  2691. __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
  2692. map->epoch);
  2693. }
  2694. if (lreq->map_dne_bound) {
  2695. if (map->epoch >= lreq->map_dne_bound) {
  2696. /* we had a new enough map */
  2697. pr_info("linger_id %llu pool does not exist\n",
  2698. lreq->linger_id);
  2699. linger_reg_commit_complete(lreq, -ENOENT);
  2700. __linger_cancel(lreq);
  2701. }
  2702. } else {
  2703. send_linger_map_check(lreq);
  2704. }
  2705. }
  2706. static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
  2707. {
  2708. struct ceph_osd_client *osdc = &greq->monc->client->osdc;
  2709. struct ceph_osd_linger_request *lreq;
  2710. u64 linger_id = greq->private_data;
  2711. WARN_ON(greq->result || !greq->u.newest);
  2712. down_write(&osdc->lock);
  2713. lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
  2714. if (!lreq) {
  2715. dout("%s linger_id %llu dne\n", __func__, linger_id);
  2716. goto out_unlock;
  2717. }
  2718. dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
  2719. __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
  2720. greq->u.newest);
  2721. if (!lreq->map_dne_bound)
  2722. lreq->map_dne_bound = greq->u.newest;
  2723. erase_linger_mc(&osdc->linger_map_checks, lreq);
  2724. check_linger_pool_dne(lreq);
  2725. linger_put(lreq);
  2726. out_unlock:
  2727. up_write(&osdc->lock);
  2728. }
  2729. static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
  2730. {
  2731. struct ceph_osd_client *osdc = lreq->osdc;
  2732. struct ceph_osd_linger_request *lookup_lreq;
  2733. int ret;
  2734. verify_osdc_wrlocked(osdc);
  2735. lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
  2736. lreq->linger_id);
  2737. if (lookup_lreq) {
  2738. WARN_ON(lookup_lreq != lreq);
  2739. return;
  2740. }
  2741. linger_get(lreq);
  2742. insert_linger_mc(&osdc->linger_map_checks, lreq);
  2743. ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
  2744. linger_map_check_cb, lreq->linger_id);
  2745. WARN_ON(ret);
  2746. }
  2747. static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
  2748. {
  2749. int ret;
  2750. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2751. ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
  2752. return ret ?: lreq->reg_commit_error;
  2753. }
  2754. static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
  2755. {
  2756. int ret;
  2757. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2758. ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
  2759. return ret ?: lreq->notify_finish_error;
  2760. }
  2761. /*
  2762. * Timeout callback, called every N seconds. When 1 or more OSD
  2763. * requests has been active for more than N seconds, we send a keepalive
  2764. * (tag + timestamp) to its OSD to ensure any communications channel
  2765. * reset is detected.
  2766. */
  2767. static void handle_timeout(struct work_struct *work)
  2768. {
  2769. struct ceph_osd_client *osdc =
  2770. container_of(work, struct ceph_osd_client, timeout_work.work);
  2771. struct ceph_options *opts = osdc->client->options;
  2772. unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
  2773. unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
  2774. LIST_HEAD(slow_osds);
  2775. struct rb_node *n, *p;
  2776. dout("%s osdc %p\n", __func__, osdc);
  2777. down_write(&osdc->lock);
  2778. /*
  2779. * ping osds that are a bit slow. this ensures that if there
  2780. * is a break in the TCP connection we will notice, and reopen
  2781. * a connection with that osd (from the fault callback).
  2782. */
  2783. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  2784. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  2785. bool found = false;
  2786. for (p = rb_first(&osd->o_requests); p; ) {
  2787. struct ceph_osd_request *req =
  2788. rb_entry(p, struct ceph_osd_request, r_node);
  2789. p = rb_next(p); /* abort_request() */
  2790. if (time_before(req->r_stamp, cutoff)) {
  2791. dout(" req %p tid %llu on osd%d is laggy\n",
  2792. req, req->r_tid, osd->o_osd);
  2793. found = true;
  2794. }
  2795. if (opts->osd_request_timeout &&
  2796. time_before(req->r_start_stamp, expiry_cutoff)) {
  2797. pr_err_ratelimited("tid %llu on osd%d timeout\n",
  2798. req->r_tid, osd->o_osd);
  2799. abort_request(req, -ETIMEDOUT);
  2800. }
  2801. }
  2802. for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
  2803. struct ceph_osd_linger_request *lreq =
  2804. rb_entry(p, struct ceph_osd_linger_request, node);
  2805. dout(" lreq %p linger_id %llu is served by osd%d\n",
  2806. lreq, lreq->linger_id, osd->o_osd);
  2807. found = true;
  2808. mutex_lock(&lreq->lock);
  2809. if (lreq->is_watch && lreq->committed && !lreq->last_error)
  2810. send_linger_ping(lreq);
  2811. mutex_unlock(&lreq->lock);
  2812. }
  2813. if (found)
  2814. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  2815. }
  2816. if (opts->osd_request_timeout) {
  2817. for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
  2818. struct ceph_osd_request *req =
  2819. rb_entry(p, struct ceph_osd_request, r_node);
  2820. p = rb_next(p); /* abort_request() */
  2821. if (time_before(req->r_start_stamp, expiry_cutoff)) {
  2822. pr_err_ratelimited("tid %llu on osd%d timeout\n",
  2823. req->r_tid, osdc->homeless_osd.o_osd);
  2824. abort_request(req, -ETIMEDOUT);
  2825. }
  2826. }
  2827. }
  2828. if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
  2829. maybe_request_map(osdc);
  2830. while (!list_empty(&slow_osds)) {
  2831. struct ceph_osd *osd = list_first_entry(&slow_osds,
  2832. struct ceph_osd,
  2833. o_keepalive_item);
  2834. list_del_init(&osd->o_keepalive_item);
  2835. ceph_con_keepalive(&osd->o_con);
  2836. }
  2837. up_write(&osdc->lock);
  2838. schedule_delayed_work(&osdc->timeout_work,
  2839. osdc->client->options->osd_keepalive_timeout);
  2840. }
  2841. static void handle_osds_timeout(struct work_struct *work)
  2842. {
  2843. struct ceph_osd_client *osdc =
  2844. container_of(work, struct ceph_osd_client,
  2845. osds_timeout_work.work);
  2846. unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
  2847. struct ceph_osd *osd, *nosd;
  2848. dout("%s osdc %p\n", __func__, osdc);
  2849. down_write(&osdc->lock);
  2850. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  2851. if (time_before(jiffies, osd->lru_ttl))
  2852. break;
  2853. WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
  2854. WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
  2855. close_osd(osd);
  2856. }
  2857. up_write(&osdc->lock);
  2858. schedule_delayed_work(&osdc->osds_timeout_work,
  2859. round_jiffies_relative(delay));
  2860. }
  2861. static int ceph_oloc_decode(void **p, void *end,
  2862. struct ceph_object_locator *oloc)
  2863. {
  2864. u8 struct_v, struct_cv;
  2865. u32 len;
  2866. void *struct_end;
  2867. int ret = 0;
  2868. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  2869. struct_v = ceph_decode_8(p);
  2870. struct_cv = ceph_decode_8(p);
  2871. if (struct_v < 3) {
  2872. pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
  2873. struct_v, struct_cv);
  2874. goto e_inval;
  2875. }
  2876. if (struct_cv > 6) {
  2877. pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
  2878. struct_v, struct_cv);
  2879. goto e_inval;
  2880. }
  2881. len = ceph_decode_32(p);
  2882. ceph_decode_need(p, end, len, e_inval);
  2883. struct_end = *p + len;
  2884. oloc->pool = ceph_decode_64(p);
  2885. *p += 4; /* skip preferred */
  2886. len = ceph_decode_32(p);
  2887. if (len > 0) {
  2888. pr_warn("ceph_object_locator::key is set\n");
  2889. goto e_inval;
  2890. }
  2891. if (struct_v >= 5) {
  2892. bool changed = false;
  2893. len = ceph_decode_32(p);
  2894. if (len > 0) {
  2895. ceph_decode_need(p, end, len, e_inval);
  2896. if (!oloc->pool_ns ||
  2897. ceph_compare_string(oloc->pool_ns, *p, len))
  2898. changed = true;
  2899. *p += len;
  2900. } else {
  2901. if (oloc->pool_ns)
  2902. changed = true;
  2903. }
  2904. if (changed) {
  2905. /* redirect changes namespace */
  2906. pr_warn("ceph_object_locator::nspace is changed\n");
  2907. goto e_inval;
  2908. }
  2909. }
  2910. if (struct_v >= 6) {
  2911. s64 hash = ceph_decode_64(p);
  2912. if (hash != -1) {
  2913. pr_warn("ceph_object_locator::hash is set\n");
  2914. goto e_inval;
  2915. }
  2916. }
  2917. /* skip the rest */
  2918. *p = struct_end;
  2919. out:
  2920. return ret;
  2921. e_inval:
  2922. ret = -EINVAL;
  2923. goto out;
  2924. }
  2925. static int ceph_redirect_decode(void **p, void *end,
  2926. struct ceph_request_redirect *redir)
  2927. {
  2928. u8 struct_v, struct_cv;
  2929. u32 len;
  2930. void *struct_end;
  2931. int ret;
  2932. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  2933. struct_v = ceph_decode_8(p);
  2934. struct_cv = ceph_decode_8(p);
  2935. if (struct_cv > 1) {
  2936. pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
  2937. struct_v, struct_cv);
  2938. goto e_inval;
  2939. }
  2940. len = ceph_decode_32(p);
  2941. ceph_decode_need(p, end, len, e_inval);
  2942. struct_end = *p + len;
  2943. ret = ceph_oloc_decode(p, end, &redir->oloc);
  2944. if (ret)
  2945. goto out;
  2946. len = ceph_decode_32(p);
  2947. if (len > 0) {
  2948. pr_warn("ceph_request_redirect::object_name is set\n");
  2949. goto e_inval;
  2950. }
  2951. /* skip the rest */
  2952. *p = struct_end;
  2953. out:
  2954. return ret;
  2955. e_inval:
  2956. ret = -EINVAL;
  2957. goto out;
  2958. }
  2959. struct MOSDOpReply {
  2960. struct ceph_pg pgid;
  2961. u64 flags;
  2962. int result;
  2963. u32 epoch;
  2964. int num_ops;
  2965. u32 outdata_len[CEPH_OSD_MAX_OPS];
  2966. s32 rval[CEPH_OSD_MAX_OPS];
  2967. int retry_attempt;
  2968. struct ceph_eversion replay_version;
  2969. u64 user_version;
  2970. struct ceph_request_redirect redirect;
  2971. };
  2972. static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
  2973. {
  2974. void *p = msg->front.iov_base;
  2975. void *const end = p + msg->front.iov_len;
  2976. u16 version = le16_to_cpu(msg->hdr.version);
  2977. struct ceph_eversion bad_replay_version;
  2978. u8 decode_redir;
  2979. u32 len;
  2980. int ret;
  2981. int i;
  2982. ceph_decode_32_safe(&p, end, len, e_inval);
  2983. ceph_decode_need(&p, end, len, e_inval);
  2984. p += len; /* skip oid */
  2985. ret = ceph_decode_pgid(&p, end, &m->pgid);
  2986. if (ret)
  2987. return ret;
  2988. ceph_decode_64_safe(&p, end, m->flags, e_inval);
  2989. ceph_decode_32_safe(&p, end, m->result, e_inval);
  2990. ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
  2991. memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
  2992. p += sizeof(bad_replay_version);
  2993. ceph_decode_32_safe(&p, end, m->epoch, e_inval);
  2994. ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
  2995. if (m->num_ops > ARRAY_SIZE(m->outdata_len))
  2996. goto e_inval;
  2997. ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
  2998. e_inval);
  2999. for (i = 0; i < m->num_ops; i++) {
  3000. struct ceph_osd_op *op = p;
  3001. m->outdata_len[i] = le32_to_cpu(op->payload_len);
  3002. p += sizeof(*op);
  3003. }
  3004. ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
  3005. for (i = 0; i < m->num_ops; i++)
  3006. ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
  3007. if (version >= 5) {
  3008. ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
  3009. memcpy(&m->replay_version, p, sizeof(m->replay_version));
  3010. p += sizeof(m->replay_version);
  3011. ceph_decode_64_safe(&p, end, m->user_version, e_inval);
  3012. } else {
  3013. m->replay_version = bad_replay_version; /* struct */
  3014. m->user_version = le64_to_cpu(m->replay_version.version);
  3015. }
  3016. if (version >= 6) {
  3017. if (version >= 7)
  3018. ceph_decode_8_safe(&p, end, decode_redir, e_inval);
  3019. else
  3020. decode_redir = 1;
  3021. } else {
  3022. decode_redir = 0;
  3023. }
  3024. if (decode_redir) {
  3025. ret = ceph_redirect_decode(&p, end, &m->redirect);
  3026. if (ret)
  3027. return ret;
  3028. } else {
  3029. ceph_oloc_init(&m->redirect.oloc);
  3030. }
  3031. return 0;
  3032. e_inval:
  3033. return -EINVAL;
  3034. }
  3035. /*
  3036. * Handle MOSDOpReply. Set ->r_result and call the callback if it is
  3037. * specified.
  3038. */
  3039. static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
  3040. {
  3041. struct ceph_osd_client *osdc = osd->o_osdc;
  3042. struct ceph_osd_request *req;
  3043. struct MOSDOpReply m;
  3044. u64 tid = le64_to_cpu(msg->hdr.tid);
  3045. u32 data_len = 0;
  3046. int ret;
  3047. int i;
  3048. dout("%s msg %p tid %llu\n", __func__, msg, tid);
  3049. down_read(&osdc->lock);
  3050. if (!osd_registered(osd)) {
  3051. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3052. goto out_unlock_osdc;
  3053. }
  3054. WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
  3055. mutex_lock(&osd->lock);
  3056. req = lookup_request(&osd->o_requests, tid);
  3057. if (!req) {
  3058. dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
  3059. goto out_unlock_session;
  3060. }
  3061. m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
  3062. ret = decode_MOSDOpReply(msg, &m);
  3063. m.redirect.oloc.pool_ns = NULL;
  3064. if (ret) {
  3065. pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
  3066. req->r_tid, ret);
  3067. ceph_msg_dump(msg);
  3068. goto fail_request;
  3069. }
  3070. dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
  3071. __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
  3072. m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
  3073. le64_to_cpu(m.replay_version.version), m.user_version);
  3074. if (m.retry_attempt >= 0) {
  3075. if (m.retry_attempt != req->r_attempts - 1) {
  3076. dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
  3077. req, req->r_tid, m.retry_attempt,
  3078. req->r_attempts - 1);
  3079. goto out_unlock_session;
  3080. }
  3081. } else {
  3082. WARN_ON(1); /* MOSDOpReply v4 is assumed */
  3083. }
  3084. if (!ceph_oloc_empty(&m.redirect.oloc)) {
  3085. dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
  3086. m.redirect.oloc.pool);
  3087. unlink_request(osd, req);
  3088. mutex_unlock(&osd->lock);
  3089. /*
  3090. * Not ceph_oloc_copy() - changing pool_ns is not
  3091. * supported.
  3092. */
  3093. req->r_t.target_oloc.pool = m.redirect.oloc.pool;
  3094. req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
  3095. req->r_tid = 0;
  3096. __submit_request(req, false);
  3097. goto out_unlock_osdc;
  3098. }
  3099. if (m.num_ops != req->r_num_ops) {
  3100. pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
  3101. req->r_num_ops, req->r_tid);
  3102. goto fail_request;
  3103. }
  3104. for (i = 0; i < req->r_num_ops; i++) {
  3105. dout(" req %p tid %llu op %d rval %d len %u\n", req,
  3106. req->r_tid, i, m.rval[i], m.outdata_len[i]);
  3107. req->r_ops[i].rval = m.rval[i];
  3108. req->r_ops[i].outdata_len = m.outdata_len[i];
  3109. data_len += m.outdata_len[i];
  3110. }
  3111. if (data_len != le32_to_cpu(msg->hdr.data_len)) {
  3112. pr_err("sum of lens %u != %u for tid %llu\n", data_len,
  3113. le32_to_cpu(msg->hdr.data_len), req->r_tid);
  3114. goto fail_request;
  3115. }
  3116. dout("%s req %p tid %llu result %d data_len %u\n", __func__,
  3117. req, req->r_tid, m.result, data_len);
  3118. /*
  3119. * Since we only ever request ONDISK, we should only ever get
  3120. * one (type of) reply back.
  3121. */
  3122. WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
  3123. req->r_result = m.result ?: data_len;
  3124. finish_request(req);
  3125. mutex_unlock(&osd->lock);
  3126. up_read(&osdc->lock);
  3127. __complete_request(req);
  3128. return;
  3129. fail_request:
  3130. complete_request(req, -EIO);
  3131. out_unlock_session:
  3132. mutex_unlock(&osd->lock);
  3133. out_unlock_osdc:
  3134. up_read(&osdc->lock);
  3135. }
  3136. static void set_pool_was_full(struct ceph_osd_client *osdc)
  3137. {
  3138. struct rb_node *n;
  3139. for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
  3140. struct ceph_pg_pool_info *pi =
  3141. rb_entry(n, struct ceph_pg_pool_info, node);
  3142. pi->was_full = __pool_full(pi);
  3143. }
  3144. }
  3145. static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
  3146. {
  3147. struct ceph_pg_pool_info *pi;
  3148. pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
  3149. if (!pi)
  3150. return false;
  3151. return pi->was_full && !__pool_full(pi);
  3152. }
  3153. static enum calc_target_result
  3154. recalc_linger_target(struct ceph_osd_linger_request *lreq)
  3155. {
  3156. struct ceph_osd_client *osdc = lreq->osdc;
  3157. enum calc_target_result ct_res;
  3158. ct_res = calc_target(osdc, &lreq->t, true);
  3159. if (ct_res == CALC_TARGET_NEED_RESEND) {
  3160. struct ceph_osd *osd;
  3161. osd = lookup_create_osd(osdc, lreq->t.osd, true);
  3162. if (osd != lreq->osd) {
  3163. unlink_linger(lreq->osd, lreq);
  3164. link_linger(osd, lreq);
  3165. }
  3166. }
  3167. return ct_res;
  3168. }
  3169. /*
  3170. * Requeue requests whose mapping to an OSD has changed.
  3171. */
  3172. static void scan_requests(struct ceph_osd *osd,
  3173. bool force_resend,
  3174. bool cleared_full,
  3175. bool check_pool_cleared_full,
  3176. struct rb_root *need_resend,
  3177. struct list_head *need_resend_linger)
  3178. {
  3179. struct ceph_osd_client *osdc = osd->o_osdc;
  3180. struct rb_node *n;
  3181. bool force_resend_writes;
  3182. for (n = rb_first(&osd->o_linger_requests); n; ) {
  3183. struct ceph_osd_linger_request *lreq =
  3184. rb_entry(n, struct ceph_osd_linger_request, node);
  3185. enum calc_target_result ct_res;
  3186. n = rb_next(n); /* recalc_linger_target() */
  3187. dout("%s lreq %p linger_id %llu\n", __func__, lreq,
  3188. lreq->linger_id);
  3189. ct_res = recalc_linger_target(lreq);
  3190. switch (ct_res) {
  3191. case CALC_TARGET_NO_ACTION:
  3192. force_resend_writes = cleared_full ||
  3193. (check_pool_cleared_full &&
  3194. pool_cleared_full(osdc, lreq->t.base_oloc.pool));
  3195. if (!force_resend && !force_resend_writes)
  3196. break;
  3197. /* fall through */
  3198. case CALC_TARGET_NEED_RESEND:
  3199. cancel_linger_map_check(lreq);
  3200. /*
  3201. * scan_requests() for the previous epoch(s)
  3202. * may have already added it to the list, since
  3203. * it's not unlinked here.
  3204. */
  3205. if (list_empty(&lreq->scan_item))
  3206. list_add_tail(&lreq->scan_item, need_resend_linger);
  3207. break;
  3208. case CALC_TARGET_POOL_DNE:
  3209. list_del_init(&lreq->scan_item);
  3210. check_linger_pool_dne(lreq);
  3211. break;
  3212. }
  3213. }
  3214. for (n = rb_first(&osd->o_requests); n; ) {
  3215. struct ceph_osd_request *req =
  3216. rb_entry(n, struct ceph_osd_request, r_node);
  3217. enum calc_target_result ct_res;
  3218. n = rb_next(n); /* unlink_request(), check_pool_dne() */
  3219. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  3220. ct_res = calc_target(osdc, &req->r_t, false);
  3221. switch (ct_res) {
  3222. case CALC_TARGET_NO_ACTION:
  3223. force_resend_writes = cleared_full ||
  3224. (check_pool_cleared_full &&
  3225. pool_cleared_full(osdc, req->r_t.base_oloc.pool));
  3226. if (!force_resend &&
  3227. (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
  3228. !force_resend_writes))
  3229. break;
  3230. /* fall through */
  3231. case CALC_TARGET_NEED_RESEND:
  3232. cancel_map_check(req);
  3233. unlink_request(osd, req);
  3234. insert_request(need_resend, req);
  3235. break;
  3236. case CALC_TARGET_POOL_DNE:
  3237. check_pool_dne(req);
  3238. break;
  3239. }
  3240. }
  3241. }
  3242. static int handle_one_map(struct ceph_osd_client *osdc,
  3243. void *p, void *end, bool incremental,
  3244. struct rb_root *need_resend,
  3245. struct list_head *need_resend_linger)
  3246. {
  3247. struct ceph_osdmap *newmap;
  3248. struct rb_node *n;
  3249. bool skipped_map = false;
  3250. bool was_full;
  3251. was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
  3252. set_pool_was_full(osdc);
  3253. if (incremental)
  3254. newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
  3255. else
  3256. newmap = ceph_osdmap_decode(&p, end);
  3257. if (IS_ERR(newmap))
  3258. return PTR_ERR(newmap);
  3259. if (newmap != osdc->osdmap) {
  3260. /*
  3261. * Preserve ->was_full before destroying the old map.
  3262. * For pools that weren't in the old map, ->was_full
  3263. * should be false.
  3264. */
  3265. for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
  3266. struct ceph_pg_pool_info *pi =
  3267. rb_entry(n, struct ceph_pg_pool_info, node);
  3268. struct ceph_pg_pool_info *old_pi;
  3269. old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
  3270. if (old_pi)
  3271. pi->was_full = old_pi->was_full;
  3272. else
  3273. WARN_ON(pi->was_full);
  3274. }
  3275. if (osdc->osdmap->epoch &&
  3276. osdc->osdmap->epoch + 1 < newmap->epoch) {
  3277. WARN_ON(incremental);
  3278. skipped_map = true;
  3279. }
  3280. ceph_osdmap_destroy(osdc->osdmap);
  3281. osdc->osdmap = newmap;
  3282. }
  3283. was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
  3284. scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
  3285. need_resend, need_resend_linger);
  3286. for (n = rb_first(&osdc->osds); n; ) {
  3287. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  3288. n = rb_next(n); /* close_osd() */
  3289. scan_requests(osd, skipped_map, was_full, true, need_resend,
  3290. need_resend_linger);
  3291. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  3292. memcmp(&osd->o_con.peer_addr,
  3293. ceph_osd_addr(osdc->osdmap, osd->o_osd),
  3294. sizeof(struct ceph_entity_addr)))
  3295. close_osd(osd);
  3296. }
  3297. return 0;
  3298. }
  3299. static void kick_requests(struct ceph_osd_client *osdc,
  3300. struct rb_root *need_resend,
  3301. struct list_head *need_resend_linger)
  3302. {
  3303. struct ceph_osd_linger_request *lreq, *nlreq;
  3304. enum calc_target_result ct_res;
  3305. struct rb_node *n;
  3306. /* make sure need_resend targets reflect latest map */
  3307. for (n = rb_first(need_resend); n; ) {
  3308. struct ceph_osd_request *req =
  3309. rb_entry(n, struct ceph_osd_request, r_node);
  3310. n = rb_next(n);
  3311. if (req->r_t.epoch < osdc->osdmap->epoch) {
  3312. ct_res = calc_target(osdc, &req->r_t, false);
  3313. if (ct_res == CALC_TARGET_POOL_DNE) {
  3314. erase_request(need_resend, req);
  3315. check_pool_dne(req);
  3316. }
  3317. }
  3318. }
  3319. for (n = rb_first(need_resend); n; ) {
  3320. struct ceph_osd_request *req =
  3321. rb_entry(n, struct ceph_osd_request, r_node);
  3322. struct ceph_osd *osd;
  3323. n = rb_next(n);
  3324. erase_request(need_resend, req); /* before link_request() */
  3325. osd = lookup_create_osd(osdc, req->r_t.osd, true);
  3326. link_request(osd, req);
  3327. if (!req->r_linger) {
  3328. if (!osd_homeless(osd) && !req->r_t.paused)
  3329. send_request(req);
  3330. } else {
  3331. cancel_linger_request(req);
  3332. }
  3333. }
  3334. list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
  3335. if (!osd_homeless(lreq->osd))
  3336. send_linger(lreq);
  3337. list_del_init(&lreq->scan_item);
  3338. }
  3339. }
  3340. /*
  3341. * Process updated osd map.
  3342. *
  3343. * The message contains any number of incremental and full maps, normally
  3344. * indicating some sort of topology change in the cluster. Kick requests
  3345. * off to different OSDs as needed.
  3346. */
  3347. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  3348. {
  3349. void *p = msg->front.iov_base;
  3350. void *const end = p + msg->front.iov_len;
  3351. u32 nr_maps, maplen;
  3352. u32 epoch;
  3353. struct ceph_fsid fsid;
  3354. struct rb_root need_resend = RB_ROOT;
  3355. LIST_HEAD(need_resend_linger);
  3356. bool handled_incremental = false;
  3357. bool was_pauserd, was_pausewr;
  3358. bool pauserd, pausewr;
  3359. int err;
  3360. dout("%s have %u\n", __func__, osdc->osdmap->epoch);
  3361. down_write(&osdc->lock);
  3362. /* verify fsid */
  3363. ceph_decode_need(&p, end, sizeof(fsid), bad);
  3364. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  3365. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  3366. goto bad;
  3367. was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  3368. was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  3369. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  3370. have_pool_full(osdc);
  3371. /* incremental maps */
  3372. ceph_decode_32_safe(&p, end, nr_maps, bad);
  3373. dout(" %d inc maps\n", nr_maps);
  3374. while (nr_maps > 0) {
  3375. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  3376. epoch = ceph_decode_32(&p);
  3377. maplen = ceph_decode_32(&p);
  3378. ceph_decode_need(&p, end, maplen, bad);
  3379. if (osdc->osdmap->epoch &&
  3380. osdc->osdmap->epoch + 1 == epoch) {
  3381. dout("applying incremental map %u len %d\n",
  3382. epoch, maplen);
  3383. err = handle_one_map(osdc, p, p + maplen, true,
  3384. &need_resend, &need_resend_linger);
  3385. if (err)
  3386. goto bad;
  3387. handled_incremental = true;
  3388. } else {
  3389. dout("ignoring incremental map %u len %d\n",
  3390. epoch, maplen);
  3391. }
  3392. p += maplen;
  3393. nr_maps--;
  3394. }
  3395. if (handled_incremental)
  3396. goto done;
  3397. /* full maps */
  3398. ceph_decode_32_safe(&p, end, nr_maps, bad);
  3399. dout(" %d full maps\n", nr_maps);
  3400. while (nr_maps) {
  3401. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  3402. epoch = ceph_decode_32(&p);
  3403. maplen = ceph_decode_32(&p);
  3404. ceph_decode_need(&p, end, maplen, bad);
  3405. if (nr_maps > 1) {
  3406. dout("skipping non-latest full map %u len %d\n",
  3407. epoch, maplen);
  3408. } else if (osdc->osdmap->epoch >= epoch) {
  3409. dout("skipping full map %u len %d, "
  3410. "older than our %u\n", epoch, maplen,
  3411. osdc->osdmap->epoch);
  3412. } else {
  3413. dout("taking full map %u len %d\n", epoch, maplen);
  3414. err = handle_one_map(osdc, p, p + maplen, false,
  3415. &need_resend, &need_resend_linger);
  3416. if (err)
  3417. goto bad;
  3418. }
  3419. p += maplen;
  3420. nr_maps--;
  3421. }
  3422. done:
  3423. /*
  3424. * subscribe to subsequent osdmap updates if full to ensure
  3425. * we find out when we are no longer full and stop returning
  3426. * ENOSPC.
  3427. */
  3428. pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  3429. pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  3430. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  3431. have_pool_full(osdc);
  3432. if (was_pauserd || was_pausewr || pauserd || pausewr ||
  3433. osdc->osdmap->epoch < osdc->epoch_barrier)
  3434. maybe_request_map(osdc);
  3435. kick_requests(osdc, &need_resend, &need_resend_linger);
  3436. ceph_osdc_abort_on_full(osdc);
  3437. ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
  3438. osdc->osdmap->epoch);
  3439. up_write(&osdc->lock);
  3440. wake_up_all(&osdc->client->auth_wq);
  3441. return;
  3442. bad:
  3443. pr_err("osdc handle_map corrupt msg\n");
  3444. ceph_msg_dump(msg);
  3445. up_write(&osdc->lock);
  3446. }
  3447. /*
  3448. * Resubmit requests pending on the given osd.
  3449. */
  3450. static void kick_osd_requests(struct ceph_osd *osd)
  3451. {
  3452. struct rb_node *n;
  3453. clear_backoffs(osd);
  3454. for (n = rb_first(&osd->o_requests); n; ) {
  3455. struct ceph_osd_request *req =
  3456. rb_entry(n, struct ceph_osd_request, r_node);
  3457. n = rb_next(n); /* cancel_linger_request() */
  3458. if (!req->r_linger) {
  3459. if (!req->r_t.paused)
  3460. send_request(req);
  3461. } else {
  3462. cancel_linger_request(req);
  3463. }
  3464. }
  3465. for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
  3466. struct ceph_osd_linger_request *lreq =
  3467. rb_entry(n, struct ceph_osd_linger_request, node);
  3468. send_linger(lreq);
  3469. }
  3470. }
  3471. /*
  3472. * If the osd connection drops, we need to resubmit all requests.
  3473. */
  3474. static void osd_fault(struct ceph_connection *con)
  3475. {
  3476. struct ceph_osd *osd = con->private;
  3477. struct ceph_osd_client *osdc = osd->o_osdc;
  3478. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  3479. down_write(&osdc->lock);
  3480. if (!osd_registered(osd)) {
  3481. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3482. goto out_unlock;
  3483. }
  3484. if (!reopen_osd(osd))
  3485. kick_osd_requests(osd);
  3486. maybe_request_map(osdc);
  3487. out_unlock:
  3488. up_write(&osdc->lock);
  3489. }
  3490. struct MOSDBackoff {
  3491. struct ceph_spg spgid;
  3492. u32 map_epoch;
  3493. u8 op;
  3494. u64 id;
  3495. struct ceph_hobject_id *begin;
  3496. struct ceph_hobject_id *end;
  3497. };
  3498. static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
  3499. {
  3500. void *p = msg->front.iov_base;
  3501. void *const end = p + msg->front.iov_len;
  3502. u8 struct_v;
  3503. u32 struct_len;
  3504. int ret;
  3505. ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
  3506. if (ret)
  3507. return ret;
  3508. ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
  3509. if (ret)
  3510. return ret;
  3511. ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
  3512. ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
  3513. ceph_decode_8_safe(&p, end, m->op, e_inval);
  3514. ceph_decode_64_safe(&p, end, m->id, e_inval);
  3515. m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
  3516. if (!m->begin)
  3517. return -ENOMEM;
  3518. ret = decode_hoid(&p, end, m->begin);
  3519. if (ret) {
  3520. free_hoid(m->begin);
  3521. return ret;
  3522. }
  3523. m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
  3524. if (!m->end) {
  3525. free_hoid(m->begin);
  3526. return -ENOMEM;
  3527. }
  3528. ret = decode_hoid(&p, end, m->end);
  3529. if (ret) {
  3530. free_hoid(m->begin);
  3531. free_hoid(m->end);
  3532. return ret;
  3533. }
  3534. return 0;
  3535. e_inval:
  3536. return -EINVAL;
  3537. }
  3538. static struct ceph_msg *create_backoff_message(
  3539. const struct ceph_osd_backoff *backoff,
  3540. u32 map_epoch)
  3541. {
  3542. struct ceph_msg *msg;
  3543. void *p, *end;
  3544. int msg_size;
  3545. msg_size = CEPH_ENCODING_START_BLK_LEN +
  3546. CEPH_PGID_ENCODING_LEN + 1; /* spgid */
  3547. msg_size += 4 + 1 + 8; /* map_epoch, op, id */
  3548. msg_size += CEPH_ENCODING_START_BLK_LEN +
  3549. hoid_encoding_size(backoff->begin);
  3550. msg_size += CEPH_ENCODING_START_BLK_LEN +
  3551. hoid_encoding_size(backoff->end);
  3552. msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
  3553. if (!msg)
  3554. return NULL;
  3555. p = msg->front.iov_base;
  3556. end = p + msg->front_alloc_len;
  3557. encode_spgid(&p, &backoff->spgid);
  3558. ceph_encode_32(&p, map_epoch);
  3559. ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
  3560. ceph_encode_64(&p, backoff->id);
  3561. encode_hoid(&p, end, backoff->begin);
  3562. encode_hoid(&p, end, backoff->end);
  3563. BUG_ON(p != end);
  3564. msg->front.iov_len = p - msg->front.iov_base;
  3565. msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
  3566. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  3567. return msg;
  3568. }
  3569. static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
  3570. {
  3571. struct ceph_spg_mapping *spg;
  3572. struct ceph_osd_backoff *backoff;
  3573. struct ceph_msg *msg;
  3574. dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
  3575. m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
  3576. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
  3577. if (!spg) {
  3578. spg = alloc_spg_mapping();
  3579. if (!spg) {
  3580. pr_err("%s failed to allocate spg\n", __func__);
  3581. return;
  3582. }
  3583. spg->spgid = m->spgid; /* struct */
  3584. insert_spg_mapping(&osd->o_backoff_mappings, spg);
  3585. }
  3586. backoff = alloc_backoff();
  3587. if (!backoff) {
  3588. pr_err("%s failed to allocate backoff\n", __func__);
  3589. return;
  3590. }
  3591. backoff->spgid = m->spgid; /* struct */
  3592. backoff->id = m->id;
  3593. backoff->begin = m->begin;
  3594. m->begin = NULL; /* backoff now owns this */
  3595. backoff->end = m->end;
  3596. m->end = NULL; /* ditto */
  3597. insert_backoff(&spg->backoffs, backoff);
  3598. insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  3599. /*
  3600. * Ack with original backoff's epoch so that the OSD can
  3601. * discard this if there was a PG split.
  3602. */
  3603. msg = create_backoff_message(backoff, m->map_epoch);
  3604. if (!msg) {
  3605. pr_err("%s failed to allocate msg\n", __func__);
  3606. return;
  3607. }
  3608. ceph_con_send(&osd->o_con, msg);
  3609. }
  3610. static bool target_contained_by(const struct ceph_osd_request_target *t,
  3611. const struct ceph_hobject_id *begin,
  3612. const struct ceph_hobject_id *end)
  3613. {
  3614. struct ceph_hobject_id hoid;
  3615. int cmp;
  3616. hoid_fill_from_target(&hoid, t);
  3617. cmp = hoid_compare(&hoid, begin);
  3618. return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
  3619. }
  3620. static void handle_backoff_unblock(struct ceph_osd *osd,
  3621. const struct MOSDBackoff *m)
  3622. {
  3623. struct ceph_spg_mapping *spg;
  3624. struct ceph_osd_backoff *backoff;
  3625. struct rb_node *n;
  3626. dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
  3627. m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
  3628. backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
  3629. if (!backoff) {
  3630. pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
  3631. __func__, osd->o_osd, m->spgid.pgid.pool,
  3632. m->spgid.pgid.seed, m->spgid.shard, m->id);
  3633. return;
  3634. }
  3635. if (hoid_compare(backoff->begin, m->begin) &&
  3636. hoid_compare(backoff->end, m->end)) {
  3637. pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
  3638. __func__, osd->o_osd, m->spgid.pgid.pool,
  3639. m->spgid.pgid.seed, m->spgid.shard, m->id);
  3640. /* unblock it anyway... */
  3641. }
  3642. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
  3643. BUG_ON(!spg);
  3644. erase_backoff(&spg->backoffs, backoff);
  3645. erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  3646. free_backoff(backoff);
  3647. if (RB_EMPTY_ROOT(&spg->backoffs)) {
  3648. erase_spg_mapping(&osd->o_backoff_mappings, spg);
  3649. free_spg_mapping(spg);
  3650. }
  3651. for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
  3652. struct ceph_osd_request *req =
  3653. rb_entry(n, struct ceph_osd_request, r_node);
  3654. if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
  3655. /*
  3656. * Match against @m, not @backoff -- the PG may
  3657. * have split on the OSD.
  3658. */
  3659. if (target_contained_by(&req->r_t, m->begin, m->end)) {
  3660. /*
  3661. * If no other installed backoff applies,
  3662. * resend.
  3663. */
  3664. send_request(req);
  3665. }
  3666. }
  3667. }
  3668. }
  3669. static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
  3670. {
  3671. struct ceph_osd_client *osdc = osd->o_osdc;
  3672. struct MOSDBackoff m;
  3673. int ret;
  3674. down_read(&osdc->lock);
  3675. if (!osd_registered(osd)) {
  3676. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3677. up_read(&osdc->lock);
  3678. return;
  3679. }
  3680. WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
  3681. mutex_lock(&osd->lock);
  3682. ret = decode_MOSDBackoff(msg, &m);
  3683. if (ret) {
  3684. pr_err("failed to decode MOSDBackoff: %d\n", ret);
  3685. ceph_msg_dump(msg);
  3686. goto out_unlock;
  3687. }
  3688. switch (m.op) {
  3689. case CEPH_OSD_BACKOFF_OP_BLOCK:
  3690. handle_backoff_block(osd, &m);
  3691. break;
  3692. case CEPH_OSD_BACKOFF_OP_UNBLOCK:
  3693. handle_backoff_unblock(osd, &m);
  3694. break;
  3695. default:
  3696. pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
  3697. }
  3698. free_hoid(m.begin);
  3699. free_hoid(m.end);
  3700. out_unlock:
  3701. mutex_unlock(&osd->lock);
  3702. up_read(&osdc->lock);
  3703. }
  3704. /*
  3705. * Process osd watch notifications
  3706. */
  3707. static void handle_watch_notify(struct ceph_osd_client *osdc,
  3708. struct ceph_msg *msg)
  3709. {
  3710. void *p = msg->front.iov_base;
  3711. void *const end = p + msg->front.iov_len;
  3712. struct ceph_osd_linger_request *lreq;
  3713. struct linger_work *lwork;
  3714. u8 proto_ver, opcode;
  3715. u64 cookie, notify_id;
  3716. u64 notifier_id = 0;
  3717. s32 return_code = 0;
  3718. void *payload = NULL;
  3719. u32 payload_len = 0;
  3720. ceph_decode_8_safe(&p, end, proto_ver, bad);
  3721. ceph_decode_8_safe(&p, end, opcode, bad);
  3722. ceph_decode_64_safe(&p, end, cookie, bad);
  3723. p += 8; /* skip ver */
  3724. ceph_decode_64_safe(&p, end, notify_id, bad);
  3725. if (proto_ver >= 1) {
  3726. ceph_decode_32_safe(&p, end, payload_len, bad);
  3727. ceph_decode_need(&p, end, payload_len, bad);
  3728. payload = p;
  3729. p += payload_len;
  3730. }
  3731. if (le16_to_cpu(msg->hdr.version) >= 2)
  3732. ceph_decode_32_safe(&p, end, return_code, bad);
  3733. if (le16_to_cpu(msg->hdr.version) >= 3)
  3734. ceph_decode_64_safe(&p, end, notifier_id, bad);
  3735. down_read(&osdc->lock);
  3736. lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
  3737. if (!lreq) {
  3738. dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
  3739. cookie);
  3740. goto out_unlock_osdc;
  3741. }
  3742. mutex_lock(&lreq->lock);
  3743. dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
  3744. opcode, cookie, lreq, lreq->is_watch);
  3745. if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
  3746. if (!lreq->last_error) {
  3747. lreq->last_error = -ENOTCONN;
  3748. queue_watch_error(lreq);
  3749. }
  3750. } else if (!lreq->is_watch) {
  3751. /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
  3752. if (lreq->notify_id && lreq->notify_id != notify_id) {
  3753. dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
  3754. lreq->notify_id, notify_id);
  3755. } else if (!completion_done(&lreq->notify_finish_wait)) {
  3756. struct ceph_msg_data *data =
  3757. msg->num_data_items ? &msg->data[0] : NULL;
  3758. if (data) {
  3759. if (lreq->preply_pages) {
  3760. WARN_ON(data->type !=
  3761. CEPH_MSG_DATA_PAGES);
  3762. *lreq->preply_pages = data->pages;
  3763. *lreq->preply_len = data->length;
  3764. data->own_pages = false;
  3765. }
  3766. }
  3767. lreq->notify_finish_error = return_code;
  3768. complete_all(&lreq->notify_finish_wait);
  3769. }
  3770. } else {
  3771. /* CEPH_WATCH_EVENT_NOTIFY */
  3772. lwork = lwork_alloc(lreq, do_watch_notify);
  3773. if (!lwork) {
  3774. pr_err("failed to allocate notify-lwork\n");
  3775. goto out_unlock_lreq;
  3776. }
  3777. lwork->notify.notify_id = notify_id;
  3778. lwork->notify.notifier_id = notifier_id;
  3779. lwork->notify.payload = payload;
  3780. lwork->notify.payload_len = payload_len;
  3781. lwork->notify.msg = ceph_msg_get(msg);
  3782. lwork_queue(lwork);
  3783. }
  3784. out_unlock_lreq:
  3785. mutex_unlock(&lreq->lock);
  3786. out_unlock_osdc:
  3787. up_read(&osdc->lock);
  3788. return;
  3789. bad:
  3790. pr_err("osdc handle_watch_notify corrupt msg\n");
  3791. }
  3792. /*
  3793. * Register request, send initial attempt.
  3794. */
  3795. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  3796. struct ceph_osd_request *req,
  3797. bool nofail)
  3798. {
  3799. down_read(&osdc->lock);
  3800. submit_request(req, false);
  3801. up_read(&osdc->lock);
  3802. return 0;
  3803. }
  3804. EXPORT_SYMBOL(ceph_osdc_start_request);
  3805. /*
  3806. * Unregister a registered request. The request is not completed:
  3807. * ->r_result isn't set and __complete_request() isn't called.
  3808. */
  3809. void ceph_osdc_cancel_request(struct ceph_osd_request *req)
  3810. {
  3811. struct ceph_osd_client *osdc = req->r_osdc;
  3812. down_write(&osdc->lock);
  3813. if (req->r_osd)
  3814. cancel_request(req);
  3815. up_write(&osdc->lock);
  3816. }
  3817. EXPORT_SYMBOL(ceph_osdc_cancel_request);
  3818. /*
  3819. * @timeout: in jiffies, 0 means "wait forever"
  3820. */
  3821. static int wait_request_timeout(struct ceph_osd_request *req,
  3822. unsigned long timeout)
  3823. {
  3824. long left;
  3825. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  3826. left = wait_for_completion_killable_timeout(&req->r_completion,
  3827. ceph_timeout_jiffies(timeout));
  3828. if (left <= 0) {
  3829. left = left ?: -ETIMEDOUT;
  3830. ceph_osdc_cancel_request(req);
  3831. } else {
  3832. left = req->r_result; /* completed */
  3833. }
  3834. return left;
  3835. }
  3836. /*
  3837. * wait for a request to complete
  3838. */
  3839. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  3840. struct ceph_osd_request *req)
  3841. {
  3842. return wait_request_timeout(req, 0);
  3843. }
  3844. EXPORT_SYMBOL(ceph_osdc_wait_request);
  3845. /*
  3846. * sync - wait for all in-flight requests to flush. avoid starvation.
  3847. */
  3848. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  3849. {
  3850. struct rb_node *n, *p;
  3851. u64 last_tid = atomic64_read(&osdc->last_tid);
  3852. again:
  3853. down_read(&osdc->lock);
  3854. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  3855. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  3856. mutex_lock(&osd->lock);
  3857. for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
  3858. struct ceph_osd_request *req =
  3859. rb_entry(p, struct ceph_osd_request, r_node);
  3860. if (req->r_tid > last_tid)
  3861. break;
  3862. if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
  3863. continue;
  3864. ceph_osdc_get_request(req);
  3865. mutex_unlock(&osd->lock);
  3866. up_read(&osdc->lock);
  3867. dout("%s waiting on req %p tid %llu last_tid %llu\n",
  3868. __func__, req, req->r_tid, last_tid);
  3869. wait_for_completion(&req->r_completion);
  3870. ceph_osdc_put_request(req);
  3871. goto again;
  3872. }
  3873. mutex_unlock(&osd->lock);
  3874. }
  3875. up_read(&osdc->lock);
  3876. dout("%s done last_tid %llu\n", __func__, last_tid);
  3877. }
  3878. EXPORT_SYMBOL(ceph_osdc_sync);
  3879. static struct ceph_osd_request *
  3880. alloc_linger_request(struct ceph_osd_linger_request *lreq)
  3881. {
  3882. struct ceph_osd_request *req;
  3883. req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
  3884. if (!req)
  3885. return NULL;
  3886. ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
  3887. ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
  3888. return req;
  3889. }
  3890. static struct ceph_osd_request *
  3891. alloc_watch_request(struct ceph_osd_linger_request *lreq, u8 watch_opcode)
  3892. {
  3893. struct ceph_osd_request *req;
  3894. req = alloc_linger_request(lreq);
  3895. if (!req)
  3896. return NULL;
  3897. /*
  3898. * Pass 0 for cookie because we don't know it yet, it will be
  3899. * filled in by linger_submit().
  3900. */
  3901. osd_req_op_watch_init(req, 0, 0, watch_opcode);
  3902. if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
  3903. ceph_osdc_put_request(req);
  3904. return NULL;
  3905. }
  3906. return req;
  3907. }
  3908. /*
  3909. * Returns a handle, caller owns a ref.
  3910. */
  3911. struct ceph_osd_linger_request *
  3912. ceph_osdc_watch(struct ceph_osd_client *osdc,
  3913. struct ceph_object_id *oid,
  3914. struct ceph_object_locator *oloc,
  3915. rados_watchcb2_t wcb,
  3916. rados_watcherrcb_t errcb,
  3917. void *data)
  3918. {
  3919. struct ceph_osd_linger_request *lreq;
  3920. int ret;
  3921. lreq = linger_alloc(osdc);
  3922. if (!lreq)
  3923. return ERR_PTR(-ENOMEM);
  3924. lreq->is_watch = true;
  3925. lreq->wcb = wcb;
  3926. lreq->errcb = errcb;
  3927. lreq->data = data;
  3928. lreq->watch_valid_thru = jiffies;
  3929. ceph_oid_copy(&lreq->t.base_oid, oid);
  3930. ceph_oloc_copy(&lreq->t.base_oloc, oloc);
  3931. lreq->t.flags = CEPH_OSD_FLAG_WRITE;
  3932. ktime_get_real_ts64(&lreq->mtime);
  3933. lreq->reg_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_WATCH);
  3934. if (!lreq->reg_req) {
  3935. ret = -ENOMEM;
  3936. goto err_put_lreq;
  3937. }
  3938. lreq->ping_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_PING);
  3939. if (!lreq->ping_req) {
  3940. ret = -ENOMEM;
  3941. goto err_put_lreq;
  3942. }
  3943. linger_submit(lreq);
  3944. ret = linger_reg_commit_wait(lreq);
  3945. if (ret) {
  3946. linger_cancel(lreq);
  3947. goto err_put_lreq;
  3948. }
  3949. return lreq;
  3950. err_put_lreq:
  3951. linger_put(lreq);
  3952. return ERR_PTR(ret);
  3953. }
  3954. EXPORT_SYMBOL(ceph_osdc_watch);
  3955. /*
  3956. * Releases a ref.
  3957. *
  3958. * Times out after mount_timeout to preserve rbd unmap behaviour
  3959. * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
  3960. * with mount_timeout").
  3961. */
  3962. int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
  3963. struct ceph_osd_linger_request *lreq)
  3964. {
  3965. struct ceph_options *opts = osdc->client->options;
  3966. struct ceph_osd_request *req;
  3967. int ret;
  3968. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  3969. if (!req)
  3970. return -ENOMEM;
  3971. ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
  3972. ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
  3973. req->r_flags = CEPH_OSD_FLAG_WRITE;
  3974. ktime_get_real_ts64(&req->r_mtime);
  3975. osd_req_op_watch_init(req, 0, lreq->linger_id,
  3976. CEPH_OSD_WATCH_OP_UNWATCH);
  3977. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  3978. if (ret)
  3979. goto out_put_req;
  3980. ceph_osdc_start_request(osdc, req, false);
  3981. linger_cancel(lreq);
  3982. linger_put(lreq);
  3983. ret = wait_request_timeout(req, opts->mount_timeout);
  3984. out_put_req:
  3985. ceph_osdc_put_request(req);
  3986. return ret;
  3987. }
  3988. EXPORT_SYMBOL(ceph_osdc_unwatch);
  3989. static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
  3990. u64 notify_id, u64 cookie, void *payload,
  3991. u32 payload_len)
  3992. {
  3993. struct ceph_osd_req_op *op;
  3994. struct ceph_pagelist *pl;
  3995. int ret;
  3996. op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
  3997. pl = ceph_pagelist_alloc(GFP_NOIO);
  3998. if (!pl)
  3999. return -ENOMEM;
  4000. ret = ceph_pagelist_encode_64(pl, notify_id);
  4001. ret |= ceph_pagelist_encode_64(pl, cookie);
  4002. if (payload) {
  4003. ret |= ceph_pagelist_encode_32(pl, payload_len);
  4004. ret |= ceph_pagelist_append(pl, payload, payload_len);
  4005. } else {
  4006. ret |= ceph_pagelist_encode_32(pl, 0);
  4007. }
  4008. if (ret) {
  4009. ceph_pagelist_release(pl);
  4010. return -ENOMEM;
  4011. }
  4012. ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
  4013. op->indata_len = pl->length;
  4014. return 0;
  4015. }
  4016. int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
  4017. struct ceph_object_id *oid,
  4018. struct ceph_object_locator *oloc,
  4019. u64 notify_id,
  4020. u64 cookie,
  4021. void *payload,
  4022. u32 payload_len)
  4023. {
  4024. struct ceph_osd_request *req;
  4025. int ret;
  4026. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4027. if (!req)
  4028. return -ENOMEM;
  4029. ceph_oid_copy(&req->r_base_oid, oid);
  4030. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4031. req->r_flags = CEPH_OSD_FLAG_READ;
  4032. ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
  4033. payload_len);
  4034. if (ret)
  4035. goto out_put_req;
  4036. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4037. if (ret)
  4038. goto out_put_req;
  4039. ceph_osdc_start_request(osdc, req, false);
  4040. ret = ceph_osdc_wait_request(osdc, req);
  4041. out_put_req:
  4042. ceph_osdc_put_request(req);
  4043. return ret;
  4044. }
  4045. EXPORT_SYMBOL(ceph_osdc_notify_ack);
  4046. static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
  4047. u64 cookie, u32 prot_ver, u32 timeout,
  4048. void *payload, u32 payload_len)
  4049. {
  4050. struct ceph_osd_req_op *op;
  4051. struct ceph_pagelist *pl;
  4052. int ret;
  4053. op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
  4054. op->notify.cookie = cookie;
  4055. pl = ceph_pagelist_alloc(GFP_NOIO);
  4056. if (!pl)
  4057. return -ENOMEM;
  4058. ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
  4059. ret |= ceph_pagelist_encode_32(pl, timeout);
  4060. ret |= ceph_pagelist_encode_32(pl, payload_len);
  4061. ret |= ceph_pagelist_append(pl, payload, payload_len);
  4062. if (ret) {
  4063. ceph_pagelist_release(pl);
  4064. return -ENOMEM;
  4065. }
  4066. ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
  4067. op->indata_len = pl->length;
  4068. return 0;
  4069. }
  4070. /*
  4071. * @timeout: in seconds
  4072. *
  4073. * @preply_{pages,len} are initialized both on success and error.
  4074. * The caller is responsible for:
  4075. *
  4076. * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
  4077. */
  4078. int ceph_osdc_notify(struct ceph_osd_client *osdc,
  4079. struct ceph_object_id *oid,
  4080. struct ceph_object_locator *oloc,
  4081. void *payload,
  4082. u32 payload_len,
  4083. u32 timeout,
  4084. struct page ***preply_pages,
  4085. size_t *preply_len)
  4086. {
  4087. struct ceph_osd_linger_request *lreq;
  4088. struct page **pages;
  4089. int ret;
  4090. WARN_ON(!timeout);
  4091. if (preply_pages) {
  4092. *preply_pages = NULL;
  4093. *preply_len = 0;
  4094. }
  4095. lreq = linger_alloc(osdc);
  4096. if (!lreq)
  4097. return -ENOMEM;
  4098. lreq->preply_pages = preply_pages;
  4099. lreq->preply_len = preply_len;
  4100. ceph_oid_copy(&lreq->t.base_oid, oid);
  4101. ceph_oloc_copy(&lreq->t.base_oloc, oloc);
  4102. lreq->t.flags = CEPH_OSD_FLAG_READ;
  4103. lreq->reg_req = alloc_linger_request(lreq);
  4104. if (!lreq->reg_req) {
  4105. ret = -ENOMEM;
  4106. goto out_put_lreq;
  4107. }
  4108. /*
  4109. * Pass 0 for cookie because we don't know it yet, it will be
  4110. * filled in by linger_submit().
  4111. */
  4112. ret = osd_req_op_notify_init(lreq->reg_req, 0, 0, 1, timeout,
  4113. payload, payload_len);
  4114. if (ret)
  4115. goto out_put_lreq;
  4116. /* for notify_id */
  4117. pages = ceph_alloc_page_vector(1, GFP_NOIO);
  4118. if (IS_ERR(pages)) {
  4119. ret = PTR_ERR(pages);
  4120. goto out_put_lreq;
  4121. }
  4122. ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
  4123. response_data),
  4124. pages, PAGE_SIZE, 0, false, true);
  4125. ret = ceph_osdc_alloc_messages(lreq->reg_req, GFP_NOIO);
  4126. if (ret)
  4127. goto out_put_lreq;
  4128. linger_submit(lreq);
  4129. ret = linger_reg_commit_wait(lreq);
  4130. if (!ret)
  4131. ret = linger_notify_finish_wait(lreq);
  4132. else
  4133. dout("lreq %p failed to initiate notify %d\n", lreq, ret);
  4134. linger_cancel(lreq);
  4135. out_put_lreq:
  4136. linger_put(lreq);
  4137. return ret;
  4138. }
  4139. EXPORT_SYMBOL(ceph_osdc_notify);
  4140. /*
  4141. * Return the number of milliseconds since the watch was last
  4142. * confirmed, or an error. If there is an error, the watch is no
  4143. * longer valid, and should be destroyed with ceph_osdc_unwatch().
  4144. */
  4145. int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
  4146. struct ceph_osd_linger_request *lreq)
  4147. {
  4148. unsigned long stamp, age;
  4149. int ret;
  4150. down_read(&osdc->lock);
  4151. mutex_lock(&lreq->lock);
  4152. stamp = lreq->watch_valid_thru;
  4153. if (!list_empty(&lreq->pending_lworks)) {
  4154. struct linger_work *lwork =
  4155. list_first_entry(&lreq->pending_lworks,
  4156. struct linger_work,
  4157. pending_item);
  4158. if (time_before(lwork->queued_stamp, stamp))
  4159. stamp = lwork->queued_stamp;
  4160. }
  4161. age = jiffies - stamp;
  4162. dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
  4163. lreq, lreq->linger_id, age, lreq->last_error);
  4164. /* we are truncating to msecs, so return a safe upper bound */
  4165. ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
  4166. mutex_unlock(&lreq->lock);
  4167. up_read(&osdc->lock);
  4168. return ret;
  4169. }
  4170. static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
  4171. {
  4172. u8 struct_v;
  4173. u32 struct_len;
  4174. int ret;
  4175. ret = ceph_start_decoding(p, end, 2, "watch_item_t",
  4176. &struct_v, &struct_len);
  4177. if (ret)
  4178. goto bad;
  4179. ret = -EINVAL;
  4180. ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
  4181. ceph_decode_64_safe(p, end, item->cookie, bad);
  4182. ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
  4183. if (struct_v >= 2) {
  4184. ret = ceph_decode_entity_addr(p, end, &item->addr);
  4185. if (ret)
  4186. goto bad;
  4187. } else {
  4188. ret = 0;
  4189. }
  4190. dout("%s %s%llu cookie %llu addr %s\n", __func__,
  4191. ENTITY_NAME(item->name), item->cookie,
  4192. ceph_pr_addr(&item->addr));
  4193. bad:
  4194. return ret;
  4195. }
  4196. static int decode_watchers(void **p, void *end,
  4197. struct ceph_watch_item **watchers,
  4198. u32 *num_watchers)
  4199. {
  4200. u8 struct_v;
  4201. u32 struct_len;
  4202. int i;
  4203. int ret;
  4204. ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
  4205. &struct_v, &struct_len);
  4206. if (ret)
  4207. return ret;
  4208. *num_watchers = ceph_decode_32(p);
  4209. *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
  4210. if (!*watchers)
  4211. return -ENOMEM;
  4212. for (i = 0; i < *num_watchers; i++) {
  4213. ret = decode_watcher(p, end, *watchers + i);
  4214. if (ret) {
  4215. kfree(*watchers);
  4216. return ret;
  4217. }
  4218. }
  4219. return 0;
  4220. }
  4221. /*
  4222. * On success, the caller is responsible for:
  4223. *
  4224. * kfree(watchers);
  4225. */
  4226. int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
  4227. struct ceph_object_id *oid,
  4228. struct ceph_object_locator *oloc,
  4229. struct ceph_watch_item **watchers,
  4230. u32 *num_watchers)
  4231. {
  4232. struct ceph_osd_request *req;
  4233. struct page **pages;
  4234. int ret;
  4235. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4236. if (!req)
  4237. return -ENOMEM;
  4238. ceph_oid_copy(&req->r_base_oid, oid);
  4239. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4240. req->r_flags = CEPH_OSD_FLAG_READ;
  4241. pages = ceph_alloc_page_vector(1, GFP_NOIO);
  4242. if (IS_ERR(pages)) {
  4243. ret = PTR_ERR(pages);
  4244. goto out_put_req;
  4245. }
  4246. osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
  4247. ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
  4248. response_data),
  4249. pages, PAGE_SIZE, 0, false, true);
  4250. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4251. if (ret)
  4252. goto out_put_req;
  4253. ceph_osdc_start_request(osdc, req, false);
  4254. ret = ceph_osdc_wait_request(osdc, req);
  4255. if (ret >= 0) {
  4256. void *p = page_address(pages[0]);
  4257. void *const end = p + req->r_ops[0].outdata_len;
  4258. ret = decode_watchers(&p, end, watchers, num_watchers);
  4259. }
  4260. out_put_req:
  4261. ceph_osdc_put_request(req);
  4262. return ret;
  4263. }
  4264. EXPORT_SYMBOL(ceph_osdc_list_watchers);
  4265. /*
  4266. * Call all pending notify callbacks - for use after a watch is
  4267. * unregistered, to make sure no more callbacks for it will be invoked
  4268. */
  4269. void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
  4270. {
  4271. dout("%s osdc %p\n", __func__, osdc);
  4272. flush_workqueue(osdc->notify_wq);
  4273. }
  4274. EXPORT_SYMBOL(ceph_osdc_flush_notifies);
  4275. void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
  4276. {
  4277. down_read(&osdc->lock);
  4278. maybe_request_map(osdc);
  4279. up_read(&osdc->lock);
  4280. }
  4281. EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
  4282. /*
  4283. * Execute an OSD class method on an object.
  4284. *
  4285. * @flags: CEPH_OSD_FLAG_*
  4286. * @resp_len: in/out param for reply length
  4287. */
  4288. int ceph_osdc_call(struct ceph_osd_client *osdc,
  4289. struct ceph_object_id *oid,
  4290. struct ceph_object_locator *oloc,
  4291. const char *class, const char *method,
  4292. unsigned int flags,
  4293. struct page *req_page, size_t req_len,
  4294. struct page **resp_pages, size_t *resp_len)
  4295. {
  4296. struct ceph_osd_request *req;
  4297. int ret;
  4298. if (req_len > PAGE_SIZE)
  4299. return -E2BIG;
  4300. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4301. if (!req)
  4302. return -ENOMEM;
  4303. ceph_oid_copy(&req->r_base_oid, oid);
  4304. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4305. req->r_flags = flags;
  4306. ret = osd_req_op_cls_init(req, 0, class, method);
  4307. if (ret)
  4308. goto out_put_req;
  4309. if (req_page)
  4310. osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
  4311. 0, false, false);
  4312. if (resp_pages)
  4313. osd_req_op_cls_response_data_pages(req, 0, resp_pages,
  4314. *resp_len, 0, false, false);
  4315. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4316. if (ret)
  4317. goto out_put_req;
  4318. ceph_osdc_start_request(osdc, req, false);
  4319. ret = ceph_osdc_wait_request(osdc, req);
  4320. if (ret >= 0) {
  4321. ret = req->r_ops[0].rval;
  4322. if (resp_pages)
  4323. *resp_len = req->r_ops[0].outdata_len;
  4324. }
  4325. out_put_req:
  4326. ceph_osdc_put_request(req);
  4327. return ret;
  4328. }
  4329. EXPORT_SYMBOL(ceph_osdc_call);
  4330. /*
  4331. * reset all osd connections
  4332. */
  4333. void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
  4334. {
  4335. struct rb_node *n;
  4336. down_write(&osdc->lock);
  4337. for (n = rb_first(&osdc->osds); n; ) {
  4338. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  4339. n = rb_next(n);
  4340. if (!reopen_osd(osd))
  4341. kick_osd_requests(osd);
  4342. }
  4343. up_write(&osdc->lock);
  4344. }
  4345. /*
  4346. * init, shutdown
  4347. */
  4348. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  4349. {
  4350. int err;
  4351. dout("init\n");
  4352. osdc->client = client;
  4353. init_rwsem(&osdc->lock);
  4354. osdc->osds = RB_ROOT;
  4355. INIT_LIST_HEAD(&osdc->osd_lru);
  4356. spin_lock_init(&osdc->osd_lru_lock);
  4357. osd_init(&osdc->homeless_osd);
  4358. osdc->homeless_osd.o_osdc = osdc;
  4359. osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
  4360. osdc->last_linger_id = CEPH_LINGER_ID_START;
  4361. osdc->linger_requests = RB_ROOT;
  4362. osdc->map_checks = RB_ROOT;
  4363. osdc->linger_map_checks = RB_ROOT;
  4364. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  4365. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  4366. err = -ENOMEM;
  4367. osdc->osdmap = ceph_osdmap_alloc();
  4368. if (!osdc->osdmap)
  4369. goto out;
  4370. osdc->req_mempool = mempool_create_slab_pool(10,
  4371. ceph_osd_request_cache);
  4372. if (!osdc->req_mempool)
  4373. goto out_map;
  4374. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  4375. PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
  4376. if (err < 0)
  4377. goto out_mempool;
  4378. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  4379. PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
  4380. "osd_op_reply");
  4381. if (err < 0)
  4382. goto out_msgpool;
  4383. err = -ENOMEM;
  4384. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  4385. if (!osdc->notify_wq)
  4386. goto out_msgpool_reply;
  4387. osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
  4388. if (!osdc->completion_wq)
  4389. goto out_notify_wq;
  4390. schedule_delayed_work(&osdc->timeout_work,
  4391. osdc->client->options->osd_keepalive_timeout);
  4392. schedule_delayed_work(&osdc->osds_timeout_work,
  4393. round_jiffies_relative(osdc->client->options->osd_idle_ttl));
  4394. return 0;
  4395. out_notify_wq:
  4396. destroy_workqueue(osdc->notify_wq);
  4397. out_msgpool_reply:
  4398. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  4399. out_msgpool:
  4400. ceph_msgpool_destroy(&osdc->msgpool_op);
  4401. out_mempool:
  4402. mempool_destroy(osdc->req_mempool);
  4403. out_map:
  4404. ceph_osdmap_destroy(osdc->osdmap);
  4405. out:
  4406. return err;
  4407. }
  4408. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  4409. {
  4410. destroy_workqueue(osdc->completion_wq);
  4411. destroy_workqueue(osdc->notify_wq);
  4412. cancel_delayed_work_sync(&osdc->timeout_work);
  4413. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  4414. down_write(&osdc->lock);
  4415. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  4416. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  4417. struct ceph_osd, o_node);
  4418. close_osd(osd);
  4419. }
  4420. up_write(&osdc->lock);
  4421. WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
  4422. osd_cleanup(&osdc->homeless_osd);
  4423. WARN_ON(!list_empty(&osdc->osd_lru));
  4424. WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
  4425. WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
  4426. WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
  4427. WARN_ON(atomic_read(&osdc->num_requests));
  4428. WARN_ON(atomic_read(&osdc->num_homeless));
  4429. ceph_osdmap_destroy(osdc->osdmap);
  4430. mempool_destroy(osdc->req_mempool);
  4431. ceph_msgpool_destroy(&osdc->msgpool_op);
  4432. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  4433. }
  4434. static int osd_req_op_copy_from_init(struct ceph_osd_request *req,
  4435. u64 src_snapid, u64 src_version,
  4436. struct ceph_object_id *src_oid,
  4437. struct ceph_object_locator *src_oloc,
  4438. u32 src_fadvise_flags,
  4439. u32 dst_fadvise_flags,
  4440. u32 truncate_seq, u64 truncate_size,
  4441. u8 copy_from_flags)
  4442. {
  4443. struct ceph_osd_req_op *op;
  4444. struct page **pages;
  4445. void *p, *end;
  4446. pages = ceph_alloc_page_vector(1, GFP_KERNEL);
  4447. if (IS_ERR(pages))
  4448. return PTR_ERR(pages);
  4449. op = _osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
  4450. dst_fadvise_flags);
  4451. op->copy_from.snapid = src_snapid;
  4452. op->copy_from.src_version = src_version;
  4453. op->copy_from.flags = copy_from_flags;
  4454. op->copy_from.src_fadvise_flags = src_fadvise_flags;
  4455. p = page_address(pages[0]);
  4456. end = p + PAGE_SIZE;
  4457. ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
  4458. encode_oloc(&p, end, src_oloc);
  4459. ceph_encode_32(&p, truncate_seq);
  4460. ceph_encode_64(&p, truncate_size);
  4461. op->indata_len = PAGE_SIZE - (end - p);
  4462. ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
  4463. op->indata_len, 0, false, true);
  4464. return 0;
  4465. }
  4466. int ceph_osdc_copy_from(struct ceph_osd_client *osdc,
  4467. u64 src_snapid, u64 src_version,
  4468. struct ceph_object_id *src_oid,
  4469. struct ceph_object_locator *src_oloc,
  4470. u32 src_fadvise_flags,
  4471. struct ceph_object_id *dst_oid,
  4472. struct ceph_object_locator *dst_oloc,
  4473. u32 dst_fadvise_flags,
  4474. u32 truncate_seq, u64 truncate_size,
  4475. u8 copy_from_flags)
  4476. {
  4477. struct ceph_osd_request *req;
  4478. int ret;
  4479. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
  4480. if (!req)
  4481. return -ENOMEM;
  4482. req->r_flags = CEPH_OSD_FLAG_WRITE;
  4483. ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
  4484. ceph_oid_copy(&req->r_t.base_oid, dst_oid);
  4485. ret = osd_req_op_copy_from_init(req, src_snapid, src_version, src_oid,
  4486. src_oloc, src_fadvise_flags,
  4487. dst_fadvise_flags, truncate_seq,
  4488. truncate_size, copy_from_flags);
  4489. if (ret)
  4490. goto out;
  4491. ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
  4492. if (ret)
  4493. goto out;
  4494. ceph_osdc_start_request(osdc, req, false);
  4495. ret = ceph_osdc_wait_request(osdc, req);
  4496. out:
  4497. ceph_osdc_put_request(req);
  4498. return ret;
  4499. }
  4500. EXPORT_SYMBOL(ceph_osdc_copy_from);
  4501. int __init ceph_osdc_setup(void)
  4502. {
  4503. size_t size = sizeof(struct ceph_osd_request) +
  4504. CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
  4505. BUG_ON(ceph_osd_request_cache);
  4506. ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
  4507. 0, 0, NULL);
  4508. return ceph_osd_request_cache ? 0 : -ENOMEM;
  4509. }
  4510. void ceph_osdc_cleanup(void)
  4511. {
  4512. BUG_ON(!ceph_osd_request_cache);
  4513. kmem_cache_destroy(ceph_osd_request_cache);
  4514. ceph_osd_request_cache = NULL;
  4515. }
  4516. /*
  4517. * handle incoming message
  4518. */
  4519. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  4520. {
  4521. struct ceph_osd *osd = con->private;
  4522. struct ceph_osd_client *osdc = osd->o_osdc;
  4523. int type = le16_to_cpu(msg->hdr.type);
  4524. switch (type) {
  4525. case CEPH_MSG_OSD_MAP:
  4526. ceph_osdc_handle_map(osdc, msg);
  4527. break;
  4528. case CEPH_MSG_OSD_OPREPLY:
  4529. handle_reply(osd, msg);
  4530. break;
  4531. case CEPH_MSG_OSD_BACKOFF:
  4532. handle_backoff(osd, msg);
  4533. break;
  4534. case CEPH_MSG_WATCH_NOTIFY:
  4535. handle_watch_notify(osdc, msg);
  4536. break;
  4537. default:
  4538. pr_err("received unknown message type %d %s\n", type,
  4539. ceph_msg_type_name(type));
  4540. }
  4541. ceph_msg_put(msg);
  4542. }
  4543. /*
  4544. * Lookup and return message for incoming reply. Don't try to do
  4545. * anything about a larger than preallocated data portion of the
  4546. * message at the moment - for now, just skip the message.
  4547. */
  4548. static struct ceph_msg *get_reply(struct ceph_connection *con,
  4549. struct ceph_msg_header *hdr,
  4550. int *skip)
  4551. {
  4552. struct ceph_osd *osd = con->private;
  4553. struct ceph_osd_client *osdc = osd->o_osdc;
  4554. struct ceph_msg *m = NULL;
  4555. struct ceph_osd_request *req;
  4556. int front_len = le32_to_cpu(hdr->front_len);
  4557. int data_len = le32_to_cpu(hdr->data_len);
  4558. u64 tid = le64_to_cpu(hdr->tid);
  4559. down_read(&osdc->lock);
  4560. if (!osd_registered(osd)) {
  4561. dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
  4562. *skip = 1;
  4563. goto out_unlock_osdc;
  4564. }
  4565. WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
  4566. mutex_lock(&osd->lock);
  4567. req = lookup_request(&osd->o_requests, tid);
  4568. if (!req) {
  4569. dout("%s osd%d tid %llu unknown, skipping\n", __func__,
  4570. osd->o_osd, tid);
  4571. *skip = 1;
  4572. goto out_unlock_session;
  4573. }
  4574. ceph_msg_revoke_incoming(req->r_reply);
  4575. if (front_len > req->r_reply->front_alloc_len) {
  4576. pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
  4577. __func__, osd->o_osd, req->r_tid, front_len,
  4578. req->r_reply->front_alloc_len);
  4579. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
  4580. false);
  4581. if (!m)
  4582. goto out_unlock_session;
  4583. ceph_msg_put(req->r_reply);
  4584. req->r_reply = m;
  4585. }
  4586. if (data_len > req->r_reply->data_length) {
  4587. pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
  4588. __func__, osd->o_osd, req->r_tid, data_len,
  4589. req->r_reply->data_length);
  4590. m = NULL;
  4591. *skip = 1;
  4592. goto out_unlock_session;
  4593. }
  4594. m = ceph_msg_get(req->r_reply);
  4595. dout("get_reply tid %lld %p\n", tid, m);
  4596. out_unlock_session:
  4597. mutex_unlock(&osd->lock);
  4598. out_unlock_osdc:
  4599. up_read(&osdc->lock);
  4600. return m;
  4601. }
  4602. static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
  4603. {
  4604. struct ceph_msg *m;
  4605. int type = le16_to_cpu(hdr->type);
  4606. u32 front_len = le32_to_cpu(hdr->front_len);
  4607. u32 data_len = le32_to_cpu(hdr->data_len);
  4608. m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
  4609. if (!m)
  4610. return NULL;
  4611. if (data_len) {
  4612. struct page **pages;
  4613. pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
  4614. GFP_NOIO);
  4615. if (IS_ERR(pages)) {
  4616. ceph_msg_put(m);
  4617. return NULL;
  4618. }
  4619. ceph_msg_data_add_pages(m, pages, data_len, 0, true);
  4620. }
  4621. return m;
  4622. }
  4623. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  4624. struct ceph_msg_header *hdr,
  4625. int *skip)
  4626. {
  4627. struct ceph_osd *osd = con->private;
  4628. int type = le16_to_cpu(hdr->type);
  4629. *skip = 0;
  4630. switch (type) {
  4631. case CEPH_MSG_OSD_MAP:
  4632. case CEPH_MSG_OSD_BACKOFF:
  4633. case CEPH_MSG_WATCH_NOTIFY:
  4634. return alloc_msg_with_page_vector(hdr);
  4635. case CEPH_MSG_OSD_OPREPLY:
  4636. return get_reply(con, hdr, skip);
  4637. default:
  4638. pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
  4639. osd->o_osd, type);
  4640. *skip = 1;
  4641. return NULL;
  4642. }
  4643. }
  4644. /*
  4645. * Wrappers to refcount containing ceph_osd struct
  4646. */
  4647. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  4648. {
  4649. struct ceph_osd *osd = con->private;
  4650. if (get_osd(osd))
  4651. return con;
  4652. return NULL;
  4653. }
  4654. static void put_osd_con(struct ceph_connection *con)
  4655. {
  4656. struct ceph_osd *osd = con->private;
  4657. put_osd(osd);
  4658. }
  4659. /*
  4660. * authentication
  4661. */
  4662. /*
  4663. * Note: returned pointer is the address of a structure that's
  4664. * managed separately. Caller must *not* attempt to free it.
  4665. */
  4666. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  4667. int *proto, int force_new)
  4668. {
  4669. struct ceph_osd *o = con->private;
  4670. struct ceph_osd_client *osdc = o->o_osdc;
  4671. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4672. struct ceph_auth_handshake *auth = &o->o_auth;
  4673. if (force_new && auth->authorizer) {
  4674. ceph_auth_destroy_authorizer(auth->authorizer);
  4675. auth->authorizer = NULL;
  4676. }
  4677. if (!auth->authorizer) {
  4678. int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  4679. auth);
  4680. if (ret)
  4681. return ERR_PTR(ret);
  4682. } else {
  4683. int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  4684. auth);
  4685. if (ret)
  4686. return ERR_PTR(ret);
  4687. }
  4688. *proto = ac->protocol;
  4689. return auth;
  4690. }
  4691. static int add_authorizer_challenge(struct ceph_connection *con,
  4692. void *challenge_buf, int challenge_buf_len)
  4693. {
  4694. struct ceph_osd *o = con->private;
  4695. struct ceph_osd_client *osdc = o->o_osdc;
  4696. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4697. return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
  4698. challenge_buf, challenge_buf_len);
  4699. }
  4700. static int verify_authorizer_reply(struct ceph_connection *con)
  4701. {
  4702. struct ceph_osd *o = con->private;
  4703. struct ceph_osd_client *osdc = o->o_osdc;
  4704. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4705. return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
  4706. }
  4707. static int invalidate_authorizer(struct ceph_connection *con)
  4708. {
  4709. struct ceph_osd *o = con->private;
  4710. struct ceph_osd_client *osdc = o->o_osdc;
  4711. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4712. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  4713. return ceph_monc_validate_auth(&osdc->client->monc);
  4714. }
  4715. static void osd_reencode_message(struct ceph_msg *msg)
  4716. {
  4717. int type = le16_to_cpu(msg->hdr.type);
  4718. if (type == CEPH_MSG_OSD_OP)
  4719. encode_request_finish(msg);
  4720. }
  4721. static int osd_sign_message(struct ceph_msg *msg)
  4722. {
  4723. struct ceph_osd *o = msg->con->private;
  4724. struct ceph_auth_handshake *auth = &o->o_auth;
  4725. return ceph_auth_sign_message(auth, msg);
  4726. }
  4727. static int osd_check_message_signature(struct ceph_msg *msg)
  4728. {
  4729. struct ceph_osd *o = msg->con->private;
  4730. struct ceph_auth_handshake *auth = &o->o_auth;
  4731. return ceph_auth_check_message_signature(auth, msg);
  4732. }
  4733. static const struct ceph_connection_operations osd_con_ops = {
  4734. .get = get_osd_con,
  4735. .put = put_osd_con,
  4736. .dispatch = dispatch,
  4737. .get_authorizer = get_authorizer,
  4738. .add_authorizer_challenge = add_authorizer_challenge,
  4739. .verify_authorizer_reply = verify_authorizer_reply,
  4740. .invalidate_authorizer = invalidate_authorizer,
  4741. .alloc_msg = alloc_msg,
  4742. .reencode_message = osd_reencode_message,
  4743. .sign_message = osd_sign_message,
  4744. .check_message_signature = osd_check_message_signature,
  4745. .fault = osd_fault,
  4746. };