osd_client.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/err.h>
  4. #include <linux/highmem.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #ifdef CONFIG_BLOCK
  10. #include <linux/bio.h>
  11. #endif
  12. #include <linux/ceph/libceph.h>
  13. #include <linux/ceph/osd_client.h>
  14. #include <linux/ceph/messenger.h>
  15. #include <linux/ceph/decode.h>
  16. #include <linux/ceph/auth.h>
  17. #include <linux/ceph/pagelist.h>
  18. #define OSD_OP_FRONT_LEN 4096
  19. #define OSD_OPREPLY_FRONT_LEN 512
  20. static const struct ceph_connection_operations osd_con_ops;
  21. static void __send_queued(struct ceph_osd_client *osdc);
  22. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  23. static void __register_request(struct ceph_osd_client *osdc,
  24. struct ceph_osd_request *req);
  25. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  26. struct ceph_osd_request *req);
  27. static void __send_request(struct ceph_osd_client *osdc,
  28. struct ceph_osd_request *req);
  29. static int op_has_extent(int op)
  30. {
  31. return (op == CEPH_OSD_OP_READ ||
  32. op == CEPH_OSD_OP_WRITE);
  33. }
  34. /*
  35. * Implement client access to distributed object storage cluster.
  36. *
  37. * All data objects are stored within a cluster/cloud of OSDs, or
  38. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  39. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  40. * remote daemons serving up and coordinating consistent and safe
  41. * access to storage.
  42. *
  43. * Cluster membership and the mapping of data objects onto storage devices
  44. * are described by the osd map.
  45. *
  46. * We keep track of pending OSD requests (read, write), resubmit
  47. * requests to different OSDs when the cluster topology/data layout
  48. * change, or retry the affected requests when the communications
  49. * channel with an OSD is reset.
  50. */
  51. /*
  52. * calculate the mapping of a file extent onto an object, and fill out the
  53. * request accordingly. shorten extent as necessary if it crosses an
  54. * object boundary.
  55. *
  56. * fill osd op in request message.
  57. */
  58. static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
  59. struct ceph_osd_req_op *op, u64 *bno)
  60. {
  61. u64 orig_len = *plen;
  62. u64 objoff = 0;
  63. u64 objlen = 0;
  64. int r;
  65. /* object extent? */
  66. r = ceph_calc_file_object_mapping(layout, off, orig_len, bno,
  67. &objoff, &objlen);
  68. if (r < 0)
  69. return r;
  70. if (objlen < orig_len) {
  71. *plen = objlen;
  72. dout(" skipping last %llu, final file extent %llu~%llu\n",
  73. orig_len - *plen, off, *plen);
  74. }
  75. if (op_has_extent(op->op)) {
  76. u32 osize = le32_to_cpu(layout->fl_object_size);
  77. op->extent.offset = objoff;
  78. op->extent.length = objlen;
  79. if (op->extent.truncate_size <= off - objoff) {
  80. op->extent.truncate_size = 0;
  81. } else {
  82. op->extent.truncate_size -= off - objoff;
  83. if (op->extent.truncate_size > osize)
  84. op->extent.truncate_size = osize;
  85. }
  86. }
  87. if (op->op == CEPH_OSD_OP_WRITE)
  88. op->payload_len = *plen;
  89. dout("calc_layout bno=%llx %llu~%llu\n", *bno, objoff, objlen);
  90. return 0;
  91. }
  92. /*
  93. * requests
  94. */
  95. void ceph_osdc_release_request(struct kref *kref)
  96. {
  97. struct ceph_osd_request *req = container_of(kref,
  98. struct ceph_osd_request,
  99. r_kref);
  100. if (req->r_request)
  101. ceph_msg_put(req->r_request);
  102. if (req->r_con_filling_msg) {
  103. dout("%s revoking msg %p from con %p\n", __func__,
  104. req->r_reply, req->r_con_filling_msg);
  105. ceph_msg_revoke_incoming(req->r_reply);
  106. req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
  107. req->r_con_filling_msg = NULL;
  108. }
  109. if (req->r_reply)
  110. ceph_msg_put(req->r_reply);
  111. if (req->r_own_pages)
  112. ceph_release_page_vector(req->r_pages,
  113. req->r_num_pages);
  114. ceph_put_snap_context(req->r_snapc);
  115. ceph_pagelist_release(&req->r_trail);
  116. if (req->r_mempool)
  117. mempool_free(req, req->r_osdc->req_mempool);
  118. else
  119. kfree(req);
  120. }
  121. EXPORT_SYMBOL(ceph_osdc_release_request);
  122. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  123. struct ceph_snap_context *snapc,
  124. unsigned int num_ops,
  125. bool use_mempool,
  126. gfp_t gfp_flags)
  127. {
  128. struct ceph_osd_request *req;
  129. struct ceph_msg *msg;
  130. size_t msg_size;
  131. msg_size = 4 + 4 + 8 + 8 + 4+8;
  132. msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
  133. msg_size += 1 + 8 + 4 + 4; /* pg_t */
  134. msg_size += 4 + MAX_OBJ_NAME_SIZE;
  135. msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
  136. msg_size += 8; /* snapid */
  137. msg_size += 8; /* snap_seq */
  138. msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
  139. msg_size += 4;
  140. if (use_mempool) {
  141. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  142. memset(req, 0, sizeof(*req));
  143. } else {
  144. req = kzalloc(sizeof(*req), gfp_flags);
  145. }
  146. if (req == NULL)
  147. return NULL;
  148. req->r_osdc = osdc;
  149. req->r_mempool = use_mempool;
  150. kref_init(&req->r_kref);
  151. init_completion(&req->r_completion);
  152. init_completion(&req->r_safe_completion);
  153. RB_CLEAR_NODE(&req->r_node);
  154. INIT_LIST_HEAD(&req->r_unsafe_item);
  155. INIT_LIST_HEAD(&req->r_linger_item);
  156. INIT_LIST_HEAD(&req->r_linger_osd);
  157. INIT_LIST_HEAD(&req->r_req_lru_item);
  158. INIT_LIST_HEAD(&req->r_osd_item);
  159. /* create reply message */
  160. if (use_mempool)
  161. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
  162. else
  163. msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
  164. OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
  165. if (!msg) {
  166. ceph_osdc_put_request(req);
  167. return NULL;
  168. }
  169. req->r_reply = msg;
  170. ceph_pagelist_init(&req->r_trail);
  171. /* create request message; allow space for oid */
  172. if (use_mempool)
  173. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  174. else
  175. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
  176. if (!msg) {
  177. ceph_osdc_put_request(req);
  178. return NULL;
  179. }
  180. memset(msg->front.iov_base, 0, msg->front.iov_len);
  181. req->r_request = msg;
  182. return req;
  183. }
  184. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  185. static void osd_req_encode_op(struct ceph_osd_request *req,
  186. struct ceph_osd_op *dst,
  187. struct ceph_osd_req_op *src)
  188. {
  189. dst->op = cpu_to_le16(src->op);
  190. switch (src->op) {
  191. case CEPH_OSD_OP_STAT:
  192. break;
  193. case CEPH_OSD_OP_READ:
  194. case CEPH_OSD_OP_WRITE:
  195. dst->extent.offset =
  196. cpu_to_le64(src->extent.offset);
  197. dst->extent.length =
  198. cpu_to_le64(src->extent.length);
  199. dst->extent.truncate_size =
  200. cpu_to_le64(src->extent.truncate_size);
  201. dst->extent.truncate_seq =
  202. cpu_to_le32(src->extent.truncate_seq);
  203. break;
  204. case CEPH_OSD_OP_CALL:
  205. dst->cls.class_len = src->cls.class_len;
  206. dst->cls.method_len = src->cls.method_len;
  207. dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
  208. ceph_pagelist_append(&req->r_trail, src->cls.class_name,
  209. src->cls.class_len);
  210. ceph_pagelist_append(&req->r_trail, src->cls.method_name,
  211. src->cls.method_len);
  212. ceph_pagelist_append(&req->r_trail, src->cls.indata,
  213. src->cls.indata_len);
  214. break;
  215. case CEPH_OSD_OP_STARTSYNC:
  216. break;
  217. case CEPH_OSD_OP_NOTIFY_ACK:
  218. case CEPH_OSD_OP_WATCH:
  219. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  220. dst->watch.ver = cpu_to_le64(src->watch.ver);
  221. dst->watch.flag = src->watch.flag;
  222. break;
  223. default:
  224. pr_err("unrecognized osd opcode %d\n", src->op);
  225. WARN_ON(1);
  226. break;
  227. case CEPH_OSD_OP_MAPEXT:
  228. case CEPH_OSD_OP_MASKTRUNC:
  229. case CEPH_OSD_OP_SPARSE_READ:
  230. case CEPH_OSD_OP_NOTIFY:
  231. case CEPH_OSD_OP_ASSERT_VER:
  232. case CEPH_OSD_OP_WRITEFULL:
  233. case CEPH_OSD_OP_TRUNCATE:
  234. case CEPH_OSD_OP_ZERO:
  235. case CEPH_OSD_OP_DELETE:
  236. case CEPH_OSD_OP_APPEND:
  237. case CEPH_OSD_OP_SETTRUNC:
  238. case CEPH_OSD_OP_TRIMTRUNC:
  239. case CEPH_OSD_OP_TMAPUP:
  240. case CEPH_OSD_OP_TMAPPUT:
  241. case CEPH_OSD_OP_TMAPGET:
  242. case CEPH_OSD_OP_CREATE:
  243. case CEPH_OSD_OP_ROLLBACK:
  244. case CEPH_OSD_OP_OMAPGETKEYS:
  245. case CEPH_OSD_OP_OMAPGETVALS:
  246. case CEPH_OSD_OP_OMAPGETHEADER:
  247. case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
  248. case CEPH_OSD_OP_MODE_RD:
  249. case CEPH_OSD_OP_OMAPSETVALS:
  250. case CEPH_OSD_OP_OMAPSETHEADER:
  251. case CEPH_OSD_OP_OMAPCLEAR:
  252. case CEPH_OSD_OP_OMAPRMKEYS:
  253. case CEPH_OSD_OP_OMAP_CMP:
  254. case CEPH_OSD_OP_CLONERANGE:
  255. case CEPH_OSD_OP_ASSERT_SRC_VERSION:
  256. case CEPH_OSD_OP_SRC_CMPXATTR:
  257. case CEPH_OSD_OP_GETXATTR:
  258. case CEPH_OSD_OP_GETXATTRS:
  259. case CEPH_OSD_OP_CMPXATTR:
  260. case CEPH_OSD_OP_SETXATTR:
  261. case CEPH_OSD_OP_SETXATTRS:
  262. case CEPH_OSD_OP_RESETXATTRS:
  263. case CEPH_OSD_OP_RMXATTR:
  264. case CEPH_OSD_OP_PULL:
  265. case CEPH_OSD_OP_PUSH:
  266. case CEPH_OSD_OP_BALANCEREADS:
  267. case CEPH_OSD_OP_UNBALANCEREADS:
  268. case CEPH_OSD_OP_SCRUB:
  269. case CEPH_OSD_OP_SCRUB_RESERVE:
  270. case CEPH_OSD_OP_SCRUB_UNRESERVE:
  271. case CEPH_OSD_OP_SCRUB_STOP:
  272. case CEPH_OSD_OP_SCRUB_MAP:
  273. case CEPH_OSD_OP_WRLOCK:
  274. case CEPH_OSD_OP_WRUNLOCK:
  275. case CEPH_OSD_OP_RDLOCK:
  276. case CEPH_OSD_OP_RDUNLOCK:
  277. case CEPH_OSD_OP_UPLOCK:
  278. case CEPH_OSD_OP_DNLOCK:
  279. case CEPH_OSD_OP_PGLS:
  280. case CEPH_OSD_OP_PGLS_FILTER:
  281. pr_err("unsupported osd opcode %s\n",
  282. ceph_osd_op_name(src->op));
  283. WARN_ON(1);
  284. break;
  285. }
  286. dst->payload_len = cpu_to_le32(src->payload_len);
  287. }
  288. /*
  289. * build new request AND message
  290. *
  291. */
  292. void ceph_osdc_build_request(struct ceph_osd_request *req,
  293. u64 off, u64 len, unsigned int num_ops,
  294. struct ceph_osd_req_op *src_ops,
  295. struct ceph_snap_context *snapc, u64 snap_id,
  296. struct timespec *mtime)
  297. {
  298. struct ceph_msg *msg = req->r_request;
  299. struct ceph_osd_req_op *src_op;
  300. void *p;
  301. size_t msg_size;
  302. int flags = req->r_flags;
  303. u64 data_len;
  304. int i;
  305. req->r_num_ops = num_ops;
  306. req->r_snapid = snap_id;
  307. req->r_snapc = ceph_get_snap_context(snapc);
  308. /* encode request */
  309. msg->hdr.version = cpu_to_le16(4);
  310. p = msg->front.iov_base;
  311. ceph_encode_32(&p, 1); /* client_inc is always 1 */
  312. req->r_request_osdmap_epoch = p;
  313. p += 4;
  314. req->r_request_flags = p;
  315. p += 4;
  316. if (req->r_flags & CEPH_OSD_FLAG_WRITE)
  317. ceph_encode_timespec(p, mtime);
  318. p += sizeof(struct ceph_timespec);
  319. req->r_request_reassert_version = p;
  320. p += sizeof(struct ceph_eversion); /* will get filled in */
  321. /* oloc */
  322. ceph_encode_8(&p, 4);
  323. ceph_encode_8(&p, 4);
  324. ceph_encode_32(&p, 8 + 4 + 4);
  325. req->r_request_pool = p;
  326. p += 8;
  327. ceph_encode_32(&p, -1); /* preferred */
  328. ceph_encode_32(&p, 0); /* key len */
  329. ceph_encode_8(&p, 1);
  330. req->r_request_pgid = p;
  331. p += 8 + 4;
  332. ceph_encode_32(&p, -1); /* preferred */
  333. /* oid */
  334. ceph_encode_32(&p, req->r_oid_len);
  335. memcpy(p, req->r_oid, req->r_oid_len);
  336. dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
  337. p += req->r_oid_len;
  338. /* ops */
  339. ceph_encode_16(&p, num_ops);
  340. src_op = src_ops;
  341. req->r_request_ops = p;
  342. for (i = 0; i < num_ops; i++, src_op++) {
  343. osd_req_encode_op(req, p, src_op);
  344. p += sizeof(struct ceph_osd_op);
  345. }
  346. /* snaps */
  347. ceph_encode_64(&p, req->r_snapid);
  348. ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
  349. ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
  350. if (req->r_snapc) {
  351. for (i = 0; i < snapc->num_snaps; i++) {
  352. ceph_encode_64(&p, req->r_snapc->snaps[i]);
  353. }
  354. }
  355. req->r_request_attempts = p;
  356. p += 4;
  357. data_len = req->r_trail.length;
  358. if (flags & CEPH_OSD_FLAG_WRITE) {
  359. req->r_request->hdr.data_off = cpu_to_le16(off);
  360. data_len += len;
  361. }
  362. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  363. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  364. msg_size = p - msg->front.iov_base;
  365. msg->front.iov_len = msg_size;
  366. msg->hdr.front_len = cpu_to_le32(msg_size);
  367. dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
  368. num_ops);
  369. return;
  370. }
  371. EXPORT_SYMBOL(ceph_osdc_build_request);
  372. /*
  373. * build new request AND message, calculate layout, and adjust file
  374. * extent as needed.
  375. *
  376. * if the file was recently truncated, we include information about its
  377. * old and new size so that the object can be updated appropriately. (we
  378. * avoid synchronously deleting truncated objects because it's slow.)
  379. *
  380. * if @do_sync, include a 'startsync' command so that the osd will flush
  381. * data quickly.
  382. */
  383. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  384. struct ceph_file_layout *layout,
  385. struct ceph_vino vino,
  386. u64 off, u64 *plen,
  387. int opcode, int flags,
  388. struct ceph_snap_context *snapc,
  389. int do_sync,
  390. u32 truncate_seq,
  391. u64 truncate_size,
  392. struct timespec *mtime,
  393. bool use_mempool,
  394. int page_align)
  395. {
  396. struct ceph_osd_req_op ops[2];
  397. struct ceph_osd_request *req;
  398. unsigned int num_op = 1;
  399. u64 bno = 0;
  400. int r;
  401. memset(&ops, 0, sizeof ops);
  402. ops[0].op = opcode;
  403. ops[0].extent.truncate_seq = truncate_seq;
  404. ops[0].extent.truncate_size = truncate_size;
  405. if (do_sync) {
  406. ops[1].op = CEPH_OSD_OP_STARTSYNC;
  407. num_op++;
  408. }
  409. req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
  410. GFP_NOFS);
  411. if (!req)
  412. return ERR_PTR(-ENOMEM);
  413. req->r_flags = flags;
  414. /* calculate max write size */
  415. r = calc_layout(layout, off, plen, ops, &bno);
  416. if (r < 0) {
  417. ceph_osdc_put_request(req);
  418. return ERR_PTR(r);
  419. }
  420. req->r_file_layout = *layout; /* keep a copy */
  421. snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
  422. req->r_oid_len = strlen(req->r_oid);
  423. /* The alignment may differ from the natural (file) alignment */
  424. req->r_num_pages = calc_pages_for(page_align, *plen);
  425. req->r_page_alignment = page_align;
  426. ceph_osdc_build_request(req, off, *plen, num_op, ops,
  427. snapc, vino.snap, mtime);
  428. return req;
  429. }
  430. EXPORT_SYMBOL(ceph_osdc_new_request);
  431. /*
  432. * We keep osd requests in an rbtree, sorted by ->r_tid.
  433. */
  434. static void __insert_request(struct ceph_osd_client *osdc,
  435. struct ceph_osd_request *new)
  436. {
  437. struct rb_node **p = &osdc->requests.rb_node;
  438. struct rb_node *parent = NULL;
  439. struct ceph_osd_request *req = NULL;
  440. while (*p) {
  441. parent = *p;
  442. req = rb_entry(parent, struct ceph_osd_request, r_node);
  443. if (new->r_tid < req->r_tid)
  444. p = &(*p)->rb_left;
  445. else if (new->r_tid > req->r_tid)
  446. p = &(*p)->rb_right;
  447. else
  448. BUG();
  449. }
  450. rb_link_node(&new->r_node, parent, p);
  451. rb_insert_color(&new->r_node, &osdc->requests);
  452. }
  453. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  454. u64 tid)
  455. {
  456. struct ceph_osd_request *req;
  457. struct rb_node *n = osdc->requests.rb_node;
  458. while (n) {
  459. req = rb_entry(n, struct ceph_osd_request, r_node);
  460. if (tid < req->r_tid)
  461. n = n->rb_left;
  462. else if (tid > req->r_tid)
  463. n = n->rb_right;
  464. else
  465. return req;
  466. }
  467. return NULL;
  468. }
  469. static struct ceph_osd_request *
  470. __lookup_request_ge(struct ceph_osd_client *osdc,
  471. u64 tid)
  472. {
  473. struct ceph_osd_request *req;
  474. struct rb_node *n = osdc->requests.rb_node;
  475. while (n) {
  476. req = rb_entry(n, struct ceph_osd_request, r_node);
  477. if (tid < req->r_tid) {
  478. if (!n->rb_left)
  479. return req;
  480. n = n->rb_left;
  481. } else if (tid > req->r_tid) {
  482. n = n->rb_right;
  483. } else {
  484. return req;
  485. }
  486. }
  487. return NULL;
  488. }
  489. /*
  490. * Resubmit requests pending on the given osd.
  491. */
  492. static void __kick_osd_requests(struct ceph_osd_client *osdc,
  493. struct ceph_osd *osd)
  494. {
  495. struct ceph_osd_request *req, *nreq;
  496. int err;
  497. dout("__kick_osd_requests osd%d\n", osd->o_osd);
  498. err = __reset_osd(osdc, osd);
  499. if (err)
  500. return;
  501. list_for_each_entry(req, &osd->o_requests, r_osd_item) {
  502. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  503. dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
  504. osd->o_osd);
  505. if (!req->r_linger)
  506. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  507. }
  508. list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
  509. r_linger_osd) {
  510. /*
  511. * reregister request prior to unregistering linger so
  512. * that r_osd is preserved.
  513. */
  514. BUG_ON(!list_empty(&req->r_req_lru_item));
  515. __register_request(osdc, req);
  516. list_add(&req->r_req_lru_item, &osdc->req_unsent);
  517. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  518. __unregister_linger_request(osdc, req);
  519. dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
  520. osd->o_osd);
  521. }
  522. }
  523. /*
  524. * If the osd connection drops, we need to resubmit all requests.
  525. */
  526. static void osd_reset(struct ceph_connection *con)
  527. {
  528. struct ceph_osd *osd = con->private;
  529. struct ceph_osd_client *osdc;
  530. if (!osd)
  531. return;
  532. dout("osd_reset osd%d\n", osd->o_osd);
  533. osdc = osd->o_osdc;
  534. down_read(&osdc->map_sem);
  535. mutex_lock(&osdc->request_mutex);
  536. __kick_osd_requests(osdc, osd);
  537. __send_queued(osdc);
  538. mutex_unlock(&osdc->request_mutex);
  539. up_read(&osdc->map_sem);
  540. }
  541. /*
  542. * Track open sessions with osds.
  543. */
  544. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
  545. {
  546. struct ceph_osd *osd;
  547. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  548. if (!osd)
  549. return NULL;
  550. atomic_set(&osd->o_ref, 1);
  551. osd->o_osdc = osdc;
  552. osd->o_osd = onum;
  553. RB_CLEAR_NODE(&osd->o_node);
  554. INIT_LIST_HEAD(&osd->o_requests);
  555. INIT_LIST_HEAD(&osd->o_linger_requests);
  556. INIT_LIST_HEAD(&osd->o_osd_lru);
  557. osd->o_incarnation = 1;
  558. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  559. INIT_LIST_HEAD(&osd->o_keepalive_item);
  560. return osd;
  561. }
  562. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  563. {
  564. if (atomic_inc_not_zero(&osd->o_ref)) {
  565. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  566. atomic_read(&osd->o_ref));
  567. return osd;
  568. } else {
  569. dout("get_osd %p FAIL\n", osd);
  570. return NULL;
  571. }
  572. }
  573. static void put_osd(struct ceph_osd *osd)
  574. {
  575. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  576. atomic_read(&osd->o_ref) - 1);
  577. if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
  578. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  579. if (ac->ops && ac->ops->destroy_authorizer)
  580. ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
  581. kfree(osd);
  582. }
  583. }
  584. /*
  585. * remove an osd from our map
  586. */
  587. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  588. {
  589. dout("__remove_osd %p\n", osd);
  590. BUG_ON(!list_empty(&osd->o_requests));
  591. rb_erase(&osd->o_node, &osdc->osds);
  592. list_del_init(&osd->o_osd_lru);
  593. ceph_con_close(&osd->o_con);
  594. put_osd(osd);
  595. }
  596. static void remove_all_osds(struct ceph_osd_client *osdc)
  597. {
  598. dout("%s %p\n", __func__, osdc);
  599. mutex_lock(&osdc->request_mutex);
  600. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  601. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  602. struct ceph_osd, o_node);
  603. __remove_osd(osdc, osd);
  604. }
  605. mutex_unlock(&osdc->request_mutex);
  606. }
  607. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  608. struct ceph_osd *osd)
  609. {
  610. dout("__move_osd_to_lru %p\n", osd);
  611. BUG_ON(!list_empty(&osd->o_osd_lru));
  612. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  613. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
  614. }
  615. static void __remove_osd_from_lru(struct ceph_osd *osd)
  616. {
  617. dout("__remove_osd_from_lru %p\n", osd);
  618. if (!list_empty(&osd->o_osd_lru))
  619. list_del_init(&osd->o_osd_lru);
  620. }
  621. static void remove_old_osds(struct ceph_osd_client *osdc)
  622. {
  623. struct ceph_osd *osd, *nosd;
  624. dout("__remove_old_osds %p\n", osdc);
  625. mutex_lock(&osdc->request_mutex);
  626. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  627. if (time_before(jiffies, osd->lru_ttl))
  628. break;
  629. __remove_osd(osdc, osd);
  630. }
  631. mutex_unlock(&osdc->request_mutex);
  632. }
  633. /*
  634. * reset osd connect
  635. */
  636. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  637. {
  638. struct ceph_entity_addr *peer_addr;
  639. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  640. if (list_empty(&osd->o_requests) &&
  641. list_empty(&osd->o_linger_requests)) {
  642. __remove_osd(osdc, osd);
  643. return -ENODEV;
  644. }
  645. peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
  646. if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
  647. !ceph_con_opened(&osd->o_con)) {
  648. struct ceph_osd_request *req;
  649. dout(" osd addr hasn't changed and connection never opened,"
  650. " letting msgr retry");
  651. /* touch each r_stamp for handle_timeout()'s benfit */
  652. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  653. req->r_stamp = jiffies;
  654. return -EAGAIN;
  655. }
  656. ceph_con_close(&osd->o_con);
  657. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
  658. osd->o_incarnation++;
  659. return 0;
  660. }
  661. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  662. {
  663. struct rb_node **p = &osdc->osds.rb_node;
  664. struct rb_node *parent = NULL;
  665. struct ceph_osd *osd = NULL;
  666. dout("__insert_osd %p osd%d\n", new, new->o_osd);
  667. while (*p) {
  668. parent = *p;
  669. osd = rb_entry(parent, struct ceph_osd, o_node);
  670. if (new->o_osd < osd->o_osd)
  671. p = &(*p)->rb_left;
  672. else if (new->o_osd > osd->o_osd)
  673. p = &(*p)->rb_right;
  674. else
  675. BUG();
  676. }
  677. rb_link_node(&new->o_node, parent, p);
  678. rb_insert_color(&new->o_node, &osdc->osds);
  679. }
  680. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  681. {
  682. struct ceph_osd *osd;
  683. struct rb_node *n = osdc->osds.rb_node;
  684. while (n) {
  685. osd = rb_entry(n, struct ceph_osd, o_node);
  686. if (o < osd->o_osd)
  687. n = n->rb_left;
  688. else if (o > osd->o_osd)
  689. n = n->rb_right;
  690. else
  691. return osd;
  692. }
  693. return NULL;
  694. }
  695. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  696. {
  697. schedule_delayed_work(&osdc->timeout_work,
  698. osdc->client->options->osd_keepalive_timeout * HZ);
  699. }
  700. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  701. {
  702. cancel_delayed_work(&osdc->timeout_work);
  703. }
  704. /*
  705. * Register request, assign tid. If this is the first request, set up
  706. * the timeout event.
  707. */
  708. static void __register_request(struct ceph_osd_client *osdc,
  709. struct ceph_osd_request *req)
  710. {
  711. req->r_tid = ++osdc->last_tid;
  712. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  713. dout("__register_request %p tid %lld\n", req, req->r_tid);
  714. __insert_request(osdc, req);
  715. ceph_osdc_get_request(req);
  716. osdc->num_requests++;
  717. if (osdc->num_requests == 1) {
  718. dout(" first request, scheduling timeout\n");
  719. __schedule_osd_timeout(osdc);
  720. }
  721. }
  722. static void register_request(struct ceph_osd_client *osdc,
  723. struct ceph_osd_request *req)
  724. {
  725. mutex_lock(&osdc->request_mutex);
  726. __register_request(osdc, req);
  727. mutex_unlock(&osdc->request_mutex);
  728. }
  729. /*
  730. * called under osdc->request_mutex
  731. */
  732. static void __unregister_request(struct ceph_osd_client *osdc,
  733. struct ceph_osd_request *req)
  734. {
  735. if (RB_EMPTY_NODE(&req->r_node)) {
  736. dout("__unregister_request %p tid %lld not registered\n",
  737. req, req->r_tid);
  738. return;
  739. }
  740. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  741. rb_erase(&req->r_node, &osdc->requests);
  742. osdc->num_requests--;
  743. if (req->r_osd) {
  744. /* make sure the original request isn't in flight. */
  745. ceph_msg_revoke(req->r_request);
  746. list_del_init(&req->r_osd_item);
  747. if (list_empty(&req->r_osd->o_requests) &&
  748. list_empty(&req->r_osd->o_linger_requests)) {
  749. dout("moving osd to %p lru\n", req->r_osd);
  750. __move_osd_to_lru(osdc, req->r_osd);
  751. }
  752. if (list_empty(&req->r_linger_item))
  753. req->r_osd = NULL;
  754. }
  755. list_del_init(&req->r_req_lru_item);
  756. ceph_osdc_put_request(req);
  757. if (osdc->num_requests == 0) {
  758. dout(" no requests, canceling timeout\n");
  759. __cancel_osd_timeout(osdc);
  760. }
  761. }
  762. /*
  763. * Cancel a previously queued request message
  764. */
  765. static void __cancel_request(struct ceph_osd_request *req)
  766. {
  767. if (req->r_sent && req->r_osd) {
  768. ceph_msg_revoke(req->r_request);
  769. req->r_sent = 0;
  770. }
  771. }
  772. static void __register_linger_request(struct ceph_osd_client *osdc,
  773. struct ceph_osd_request *req)
  774. {
  775. dout("__register_linger_request %p\n", req);
  776. list_add_tail(&req->r_linger_item, &osdc->req_linger);
  777. if (req->r_osd)
  778. list_add_tail(&req->r_linger_osd,
  779. &req->r_osd->o_linger_requests);
  780. }
  781. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  782. struct ceph_osd_request *req)
  783. {
  784. dout("__unregister_linger_request %p\n", req);
  785. list_del_init(&req->r_linger_item);
  786. if (req->r_osd) {
  787. list_del_init(&req->r_linger_osd);
  788. if (list_empty(&req->r_osd->o_requests) &&
  789. list_empty(&req->r_osd->o_linger_requests)) {
  790. dout("moving osd to %p lru\n", req->r_osd);
  791. __move_osd_to_lru(osdc, req->r_osd);
  792. }
  793. if (list_empty(&req->r_osd_item))
  794. req->r_osd = NULL;
  795. }
  796. }
  797. void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
  798. struct ceph_osd_request *req)
  799. {
  800. mutex_lock(&osdc->request_mutex);
  801. if (req->r_linger) {
  802. __unregister_linger_request(osdc, req);
  803. ceph_osdc_put_request(req);
  804. }
  805. mutex_unlock(&osdc->request_mutex);
  806. }
  807. EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
  808. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  809. struct ceph_osd_request *req)
  810. {
  811. if (!req->r_linger) {
  812. dout("set_request_linger %p\n", req);
  813. req->r_linger = 1;
  814. /*
  815. * caller is now responsible for calling
  816. * unregister_linger_request
  817. */
  818. ceph_osdc_get_request(req);
  819. }
  820. }
  821. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  822. /*
  823. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  824. * (as needed), and set the request r_osd appropriately. If there is
  825. * no up osd, set r_osd to NULL. Move the request to the appropriate list
  826. * (unsent, homeless) or leave on in-flight lru.
  827. *
  828. * Return 0 if unchanged, 1 if changed, or negative on error.
  829. *
  830. * Caller should hold map_sem for read and request_mutex.
  831. */
  832. static int __map_request(struct ceph_osd_client *osdc,
  833. struct ceph_osd_request *req, int force_resend)
  834. {
  835. struct ceph_pg pgid;
  836. int acting[CEPH_PG_MAX_SIZE];
  837. int o = -1, num = 0;
  838. int err;
  839. dout("map_request %p tid %lld\n", req, req->r_tid);
  840. err = ceph_calc_object_layout(&pgid, req->r_oid,
  841. &req->r_file_layout, osdc->osdmap);
  842. if (err) {
  843. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  844. return err;
  845. }
  846. req->r_pgid = pgid;
  847. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  848. if (err > 0) {
  849. o = acting[0];
  850. num = err;
  851. }
  852. if ((!force_resend &&
  853. req->r_osd && req->r_osd->o_osd == o &&
  854. req->r_sent >= req->r_osd->o_incarnation &&
  855. req->r_num_pg_osds == num &&
  856. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  857. (req->r_osd == NULL && o == -1))
  858. return 0; /* no change */
  859. dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
  860. req->r_tid, pgid.pool, pgid.seed, o,
  861. req->r_osd ? req->r_osd->o_osd : -1);
  862. /* record full pg acting set */
  863. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  864. req->r_num_pg_osds = num;
  865. if (req->r_osd) {
  866. __cancel_request(req);
  867. list_del_init(&req->r_osd_item);
  868. req->r_osd = NULL;
  869. }
  870. req->r_osd = __lookup_osd(osdc, o);
  871. if (!req->r_osd && o >= 0) {
  872. err = -ENOMEM;
  873. req->r_osd = create_osd(osdc, o);
  874. if (!req->r_osd) {
  875. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  876. goto out;
  877. }
  878. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  879. __insert_osd(osdc, req->r_osd);
  880. ceph_con_open(&req->r_osd->o_con,
  881. CEPH_ENTITY_TYPE_OSD, o,
  882. &osdc->osdmap->osd_addr[o]);
  883. }
  884. if (req->r_osd) {
  885. __remove_osd_from_lru(req->r_osd);
  886. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  887. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  888. } else {
  889. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  890. }
  891. err = 1; /* osd or pg changed */
  892. out:
  893. return err;
  894. }
  895. /*
  896. * caller should hold map_sem (for read) and request_mutex
  897. */
  898. static void __send_request(struct ceph_osd_client *osdc,
  899. struct ceph_osd_request *req)
  900. {
  901. void *p;
  902. dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
  903. req, req->r_tid, req->r_osd->o_osd, req->r_flags,
  904. (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
  905. /* fill in message content that changes each time we send it */
  906. put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
  907. put_unaligned_le32(req->r_flags, req->r_request_flags);
  908. put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
  909. p = req->r_request_pgid;
  910. ceph_encode_64(&p, req->r_pgid.pool);
  911. ceph_encode_32(&p, req->r_pgid.seed);
  912. put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
  913. memcpy(req->r_request_reassert_version, &req->r_reassert_version,
  914. sizeof(req->r_reassert_version));
  915. req->r_stamp = jiffies;
  916. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  917. ceph_msg_get(req->r_request); /* send consumes a ref */
  918. ceph_con_send(&req->r_osd->o_con, req->r_request);
  919. req->r_sent = req->r_osd->o_incarnation;
  920. }
  921. /*
  922. * Send any requests in the queue (req_unsent).
  923. */
  924. static void __send_queued(struct ceph_osd_client *osdc)
  925. {
  926. struct ceph_osd_request *req, *tmp;
  927. dout("__send_queued\n");
  928. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
  929. __send_request(osdc, req);
  930. }
  931. /*
  932. * Timeout callback, called every N seconds when 1 or more osd
  933. * requests has been active for more than N seconds. When this
  934. * happens, we ping all OSDs with requests who have timed out to
  935. * ensure any communications channel reset is detected. Reset the
  936. * request timeouts another N seconds in the future as we go.
  937. * Reschedule the timeout event another N seconds in future (unless
  938. * there are no open requests).
  939. */
  940. static void handle_timeout(struct work_struct *work)
  941. {
  942. struct ceph_osd_client *osdc =
  943. container_of(work, struct ceph_osd_client, timeout_work.work);
  944. struct ceph_osd_request *req;
  945. struct ceph_osd *osd;
  946. unsigned long keepalive =
  947. osdc->client->options->osd_keepalive_timeout * HZ;
  948. struct list_head slow_osds;
  949. dout("timeout\n");
  950. down_read(&osdc->map_sem);
  951. ceph_monc_request_next_osdmap(&osdc->client->monc);
  952. mutex_lock(&osdc->request_mutex);
  953. /*
  954. * ping osds that are a bit slow. this ensures that if there
  955. * is a break in the TCP connection we will notice, and reopen
  956. * a connection with that osd (from the fault callback).
  957. */
  958. INIT_LIST_HEAD(&slow_osds);
  959. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  960. if (time_before(jiffies, req->r_stamp + keepalive))
  961. break;
  962. osd = req->r_osd;
  963. BUG_ON(!osd);
  964. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  965. req->r_tid, osd->o_osd);
  966. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  967. }
  968. while (!list_empty(&slow_osds)) {
  969. osd = list_entry(slow_osds.next, struct ceph_osd,
  970. o_keepalive_item);
  971. list_del_init(&osd->o_keepalive_item);
  972. ceph_con_keepalive(&osd->o_con);
  973. }
  974. __schedule_osd_timeout(osdc);
  975. __send_queued(osdc);
  976. mutex_unlock(&osdc->request_mutex);
  977. up_read(&osdc->map_sem);
  978. }
  979. static void handle_osds_timeout(struct work_struct *work)
  980. {
  981. struct ceph_osd_client *osdc =
  982. container_of(work, struct ceph_osd_client,
  983. osds_timeout_work.work);
  984. unsigned long delay =
  985. osdc->client->options->osd_idle_ttl * HZ >> 2;
  986. dout("osds timeout\n");
  987. down_read(&osdc->map_sem);
  988. remove_old_osds(osdc);
  989. up_read(&osdc->map_sem);
  990. schedule_delayed_work(&osdc->osds_timeout_work,
  991. round_jiffies_relative(delay));
  992. }
  993. static void complete_request(struct ceph_osd_request *req)
  994. {
  995. if (req->r_safe_callback)
  996. req->r_safe_callback(req, NULL);
  997. complete_all(&req->r_safe_completion); /* fsync waiter */
  998. }
  999. static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
  1000. {
  1001. __u8 v;
  1002. ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
  1003. v = ceph_decode_8(p);
  1004. if (v > 1) {
  1005. pr_warning("do not understand pg encoding %d > 1", v);
  1006. return -EINVAL;
  1007. }
  1008. pgid->pool = ceph_decode_64(p);
  1009. pgid->seed = ceph_decode_32(p);
  1010. *p += 4;
  1011. return 0;
  1012. bad:
  1013. pr_warning("incomplete pg encoding");
  1014. return -EINVAL;
  1015. }
  1016. /*
  1017. * handle osd op reply. either call the callback if it is specified,
  1018. * or do the completion to wake up the waiting thread.
  1019. */
  1020. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  1021. struct ceph_connection *con)
  1022. {
  1023. void *p, *end;
  1024. struct ceph_osd_request *req;
  1025. u64 tid;
  1026. int object_len;
  1027. int numops, payload_len, flags;
  1028. s32 result;
  1029. s32 retry_attempt;
  1030. struct ceph_pg pg;
  1031. int err;
  1032. u32 reassert_epoch;
  1033. u64 reassert_version;
  1034. u32 osdmap_epoch;
  1035. int already_completed;
  1036. int i;
  1037. tid = le64_to_cpu(msg->hdr.tid);
  1038. dout("handle_reply %p tid %llu\n", msg, tid);
  1039. p = msg->front.iov_base;
  1040. end = p + msg->front.iov_len;
  1041. ceph_decode_need(&p, end, 4, bad);
  1042. object_len = ceph_decode_32(&p);
  1043. ceph_decode_need(&p, end, object_len, bad);
  1044. p += object_len;
  1045. err = __decode_pgid(&p, end, &pg);
  1046. if (err)
  1047. goto bad;
  1048. ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
  1049. flags = ceph_decode_64(&p);
  1050. result = ceph_decode_32(&p);
  1051. reassert_epoch = ceph_decode_32(&p);
  1052. reassert_version = ceph_decode_64(&p);
  1053. osdmap_epoch = ceph_decode_32(&p);
  1054. /* lookup */
  1055. mutex_lock(&osdc->request_mutex);
  1056. req = __lookup_request(osdc, tid);
  1057. if (req == NULL) {
  1058. dout("handle_reply tid %llu dne\n", tid);
  1059. mutex_unlock(&osdc->request_mutex);
  1060. return;
  1061. }
  1062. ceph_osdc_get_request(req);
  1063. dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
  1064. req, result);
  1065. ceph_decode_need(&p, end, 4, bad);
  1066. numops = ceph_decode_32(&p);
  1067. if (numops > CEPH_OSD_MAX_OP)
  1068. goto bad_put;
  1069. if (numops != req->r_num_ops)
  1070. goto bad_put;
  1071. payload_len = 0;
  1072. ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
  1073. for (i = 0; i < numops; i++) {
  1074. struct ceph_osd_op *op = p;
  1075. int len;
  1076. len = le32_to_cpu(op->payload_len);
  1077. req->r_reply_op_len[i] = len;
  1078. dout(" op %d has %d bytes\n", i, len);
  1079. payload_len += len;
  1080. p += sizeof(*op);
  1081. }
  1082. if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
  1083. pr_warning("sum of op payload lens %d != data_len %d",
  1084. payload_len, le32_to_cpu(msg->hdr.data_len));
  1085. goto bad_put;
  1086. }
  1087. ceph_decode_need(&p, end, 4 + numops * 4, bad);
  1088. retry_attempt = ceph_decode_32(&p);
  1089. for (i = 0; i < numops; i++)
  1090. req->r_reply_op_result[i] = ceph_decode_32(&p);
  1091. /*
  1092. * if this connection filled our message, drop our reference now, to
  1093. * avoid a (safe but slower) revoke later.
  1094. */
  1095. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  1096. dout(" dropping con_filling_msg ref %p\n", con);
  1097. req->r_con_filling_msg = NULL;
  1098. con->ops->put(con);
  1099. }
  1100. if (!req->r_got_reply) {
  1101. unsigned int bytes;
  1102. req->r_result = result;
  1103. bytes = le32_to_cpu(msg->hdr.data_len);
  1104. dout("handle_reply result %d bytes %d\n", req->r_result,
  1105. bytes);
  1106. if (req->r_result == 0)
  1107. req->r_result = bytes;
  1108. /* in case this is a write and we need to replay, */
  1109. req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
  1110. req->r_reassert_version.version = cpu_to_le64(reassert_version);
  1111. req->r_got_reply = 1;
  1112. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1113. dout("handle_reply tid %llu dup ack\n", tid);
  1114. mutex_unlock(&osdc->request_mutex);
  1115. goto done;
  1116. }
  1117. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1118. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1119. __register_linger_request(osdc, req);
  1120. /* either this is a read, or we got the safe response */
  1121. if (result < 0 ||
  1122. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1123. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1124. __unregister_request(osdc, req);
  1125. already_completed = req->r_completed;
  1126. req->r_completed = 1;
  1127. mutex_unlock(&osdc->request_mutex);
  1128. if (already_completed)
  1129. goto done;
  1130. if (req->r_callback)
  1131. req->r_callback(req, msg);
  1132. else
  1133. complete_all(&req->r_completion);
  1134. if (flags & CEPH_OSD_FLAG_ONDISK)
  1135. complete_request(req);
  1136. done:
  1137. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1138. ceph_osdc_put_request(req);
  1139. return;
  1140. bad_put:
  1141. ceph_osdc_put_request(req);
  1142. bad:
  1143. pr_err("corrupt osd_op_reply got %d %d\n",
  1144. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
  1145. ceph_msg_dump(msg);
  1146. }
  1147. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1148. {
  1149. struct rb_node *p, *n;
  1150. for (p = rb_first(&osdc->osds); p; p = n) {
  1151. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1152. n = rb_next(p);
  1153. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1154. memcmp(&osd->o_con.peer_addr,
  1155. ceph_osd_addr(osdc->osdmap,
  1156. osd->o_osd),
  1157. sizeof(struct ceph_entity_addr)) != 0)
  1158. __reset_osd(osdc, osd);
  1159. }
  1160. }
  1161. /*
  1162. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1163. * no osd, request a new map.
  1164. *
  1165. * Caller should hold map_sem for read.
  1166. */
  1167. static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
  1168. {
  1169. struct ceph_osd_request *req, *nreq;
  1170. struct rb_node *p;
  1171. int needmap = 0;
  1172. int err;
  1173. dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
  1174. mutex_lock(&osdc->request_mutex);
  1175. for (p = rb_first(&osdc->requests); p; ) {
  1176. req = rb_entry(p, struct ceph_osd_request, r_node);
  1177. p = rb_next(p);
  1178. /*
  1179. * For linger requests that have not yet been
  1180. * registered, move them to the linger list; they'll
  1181. * be sent to the osd in the loop below. Unregister
  1182. * the request before re-registering it as a linger
  1183. * request to ensure the __map_request() below
  1184. * will decide it needs to be sent.
  1185. */
  1186. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1187. dout("%p tid %llu restart on osd%d\n",
  1188. req, req->r_tid,
  1189. req->r_osd ? req->r_osd->o_osd : -1);
  1190. __unregister_request(osdc, req);
  1191. __register_linger_request(osdc, req);
  1192. continue;
  1193. }
  1194. err = __map_request(osdc, req, force_resend);
  1195. if (err < 0)
  1196. continue; /* error */
  1197. if (req->r_osd == NULL) {
  1198. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1199. needmap++; /* request a newer map */
  1200. } else if (err > 0) {
  1201. if (!req->r_linger) {
  1202. dout("%p tid %llu requeued on osd%d\n", req,
  1203. req->r_tid,
  1204. req->r_osd ? req->r_osd->o_osd : -1);
  1205. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1206. }
  1207. }
  1208. }
  1209. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1210. r_linger_item) {
  1211. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1212. err = __map_request(osdc, req, force_resend);
  1213. dout("__map_request returned %d\n", err);
  1214. if (err == 0)
  1215. continue; /* no change and no osd was specified */
  1216. if (err < 0)
  1217. continue; /* hrm! */
  1218. if (req->r_osd == NULL) {
  1219. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1220. needmap++; /* request a newer map */
  1221. continue;
  1222. }
  1223. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1224. req->r_osd ? req->r_osd->o_osd : -1);
  1225. __register_request(osdc, req);
  1226. __unregister_linger_request(osdc, req);
  1227. }
  1228. mutex_unlock(&osdc->request_mutex);
  1229. if (needmap) {
  1230. dout("%d requests for down osds, need new map\n", needmap);
  1231. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1232. }
  1233. reset_changed_osds(osdc);
  1234. }
  1235. /*
  1236. * Process updated osd map.
  1237. *
  1238. * The message contains any number of incremental and full maps, normally
  1239. * indicating some sort of topology change in the cluster. Kick requests
  1240. * off to different OSDs as needed.
  1241. */
  1242. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1243. {
  1244. void *p, *end, *next;
  1245. u32 nr_maps, maplen;
  1246. u32 epoch;
  1247. struct ceph_osdmap *newmap = NULL, *oldmap;
  1248. int err;
  1249. struct ceph_fsid fsid;
  1250. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1251. p = msg->front.iov_base;
  1252. end = p + msg->front.iov_len;
  1253. /* verify fsid */
  1254. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1255. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1256. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1257. return;
  1258. down_write(&osdc->map_sem);
  1259. /* incremental maps */
  1260. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1261. dout(" %d inc maps\n", nr_maps);
  1262. while (nr_maps > 0) {
  1263. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1264. epoch = ceph_decode_32(&p);
  1265. maplen = ceph_decode_32(&p);
  1266. ceph_decode_need(&p, end, maplen, bad);
  1267. next = p + maplen;
  1268. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1269. dout("applying incremental map %u len %d\n",
  1270. epoch, maplen);
  1271. newmap = osdmap_apply_incremental(&p, next,
  1272. osdc->osdmap,
  1273. &osdc->client->msgr);
  1274. if (IS_ERR(newmap)) {
  1275. err = PTR_ERR(newmap);
  1276. goto bad;
  1277. }
  1278. BUG_ON(!newmap);
  1279. if (newmap != osdc->osdmap) {
  1280. ceph_osdmap_destroy(osdc->osdmap);
  1281. osdc->osdmap = newmap;
  1282. }
  1283. kick_requests(osdc, 0);
  1284. } else {
  1285. dout("ignoring incremental map %u len %d\n",
  1286. epoch, maplen);
  1287. }
  1288. p = next;
  1289. nr_maps--;
  1290. }
  1291. if (newmap)
  1292. goto done;
  1293. /* full maps */
  1294. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1295. dout(" %d full maps\n", nr_maps);
  1296. while (nr_maps) {
  1297. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1298. epoch = ceph_decode_32(&p);
  1299. maplen = ceph_decode_32(&p);
  1300. ceph_decode_need(&p, end, maplen, bad);
  1301. if (nr_maps > 1) {
  1302. dout("skipping non-latest full map %u len %d\n",
  1303. epoch, maplen);
  1304. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1305. dout("skipping full map %u len %d, "
  1306. "older than our %u\n", epoch, maplen,
  1307. osdc->osdmap->epoch);
  1308. } else {
  1309. int skipped_map = 0;
  1310. dout("taking full map %u len %d\n", epoch, maplen);
  1311. newmap = osdmap_decode(&p, p+maplen);
  1312. if (IS_ERR(newmap)) {
  1313. err = PTR_ERR(newmap);
  1314. goto bad;
  1315. }
  1316. BUG_ON(!newmap);
  1317. oldmap = osdc->osdmap;
  1318. osdc->osdmap = newmap;
  1319. if (oldmap) {
  1320. if (oldmap->epoch + 1 < newmap->epoch)
  1321. skipped_map = 1;
  1322. ceph_osdmap_destroy(oldmap);
  1323. }
  1324. kick_requests(osdc, skipped_map);
  1325. }
  1326. p += maplen;
  1327. nr_maps--;
  1328. }
  1329. done:
  1330. downgrade_write(&osdc->map_sem);
  1331. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1332. /*
  1333. * subscribe to subsequent osdmap updates if full to ensure
  1334. * we find out when we are no longer full and stop returning
  1335. * ENOSPC.
  1336. */
  1337. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  1338. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1339. mutex_lock(&osdc->request_mutex);
  1340. __send_queued(osdc);
  1341. mutex_unlock(&osdc->request_mutex);
  1342. up_read(&osdc->map_sem);
  1343. wake_up_all(&osdc->client->auth_wq);
  1344. return;
  1345. bad:
  1346. pr_err("osdc handle_map corrupt msg\n");
  1347. ceph_msg_dump(msg);
  1348. up_write(&osdc->map_sem);
  1349. return;
  1350. }
  1351. /*
  1352. * watch/notify callback event infrastructure
  1353. *
  1354. * These callbacks are used both for watch and notify operations.
  1355. */
  1356. static void __release_event(struct kref *kref)
  1357. {
  1358. struct ceph_osd_event *event =
  1359. container_of(kref, struct ceph_osd_event, kref);
  1360. dout("__release_event %p\n", event);
  1361. kfree(event);
  1362. }
  1363. static void get_event(struct ceph_osd_event *event)
  1364. {
  1365. kref_get(&event->kref);
  1366. }
  1367. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1368. {
  1369. kref_put(&event->kref, __release_event);
  1370. }
  1371. EXPORT_SYMBOL(ceph_osdc_put_event);
  1372. static void __insert_event(struct ceph_osd_client *osdc,
  1373. struct ceph_osd_event *new)
  1374. {
  1375. struct rb_node **p = &osdc->event_tree.rb_node;
  1376. struct rb_node *parent = NULL;
  1377. struct ceph_osd_event *event = NULL;
  1378. while (*p) {
  1379. parent = *p;
  1380. event = rb_entry(parent, struct ceph_osd_event, node);
  1381. if (new->cookie < event->cookie)
  1382. p = &(*p)->rb_left;
  1383. else if (new->cookie > event->cookie)
  1384. p = &(*p)->rb_right;
  1385. else
  1386. BUG();
  1387. }
  1388. rb_link_node(&new->node, parent, p);
  1389. rb_insert_color(&new->node, &osdc->event_tree);
  1390. }
  1391. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1392. u64 cookie)
  1393. {
  1394. struct rb_node **p = &osdc->event_tree.rb_node;
  1395. struct rb_node *parent = NULL;
  1396. struct ceph_osd_event *event = NULL;
  1397. while (*p) {
  1398. parent = *p;
  1399. event = rb_entry(parent, struct ceph_osd_event, node);
  1400. if (cookie < event->cookie)
  1401. p = &(*p)->rb_left;
  1402. else if (cookie > event->cookie)
  1403. p = &(*p)->rb_right;
  1404. else
  1405. return event;
  1406. }
  1407. return NULL;
  1408. }
  1409. static void __remove_event(struct ceph_osd_event *event)
  1410. {
  1411. struct ceph_osd_client *osdc = event->osdc;
  1412. if (!RB_EMPTY_NODE(&event->node)) {
  1413. dout("__remove_event removed %p\n", event);
  1414. rb_erase(&event->node, &osdc->event_tree);
  1415. ceph_osdc_put_event(event);
  1416. } else {
  1417. dout("__remove_event didn't remove %p\n", event);
  1418. }
  1419. }
  1420. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1421. void (*event_cb)(u64, u64, u8, void *),
  1422. void *data, struct ceph_osd_event **pevent)
  1423. {
  1424. struct ceph_osd_event *event;
  1425. event = kmalloc(sizeof(*event), GFP_NOIO);
  1426. if (!event)
  1427. return -ENOMEM;
  1428. dout("create_event %p\n", event);
  1429. event->cb = event_cb;
  1430. event->one_shot = 0;
  1431. event->data = data;
  1432. event->osdc = osdc;
  1433. INIT_LIST_HEAD(&event->osd_node);
  1434. RB_CLEAR_NODE(&event->node);
  1435. kref_init(&event->kref); /* one ref for us */
  1436. kref_get(&event->kref); /* one ref for the caller */
  1437. spin_lock(&osdc->event_lock);
  1438. event->cookie = ++osdc->event_count;
  1439. __insert_event(osdc, event);
  1440. spin_unlock(&osdc->event_lock);
  1441. *pevent = event;
  1442. return 0;
  1443. }
  1444. EXPORT_SYMBOL(ceph_osdc_create_event);
  1445. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1446. {
  1447. struct ceph_osd_client *osdc = event->osdc;
  1448. dout("cancel_event %p\n", event);
  1449. spin_lock(&osdc->event_lock);
  1450. __remove_event(event);
  1451. spin_unlock(&osdc->event_lock);
  1452. ceph_osdc_put_event(event); /* caller's */
  1453. }
  1454. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1455. static void do_event_work(struct work_struct *work)
  1456. {
  1457. struct ceph_osd_event_work *event_work =
  1458. container_of(work, struct ceph_osd_event_work, work);
  1459. struct ceph_osd_event *event = event_work->event;
  1460. u64 ver = event_work->ver;
  1461. u64 notify_id = event_work->notify_id;
  1462. u8 opcode = event_work->opcode;
  1463. dout("do_event_work completing %p\n", event);
  1464. event->cb(ver, notify_id, opcode, event->data);
  1465. dout("do_event_work completed %p\n", event);
  1466. ceph_osdc_put_event(event);
  1467. kfree(event_work);
  1468. }
  1469. /*
  1470. * Process osd watch notifications
  1471. */
  1472. static void handle_watch_notify(struct ceph_osd_client *osdc,
  1473. struct ceph_msg *msg)
  1474. {
  1475. void *p, *end;
  1476. u8 proto_ver;
  1477. u64 cookie, ver, notify_id;
  1478. u8 opcode;
  1479. struct ceph_osd_event *event;
  1480. struct ceph_osd_event_work *event_work;
  1481. p = msg->front.iov_base;
  1482. end = p + msg->front.iov_len;
  1483. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1484. ceph_decode_8_safe(&p, end, opcode, bad);
  1485. ceph_decode_64_safe(&p, end, cookie, bad);
  1486. ceph_decode_64_safe(&p, end, ver, bad);
  1487. ceph_decode_64_safe(&p, end, notify_id, bad);
  1488. spin_lock(&osdc->event_lock);
  1489. event = __find_event(osdc, cookie);
  1490. if (event) {
  1491. BUG_ON(event->one_shot);
  1492. get_event(event);
  1493. }
  1494. spin_unlock(&osdc->event_lock);
  1495. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1496. cookie, ver, event);
  1497. if (event) {
  1498. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1499. if (!event_work) {
  1500. dout("ERROR: could not allocate event_work\n");
  1501. goto done_err;
  1502. }
  1503. INIT_WORK(&event_work->work, do_event_work);
  1504. event_work->event = event;
  1505. event_work->ver = ver;
  1506. event_work->notify_id = notify_id;
  1507. event_work->opcode = opcode;
  1508. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1509. dout("WARNING: failed to queue notify event work\n");
  1510. goto done_err;
  1511. }
  1512. }
  1513. return;
  1514. done_err:
  1515. ceph_osdc_put_event(event);
  1516. return;
  1517. bad:
  1518. pr_err("osdc handle_watch_notify corrupt msg\n");
  1519. return;
  1520. }
  1521. /*
  1522. * Register request, send initial attempt.
  1523. */
  1524. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1525. struct ceph_osd_request *req,
  1526. bool nofail)
  1527. {
  1528. int rc = 0;
  1529. req->r_request->pages = req->r_pages;
  1530. req->r_request->page_count = req->r_num_pages;
  1531. req->r_request->page_alignment = req->r_page_alignment;
  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->page_count = 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. };