osd_client.c 57 KB

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