osd_client.c 58 KB

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