osd_client.c 56 KB

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