osd_client.c 61 KB

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