osd_client.c 55 KB

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