osd_client.c 56 KB

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