osd_client.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  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_con_filling_msg) {
  124. dout("%s revoking pages %p from con %p\n", __func__,
  125. req->r_pages, req->r_con_filling_msg);
  126. ceph_msg_revoke_incoming(req->r_reply);
  127. req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
  128. }
  129. if (req->r_reply)
  130. ceph_msg_put(req->r_reply);
  131. if (req->r_own_pages)
  132. ceph_release_page_vector(req->r_pages,
  133. req->r_num_pages);
  134. #ifdef CONFIG_BLOCK
  135. if (req->r_bio)
  136. bio_put(req->r_bio);
  137. #endif
  138. ceph_put_snap_context(req->r_snapc);
  139. if (req->r_trail) {
  140. ceph_pagelist_release(req->r_trail);
  141. kfree(req->r_trail);
  142. }
  143. if (req->r_mempool)
  144. mempool_free(req, req->r_osdc->req_mempool);
  145. else
  146. kfree(req);
  147. }
  148. EXPORT_SYMBOL(ceph_osdc_release_request);
  149. static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
  150. {
  151. int i = 0;
  152. if (needs_trail)
  153. *needs_trail = 0;
  154. while (ops[i].op) {
  155. if (needs_trail && op_needs_trail(ops[i].op))
  156. *needs_trail = 1;
  157. i++;
  158. }
  159. return i;
  160. }
  161. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  162. int flags,
  163. struct ceph_snap_context *snapc,
  164. struct ceph_osd_req_op *ops,
  165. bool use_mempool,
  166. gfp_t gfp_flags,
  167. struct page **pages,
  168. struct bio *bio)
  169. {
  170. struct ceph_osd_request *req;
  171. struct ceph_msg *msg;
  172. int needs_trail;
  173. int num_op = get_num_ops(ops, &needs_trail);
  174. size_t msg_size = sizeof(struct ceph_osd_request_head);
  175. msg_size += num_op*sizeof(struct ceph_osd_op);
  176. if (use_mempool) {
  177. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  178. memset(req, 0, sizeof(*req));
  179. } else {
  180. req = kzalloc(sizeof(*req), gfp_flags);
  181. }
  182. if (req == NULL)
  183. return NULL;
  184. req->r_osdc = osdc;
  185. req->r_mempool = use_mempool;
  186. kref_init(&req->r_kref);
  187. init_completion(&req->r_completion);
  188. init_completion(&req->r_safe_completion);
  189. rb_init_node(&req->r_node);
  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. INIT_LIST_HEAD(&req->r_osd_item);
  195. req->r_flags = flags;
  196. WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
  197. /* create reply message */
  198. if (use_mempool)
  199. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
  200. else
  201. msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
  202. OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
  203. if (!msg) {
  204. ceph_osdc_put_request(req);
  205. return NULL;
  206. }
  207. req->r_reply = msg;
  208. /* allocate space for the trailing data */
  209. if (needs_trail) {
  210. req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
  211. if (!req->r_trail) {
  212. ceph_osdc_put_request(req);
  213. return NULL;
  214. }
  215. ceph_pagelist_init(req->r_trail);
  216. }
  217. /* create request message; allow space for oid */
  218. msg_size += MAX_OBJ_NAME_SIZE;
  219. if (snapc)
  220. msg_size += sizeof(u64) * snapc->num_snaps;
  221. if (use_mempool)
  222. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  223. else
  224. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
  225. if (!msg) {
  226. ceph_osdc_put_request(req);
  227. return NULL;
  228. }
  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 (src->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, int onum)
  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. osd->o_osd = onum;
  560. INIT_LIST_HEAD(&osd->o_requests);
  561. INIT_LIST_HEAD(&osd->o_linger_requests);
  562. INIT_LIST_HEAD(&osd->o_osd_lru);
  563. osd->o_incarnation = 1;
  564. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  565. INIT_LIST_HEAD(&osd->o_keepalive_item);
  566. return osd;
  567. }
  568. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  569. {
  570. if (atomic_inc_not_zero(&osd->o_ref)) {
  571. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  572. atomic_read(&osd->o_ref));
  573. return osd;
  574. } else {
  575. dout("get_osd %p FAIL\n", osd);
  576. return NULL;
  577. }
  578. }
  579. static void put_osd(struct ceph_osd *osd)
  580. {
  581. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  582. atomic_read(&osd->o_ref) - 1);
  583. if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
  584. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  585. if (ac->ops && ac->ops->destroy_authorizer)
  586. ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
  587. kfree(osd);
  588. }
  589. }
  590. /*
  591. * remove an osd from our map
  592. */
  593. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  594. {
  595. dout("__remove_osd %p\n", osd);
  596. BUG_ON(!list_empty(&osd->o_requests));
  597. rb_erase(&osd->o_node, &osdc->osds);
  598. list_del_init(&osd->o_osd_lru);
  599. ceph_con_close(&osd->o_con);
  600. put_osd(osd);
  601. }
  602. static void remove_all_osds(struct ceph_osd_client *osdc)
  603. {
  604. dout("%s %p\n", __func__, osdc);
  605. mutex_lock(&osdc->request_mutex);
  606. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  607. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  608. struct ceph_osd, o_node);
  609. __remove_osd(osdc, osd);
  610. }
  611. mutex_unlock(&osdc->request_mutex);
  612. }
  613. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  614. struct ceph_osd *osd)
  615. {
  616. dout("__move_osd_to_lru %p\n", osd);
  617. BUG_ON(!list_empty(&osd->o_osd_lru));
  618. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  619. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
  620. }
  621. static void __remove_osd_from_lru(struct ceph_osd *osd)
  622. {
  623. dout("__remove_osd_from_lru %p\n", osd);
  624. if (!list_empty(&osd->o_osd_lru))
  625. list_del_init(&osd->o_osd_lru);
  626. }
  627. static void remove_old_osds(struct ceph_osd_client *osdc)
  628. {
  629. struct ceph_osd *osd, *nosd;
  630. dout("__remove_old_osds %p\n", osdc);
  631. mutex_lock(&osdc->request_mutex);
  632. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  633. if (time_before(jiffies, osd->lru_ttl))
  634. break;
  635. __remove_osd(osdc, osd);
  636. }
  637. mutex_unlock(&osdc->request_mutex);
  638. }
  639. /*
  640. * reset osd connect
  641. */
  642. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  643. {
  644. struct ceph_osd_request *req;
  645. int ret = 0;
  646. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  647. if (list_empty(&osd->o_requests) &&
  648. list_empty(&osd->o_linger_requests)) {
  649. __remove_osd(osdc, osd);
  650. } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
  651. &osd->o_con.peer_addr,
  652. sizeof(osd->o_con.peer_addr)) == 0 &&
  653. !ceph_con_opened(&osd->o_con)) {
  654. dout(" osd addr hasn't changed and connection never opened,"
  655. " letting msgr retry");
  656. /* touch each r_stamp for handle_timeout()'s benfit */
  657. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  658. req->r_stamp = jiffies;
  659. ret = -EAGAIN;
  660. } else {
  661. ceph_con_close(&osd->o_con);
  662. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
  663. &osdc->osdmap->osd_addr[osd->o_osd]);
  664. osd->o_incarnation++;
  665. }
  666. return ret;
  667. }
  668. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  669. {
  670. struct rb_node **p = &osdc->osds.rb_node;
  671. struct rb_node *parent = NULL;
  672. struct ceph_osd *osd = NULL;
  673. dout("__insert_osd %p osd%d\n", new, new->o_osd);
  674. while (*p) {
  675. parent = *p;
  676. osd = rb_entry(parent, struct ceph_osd, o_node);
  677. if (new->o_osd < osd->o_osd)
  678. p = &(*p)->rb_left;
  679. else if (new->o_osd > osd->o_osd)
  680. p = &(*p)->rb_right;
  681. else
  682. BUG();
  683. }
  684. rb_link_node(&new->o_node, parent, p);
  685. rb_insert_color(&new->o_node, &osdc->osds);
  686. }
  687. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  688. {
  689. struct ceph_osd *osd;
  690. struct rb_node *n = osdc->osds.rb_node;
  691. while (n) {
  692. osd = rb_entry(n, struct ceph_osd, o_node);
  693. if (o < osd->o_osd)
  694. n = n->rb_left;
  695. else if (o > osd->o_osd)
  696. n = n->rb_right;
  697. else
  698. return osd;
  699. }
  700. return NULL;
  701. }
  702. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  703. {
  704. schedule_delayed_work(&osdc->timeout_work,
  705. osdc->client->options->osd_keepalive_timeout * HZ);
  706. }
  707. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  708. {
  709. cancel_delayed_work(&osdc->timeout_work);
  710. }
  711. /*
  712. * Register request, assign tid. If this is the first request, set up
  713. * the timeout event.
  714. */
  715. static void __register_request(struct ceph_osd_client *osdc,
  716. struct ceph_osd_request *req)
  717. {
  718. req->r_tid = ++osdc->last_tid;
  719. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  720. dout("__register_request %p tid %lld\n", req, req->r_tid);
  721. __insert_request(osdc, req);
  722. ceph_osdc_get_request(req);
  723. osdc->num_requests++;
  724. if (osdc->num_requests == 1) {
  725. dout(" first request, scheduling timeout\n");
  726. __schedule_osd_timeout(osdc);
  727. }
  728. }
  729. static void register_request(struct ceph_osd_client *osdc,
  730. struct ceph_osd_request *req)
  731. {
  732. mutex_lock(&osdc->request_mutex);
  733. __register_request(osdc, req);
  734. mutex_unlock(&osdc->request_mutex);
  735. }
  736. /*
  737. * called under osdc->request_mutex
  738. */
  739. static void __unregister_request(struct ceph_osd_client *osdc,
  740. struct ceph_osd_request *req)
  741. {
  742. if (RB_EMPTY_NODE(&req->r_node)) {
  743. dout("__unregister_request %p tid %lld not registered\n",
  744. req, req->r_tid);
  745. return;
  746. }
  747. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  748. rb_erase(&req->r_node, &osdc->requests);
  749. osdc->num_requests--;
  750. if (req->r_osd) {
  751. /* make sure the original request isn't in flight. */
  752. ceph_msg_revoke(req->r_request);
  753. list_del_init(&req->r_osd_item);
  754. if (list_empty(&req->r_osd->o_requests) &&
  755. list_empty(&req->r_osd->o_linger_requests)) {
  756. dout("moving osd to %p lru\n", req->r_osd);
  757. __move_osd_to_lru(osdc, req->r_osd);
  758. }
  759. if (list_empty(&req->r_linger_item))
  760. req->r_osd = NULL;
  761. }
  762. ceph_osdc_put_request(req);
  763. list_del_init(&req->r_req_lru_item);
  764. if (osdc->num_requests == 0) {
  765. dout(" no requests, canceling timeout\n");
  766. __cancel_osd_timeout(osdc);
  767. }
  768. }
  769. /*
  770. * Cancel a previously queued request message
  771. */
  772. static void __cancel_request(struct ceph_osd_request *req)
  773. {
  774. if (req->r_sent && req->r_osd) {
  775. ceph_msg_revoke(req->r_request);
  776. req->r_sent = 0;
  777. }
  778. }
  779. static void __register_linger_request(struct ceph_osd_client *osdc,
  780. struct ceph_osd_request *req)
  781. {
  782. dout("__register_linger_request %p\n", req);
  783. list_add_tail(&req->r_linger_item, &osdc->req_linger);
  784. if (req->r_osd)
  785. list_add_tail(&req->r_linger_osd,
  786. &req->r_osd->o_linger_requests);
  787. }
  788. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  789. struct ceph_osd_request *req)
  790. {
  791. dout("__unregister_linger_request %p\n", req);
  792. if (req->r_osd) {
  793. list_del_init(&req->r_linger_item);
  794. list_del_init(&req->r_linger_osd);
  795. if (list_empty(&req->r_osd->o_requests) &&
  796. list_empty(&req->r_osd->o_linger_requests)) {
  797. dout("moving osd to %p lru\n", req->r_osd);
  798. __move_osd_to_lru(osdc, req->r_osd);
  799. }
  800. if (list_empty(&req->r_osd_item))
  801. req->r_osd = NULL;
  802. }
  803. }
  804. void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
  805. struct ceph_osd_request *req)
  806. {
  807. mutex_lock(&osdc->request_mutex);
  808. if (req->r_linger) {
  809. __unregister_linger_request(osdc, req);
  810. ceph_osdc_put_request(req);
  811. }
  812. mutex_unlock(&osdc->request_mutex);
  813. }
  814. EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
  815. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  816. struct ceph_osd_request *req)
  817. {
  818. if (!req->r_linger) {
  819. dout("set_request_linger %p\n", req);
  820. req->r_linger = 1;
  821. /*
  822. * caller is now responsible for calling
  823. * unregister_linger_request
  824. */
  825. ceph_osdc_get_request(req);
  826. }
  827. }
  828. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  829. /*
  830. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  831. * (as needed), and set the request r_osd appropriately. If there is
  832. * no up osd, set r_osd to NULL. Move the request to the appropriate list
  833. * (unsent, homeless) or leave on in-flight lru.
  834. *
  835. * Return 0 if unchanged, 1 if changed, or negative on error.
  836. *
  837. * Caller should hold map_sem for read and request_mutex.
  838. */
  839. static int __map_request(struct ceph_osd_client *osdc,
  840. struct ceph_osd_request *req, int force_resend)
  841. {
  842. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  843. struct ceph_pg pgid;
  844. int acting[CEPH_PG_MAX_SIZE];
  845. int o = -1, num = 0;
  846. int err;
  847. dout("map_request %p tid %lld\n", req, req->r_tid);
  848. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  849. &req->r_file_layout, osdc->osdmap);
  850. if (err) {
  851. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  852. return err;
  853. }
  854. pgid = reqhead->layout.ol_pgid;
  855. req->r_pgid = pgid;
  856. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  857. if (err > 0) {
  858. o = acting[0];
  859. num = err;
  860. }
  861. if ((!force_resend &&
  862. req->r_osd && req->r_osd->o_osd == o &&
  863. req->r_sent >= req->r_osd->o_incarnation &&
  864. req->r_num_pg_osds == num &&
  865. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  866. (req->r_osd == NULL && o == -1))
  867. return 0; /* no change */
  868. dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
  869. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  870. req->r_osd ? req->r_osd->o_osd : -1);
  871. /* record full pg acting set */
  872. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  873. req->r_num_pg_osds = num;
  874. if (req->r_osd) {
  875. __cancel_request(req);
  876. list_del_init(&req->r_osd_item);
  877. req->r_osd = NULL;
  878. }
  879. req->r_osd = __lookup_osd(osdc, o);
  880. if (!req->r_osd && o >= 0) {
  881. err = -ENOMEM;
  882. req->r_osd = create_osd(osdc, o);
  883. if (!req->r_osd) {
  884. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  885. goto out;
  886. }
  887. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  888. __insert_osd(osdc, req->r_osd);
  889. ceph_con_open(&req->r_osd->o_con,
  890. CEPH_ENTITY_TYPE_OSD, o,
  891. &osdc->osdmap->osd_addr[o]);
  892. }
  893. if (req->r_osd) {
  894. __remove_osd_from_lru(req->r_osd);
  895. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  896. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  897. } else {
  898. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  899. }
  900. err = 1; /* osd or pg changed */
  901. out:
  902. return err;
  903. }
  904. /*
  905. * caller should hold map_sem (for read) and request_mutex
  906. */
  907. static void __send_request(struct ceph_osd_client *osdc,
  908. struct ceph_osd_request *req)
  909. {
  910. struct ceph_osd_request_head *reqhead;
  911. dout("send_request %p tid %llu to osd%d flags %d\n",
  912. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  913. reqhead = req->r_request->front.iov_base;
  914. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  915. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  916. reqhead->reassert_version = req->r_reassert_version;
  917. req->r_stamp = jiffies;
  918. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  919. ceph_msg_get(req->r_request); /* send consumes a ref */
  920. ceph_con_send(&req->r_osd->o_con, req->r_request);
  921. req->r_sent = req->r_osd->o_incarnation;
  922. }
  923. /*
  924. * Send any requests in the queue (req_unsent).
  925. */
  926. static void send_queued(struct ceph_osd_client *osdc)
  927. {
  928. struct ceph_osd_request *req, *tmp;
  929. dout("send_queued\n");
  930. mutex_lock(&osdc->request_mutex);
  931. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
  932. __send_request(osdc, req);
  933. }
  934. mutex_unlock(&osdc->request_mutex);
  935. }
  936. /*
  937. * Timeout callback, called every N seconds when 1 or more osd
  938. * requests has been active for more than N seconds. When this
  939. * happens, we ping all OSDs with requests who have timed out to
  940. * ensure any communications channel reset is detected. Reset the
  941. * request timeouts another N seconds in the future as we go.
  942. * Reschedule the timeout event another N seconds in future (unless
  943. * there are no open requests).
  944. */
  945. static void handle_timeout(struct work_struct *work)
  946. {
  947. struct ceph_osd_client *osdc =
  948. container_of(work, struct ceph_osd_client, timeout_work.work);
  949. struct ceph_osd_request *req, *last_req = NULL;
  950. struct ceph_osd *osd;
  951. unsigned long timeout = osdc->client->options->osd_timeout * HZ;
  952. unsigned long keepalive =
  953. osdc->client->options->osd_keepalive_timeout * HZ;
  954. unsigned long last_stamp = 0;
  955. struct list_head slow_osds;
  956. dout("timeout\n");
  957. down_read(&osdc->map_sem);
  958. ceph_monc_request_next_osdmap(&osdc->client->monc);
  959. mutex_lock(&osdc->request_mutex);
  960. /*
  961. * reset osds that appear to be _really_ unresponsive. this
  962. * is a failsafe measure.. we really shouldn't be getting to
  963. * this point if the system is working properly. the monitors
  964. * should mark the osd as failed and we should find out about
  965. * it from an updated osd map.
  966. */
  967. while (timeout && !list_empty(&osdc->req_lru)) {
  968. req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
  969. r_req_lru_item);
  970. /* hasn't been long enough since we sent it? */
  971. if (time_before(jiffies, req->r_stamp + timeout))
  972. break;
  973. /* hasn't been long enough since it was acked? */
  974. if (req->r_request->ack_stamp == 0 ||
  975. time_before(jiffies, req->r_request->ack_stamp + timeout))
  976. break;
  977. BUG_ON(req == last_req && req->r_stamp == last_stamp);
  978. last_req = req;
  979. last_stamp = req->r_stamp;
  980. osd = req->r_osd;
  981. BUG_ON(!osd);
  982. pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
  983. req->r_tid, osd->o_osd);
  984. __kick_osd_requests(osdc, osd);
  985. }
  986. /*
  987. * ping osds that are a bit slow. this ensures that if there
  988. * is a break in the TCP connection we will notice, and reopen
  989. * a connection with that osd (from the fault callback).
  990. */
  991. INIT_LIST_HEAD(&slow_osds);
  992. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  993. if (time_before(jiffies, req->r_stamp + keepalive))
  994. break;
  995. osd = req->r_osd;
  996. BUG_ON(!osd);
  997. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  998. req->r_tid, osd->o_osd);
  999. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  1000. }
  1001. while (!list_empty(&slow_osds)) {
  1002. osd = list_entry(slow_osds.next, struct ceph_osd,
  1003. o_keepalive_item);
  1004. list_del_init(&osd->o_keepalive_item);
  1005. ceph_con_keepalive(&osd->o_con);
  1006. }
  1007. __schedule_osd_timeout(osdc);
  1008. mutex_unlock(&osdc->request_mutex);
  1009. send_queued(osdc);
  1010. up_read(&osdc->map_sem);
  1011. }
  1012. static void handle_osds_timeout(struct work_struct *work)
  1013. {
  1014. struct ceph_osd_client *osdc =
  1015. container_of(work, struct ceph_osd_client,
  1016. osds_timeout_work.work);
  1017. unsigned long delay =
  1018. osdc->client->options->osd_idle_ttl * HZ >> 2;
  1019. dout("osds timeout\n");
  1020. down_read(&osdc->map_sem);
  1021. remove_old_osds(osdc);
  1022. up_read(&osdc->map_sem);
  1023. schedule_delayed_work(&osdc->osds_timeout_work,
  1024. round_jiffies_relative(delay));
  1025. }
  1026. static void complete_request(struct ceph_osd_request *req)
  1027. {
  1028. if (req->r_safe_callback)
  1029. req->r_safe_callback(req, NULL);
  1030. complete_all(&req->r_safe_completion); /* fsync waiter */
  1031. }
  1032. /*
  1033. * handle osd op reply. either call the callback if it is specified,
  1034. * or do the completion to wake up the waiting thread.
  1035. */
  1036. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  1037. struct ceph_connection *con)
  1038. {
  1039. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  1040. struct ceph_osd_request *req;
  1041. u64 tid;
  1042. int numops, object_len, flags;
  1043. s32 result;
  1044. tid = le64_to_cpu(msg->hdr.tid);
  1045. if (msg->front.iov_len < sizeof(*rhead))
  1046. goto bad;
  1047. numops = le32_to_cpu(rhead->num_ops);
  1048. object_len = le32_to_cpu(rhead->object_len);
  1049. result = le32_to_cpu(rhead->result);
  1050. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  1051. numops * sizeof(struct ceph_osd_op))
  1052. goto bad;
  1053. dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
  1054. /* lookup */
  1055. mutex_lock(&osdc->request_mutex);
  1056. req = __lookup_request(osdc, tid);
  1057. if (req == NULL) {
  1058. dout("handle_reply tid %llu dne\n", tid);
  1059. mutex_unlock(&osdc->request_mutex);
  1060. return;
  1061. }
  1062. ceph_osdc_get_request(req);
  1063. flags = le32_to_cpu(rhead->flags);
  1064. /*
  1065. * if this connection filled our message, drop our reference now, to
  1066. * avoid a (safe but slower) revoke later.
  1067. */
  1068. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  1069. dout(" dropping con_filling_msg ref %p\n", con);
  1070. req->r_con_filling_msg = NULL;
  1071. con->ops->put(con);
  1072. }
  1073. if (!req->r_got_reply) {
  1074. unsigned int bytes;
  1075. req->r_result = le32_to_cpu(rhead->result);
  1076. bytes = le32_to_cpu(msg->hdr.data_len);
  1077. dout("handle_reply result %d bytes %d\n", req->r_result,
  1078. bytes);
  1079. if (req->r_result == 0)
  1080. req->r_result = bytes;
  1081. /* in case this is a write and we need to replay, */
  1082. req->r_reassert_version = rhead->reassert_version;
  1083. req->r_got_reply = 1;
  1084. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1085. dout("handle_reply tid %llu dup ack\n", tid);
  1086. mutex_unlock(&osdc->request_mutex);
  1087. goto done;
  1088. }
  1089. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1090. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1091. __register_linger_request(osdc, req);
  1092. /* either this is a read, or we got the safe response */
  1093. if (result < 0 ||
  1094. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1095. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1096. __unregister_request(osdc, req);
  1097. mutex_unlock(&osdc->request_mutex);
  1098. if (req->r_callback)
  1099. req->r_callback(req, msg);
  1100. else
  1101. complete_all(&req->r_completion);
  1102. if (flags & CEPH_OSD_FLAG_ONDISK)
  1103. complete_request(req);
  1104. done:
  1105. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1106. ceph_osdc_put_request(req);
  1107. return;
  1108. bad:
  1109. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  1110. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  1111. (int)sizeof(*rhead));
  1112. ceph_msg_dump(msg);
  1113. }
  1114. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1115. {
  1116. struct rb_node *p, *n;
  1117. for (p = rb_first(&osdc->osds); p; p = n) {
  1118. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1119. n = rb_next(p);
  1120. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1121. memcmp(&osd->o_con.peer_addr,
  1122. ceph_osd_addr(osdc->osdmap,
  1123. osd->o_osd),
  1124. sizeof(struct ceph_entity_addr)) != 0)
  1125. __reset_osd(osdc, osd);
  1126. }
  1127. }
  1128. /*
  1129. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1130. * no osd, request a new map.
  1131. *
  1132. * Caller should hold map_sem for read and request_mutex.
  1133. */
  1134. static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
  1135. {
  1136. struct ceph_osd_request *req, *nreq;
  1137. struct rb_node *p;
  1138. int needmap = 0;
  1139. int err;
  1140. dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
  1141. mutex_lock(&osdc->request_mutex);
  1142. for (p = rb_first(&osdc->requests); p; ) {
  1143. req = rb_entry(p, struct ceph_osd_request, r_node);
  1144. p = rb_next(p);
  1145. err = __map_request(osdc, req, force_resend);
  1146. if (err < 0)
  1147. continue; /* error */
  1148. if (req->r_osd == NULL) {
  1149. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1150. needmap++; /* request a newer map */
  1151. } else if (err > 0) {
  1152. if (!req->r_linger) {
  1153. dout("%p tid %llu requeued on osd%d\n", req,
  1154. req->r_tid,
  1155. req->r_osd ? req->r_osd->o_osd : -1);
  1156. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1157. }
  1158. }
  1159. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1160. /*
  1161. * register as a linger so that we will
  1162. * re-submit below and get a new tid
  1163. */
  1164. dout("%p tid %llu restart on osd%d\n",
  1165. req, req->r_tid,
  1166. req->r_osd ? req->r_osd->o_osd : -1);
  1167. __register_linger_request(osdc, req);
  1168. __unregister_request(osdc, req);
  1169. }
  1170. }
  1171. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1172. r_linger_item) {
  1173. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1174. err = __map_request(osdc, req, force_resend);
  1175. if (err == 0)
  1176. continue; /* no change and no osd was specified */
  1177. if (err < 0)
  1178. continue; /* hrm! */
  1179. if (req->r_osd == NULL) {
  1180. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1181. needmap++; /* request a newer map */
  1182. continue;
  1183. }
  1184. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1185. req->r_osd ? req->r_osd->o_osd : -1);
  1186. __unregister_linger_request(osdc, req);
  1187. __register_request(osdc, req);
  1188. }
  1189. mutex_unlock(&osdc->request_mutex);
  1190. if (needmap) {
  1191. dout("%d requests for down osds, need new map\n", needmap);
  1192. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1193. }
  1194. }
  1195. /*
  1196. * Process updated osd map.
  1197. *
  1198. * The message contains any number of incremental and full maps, normally
  1199. * indicating some sort of topology change in the cluster. Kick requests
  1200. * off to different OSDs as needed.
  1201. */
  1202. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1203. {
  1204. void *p, *end, *next;
  1205. u32 nr_maps, maplen;
  1206. u32 epoch;
  1207. struct ceph_osdmap *newmap = NULL, *oldmap;
  1208. int err;
  1209. struct ceph_fsid fsid;
  1210. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1211. p = msg->front.iov_base;
  1212. end = p + msg->front.iov_len;
  1213. /* verify fsid */
  1214. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1215. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1216. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1217. return;
  1218. down_write(&osdc->map_sem);
  1219. /* incremental maps */
  1220. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1221. dout(" %d inc maps\n", nr_maps);
  1222. while (nr_maps > 0) {
  1223. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1224. epoch = ceph_decode_32(&p);
  1225. maplen = ceph_decode_32(&p);
  1226. ceph_decode_need(&p, end, maplen, bad);
  1227. next = p + maplen;
  1228. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1229. dout("applying incremental map %u len %d\n",
  1230. epoch, maplen);
  1231. newmap = osdmap_apply_incremental(&p, next,
  1232. osdc->osdmap,
  1233. &osdc->client->msgr);
  1234. if (IS_ERR(newmap)) {
  1235. err = PTR_ERR(newmap);
  1236. goto bad;
  1237. }
  1238. BUG_ON(!newmap);
  1239. if (newmap != osdc->osdmap) {
  1240. ceph_osdmap_destroy(osdc->osdmap);
  1241. osdc->osdmap = newmap;
  1242. }
  1243. kick_requests(osdc, 0);
  1244. reset_changed_osds(osdc);
  1245. } else {
  1246. dout("ignoring incremental map %u len %d\n",
  1247. epoch, maplen);
  1248. }
  1249. p = next;
  1250. nr_maps--;
  1251. }
  1252. if (newmap)
  1253. goto done;
  1254. /* full maps */
  1255. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1256. dout(" %d full maps\n", nr_maps);
  1257. while (nr_maps) {
  1258. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1259. epoch = ceph_decode_32(&p);
  1260. maplen = ceph_decode_32(&p);
  1261. ceph_decode_need(&p, end, maplen, bad);
  1262. if (nr_maps > 1) {
  1263. dout("skipping non-latest full map %u len %d\n",
  1264. epoch, maplen);
  1265. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1266. dout("skipping full map %u len %d, "
  1267. "older than our %u\n", epoch, maplen,
  1268. osdc->osdmap->epoch);
  1269. } else {
  1270. int skipped_map = 0;
  1271. dout("taking full map %u len %d\n", epoch, maplen);
  1272. newmap = osdmap_decode(&p, p+maplen);
  1273. if (IS_ERR(newmap)) {
  1274. err = PTR_ERR(newmap);
  1275. goto bad;
  1276. }
  1277. BUG_ON(!newmap);
  1278. oldmap = osdc->osdmap;
  1279. osdc->osdmap = newmap;
  1280. if (oldmap) {
  1281. if (oldmap->epoch + 1 < newmap->epoch)
  1282. skipped_map = 1;
  1283. ceph_osdmap_destroy(oldmap);
  1284. }
  1285. kick_requests(osdc, skipped_map);
  1286. }
  1287. p += maplen;
  1288. nr_maps--;
  1289. }
  1290. done:
  1291. downgrade_write(&osdc->map_sem);
  1292. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1293. /*
  1294. * subscribe to subsequent osdmap updates if full to ensure
  1295. * we find out when we are no longer full and stop returning
  1296. * ENOSPC.
  1297. */
  1298. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  1299. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1300. send_queued(osdc);
  1301. up_read(&osdc->map_sem);
  1302. wake_up_all(&osdc->client->auth_wq);
  1303. return;
  1304. bad:
  1305. pr_err("osdc handle_map corrupt msg\n");
  1306. ceph_msg_dump(msg);
  1307. up_write(&osdc->map_sem);
  1308. return;
  1309. }
  1310. /*
  1311. * watch/notify callback event infrastructure
  1312. *
  1313. * These callbacks are used both for watch and notify operations.
  1314. */
  1315. static void __release_event(struct kref *kref)
  1316. {
  1317. struct ceph_osd_event *event =
  1318. container_of(kref, struct ceph_osd_event, kref);
  1319. dout("__release_event %p\n", event);
  1320. kfree(event);
  1321. }
  1322. static void get_event(struct ceph_osd_event *event)
  1323. {
  1324. kref_get(&event->kref);
  1325. }
  1326. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1327. {
  1328. kref_put(&event->kref, __release_event);
  1329. }
  1330. EXPORT_SYMBOL(ceph_osdc_put_event);
  1331. static void __insert_event(struct ceph_osd_client *osdc,
  1332. struct ceph_osd_event *new)
  1333. {
  1334. struct rb_node **p = &osdc->event_tree.rb_node;
  1335. struct rb_node *parent = NULL;
  1336. struct ceph_osd_event *event = NULL;
  1337. while (*p) {
  1338. parent = *p;
  1339. event = rb_entry(parent, struct ceph_osd_event, node);
  1340. if (new->cookie < event->cookie)
  1341. p = &(*p)->rb_left;
  1342. else if (new->cookie > event->cookie)
  1343. p = &(*p)->rb_right;
  1344. else
  1345. BUG();
  1346. }
  1347. rb_link_node(&new->node, parent, p);
  1348. rb_insert_color(&new->node, &osdc->event_tree);
  1349. }
  1350. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1351. u64 cookie)
  1352. {
  1353. struct rb_node **p = &osdc->event_tree.rb_node;
  1354. struct rb_node *parent = NULL;
  1355. struct ceph_osd_event *event = NULL;
  1356. while (*p) {
  1357. parent = *p;
  1358. event = rb_entry(parent, struct ceph_osd_event, node);
  1359. if (cookie < event->cookie)
  1360. p = &(*p)->rb_left;
  1361. else if (cookie > event->cookie)
  1362. p = &(*p)->rb_right;
  1363. else
  1364. return event;
  1365. }
  1366. return NULL;
  1367. }
  1368. static void __remove_event(struct ceph_osd_event *event)
  1369. {
  1370. struct ceph_osd_client *osdc = event->osdc;
  1371. if (!RB_EMPTY_NODE(&event->node)) {
  1372. dout("__remove_event removed %p\n", event);
  1373. rb_erase(&event->node, &osdc->event_tree);
  1374. ceph_osdc_put_event(event);
  1375. } else {
  1376. dout("__remove_event didn't remove %p\n", event);
  1377. }
  1378. }
  1379. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1380. void (*event_cb)(u64, u64, u8, void *),
  1381. int one_shot, void *data,
  1382. struct ceph_osd_event **pevent)
  1383. {
  1384. struct ceph_osd_event *event;
  1385. event = kmalloc(sizeof(*event), GFP_NOIO);
  1386. if (!event)
  1387. return -ENOMEM;
  1388. dout("create_event %p\n", event);
  1389. event->cb = event_cb;
  1390. event->one_shot = one_shot;
  1391. event->data = data;
  1392. event->osdc = osdc;
  1393. INIT_LIST_HEAD(&event->osd_node);
  1394. kref_init(&event->kref); /* one ref for us */
  1395. kref_get(&event->kref); /* one ref for the caller */
  1396. init_completion(&event->completion);
  1397. spin_lock(&osdc->event_lock);
  1398. event->cookie = ++osdc->event_count;
  1399. __insert_event(osdc, event);
  1400. spin_unlock(&osdc->event_lock);
  1401. *pevent = event;
  1402. return 0;
  1403. }
  1404. EXPORT_SYMBOL(ceph_osdc_create_event);
  1405. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1406. {
  1407. struct ceph_osd_client *osdc = event->osdc;
  1408. dout("cancel_event %p\n", event);
  1409. spin_lock(&osdc->event_lock);
  1410. __remove_event(event);
  1411. spin_unlock(&osdc->event_lock);
  1412. ceph_osdc_put_event(event); /* caller's */
  1413. }
  1414. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1415. static void do_event_work(struct work_struct *work)
  1416. {
  1417. struct ceph_osd_event_work *event_work =
  1418. container_of(work, struct ceph_osd_event_work, work);
  1419. struct ceph_osd_event *event = event_work->event;
  1420. u64 ver = event_work->ver;
  1421. u64 notify_id = event_work->notify_id;
  1422. u8 opcode = event_work->opcode;
  1423. dout("do_event_work completing %p\n", event);
  1424. event->cb(ver, notify_id, opcode, event->data);
  1425. complete(&event->completion);
  1426. dout("do_event_work completed %p\n", event);
  1427. ceph_osdc_put_event(event);
  1428. kfree(event_work);
  1429. }
  1430. /*
  1431. * Process osd watch notifications
  1432. */
  1433. void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1434. {
  1435. void *p, *end;
  1436. u8 proto_ver;
  1437. u64 cookie, ver, notify_id;
  1438. u8 opcode;
  1439. struct ceph_osd_event *event;
  1440. struct ceph_osd_event_work *event_work;
  1441. p = msg->front.iov_base;
  1442. end = p + msg->front.iov_len;
  1443. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1444. ceph_decode_8_safe(&p, end, opcode, bad);
  1445. ceph_decode_64_safe(&p, end, cookie, bad);
  1446. ceph_decode_64_safe(&p, end, ver, bad);
  1447. ceph_decode_64_safe(&p, end, notify_id, bad);
  1448. spin_lock(&osdc->event_lock);
  1449. event = __find_event(osdc, cookie);
  1450. if (event) {
  1451. get_event(event);
  1452. if (event->one_shot)
  1453. __remove_event(event);
  1454. }
  1455. spin_unlock(&osdc->event_lock);
  1456. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1457. cookie, ver, event);
  1458. if (event) {
  1459. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1460. if (!event_work) {
  1461. dout("ERROR: could not allocate event_work\n");
  1462. goto done_err;
  1463. }
  1464. INIT_WORK(&event_work->work, do_event_work);
  1465. event_work->event = event;
  1466. event_work->ver = ver;
  1467. event_work->notify_id = notify_id;
  1468. event_work->opcode = opcode;
  1469. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1470. dout("WARNING: failed to queue notify event work\n");
  1471. goto done_err;
  1472. }
  1473. }
  1474. return;
  1475. done_err:
  1476. complete(&event->completion);
  1477. ceph_osdc_put_event(event);
  1478. return;
  1479. bad:
  1480. pr_err("osdc handle_watch_notify corrupt msg\n");
  1481. return;
  1482. }
  1483. int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
  1484. {
  1485. int err;
  1486. dout("wait_event %p\n", event);
  1487. err = wait_for_completion_interruptible_timeout(&event->completion,
  1488. timeout * HZ);
  1489. ceph_osdc_put_event(event);
  1490. if (err > 0)
  1491. err = 0;
  1492. dout("wait_event %p returns %d\n", event, err);
  1493. return err;
  1494. }
  1495. EXPORT_SYMBOL(ceph_osdc_wait_event);
  1496. /*
  1497. * Register request, send initial attempt.
  1498. */
  1499. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1500. struct ceph_osd_request *req,
  1501. bool nofail)
  1502. {
  1503. int rc = 0;
  1504. req->r_request->pages = req->r_pages;
  1505. req->r_request->nr_pages = req->r_num_pages;
  1506. #ifdef CONFIG_BLOCK
  1507. req->r_request->bio = req->r_bio;
  1508. #endif
  1509. req->r_request->trail = req->r_trail;
  1510. register_request(osdc, req);
  1511. down_read(&osdc->map_sem);
  1512. mutex_lock(&osdc->request_mutex);
  1513. /*
  1514. * a racing kick_requests() may have sent the message for us
  1515. * while we dropped request_mutex above, so only send now if
  1516. * the request still han't been touched yet.
  1517. */
  1518. if (req->r_sent == 0) {
  1519. rc = __map_request(osdc, req, 0);
  1520. if (rc < 0) {
  1521. if (nofail) {
  1522. dout("osdc_start_request failed map, "
  1523. " will retry %lld\n", req->r_tid);
  1524. rc = 0;
  1525. }
  1526. goto out_unlock;
  1527. }
  1528. if (req->r_osd == NULL) {
  1529. dout("send_request %p no up osds in pg\n", req);
  1530. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1531. } else {
  1532. __send_request(osdc, req);
  1533. }
  1534. rc = 0;
  1535. }
  1536. out_unlock:
  1537. mutex_unlock(&osdc->request_mutex);
  1538. up_read(&osdc->map_sem);
  1539. return rc;
  1540. }
  1541. EXPORT_SYMBOL(ceph_osdc_start_request);
  1542. /*
  1543. * wait for a request to complete
  1544. */
  1545. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1546. struct ceph_osd_request *req)
  1547. {
  1548. int rc;
  1549. rc = wait_for_completion_interruptible(&req->r_completion);
  1550. if (rc < 0) {
  1551. mutex_lock(&osdc->request_mutex);
  1552. __cancel_request(req);
  1553. __unregister_request(osdc, req);
  1554. mutex_unlock(&osdc->request_mutex);
  1555. complete_request(req);
  1556. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1557. return rc;
  1558. }
  1559. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1560. return req->r_result;
  1561. }
  1562. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1563. /*
  1564. * sync - wait for all in-flight requests to flush. avoid starvation.
  1565. */
  1566. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1567. {
  1568. struct ceph_osd_request *req;
  1569. u64 last_tid, next_tid = 0;
  1570. mutex_lock(&osdc->request_mutex);
  1571. last_tid = osdc->last_tid;
  1572. while (1) {
  1573. req = __lookup_request_ge(osdc, next_tid);
  1574. if (!req)
  1575. break;
  1576. if (req->r_tid > last_tid)
  1577. break;
  1578. next_tid = req->r_tid + 1;
  1579. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1580. continue;
  1581. ceph_osdc_get_request(req);
  1582. mutex_unlock(&osdc->request_mutex);
  1583. dout("sync waiting on tid %llu (last is %llu)\n",
  1584. req->r_tid, last_tid);
  1585. wait_for_completion(&req->r_safe_completion);
  1586. mutex_lock(&osdc->request_mutex);
  1587. ceph_osdc_put_request(req);
  1588. }
  1589. mutex_unlock(&osdc->request_mutex);
  1590. dout("sync done (thru tid %llu)\n", last_tid);
  1591. }
  1592. EXPORT_SYMBOL(ceph_osdc_sync);
  1593. /*
  1594. * init, shutdown
  1595. */
  1596. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1597. {
  1598. int err;
  1599. dout("init\n");
  1600. osdc->client = client;
  1601. osdc->osdmap = NULL;
  1602. init_rwsem(&osdc->map_sem);
  1603. init_completion(&osdc->map_waiters);
  1604. osdc->last_requested_map = 0;
  1605. mutex_init(&osdc->request_mutex);
  1606. osdc->last_tid = 0;
  1607. osdc->osds = RB_ROOT;
  1608. INIT_LIST_HEAD(&osdc->osd_lru);
  1609. osdc->requests = RB_ROOT;
  1610. INIT_LIST_HEAD(&osdc->req_lru);
  1611. INIT_LIST_HEAD(&osdc->req_unsent);
  1612. INIT_LIST_HEAD(&osdc->req_notarget);
  1613. INIT_LIST_HEAD(&osdc->req_linger);
  1614. osdc->num_requests = 0;
  1615. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1616. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1617. spin_lock_init(&osdc->event_lock);
  1618. osdc->event_tree = RB_ROOT;
  1619. osdc->event_count = 0;
  1620. schedule_delayed_work(&osdc->osds_timeout_work,
  1621. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1622. err = -ENOMEM;
  1623. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1624. sizeof(struct ceph_osd_request));
  1625. if (!osdc->req_mempool)
  1626. goto out;
  1627. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  1628. OSD_OP_FRONT_LEN, 10, true,
  1629. "osd_op");
  1630. if (err < 0)
  1631. goto out_mempool;
  1632. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  1633. OSD_OPREPLY_FRONT_LEN, 10, true,
  1634. "osd_op_reply");
  1635. if (err < 0)
  1636. goto out_msgpool;
  1637. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  1638. if (IS_ERR(osdc->notify_wq)) {
  1639. err = PTR_ERR(osdc->notify_wq);
  1640. osdc->notify_wq = NULL;
  1641. goto out_msgpool;
  1642. }
  1643. return 0;
  1644. out_msgpool:
  1645. ceph_msgpool_destroy(&osdc->msgpool_op);
  1646. out_mempool:
  1647. mempool_destroy(osdc->req_mempool);
  1648. out:
  1649. return err;
  1650. }
  1651. EXPORT_SYMBOL(ceph_osdc_init);
  1652. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1653. {
  1654. flush_workqueue(osdc->notify_wq);
  1655. destroy_workqueue(osdc->notify_wq);
  1656. cancel_delayed_work_sync(&osdc->timeout_work);
  1657. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1658. if (osdc->osdmap) {
  1659. ceph_osdmap_destroy(osdc->osdmap);
  1660. osdc->osdmap = NULL;
  1661. }
  1662. remove_all_osds(osdc);
  1663. mempool_destroy(osdc->req_mempool);
  1664. ceph_msgpool_destroy(&osdc->msgpool_op);
  1665. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1666. }
  1667. EXPORT_SYMBOL(ceph_osdc_stop);
  1668. /*
  1669. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1670. * *plen. Return number of bytes read, or error.
  1671. */
  1672. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1673. struct ceph_vino vino, struct ceph_file_layout *layout,
  1674. u64 off, u64 *plen,
  1675. u32 truncate_seq, u64 truncate_size,
  1676. struct page **pages, int num_pages, int page_align)
  1677. {
  1678. struct ceph_osd_request *req;
  1679. int rc = 0;
  1680. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1681. vino.snap, off, *plen);
  1682. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1683. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1684. NULL, 0, truncate_seq, truncate_size, NULL,
  1685. false, 1, page_align);
  1686. if (!req)
  1687. return -ENOMEM;
  1688. /* it may be a short read due to an object boundary */
  1689. req->r_pages = pages;
  1690. dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
  1691. off, *plen, req->r_num_pages, page_align);
  1692. rc = ceph_osdc_start_request(osdc, req, false);
  1693. if (!rc)
  1694. rc = ceph_osdc_wait_request(osdc, req);
  1695. ceph_osdc_put_request(req);
  1696. dout("readpages result %d\n", rc);
  1697. return rc;
  1698. }
  1699. EXPORT_SYMBOL(ceph_osdc_readpages);
  1700. /*
  1701. * do a synchronous write on N pages
  1702. */
  1703. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1704. struct ceph_file_layout *layout,
  1705. struct ceph_snap_context *snapc,
  1706. u64 off, u64 len,
  1707. u32 truncate_seq, u64 truncate_size,
  1708. struct timespec *mtime,
  1709. struct page **pages, int num_pages,
  1710. int flags, int do_sync, bool nofail)
  1711. {
  1712. struct ceph_osd_request *req;
  1713. int rc = 0;
  1714. int page_align = off & ~PAGE_MASK;
  1715. BUG_ON(vino.snap != CEPH_NOSNAP);
  1716. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1717. CEPH_OSD_OP_WRITE,
  1718. flags | CEPH_OSD_FLAG_ONDISK |
  1719. CEPH_OSD_FLAG_WRITE,
  1720. snapc, do_sync,
  1721. truncate_seq, truncate_size, mtime,
  1722. nofail, 1, page_align);
  1723. if (!req)
  1724. return -ENOMEM;
  1725. /* it may be a short write due to an object boundary */
  1726. req->r_pages = pages;
  1727. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1728. req->r_num_pages);
  1729. rc = ceph_osdc_start_request(osdc, req, nofail);
  1730. if (!rc)
  1731. rc = ceph_osdc_wait_request(osdc, req);
  1732. ceph_osdc_put_request(req);
  1733. if (rc == 0)
  1734. rc = len;
  1735. dout("writepages result %d\n", rc);
  1736. return rc;
  1737. }
  1738. EXPORT_SYMBOL(ceph_osdc_writepages);
  1739. /*
  1740. * handle incoming message
  1741. */
  1742. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1743. {
  1744. struct ceph_osd *osd = con->private;
  1745. struct ceph_osd_client *osdc;
  1746. int type = le16_to_cpu(msg->hdr.type);
  1747. if (!osd)
  1748. goto out;
  1749. osdc = osd->o_osdc;
  1750. switch (type) {
  1751. case CEPH_MSG_OSD_MAP:
  1752. ceph_osdc_handle_map(osdc, msg);
  1753. break;
  1754. case CEPH_MSG_OSD_OPREPLY:
  1755. handle_reply(osdc, msg, con);
  1756. break;
  1757. case CEPH_MSG_WATCH_NOTIFY:
  1758. handle_watch_notify(osdc, msg);
  1759. break;
  1760. default:
  1761. pr_err("received unknown message type %d %s\n", type,
  1762. ceph_msg_type_name(type));
  1763. }
  1764. out:
  1765. ceph_msg_put(msg);
  1766. }
  1767. /*
  1768. * lookup and return message for incoming reply. set up reply message
  1769. * pages.
  1770. */
  1771. static struct ceph_msg *get_reply(struct ceph_connection *con,
  1772. struct ceph_msg_header *hdr,
  1773. int *skip)
  1774. {
  1775. struct ceph_osd *osd = con->private;
  1776. struct ceph_osd_client *osdc = osd->o_osdc;
  1777. struct ceph_msg *m;
  1778. struct ceph_osd_request *req;
  1779. int front = le32_to_cpu(hdr->front_len);
  1780. int data_len = le32_to_cpu(hdr->data_len);
  1781. u64 tid;
  1782. tid = le64_to_cpu(hdr->tid);
  1783. mutex_lock(&osdc->request_mutex);
  1784. req = __lookup_request(osdc, tid);
  1785. if (!req) {
  1786. *skip = 1;
  1787. m = NULL;
  1788. dout("get_reply unknown tid %llu from osd%d\n", tid,
  1789. osd->o_osd);
  1790. goto out;
  1791. }
  1792. if (req->r_con_filling_msg) {
  1793. dout("%s revoking msg %p from old con %p\n", __func__,
  1794. req->r_reply, req->r_con_filling_msg);
  1795. ceph_msg_revoke_incoming(req->r_reply);
  1796. req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
  1797. req->r_con_filling_msg = NULL;
  1798. }
  1799. if (front > req->r_reply->front.iov_len) {
  1800. pr_warning("get_reply front %d > preallocated %d\n",
  1801. front, (int)req->r_reply->front.iov_len);
  1802. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
  1803. if (!m)
  1804. goto out;
  1805. ceph_msg_put(req->r_reply);
  1806. req->r_reply = m;
  1807. }
  1808. m = ceph_msg_get(req->r_reply);
  1809. if (data_len > 0) {
  1810. int want = calc_pages_for(req->r_page_alignment, data_len);
  1811. if (unlikely(req->r_num_pages < want)) {
  1812. pr_warning("tid %lld reply has %d bytes %d pages, we"
  1813. " had only %d pages ready\n", tid, data_len,
  1814. want, req->r_num_pages);
  1815. *skip = 1;
  1816. ceph_msg_put(m);
  1817. m = NULL;
  1818. goto out;
  1819. }
  1820. m->pages = req->r_pages;
  1821. m->nr_pages = req->r_num_pages;
  1822. m->page_alignment = req->r_page_alignment;
  1823. #ifdef CONFIG_BLOCK
  1824. m->bio = req->r_bio;
  1825. #endif
  1826. }
  1827. *skip = 0;
  1828. req->r_con_filling_msg = con->ops->get(con);
  1829. dout("get_reply tid %lld %p\n", tid, m);
  1830. out:
  1831. mutex_unlock(&osdc->request_mutex);
  1832. return m;
  1833. }
  1834. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1835. struct ceph_msg_header *hdr,
  1836. int *skip)
  1837. {
  1838. struct ceph_osd *osd = con->private;
  1839. int type = le16_to_cpu(hdr->type);
  1840. int front = le32_to_cpu(hdr->front_len);
  1841. *skip = 0;
  1842. switch (type) {
  1843. case CEPH_MSG_OSD_MAP:
  1844. case CEPH_MSG_WATCH_NOTIFY:
  1845. return ceph_msg_new(type, front, GFP_NOFS, false);
  1846. case CEPH_MSG_OSD_OPREPLY:
  1847. return get_reply(con, hdr, skip);
  1848. default:
  1849. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  1850. osd->o_osd);
  1851. *skip = 1;
  1852. return NULL;
  1853. }
  1854. }
  1855. /*
  1856. * Wrappers to refcount containing ceph_osd struct
  1857. */
  1858. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1859. {
  1860. struct ceph_osd *osd = con->private;
  1861. if (get_osd(osd))
  1862. return con;
  1863. return NULL;
  1864. }
  1865. static void put_osd_con(struct ceph_connection *con)
  1866. {
  1867. struct ceph_osd *osd = con->private;
  1868. put_osd(osd);
  1869. }
  1870. /*
  1871. * authentication
  1872. */
  1873. /*
  1874. * Note: returned pointer is the address of a structure that's
  1875. * managed separately. Caller must *not* attempt to free it.
  1876. */
  1877. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  1878. int *proto, int force_new)
  1879. {
  1880. struct ceph_osd *o = con->private;
  1881. struct ceph_osd_client *osdc = o->o_osdc;
  1882. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1883. struct ceph_auth_handshake *auth = &o->o_auth;
  1884. if (force_new && auth->authorizer) {
  1885. if (ac->ops && ac->ops->destroy_authorizer)
  1886. ac->ops->destroy_authorizer(ac, auth->authorizer);
  1887. auth->authorizer = NULL;
  1888. }
  1889. if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
  1890. int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  1891. auth);
  1892. if (ret)
  1893. return ERR_PTR(ret);
  1894. }
  1895. *proto = ac->protocol;
  1896. return auth;
  1897. }
  1898. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1899. {
  1900. struct ceph_osd *o = con->private;
  1901. struct ceph_osd_client *osdc = o->o_osdc;
  1902. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1903. /*
  1904. * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
  1905. * XXX which do we do: succeed or fail?
  1906. */
  1907. return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
  1908. }
  1909. static int invalidate_authorizer(struct ceph_connection *con)
  1910. {
  1911. struct ceph_osd *o = con->private;
  1912. struct ceph_osd_client *osdc = o->o_osdc;
  1913. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1914. if (ac->ops && ac->ops->invalidate_authorizer)
  1915. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  1916. return ceph_monc_validate_auth(&osdc->client->monc);
  1917. }
  1918. static const struct ceph_connection_operations osd_con_ops = {
  1919. .get = get_osd_con,
  1920. .put = put_osd_con,
  1921. .dispatch = dispatch,
  1922. .get_authorizer = get_authorizer,
  1923. .verify_authorizer_reply = verify_authorizer_reply,
  1924. .invalidate_authorizer = invalidate_authorizer,
  1925. .alloc_msg = alloc_msg,
  1926. .fault = osd_reset,
  1927. };