osd_client.c 45 KB

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