osd_client.c 59 KB

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