osd_client.c 55 KB

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