osd_client.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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 int __kick_requests(struct ceph_osd_client *osdc,
  22. struct ceph_osd *kickosd);
  23. static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  24. static int op_needs_trail(int op)
  25. {
  26. switch (op) {
  27. case CEPH_OSD_OP_GETXATTR:
  28. case CEPH_OSD_OP_SETXATTR:
  29. case CEPH_OSD_OP_CMPXATTR:
  30. case CEPH_OSD_OP_CALL:
  31. return 1;
  32. default:
  33. return 0;
  34. }
  35. }
  36. static int op_has_extent(int op)
  37. {
  38. return (op == CEPH_OSD_OP_READ ||
  39. op == CEPH_OSD_OP_WRITE);
  40. }
  41. void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
  42. struct ceph_file_layout *layout,
  43. u64 snapid,
  44. u64 off, u64 *plen, u64 *bno,
  45. struct ceph_osd_request *req,
  46. struct ceph_osd_req_op *op)
  47. {
  48. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  49. u64 orig_len = *plen;
  50. u64 objoff, objlen; /* extent in object */
  51. reqhead->snapid = cpu_to_le64(snapid);
  52. /* object extent? */
  53. ceph_calc_file_object_mapping(layout, off, plen, bno,
  54. &objoff, &objlen);
  55. if (*plen < orig_len)
  56. dout(" skipping last %llu, final file extent %llu~%llu\n",
  57. orig_len - *plen, off, *plen);
  58. if (op_has_extent(op->op)) {
  59. op->extent.offset = objoff;
  60. op->extent.length = objlen;
  61. }
  62. req->r_num_pages = calc_pages_for(off, *plen);
  63. if (op->op == CEPH_OSD_OP_WRITE)
  64. op->payload_len = *plen;
  65. dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
  66. *bno, objoff, objlen, req->r_num_pages);
  67. }
  68. EXPORT_SYMBOL(ceph_calc_raw_layout);
  69. /*
  70. * Implement client access to distributed object storage cluster.
  71. *
  72. * All data objects are stored within a cluster/cloud of OSDs, or
  73. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  74. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  75. * remote daemons serving up and coordinating consistent and safe
  76. * access to storage.
  77. *
  78. * Cluster membership and the mapping of data objects onto storage devices
  79. * are described by the osd map.
  80. *
  81. * We keep track of pending OSD requests (read, write), resubmit
  82. * requests to different OSDs when the cluster topology/data layout
  83. * change, or retry the affected requests when the communications
  84. * channel with an OSD is reset.
  85. */
  86. /*
  87. * calculate the mapping of a file extent onto an object, and fill out the
  88. * request accordingly. shorten extent as necessary if it crosses an
  89. * object boundary.
  90. *
  91. * fill osd op in request message.
  92. */
  93. static void calc_layout(struct ceph_osd_client *osdc,
  94. struct ceph_vino vino,
  95. struct ceph_file_layout *layout,
  96. u64 off, u64 *plen,
  97. struct ceph_osd_request *req,
  98. struct ceph_osd_req_op *op)
  99. {
  100. u64 bno;
  101. ceph_calc_raw_layout(osdc, layout, vino.snap, off,
  102. plen, &bno, req, op);
  103. sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
  104. req->r_oid_len = strlen(req->r_oid);
  105. }
  106. /*
  107. * requests
  108. */
  109. void ceph_osdc_release_request(struct kref *kref)
  110. {
  111. struct ceph_osd_request *req = container_of(kref,
  112. struct ceph_osd_request,
  113. r_kref);
  114. if (req->r_request)
  115. ceph_msg_put(req->r_request);
  116. if (req->r_reply)
  117. ceph_msg_put(req->r_reply);
  118. if (req->r_con_filling_msg) {
  119. dout("release_request revoking pages %p from con %p\n",
  120. req->r_pages, req->r_con_filling_msg);
  121. ceph_con_revoke_message(req->r_con_filling_msg,
  122. req->r_reply);
  123. ceph_con_put(req->r_con_filling_msg);
  124. }
  125. if (req->r_own_pages)
  126. ceph_release_page_vector(req->r_pages,
  127. req->r_num_pages);
  128. #ifdef CONFIG_BLOCK
  129. if (req->r_bio)
  130. bio_put(req->r_bio);
  131. #endif
  132. ceph_put_snap_context(req->r_snapc);
  133. if (req->r_trail) {
  134. ceph_pagelist_release(req->r_trail);
  135. kfree(req->r_trail);
  136. }
  137. if (req->r_mempool)
  138. mempool_free(req, req->r_osdc->req_mempool);
  139. else
  140. kfree(req);
  141. }
  142. EXPORT_SYMBOL(ceph_osdc_release_request);
  143. static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
  144. {
  145. int i = 0;
  146. if (needs_trail)
  147. *needs_trail = 0;
  148. while (ops[i].op) {
  149. if (needs_trail && op_needs_trail(ops[i].op))
  150. *needs_trail = 1;
  151. i++;
  152. }
  153. return i;
  154. }
  155. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  156. int flags,
  157. struct ceph_snap_context *snapc,
  158. struct ceph_osd_req_op *ops,
  159. bool use_mempool,
  160. gfp_t gfp_flags,
  161. struct page **pages,
  162. struct bio *bio)
  163. {
  164. struct ceph_osd_request *req;
  165. struct ceph_msg *msg;
  166. int needs_trail;
  167. int num_op = get_num_ops(ops, &needs_trail);
  168. size_t msg_size = sizeof(struct ceph_osd_request_head);
  169. msg_size += num_op*sizeof(struct ceph_osd_op);
  170. if (use_mempool) {
  171. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  172. memset(req, 0, sizeof(*req));
  173. } else {
  174. req = kzalloc(sizeof(*req), gfp_flags);
  175. }
  176. if (req == NULL)
  177. return NULL;
  178. req->r_osdc = osdc;
  179. req->r_mempool = use_mempool;
  180. kref_init(&req->r_kref);
  181. init_completion(&req->r_completion);
  182. init_completion(&req->r_safe_completion);
  183. INIT_LIST_HEAD(&req->r_unsafe_item);
  184. req->r_flags = flags;
  185. WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
  186. /* create reply message */
  187. if (use_mempool)
  188. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
  189. else
  190. msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
  191. OSD_OPREPLY_FRONT_LEN, gfp_flags);
  192. if (!msg) {
  193. ceph_osdc_put_request(req);
  194. return NULL;
  195. }
  196. req->r_reply = msg;
  197. /* allocate space for the trailing data */
  198. if (needs_trail) {
  199. req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
  200. if (!req->r_trail) {
  201. ceph_osdc_put_request(req);
  202. return NULL;
  203. }
  204. ceph_pagelist_init(req->r_trail);
  205. }
  206. /* create request message; allow space for oid */
  207. msg_size += 40;
  208. if (snapc)
  209. msg_size += sizeof(u64) * snapc->num_snaps;
  210. if (use_mempool)
  211. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  212. else
  213. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags);
  214. if (!msg) {
  215. ceph_osdc_put_request(req);
  216. return NULL;
  217. }
  218. msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
  219. memset(msg->front.iov_base, 0, msg->front.iov_len);
  220. req->r_request = msg;
  221. req->r_pages = pages;
  222. #ifdef CONFIG_BLOCK
  223. if (bio) {
  224. req->r_bio = bio;
  225. bio_get(req->r_bio);
  226. }
  227. #endif
  228. return req;
  229. }
  230. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  231. static void osd_req_encode_op(struct ceph_osd_request *req,
  232. struct ceph_osd_op *dst,
  233. struct ceph_osd_req_op *src)
  234. {
  235. dst->op = cpu_to_le16(src->op);
  236. switch (dst->op) {
  237. case CEPH_OSD_OP_READ:
  238. case CEPH_OSD_OP_WRITE:
  239. dst->extent.offset =
  240. cpu_to_le64(src->extent.offset);
  241. dst->extent.length =
  242. cpu_to_le64(src->extent.length);
  243. dst->extent.truncate_size =
  244. cpu_to_le64(src->extent.truncate_size);
  245. dst->extent.truncate_seq =
  246. cpu_to_le32(src->extent.truncate_seq);
  247. break;
  248. case CEPH_OSD_OP_GETXATTR:
  249. case CEPH_OSD_OP_SETXATTR:
  250. case CEPH_OSD_OP_CMPXATTR:
  251. BUG_ON(!req->r_trail);
  252. dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
  253. dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
  254. dst->xattr.cmp_op = src->xattr.cmp_op;
  255. dst->xattr.cmp_mode = src->xattr.cmp_mode;
  256. ceph_pagelist_append(req->r_trail, src->xattr.name,
  257. src->xattr.name_len);
  258. ceph_pagelist_append(req->r_trail, src->xattr.val,
  259. src->xattr.value_len);
  260. break;
  261. case CEPH_OSD_OP_CALL:
  262. BUG_ON(!req->r_trail);
  263. dst->cls.class_len = src->cls.class_len;
  264. dst->cls.method_len = src->cls.method_len;
  265. dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
  266. ceph_pagelist_append(req->r_trail, src->cls.class_name,
  267. src->cls.class_len);
  268. ceph_pagelist_append(req->r_trail, src->cls.method_name,
  269. src->cls.method_len);
  270. ceph_pagelist_append(req->r_trail, src->cls.indata,
  271. src->cls.indata_len);
  272. break;
  273. case CEPH_OSD_OP_ROLLBACK:
  274. dst->snap.snapid = cpu_to_le64(src->snap.snapid);
  275. break;
  276. case CEPH_OSD_OP_STARTSYNC:
  277. break;
  278. default:
  279. pr_err("unrecognized osd opcode %d\n", dst->op);
  280. WARN_ON(1);
  281. break;
  282. }
  283. dst->payload_len = cpu_to_le32(src->payload_len);
  284. }
  285. /*
  286. * build new request AND message
  287. *
  288. */
  289. void ceph_osdc_build_request(struct ceph_osd_request *req,
  290. u64 off, u64 *plen,
  291. struct ceph_osd_req_op *src_ops,
  292. struct ceph_snap_context *snapc,
  293. struct timespec *mtime,
  294. const char *oid,
  295. int oid_len)
  296. {
  297. struct ceph_msg *msg = req->r_request;
  298. struct ceph_osd_request_head *head;
  299. struct ceph_osd_req_op *src_op;
  300. struct ceph_osd_op *op;
  301. void *p;
  302. int num_op = get_num_ops(src_ops, NULL);
  303. size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
  304. int flags = req->r_flags;
  305. u64 data_len = 0;
  306. int i;
  307. head = msg->front.iov_base;
  308. op = (void *)(head + 1);
  309. p = (void *)(op + num_op);
  310. req->r_snapc = ceph_get_snap_context(snapc);
  311. head->client_inc = cpu_to_le32(1); /* always, for now. */
  312. head->flags = cpu_to_le32(flags);
  313. if (flags & CEPH_OSD_FLAG_WRITE)
  314. ceph_encode_timespec(&head->mtime, mtime);
  315. head->num_ops = cpu_to_le16(num_op);
  316. /* fill in oid */
  317. head->object_len = cpu_to_le32(oid_len);
  318. memcpy(p, oid, oid_len);
  319. p += oid_len;
  320. src_op = src_ops;
  321. while (src_op->op) {
  322. osd_req_encode_op(req, op, src_op);
  323. src_op++;
  324. op++;
  325. }
  326. if (req->r_trail)
  327. data_len += req->r_trail->length;
  328. if (snapc) {
  329. head->snap_seq = cpu_to_le64(snapc->seq);
  330. head->num_snaps = cpu_to_le32(snapc->num_snaps);
  331. for (i = 0; i < snapc->num_snaps; i++) {
  332. put_unaligned_le64(snapc->snaps[i], p);
  333. p += sizeof(u64);
  334. }
  335. }
  336. if (flags & CEPH_OSD_FLAG_WRITE) {
  337. req->r_request->hdr.data_off = cpu_to_le16(off);
  338. req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
  339. } else if (data_len) {
  340. req->r_request->hdr.data_off = 0;
  341. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  342. }
  343. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  344. msg_size = p - msg->front.iov_base;
  345. msg->front.iov_len = msg_size;
  346. msg->hdr.front_len = cpu_to_le32(msg_size);
  347. return;
  348. }
  349. EXPORT_SYMBOL(ceph_osdc_build_request);
  350. /*
  351. * build new request AND message, calculate layout, and adjust file
  352. * extent as needed.
  353. *
  354. * if the file was recently truncated, we include information about its
  355. * old and new size so that the object can be updated appropriately. (we
  356. * avoid synchronously deleting truncated objects because it's slow.)
  357. *
  358. * if @do_sync, include a 'startsync' command so that the osd will flush
  359. * data quickly.
  360. */
  361. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  362. struct ceph_file_layout *layout,
  363. struct ceph_vino vino,
  364. u64 off, u64 *plen,
  365. int opcode, int flags,
  366. struct ceph_snap_context *snapc,
  367. int do_sync,
  368. u32 truncate_seq,
  369. u64 truncate_size,
  370. struct timespec *mtime,
  371. bool use_mempool, int num_reply)
  372. {
  373. struct ceph_osd_req_op ops[3];
  374. struct ceph_osd_request *req;
  375. ops[0].op = opcode;
  376. ops[0].extent.truncate_seq = truncate_seq;
  377. ops[0].extent.truncate_size = truncate_size;
  378. ops[0].payload_len = 0;
  379. if (do_sync) {
  380. ops[1].op = CEPH_OSD_OP_STARTSYNC;
  381. ops[1].payload_len = 0;
  382. ops[2].op = 0;
  383. } else
  384. ops[1].op = 0;
  385. req = ceph_osdc_alloc_request(osdc, flags,
  386. snapc, ops,
  387. use_mempool,
  388. GFP_NOFS, NULL, NULL);
  389. if (IS_ERR(req))
  390. return req;
  391. /* calculate max write size */
  392. calc_layout(osdc, vino, layout, off, plen, req, ops);
  393. req->r_file_layout = *layout; /* keep a copy */
  394. ceph_osdc_build_request(req, off, plen, ops,
  395. snapc,
  396. mtime,
  397. req->r_oid, req->r_oid_len);
  398. return req;
  399. }
  400. EXPORT_SYMBOL(ceph_osdc_new_request);
  401. /*
  402. * We keep osd requests in an rbtree, sorted by ->r_tid.
  403. */
  404. static void __insert_request(struct ceph_osd_client *osdc,
  405. struct ceph_osd_request *new)
  406. {
  407. struct rb_node **p = &osdc->requests.rb_node;
  408. struct rb_node *parent = NULL;
  409. struct ceph_osd_request *req = NULL;
  410. while (*p) {
  411. parent = *p;
  412. req = rb_entry(parent, struct ceph_osd_request, r_node);
  413. if (new->r_tid < req->r_tid)
  414. p = &(*p)->rb_left;
  415. else if (new->r_tid > req->r_tid)
  416. p = &(*p)->rb_right;
  417. else
  418. BUG();
  419. }
  420. rb_link_node(&new->r_node, parent, p);
  421. rb_insert_color(&new->r_node, &osdc->requests);
  422. }
  423. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  424. u64 tid)
  425. {
  426. struct ceph_osd_request *req;
  427. struct rb_node *n = osdc->requests.rb_node;
  428. while (n) {
  429. req = rb_entry(n, struct ceph_osd_request, r_node);
  430. if (tid < req->r_tid)
  431. n = n->rb_left;
  432. else if (tid > req->r_tid)
  433. n = n->rb_right;
  434. else
  435. return req;
  436. }
  437. return NULL;
  438. }
  439. static struct ceph_osd_request *
  440. __lookup_request_ge(struct ceph_osd_client *osdc,
  441. u64 tid)
  442. {
  443. struct ceph_osd_request *req;
  444. struct rb_node *n = osdc->requests.rb_node;
  445. while (n) {
  446. req = rb_entry(n, struct ceph_osd_request, r_node);
  447. if (tid < req->r_tid) {
  448. if (!n->rb_left)
  449. return req;
  450. n = n->rb_left;
  451. } else if (tid > req->r_tid) {
  452. n = n->rb_right;
  453. } else {
  454. return req;
  455. }
  456. }
  457. return NULL;
  458. }
  459. /*
  460. * If the osd connection drops, we need to resubmit all requests.
  461. */
  462. static void osd_reset(struct ceph_connection *con)
  463. {
  464. struct ceph_osd *osd = con->private;
  465. struct ceph_osd_client *osdc;
  466. if (!osd)
  467. return;
  468. dout("osd_reset osd%d\n", osd->o_osd);
  469. osdc = osd->o_osdc;
  470. down_read(&osdc->map_sem);
  471. kick_requests(osdc, osd);
  472. up_read(&osdc->map_sem);
  473. }
  474. /*
  475. * Track open sessions with osds.
  476. */
  477. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
  478. {
  479. struct ceph_osd *osd;
  480. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  481. if (!osd)
  482. return NULL;
  483. atomic_set(&osd->o_ref, 1);
  484. osd->o_osdc = osdc;
  485. INIT_LIST_HEAD(&osd->o_requests);
  486. INIT_LIST_HEAD(&osd->o_osd_lru);
  487. osd->o_incarnation = 1;
  488. ceph_con_init(osdc->client->msgr, &osd->o_con);
  489. osd->o_con.private = osd;
  490. osd->o_con.ops = &osd_con_ops;
  491. osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
  492. INIT_LIST_HEAD(&osd->o_keepalive_item);
  493. return osd;
  494. }
  495. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  496. {
  497. if (atomic_inc_not_zero(&osd->o_ref)) {
  498. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  499. atomic_read(&osd->o_ref));
  500. return osd;
  501. } else {
  502. dout("get_osd %p FAIL\n", osd);
  503. return NULL;
  504. }
  505. }
  506. static void put_osd(struct ceph_osd *osd)
  507. {
  508. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  509. atomic_read(&osd->o_ref) - 1);
  510. if (atomic_dec_and_test(&osd->o_ref)) {
  511. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  512. if (osd->o_authorizer)
  513. ac->ops->destroy_authorizer(ac, osd->o_authorizer);
  514. kfree(osd);
  515. }
  516. }
  517. /*
  518. * remove an osd from our map
  519. */
  520. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  521. {
  522. dout("__remove_osd %p\n", osd);
  523. BUG_ON(!list_empty(&osd->o_requests));
  524. rb_erase(&osd->o_node, &osdc->osds);
  525. list_del_init(&osd->o_osd_lru);
  526. ceph_con_close(&osd->o_con);
  527. put_osd(osd);
  528. }
  529. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  530. struct ceph_osd *osd)
  531. {
  532. dout("__move_osd_to_lru %p\n", osd);
  533. BUG_ON(!list_empty(&osd->o_osd_lru));
  534. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  535. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
  536. }
  537. static void __remove_osd_from_lru(struct ceph_osd *osd)
  538. {
  539. dout("__remove_osd_from_lru %p\n", osd);
  540. if (!list_empty(&osd->o_osd_lru))
  541. list_del_init(&osd->o_osd_lru);
  542. }
  543. static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
  544. {
  545. struct ceph_osd *osd, *nosd;
  546. dout("__remove_old_osds %p\n", osdc);
  547. mutex_lock(&osdc->request_mutex);
  548. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  549. if (!remove_all && time_before(jiffies, osd->lru_ttl))
  550. break;
  551. __remove_osd(osdc, osd);
  552. }
  553. mutex_unlock(&osdc->request_mutex);
  554. }
  555. /*
  556. * reset osd connect
  557. */
  558. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  559. {
  560. struct ceph_osd_request *req;
  561. int ret = 0;
  562. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  563. if (list_empty(&osd->o_requests)) {
  564. __remove_osd(osdc, osd);
  565. } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
  566. &osd->o_con.peer_addr,
  567. sizeof(osd->o_con.peer_addr)) == 0 &&
  568. !ceph_con_opened(&osd->o_con)) {
  569. dout(" osd addr hasn't changed and connection never opened,"
  570. " letting msgr retry");
  571. /* touch each r_stamp for handle_timeout()'s benfit */
  572. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  573. req->r_stamp = jiffies;
  574. ret = -EAGAIN;
  575. } else {
  576. ceph_con_close(&osd->o_con);
  577. ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
  578. osd->o_incarnation++;
  579. }
  580. return ret;
  581. }
  582. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  583. {
  584. struct rb_node **p = &osdc->osds.rb_node;
  585. struct rb_node *parent = NULL;
  586. struct ceph_osd *osd = NULL;
  587. while (*p) {
  588. parent = *p;
  589. osd = rb_entry(parent, struct ceph_osd, o_node);
  590. if (new->o_osd < osd->o_osd)
  591. p = &(*p)->rb_left;
  592. else if (new->o_osd > osd->o_osd)
  593. p = &(*p)->rb_right;
  594. else
  595. BUG();
  596. }
  597. rb_link_node(&new->o_node, parent, p);
  598. rb_insert_color(&new->o_node, &osdc->osds);
  599. }
  600. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  601. {
  602. struct ceph_osd *osd;
  603. struct rb_node *n = osdc->osds.rb_node;
  604. while (n) {
  605. osd = rb_entry(n, struct ceph_osd, o_node);
  606. if (o < osd->o_osd)
  607. n = n->rb_left;
  608. else if (o > osd->o_osd)
  609. n = n->rb_right;
  610. else
  611. return osd;
  612. }
  613. return NULL;
  614. }
  615. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  616. {
  617. schedule_delayed_work(&osdc->timeout_work,
  618. osdc->client->options->osd_keepalive_timeout * HZ);
  619. }
  620. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  621. {
  622. cancel_delayed_work(&osdc->timeout_work);
  623. }
  624. /*
  625. * Register request, assign tid. If this is the first request, set up
  626. * the timeout event.
  627. */
  628. static void register_request(struct ceph_osd_client *osdc,
  629. struct ceph_osd_request *req)
  630. {
  631. mutex_lock(&osdc->request_mutex);
  632. req->r_tid = ++osdc->last_tid;
  633. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  634. INIT_LIST_HEAD(&req->r_req_lru_item);
  635. dout("register_request %p tid %lld\n", req, req->r_tid);
  636. __insert_request(osdc, req);
  637. ceph_osdc_get_request(req);
  638. osdc->num_requests++;
  639. if (osdc->num_requests == 1) {
  640. dout(" first request, scheduling timeout\n");
  641. __schedule_osd_timeout(osdc);
  642. }
  643. mutex_unlock(&osdc->request_mutex);
  644. }
  645. /*
  646. * called under osdc->request_mutex
  647. */
  648. static void __unregister_request(struct ceph_osd_client *osdc,
  649. struct ceph_osd_request *req)
  650. {
  651. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  652. rb_erase(&req->r_node, &osdc->requests);
  653. osdc->num_requests--;
  654. if (req->r_osd) {
  655. /* make sure the original request isn't in flight. */
  656. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  657. list_del_init(&req->r_osd_item);
  658. if (list_empty(&req->r_osd->o_requests))
  659. __move_osd_to_lru(osdc, req->r_osd);
  660. req->r_osd = NULL;
  661. }
  662. ceph_osdc_put_request(req);
  663. list_del_init(&req->r_req_lru_item);
  664. if (osdc->num_requests == 0) {
  665. dout(" no requests, canceling timeout\n");
  666. __cancel_osd_timeout(osdc);
  667. }
  668. }
  669. /*
  670. * Cancel a previously queued request message
  671. */
  672. static void __cancel_request(struct ceph_osd_request *req)
  673. {
  674. if (req->r_sent && req->r_osd) {
  675. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  676. req->r_sent = 0;
  677. }
  678. list_del_init(&req->r_req_lru_item);
  679. }
  680. /*
  681. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  682. * (as needed), and set the request r_osd appropriately. If there is
  683. * no up osd, set r_osd to NULL.
  684. *
  685. * Return 0 if unchanged, 1 if changed, or negative on error.
  686. *
  687. * Caller should hold map_sem for read and request_mutex.
  688. */
  689. static int __map_osds(struct ceph_osd_client *osdc,
  690. struct ceph_osd_request *req)
  691. {
  692. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  693. struct ceph_pg pgid;
  694. int acting[CEPH_PG_MAX_SIZE];
  695. int o = -1, num = 0;
  696. int err;
  697. dout("map_osds %p tid %lld\n", req, req->r_tid);
  698. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  699. &req->r_file_layout, osdc->osdmap);
  700. if (err)
  701. return err;
  702. pgid = reqhead->layout.ol_pgid;
  703. req->r_pgid = pgid;
  704. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  705. if (err > 0) {
  706. o = acting[0];
  707. num = err;
  708. }
  709. if ((req->r_osd && req->r_osd->o_osd == o &&
  710. req->r_sent >= req->r_osd->o_incarnation &&
  711. req->r_num_pg_osds == num &&
  712. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  713. (req->r_osd == NULL && o == -1))
  714. return 0; /* no change */
  715. dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
  716. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  717. req->r_osd ? req->r_osd->o_osd : -1);
  718. /* record full pg acting set */
  719. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  720. req->r_num_pg_osds = num;
  721. if (req->r_osd) {
  722. __cancel_request(req);
  723. list_del_init(&req->r_osd_item);
  724. req->r_osd = NULL;
  725. }
  726. req->r_osd = __lookup_osd(osdc, o);
  727. if (!req->r_osd && o >= 0) {
  728. err = -ENOMEM;
  729. req->r_osd = create_osd(osdc);
  730. if (!req->r_osd)
  731. goto out;
  732. dout("map_osds osd %p is osd%d\n", req->r_osd, o);
  733. req->r_osd->o_osd = o;
  734. req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
  735. __insert_osd(osdc, req->r_osd);
  736. ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
  737. }
  738. if (req->r_osd) {
  739. __remove_osd_from_lru(req->r_osd);
  740. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  741. }
  742. err = 1; /* osd or pg changed */
  743. out:
  744. return err;
  745. }
  746. /*
  747. * caller should hold map_sem (for read) and request_mutex
  748. */
  749. static int __send_request(struct ceph_osd_client *osdc,
  750. struct ceph_osd_request *req)
  751. {
  752. struct ceph_osd_request_head *reqhead;
  753. int err;
  754. err = __map_osds(osdc, req);
  755. if (err < 0)
  756. return err;
  757. if (req->r_osd == NULL) {
  758. dout("send_request %p no up osds in pg\n", req);
  759. ceph_monc_request_next_osdmap(&osdc->client->monc);
  760. return 0;
  761. }
  762. dout("send_request %p tid %llu to osd%d flags %d\n",
  763. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  764. reqhead = req->r_request->front.iov_base;
  765. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  766. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  767. reqhead->reassert_version = req->r_reassert_version;
  768. req->r_stamp = jiffies;
  769. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  770. ceph_msg_get(req->r_request); /* send consumes a ref */
  771. ceph_con_send(&req->r_osd->o_con, req->r_request);
  772. req->r_sent = req->r_osd->o_incarnation;
  773. return 0;
  774. }
  775. /*
  776. * Timeout callback, called every N seconds when 1 or more osd
  777. * requests has been active for more than N seconds. When this
  778. * happens, we ping all OSDs with requests who have timed out to
  779. * ensure any communications channel reset is detected. Reset the
  780. * request timeouts another N seconds in the future as we go.
  781. * Reschedule the timeout event another N seconds in future (unless
  782. * there are no open requests).
  783. */
  784. static void handle_timeout(struct work_struct *work)
  785. {
  786. struct ceph_osd_client *osdc =
  787. container_of(work, struct ceph_osd_client, timeout_work.work);
  788. struct ceph_osd_request *req, *last_req = NULL;
  789. struct ceph_osd *osd;
  790. unsigned long timeout = osdc->client->options->osd_timeout * HZ;
  791. unsigned long keepalive =
  792. osdc->client->options->osd_keepalive_timeout * HZ;
  793. unsigned long last_stamp = 0;
  794. struct rb_node *p;
  795. struct list_head slow_osds;
  796. dout("timeout\n");
  797. down_read(&osdc->map_sem);
  798. ceph_monc_request_next_osdmap(&osdc->client->monc);
  799. mutex_lock(&osdc->request_mutex);
  800. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  801. req = rb_entry(p, struct ceph_osd_request, r_node);
  802. if (req->r_resend) {
  803. int err;
  804. dout("osdc resending prev failed %lld\n", req->r_tid);
  805. err = __send_request(osdc, req);
  806. if (err)
  807. dout("osdc failed again on %lld\n", req->r_tid);
  808. else
  809. req->r_resend = false;
  810. continue;
  811. }
  812. }
  813. /*
  814. * reset osds that appear to be _really_ unresponsive. this
  815. * is a failsafe measure.. we really shouldn't be getting to
  816. * this point if the system is working properly. the monitors
  817. * should mark the osd as failed and we should find out about
  818. * it from an updated osd map.
  819. */
  820. while (timeout && !list_empty(&osdc->req_lru)) {
  821. req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
  822. r_req_lru_item);
  823. if (time_before(jiffies, req->r_stamp + timeout))
  824. break;
  825. BUG_ON(req == last_req && req->r_stamp == last_stamp);
  826. last_req = req;
  827. last_stamp = req->r_stamp;
  828. osd = req->r_osd;
  829. BUG_ON(!osd);
  830. pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
  831. req->r_tid, osd->o_osd);
  832. __kick_requests(osdc, osd);
  833. }
  834. /*
  835. * ping osds that are a bit slow. this ensures that if there
  836. * is a break in the TCP connection we will notice, and reopen
  837. * a connection with that osd (from the fault callback).
  838. */
  839. INIT_LIST_HEAD(&slow_osds);
  840. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  841. if (time_before(jiffies, req->r_stamp + keepalive))
  842. break;
  843. osd = req->r_osd;
  844. BUG_ON(!osd);
  845. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  846. req->r_tid, osd->o_osd);
  847. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  848. }
  849. while (!list_empty(&slow_osds)) {
  850. osd = list_entry(slow_osds.next, struct ceph_osd,
  851. o_keepalive_item);
  852. list_del_init(&osd->o_keepalive_item);
  853. ceph_con_keepalive(&osd->o_con);
  854. }
  855. __schedule_osd_timeout(osdc);
  856. mutex_unlock(&osdc->request_mutex);
  857. up_read(&osdc->map_sem);
  858. }
  859. static void handle_osds_timeout(struct work_struct *work)
  860. {
  861. struct ceph_osd_client *osdc =
  862. container_of(work, struct ceph_osd_client,
  863. osds_timeout_work.work);
  864. unsigned long delay =
  865. osdc->client->options->osd_idle_ttl * HZ >> 2;
  866. dout("osds timeout\n");
  867. down_read(&osdc->map_sem);
  868. remove_old_osds(osdc, 0);
  869. up_read(&osdc->map_sem);
  870. schedule_delayed_work(&osdc->osds_timeout_work,
  871. round_jiffies_relative(delay));
  872. }
  873. /*
  874. * handle osd op reply. either call the callback if it is specified,
  875. * or do the completion to wake up the waiting thread.
  876. */
  877. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  878. struct ceph_connection *con)
  879. {
  880. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  881. struct ceph_osd_request *req;
  882. u64 tid;
  883. int numops, object_len, flags;
  884. s32 result;
  885. tid = le64_to_cpu(msg->hdr.tid);
  886. if (msg->front.iov_len < sizeof(*rhead))
  887. goto bad;
  888. numops = le32_to_cpu(rhead->num_ops);
  889. object_len = le32_to_cpu(rhead->object_len);
  890. result = le32_to_cpu(rhead->result);
  891. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  892. numops * sizeof(struct ceph_osd_op))
  893. goto bad;
  894. dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
  895. /* lookup */
  896. mutex_lock(&osdc->request_mutex);
  897. req = __lookup_request(osdc, tid);
  898. if (req == NULL) {
  899. dout("handle_reply tid %llu dne\n", tid);
  900. mutex_unlock(&osdc->request_mutex);
  901. return;
  902. }
  903. ceph_osdc_get_request(req);
  904. flags = le32_to_cpu(rhead->flags);
  905. /*
  906. * if this connection filled our message, drop our reference now, to
  907. * avoid a (safe but slower) revoke later.
  908. */
  909. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  910. dout(" dropping con_filling_msg ref %p\n", con);
  911. req->r_con_filling_msg = NULL;
  912. ceph_con_put(con);
  913. }
  914. if (!req->r_got_reply) {
  915. unsigned bytes;
  916. req->r_result = le32_to_cpu(rhead->result);
  917. bytes = le32_to_cpu(msg->hdr.data_len);
  918. dout("handle_reply result %d bytes %d\n", req->r_result,
  919. bytes);
  920. if (req->r_result == 0)
  921. req->r_result = bytes;
  922. /* in case this is a write and we need to replay, */
  923. req->r_reassert_version = rhead->reassert_version;
  924. req->r_got_reply = 1;
  925. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  926. dout("handle_reply tid %llu dup ack\n", tid);
  927. mutex_unlock(&osdc->request_mutex);
  928. goto done;
  929. }
  930. dout("handle_reply tid %llu flags %d\n", tid, flags);
  931. /* either this is a read, or we got the safe response */
  932. if (result < 0 ||
  933. (flags & CEPH_OSD_FLAG_ONDISK) ||
  934. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  935. __unregister_request(osdc, req);
  936. mutex_unlock(&osdc->request_mutex);
  937. if (req->r_callback)
  938. req->r_callback(req, msg);
  939. else
  940. complete_all(&req->r_completion);
  941. if (flags & CEPH_OSD_FLAG_ONDISK) {
  942. if (req->r_safe_callback)
  943. req->r_safe_callback(req, msg);
  944. complete_all(&req->r_safe_completion); /* fsync waiter */
  945. }
  946. done:
  947. ceph_osdc_put_request(req);
  948. return;
  949. bad:
  950. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  951. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  952. (int)sizeof(*rhead));
  953. ceph_msg_dump(msg);
  954. }
  955. static int __kick_requests(struct ceph_osd_client *osdc,
  956. struct ceph_osd *kickosd)
  957. {
  958. struct ceph_osd_request *req;
  959. struct rb_node *p, *n;
  960. int needmap = 0;
  961. int err;
  962. dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
  963. if (kickosd) {
  964. err = __reset_osd(osdc, kickosd);
  965. if (err == -EAGAIN)
  966. return 1;
  967. } else {
  968. for (p = rb_first(&osdc->osds); p; p = n) {
  969. struct ceph_osd *osd =
  970. rb_entry(p, struct ceph_osd, o_node);
  971. n = rb_next(p);
  972. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  973. memcmp(&osd->o_con.peer_addr,
  974. ceph_osd_addr(osdc->osdmap,
  975. osd->o_osd),
  976. sizeof(struct ceph_entity_addr)) != 0)
  977. __reset_osd(osdc, osd);
  978. }
  979. }
  980. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  981. req = rb_entry(p, struct ceph_osd_request, r_node);
  982. if (req->r_resend) {
  983. dout(" r_resend set on tid %llu\n", req->r_tid);
  984. __cancel_request(req);
  985. goto kick;
  986. }
  987. if (req->r_osd && kickosd == req->r_osd) {
  988. __cancel_request(req);
  989. goto kick;
  990. }
  991. err = __map_osds(osdc, req);
  992. if (err == 0)
  993. continue; /* no change */
  994. if (err < 0) {
  995. /*
  996. * FIXME: really, we should set the request
  997. * error and fail if this isn't a 'nofail'
  998. * request, but that's a fair bit more
  999. * complicated to do. So retry!
  1000. */
  1001. dout(" setting r_resend on %llu\n", req->r_tid);
  1002. req->r_resend = true;
  1003. continue;
  1004. }
  1005. if (req->r_osd == NULL) {
  1006. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1007. needmap++; /* request a newer map */
  1008. continue;
  1009. }
  1010. kick:
  1011. dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
  1012. req->r_osd ? req->r_osd->o_osd : -1);
  1013. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1014. err = __send_request(osdc, req);
  1015. if (err) {
  1016. dout(" setting r_resend on %llu\n", req->r_tid);
  1017. req->r_resend = true;
  1018. }
  1019. }
  1020. return needmap;
  1021. }
  1022. /*
  1023. * Resubmit osd requests whose osd or osd address has changed. Request
  1024. * a new osd map if osds are down, or we are otherwise unable to determine
  1025. * how to direct a request.
  1026. *
  1027. * Close connections to down osds.
  1028. *
  1029. * If @who is specified, resubmit requests for that specific osd.
  1030. *
  1031. * Caller should hold map_sem for read and request_mutex.
  1032. */
  1033. static void kick_requests(struct ceph_osd_client *osdc,
  1034. struct ceph_osd *kickosd)
  1035. {
  1036. int needmap;
  1037. mutex_lock(&osdc->request_mutex);
  1038. needmap = __kick_requests(osdc, kickosd);
  1039. mutex_unlock(&osdc->request_mutex);
  1040. if (needmap) {
  1041. dout("%d requests for down osds, need new map\n", needmap);
  1042. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1043. }
  1044. }
  1045. /*
  1046. * Process updated osd map.
  1047. *
  1048. * The message contains any number of incremental and full maps, normally
  1049. * indicating some sort of topology change in the cluster. Kick requests
  1050. * off to different OSDs as needed.
  1051. */
  1052. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1053. {
  1054. void *p, *end, *next;
  1055. u32 nr_maps, maplen;
  1056. u32 epoch;
  1057. struct ceph_osdmap *newmap = NULL, *oldmap;
  1058. int err;
  1059. struct ceph_fsid fsid;
  1060. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1061. p = msg->front.iov_base;
  1062. end = p + msg->front.iov_len;
  1063. /* verify fsid */
  1064. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1065. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1066. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1067. return;
  1068. down_write(&osdc->map_sem);
  1069. /* incremental maps */
  1070. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1071. dout(" %d inc maps\n", nr_maps);
  1072. while (nr_maps > 0) {
  1073. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1074. epoch = ceph_decode_32(&p);
  1075. maplen = ceph_decode_32(&p);
  1076. ceph_decode_need(&p, end, maplen, bad);
  1077. next = p + maplen;
  1078. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1079. dout("applying incremental map %u len %d\n",
  1080. epoch, maplen);
  1081. newmap = osdmap_apply_incremental(&p, next,
  1082. osdc->osdmap,
  1083. osdc->client->msgr);
  1084. if (IS_ERR(newmap)) {
  1085. err = PTR_ERR(newmap);
  1086. goto bad;
  1087. }
  1088. BUG_ON(!newmap);
  1089. if (newmap != osdc->osdmap) {
  1090. ceph_osdmap_destroy(osdc->osdmap);
  1091. osdc->osdmap = newmap;
  1092. }
  1093. } else {
  1094. dout("ignoring incremental map %u len %d\n",
  1095. epoch, maplen);
  1096. }
  1097. p = next;
  1098. nr_maps--;
  1099. }
  1100. if (newmap)
  1101. goto done;
  1102. /* full maps */
  1103. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1104. dout(" %d full maps\n", nr_maps);
  1105. while (nr_maps) {
  1106. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1107. epoch = ceph_decode_32(&p);
  1108. maplen = ceph_decode_32(&p);
  1109. ceph_decode_need(&p, end, maplen, bad);
  1110. if (nr_maps > 1) {
  1111. dout("skipping non-latest full map %u len %d\n",
  1112. epoch, maplen);
  1113. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1114. dout("skipping full map %u len %d, "
  1115. "older than our %u\n", epoch, maplen,
  1116. osdc->osdmap->epoch);
  1117. } else {
  1118. dout("taking full map %u len %d\n", epoch, maplen);
  1119. newmap = osdmap_decode(&p, p+maplen);
  1120. if (IS_ERR(newmap)) {
  1121. err = PTR_ERR(newmap);
  1122. goto bad;
  1123. }
  1124. BUG_ON(!newmap);
  1125. oldmap = osdc->osdmap;
  1126. osdc->osdmap = newmap;
  1127. if (oldmap)
  1128. ceph_osdmap_destroy(oldmap);
  1129. }
  1130. p += maplen;
  1131. nr_maps--;
  1132. }
  1133. done:
  1134. downgrade_write(&osdc->map_sem);
  1135. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1136. if (newmap)
  1137. kick_requests(osdc, NULL);
  1138. up_read(&osdc->map_sem);
  1139. wake_up_all(&osdc->client->auth_wq);
  1140. return;
  1141. bad:
  1142. pr_err("osdc handle_map corrupt msg\n");
  1143. ceph_msg_dump(msg);
  1144. up_write(&osdc->map_sem);
  1145. return;
  1146. }
  1147. /*
  1148. * Register request, send initial attempt.
  1149. */
  1150. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1151. struct ceph_osd_request *req,
  1152. bool nofail)
  1153. {
  1154. int rc = 0;
  1155. req->r_request->pages = req->r_pages;
  1156. req->r_request->nr_pages = req->r_num_pages;
  1157. #ifdef CONFIG_BLOCK
  1158. req->r_request->bio = req->r_bio;
  1159. #endif
  1160. req->r_request->trail = req->r_trail;
  1161. register_request(osdc, req);
  1162. down_read(&osdc->map_sem);
  1163. mutex_lock(&osdc->request_mutex);
  1164. /*
  1165. * a racing kick_requests() may have sent the message for us
  1166. * while we dropped request_mutex above, so only send now if
  1167. * the request still han't been touched yet.
  1168. */
  1169. if (req->r_sent == 0) {
  1170. rc = __send_request(osdc, req);
  1171. if (rc) {
  1172. if (nofail) {
  1173. dout("osdc_start_request failed send, "
  1174. " marking %lld\n", req->r_tid);
  1175. req->r_resend = true;
  1176. rc = 0;
  1177. } else {
  1178. __unregister_request(osdc, req);
  1179. }
  1180. }
  1181. }
  1182. mutex_unlock(&osdc->request_mutex);
  1183. up_read(&osdc->map_sem);
  1184. return rc;
  1185. }
  1186. EXPORT_SYMBOL(ceph_osdc_start_request);
  1187. /*
  1188. * wait for a request to complete
  1189. */
  1190. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1191. struct ceph_osd_request *req)
  1192. {
  1193. int rc;
  1194. rc = wait_for_completion_interruptible(&req->r_completion);
  1195. if (rc < 0) {
  1196. mutex_lock(&osdc->request_mutex);
  1197. __cancel_request(req);
  1198. __unregister_request(osdc, req);
  1199. mutex_unlock(&osdc->request_mutex);
  1200. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1201. return rc;
  1202. }
  1203. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1204. return req->r_result;
  1205. }
  1206. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1207. /*
  1208. * sync - wait for all in-flight requests to flush. avoid starvation.
  1209. */
  1210. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1211. {
  1212. struct ceph_osd_request *req;
  1213. u64 last_tid, next_tid = 0;
  1214. mutex_lock(&osdc->request_mutex);
  1215. last_tid = osdc->last_tid;
  1216. while (1) {
  1217. req = __lookup_request_ge(osdc, next_tid);
  1218. if (!req)
  1219. break;
  1220. if (req->r_tid > last_tid)
  1221. break;
  1222. next_tid = req->r_tid + 1;
  1223. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1224. continue;
  1225. ceph_osdc_get_request(req);
  1226. mutex_unlock(&osdc->request_mutex);
  1227. dout("sync waiting on tid %llu (last is %llu)\n",
  1228. req->r_tid, last_tid);
  1229. wait_for_completion(&req->r_safe_completion);
  1230. mutex_lock(&osdc->request_mutex);
  1231. ceph_osdc_put_request(req);
  1232. }
  1233. mutex_unlock(&osdc->request_mutex);
  1234. dout("sync done (thru tid %llu)\n", last_tid);
  1235. }
  1236. EXPORT_SYMBOL(ceph_osdc_sync);
  1237. /*
  1238. * init, shutdown
  1239. */
  1240. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1241. {
  1242. int err;
  1243. dout("init\n");
  1244. osdc->client = client;
  1245. osdc->osdmap = NULL;
  1246. init_rwsem(&osdc->map_sem);
  1247. init_completion(&osdc->map_waiters);
  1248. osdc->last_requested_map = 0;
  1249. mutex_init(&osdc->request_mutex);
  1250. osdc->last_tid = 0;
  1251. osdc->osds = RB_ROOT;
  1252. INIT_LIST_HEAD(&osdc->osd_lru);
  1253. osdc->requests = RB_ROOT;
  1254. INIT_LIST_HEAD(&osdc->req_lru);
  1255. osdc->num_requests = 0;
  1256. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1257. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1258. schedule_delayed_work(&osdc->osds_timeout_work,
  1259. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1260. err = -ENOMEM;
  1261. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1262. sizeof(struct ceph_osd_request));
  1263. if (!osdc->req_mempool)
  1264. goto out;
  1265. err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
  1266. "osd_op");
  1267. if (err < 0)
  1268. goto out_mempool;
  1269. err = ceph_msgpool_init(&osdc->msgpool_op_reply,
  1270. OSD_OPREPLY_FRONT_LEN, 10, true,
  1271. "osd_op_reply");
  1272. if (err < 0)
  1273. goto out_msgpool;
  1274. return 0;
  1275. out_msgpool:
  1276. ceph_msgpool_destroy(&osdc->msgpool_op);
  1277. out_mempool:
  1278. mempool_destroy(osdc->req_mempool);
  1279. out:
  1280. return err;
  1281. }
  1282. EXPORT_SYMBOL(ceph_osdc_init);
  1283. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1284. {
  1285. cancel_delayed_work_sync(&osdc->timeout_work);
  1286. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1287. if (osdc->osdmap) {
  1288. ceph_osdmap_destroy(osdc->osdmap);
  1289. osdc->osdmap = NULL;
  1290. }
  1291. remove_old_osds(osdc, 1);
  1292. mempool_destroy(osdc->req_mempool);
  1293. ceph_msgpool_destroy(&osdc->msgpool_op);
  1294. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1295. }
  1296. EXPORT_SYMBOL(ceph_osdc_stop);
  1297. /*
  1298. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1299. * *plen. Return number of bytes read, or error.
  1300. */
  1301. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1302. struct ceph_vino vino, struct ceph_file_layout *layout,
  1303. u64 off, u64 *plen,
  1304. u32 truncate_seq, u64 truncate_size,
  1305. struct page **pages, int num_pages)
  1306. {
  1307. struct ceph_osd_request *req;
  1308. int rc = 0;
  1309. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1310. vino.snap, off, *plen);
  1311. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1312. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1313. NULL, 0, truncate_seq, truncate_size, NULL,
  1314. false, 1);
  1315. if (!req)
  1316. return -ENOMEM;
  1317. /* it may be a short read due to an object boundary */
  1318. req->r_pages = pages;
  1319. dout("readpages final extent is %llu~%llu (%d pages)\n",
  1320. off, *plen, req->r_num_pages);
  1321. rc = ceph_osdc_start_request(osdc, req, false);
  1322. if (!rc)
  1323. rc = ceph_osdc_wait_request(osdc, req);
  1324. ceph_osdc_put_request(req);
  1325. dout("readpages result %d\n", rc);
  1326. return rc;
  1327. }
  1328. EXPORT_SYMBOL(ceph_osdc_readpages);
  1329. /*
  1330. * do a synchronous write on N pages
  1331. */
  1332. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1333. struct ceph_file_layout *layout,
  1334. struct ceph_snap_context *snapc,
  1335. u64 off, u64 len,
  1336. u32 truncate_seq, u64 truncate_size,
  1337. struct timespec *mtime,
  1338. struct page **pages, int num_pages,
  1339. int flags, int do_sync, bool nofail)
  1340. {
  1341. struct ceph_osd_request *req;
  1342. int rc = 0;
  1343. BUG_ON(vino.snap != CEPH_NOSNAP);
  1344. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1345. CEPH_OSD_OP_WRITE,
  1346. flags | CEPH_OSD_FLAG_ONDISK |
  1347. CEPH_OSD_FLAG_WRITE,
  1348. snapc, do_sync,
  1349. truncate_seq, truncate_size, mtime,
  1350. nofail, 1);
  1351. if (!req)
  1352. return -ENOMEM;
  1353. /* it may be a short write due to an object boundary */
  1354. req->r_pages = pages;
  1355. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1356. req->r_num_pages);
  1357. rc = ceph_osdc_start_request(osdc, req, nofail);
  1358. if (!rc)
  1359. rc = ceph_osdc_wait_request(osdc, req);
  1360. ceph_osdc_put_request(req);
  1361. if (rc == 0)
  1362. rc = len;
  1363. dout("writepages result %d\n", rc);
  1364. return rc;
  1365. }
  1366. EXPORT_SYMBOL(ceph_osdc_writepages);
  1367. /*
  1368. * handle incoming message
  1369. */
  1370. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1371. {
  1372. struct ceph_osd *osd = con->private;
  1373. struct ceph_osd_client *osdc;
  1374. int type = le16_to_cpu(msg->hdr.type);
  1375. if (!osd)
  1376. goto out;
  1377. osdc = osd->o_osdc;
  1378. switch (type) {
  1379. case CEPH_MSG_OSD_MAP:
  1380. ceph_osdc_handle_map(osdc, msg);
  1381. break;
  1382. case CEPH_MSG_OSD_OPREPLY:
  1383. handle_reply(osdc, msg, con);
  1384. break;
  1385. default:
  1386. pr_err("received unknown message type %d %s\n", type,
  1387. ceph_msg_type_name(type));
  1388. }
  1389. out:
  1390. ceph_msg_put(msg);
  1391. }
  1392. /*
  1393. * lookup and return message for incoming reply. set up reply message
  1394. * pages.
  1395. */
  1396. static struct ceph_msg *get_reply(struct ceph_connection *con,
  1397. struct ceph_msg_header *hdr,
  1398. int *skip)
  1399. {
  1400. struct ceph_osd *osd = con->private;
  1401. struct ceph_osd_client *osdc = osd->o_osdc;
  1402. struct ceph_msg *m;
  1403. struct ceph_osd_request *req;
  1404. int front = le32_to_cpu(hdr->front_len);
  1405. int data_len = le32_to_cpu(hdr->data_len);
  1406. u64 tid;
  1407. tid = le64_to_cpu(hdr->tid);
  1408. mutex_lock(&osdc->request_mutex);
  1409. req = __lookup_request(osdc, tid);
  1410. if (!req) {
  1411. *skip = 1;
  1412. m = NULL;
  1413. pr_info("get_reply unknown tid %llu from osd%d\n", tid,
  1414. osd->o_osd);
  1415. goto out;
  1416. }
  1417. if (req->r_con_filling_msg) {
  1418. dout("get_reply revoking msg %p from old con %p\n",
  1419. req->r_reply, req->r_con_filling_msg);
  1420. ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
  1421. ceph_con_put(req->r_con_filling_msg);
  1422. req->r_con_filling_msg = NULL;
  1423. }
  1424. if (front > req->r_reply->front.iov_len) {
  1425. pr_warning("get_reply front %d > preallocated %d\n",
  1426. front, (int)req->r_reply->front.iov_len);
  1427. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS);
  1428. if (!m)
  1429. goto out;
  1430. ceph_msg_put(req->r_reply);
  1431. req->r_reply = m;
  1432. }
  1433. m = ceph_msg_get(req->r_reply);
  1434. if (data_len > 0) {
  1435. unsigned data_off = le16_to_cpu(hdr->data_off);
  1436. int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
  1437. if (unlikely(req->r_num_pages < want)) {
  1438. pr_warning("tid %lld reply %d > expected %d pages\n",
  1439. tid, want, m->nr_pages);
  1440. *skip = 1;
  1441. ceph_msg_put(m);
  1442. m = NULL;
  1443. goto out;
  1444. }
  1445. m->pages = req->r_pages;
  1446. m->nr_pages = req->r_num_pages;
  1447. #ifdef CONFIG_BLOCK
  1448. m->bio = req->r_bio;
  1449. #endif
  1450. }
  1451. *skip = 0;
  1452. req->r_con_filling_msg = ceph_con_get(con);
  1453. dout("get_reply tid %lld %p\n", tid, m);
  1454. out:
  1455. mutex_unlock(&osdc->request_mutex);
  1456. return m;
  1457. }
  1458. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1459. struct ceph_msg_header *hdr,
  1460. int *skip)
  1461. {
  1462. struct ceph_osd *osd = con->private;
  1463. int type = le16_to_cpu(hdr->type);
  1464. int front = le32_to_cpu(hdr->front_len);
  1465. switch (type) {
  1466. case CEPH_MSG_OSD_MAP:
  1467. return ceph_msg_new(type, front, GFP_NOFS);
  1468. case CEPH_MSG_OSD_OPREPLY:
  1469. return get_reply(con, hdr, skip);
  1470. default:
  1471. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  1472. osd->o_osd);
  1473. *skip = 1;
  1474. return NULL;
  1475. }
  1476. }
  1477. /*
  1478. * Wrappers to refcount containing ceph_osd struct
  1479. */
  1480. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1481. {
  1482. struct ceph_osd *osd = con->private;
  1483. if (get_osd(osd))
  1484. return con;
  1485. return NULL;
  1486. }
  1487. static void put_osd_con(struct ceph_connection *con)
  1488. {
  1489. struct ceph_osd *osd = con->private;
  1490. put_osd(osd);
  1491. }
  1492. /*
  1493. * authentication
  1494. */
  1495. static int get_authorizer(struct ceph_connection *con,
  1496. void **buf, int *len, int *proto,
  1497. void **reply_buf, int *reply_len, int force_new)
  1498. {
  1499. struct ceph_osd *o = con->private;
  1500. struct ceph_osd_client *osdc = o->o_osdc;
  1501. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1502. int ret = 0;
  1503. if (force_new && o->o_authorizer) {
  1504. ac->ops->destroy_authorizer(ac, o->o_authorizer);
  1505. o->o_authorizer = NULL;
  1506. }
  1507. if (o->o_authorizer == NULL) {
  1508. ret = ac->ops->create_authorizer(
  1509. ac, CEPH_ENTITY_TYPE_OSD,
  1510. &o->o_authorizer,
  1511. &o->o_authorizer_buf,
  1512. &o->o_authorizer_buf_len,
  1513. &o->o_authorizer_reply_buf,
  1514. &o->o_authorizer_reply_buf_len);
  1515. if (ret)
  1516. return ret;
  1517. }
  1518. *proto = ac->protocol;
  1519. *buf = o->o_authorizer_buf;
  1520. *len = o->o_authorizer_buf_len;
  1521. *reply_buf = o->o_authorizer_reply_buf;
  1522. *reply_len = o->o_authorizer_reply_buf_len;
  1523. return 0;
  1524. }
  1525. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1526. {
  1527. struct ceph_osd *o = con->private;
  1528. struct ceph_osd_client *osdc = o->o_osdc;
  1529. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1530. return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
  1531. }
  1532. static int invalidate_authorizer(struct ceph_connection *con)
  1533. {
  1534. struct ceph_osd *o = con->private;
  1535. struct ceph_osd_client *osdc = o->o_osdc;
  1536. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1537. if (ac->ops->invalidate_authorizer)
  1538. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  1539. return ceph_monc_validate_auth(&osdc->client->monc);
  1540. }
  1541. static const struct ceph_connection_operations osd_con_ops = {
  1542. .get = get_osd_con,
  1543. .put = put_osd_con,
  1544. .dispatch = dispatch,
  1545. .get_authorizer = get_authorizer,
  1546. .verify_authorizer_reply = verify_authorizer_reply,
  1547. .invalidate_authorizer = invalidate_authorizer,
  1548. .alloc_msg = alloc_msg,
  1549. .fault = osd_reset,
  1550. };