osd_client.c 35 KB

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