osd_client.c 55 KB

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