osd_client.c 44 KB

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