osd_client.c 40 KB

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