osd_client.c 57 KB

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