osd_client.c 41 KB

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