osd_client.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/err.h>
  4. #include <linux/highmem.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #ifdef CONFIG_BLOCK
  10. #include <linux/bio.h>
  11. #endif
  12. #include <linux/ceph/libceph.h>
  13. #include <linux/ceph/osd_client.h>
  14. #include <linux/ceph/messenger.h>
  15. #include <linux/ceph/decode.h>
  16. #include <linux/ceph/auth.h>
  17. #include <linux/ceph/pagelist.h>
  18. #define OSD_OP_FRONT_LEN 4096
  19. #define OSD_OPREPLY_FRONT_LEN 512
  20. static const struct ceph_connection_operations osd_con_ops;
  21. static void send_queued(struct ceph_osd_client *osdc);
  22. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  23. static void __register_request(struct ceph_osd_client *osdc,
  24. struct ceph_osd_request *req);
  25. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  26. struct ceph_osd_request *req);
  27. static int __send_request(struct ceph_osd_client *osdc,
  28. struct ceph_osd_request *req);
  29. static int op_needs_trail(int op)
  30. {
  31. switch (op) {
  32. case CEPH_OSD_OP_GETXATTR:
  33. case CEPH_OSD_OP_SETXATTR:
  34. case CEPH_OSD_OP_CMPXATTR:
  35. case CEPH_OSD_OP_CALL:
  36. case CEPH_OSD_OP_NOTIFY:
  37. return 1;
  38. default:
  39. return 0;
  40. }
  41. }
  42. static int op_has_extent(int op)
  43. {
  44. return (op == CEPH_OSD_OP_READ ||
  45. op == CEPH_OSD_OP_WRITE);
  46. }
  47. void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
  48. struct ceph_file_layout *layout,
  49. u64 snapid,
  50. u64 off, u64 *plen, u64 *bno,
  51. struct ceph_osd_request *req,
  52. struct ceph_osd_req_op *op)
  53. {
  54. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  55. u64 orig_len = *plen;
  56. u64 objoff, objlen; /* extent in object */
  57. reqhead->snapid = cpu_to_le64(snapid);
  58. /* object extent? */
  59. ceph_calc_file_object_mapping(layout, off, plen, bno,
  60. &objoff, &objlen);
  61. if (*plen < orig_len)
  62. dout(" skipping last %llu, final file extent %llu~%llu\n",
  63. orig_len - *plen, off, *plen);
  64. if (op_has_extent(op->op)) {
  65. op->extent.offset = objoff;
  66. op->extent.length = objlen;
  67. }
  68. req->r_num_pages = calc_pages_for(off, *plen);
  69. req->r_page_alignment = off & ~PAGE_MASK;
  70. if (op->op == CEPH_OSD_OP_WRITE)
  71. op->payload_len = *plen;
  72. dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
  73. *bno, objoff, objlen, req->r_num_pages);
  74. }
  75. EXPORT_SYMBOL(ceph_calc_raw_layout);
  76. /*
  77. * Implement client access to distributed object storage cluster.
  78. *
  79. * All data objects are stored within a cluster/cloud of OSDs, or
  80. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  81. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  82. * remote daemons serving up and coordinating consistent and safe
  83. * access to storage.
  84. *
  85. * Cluster membership and the mapping of data objects onto storage devices
  86. * are described by the osd map.
  87. *
  88. * We keep track of pending OSD requests (read, write), resubmit
  89. * requests to different OSDs when the cluster topology/data layout
  90. * change, or retry the affected requests when the communications
  91. * channel with an OSD is reset.
  92. */
  93. /*
  94. * calculate the mapping of a file extent onto an object, and fill out the
  95. * request accordingly. shorten extent as necessary if it crosses an
  96. * object boundary.
  97. *
  98. * fill osd op in request message.
  99. */
  100. static void calc_layout(struct ceph_osd_client *osdc,
  101. struct ceph_vino vino,
  102. struct ceph_file_layout *layout,
  103. u64 off, u64 *plen,
  104. struct ceph_osd_request *req,
  105. struct ceph_osd_req_op *op)
  106. {
  107. u64 bno;
  108. ceph_calc_raw_layout(osdc, layout, vino.snap, off,
  109. plen, &bno, req, op);
  110. sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
  111. req->r_oid_len = strlen(req->r_oid);
  112. }
  113. /*
  114. * requests
  115. */
  116. void ceph_osdc_release_request(struct kref *kref)
  117. {
  118. struct ceph_osd_request *req = container_of(kref,
  119. struct ceph_osd_request,
  120. r_kref);
  121. if (req->r_request)
  122. ceph_msg_put(req->r_request);
  123. if (req->r_reply)
  124. ceph_msg_put(req->r_reply);
  125. if (req->r_con_filling_msg) {
  126. dout("release_request revoking pages %p from con %p\n",
  127. req->r_pages, req->r_con_filling_msg);
  128. ceph_con_revoke_message(req->r_con_filling_msg,
  129. req->r_reply);
  130. ceph_con_put(req->r_con_filling_msg);
  131. }
  132. if (req->r_own_pages)
  133. ceph_release_page_vector(req->r_pages,
  134. req->r_num_pages);
  135. #ifdef CONFIG_BLOCK
  136. if (req->r_bio)
  137. bio_put(req->r_bio);
  138. #endif
  139. ceph_put_snap_context(req->r_snapc);
  140. if (req->r_trail) {
  141. ceph_pagelist_release(req->r_trail);
  142. kfree(req->r_trail);
  143. }
  144. if (req->r_mempool)
  145. mempool_free(req, req->r_osdc->req_mempool);
  146. else
  147. kfree(req);
  148. }
  149. EXPORT_SYMBOL(ceph_osdc_release_request);
  150. static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
  151. {
  152. int i = 0;
  153. if (needs_trail)
  154. *needs_trail = 0;
  155. while (ops[i].op) {
  156. if (needs_trail && op_needs_trail(ops[i].op))
  157. *needs_trail = 1;
  158. i++;
  159. }
  160. return i;
  161. }
  162. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  163. int flags,
  164. struct ceph_snap_context *snapc,
  165. struct ceph_osd_req_op *ops,
  166. bool use_mempool,
  167. gfp_t gfp_flags,
  168. struct page **pages,
  169. struct bio *bio)
  170. {
  171. struct ceph_osd_request *req;
  172. struct ceph_msg *msg;
  173. int needs_trail;
  174. int num_op = get_num_ops(ops, &needs_trail);
  175. size_t msg_size = sizeof(struct ceph_osd_request_head);
  176. msg_size += num_op*sizeof(struct ceph_osd_op);
  177. if (use_mempool) {
  178. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  179. memset(req, 0, sizeof(*req));
  180. } else {
  181. req = kzalloc(sizeof(*req), gfp_flags);
  182. }
  183. if (req == NULL)
  184. return NULL;
  185. req->r_osdc = osdc;
  186. req->r_mempool = use_mempool;
  187. kref_init(&req->r_kref);
  188. init_completion(&req->r_completion);
  189. init_completion(&req->r_safe_completion);
  190. INIT_LIST_HEAD(&req->r_unsafe_item);
  191. INIT_LIST_HEAD(&req->r_linger_item);
  192. INIT_LIST_HEAD(&req->r_linger_osd);
  193. req->r_flags = flags;
  194. WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
  195. /* create reply message */
  196. if (use_mempool)
  197. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
  198. else
  199. msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
  200. OSD_OPREPLY_FRONT_LEN, gfp_flags);
  201. if (!msg) {
  202. ceph_osdc_put_request(req);
  203. return NULL;
  204. }
  205. req->r_reply = msg;
  206. /* allocate space for the trailing data */
  207. if (needs_trail) {
  208. req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
  209. if (!req->r_trail) {
  210. ceph_osdc_put_request(req);
  211. return NULL;
  212. }
  213. ceph_pagelist_init(req->r_trail);
  214. }
  215. /* create request message; allow space for oid */
  216. msg_size += 40;
  217. if (snapc)
  218. msg_size += sizeof(u64) * snapc->num_snaps;
  219. if (use_mempool)
  220. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  221. else
  222. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags);
  223. if (!msg) {
  224. ceph_osdc_put_request(req);
  225. return NULL;
  226. }
  227. msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
  228. memset(msg->front.iov_base, 0, msg->front.iov_len);
  229. req->r_request = msg;
  230. req->r_pages = pages;
  231. #ifdef CONFIG_BLOCK
  232. if (bio) {
  233. req->r_bio = bio;
  234. bio_get(req->r_bio);
  235. }
  236. #endif
  237. return req;
  238. }
  239. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  240. static void osd_req_encode_op(struct ceph_osd_request *req,
  241. struct ceph_osd_op *dst,
  242. struct ceph_osd_req_op *src)
  243. {
  244. dst->op = cpu_to_le16(src->op);
  245. switch (dst->op) {
  246. case CEPH_OSD_OP_READ:
  247. case CEPH_OSD_OP_WRITE:
  248. dst->extent.offset =
  249. cpu_to_le64(src->extent.offset);
  250. dst->extent.length =
  251. cpu_to_le64(src->extent.length);
  252. dst->extent.truncate_size =
  253. cpu_to_le64(src->extent.truncate_size);
  254. dst->extent.truncate_seq =
  255. cpu_to_le32(src->extent.truncate_seq);
  256. break;
  257. case CEPH_OSD_OP_GETXATTR:
  258. case CEPH_OSD_OP_SETXATTR:
  259. case CEPH_OSD_OP_CMPXATTR:
  260. BUG_ON(!req->r_trail);
  261. dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
  262. dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
  263. dst->xattr.cmp_op = src->xattr.cmp_op;
  264. dst->xattr.cmp_mode = src->xattr.cmp_mode;
  265. ceph_pagelist_append(req->r_trail, src->xattr.name,
  266. src->xattr.name_len);
  267. ceph_pagelist_append(req->r_trail, src->xattr.val,
  268. src->xattr.value_len);
  269. break;
  270. case CEPH_OSD_OP_CALL:
  271. BUG_ON(!req->r_trail);
  272. dst->cls.class_len = src->cls.class_len;
  273. dst->cls.method_len = src->cls.method_len;
  274. dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
  275. ceph_pagelist_append(req->r_trail, src->cls.class_name,
  276. src->cls.class_len);
  277. ceph_pagelist_append(req->r_trail, src->cls.method_name,
  278. src->cls.method_len);
  279. ceph_pagelist_append(req->r_trail, src->cls.indata,
  280. src->cls.indata_len);
  281. break;
  282. case CEPH_OSD_OP_ROLLBACK:
  283. dst->snap.snapid = cpu_to_le64(src->snap.snapid);
  284. break;
  285. case CEPH_OSD_OP_STARTSYNC:
  286. break;
  287. case CEPH_OSD_OP_NOTIFY:
  288. {
  289. __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
  290. __le32 timeout = cpu_to_le32(src->watch.timeout);
  291. BUG_ON(!req->r_trail);
  292. ceph_pagelist_append(req->r_trail,
  293. &prot_ver, sizeof(prot_ver));
  294. ceph_pagelist_append(req->r_trail,
  295. &timeout, sizeof(timeout));
  296. }
  297. case CEPH_OSD_OP_NOTIFY_ACK:
  298. case CEPH_OSD_OP_WATCH:
  299. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  300. dst->watch.ver = cpu_to_le64(src->watch.ver);
  301. dst->watch.flag = src->watch.flag;
  302. break;
  303. default:
  304. pr_err("unrecognized osd opcode %d\n", dst->op);
  305. WARN_ON(1);
  306. break;
  307. }
  308. dst->payload_len = cpu_to_le32(src->payload_len);
  309. }
  310. /*
  311. * build new request AND message
  312. *
  313. */
  314. void ceph_osdc_build_request(struct ceph_osd_request *req,
  315. u64 off, u64 *plen,
  316. struct ceph_osd_req_op *src_ops,
  317. struct ceph_snap_context *snapc,
  318. struct timespec *mtime,
  319. const char *oid,
  320. int oid_len)
  321. {
  322. struct ceph_msg *msg = req->r_request;
  323. struct ceph_osd_request_head *head;
  324. struct ceph_osd_req_op *src_op;
  325. struct ceph_osd_op *op;
  326. void *p;
  327. int num_op = get_num_ops(src_ops, NULL);
  328. size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
  329. int flags = req->r_flags;
  330. u64 data_len = 0;
  331. int i;
  332. head = msg->front.iov_base;
  333. op = (void *)(head + 1);
  334. p = (void *)(op + num_op);
  335. req->r_snapc = ceph_get_snap_context(snapc);
  336. head->client_inc = cpu_to_le32(1); /* always, for now. */
  337. head->flags = cpu_to_le32(flags);
  338. if (flags & CEPH_OSD_FLAG_WRITE)
  339. ceph_encode_timespec(&head->mtime, mtime);
  340. head->num_ops = cpu_to_le16(num_op);
  341. /* fill in oid */
  342. head->object_len = cpu_to_le32(oid_len);
  343. memcpy(p, oid, oid_len);
  344. p += oid_len;
  345. src_op = src_ops;
  346. while (src_op->op) {
  347. osd_req_encode_op(req, op, src_op);
  348. src_op++;
  349. op++;
  350. }
  351. if (req->r_trail)
  352. data_len += req->r_trail->length;
  353. if (snapc) {
  354. head->snap_seq = cpu_to_le64(snapc->seq);
  355. head->num_snaps = cpu_to_le32(snapc->num_snaps);
  356. for (i = 0; i < snapc->num_snaps; i++) {
  357. put_unaligned_le64(snapc->snaps[i], p);
  358. p += sizeof(u64);
  359. }
  360. }
  361. if (flags & CEPH_OSD_FLAG_WRITE) {
  362. req->r_request->hdr.data_off = cpu_to_le16(off);
  363. req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
  364. } else if (data_len) {
  365. req->r_request->hdr.data_off = 0;
  366. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  367. }
  368. req->r_request->page_alignment = req->r_page_alignment;
  369. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  370. msg_size = p - msg->front.iov_base;
  371. msg->front.iov_len = msg_size;
  372. msg->hdr.front_len = cpu_to_le32(msg_size);
  373. return;
  374. }
  375. EXPORT_SYMBOL(ceph_osdc_build_request);
  376. /*
  377. * build new request AND message, calculate layout, and adjust file
  378. * extent as needed.
  379. *
  380. * if the file was recently truncated, we include information about its
  381. * old and new size so that the object can be updated appropriately. (we
  382. * avoid synchronously deleting truncated objects because it's slow.)
  383. *
  384. * if @do_sync, include a 'startsync' command so that the osd will flush
  385. * data quickly.
  386. */
  387. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  388. struct ceph_file_layout *layout,
  389. struct ceph_vino vino,
  390. u64 off, u64 *plen,
  391. int opcode, int flags,
  392. struct ceph_snap_context *snapc,
  393. int do_sync,
  394. u32 truncate_seq,
  395. u64 truncate_size,
  396. struct timespec *mtime,
  397. bool use_mempool, int num_reply,
  398. int page_align)
  399. {
  400. struct ceph_osd_req_op ops[3];
  401. struct ceph_osd_request *req;
  402. ops[0].op = opcode;
  403. ops[0].extent.truncate_seq = truncate_seq;
  404. ops[0].extent.truncate_size = truncate_size;
  405. ops[0].payload_len = 0;
  406. if (do_sync) {
  407. ops[1].op = CEPH_OSD_OP_STARTSYNC;
  408. ops[1].payload_len = 0;
  409. ops[2].op = 0;
  410. } else
  411. ops[1].op = 0;
  412. req = ceph_osdc_alloc_request(osdc, flags,
  413. snapc, ops,
  414. use_mempool,
  415. GFP_NOFS, NULL, NULL);
  416. if (IS_ERR(req))
  417. return req;
  418. /* calculate max write size */
  419. calc_layout(osdc, vino, layout, off, plen, req, ops);
  420. req->r_file_layout = *layout; /* keep a copy */
  421. /* in case it differs from natural alignment that calc_layout
  422. filled in for us */
  423. req->r_page_alignment = page_align;
  424. ceph_osdc_build_request(req, off, plen, ops,
  425. snapc,
  426. mtime,
  427. req->r_oid, req->r_oid_len);
  428. return req;
  429. }
  430. EXPORT_SYMBOL(ceph_osdc_new_request);
  431. /*
  432. * We keep osd requests in an rbtree, sorted by ->r_tid.
  433. */
  434. static void __insert_request(struct ceph_osd_client *osdc,
  435. struct ceph_osd_request *new)
  436. {
  437. struct rb_node **p = &osdc->requests.rb_node;
  438. struct rb_node *parent = NULL;
  439. struct ceph_osd_request *req = NULL;
  440. while (*p) {
  441. parent = *p;
  442. req = rb_entry(parent, struct ceph_osd_request, r_node);
  443. if (new->r_tid < req->r_tid)
  444. p = &(*p)->rb_left;
  445. else if (new->r_tid > req->r_tid)
  446. p = &(*p)->rb_right;
  447. else
  448. BUG();
  449. }
  450. rb_link_node(&new->r_node, parent, p);
  451. rb_insert_color(&new->r_node, &osdc->requests);
  452. }
  453. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  454. u64 tid)
  455. {
  456. struct ceph_osd_request *req;
  457. struct rb_node *n = osdc->requests.rb_node;
  458. while (n) {
  459. req = rb_entry(n, struct ceph_osd_request, r_node);
  460. if (tid < req->r_tid)
  461. n = n->rb_left;
  462. else if (tid > req->r_tid)
  463. n = n->rb_right;
  464. else
  465. return req;
  466. }
  467. return NULL;
  468. }
  469. static struct ceph_osd_request *
  470. __lookup_request_ge(struct ceph_osd_client *osdc,
  471. u64 tid)
  472. {
  473. struct ceph_osd_request *req;
  474. struct rb_node *n = osdc->requests.rb_node;
  475. while (n) {
  476. req = rb_entry(n, struct ceph_osd_request, r_node);
  477. if (tid < req->r_tid) {
  478. if (!n->rb_left)
  479. return req;
  480. n = n->rb_left;
  481. } else if (tid > req->r_tid) {
  482. n = n->rb_right;
  483. } else {
  484. return req;
  485. }
  486. }
  487. return NULL;
  488. }
  489. /*
  490. * Resubmit requests pending on the given osd.
  491. */
  492. static void __kick_osd_requests(struct ceph_osd_client *osdc,
  493. struct ceph_osd *osd)
  494. {
  495. struct ceph_osd_request *req, *nreq;
  496. int err;
  497. dout("__kick_osd_requests osd%d\n", osd->o_osd);
  498. err = __reset_osd(osdc, osd);
  499. if (err == -EAGAIN)
  500. return;
  501. list_for_each_entry(req, &osd->o_requests, r_osd_item) {
  502. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  503. dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
  504. osd->o_osd);
  505. if (!req->r_linger)
  506. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  507. }
  508. list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
  509. r_linger_osd) {
  510. __unregister_linger_request(osdc, req);
  511. __register_request(osdc, req);
  512. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  513. dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
  514. osd->o_osd);
  515. }
  516. }
  517. static void kick_osd_requests(struct ceph_osd_client *osdc,
  518. struct ceph_osd *kickosd)
  519. {
  520. mutex_lock(&osdc->request_mutex);
  521. __kick_osd_requests(osdc, kickosd);
  522. mutex_unlock(&osdc->request_mutex);
  523. }
  524. /*
  525. * If the osd connection drops, we need to resubmit all requests.
  526. */
  527. static void osd_reset(struct ceph_connection *con)
  528. {
  529. struct ceph_osd *osd = con->private;
  530. struct ceph_osd_client *osdc;
  531. if (!osd)
  532. return;
  533. dout("osd_reset osd%d\n", osd->o_osd);
  534. osdc = osd->o_osdc;
  535. down_read(&osdc->map_sem);
  536. kick_osd_requests(osdc, osd);
  537. send_queued(osdc);
  538. up_read(&osdc->map_sem);
  539. }
  540. /*
  541. * Track open sessions with osds.
  542. */
  543. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
  544. {
  545. struct ceph_osd *osd;
  546. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  547. if (!osd)
  548. return NULL;
  549. atomic_set(&osd->o_ref, 1);
  550. osd->o_osdc = osdc;
  551. INIT_LIST_HEAD(&osd->o_requests);
  552. INIT_LIST_HEAD(&osd->o_linger_requests);
  553. INIT_LIST_HEAD(&osd->o_osd_lru);
  554. osd->o_incarnation = 1;
  555. ceph_con_init(osdc->client->msgr, &osd->o_con);
  556. osd->o_con.private = osd;
  557. osd->o_con.ops = &osd_con_ops;
  558. osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
  559. INIT_LIST_HEAD(&osd->o_keepalive_item);
  560. return osd;
  561. }
  562. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  563. {
  564. if (atomic_inc_not_zero(&osd->o_ref)) {
  565. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  566. atomic_read(&osd->o_ref));
  567. return osd;
  568. } else {
  569. dout("get_osd %p FAIL\n", osd);
  570. return NULL;
  571. }
  572. }
  573. static void put_osd(struct ceph_osd *osd)
  574. {
  575. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  576. atomic_read(&osd->o_ref) - 1);
  577. if (atomic_dec_and_test(&osd->o_ref)) {
  578. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  579. if (osd->o_authorizer)
  580. ac->ops->destroy_authorizer(ac, osd->o_authorizer);
  581. kfree(osd);
  582. }
  583. }
  584. /*
  585. * remove an osd from our map
  586. */
  587. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  588. {
  589. dout("__remove_osd %p\n", osd);
  590. BUG_ON(!list_empty(&osd->o_requests));
  591. rb_erase(&osd->o_node, &osdc->osds);
  592. list_del_init(&osd->o_osd_lru);
  593. ceph_con_close(&osd->o_con);
  594. put_osd(osd);
  595. }
  596. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  597. struct ceph_osd *osd)
  598. {
  599. dout("__move_osd_to_lru %p\n", osd);
  600. BUG_ON(!list_empty(&osd->o_osd_lru));
  601. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  602. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
  603. }
  604. static void __remove_osd_from_lru(struct ceph_osd *osd)
  605. {
  606. dout("__remove_osd_from_lru %p\n", osd);
  607. if (!list_empty(&osd->o_osd_lru))
  608. list_del_init(&osd->o_osd_lru);
  609. }
  610. static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
  611. {
  612. struct ceph_osd *osd, *nosd;
  613. dout("__remove_old_osds %p\n", osdc);
  614. mutex_lock(&osdc->request_mutex);
  615. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  616. if (!remove_all && time_before(jiffies, osd->lru_ttl))
  617. break;
  618. __remove_osd(osdc, osd);
  619. }
  620. mutex_unlock(&osdc->request_mutex);
  621. }
  622. /*
  623. * reset osd connect
  624. */
  625. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  626. {
  627. struct ceph_osd_request *req;
  628. int ret = 0;
  629. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  630. if (list_empty(&osd->o_requests) &&
  631. list_empty(&osd->o_linger_requests)) {
  632. __remove_osd(osdc, osd);
  633. } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
  634. &osd->o_con.peer_addr,
  635. sizeof(osd->o_con.peer_addr)) == 0 &&
  636. !ceph_con_opened(&osd->o_con)) {
  637. dout(" osd addr hasn't changed and connection never opened,"
  638. " letting msgr retry");
  639. /* touch each r_stamp for handle_timeout()'s benfit */
  640. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  641. req->r_stamp = jiffies;
  642. ret = -EAGAIN;
  643. } else {
  644. ceph_con_close(&osd->o_con);
  645. ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
  646. osd->o_incarnation++;
  647. }
  648. return ret;
  649. }
  650. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  651. {
  652. struct rb_node **p = &osdc->osds.rb_node;
  653. struct rb_node *parent = NULL;
  654. struct ceph_osd *osd = NULL;
  655. while (*p) {
  656. parent = *p;
  657. osd = rb_entry(parent, struct ceph_osd, o_node);
  658. if (new->o_osd < osd->o_osd)
  659. p = &(*p)->rb_left;
  660. else if (new->o_osd > osd->o_osd)
  661. p = &(*p)->rb_right;
  662. else
  663. BUG();
  664. }
  665. rb_link_node(&new->o_node, parent, p);
  666. rb_insert_color(&new->o_node, &osdc->osds);
  667. }
  668. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  669. {
  670. struct ceph_osd *osd;
  671. struct rb_node *n = osdc->osds.rb_node;
  672. while (n) {
  673. osd = rb_entry(n, struct ceph_osd, o_node);
  674. if (o < osd->o_osd)
  675. n = n->rb_left;
  676. else if (o > osd->o_osd)
  677. n = n->rb_right;
  678. else
  679. return osd;
  680. }
  681. return NULL;
  682. }
  683. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  684. {
  685. schedule_delayed_work(&osdc->timeout_work,
  686. osdc->client->options->osd_keepalive_timeout * HZ);
  687. }
  688. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  689. {
  690. cancel_delayed_work(&osdc->timeout_work);
  691. }
  692. /*
  693. * Register request, assign tid. If this is the first request, set up
  694. * the timeout event.
  695. */
  696. static void __register_request(struct ceph_osd_client *osdc,
  697. struct ceph_osd_request *req)
  698. {
  699. req->r_tid = ++osdc->last_tid;
  700. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  701. INIT_LIST_HEAD(&req->r_req_lru_item);
  702. dout("register_request %p tid %lld\n", req, req->r_tid);
  703. __insert_request(osdc, req);
  704. ceph_osdc_get_request(req);
  705. osdc->num_requests++;
  706. if (osdc->num_requests == 1) {
  707. dout(" first request, scheduling timeout\n");
  708. __schedule_osd_timeout(osdc);
  709. }
  710. }
  711. static void register_request(struct ceph_osd_client *osdc,
  712. struct ceph_osd_request *req)
  713. {
  714. mutex_lock(&osdc->request_mutex);
  715. __register_request(osdc, req);
  716. mutex_unlock(&osdc->request_mutex);
  717. }
  718. /*
  719. * called under osdc->request_mutex
  720. */
  721. static void __unregister_request(struct ceph_osd_client *osdc,
  722. struct ceph_osd_request *req)
  723. {
  724. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  725. rb_erase(&req->r_node, &osdc->requests);
  726. osdc->num_requests--;
  727. if (req->r_osd) {
  728. /* make sure the original request isn't in flight. */
  729. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  730. list_del_init(&req->r_osd_item);
  731. if (list_empty(&req->r_osd->o_requests) &&
  732. list_empty(&req->r_osd->o_linger_requests)) {
  733. dout("moving osd to %p lru\n", req->r_osd);
  734. __move_osd_to_lru(osdc, req->r_osd);
  735. }
  736. if (list_empty(&req->r_osd_item) &&
  737. list_empty(&req->r_linger_item))
  738. req->r_osd = NULL;
  739. }
  740. ceph_osdc_put_request(req);
  741. list_del_init(&req->r_req_lru_item);
  742. if (osdc->num_requests == 0) {
  743. dout(" no requests, canceling timeout\n");
  744. __cancel_osd_timeout(osdc);
  745. }
  746. }
  747. /*
  748. * Cancel a previously queued request message
  749. */
  750. static void __cancel_request(struct ceph_osd_request *req)
  751. {
  752. if (req->r_sent && req->r_osd) {
  753. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  754. req->r_sent = 0;
  755. }
  756. }
  757. static void __register_linger_request(struct ceph_osd_client *osdc,
  758. struct ceph_osd_request *req)
  759. {
  760. dout("__register_linger_request %p\n", req);
  761. list_add_tail(&req->r_linger_item, &osdc->req_linger);
  762. list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
  763. }
  764. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  765. struct ceph_osd_request *req)
  766. {
  767. dout("__unregister_linger_request %p\n", req);
  768. if (req->r_osd) {
  769. list_del_init(&req->r_linger_item);
  770. list_del_init(&req->r_linger_osd);
  771. if (list_empty(&req->r_osd->o_requests) &&
  772. list_empty(&req->r_osd->o_linger_requests)) {
  773. dout("moving osd to %p lru\n", req->r_osd);
  774. __move_osd_to_lru(osdc, req->r_osd);
  775. }
  776. req->r_osd = NULL;
  777. }
  778. }
  779. void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
  780. struct ceph_osd_request *req)
  781. {
  782. mutex_lock(&osdc->request_mutex);
  783. if (req->r_linger) {
  784. __unregister_linger_request(osdc, req);
  785. ceph_osdc_put_request(req);
  786. }
  787. mutex_unlock(&osdc->request_mutex);
  788. }
  789. EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
  790. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  791. struct ceph_osd_request *req)
  792. {
  793. if (!req->r_linger) {
  794. dout("set_request_linger %p\n", req);
  795. req->r_linger = 1;
  796. /*
  797. * caller is now responsible for calling
  798. * unregister_linger_request
  799. */
  800. ceph_osdc_get_request(req);
  801. }
  802. }
  803. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  804. /*
  805. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  806. * (as needed), and set the request r_osd appropriately. If there is
  807. * no up osd, set r_osd to NULL. Move the request to the appropiate list
  808. * (unsent, homeless) or leave on in-flight lru.
  809. *
  810. * Return 0 if unchanged, 1 if changed, or negative on error.
  811. *
  812. * Caller should hold map_sem for read and request_mutex.
  813. */
  814. static int __map_request(struct ceph_osd_client *osdc,
  815. struct ceph_osd_request *req)
  816. {
  817. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  818. struct ceph_pg pgid;
  819. int acting[CEPH_PG_MAX_SIZE];
  820. int o = -1, num = 0;
  821. int err;
  822. dout("map_request %p tid %lld\n", req, req->r_tid);
  823. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  824. &req->r_file_layout, osdc->osdmap);
  825. if (err) {
  826. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  827. return err;
  828. }
  829. pgid = reqhead->layout.ol_pgid;
  830. req->r_pgid = pgid;
  831. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  832. if (err > 0) {
  833. o = acting[0];
  834. num = err;
  835. }
  836. if ((req->r_osd && req->r_osd->o_osd == o &&
  837. req->r_sent >= req->r_osd->o_incarnation &&
  838. req->r_num_pg_osds == num &&
  839. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  840. (req->r_osd == NULL && o == -1))
  841. return 0; /* no change */
  842. dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
  843. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  844. req->r_osd ? req->r_osd->o_osd : -1);
  845. /* record full pg acting set */
  846. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  847. req->r_num_pg_osds = num;
  848. if (req->r_osd) {
  849. __cancel_request(req);
  850. list_del_init(&req->r_osd_item);
  851. req->r_osd = NULL;
  852. }
  853. req->r_osd = __lookup_osd(osdc, o);
  854. if (!req->r_osd && o >= 0) {
  855. err = -ENOMEM;
  856. req->r_osd = create_osd(osdc);
  857. if (!req->r_osd) {
  858. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  859. goto out;
  860. }
  861. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  862. req->r_osd->o_osd = o;
  863. req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
  864. __insert_osd(osdc, req->r_osd);
  865. ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
  866. }
  867. if (req->r_osd) {
  868. __remove_osd_from_lru(req->r_osd);
  869. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  870. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  871. } else {
  872. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  873. }
  874. err = 1; /* osd or pg changed */
  875. out:
  876. return err;
  877. }
  878. /*
  879. * caller should hold map_sem (for read) and request_mutex
  880. */
  881. static int __send_request(struct ceph_osd_client *osdc,
  882. struct ceph_osd_request *req)
  883. {
  884. struct ceph_osd_request_head *reqhead;
  885. dout("send_request %p tid %llu to osd%d flags %d\n",
  886. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  887. reqhead = req->r_request->front.iov_base;
  888. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  889. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  890. reqhead->reassert_version = req->r_reassert_version;
  891. req->r_stamp = jiffies;
  892. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  893. ceph_msg_get(req->r_request); /* send consumes a ref */
  894. ceph_con_send(&req->r_osd->o_con, req->r_request);
  895. req->r_sent = req->r_osd->o_incarnation;
  896. return 0;
  897. }
  898. /*
  899. * Send any requests in the queue (req_unsent).
  900. */
  901. static void send_queued(struct ceph_osd_client *osdc)
  902. {
  903. struct ceph_osd_request *req, *tmp;
  904. dout("send_queued\n");
  905. mutex_lock(&osdc->request_mutex);
  906. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
  907. __send_request(osdc, req);
  908. }
  909. mutex_unlock(&osdc->request_mutex);
  910. }
  911. /*
  912. * Timeout callback, called every N seconds when 1 or more osd
  913. * requests has been active for more than N seconds. When this
  914. * happens, we ping all OSDs with requests who have timed out to
  915. * ensure any communications channel reset is detected. Reset the
  916. * request timeouts another N seconds in the future as we go.
  917. * Reschedule the timeout event another N seconds in future (unless
  918. * there are no open requests).
  919. */
  920. static void handle_timeout(struct work_struct *work)
  921. {
  922. struct ceph_osd_client *osdc =
  923. container_of(work, struct ceph_osd_client, timeout_work.work);
  924. struct ceph_osd_request *req, *last_req = NULL;
  925. struct ceph_osd *osd;
  926. unsigned long timeout = osdc->client->options->osd_timeout * HZ;
  927. unsigned long keepalive =
  928. osdc->client->options->osd_keepalive_timeout * HZ;
  929. unsigned long last_stamp = 0;
  930. struct list_head slow_osds;
  931. dout("timeout\n");
  932. down_read(&osdc->map_sem);
  933. ceph_monc_request_next_osdmap(&osdc->client->monc);
  934. mutex_lock(&osdc->request_mutex);
  935. /*
  936. * reset osds that appear to be _really_ unresponsive. this
  937. * is a failsafe measure.. we really shouldn't be getting to
  938. * this point if the system is working properly. the monitors
  939. * should mark the osd as failed and we should find out about
  940. * it from an updated osd map.
  941. */
  942. while (timeout && !list_empty(&osdc->req_lru)) {
  943. req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
  944. r_req_lru_item);
  945. if (time_before(jiffies, req->r_stamp + timeout))
  946. break;
  947. BUG_ON(req == last_req && req->r_stamp == last_stamp);
  948. last_req = req;
  949. last_stamp = req->r_stamp;
  950. osd = req->r_osd;
  951. BUG_ON(!osd);
  952. pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
  953. req->r_tid, osd->o_osd);
  954. __kick_osd_requests(osdc, osd);
  955. }
  956. /*
  957. * ping osds that are a bit slow. this ensures that if there
  958. * is a break in the TCP connection we will notice, and reopen
  959. * a connection with that osd (from the fault callback).
  960. */
  961. INIT_LIST_HEAD(&slow_osds);
  962. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  963. if (time_before(jiffies, req->r_stamp + keepalive))
  964. break;
  965. osd = req->r_osd;
  966. BUG_ON(!osd);
  967. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  968. req->r_tid, osd->o_osd);
  969. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  970. }
  971. while (!list_empty(&slow_osds)) {
  972. osd = list_entry(slow_osds.next, struct ceph_osd,
  973. o_keepalive_item);
  974. list_del_init(&osd->o_keepalive_item);
  975. ceph_con_keepalive(&osd->o_con);
  976. }
  977. __schedule_osd_timeout(osdc);
  978. mutex_unlock(&osdc->request_mutex);
  979. send_queued(osdc);
  980. up_read(&osdc->map_sem);
  981. }
  982. static void handle_osds_timeout(struct work_struct *work)
  983. {
  984. struct ceph_osd_client *osdc =
  985. container_of(work, struct ceph_osd_client,
  986. osds_timeout_work.work);
  987. unsigned long delay =
  988. osdc->client->options->osd_idle_ttl * HZ >> 2;
  989. dout("osds timeout\n");
  990. down_read(&osdc->map_sem);
  991. remove_old_osds(osdc, 0);
  992. up_read(&osdc->map_sem);
  993. schedule_delayed_work(&osdc->osds_timeout_work,
  994. round_jiffies_relative(delay));
  995. }
  996. /*
  997. * handle osd op reply. either call the callback if it is specified,
  998. * or do the completion to wake up the waiting thread.
  999. */
  1000. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  1001. struct ceph_connection *con)
  1002. {
  1003. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  1004. struct ceph_osd_request *req;
  1005. u64 tid;
  1006. int numops, object_len, flags;
  1007. s32 result;
  1008. tid = le64_to_cpu(msg->hdr.tid);
  1009. if (msg->front.iov_len < sizeof(*rhead))
  1010. goto bad;
  1011. numops = le32_to_cpu(rhead->num_ops);
  1012. object_len = le32_to_cpu(rhead->object_len);
  1013. result = le32_to_cpu(rhead->result);
  1014. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  1015. numops * sizeof(struct ceph_osd_op))
  1016. goto bad;
  1017. dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
  1018. /* lookup */
  1019. mutex_lock(&osdc->request_mutex);
  1020. req = __lookup_request(osdc, tid);
  1021. if (req == NULL) {
  1022. dout("handle_reply tid %llu dne\n", tid);
  1023. mutex_unlock(&osdc->request_mutex);
  1024. return;
  1025. }
  1026. ceph_osdc_get_request(req);
  1027. flags = le32_to_cpu(rhead->flags);
  1028. /*
  1029. * if this connection filled our message, drop our reference now, to
  1030. * avoid a (safe but slower) revoke later.
  1031. */
  1032. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  1033. dout(" dropping con_filling_msg ref %p\n", con);
  1034. req->r_con_filling_msg = NULL;
  1035. ceph_con_put(con);
  1036. }
  1037. if (!req->r_got_reply) {
  1038. unsigned bytes;
  1039. req->r_result = le32_to_cpu(rhead->result);
  1040. bytes = le32_to_cpu(msg->hdr.data_len);
  1041. dout("handle_reply result %d bytes %d\n", req->r_result,
  1042. bytes);
  1043. if (req->r_result == 0)
  1044. req->r_result = bytes;
  1045. /* in case this is a write and we need to replay, */
  1046. req->r_reassert_version = rhead->reassert_version;
  1047. req->r_got_reply = 1;
  1048. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1049. dout("handle_reply tid %llu dup ack\n", tid);
  1050. mutex_unlock(&osdc->request_mutex);
  1051. goto done;
  1052. }
  1053. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1054. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1055. __register_linger_request(osdc, req);
  1056. /* either this is a read, or we got the safe response */
  1057. if (result < 0 ||
  1058. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1059. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1060. __unregister_request(osdc, req);
  1061. mutex_unlock(&osdc->request_mutex);
  1062. if (req->r_callback)
  1063. req->r_callback(req, msg);
  1064. else
  1065. complete_all(&req->r_completion);
  1066. if (flags & CEPH_OSD_FLAG_ONDISK) {
  1067. if (req->r_safe_callback)
  1068. req->r_safe_callback(req, msg);
  1069. complete_all(&req->r_safe_completion); /* fsync waiter */
  1070. }
  1071. done:
  1072. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1073. ceph_osdc_put_request(req);
  1074. return;
  1075. bad:
  1076. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  1077. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  1078. (int)sizeof(*rhead));
  1079. ceph_msg_dump(msg);
  1080. }
  1081. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1082. {
  1083. struct rb_node *p, *n;
  1084. for (p = rb_first(&osdc->osds); p; p = n) {
  1085. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1086. n = rb_next(p);
  1087. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1088. memcmp(&osd->o_con.peer_addr,
  1089. ceph_osd_addr(osdc->osdmap,
  1090. osd->o_osd),
  1091. sizeof(struct ceph_entity_addr)) != 0)
  1092. __reset_osd(osdc, osd);
  1093. }
  1094. }
  1095. /*
  1096. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1097. * no osd, request a new map.
  1098. *
  1099. * Caller should hold map_sem for read and request_mutex.
  1100. */
  1101. static void kick_requests(struct ceph_osd_client *osdc)
  1102. {
  1103. struct ceph_osd_request *req, *nreq;
  1104. struct rb_node *p;
  1105. int needmap = 0;
  1106. int err;
  1107. dout("kick_requests\n");
  1108. mutex_lock(&osdc->request_mutex);
  1109. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  1110. req = rb_entry(p, struct ceph_osd_request, r_node);
  1111. err = __map_request(osdc, req);
  1112. if (err < 0)
  1113. continue; /* error */
  1114. if (req->r_osd == NULL) {
  1115. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1116. needmap++; /* request a newer map */
  1117. } else if (err > 0) {
  1118. dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
  1119. req->r_osd ? req->r_osd->o_osd : -1);
  1120. if (!req->r_linger)
  1121. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1122. }
  1123. }
  1124. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1125. r_linger_item) {
  1126. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1127. err = __map_request(osdc, req);
  1128. if (err == 0)
  1129. continue; /* no change and no osd was specified */
  1130. if (err < 0)
  1131. continue; /* hrm! */
  1132. if (req->r_osd == NULL) {
  1133. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1134. needmap++; /* request a newer map */
  1135. continue;
  1136. }
  1137. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1138. req->r_osd ? req->r_osd->o_osd : -1);
  1139. __unregister_linger_request(osdc, req);
  1140. __register_request(osdc, req);
  1141. }
  1142. mutex_unlock(&osdc->request_mutex);
  1143. if (needmap) {
  1144. dout("%d requests for down osds, need new map\n", needmap);
  1145. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1146. }
  1147. }
  1148. /*
  1149. * Process updated osd map.
  1150. *
  1151. * The message contains any number of incremental and full maps, normally
  1152. * indicating some sort of topology change in the cluster. Kick requests
  1153. * off to different OSDs as needed.
  1154. */
  1155. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1156. {
  1157. void *p, *end, *next;
  1158. u32 nr_maps, maplen;
  1159. u32 epoch;
  1160. struct ceph_osdmap *newmap = NULL, *oldmap;
  1161. int err;
  1162. struct ceph_fsid fsid;
  1163. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1164. p = msg->front.iov_base;
  1165. end = p + msg->front.iov_len;
  1166. /* verify fsid */
  1167. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1168. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1169. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1170. return;
  1171. down_write(&osdc->map_sem);
  1172. /* incremental maps */
  1173. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1174. dout(" %d inc maps\n", nr_maps);
  1175. while (nr_maps > 0) {
  1176. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1177. epoch = ceph_decode_32(&p);
  1178. maplen = ceph_decode_32(&p);
  1179. ceph_decode_need(&p, end, maplen, bad);
  1180. next = p + maplen;
  1181. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1182. dout("applying incremental map %u len %d\n",
  1183. epoch, maplen);
  1184. newmap = osdmap_apply_incremental(&p, next,
  1185. osdc->osdmap,
  1186. osdc->client->msgr);
  1187. if (IS_ERR(newmap)) {
  1188. err = PTR_ERR(newmap);
  1189. goto bad;
  1190. }
  1191. BUG_ON(!newmap);
  1192. if (newmap != osdc->osdmap) {
  1193. ceph_osdmap_destroy(osdc->osdmap);
  1194. osdc->osdmap = newmap;
  1195. }
  1196. kick_requests(osdc);
  1197. reset_changed_osds(osdc);
  1198. } else {
  1199. dout("ignoring incremental map %u len %d\n",
  1200. epoch, maplen);
  1201. }
  1202. p = next;
  1203. nr_maps--;
  1204. }
  1205. if (newmap)
  1206. goto done;
  1207. /* full maps */
  1208. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1209. dout(" %d full maps\n", nr_maps);
  1210. while (nr_maps) {
  1211. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1212. epoch = ceph_decode_32(&p);
  1213. maplen = ceph_decode_32(&p);
  1214. ceph_decode_need(&p, end, maplen, bad);
  1215. if (nr_maps > 1) {
  1216. dout("skipping non-latest full map %u len %d\n",
  1217. epoch, maplen);
  1218. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1219. dout("skipping full map %u len %d, "
  1220. "older than our %u\n", epoch, maplen,
  1221. osdc->osdmap->epoch);
  1222. } else {
  1223. dout("taking full map %u len %d\n", epoch, maplen);
  1224. newmap = osdmap_decode(&p, p+maplen);
  1225. if (IS_ERR(newmap)) {
  1226. err = PTR_ERR(newmap);
  1227. goto bad;
  1228. }
  1229. BUG_ON(!newmap);
  1230. oldmap = osdc->osdmap;
  1231. osdc->osdmap = newmap;
  1232. if (oldmap)
  1233. ceph_osdmap_destroy(oldmap);
  1234. kick_requests(osdc);
  1235. }
  1236. p += maplen;
  1237. nr_maps--;
  1238. }
  1239. done:
  1240. downgrade_write(&osdc->map_sem);
  1241. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1242. send_queued(osdc);
  1243. up_read(&osdc->map_sem);
  1244. wake_up_all(&osdc->client->auth_wq);
  1245. return;
  1246. bad:
  1247. pr_err("osdc handle_map corrupt msg\n");
  1248. ceph_msg_dump(msg);
  1249. up_write(&osdc->map_sem);
  1250. return;
  1251. }
  1252. /*
  1253. * watch/notify callback event infrastructure
  1254. *
  1255. * These callbacks are used both for watch and notify operations.
  1256. */
  1257. static void __release_event(struct kref *kref)
  1258. {
  1259. struct ceph_osd_event *event =
  1260. container_of(kref, struct ceph_osd_event, kref);
  1261. dout("__release_event %p\n", event);
  1262. kfree(event);
  1263. }
  1264. static void get_event(struct ceph_osd_event *event)
  1265. {
  1266. kref_get(&event->kref);
  1267. }
  1268. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1269. {
  1270. kref_put(&event->kref, __release_event);
  1271. }
  1272. EXPORT_SYMBOL(ceph_osdc_put_event);
  1273. static void __insert_event(struct ceph_osd_client *osdc,
  1274. struct ceph_osd_event *new)
  1275. {
  1276. struct rb_node **p = &osdc->event_tree.rb_node;
  1277. struct rb_node *parent = NULL;
  1278. struct ceph_osd_event *event = NULL;
  1279. while (*p) {
  1280. parent = *p;
  1281. event = rb_entry(parent, struct ceph_osd_event, node);
  1282. if (new->cookie < event->cookie)
  1283. p = &(*p)->rb_left;
  1284. else if (new->cookie > event->cookie)
  1285. p = &(*p)->rb_right;
  1286. else
  1287. BUG();
  1288. }
  1289. rb_link_node(&new->node, parent, p);
  1290. rb_insert_color(&new->node, &osdc->event_tree);
  1291. }
  1292. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1293. u64 cookie)
  1294. {
  1295. struct rb_node **p = &osdc->event_tree.rb_node;
  1296. struct rb_node *parent = NULL;
  1297. struct ceph_osd_event *event = NULL;
  1298. while (*p) {
  1299. parent = *p;
  1300. event = rb_entry(parent, struct ceph_osd_event, node);
  1301. if (cookie < event->cookie)
  1302. p = &(*p)->rb_left;
  1303. else if (cookie > event->cookie)
  1304. p = &(*p)->rb_right;
  1305. else
  1306. return event;
  1307. }
  1308. return NULL;
  1309. }
  1310. static void __remove_event(struct ceph_osd_event *event)
  1311. {
  1312. struct ceph_osd_client *osdc = event->osdc;
  1313. if (!RB_EMPTY_NODE(&event->node)) {
  1314. dout("__remove_event removed %p\n", event);
  1315. rb_erase(&event->node, &osdc->event_tree);
  1316. ceph_osdc_put_event(event);
  1317. } else {
  1318. dout("__remove_event didn't remove %p\n", event);
  1319. }
  1320. }
  1321. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1322. void (*event_cb)(u64, u64, u8, void *),
  1323. int one_shot, void *data,
  1324. struct ceph_osd_event **pevent)
  1325. {
  1326. struct ceph_osd_event *event;
  1327. event = kmalloc(sizeof(*event), GFP_NOIO);
  1328. if (!event)
  1329. return -ENOMEM;
  1330. dout("create_event %p\n", event);
  1331. event->cb = event_cb;
  1332. event->one_shot = one_shot;
  1333. event->data = data;
  1334. event->osdc = osdc;
  1335. INIT_LIST_HEAD(&event->osd_node);
  1336. kref_init(&event->kref); /* one ref for us */
  1337. kref_get(&event->kref); /* one ref for the caller */
  1338. init_completion(&event->completion);
  1339. spin_lock(&osdc->event_lock);
  1340. event->cookie = ++osdc->event_count;
  1341. __insert_event(osdc, event);
  1342. spin_unlock(&osdc->event_lock);
  1343. *pevent = event;
  1344. return 0;
  1345. }
  1346. EXPORT_SYMBOL(ceph_osdc_create_event);
  1347. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1348. {
  1349. struct ceph_osd_client *osdc = event->osdc;
  1350. dout("cancel_event %p\n", event);
  1351. spin_lock(&osdc->event_lock);
  1352. __remove_event(event);
  1353. spin_unlock(&osdc->event_lock);
  1354. ceph_osdc_put_event(event); /* caller's */
  1355. }
  1356. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1357. static void do_event_work(struct work_struct *work)
  1358. {
  1359. struct ceph_osd_event_work *event_work =
  1360. container_of(work, struct ceph_osd_event_work, work);
  1361. struct ceph_osd_event *event = event_work->event;
  1362. u64 ver = event_work->ver;
  1363. u64 notify_id = event_work->notify_id;
  1364. u8 opcode = event_work->opcode;
  1365. dout("do_event_work completing %p\n", event);
  1366. event->cb(ver, notify_id, opcode, event->data);
  1367. complete(&event->completion);
  1368. dout("do_event_work completed %p\n", event);
  1369. ceph_osdc_put_event(event);
  1370. kfree(event_work);
  1371. }
  1372. /*
  1373. * Process osd watch notifications
  1374. */
  1375. void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1376. {
  1377. void *p, *end;
  1378. u8 proto_ver;
  1379. u64 cookie, ver, notify_id;
  1380. u8 opcode;
  1381. struct ceph_osd_event *event;
  1382. struct ceph_osd_event_work *event_work;
  1383. p = msg->front.iov_base;
  1384. end = p + msg->front.iov_len;
  1385. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1386. ceph_decode_8_safe(&p, end, opcode, bad);
  1387. ceph_decode_64_safe(&p, end, cookie, bad);
  1388. ceph_decode_64_safe(&p, end, ver, bad);
  1389. ceph_decode_64_safe(&p, end, notify_id, bad);
  1390. spin_lock(&osdc->event_lock);
  1391. event = __find_event(osdc, cookie);
  1392. if (event) {
  1393. get_event(event);
  1394. if (event->one_shot)
  1395. __remove_event(event);
  1396. }
  1397. spin_unlock(&osdc->event_lock);
  1398. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1399. cookie, ver, event);
  1400. if (event) {
  1401. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1402. INIT_WORK(&event_work->work, do_event_work);
  1403. if (!event_work) {
  1404. dout("ERROR: could not allocate event_work\n");
  1405. goto done_err;
  1406. }
  1407. event_work->event = event;
  1408. event_work->ver = ver;
  1409. event_work->notify_id = notify_id;
  1410. event_work->opcode = opcode;
  1411. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1412. dout("WARNING: failed to queue notify event work\n");
  1413. goto done_err;
  1414. }
  1415. }
  1416. return;
  1417. done_err:
  1418. complete(&event->completion);
  1419. ceph_osdc_put_event(event);
  1420. return;
  1421. bad:
  1422. pr_err("osdc handle_watch_notify corrupt msg\n");
  1423. return;
  1424. }
  1425. int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
  1426. {
  1427. int err;
  1428. dout("wait_event %p\n", event);
  1429. err = wait_for_completion_interruptible_timeout(&event->completion,
  1430. timeout * HZ);
  1431. ceph_osdc_put_event(event);
  1432. if (err > 0)
  1433. err = 0;
  1434. dout("wait_event %p returns %d\n", event, err);
  1435. return err;
  1436. }
  1437. EXPORT_SYMBOL(ceph_osdc_wait_event);
  1438. /*
  1439. * Register request, send initial attempt.
  1440. */
  1441. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1442. struct ceph_osd_request *req,
  1443. bool nofail)
  1444. {
  1445. int rc = 0;
  1446. req->r_request->pages = req->r_pages;
  1447. req->r_request->nr_pages = req->r_num_pages;
  1448. #ifdef CONFIG_BLOCK
  1449. req->r_request->bio = req->r_bio;
  1450. #endif
  1451. req->r_request->trail = req->r_trail;
  1452. register_request(osdc, req);
  1453. down_read(&osdc->map_sem);
  1454. mutex_lock(&osdc->request_mutex);
  1455. /*
  1456. * a racing kick_requests() may have sent the message for us
  1457. * while we dropped request_mutex above, so only send now if
  1458. * the request still han't been touched yet.
  1459. */
  1460. if (req->r_sent == 0) {
  1461. rc = __map_request(osdc, req);
  1462. if (rc < 0)
  1463. return rc;
  1464. if (req->r_osd == NULL) {
  1465. dout("send_request %p no up osds in pg\n", req);
  1466. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1467. } else {
  1468. rc = __send_request(osdc, req);
  1469. if (rc) {
  1470. if (nofail) {
  1471. dout("osdc_start_request failed send, "
  1472. " will retry %lld\n", req->r_tid);
  1473. rc = 0;
  1474. } else {
  1475. __unregister_request(osdc, req);
  1476. }
  1477. }
  1478. }
  1479. }
  1480. mutex_unlock(&osdc->request_mutex);
  1481. up_read(&osdc->map_sem);
  1482. return rc;
  1483. }
  1484. EXPORT_SYMBOL(ceph_osdc_start_request);
  1485. /*
  1486. * wait for a request to complete
  1487. */
  1488. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1489. struct ceph_osd_request *req)
  1490. {
  1491. int rc;
  1492. rc = wait_for_completion_interruptible(&req->r_completion);
  1493. if (rc < 0) {
  1494. mutex_lock(&osdc->request_mutex);
  1495. __cancel_request(req);
  1496. __unregister_request(osdc, req);
  1497. mutex_unlock(&osdc->request_mutex);
  1498. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1499. return rc;
  1500. }
  1501. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1502. return req->r_result;
  1503. }
  1504. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1505. /*
  1506. * sync - wait for all in-flight requests to flush. avoid starvation.
  1507. */
  1508. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1509. {
  1510. struct ceph_osd_request *req;
  1511. u64 last_tid, next_tid = 0;
  1512. mutex_lock(&osdc->request_mutex);
  1513. last_tid = osdc->last_tid;
  1514. while (1) {
  1515. req = __lookup_request_ge(osdc, next_tid);
  1516. if (!req)
  1517. break;
  1518. if (req->r_tid > last_tid)
  1519. break;
  1520. next_tid = req->r_tid + 1;
  1521. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1522. continue;
  1523. ceph_osdc_get_request(req);
  1524. mutex_unlock(&osdc->request_mutex);
  1525. dout("sync waiting on tid %llu (last is %llu)\n",
  1526. req->r_tid, last_tid);
  1527. wait_for_completion(&req->r_safe_completion);
  1528. mutex_lock(&osdc->request_mutex);
  1529. ceph_osdc_put_request(req);
  1530. }
  1531. mutex_unlock(&osdc->request_mutex);
  1532. dout("sync done (thru tid %llu)\n", last_tid);
  1533. }
  1534. EXPORT_SYMBOL(ceph_osdc_sync);
  1535. /*
  1536. * init, shutdown
  1537. */
  1538. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1539. {
  1540. int err;
  1541. dout("init\n");
  1542. osdc->client = client;
  1543. osdc->osdmap = NULL;
  1544. init_rwsem(&osdc->map_sem);
  1545. init_completion(&osdc->map_waiters);
  1546. osdc->last_requested_map = 0;
  1547. mutex_init(&osdc->request_mutex);
  1548. osdc->last_tid = 0;
  1549. osdc->osds = RB_ROOT;
  1550. INIT_LIST_HEAD(&osdc->osd_lru);
  1551. osdc->requests = RB_ROOT;
  1552. INIT_LIST_HEAD(&osdc->req_lru);
  1553. INIT_LIST_HEAD(&osdc->req_unsent);
  1554. INIT_LIST_HEAD(&osdc->req_notarget);
  1555. INIT_LIST_HEAD(&osdc->req_linger);
  1556. osdc->num_requests = 0;
  1557. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1558. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1559. spin_lock_init(&osdc->event_lock);
  1560. osdc->event_tree = RB_ROOT;
  1561. osdc->event_count = 0;
  1562. schedule_delayed_work(&osdc->osds_timeout_work,
  1563. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1564. err = -ENOMEM;
  1565. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1566. sizeof(struct ceph_osd_request));
  1567. if (!osdc->req_mempool)
  1568. goto out;
  1569. err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
  1570. "osd_op");
  1571. if (err < 0)
  1572. goto out_mempool;
  1573. err = ceph_msgpool_init(&osdc->msgpool_op_reply,
  1574. OSD_OPREPLY_FRONT_LEN, 10, true,
  1575. "osd_op_reply");
  1576. if (err < 0)
  1577. goto out_msgpool;
  1578. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  1579. if (IS_ERR(osdc->notify_wq)) {
  1580. err = PTR_ERR(osdc->notify_wq);
  1581. osdc->notify_wq = NULL;
  1582. goto out_msgpool;
  1583. }
  1584. return 0;
  1585. out_msgpool:
  1586. ceph_msgpool_destroy(&osdc->msgpool_op);
  1587. out_mempool:
  1588. mempool_destroy(osdc->req_mempool);
  1589. out:
  1590. return err;
  1591. }
  1592. EXPORT_SYMBOL(ceph_osdc_init);
  1593. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1594. {
  1595. flush_workqueue(osdc->notify_wq);
  1596. destroy_workqueue(osdc->notify_wq);
  1597. cancel_delayed_work_sync(&osdc->timeout_work);
  1598. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1599. if (osdc->osdmap) {
  1600. ceph_osdmap_destroy(osdc->osdmap);
  1601. osdc->osdmap = NULL;
  1602. }
  1603. remove_old_osds(osdc, 1);
  1604. WARN_ON(!RB_EMPTY_ROOT(&osdc->osds));
  1605. mempool_destroy(osdc->req_mempool);
  1606. ceph_msgpool_destroy(&osdc->msgpool_op);
  1607. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1608. }
  1609. EXPORT_SYMBOL(ceph_osdc_stop);
  1610. /*
  1611. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1612. * *plen. Return number of bytes read, or error.
  1613. */
  1614. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1615. struct ceph_vino vino, struct ceph_file_layout *layout,
  1616. u64 off, u64 *plen,
  1617. u32 truncate_seq, u64 truncate_size,
  1618. struct page **pages, int num_pages, int page_align)
  1619. {
  1620. struct ceph_osd_request *req;
  1621. int rc = 0;
  1622. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1623. vino.snap, off, *plen);
  1624. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1625. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1626. NULL, 0, truncate_seq, truncate_size, NULL,
  1627. false, 1, page_align);
  1628. if (!req)
  1629. return -ENOMEM;
  1630. /* it may be a short read due to an object boundary */
  1631. req->r_pages = pages;
  1632. dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
  1633. off, *plen, req->r_num_pages, page_align);
  1634. rc = ceph_osdc_start_request(osdc, req, false);
  1635. if (!rc)
  1636. rc = ceph_osdc_wait_request(osdc, req);
  1637. ceph_osdc_put_request(req);
  1638. dout("readpages result %d\n", rc);
  1639. return rc;
  1640. }
  1641. EXPORT_SYMBOL(ceph_osdc_readpages);
  1642. /*
  1643. * do a synchronous write on N pages
  1644. */
  1645. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1646. struct ceph_file_layout *layout,
  1647. struct ceph_snap_context *snapc,
  1648. u64 off, u64 len,
  1649. u32 truncate_seq, u64 truncate_size,
  1650. struct timespec *mtime,
  1651. struct page **pages, int num_pages,
  1652. int flags, int do_sync, bool nofail)
  1653. {
  1654. struct ceph_osd_request *req;
  1655. int rc = 0;
  1656. int page_align = off & ~PAGE_MASK;
  1657. BUG_ON(vino.snap != CEPH_NOSNAP);
  1658. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1659. CEPH_OSD_OP_WRITE,
  1660. flags | CEPH_OSD_FLAG_ONDISK |
  1661. CEPH_OSD_FLAG_WRITE,
  1662. snapc, do_sync,
  1663. truncate_seq, truncate_size, mtime,
  1664. nofail, 1, page_align);
  1665. if (!req)
  1666. return -ENOMEM;
  1667. /* it may be a short write due to an object boundary */
  1668. req->r_pages = pages;
  1669. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1670. req->r_num_pages);
  1671. rc = ceph_osdc_start_request(osdc, req, nofail);
  1672. if (!rc)
  1673. rc = ceph_osdc_wait_request(osdc, req);
  1674. ceph_osdc_put_request(req);
  1675. if (rc == 0)
  1676. rc = len;
  1677. dout("writepages result %d\n", rc);
  1678. return rc;
  1679. }
  1680. EXPORT_SYMBOL(ceph_osdc_writepages);
  1681. /*
  1682. * handle incoming message
  1683. */
  1684. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1685. {
  1686. struct ceph_osd *osd = con->private;
  1687. struct ceph_osd_client *osdc;
  1688. int type = le16_to_cpu(msg->hdr.type);
  1689. if (!osd)
  1690. goto out;
  1691. osdc = osd->o_osdc;
  1692. switch (type) {
  1693. case CEPH_MSG_OSD_MAP:
  1694. ceph_osdc_handle_map(osdc, msg);
  1695. break;
  1696. case CEPH_MSG_OSD_OPREPLY:
  1697. handle_reply(osdc, msg, con);
  1698. break;
  1699. case CEPH_MSG_WATCH_NOTIFY:
  1700. handle_watch_notify(osdc, msg);
  1701. break;
  1702. default:
  1703. pr_err("received unknown message type %d %s\n", type,
  1704. ceph_msg_type_name(type));
  1705. }
  1706. out:
  1707. ceph_msg_put(msg);
  1708. }
  1709. /*
  1710. * lookup and return message for incoming reply. set up reply message
  1711. * pages.
  1712. */
  1713. static struct ceph_msg *get_reply(struct ceph_connection *con,
  1714. struct ceph_msg_header *hdr,
  1715. int *skip)
  1716. {
  1717. struct ceph_osd *osd = con->private;
  1718. struct ceph_osd_client *osdc = osd->o_osdc;
  1719. struct ceph_msg *m;
  1720. struct ceph_osd_request *req;
  1721. int front = le32_to_cpu(hdr->front_len);
  1722. int data_len = le32_to_cpu(hdr->data_len);
  1723. u64 tid;
  1724. tid = le64_to_cpu(hdr->tid);
  1725. mutex_lock(&osdc->request_mutex);
  1726. req = __lookup_request(osdc, tid);
  1727. if (!req) {
  1728. *skip = 1;
  1729. m = NULL;
  1730. pr_info("get_reply unknown tid %llu from osd%d\n", tid,
  1731. osd->o_osd);
  1732. goto out;
  1733. }
  1734. if (req->r_con_filling_msg) {
  1735. dout("get_reply revoking msg %p from old con %p\n",
  1736. req->r_reply, req->r_con_filling_msg);
  1737. ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
  1738. ceph_con_put(req->r_con_filling_msg);
  1739. req->r_con_filling_msg = NULL;
  1740. }
  1741. if (front > req->r_reply->front.iov_len) {
  1742. pr_warning("get_reply front %d > preallocated %d\n",
  1743. front, (int)req->r_reply->front.iov_len);
  1744. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS);
  1745. if (!m)
  1746. goto out;
  1747. ceph_msg_put(req->r_reply);
  1748. req->r_reply = m;
  1749. }
  1750. m = ceph_msg_get(req->r_reply);
  1751. if (data_len > 0) {
  1752. int want = calc_pages_for(req->r_page_alignment, data_len);
  1753. if (unlikely(req->r_num_pages < want)) {
  1754. pr_warning("tid %lld reply %d > expected %d pages\n",
  1755. tid, want, m->nr_pages);
  1756. *skip = 1;
  1757. ceph_msg_put(m);
  1758. m = NULL;
  1759. goto out;
  1760. }
  1761. m->pages = req->r_pages;
  1762. m->nr_pages = req->r_num_pages;
  1763. m->page_alignment = req->r_page_alignment;
  1764. #ifdef CONFIG_BLOCK
  1765. m->bio = req->r_bio;
  1766. #endif
  1767. }
  1768. *skip = 0;
  1769. req->r_con_filling_msg = ceph_con_get(con);
  1770. dout("get_reply tid %lld %p\n", tid, m);
  1771. out:
  1772. mutex_unlock(&osdc->request_mutex);
  1773. return m;
  1774. }
  1775. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1776. struct ceph_msg_header *hdr,
  1777. int *skip)
  1778. {
  1779. struct ceph_osd *osd = con->private;
  1780. int type = le16_to_cpu(hdr->type);
  1781. int front = le32_to_cpu(hdr->front_len);
  1782. switch (type) {
  1783. case CEPH_MSG_OSD_MAP:
  1784. case CEPH_MSG_WATCH_NOTIFY:
  1785. return ceph_msg_new(type, front, GFP_NOFS);
  1786. case CEPH_MSG_OSD_OPREPLY:
  1787. return get_reply(con, hdr, skip);
  1788. default:
  1789. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  1790. osd->o_osd);
  1791. *skip = 1;
  1792. return NULL;
  1793. }
  1794. }
  1795. /*
  1796. * Wrappers to refcount containing ceph_osd struct
  1797. */
  1798. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1799. {
  1800. struct ceph_osd *osd = con->private;
  1801. if (get_osd(osd))
  1802. return con;
  1803. return NULL;
  1804. }
  1805. static void put_osd_con(struct ceph_connection *con)
  1806. {
  1807. struct ceph_osd *osd = con->private;
  1808. put_osd(osd);
  1809. }
  1810. /*
  1811. * authentication
  1812. */
  1813. static int get_authorizer(struct ceph_connection *con,
  1814. void **buf, int *len, int *proto,
  1815. void **reply_buf, int *reply_len, int force_new)
  1816. {
  1817. struct ceph_osd *o = con->private;
  1818. struct ceph_osd_client *osdc = o->o_osdc;
  1819. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1820. int ret = 0;
  1821. if (force_new && o->o_authorizer) {
  1822. ac->ops->destroy_authorizer(ac, o->o_authorizer);
  1823. o->o_authorizer = NULL;
  1824. }
  1825. if (o->o_authorizer == NULL) {
  1826. ret = ac->ops->create_authorizer(
  1827. ac, CEPH_ENTITY_TYPE_OSD,
  1828. &o->o_authorizer,
  1829. &o->o_authorizer_buf,
  1830. &o->o_authorizer_buf_len,
  1831. &o->o_authorizer_reply_buf,
  1832. &o->o_authorizer_reply_buf_len);
  1833. if (ret)
  1834. return ret;
  1835. }
  1836. *proto = ac->protocol;
  1837. *buf = o->o_authorizer_buf;
  1838. *len = o->o_authorizer_buf_len;
  1839. *reply_buf = o->o_authorizer_reply_buf;
  1840. *reply_len = o->o_authorizer_reply_buf_len;
  1841. return 0;
  1842. }
  1843. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1844. {
  1845. struct ceph_osd *o = con->private;
  1846. struct ceph_osd_client *osdc = o->o_osdc;
  1847. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1848. return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
  1849. }
  1850. static int invalidate_authorizer(struct ceph_connection *con)
  1851. {
  1852. struct ceph_osd *o = con->private;
  1853. struct ceph_osd_client *osdc = o->o_osdc;
  1854. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1855. if (ac->ops->invalidate_authorizer)
  1856. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  1857. return ceph_monc_validate_auth(&osdc->client->monc);
  1858. }
  1859. static const struct ceph_connection_operations osd_con_ops = {
  1860. .get = get_osd_con,
  1861. .put = put_osd_con,
  1862. .dispatch = dispatch,
  1863. .get_authorizer = get_authorizer,
  1864. .verify_authorizer_reply = verify_authorizer_reply,
  1865. .invalidate_authorizer = invalidate_authorizer,
  1866. .alloc_msg = alloc_msg,
  1867. .fault = osd_reset,
  1868. };