osd_client.c 56 KB

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