osd_client.c 56 KB

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