osd_client.c 57 KB

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