osd_client.c 57 KB

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