osd_client.c 55 KB

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