osd_client.c 33 KB

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