osd_client.c 59 KB

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