osd_client.c 55 KB

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