osd_client.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. #include "ceph_debug.h"
  2. #include <linux/err.h>
  3. #include <linux/highmem.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include "super.h"
  9. #include "osd_client.h"
  10. #include "messenger.h"
  11. #include "decode.h"
  12. #include "auth.h"
  13. const static struct ceph_connection_operations osd_con_ops;
  14. static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  15. /*
  16. * Implement client access to distributed object storage cluster.
  17. *
  18. * All data objects are stored within a cluster/cloud of OSDs, or
  19. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  20. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  21. * remote daemons serving up and coordinating consistent and safe
  22. * access to storage.
  23. *
  24. * Cluster membership and the mapping of data objects onto storage devices
  25. * are described by the osd map.
  26. *
  27. * We keep track of pending OSD requests (read, write), resubmit
  28. * requests to different OSDs when the cluster topology/data layout
  29. * change, or retry the affected requests when the communications
  30. * channel with an OSD is reset.
  31. */
  32. /*
  33. * calculate the mapping of a file extent onto an object, and fill out the
  34. * request accordingly. shorten extent as necessary if it crosses an
  35. * object boundary.
  36. *
  37. * fill osd op in request message.
  38. */
  39. static void calc_layout(struct ceph_osd_client *osdc,
  40. struct ceph_vino vino, struct ceph_file_layout *layout,
  41. u64 off, u64 *plen,
  42. struct ceph_osd_request *req)
  43. {
  44. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  45. struct ceph_osd_op *op = (void *)(reqhead + 1);
  46. u64 orig_len = *plen;
  47. u64 objoff, objlen; /* extent in object */
  48. u64 bno;
  49. reqhead->snapid = cpu_to_le64(vino.snap);
  50. /* object extent? */
  51. ceph_calc_file_object_mapping(layout, off, plen, &bno,
  52. &objoff, &objlen);
  53. if (*plen < orig_len)
  54. dout(" skipping last %llu, final file extent %llu~%llu\n",
  55. orig_len - *plen, off, *plen);
  56. sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
  57. req->r_oid_len = strlen(req->r_oid);
  58. op->extent.offset = cpu_to_le64(objoff);
  59. op->extent.length = cpu_to_le64(objlen);
  60. req->r_num_pages = calc_pages_for(off, *plen);
  61. dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
  62. req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages);
  63. }
  64. /*
  65. * requests
  66. */
  67. void ceph_osdc_release_request(struct kref *kref)
  68. {
  69. struct ceph_osd_request *req = container_of(kref,
  70. struct ceph_osd_request,
  71. r_kref);
  72. if (req->r_request)
  73. ceph_msg_put(req->r_request);
  74. if (req->r_reply)
  75. ceph_msg_put(req->r_reply);
  76. if (req->r_own_pages)
  77. ceph_release_page_vector(req->r_pages,
  78. req->r_num_pages);
  79. ceph_put_snap_context(req->r_snapc);
  80. if (req->r_mempool)
  81. mempool_free(req, req->r_osdc->req_mempool);
  82. else
  83. kfree(req);
  84. }
  85. /*
  86. * build new request AND message, calculate layout, and adjust file
  87. * extent as needed.
  88. *
  89. * if the file was recently truncated, we include information about its
  90. * old and new size so that the object can be updated appropriately. (we
  91. * avoid synchronously deleting truncated objects because it's slow.)
  92. *
  93. * if @do_sync, include a 'startsync' command so that the osd will flush
  94. * data quickly.
  95. */
  96. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  97. struct ceph_file_layout *layout,
  98. struct ceph_vino vino,
  99. u64 off, u64 *plen,
  100. int opcode, int flags,
  101. struct ceph_snap_context *snapc,
  102. int do_sync,
  103. u32 truncate_seq,
  104. u64 truncate_size,
  105. struct timespec *mtime,
  106. bool use_mempool, int num_reply)
  107. {
  108. struct ceph_osd_request *req;
  109. struct ceph_msg *msg;
  110. struct ceph_osd_request_head *head;
  111. struct ceph_osd_op *op;
  112. void *p;
  113. int do_trunc = truncate_seq && (off + *plen > truncate_size);
  114. int num_op = 1 + do_sync + do_trunc;
  115. size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
  116. int err, i;
  117. u64 prevofs;
  118. if (use_mempool) {
  119. req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
  120. memset(req, 0, sizeof(*req));
  121. } else {
  122. req = kzalloc(sizeof(*req), GFP_NOFS);
  123. }
  124. if (req == NULL)
  125. return ERR_PTR(-ENOMEM);
  126. err = ceph_msgpool_resv(&osdc->msgpool_op_reply, num_reply);
  127. if (err) {
  128. ceph_osdc_put_request(req);
  129. return ERR_PTR(-ENOMEM);
  130. }
  131. req->r_osdc = osdc;
  132. req->r_mempool = use_mempool;
  133. kref_init(&req->r_kref);
  134. init_completion(&req->r_completion);
  135. init_completion(&req->r_safe_completion);
  136. INIT_LIST_HEAD(&req->r_unsafe_item);
  137. req->r_flags = flags;
  138. WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
  139. /* create message; allow space for oid */
  140. msg_size += 40;
  141. if (snapc)
  142. msg_size += sizeof(u64) * snapc->num_snaps;
  143. if (use_mempool)
  144. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  145. else
  146. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
  147. if (IS_ERR(msg)) {
  148. ceph_msgpool_resv(&osdc->msgpool_op_reply, num_reply);
  149. ceph_osdc_put_request(req);
  150. return ERR_PTR(PTR_ERR(msg));
  151. }
  152. msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
  153. memset(msg->front.iov_base, 0, msg->front.iov_len);
  154. head = msg->front.iov_base;
  155. op = (void *)(head + 1);
  156. p = (void *)(op + num_op);
  157. req->r_request = msg;
  158. req->r_snapc = ceph_get_snap_context(snapc);
  159. head->client_inc = cpu_to_le32(1); /* always, for now. */
  160. head->flags = cpu_to_le32(flags);
  161. if (flags & CEPH_OSD_FLAG_WRITE)
  162. ceph_encode_timespec(&head->mtime, mtime);
  163. head->num_ops = cpu_to_le16(num_op);
  164. op->op = cpu_to_le16(opcode);
  165. /* calculate max write size */
  166. calc_layout(osdc, vino, layout, off, plen, req);
  167. req->r_file_layout = *layout; /* keep a copy */
  168. if (flags & CEPH_OSD_FLAG_WRITE) {
  169. req->r_request->hdr.data_off = cpu_to_le16(off);
  170. req->r_request->hdr.data_len = cpu_to_le32(*plen);
  171. op->payload_len = cpu_to_le32(*plen);
  172. }
  173. /* fill in oid */
  174. head->object_len = cpu_to_le32(req->r_oid_len);
  175. memcpy(p, req->r_oid, req->r_oid_len);
  176. p += req->r_oid_len;
  177. /* additional ops */
  178. if (do_trunc) {
  179. op++;
  180. op->op = cpu_to_le16(opcode == CEPH_OSD_OP_READ ?
  181. CEPH_OSD_OP_MASKTRUNC : CEPH_OSD_OP_SETTRUNC);
  182. op->trunc.truncate_seq = cpu_to_le32(truncate_seq);
  183. prevofs = le64_to_cpu((op-1)->extent.offset);
  184. op->trunc.truncate_size = cpu_to_le64(truncate_size -
  185. (off-prevofs));
  186. }
  187. if (do_sync) {
  188. op++;
  189. op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
  190. }
  191. if (snapc) {
  192. head->snap_seq = cpu_to_le64(snapc->seq);
  193. head->num_snaps = cpu_to_le32(snapc->num_snaps);
  194. for (i = 0; i < snapc->num_snaps; i++) {
  195. put_unaligned_le64(snapc->snaps[i], p);
  196. p += sizeof(u64);
  197. }
  198. }
  199. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  200. return req;
  201. }
  202. /*
  203. * We keep osd requests in an rbtree, sorted by ->r_tid.
  204. */
  205. static void __insert_request(struct ceph_osd_client *osdc,
  206. struct ceph_osd_request *new)
  207. {
  208. struct rb_node **p = &osdc->requests.rb_node;
  209. struct rb_node *parent = NULL;
  210. struct ceph_osd_request *req = NULL;
  211. while (*p) {
  212. parent = *p;
  213. req = rb_entry(parent, struct ceph_osd_request, r_node);
  214. if (new->r_tid < req->r_tid)
  215. p = &(*p)->rb_left;
  216. else if (new->r_tid > req->r_tid)
  217. p = &(*p)->rb_right;
  218. else
  219. BUG();
  220. }
  221. rb_link_node(&new->r_node, parent, p);
  222. rb_insert_color(&new->r_node, &osdc->requests);
  223. }
  224. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  225. u64 tid)
  226. {
  227. struct ceph_osd_request *req;
  228. struct rb_node *n = osdc->requests.rb_node;
  229. while (n) {
  230. req = rb_entry(n, struct ceph_osd_request, r_node);
  231. if (tid < req->r_tid)
  232. n = n->rb_left;
  233. else if (tid > req->r_tid)
  234. n = n->rb_right;
  235. else
  236. return req;
  237. }
  238. return NULL;
  239. }
  240. static struct ceph_osd_request *
  241. __lookup_request_ge(struct ceph_osd_client *osdc,
  242. u64 tid)
  243. {
  244. struct ceph_osd_request *req;
  245. struct rb_node *n = osdc->requests.rb_node;
  246. while (n) {
  247. req = rb_entry(n, struct ceph_osd_request, r_node);
  248. if (tid < req->r_tid) {
  249. if (!n->rb_left)
  250. return req;
  251. n = n->rb_left;
  252. } else if (tid > req->r_tid) {
  253. n = n->rb_right;
  254. } else {
  255. return req;
  256. }
  257. }
  258. return NULL;
  259. }
  260. /*
  261. * If the osd connection drops, we need to resubmit all requests.
  262. */
  263. static void osd_reset(struct ceph_connection *con)
  264. {
  265. struct ceph_osd *osd = con->private;
  266. struct ceph_osd_client *osdc;
  267. if (!osd)
  268. return;
  269. dout("osd_reset osd%d\n", osd->o_osd);
  270. osdc = osd->o_osdc;
  271. osd->o_incarnation++;
  272. down_read(&osdc->map_sem);
  273. kick_requests(osdc, osd);
  274. up_read(&osdc->map_sem);
  275. }
  276. /*
  277. * Track open sessions with osds.
  278. */
  279. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
  280. {
  281. struct ceph_osd *osd;
  282. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  283. if (!osd)
  284. return NULL;
  285. atomic_set(&osd->o_ref, 1);
  286. osd->o_osdc = osdc;
  287. INIT_LIST_HEAD(&osd->o_requests);
  288. osd->o_incarnation = 1;
  289. ceph_con_init(osdc->client->msgr, &osd->o_con);
  290. osd->o_con.private = osd;
  291. osd->o_con.ops = &osd_con_ops;
  292. osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
  293. return osd;
  294. }
  295. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  296. {
  297. if (atomic_inc_not_zero(&osd->o_ref)) {
  298. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  299. atomic_read(&osd->o_ref));
  300. return osd;
  301. } else {
  302. dout("get_osd %p FAIL\n", osd);
  303. return NULL;
  304. }
  305. }
  306. static void put_osd(struct ceph_osd *osd)
  307. {
  308. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  309. atomic_read(&osd->o_ref) - 1);
  310. if (atomic_dec_and_test(&osd->o_ref))
  311. kfree(osd);
  312. }
  313. /*
  314. * remove an osd from our map
  315. */
  316. static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  317. {
  318. dout("remove_osd %p\n", osd);
  319. BUG_ON(!list_empty(&osd->o_requests));
  320. rb_erase(&osd->o_node, &osdc->osds);
  321. ceph_con_close(&osd->o_con);
  322. put_osd(osd);
  323. }
  324. /*
  325. * reset osd connect
  326. */
  327. static int reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  328. {
  329. int ret = 0;
  330. dout("reset_osd %p osd%d\n", osd, osd->o_osd);
  331. if (list_empty(&osd->o_requests)) {
  332. remove_osd(osdc, osd);
  333. } else {
  334. ceph_con_close(&osd->o_con);
  335. ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
  336. osd->o_incarnation++;
  337. }
  338. return ret;
  339. }
  340. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  341. {
  342. struct rb_node **p = &osdc->osds.rb_node;
  343. struct rb_node *parent = NULL;
  344. struct ceph_osd *osd = NULL;
  345. while (*p) {
  346. parent = *p;
  347. osd = rb_entry(parent, struct ceph_osd, o_node);
  348. if (new->o_osd < osd->o_osd)
  349. p = &(*p)->rb_left;
  350. else if (new->o_osd > osd->o_osd)
  351. p = &(*p)->rb_right;
  352. else
  353. BUG();
  354. }
  355. rb_link_node(&new->o_node, parent, p);
  356. rb_insert_color(&new->o_node, &osdc->osds);
  357. }
  358. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  359. {
  360. struct ceph_osd *osd;
  361. struct rb_node *n = osdc->osds.rb_node;
  362. while (n) {
  363. osd = rb_entry(n, struct ceph_osd, o_node);
  364. if (o < osd->o_osd)
  365. n = n->rb_left;
  366. else if (o > osd->o_osd)
  367. n = n->rb_right;
  368. else
  369. return osd;
  370. }
  371. return NULL;
  372. }
  373. /*
  374. * Register request, assign tid. If this is the first request, set up
  375. * the timeout event.
  376. */
  377. static void register_request(struct ceph_osd_client *osdc,
  378. struct ceph_osd_request *req)
  379. {
  380. struct ceph_osd_request_head *head = req->r_request->front.iov_base;
  381. mutex_lock(&osdc->request_mutex);
  382. req->r_tid = ++osdc->last_tid;
  383. head->tid = cpu_to_le64(req->r_tid);
  384. dout("register_request %p tid %lld\n", req, req->r_tid);
  385. __insert_request(osdc, req);
  386. ceph_osdc_get_request(req);
  387. osdc->num_requests++;
  388. req->r_timeout_stamp =
  389. jiffies + osdc->client->mount_args->osd_timeout*HZ;
  390. if (osdc->num_requests == 1) {
  391. osdc->timeout_tid = req->r_tid;
  392. dout(" timeout on tid %llu at %lu\n", req->r_tid,
  393. req->r_timeout_stamp);
  394. schedule_delayed_work(&osdc->timeout_work,
  395. round_jiffies_relative(req->r_timeout_stamp - jiffies));
  396. }
  397. mutex_unlock(&osdc->request_mutex);
  398. }
  399. /*
  400. * called under osdc->request_mutex
  401. */
  402. static void __unregister_request(struct ceph_osd_client *osdc,
  403. struct ceph_osd_request *req)
  404. {
  405. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  406. rb_erase(&req->r_node, &osdc->requests);
  407. osdc->num_requests--;
  408. if (req->r_osd) {
  409. /* make sure the original request isn't in flight. */
  410. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  411. list_del_init(&req->r_osd_item);
  412. if (list_empty(&req->r_osd->o_requests))
  413. remove_osd(osdc, req->r_osd);
  414. req->r_osd = NULL;
  415. }
  416. ceph_osdc_put_request(req);
  417. if (req->r_tid == osdc->timeout_tid) {
  418. if (osdc->num_requests == 0) {
  419. dout("no requests, canceling timeout\n");
  420. osdc->timeout_tid = 0;
  421. cancel_delayed_work(&osdc->timeout_work);
  422. } else {
  423. req = rb_entry(rb_first(&osdc->requests),
  424. struct ceph_osd_request, r_node);
  425. osdc->timeout_tid = req->r_tid;
  426. dout("rescheduled timeout on tid %llu at %lu\n",
  427. req->r_tid, req->r_timeout_stamp);
  428. schedule_delayed_work(&osdc->timeout_work,
  429. round_jiffies_relative(req->r_timeout_stamp -
  430. jiffies));
  431. }
  432. }
  433. }
  434. /*
  435. * Cancel a previously queued request message
  436. */
  437. static void __cancel_request(struct ceph_osd_request *req)
  438. {
  439. if (req->r_sent) {
  440. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  441. req->r_sent = 0;
  442. }
  443. }
  444. /*
  445. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  446. * (as needed), and set the request r_osd appropriately. If there is
  447. * no up osd, set r_osd to NULL.
  448. *
  449. * Return 0 if unchanged, 1 if changed, or negative on error.
  450. *
  451. * Caller should hold map_sem for read and request_mutex.
  452. */
  453. static int __map_osds(struct ceph_osd_client *osdc,
  454. struct ceph_osd_request *req)
  455. {
  456. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  457. struct ceph_pg pgid;
  458. int o = -1;
  459. int err;
  460. struct ceph_osd *newosd = NULL;
  461. dout("map_osds %p tid %lld\n", req, req->r_tid);
  462. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  463. &req->r_file_layout, osdc->osdmap);
  464. if (err)
  465. return err;
  466. pgid = reqhead->layout.ol_pgid;
  467. o = ceph_calc_pg_primary(osdc->osdmap, pgid);
  468. if ((req->r_osd && req->r_osd->o_osd == o &&
  469. req->r_sent >= req->r_osd->o_incarnation) ||
  470. (req->r_osd == NULL && o == -1))
  471. return 0; /* no change */
  472. dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
  473. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  474. req->r_osd ? req->r_osd->o_osd : -1);
  475. if (req->r_osd) {
  476. __cancel_request(req);
  477. list_del_init(&req->r_osd_item);
  478. if (list_empty(&req->r_osd->o_requests)) {
  479. /* try to re-use r_osd if possible */
  480. newosd = get_osd(req->r_osd);
  481. remove_osd(osdc, newosd);
  482. }
  483. req->r_osd = NULL;
  484. }
  485. req->r_osd = __lookup_osd(osdc, o);
  486. if (!req->r_osd && o >= 0) {
  487. if (newosd) {
  488. req->r_osd = newosd;
  489. newosd = NULL;
  490. } else {
  491. err = -ENOMEM;
  492. req->r_osd = create_osd(osdc);
  493. if (!req->r_osd)
  494. goto out;
  495. }
  496. dout("map_osds osd %p is osd%d\n", req->r_osd, o);
  497. req->r_osd->o_osd = o;
  498. req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
  499. __insert_osd(osdc, req->r_osd);
  500. ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
  501. }
  502. if (req->r_osd)
  503. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  504. err = 1; /* osd changed */
  505. out:
  506. if (newosd)
  507. put_osd(newosd);
  508. return err;
  509. }
  510. /*
  511. * caller should hold map_sem (for read) and request_mutex
  512. */
  513. static int __send_request(struct ceph_osd_client *osdc,
  514. struct ceph_osd_request *req)
  515. {
  516. struct ceph_osd_request_head *reqhead;
  517. int err;
  518. err = __map_osds(osdc, req);
  519. if (err < 0)
  520. return err;
  521. if (req->r_osd == NULL) {
  522. dout("send_request %p no up osds in pg\n", req);
  523. ceph_monc_request_next_osdmap(&osdc->client->monc);
  524. return 0;
  525. }
  526. dout("send_request %p tid %llu to osd%d flags %d\n",
  527. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  528. reqhead = req->r_request->front.iov_base;
  529. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  530. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  531. reqhead->reassert_version = req->r_reassert_version;
  532. req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
  533. ceph_msg_get(req->r_request); /* send consumes a ref */
  534. ceph_con_send(&req->r_osd->o_con, req->r_request);
  535. req->r_sent = req->r_osd->o_incarnation;
  536. return 0;
  537. }
  538. /*
  539. * Timeout callback, called every N seconds when 1 or more osd
  540. * requests has been active for more than N seconds. When this
  541. * happens, we ping all OSDs with requests who have timed out to
  542. * ensure any communications channel reset is detected. Reset the
  543. * request timeouts another N seconds in the future as we go.
  544. * Reschedule the timeout event another N seconds in future (unless
  545. * there are no open requests).
  546. */
  547. static void handle_timeout(struct work_struct *work)
  548. {
  549. struct ceph_osd_client *osdc =
  550. container_of(work, struct ceph_osd_client, timeout_work.work);
  551. struct ceph_osd_request *req;
  552. struct ceph_osd *osd;
  553. unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
  554. unsigned long next_timeout = timeout + jiffies;
  555. struct rb_node *p;
  556. dout("timeout\n");
  557. down_read(&osdc->map_sem);
  558. ceph_monc_request_next_osdmap(&osdc->client->monc);
  559. mutex_lock(&osdc->request_mutex);
  560. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  561. req = rb_entry(p, struct ceph_osd_request, r_node);
  562. if (req->r_resend) {
  563. int err;
  564. dout("osdc resending prev failed %lld\n", req->r_tid);
  565. err = __send_request(osdc, req);
  566. if (err)
  567. dout("osdc failed again on %lld\n", req->r_tid);
  568. else
  569. req->r_resend = false;
  570. continue;
  571. }
  572. }
  573. for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
  574. osd = rb_entry(p, struct ceph_osd, o_node);
  575. if (list_empty(&osd->o_requests))
  576. continue;
  577. req = list_first_entry(&osd->o_requests,
  578. struct ceph_osd_request, r_osd_item);
  579. if (time_before(jiffies, req->r_timeout_stamp))
  580. continue;
  581. dout(" tid %llu (at least) timed out on osd%d\n",
  582. req->r_tid, osd->o_osd);
  583. req->r_timeout_stamp = next_timeout;
  584. ceph_con_keepalive(&osd->o_con);
  585. }
  586. if (osdc->timeout_tid)
  587. schedule_delayed_work(&osdc->timeout_work,
  588. round_jiffies_relative(timeout));
  589. mutex_unlock(&osdc->request_mutex);
  590. up_read(&osdc->map_sem);
  591. }
  592. /*
  593. * handle osd op reply. either call the callback if it is specified,
  594. * or do the completion to wake up the waiting thread.
  595. */
  596. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  597. {
  598. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  599. struct ceph_osd_request *req;
  600. u64 tid;
  601. int numops, object_len, flags;
  602. if (msg->front.iov_len < sizeof(*rhead))
  603. goto bad;
  604. tid = le64_to_cpu(rhead->tid);
  605. numops = le32_to_cpu(rhead->num_ops);
  606. object_len = le32_to_cpu(rhead->object_len);
  607. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  608. numops * sizeof(struct ceph_osd_op))
  609. goto bad;
  610. dout("handle_reply %p tid %llu\n", msg, tid);
  611. /* lookup */
  612. mutex_lock(&osdc->request_mutex);
  613. req = __lookup_request(osdc, tid);
  614. if (req == NULL) {
  615. dout("handle_reply tid %llu dne\n", tid);
  616. mutex_unlock(&osdc->request_mutex);
  617. return;
  618. }
  619. ceph_osdc_get_request(req);
  620. flags = le32_to_cpu(rhead->flags);
  621. if (req->r_reply) {
  622. /*
  623. * once we see the message has been received, we don't
  624. * need a ref (which is only needed for revoking
  625. * pages)
  626. */
  627. ceph_msg_put(req->r_reply);
  628. req->r_reply = NULL;
  629. }
  630. if (!req->r_got_reply) {
  631. unsigned bytes;
  632. req->r_result = le32_to_cpu(rhead->result);
  633. bytes = le32_to_cpu(msg->hdr.data_len);
  634. dout("handle_reply result %d bytes %d\n", req->r_result,
  635. bytes);
  636. if (req->r_result == 0)
  637. req->r_result = bytes;
  638. /* in case this is a write and we need to replay, */
  639. req->r_reassert_version = rhead->reassert_version;
  640. req->r_got_reply = 1;
  641. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  642. dout("handle_reply tid %llu dup ack\n", tid);
  643. mutex_unlock(&osdc->request_mutex);
  644. goto done;
  645. }
  646. dout("handle_reply tid %llu flags %d\n", tid, flags);
  647. /* either this is a read, or we got the safe response */
  648. if ((flags & CEPH_OSD_FLAG_ONDISK) ||
  649. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  650. __unregister_request(osdc, req);
  651. mutex_unlock(&osdc->request_mutex);
  652. if (req->r_callback)
  653. req->r_callback(req, msg);
  654. else
  655. complete(&req->r_completion);
  656. if (flags & CEPH_OSD_FLAG_ONDISK) {
  657. if (req->r_safe_callback)
  658. req->r_safe_callback(req, msg);
  659. complete(&req->r_safe_completion); /* fsync waiter */
  660. }
  661. done:
  662. ceph_osdc_put_request(req);
  663. return;
  664. bad:
  665. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  666. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  667. (int)sizeof(*rhead));
  668. }
  669. /*
  670. * Resubmit osd requests whose osd or osd address has changed. Request
  671. * a new osd map if osds are down, or we are otherwise unable to determine
  672. * how to direct a request.
  673. *
  674. * Close connections to down osds.
  675. *
  676. * If @who is specified, resubmit requests for that specific osd.
  677. *
  678. * Caller should hold map_sem for read and request_mutex.
  679. */
  680. static void kick_requests(struct ceph_osd_client *osdc,
  681. struct ceph_osd *kickosd)
  682. {
  683. struct ceph_osd_request *req;
  684. struct rb_node *p, *n;
  685. int needmap = 0;
  686. int err;
  687. dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
  688. mutex_lock(&osdc->request_mutex);
  689. if (!kickosd) {
  690. for (p = rb_first(&osdc->osds); p; p = n) {
  691. struct ceph_osd *osd =
  692. rb_entry(p, struct ceph_osd, o_node);
  693. n = rb_next(p);
  694. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  695. !ceph_entity_addr_equal(&osd->o_con.peer_addr,
  696. ceph_osd_addr(osdc->osdmap,
  697. osd->o_osd)))
  698. reset_osd(osdc, osd);
  699. }
  700. }
  701. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  702. req = rb_entry(p, struct ceph_osd_request, r_node);
  703. if (req->r_resend) {
  704. dout(" r_resend set on tid %llu\n", req->r_tid);
  705. __cancel_request(req);
  706. goto kick;
  707. }
  708. if (req->r_osd && kickosd == req->r_osd) {
  709. __cancel_request(req);
  710. goto kick;
  711. }
  712. err = __map_osds(osdc, req);
  713. if (err == 0)
  714. continue; /* no change */
  715. if (err < 0) {
  716. /*
  717. * FIXME: really, we should set the request
  718. * error and fail if this isn't a 'nofail'
  719. * request, but that's a fair bit more
  720. * complicated to do. So retry!
  721. */
  722. dout(" setting r_resend on %llu\n", req->r_tid);
  723. req->r_resend = true;
  724. continue;
  725. }
  726. if (req->r_osd == NULL) {
  727. dout("tid %llu maps to no valid osd\n", req->r_tid);
  728. needmap++; /* request a newer map */
  729. continue;
  730. }
  731. kick:
  732. dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
  733. req->r_osd->o_osd);
  734. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  735. err = __send_request(osdc, req);
  736. if (err) {
  737. dout(" setting r_resend on %llu\n", req->r_tid);
  738. req->r_resend = true;
  739. }
  740. }
  741. mutex_unlock(&osdc->request_mutex);
  742. if (needmap) {
  743. dout("%d requests for down osds, need new map\n", needmap);
  744. ceph_monc_request_next_osdmap(&osdc->client->monc);
  745. }
  746. }
  747. /*
  748. * Process updated osd map.
  749. *
  750. * The message contains any number of incremental and full maps, normally
  751. * indicating some sort of topology change in the cluster. Kick requests
  752. * off to different OSDs as needed.
  753. */
  754. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  755. {
  756. void *p, *end, *next;
  757. u32 nr_maps, maplen;
  758. u32 epoch;
  759. struct ceph_osdmap *newmap = NULL, *oldmap;
  760. int err;
  761. struct ceph_fsid fsid;
  762. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  763. p = msg->front.iov_base;
  764. end = p + msg->front.iov_len;
  765. /* verify fsid */
  766. ceph_decode_need(&p, end, sizeof(fsid), bad);
  767. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  768. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  769. return;
  770. down_write(&osdc->map_sem);
  771. /* incremental maps */
  772. ceph_decode_32_safe(&p, end, nr_maps, bad);
  773. dout(" %d inc maps\n", nr_maps);
  774. while (nr_maps > 0) {
  775. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  776. epoch = ceph_decode_32(&p);
  777. maplen = ceph_decode_32(&p);
  778. ceph_decode_need(&p, end, maplen, bad);
  779. next = p + maplen;
  780. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  781. dout("applying incremental map %u len %d\n",
  782. epoch, maplen);
  783. newmap = osdmap_apply_incremental(&p, next,
  784. osdc->osdmap,
  785. osdc->client->msgr);
  786. if (IS_ERR(newmap)) {
  787. err = PTR_ERR(newmap);
  788. goto bad;
  789. }
  790. if (newmap != osdc->osdmap) {
  791. ceph_osdmap_destroy(osdc->osdmap);
  792. osdc->osdmap = newmap;
  793. }
  794. } else {
  795. dout("ignoring incremental map %u len %d\n",
  796. epoch, maplen);
  797. }
  798. p = next;
  799. nr_maps--;
  800. }
  801. if (newmap)
  802. goto done;
  803. /* full maps */
  804. ceph_decode_32_safe(&p, end, nr_maps, bad);
  805. dout(" %d full maps\n", nr_maps);
  806. while (nr_maps) {
  807. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  808. epoch = ceph_decode_32(&p);
  809. maplen = ceph_decode_32(&p);
  810. ceph_decode_need(&p, end, maplen, bad);
  811. if (nr_maps > 1) {
  812. dout("skipping non-latest full map %u len %d\n",
  813. epoch, maplen);
  814. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  815. dout("skipping full map %u len %d, "
  816. "older than our %u\n", epoch, maplen,
  817. osdc->osdmap->epoch);
  818. } else {
  819. dout("taking full map %u len %d\n", epoch, maplen);
  820. newmap = osdmap_decode(&p, p+maplen);
  821. if (IS_ERR(newmap)) {
  822. err = PTR_ERR(newmap);
  823. goto bad;
  824. }
  825. oldmap = osdc->osdmap;
  826. osdc->osdmap = newmap;
  827. if (oldmap)
  828. ceph_osdmap_destroy(oldmap);
  829. }
  830. p += maplen;
  831. nr_maps--;
  832. }
  833. done:
  834. downgrade_write(&osdc->map_sem);
  835. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  836. if (newmap)
  837. kick_requests(osdc, NULL);
  838. up_read(&osdc->map_sem);
  839. return;
  840. bad:
  841. pr_err("osdc handle_map corrupt msg\n");
  842. up_write(&osdc->map_sem);
  843. return;
  844. }
  845. /*
  846. * A read request prepares specific pages that data is to be read into.
  847. * When a message is being read off the wire, we call prepare_pages to
  848. * find those pages.
  849. * 0 = success, -1 failure.
  850. */
  851. static int prepare_pages(struct ceph_connection *con, struct ceph_msg *m,
  852. int want)
  853. {
  854. struct ceph_osd *osd = con->private;
  855. struct ceph_osd_client *osdc;
  856. struct ceph_osd_reply_head *rhead = m->front.iov_base;
  857. struct ceph_osd_request *req;
  858. u64 tid;
  859. int ret = -1;
  860. int type = le16_to_cpu(m->hdr.type);
  861. if (!osd)
  862. return -1;
  863. osdc = osd->o_osdc;
  864. dout("prepare_pages on msg %p want %d\n", m, want);
  865. if (unlikely(type != CEPH_MSG_OSD_OPREPLY))
  866. return -1; /* hmm! */
  867. tid = le64_to_cpu(rhead->tid);
  868. mutex_lock(&osdc->request_mutex);
  869. req = __lookup_request(osdc, tid);
  870. if (!req) {
  871. dout("prepare_pages unknown tid %llu\n", tid);
  872. goto out;
  873. }
  874. dout("prepare_pages tid %llu has %d pages, want %d\n",
  875. tid, req->r_num_pages, want);
  876. if (likely(req->r_num_pages >= want && !req->r_prepared_pages)) {
  877. m->pages = req->r_pages;
  878. m->nr_pages = req->r_num_pages;
  879. req->r_reply = m; /* only for duration of read over socket */
  880. ceph_msg_get(m);
  881. req->r_prepared_pages = 1;
  882. ret = 0; /* success */
  883. }
  884. out:
  885. mutex_unlock(&osdc->request_mutex);
  886. return ret;
  887. }
  888. /*
  889. * Register request, send initial attempt.
  890. */
  891. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  892. struct ceph_osd_request *req,
  893. bool nofail)
  894. {
  895. int rc = 0;
  896. req->r_request->pages = req->r_pages;
  897. req->r_request->nr_pages = req->r_num_pages;
  898. register_request(osdc, req);
  899. down_read(&osdc->map_sem);
  900. mutex_lock(&osdc->request_mutex);
  901. /*
  902. * a racing kick_requests() may have sent the message for us
  903. * while we dropped request_mutex above, so only send now if
  904. * the request still han't been touched yet.
  905. */
  906. if (req->r_sent == 0) {
  907. rc = __send_request(osdc, req);
  908. if (rc) {
  909. if (nofail) {
  910. dout("osdc_start_request failed send, "
  911. " marking %lld\n", req->r_tid);
  912. req->r_resend = true;
  913. rc = 0;
  914. } else {
  915. __unregister_request(osdc, req);
  916. }
  917. }
  918. }
  919. mutex_unlock(&osdc->request_mutex);
  920. up_read(&osdc->map_sem);
  921. return rc;
  922. }
  923. /*
  924. * wait for a request to complete
  925. */
  926. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  927. struct ceph_osd_request *req)
  928. {
  929. int rc;
  930. rc = wait_for_completion_interruptible(&req->r_completion);
  931. if (rc < 0) {
  932. mutex_lock(&osdc->request_mutex);
  933. __cancel_request(req);
  934. mutex_unlock(&osdc->request_mutex);
  935. dout("wait_request tid %llu timed out\n", req->r_tid);
  936. return rc;
  937. }
  938. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  939. return req->r_result;
  940. }
  941. /*
  942. * sync - wait for all in-flight requests to flush. avoid starvation.
  943. */
  944. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  945. {
  946. struct ceph_osd_request *req;
  947. u64 last_tid, next_tid = 0;
  948. mutex_lock(&osdc->request_mutex);
  949. last_tid = osdc->last_tid;
  950. while (1) {
  951. req = __lookup_request_ge(osdc, next_tid);
  952. if (!req)
  953. break;
  954. if (req->r_tid > last_tid)
  955. break;
  956. next_tid = req->r_tid + 1;
  957. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  958. continue;
  959. ceph_osdc_get_request(req);
  960. mutex_unlock(&osdc->request_mutex);
  961. dout("sync waiting on tid %llu (last is %llu)\n",
  962. req->r_tid, last_tid);
  963. wait_for_completion(&req->r_safe_completion);
  964. mutex_lock(&osdc->request_mutex);
  965. ceph_osdc_put_request(req);
  966. }
  967. mutex_unlock(&osdc->request_mutex);
  968. dout("sync done (thru tid %llu)\n", last_tid);
  969. }
  970. /*
  971. * init, shutdown
  972. */
  973. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  974. {
  975. int err;
  976. dout("init\n");
  977. osdc->client = client;
  978. osdc->osdmap = NULL;
  979. init_rwsem(&osdc->map_sem);
  980. init_completion(&osdc->map_waiters);
  981. osdc->last_requested_map = 0;
  982. mutex_init(&osdc->request_mutex);
  983. osdc->timeout_tid = 0;
  984. osdc->last_tid = 0;
  985. osdc->osds = RB_ROOT;
  986. osdc->requests = RB_ROOT;
  987. osdc->num_requests = 0;
  988. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  989. err = -ENOMEM;
  990. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  991. sizeof(struct ceph_osd_request));
  992. if (!osdc->req_mempool)
  993. goto out;
  994. err = ceph_msgpool_init(&osdc->msgpool_op, 4096, 10, true);
  995. if (err < 0)
  996. goto out_mempool;
  997. err = ceph_msgpool_init(&osdc->msgpool_op_reply, 512, 0, false);
  998. if (err < 0)
  999. goto out_msgpool;
  1000. return 0;
  1001. out_msgpool:
  1002. ceph_msgpool_destroy(&osdc->msgpool_op);
  1003. out_mempool:
  1004. mempool_destroy(osdc->req_mempool);
  1005. out:
  1006. return err;
  1007. }
  1008. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1009. {
  1010. cancel_delayed_work_sync(&osdc->timeout_work);
  1011. if (osdc->osdmap) {
  1012. ceph_osdmap_destroy(osdc->osdmap);
  1013. osdc->osdmap = NULL;
  1014. }
  1015. mempool_destroy(osdc->req_mempool);
  1016. ceph_msgpool_destroy(&osdc->msgpool_op);
  1017. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1018. }
  1019. /*
  1020. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1021. * *plen. Return number of bytes read, or error.
  1022. */
  1023. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1024. struct ceph_vino vino, struct ceph_file_layout *layout,
  1025. u64 off, u64 *plen,
  1026. u32 truncate_seq, u64 truncate_size,
  1027. struct page **pages, int num_pages)
  1028. {
  1029. struct ceph_osd_request *req;
  1030. int rc = 0;
  1031. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1032. vino.snap, off, *plen);
  1033. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1034. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1035. NULL, 0, truncate_seq, truncate_size, NULL,
  1036. false, 1);
  1037. if (IS_ERR(req))
  1038. return PTR_ERR(req);
  1039. /* it may be a short read due to an object boundary */
  1040. req->r_pages = pages;
  1041. num_pages = calc_pages_for(off, *plen);
  1042. req->r_num_pages = num_pages;
  1043. dout("readpages final extent is %llu~%llu (%d pages)\n",
  1044. off, *plen, req->r_num_pages);
  1045. rc = ceph_osdc_start_request(osdc, req, false);
  1046. if (!rc)
  1047. rc = ceph_osdc_wait_request(osdc, req);
  1048. ceph_osdc_put_request(req);
  1049. dout("readpages result %d\n", rc);
  1050. return rc;
  1051. }
  1052. /*
  1053. * do a synchronous write on N pages
  1054. */
  1055. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1056. struct ceph_file_layout *layout,
  1057. struct ceph_snap_context *snapc,
  1058. u64 off, u64 len,
  1059. u32 truncate_seq, u64 truncate_size,
  1060. struct timespec *mtime,
  1061. struct page **pages, int num_pages,
  1062. int flags, int do_sync, bool nofail)
  1063. {
  1064. struct ceph_osd_request *req;
  1065. int rc = 0;
  1066. BUG_ON(vino.snap != CEPH_NOSNAP);
  1067. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1068. CEPH_OSD_OP_WRITE,
  1069. flags | CEPH_OSD_FLAG_ONDISK |
  1070. CEPH_OSD_FLAG_WRITE,
  1071. snapc, do_sync,
  1072. truncate_seq, truncate_size, mtime,
  1073. nofail, 1);
  1074. if (IS_ERR(req))
  1075. return PTR_ERR(req);
  1076. /* it may be a short write due to an object boundary */
  1077. req->r_pages = pages;
  1078. req->r_num_pages = calc_pages_for(off, len);
  1079. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1080. req->r_num_pages);
  1081. rc = ceph_osdc_start_request(osdc, req, nofail);
  1082. if (!rc)
  1083. rc = ceph_osdc_wait_request(osdc, req);
  1084. ceph_osdc_put_request(req);
  1085. if (rc == 0)
  1086. rc = len;
  1087. dout("writepages result %d\n", rc);
  1088. return rc;
  1089. }
  1090. /*
  1091. * handle incoming message
  1092. */
  1093. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1094. {
  1095. struct ceph_osd *osd = con->private;
  1096. struct ceph_osd_client *osdc;
  1097. int type = le16_to_cpu(msg->hdr.type);
  1098. if (!osd)
  1099. return;
  1100. osdc = osd->o_osdc;
  1101. switch (type) {
  1102. case CEPH_MSG_OSD_MAP:
  1103. ceph_osdc_handle_map(osdc, msg);
  1104. break;
  1105. case CEPH_MSG_OSD_OPREPLY:
  1106. handle_reply(osdc, msg);
  1107. break;
  1108. default:
  1109. pr_err("received unknown message type %d %s\n", type,
  1110. ceph_msg_type_name(type));
  1111. }
  1112. ceph_msg_put(msg);
  1113. }
  1114. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1115. struct ceph_msg_header *hdr)
  1116. {
  1117. struct ceph_osd *osd = con->private;
  1118. struct ceph_osd_client *osdc = osd->o_osdc;
  1119. int type = le16_to_cpu(hdr->type);
  1120. int front = le32_to_cpu(hdr->front_len);
  1121. switch (type) {
  1122. case CEPH_MSG_OSD_OPREPLY:
  1123. return ceph_msgpool_get(&osdc->msgpool_op_reply, front);
  1124. }
  1125. return ceph_alloc_msg(con, hdr);
  1126. }
  1127. /*
  1128. * Wrappers to refcount containing ceph_osd struct
  1129. */
  1130. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1131. {
  1132. struct ceph_osd *osd = con->private;
  1133. if (get_osd(osd))
  1134. return con;
  1135. return NULL;
  1136. }
  1137. static void put_osd_con(struct ceph_connection *con)
  1138. {
  1139. struct ceph_osd *osd = con->private;
  1140. put_osd(osd);
  1141. }
  1142. /*
  1143. * authentication
  1144. */
  1145. static int get_authorizer(struct ceph_connection *con,
  1146. void **buf, int *len, int *proto,
  1147. void **reply_buf, int *reply_len, int force_new)
  1148. {
  1149. struct ceph_osd *o = con->private;
  1150. struct ceph_osd_client *osdc = o->o_osdc;
  1151. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1152. int ret = 0;
  1153. if (force_new && o->o_authorizer) {
  1154. ac->ops->destroy_authorizer(ac, o->o_authorizer);
  1155. o->o_authorizer = NULL;
  1156. }
  1157. if (o->o_authorizer == NULL) {
  1158. ret = ac->ops->create_authorizer(
  1159. ac, CEPH_ENTITY_TYPE_OSD,
  1160. &o->o_authorizer,
  1161. &o->o_authorizer_buf,
  1162. &o->o_authorizer_buf_len,
  1163. &o->o_authorizer_reply_buf,
  1164. &o->o_authorizer_reply_buf_len);
  1165. if (ret)
  1166. return ret;
  1167. }
  1168. *proto = ac->protocol;
  1169. *buf = o->o_authorizer_buf;
  1170. *len = o->o_authorizer_buf_len;
  1171. *reply_buf = o->o_authorizer_reply_buf;
  1172. *reply_len = o->o_authorizer_reply_buf_len;
  1173. return 0;
  1174. }
  1175. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1176. {
  1177. struct ceph_osd *o = con->private;
  1178. struct ceph_osd_client *osdc = o->o_osdc;
  1179. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1180. return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
  1181. }
  1182. const static struct ceph_connection_operations osd_con_ops = {
  1183. .get = get_osd_con,
  1184. .put = put_osd_con,
  1185. .dispatch = dispatch,
  1186. .get_authorizer = get_authorizer,
  1187. .verify_authorizer_reply = verify_authorizer_reply,
  1188. .alloc_msg = alloc_msg,
  1189. .fault = osd_reset,
  1190. .alloc_middle = ceph_alloc_middle,
  1191. .prepare_pages = prepare_pages,
  1192. };