osd_client.c 56 KB

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