osd_client.c 58 KB

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