osd_client.c 56 KB

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