osd_client.c 56 KB

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