osd_client.c 54 KB

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