osd_client.c 56 KB

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