osd_client.c 38 KB

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