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