xen-blkfront.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  1. /*
  2. * blkfront.c
  3. *
  4. * XenLinux virtual block device driver.
  5. *
  6. * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
  7. * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
  8. * Copyright (c) 2004, Christian Limpach
  9. * Copyright (c) 2004, Andrew Warfield
  10. * Copyright (c) 2005, Christopher Clark
  11. * Copyright (c) 2005, XenSource Ltd
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation; or, when distributed
  16. * separately from the Linux kernel or incorporated into other
  17. * software packages, subject to the following license:
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this source file (the "Software"), to deal in the Software without
  21. * restriction, including without limitation the rights to use, copy, modify,
  22. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  23. * and to permit persons to whom the Software is furnished to do so, subject to
  24. * the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in
  27. * all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  34. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  35. * IN THE SOFTWARE.
  36. */
  37. #include <linux/interrupt.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/hdreg.h>
  40. #include <linux/cdrom.h>
  41. #include <linux/module.h>
  42. #include <linux/slab.h>
  43. #include <linux/mutex.h>
  44. #include <linux/scatterlist.h>
  45. #include <linux/bitmap.h>
  46. #include <linux/list.h>
  47. #include <xen/xen.h>
  48. #include <xen/xenbus.h>
  49. #include <xen/grant_table.h>
  50. #include <xen/events.h>
  51. #include <xen/page.h>
  52. #include <xen/platform_pci.h>
  53. #include <xen/interface/grant_table.h>
  54. #include <xen/interface/io/blkif.h>
  55. #include <xen/interface/io/protocols.h>
  56. #include <asm/xen/hypervisor.h>
  57. enum blkif_state {
  58. BLKIF_STATE_DISCONNECTED,
  59. BLKIF_STATE_CONNECTED,
  60. BLKIF_STATE_SUSPENDED,
  61. };
  62. struct grant {
  63. grant_ref_t gref;
  64. unsigned long pfn;
  65. struct list_head node;
  66. };
  67. struct blk_shadow {
  68. struct blkif_request req;
  69. struct request *request;
  70. struct grant **grants_used;
  71. struct grant **indirect_grants;
  72. struct scatterlist *sg;
  73. };
  74. struct split_bio {
  75. struct bio *bio;
  76. atomic_t pending;
  77. int err;
  78. };
  79. static DEFINE_MUTEX(blkfront_mutex);
  80. static const struct block_device_operations xlvbd_block_fops;
  81. /*
  82. * Maximum number of segments in indirect requests, the actual value used by
  83. * the frontend driver is the minimum of this value and the value provided
  84. * by the backend driver.
  85. */
  86. static unsigned int xen_blkif_max_segments = 32;
  87. module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
  88. MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
  89. #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
  90. /*
  91. * We have one of these per vbd, whether ide, scsi or 'other'. They
  92. * hang in private_data off the gendisk structure. We may end up
  93. * putting all kinds of interesting stuff here :-)
  94. */
  95. struct blkfront_info
  96. {
  97. spinlock_t io_lock;
  98. struct mutex mutex;
  99. struct xenbus_device *xbdev;
  100. struct gendisk *gd;
  101. int vdevice;
  102. blkif_vdev_t handle;
  103. enum blkif_state connected;
  104. int ring_ref;
  105. struct blkif_front_ring ring;
  106. unsigned int evtchn, irq;
  107. struct request_queue *rq;
  108. struct work_struct work;
  109. struct gnttab_free_callback callback;
  110. struct blk_shadow shadow[BLK_RING_SIZE];
  111. struct list_head grants;
  112. struct list_head indirect_pages;
  113. unsigned int persistent_gnts_c;
  114. unsigned long shadow_free;
  115. unsigned int feature_flush;
  116. unsigned int flush_op;
  117. unsigned int feature_discard:1;
  118. unsigned int feature_secdiscard:1;
  119. unsigned int discard_granularity;
  120. unsigned int discard_alignment;
  121. unsigned int feature_persistent:1;
  122. unsigned int max_indirect_segments;
  123. int is_ready;
  124. };
  125. static unsigned int nr_minors;
  126. static unsigned long *minors;
  127. static DEFINE_SPINLOCK(minor_lock);
  128. #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
  129. (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
  130. #define GRANT_INVALID_REF 0
  131. #define PARTS_PER_DISK 16
  132. #define PARTS_PER_EXT_DISK 256
  133. #define BLKIF_MAJOR(dev) ((dev)>>8)
  134. #define BLKIF_MINOR(dev) ((dev) & 0xff)
  135. #define EXT_SHIFT 28
  136. #define EXTENDED (1<<EXT_SHIFT)
  137. #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
  138. #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
  139. #define EMULATED_HD_DISK_MINOR_OFFSET (0)
  140. #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
  141. #define EMULATED_SD_DISK_MINOR_OFFSET (0)
  142. #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
  143. #define DEV_NAME "xvd" /* name in /dev */
  144. #define SEGS_PER_INDIRECT_FRAME \
  145. (PAGE_SIZE/sizeof(struct blkif_request_segment_aligned))
  146. #define INDIRECT_GREFS(_segs) \
  147. ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
  148. static int blkfront_setup_indirect(struct blkfront_info *info);
  149. static int get_id_from_freelist(struct blkfront_info *info)
  150. {
  151. unsigned long free = info->shadow_free;
  152. BUG_ON(free >= BLK_RING_SIZE);
  153. info->shadow_free = info->shadow[free].req.u.rw.id;
  154. info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
  155. return free;
  156. }
  157. static int add_id_to_freelist(struct blkfront_info *info,
  158. unsigned long id)
  159. {
  160. if (info->shadow[id].req.u.rw.id != id)
  161. return -EINVAL;
  162. if (info->shadow[id].request == NULL)
  163. return -EINVAL;
  164. info->shadow[id].req.u.rw.id = info->shadow_free;
  165. info->shadow[id].request = NULL;
  166. info->shadow_free = id;
  167. return 0;
  168. }
  169. static int fill_grant_buffer(struct blkfront_info *info, int num)
  170. {
  171. struct page *granted_page;
  172. struct grant *gnt_list_entry, *n;
  173. int i = 0;
  174. while(i < num) {
  175. gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
  176. if (!gnt_list_entry)
  177. goto out_of_memory;
  178. if (info->feature_persistent) {
  179. granted_page = alloc_page(GFP_NOIO);
  180. if (!granted_page) {
  181. kfree(gnt_list_entry);
  182. goto out_of_memory;
  183. }
  184. gnt_list_entry->pfn = page_to_pfn(granted_page);
  185. }
  186. gnt_list_entry->gref = GRANT_INVALID_REF;
  187. list_add(&gnt_list_entry->node, &info->grants);
  188. i++;
  189. }
  190. return 0;
  191. out_of_memory:
  192. list_for_each_entry_safe(gnt_list_entry, n,
  193. &info->grants, node) {
  194. list_del(&gnt_list_entry->node);
  195. if (info->feature_persistent)
  196. __free_page(pfn_to_page(gnt_list_entry->pfn));
  197. kfree(gnt_list_entry);
  198. i--;
  199. }
  200. BUG_ON(i != 0);
  201. return -ENOMEM;
  202. }
  203. static struct grant *get_grant(grant_ref_t *gref_head,
  204. unsigned long pfn,
  205. struct blkfront_info *info)
  206. {
  207. struct grant *gnt_list_entry;
  208. unsigned long buffer_mfn;
  209. BUG_ON(list_empty(&info->grants));
  210. gnt_list_entry = list_first_entry(&info->grants, struct grant,
  211. node);
  212. list_del(&gnt_list_entry->node);
  213. if (gnt_list_entry->gref != GRANT_INVALID_REF) {
  214. info->persistent_gnts_c--;
  215. return gnt_list_entry;
  216. }
  217. /* Assign a gref to this page */
  218. gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
  219. BUG_ON(gnt_list_entry->gref == -ENOSPC);
  220. if (!info->feature_persistent) {
  221. BUG_ON(!pfn);
  222. gnt_list_entry->pfn = pfn;
  223. }
  224. buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
  225. gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
  226. info->xbdev->otherend_id,
  227. buffer_mfn, 0);
  228. return gnt_list_entry;
  229. }
  230. static const char *op_name(int op)
  231. {
  232. static const char *const names[] = {
  233. [BLKIF_OP_READ] = "read",
  234. [BLKIF_OP_WRITE] = "write",
  235. [BLKIF_OP_WRITE_BARRIER] = "barrier",
  236. [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
  237. [BLKIF_OP_DISCARD] = "discard" };
  238. if (op < 0 || op >= ARRAY_SIZE(names))
  239. return "unknown";
  240. if (!names[op])
  241. return "reserved";
  242. return names[op];
  243. }
  244. static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
  245. {
  246. unsigned int end = minor + nr;
  247. int rc;
  248. if (end > nr_minors) {
  249. unsigned long *bitmap, *old;
  250. bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
  251. GFP_KERNEL);
  252. if (bitmap == NULL)
  253. return -ENOMEM;
  254. spin_lock(&minor_lock);
  255. if (end > nr_minors) {
  256. old = minors;
  257. memcpy(bitmap, minors,
  258. BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
  259. minors = bitmap;
  260. nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
  261. } else
  262. old = bitmap;
  263. spin_unlock(&minor_lock);
  264. kfree(old);
  265. }
  266. spin_lock(&minor_lock);
  267. if (find_next_bit(minors, end, minor) >= end) {
  268. bitmap_set(minors, minor, nr);
  269. rc = 0;
  270. } else
  271. rc = -EBUSY;
  272. spin_unlock(&minor_lock);
  273. return rc;
  274. }
  275. static void xlbd_release_minors(unsigned int minor, unsigned int nr)
  276. {
  277. unsigned int end = minor + nr;
  278. BUG_ON(end > nr_minors);
  279. spin_lock(&minor_lock);
  280. bitmap_clear(minors, minor, nr);
  281. spin_unlock(&minor_lock);
  282. }
  283. static void blkif_restart_queue_callback(void *arg)
  284. {
  285. struct blkfront_info *info = (struct blkfront_info *)arg;
  286. schedule_work(&info->work);
  287. }
  288. static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
  289. {
  290. /* We don't have real geometry info, but let's at least return
  291. values consistent with the size of the device */
  292. sector_t nsect = get_capacity(bd->bd_disk);
  293. sector_t cylinders = nsect;
  294. hg->heads = 0xff;
  295. hg->sectors = 0x3f;
  296. sector_div(cylinders, hg->heads * hg->sectors);
  297. hg->cylinders = cylinders;
  298. if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
  299. hg->cylinders = 0xffff;
  300. return 0;
  301. }
  302. static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
  303. unsigned command, unsigned long argument)
  304. {
  305. struct blkfront_info *info = bdev->bd_disk->private_data;
  306. int i;
  307. dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
  308. command, (long)argument);
  309. switch (command) {
  310. case CDROMMULTISESSION:
  311. dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
  312. for (i = 0; i < sizeof(struct cdrom_multisession); i++)
  313. if (put_user(0, (char __user *)(argument + i)))
  314. return -EFAULT;
  315. return 0;
  316. case CDROM_GET_CAPABILITY: {
  317. struct gendisk *gd = info->gd;
  318. if (gd->flags & GENHD_FL_CD)
  319. return 0;
  320. return -EINVAL;
  321. }
  322. default:
  323. /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
  324. command);*/
  325. return -EINVAL; /* same return as native Linux */
  326. }
  327. return 0;
  328. }
  329. /*
  330. * Generate a Xen blkfront IO request from a blk layer request. Reads
  331. * and writes are handled as expected.
  332. *
  333. * @req: a request struct
  334. */
  335. static int blkif_queue_request(struct request *req)
  336. {
  337. struct blkfront_info *info = req->rq_disk->private_data;
  338. struct blkif_request *ring_req;
  339. unsigned long id;
  340. unsigned int fsect, lsect;
  341. int i, ref, n;
  342. struct blkif_request_segment_aligned *segments = NULL;
  343. /*
  344. * Used to store if we are able to queue the request by just using
  345. * existing persistent grants, or if we have to get new grants,
  346. * as there are not sufficiently many free.
  347. */
  348. bool new_persistent_gnts;
  349. grant_ref_t gref_head;
  350. struct grant *gnt_list_entry = NULL;
  351. struct scatterlist *sg;
  352. int nseg, max_grefs;
  353. if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
  354. return 1;
  355. max_grefs = req->nr_phys_segments;
  356. if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
  357. /*
  358. * If we are using indirect segments we need to account
  359. * for the indirect grefs used in the request.
  360. */
  361. max_grefs += INDIRECT_GREFS(req->nr_phys_segments);
  362. /* Check if we have enough grants to allocate a requests */
  363. if (info->persistent_gnts_c < max_grefs) {
  364. new_persistent_gnts = 1;
  365. if (gnttab_alloc_grant_references(
  366. max_grefs - info->persistent_gnts_c,
  367. &gref_head) < 0) {
  368. gnttab_request_free_callback(
  369. &info->callback,
  370. blkif_restart_queue_callback,
  371. info,
  372. max_grefs);
  373. return 1;
  374. }
  375. } else
  376. new_persistent_gnts = 0;
  377. /* Fill out a communications ring structure. */
  378. ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  379. id = get_id_from_freelist(info);
  380. info->shadow[id].request = req;
  381. if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
  382. ring_req->operation = BLKIF_OP_DISCARD;
  383. ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
  384. ring_req->u.discard.id = id;
  385. ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
  386. if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
  387. ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
  388. else
  389. ring_req->u.discard.flag = 0;
  390. } else {
  391. BUG_ON(info->max_indirect_segments == 0 &&
  392. req->nr_phys_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
  393. BUG_ON(info->max_indirect_segments &&
  394. req->nr_phys_segments > info->max_indirect_segments);
  395. nseg = blk_rq_map_sg(req->q, req, info->shadow[id].sg);
  396. ring_req->u.rw.id = id;
  397. if (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
  398. /*
  399. * The indirect operation can only be a BLKIF_OP_READ or
  400. * BLKIF_OP_WRITE
  401. */
  402. BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
  403. ring_req->operation = BLKIF_OP_INDIRECT;
  404. ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
  405. BLKIF_OP_WRITE : BLKIF_OP_READ;
  406. ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
  407. ring_req->u.indirect.handle = info->handle;
  408. ring_req->u.indirect.nr_segments = nseg;
  409. } else {
  410. ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
  411. ring_req->u.rw.handle = info->handle;
  412. ring_req->operation = rq_data_dir(req) ?
  413. BLKIF_OP_WRITE : BLKIF_OP_READ;
  414. if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
  415. /*
  416. * Ideally we can do an unordered flush-to-disk. In case the
  417. * backend onlysupports barriers, use that. A barrier request
  418. * a superset of FUA, so we can implement it the same
  419. * way. (It's also a FLUSH+FUA, since it is
  420. * guaranteed ordered WRT previous writes.)
  421. */
  422. ring_req->operation = info->flush_op;
  423. }
  424. ring_req->u.rw.nr_segments = nseg;
  425. }
  426. for_each_sg(info->shadow[id].sg, sg, nseg, i) {
  427. fsect = sg->offset >> 9;
  428. lsect = fsect + (sg->length >> 9) - 1;
  429. if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
  430. (i % SEGS_PER_INDIRECT_FRAME == 0)) {
  431. unsigned long pfn;
  432. if (segments)
  433. kunmap_atomic(segments);
  434. n = i / SEGS_PER_INDIRECT_FRAME;
  435. if (!info->feature_persistent) {
  436. struct page *indirect_page;
  437. /* Fetch a pre-allocated page to use for indirect grefs */
  438. BUG_ON(list_empty(&info->indirect_pages));
  439. indirect_page = list_first_entry(&info->indirect_pages,
  440. struct page, lru);
  441. list_del(&indirect_page->lru);
  442. pfn = page_to_pfn(indirect_page);
  443. }
  444. gnt_list_entry = get_grant(&gref_head, pfn, info);
  445. info->shadow[id].indirect_grants[n] = gnt_list_entry;
  446. segments = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
  447. ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
  448. }
  449. gnt_list_entry = get_grant(&gref_head, page_to_pfn(sg_page(sg)), info);
  450. ref = gnt_list_entry->gref;
  451. info->shadow[id].grants_used[i] = gnt_list_entry;
  452. if (rq_data_dir(req) && info->feature_persistent) {
  453. char *bvec_data;
  454. void *shared_data;
  455. BUG_ON(sg->offset + sg->length > PAGE_SIZE);
  456. shared_data = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
  457. bvec_data = kmap_atomic(sg_page(sg));
  458. /*
  459. * this does not wipe data stored outside the
  460. * range sg->offset..sg->offset+sg->length.
  461. * Therefore, blkback *could* see data from
  462. * previous requests. This is OK as long as
  463. * persistent grants are shared with just one
  464. * domain. It may need refactoring if this
  465. * changes
  466. */
  467. memcpy(shared_data + sg->offset,
  468. bvec_data + sg->offset,
  469. sg->length);
  470. kunmap_atomic(bvec_data);
  471. kunmap_atomic(shared_data);
  472. }
  473. if (ring_req->operation != BLKIF_OP_INDIRECT) {
  474. ring_req->u.rw.seg[i] =
  475. (struct blkif_request_segment) {
  476. .gref = ref,
  477. .first_sect = fsect,
  478. .last_sect = lsect };
  479. } else {
  480. n = i % SEGS_PER_INDIRECT_FRAME;
  481. segments[n] =
  482. (struct blkif_request_segment_aligned) {
  483. .gref = ref,
  484. .first_sect = fsect,
  485. .last_sect = lsect };
  486. }
  487. }
  488. if (segments)
  489. kunmap_atomic(segments);
  490. }
  491. info->ring.req_prod_pvt++;
  492. /* Keep a private copy so we can reissue requests when recovering. */
  493. info->shadow[id].req = *ring_req;
  494. if (new_persistent_gnts)
  495. gnttab_free_grant_references(gref_head);
  496. return 0;
  497. }
  498. static inline void flush_requests(struct blkfront_info *info)
  499. {
  500. int notify;
  501. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
  502. if (notify)
  503. notify_remote_via_irq(info->irq);
  504. }
  505. /*
  506. * do_blkif_request
  507. * read a block; request is in a request queue
  508. */
  509. static void do_blkif_request(struct request_queue *rq)
  510. {
  511. struct blkfront_info *info = NULL;
  512. struct request *req;
  513. int queued;
  514. pr_debug("Entered do_blkif_request\n");
  515. queued = 0;
  516. while ((req = blk_peek_request(rq)) != NULL) {
  517. info = req->rq_disk->private_data;
  518. if (RING_FULL(&info->ring))
  519. goto wait;
  520. blk_start_request(req);
  521. if ((req->cmd_type != REQ_TYPE_FS) ||
  522. ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
  523. !info->flush_op)) {
  524. __blk_end_request_all(req, -EIO);
  525. continue;
  526. }
  527. pr_debug("do_blk_req %p: cmd %p, sec %lx, "
  528. "(%u/%u) buffer:%p [%s]\n",
  529. req, req->cmd, (unsigned long)blk_rq_pos(req),
  530. blk_rq_cur_sectors(req), blk_rq_sectors(req),
  531. req->buffer, rq_data_dir(req) ? "write" : "read");
  532. if (blkif_queue_request(req)) {
  533. blk_requeue_request(rq, req);
  534. wait:
  535. /* Avoid pointless unplugs. */
  536. blk_stop_queue(rq);
  537. break;
  538. }
  539. queued++;
  540. }
  541. if (queued != 0)
  542. flush_requests(info);
  543. }
  544. static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
  545. unsigned int physical_sector_size,
  546. unsigned int segments)
  547. {
  548. struct request_queue *rq;
  549. struct blkfront_info *info = gd->private_data;
  550. rq = blk_init_queue(do_blkif_request, &info->io_lock);
  551. if (rq == NULL)
  552. return -1;
  553. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
  554. if (info->feature_discard) {
  555. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
  556. blk_queue_max_discard_sectors(rq, get_capacity(gd));
  557. rq->limits.discard_granularity = info->discard_granularity;
  558. rq->limits.discard_alignment = info->discard_alignment;
  559. if (info->feature_secdiscard)
  560. queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
  561. }
  562. /* Hard sector size and max sectors impersonate the equiv. hardware. */
  563. blk_queue_logical_block_size(rq, sector_size);
  564. blk_queue_physical_block_size(rq, physical_sector_size);
  565. blk_queue_max_hw_sectors(rq, (segments * PAGE_SIZE) / 512);
  566. /* Each segment in a request is up to an aligned page in size. */
  567. blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
  568. blk_queue_max_segment_size(rq, PAGE_SIZE);
  569. /* Ensure a merged request will fit in a single I/O ring slot. */
  570. blk_queue_max_segments(rq, segments);
  571. /* Make sure buffer addresses are sector-aligned. */
  572. blk_queue_dma_alignment(rq, 511);
  573. /* Make sure we don't use bounce buffers. */
  574. blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
  575. gd->queue = rq;
  576. return 0;
  577. }
  578. static void xlvbd_flush(struct blkfront_info *info)
  579. {
  580. blk_queue_flush(info->rq, info->feature_flush);
  581. printk(KERN_INFO "blkfront: %s: %s: %s %s %s %s %s\n",
  582. info->gd->disk_name,
  583. info->flush_op == BLKIF_OP_WRITE_BARRIER ?
  584. "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
  585. "flush diskcache" : "barrier or flush"),
  586. info->feature_flush ? "enabled;" : "disabled;",
  587. "persistent grants:",
  588. info->feature_persistent ? "enabled;" : "disabled;",
  589. "indirect descriptors:",
  590. info->max_indirect_segments ? "enabled;" : "disabled;");
  591. }
  592. static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
  593. {
  594. int major;
  595. major = BLKIF_MAJOR(vdevice);
  596. *minor = BLKIF_MINOR(vdevice);
  597. switch (major) {
  598. case XEN_IDE0_MAJOR:
  599. *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
  600. *minor = ((*minor / 64) * PARTS_PER_DISK) +
  601. EMULATED_HD_DISK_MINOR_OFFSET;
  602. break;
  603. case XEN_IDE1_MAJOR:
  604. *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
  605. *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
  606. EMULATED_HD_DISK_MINOR_OFFSET;
  607. break;
  608. case XEN_SCSI_DISK0_MAJOR:
  609. *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
  610. *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
  611. break;
  612. case XEN_SCSI_DISK1_MAJOR:
  613. case XEN_SCSI_DISK2_MAJOR:
  614. case XEN_SCSI_DISK3_MAJOR:
  615. case XEN_SCSI_DISK4_MAJOR:
  616. case XEN_SCSI_DISK5_MAJOR:
  617. case XEN_SCSI_DISK6_MAJOR:
  618. case XEN_SCSI_DISK7_MAJOR:
  619. *offset = (*minor / PARTS_PER_DISK) +
  620. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
  621. EMULATED_SD_DISK_NAME_OFFSET;
  622. *minor = *minor +
  623. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
  624. EMULATED_SD_DISK_MINOR_OFFSET;
  625. break;
  626. case XEN_SCSI_DISK8_MAJOR:
  627. case XEN_SCSI_DISK9_MAJOR:
  628. case XEN_SCSI_DISK10_MAJOR:
  629. case XEN_SCSI_DISK11_MAJOR:
  630. case XEN_SCSI_DISK12_MAJOR:
  631. case XEN_SCSI_DISK13_MAJOR:
  632. case XEN_SCSI_DISK14_MAJOR:
  633. case XEN_SCSI_DISK15_MAJOR:
  634. *offset = (*minor / PARTS_PER_DISK) +
  635. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
  636. EMULATED_SD_DISK_NAME_OFFSET;
  637. *minor = *minor +
  638. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
  639. EMULATED_SD_DISK_MINOR_OFFSET;
  640. break;
  641. case XENVBD_MAJOR:
  642. *offset = *minor / PARTS_PER_DISK;
  643. break;
  644. default:
  645. printk(KERN_WARNING "blkfront: your disk configuration is "
  646. "incorrect, please use an xvd device instead\n");
  647. return -ENODEV;
  648. }
  649. return 0;
  650. }
  651. static char *encode_disk_name(char *ptr, unsigned int n)
  652. {
  653. if (n >= 26)
  654. ptr = encode_disk_name(ptr, n / 26 - 1);
  655. *ptr = 'a' + n % 26;
  656. return ptr + 1;
  657. }
  658. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  659. struct blkfront_info *info,
  660. u16 vdisk_info, u16 sector_size,
  661. unsigned int physical_sector_size)
  662. {
  663. struct gendisk *gd;
  664. int nr_minors = 1;
  665. int err;
  666. unsigned int offset;
  667. int minor;
  668. int nr_parts;
  669. char *ptr;
  670. BUG_ON(info->gd != NULL);
  671. BUG_ON(info->rq != NULL);
  672. if ((info->vdevice>>EXT_SHIFT) > 1) {
  673. /* this is above the extended range; something is wrong */
  674. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  675. return -ENODEV;
  676. }
  677. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  678. err = xen_translate_vdev(info->vdevice, &minor, &offset);
  679. if (err)
  680. return err;
  681. nr_parts = PARTS_PER_DISK;
  682. } else {
  683. minor = BLKIF_MINOR_EXT(info->vdevice);
  684. nr_parts = PARTS_PER_EXT_DISK;
  685. offset = minor / nr_parts;
  686. if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
  687. printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
  688. "emulated IDE disks,\n\t choose an xvd device name"
  689. "from xvde on\n", info->vdevice);
  690. }
  691. if (minor >> MINORBITS) {
  692. pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
  693. info->vdevice, minor);
  694. return -ENODEV;
  695. }
  696. if ((minor % nr_parts) == 0)
  697. nr_minors = nr_parts;
  698. err = xlbd_reserve_minors(minor, nr_minors);
  699. if (err)
  700. goto out;
  701. err = -ENODEV;
  702. gd = alloc_disk(nr_minors);
  703. if (gd == NULL)
  704. goto release;
  705. strcpy(gd->disk_name, DEV_NAME);
  706. ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
  707. BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
  708. if (nr_minors > 1)
  709. *ptr = 0;
  710. else
  711. snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
  712. "%d", minor & (nr_parts - 1));
  713. gd->major = XENVBD_MAJOR;
  714. gd->first_minor = minor;
  715. gd->fops = &xlvbd_block_fops;
  716. gd->private_data = info;
  717. gd->driverfs_dev = &(info->xbdev->dev);
  718. set_capacity(gd, capacity);
  719. if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
  720. info->max_indirect_segments ? :
  721. BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  722. del_gendisk(gd);
  723. goto release;
  724. }
  725. info->rq = gd->queue;
  726. info->gd = gd;
  727. xlvbd_flush(info);
  728. if (vdisk_info & VDISK_READONLY)
  729. set_disk_ro(gd, 1);
  730. if (vdisk_info & VDISK_REMOVABLE)
  731. gd->flags |= GENHD_FL_REMOVABLE;
  732. if (vdisk_info & VDISK_CDROM)
  733. gd->flags |= GENHD_FL_CD;
  734. return 0;
  735. release:
  736. xlbd_release_minors(minor, nr_minors);
  737. out:
  738. return err;
  739. }
  740. static void xlvbd_release_gendisk(struct blkfront_info *info)
  741. {
  742. unsigned int minor, nr_minors;
  743. unsigned long flags;
  744. if (info->rq == NULL)
  745. return;
  746. spin_lock_irqsave(&info->io_lock, flags);
  747. /* No more blkif_request(). */
  748. blk_stop_queue(info->rq);
  749. /* No more gnttab callback work. */
  750. gnttab_cancel_free_callback(&info->callback);
  751. spin_unlock_irqrestore(&info->io_lock, flags);
  752. /* Flush gnttab callback work. Must be done with no locks held. */
  753. flush_work(&info->work);
  754. del_gendisk(info->gd);
  755. minor = info->gd->first_minor;
  756. nr_minors = info->gd->minors;
  757. xlbd_release_minors(minor, nr_minors);
  758. blk_cleanup_queue(info->rq);
  759. info->rq = NULL;
  760. put_disk(info->gd);
  761. info->gd = NULL;
  762. }
  763. static void kick_pending_request_queues(struct blkfront_info *info)
  764. {
  765. if (!RING_FULL(&info->ring)) {
  766. /* Re-enable calldowns. */
  767. blk_start_queue(info->rq);
  768. /* Kick things off immediately. */
  769. do_blkif_request(info->rq);
  770. }
  771. }
  772. static void blkif_restart_queue(struct work_struct *work)
  773. {
  774. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  775. spin_lock_irq(&info->io_lock);
  776. if (info->connected == BLKIF_STATE_CONNECTED)
  777. kick_pending_request_queues(info);
  778. spin_unlock_irq(&info->io_lock);
  779. }
  780. static void blkif_free(struct blkfront_info *info, int suspend)
  781. {
  782. struct grant *persistent_gnt;
  783. struct grant *n;
  784. int i, j, segs;
  785. /* Prevent new requests being issued until we fix things up. */
  786. spin_lock_irq(&info->io_lock);
  787. info->connected = suspend ?
  788. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  789. /* No more blkif_request(). */
  790. if (info->rq)
  791. blk_stop_queue(info->rq);
  792. /* Remove all persistent grants */
  793. if (!list_empty(&info->grants)) {
  794. list_for_each_entry_safe(persistent_gnt, n,
  795. &info->grants, node) {
  796. list_del(&persistent_gnt->node);
  797. if (persistent_gnt->gref != GRANT_INVALID_REF) {
  798. gnttab_end_foreign_access(persistent_gnt->gref,
  799. 0, 0UL);
  800. info->persistent_gnts_c--;
  801. }
  802. if (info->feature_persistent)
  803. __free_page(pfn_to_page(persistent_gnt->pfn));
  804. kfree(persistent_gnt);
  805. }
  806. }
  807. BUG_ON(info->persistent_gnts_c != 0);
  808. /*
  809. * Remove indirect pages, this only happens when using indirect
  810. * descriptors but not persistent grants
  811. */
  812. if (!list_empty(&info->indirect_pages)) {
  813. struct page *indirect_page, *n;
  814. BUG_ON(info->feature_persistent);
  815. list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
  816. list_del(&indirect_page->lru);
  817. __free_page(indirect_page);
  818. }
  819. }
  820. for (i = 0; i < BLK_RING_SIZE; i++) {
  821. /*
  822. * Clear persistent grants present in requests already
  823. * on the shared ring
  824. */
  825. if (!info->shadow[i].request)
  826. goto free_shadow;
  827. segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
  828. info->shadow[i].req.u.indirect.nr_segments :
  829. info->shadow[i].req.u.rw.nr_segments;
  830. for (j = 0; j < segs; j++) {
  831. persistent_gnt = info->shadow[i].grants_used[j];
  832. gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
  833. if (info->feature_persistent)
  834. __free_page(pfn_to_page(persistent_gnt->pfn));
  835. kfree(persistent_gnt);
  836. }
  837. if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
  838. /*
  839. * If this is not an indirect operation don't try to
  840. * free indirect segments
  841. */
  842. goto free_shadow;
  843. for (j = 0; j < INDIRECT_GREFS(segs); j++) {
  844. persistent_gnt = info->shadow[i].indirect_grants[j];
  845. gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
  846. __free_page(pfn_to_page(persistent_gnt->pfn));
  847. kfree(persistent_gnt);
  848. }
  849. free_shadow:
  850. kfree(info->shadow[i].grants_used);
  851. info->shadow[i].grants_used = NULL;
  852. kfree(info->shadow[i].indirect_grants);
  853. info->shadow[i].indirect_grants = NULL;
  854. kfree(info->shadow[i].sg);
  855. info->shadow[i].sg = NULL;
  856. }
  857. /* No more gnttab callback work. */
  858. gnttab_cancel_free_callback(&info->callback);
  859. spin_unlock_irq(&info->io_lock);
  860. /* Flush gnttab callback work. Must be done with no locks held. */
  861. flush_work(&info->work);
  862. /* Free resources associated with old device channel. */
  863. if (info->ring_ref != GRANT_INVALID_REF) {
  864. gnttab_end_foreign_access(info->ring_ref, 0,
  865. (unsigned long)info->ring.sring);
  866. info->ring_ref = GRANT_INVALID_REF;
  867. info->ring.sring = NULL;
  868. }
  869. if (info->irq)
  870. unbind_from_irqhandler(info->irq, info);
  871. info->evtchn = info->irq = 0;
  872. }
  873. static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
  874. struct blkif_response *bret)
  875. {
  876. int i = 0;
  877. struct scatterlist *sg;
  878. char *bvec_data;
  879. void *shared_data;
  880. int nseg;
  881. nseg = s->req.operation == BLKIF_OP_INDIRECT ?
  882. s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
  883. if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
  884. /*
  885. * Copy the data received from the backend into the bvec.
  886. * Since bv_offset can be different than 0, and bv_len different
  887. * than PAGE_SIZE, we have to keep track of the current offset,
  888. * to be sure we are copying the data from the right shared page.
  889. */
  890. for_each_sg(s->sg, sg, nseg, i) {
  891. BUG_ON(sg->offset + sg->length > PAGE_SIZE);
  892. shared_data = kmap_atomic(
  893. pfn_to_page(s->grants_used[i]->pfn));
  894. bvec_data = kmap_atomic(sg_page(sg));
  895. memcpy(bvec_data + sg->offset,
  896. shared_data + sg->offset,
  897. sg->length);
  898. kunmap_atomic(bvec_data);
  899. kunmap_atomic(shared_data);
  900. }
  901. }
  902. /* Add the persistent grant into the list of free grants */
  903. for (i = 0; i < nseg; i++) {
  904. if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
  905. /*
  906. * If the grant is still mapped by the backend (the
  907. * backend has chosen to make this grant persistent)
  908. * we add it at the head of the list, so it will be
  909. * reused first.
  910. */
  911. if (!info->feature_persistent)
  912. pr_alert_ratelimited("backed has not unmapped grant: %u\n",
  913. s->grants_used[i]->gref);
  914. list_add(&s->grants_used[i]->node, &info->grants);
  915. info->persistent_gnts_c++;
  916. } else {
  917. /*
  918. * If the grant is not mapped by the backend we end the
  919. * foreign access and add it to the tail of the list,
  920. * so it will not be picked again unless we run out of
  921. * persistent grants.
  922. */
  923. gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
  924. s->grants_used[i]->gref = GRANT_INVALID_REF;
  925. list_add_tail(&s->grants_used[i]->node, &info->grants);
  926. }
  927. }
  928. if (s->req.operation == BLKIF_OP_INDIRECT) {
  929. for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
  930. if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
  931. if (!info->feature_persistent)
  932. pr_alert_ratelimited("backed has not unmapped grant: %u\n",
  933. s->indirect_grants[i]->gref);
  934. list_add(&s->indirect_grants[i]->node, &info->grants);
  935. info->persistent_gnts_c++;
  936. } else {
  937. struct page *indirect_page;
  938. gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
  939. /*
  940. * Add the used indirect page back to the list of
  941. * available pages for indirect grefs.
  942. */
  943. indirect_page = pfn_to_page(s->indirect_grants[i]->pfn);
  944. list_add(&indirect_page->lru, &info->indirect_pages);
  945. s->indirect_grants[i]->gref = GRANT_INVALID_REF;
  946. list_add_tail(&s->indirect_grants[i]->node, &info->grants);
  947. }
  948. }
  949. }
  950. }
  951. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  952. {
  953. struct request *req;
  954. struct blkif_response *bret;
  955. RING_IDX i, rp;
  956. unsigned long flags;
  957. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  958. int error;
  959. spin_lock_irqsave(&info->io_lock, flags);
  960. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  961. spin_unlock_irqrestore(&info->io_lock, flags);
  962. return IRQ_HANDLED;
  963. }
  964. again:
  965. rp = info->ring.sring->rsp_prod;
  966. rmb(); /* Ensure we see queued responses up to 'rp'. */
  967. for (i = info->ring.rsp_cons; i != rp; i++) {
  968. unsigned long id;
  969. bret = RING_GET_RESPONSE(&info->ring, i);
  970. id = bret->id;
  971. /*
  972. * The backend has messed up and given us an id that we would
  973. * never have given to it (we stamp it up to BLK_RING_SIZE -
  974. * look in get_id_from_freelist.
  975. */
  976. if (id >= BLK_RING_SIZE) {
  977. WARN(1, "%s: response to %s has incorrect id (%ld)\n",
  978. info->gd->disk_name, op_name(bret->operation), id);
  979. /* We can't safely get the 'struct request' as
  980. * the id is busted. */
  981. continue;
  982. }
  983. req = info->shadow[id].request;
  984. if (bret->operation != BLKIF_OP_DISCARD)
  985. blkif_completion(&info->shadow[id], info, bret);
  986. if (add_id_to_freelist(info, id)) {
  987. WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
  988. info->gd->disk_name, op_name(bret->operation), id);
  989. continue;
  990. }
  991. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  992. switch (bret->operation) {
  993. case BLKIF_OP_DISCARD:
  994. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  995. struct request_queue *rq = info->rq;
  996. printk(KERN_WARNING "blkfront: %s: %s op failed\n",
  997. info->gd->disk_name, op_name(bret->operation));
  998. error = -EOPNOTSUPP;
  999. info->feature_discard = 0;
  1000. info->feature_secdiscard = 0;
  1001. queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
  1002. queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
  1003. }
  1004. __blk_end_request_all(req, error);
  1005. break;
  1006. case BLKIF_OP_FLUSH_DISKCACHE:
  1007. case BLKIF_OP_WRITE_BARRIER:
  1008. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  1009. printk(KERN_WARNING "blkfront: %s: %s op failed\n",
  1010. info->gd->disk_name, op_name(bret->operation));
  1011. error = -EOPNOTSUPP;
  1012. }
  1013. if (unlikely(bret->status == BLKIF_RSP_ERROR &&
  1014. info->shadow[id].req.u.rw.nr_segments == 0)) {
  1015. printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
  1016. info->gd->disk_name, op_name(bret->operation));
  1017. error = -EOPNOTSUPP;
  1018. }
  1019. if (unlikely(error)) {
  1020. if (error == -EOPNOTSUPP)
  1021. error = 0;
  1022. info->feature_flush = 0;
  1023. info->flush_op = 0;
  1024. xlvbd_flush(info);
  1025. }
  1026. /* fall through */
  1027. case BLKIF_OP_READ:
  1028. case BLKIF_OP_WRITE:
  1029. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  1030. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  1031. "request: %x\n", bret->status);
  1032. __blk_end_request_all(req, error);
  1033. break;
  1034. default:
  1035. BUG();
  1036. }
  1037. }
  1038. info->ring.rsp_cons = i;
  1039. if (i != info->ring.req_prod_pvt) {
  1040. int more_to_do;
  1041. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  1042. if (more_to_do)
  1043. goto again;
  1044. } else
  1045. info->ring.sring->rsp_event = i + 1;
  1046. kick_pending_request_queues(info);
  1047. spin_unlock_irqrestore(&info->io_lock, flags);
  1048. return IRQ_HANDLED;
  1049. }
  1050. static int setup_blkring(struct xenbus_device *dev,
  1051. struct blkfront_info *info)
  1052. {
  1053. struct blkif_sring *sring;
  1054. int err;
  1055. info->ring_ref = GRANT_INVALID_REF;
  1056. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  1057. if (!sring) {
  1058. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  1059. return -ENOMEM;
  1060. }
  1061. SHARED_RING_INIT(sring);
  1062. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  1063. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  1064. if (err < 0) {
  1065. free_page((unsigned long)sring);
  1066. info->ring.sring = NULL;
  1067. goto fail;
  1068. }
  1069. info->ring_ref = err;
  1070. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  1071. if (err)
  1072. goto fail;
  1073. err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
  1074. "blkif", info);
  1075. if (err <= 0) {
  1076. xenbus_dev_fatal(dev, err,
  1077. "bind_evtchn_to_irqhandler failed");
  1078. goto fail;
  1079. }
  1080. info->irq = err;
  1081. return 0;
  1082. fail:
  1083. blkif_free(info, 0);
  1084. return err;
  1085. }
  1086. /* Common code used when first setting up, and when resuming. */
  1087. static int talk_to_blkback(struct xenbus_device *dev,
  1088. struct blkfront_info *info)
  1089. {
  1090. const char *message = NULL;
  1091. struct xenbus_transaction xbt;
  1092. int err;
  1093. /* Create shared ring, alloc event channel. */
  1094. err = setup_blkring(dev, info);
  1095. if (err)
  1096. goto out;
  1097. again:
  1098. err = xenbus_transaction_start(&xbt);
  1099. if (err) {
  1100. xenbus_dev_fatal(dev, err, "starting transaction");
  1101. goto destroy_blkring;
  1102. }
  1103. err = xenbus_printf(xbt, dev->nodename,
  1104. "ring-ref", "%u", info->ring_ref);
  1105. if (err) {
  1106. message = "writing ring-ref";
  1107. goto abort_transaction;
  1108. }
  1109. err = xenbus_printf(xbt, dev->nodename,
  1110. "event-channel", "%u", info->evtchn);
  1111. if (err) {
  1112. message = "writing event-channel";
  1113. goto abort_transaction;
  1114. }
  1115. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  1116. XEN_IO_PROTO_ABI_NATIVE);
  1117. if (err) {
  1118. message = "writing protocol";
  1119. goto abort_transaction;
  1120. }
  1121. err = xenbus_printf(xbt, dev->nodename,
  1122. "feature-persistent", "%u", 1);
  1123. if (err)
  1124. dev_warn(&dev->dev,
  1125. "writing persistent grants feature to xenbus");
  1126. err = xenbus_transaction_end(xbt, 0);
  1127. if (err) {
  1128. if (err == -EAGAIN)
  1129. goto again;
  1130. xenbus_dev_fatal(dev, err, "completing transaction");
  1131. goto destroy_blkring;
  1132. }
  1133. xenbus_switch_state(dev, XenbusStateInitialised);
  1134. return 0;
  1135. abort_transaction:
  1136. xenbus_transaction_end(xbt, 1);
  1137. if (message)
  1138. xenbus_dev_fatal(dev, err, "%s", message);
  1139. destroy_blkring:
  1140. blkif_free(info, 0);
  1141. out:
  1142. return err;
  1143. }
  1144. /**
  1145. * Entry point to this code when a new device is created. Allocate the basic
  1146. * structures and the ring buffer for communication with the backend, and
  1147. * inform the backend of the appropriate details for those. Switch to
  1148. * Initialised state.
  1149. */
  1150. static int blkfront_probe(struct xenbus_device *dev,
  1151. const struct xenbus_device_id *id)
  1152. {
  1153. int err, vdevice, i;
  1154. struct blkfront_info *info;
  1155. /* FIXME: Use dynamic device id if this is not set. */
  1156. err = xenbus_scanf(XBT_NIL, dev->nodename,
  1157. "virtual-device", "%i", &vdevice);
  1158. if (err != 1) {
  1159. /* go looking in the extended area instead */
  1160. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  1161. "%i", &vdevice);
  1162. if (err != 1) {
  1163. xenbus_dev_fatal(dev, err, "reading virtual-device");
  1164. return err;
  1165. }
  1166. }
  1167. if (xen_hvm_domain()) {
  1168. char *type;
  1169. int len;
  1170. /* no unplug has been done: do not hook devices != xen vbds */
  1171. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
  1172. int major;
  1173. if (!VDEV_IS_EXTENDED(vdevice))
  1174. major = BLKIF_MAJOR(vdevice);
  1175. else
  1176. major = XENVBD_MAJOR;
  1177. if (major != XENVBD_MAJOR) {
  1178. printk(KERN_INFO
  1179. "%s: HVM does not support vbd %d as xen block device\n",
  1180. __FUNCTION__, vdevice);
  1181. return -ENODEV;
  1182. }
  1183. }
  1184. /* do not create a PV cdrom device if we are an HVM guest */
  1185. type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
  1186. if (IS_ERR(type))
  1187. return -ENODEV;
  1188. if (strncmp(type, "cdrom", 5) == 0) {
  1189. kfree(type);
  1190. return -ENODEV;
  1191. }
  1192. kfree(type);
  1193. }
  1194. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1195. if (!info) {
  1196. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  1197. return -ENOMEM;
  1198. }
  1199. mutex_init(&info->mutex);
  1200. spin_lock_init(&info->io_lock);
  1201. info->xbdev = dev;
  1202. info->vdevice = vdevice;
  1203. INIT_LIST_HEAD(&info->grants);
  1204. INIT_LIST_HEAD(&info->indirect_pages);
  1205. info->persistent_gnts_c = 0;
  1206. info->connected = BLKIF_STATE_DISCONNECTED;
  1207. INIT_WORK(&info->work, blkif_restart_queue);
  1208. for (i = 0; i < BLK_RING_SIZE; i++)
  1209. info->shadow[i].req.u.rw.id = i+1;
  1210. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  1211. /* Front end dir is a number, which is used as the id. */
  1212. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  1213. dev_set_drvdata(&dev->dev, info);
  1214. err = talk_to_blkback(dev, info);
  1215. if (err) {
  1216. kfree(info);
  1217. dev_set_drvdata(&dev->dev, NULL);
  1218. return err;
  1219. }
  1220. return 0;
  1221. }
  1222. static void split_bio_end(struct bio *bio, int error)
  1223. {
  1224. struct split_bio *split_bio = bio->bi_private;
  1225. if (error)
  1226. split_bio->err = error;
  1227. if (atomic_dec_and_test(&split_bio->pending)) {
  1228. split_bio->bio->bi_phys_segments = 0;
  1229. bio_endio(split_bio->bio, split_bio->err);
  1230. kfree(split_bio);
  1231. }
  1232. bio_put(bio);
  1233. }
  1234. static int blkif_recover(struct blkfront_info *info)
  1235. {
  1236. int i;
  1237. struct request *req, *n;
  1238. struct blk_shadow *copy;
  1239. int rc;
  1240. struct bio *bio, *cloned_bio;
  1241. struct bio_list bio_list, merge_bio;
  1242. unsigned int segs, offset;
  1243. int pending, size;
  1244. struct split_bio *split_bio;
  1245. struct list_head requests;
  1246. /* Stage 1: Make a safe copy of the shadow state. */
  1247. copy = kmemdup(info->shadow, sizeof(info->shadow),
  1248. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  1249. if (!copy)
  1250. return -ENOMEM;
  1251. /* Stage 2: Set up free list. */
  1252. memset(&info->shadow, 0, sizeof(info->shadow));
  1253. for (i = 0; i < BLK_RING_SIZE; i++)
  1254. info->shadow[i].req.u.rw.id = i+1;
  1255. info->shadow_free = info->ring.req_prod_pvt;
  1256. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  1257. rc = blkfront_setup_indirect(info);
  1258. if (rc) {
  1259. kfree(copy);
  1260. return rc;
  1261. }
  1262. segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
  1263. blk_queue_max_segments(info->rq, segs);
  1264. bio_list_init(&bio_list);
  1265. INIT_LIST_HEAD(&requests);
  1266. for (i = 0; i < BLK_RING_SIZE; i++) {
  1267. /* Not in use? */
  1268. if (!copy[i].request)
  1269. continue;
  1270. /*
  1271. * Get the bios in the request so we can re-queue them.
  1272. */
  1273. if (copy[i].request->cmd_flags &
  1274. (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
  1275. /*
  1276. * Flush operations don't contain bios, so
  1277. * we need to requeue the whole request
  1278. */
  1279. list_add(&copy[i].request->queuelist, &requests);
  1280. continue;
  1281. }
  1282. merge_bio.head = copy[i].request->bio;
  1283. merge_bio.tail = copy[i].request->biotail;
  1284. bio_list_merge(&bio_list, &merge_bio);
  1285. copy[i].request->bio = NULL;
  1286. blk_put_request(copy[i].request);
  1287. }
  1288. kfree(copy);
  1289. /*
  1290. * Empty the queue, this is important because we might have
  1291. * requests in the queue with more segments than what we
  1292. * can handle now.
  1293. */
  1294. spin_lock_irq(&info->io_lock);
  1295. while ((req = blk_fetch_request(info->rq)) != NULL) {
  1296. if (req->cmd_flags &
  1297. (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
  1298. list_add(&req->queuelist, &requests);
  1299. continue;
  1300. }
  1301. merge_bio.head = req->bio;
  1302. merge_bio.tail = req->biotail;
  1303. bio_list_merge(&bio_list, &merge_bio);
  1304. req->bio = NULL;
  1305. if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
  1306. pr_alert("diskcache flush request found!\n");
  1307. __blk_put_request(info->rq, req);
  1308. }
  1309. spin_unlock_irq(&info->io_lock);
  1310. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1311. spin_lock_irq(&info->io_lock);
  1312. /* Now safe for us to use the shared ring */
  1313. info->connected = BLKIF_STATE_CONNECTED;
  1314. /* Kick any other new requests queued since we resumed */
  1315. kick_pending_request_queues(info);
  1316. list_for_each_entry_safe(req, n, &requests, queuelist) {
  1317. /* Requeue pending requests (flush or discard) */
  1318. list_del_init(&req->queuelist);
  1319. BUG_ON(req->nr_phys_segments > segs);
  1320. blk_requeue_request(info->rq, req);
  1321. }
  1322. spin_unlock_irq(&info->io_lock);
  1323. while ((bio = bio_list_pop(&bio_list)) != NULL) {
  1324. /* Traverse the list of pending bios and re-queue them */
  1325. if (bio_segments(bio) > segs) {
  1326. /*
  1327. * This bio has more segments than what we can
  1328. * handle, we have to split it.
  1329. */
  1330. pending = (bio_segments(bio) + segs - 1) / segs;
  1331. split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
  1332. BUG_ON(split_bio == NULL);
  1333. atomic_set(&split_bio->pending, pending);
  1334. split_bio->bio = bio;
  1335. for (i = 0; i < pending; i++) {
  1336. offset = (i * segs * PAGE_SIZE) >> 9;
  1337. size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
  1338. (unsigned int)(bio->bi_size >> 9) - offset);
  1339. cloned_bio = bio_clone(bio, GFP_NOIO);
  1340. BUG_ON(cloned_bio == NULL);
  1341. bio_trim(cloned_bio, offset, size);
  1342. cloned_bio->bi_private = split_bio;
  1343. cloned_bio->bi_end_io = split_bio_end;
  1344. submit_bio(cloned_bio->bi_rw, cloned_bio);
  1345. }
  1346. /*
  1347. * Now we have to wait for all those smaller bios to
  1348. * end, so we can also end the "parent" bio.
  1349. */
  1350. continue;
  1351. }
  1352. /* We don't need to split this bio */
  1353. submit_bio(bio->bi_rw, bio);
  1354. }
  1355. return 0;
  1356. }
  1357. /**
  1358. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1359. * driver restart. We tear down our blkif structure and recreate it, but
  1360. * leave the device-layer structures intact so that this is transparent to the
  1361. * rest of the kernel.
  1362. */
  1363. static int blkfront_resume(struct xenbus_device *dev)
  1364. {
  1365. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1366. int err;
  1367. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  1368. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  1369. err = talk_to_blkback(dev, info);
  1370. /*
  1371. * We have to wait for the backend to switch to
  1372. * connected state, since we want to read which
  1373. * features it supports.
  1374. */
  1375. return err;
  1376. }
  1377. static void
  1378. blkfront_closing(struct blkfront_info *info)
  1379. {
  1380. struct xenbus_device *xbdev = info->xbdev;
  1381. struct block_device *bdev = NULL;
  1382. mutex_lock(&info->mutex);
  1383. if (xbdev->state == XenbusStateClosing) {
  1384. mutex_unlock(&info->mutex);
  1385. return;
  1386. }
  1387. if (info->gd)
  1388. bdev = bdget_disk(info->gd, 0);
  1389. mutex_unlock(&info->mutex);
  1390. if (!bdev) {
  1391. xenbus_frontend_closed(xbdev);
  1392. return;
  1393. }
  1394. mutex_lock(&bdev->bd_mutex);
  1395. if (bdev->bd_openers) {
  1396. xenbus_dev_error(xbdev, -EBUSY,
  1397. "Device in use; refusing to close");
  1398. xenbus_switch_state(xbdev, XenbusStateClosing);
  1399. } else {
  1400. xlvbd_release_gendisk(info);
  1401. xenbus_frontend_closed(xbdev);
  1402. }
  1403. mutex_unlock(&bdev->bd_mutex);
  1404. bdput(bdev);
  1405. }
  1406. static void blkfront_setup_discard(struct blkfront_info *info)
  1407. {
  1408. int err;
  1409. char *type;
  1410. unsigned int discard_granularity;
  1411. unsigned int discard_alignment;
  1412. unsigned int discard_secure;
  1413. type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
  1414. if (IS_ERR(type))
  1415. return;
  1416. info->feature_secdiscard = 0;
  1417. if (strncmp(type, "phy", 3) == 0) {
  1418. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1419. "discard-granularity", "%u", &discard_granularity,
  1420. "discard-alignment", "%u", &discard_alignment,
  1421. NULL);
  1422. if (!err) {
  1423. info->feature_discard = 1;
  1424. info->discard_granularity = discard_granularity;
  1425. info->discard_alignment = discard_alignment;
  1426. }
  1427. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1428. "discard-secure", "%d", &discard_secure,
  1429. NULL);
  1430. if (!err)
  1431. info->feature_secdiscard = discard_secure;
  1432. } else if (strncmp(type, "file", 4) == 0)
  1433. info->feature_discard = 1;
  1434. kfree(type);
  1435. }
  1436. static int blkfront_setup_indirect(struct blkfront_info *info)
  1437. {
  1438. unsigned int indirect_segments, segs;
  1439. int err, i;
  1440. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1441. "feature-max-indirect-segments", "%u", &indirect_segments,
  1442. NULL);
  1443. if (err) {
  1444. info->max_indirect_segments = 0;
  1445. segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  1446. } else {
  1447. info->max_indirect_segments = min(indirect_segments,
  1448. xen_blkif_max_segments);
  1449. segs = info->max_indirect_segments;
  1450. }
  1451. err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE);
  1452. if (err)
  1453. goto out_of_memory;
  1454. if (!info->feature_persistent && info->max_indirect_segments) {
  1455. /*
  1456. * We are using indirect descriptors but not persistent
  1457. * grants, we need to allocate a set of pages that can be
  1458. * used for mapping indirect grefs
  1459. */
  1460. int num = INDIRECT_GREFS(segs) * BLK_RING_SIZE;
  1461. BUG_ON(!list_empty(&info->indirect_pages));
  1462. for (i = 0; i < num; i++) {
  1463. struct page *indirect_page = alloc_page(GFP_NOIO);
  1464. if (!indirect_page)
  1465. goto out_of_memory;
  1466. list_add(&indirect_page->lru, &info->indirect_pages);
  1467. }
  1468. }
  1469. for (i = 0; i < BLK_RING_SIZE; i++) {
  1470. info->shadow[i].grants_used = kzalloc(
  1471. sizeof(info->shadow[i].grants_used[0]) * segs,
  1472. GFP_NOIO);
  1473. info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * segs, GFP_NOIO);
  1474. if (info->max_indirect_segments)
  1475. info->shadow[i].indirect_grants = kzalloc(
  1476. sizeof(info->shadow[i].indirect_grants[0]) *
  1477. INDIRECT_GREFS(segs),
  1478. GFP_NOIO);
  1479. if ((info->shadow[i].grants_used == NULL) ||
  1480. (info->shadow[i].sg == NULL) ||
  1481. (info->max_indirect_segments &&
  1482. (info->shadow[i].indirect_grants == NULL)))
  1483. goto out_of_memory;
  1484. sg_init_table(info->shadow[i].sg, segs);
  1485. }
  1486. return 0;
  1487. out_of_memory:
  1488. for (i = 0; i < BLK_RING_SIZE; i++) {
  1489. kfree(info->shadow[i].grants_used);
  1490. info->shadow[i].grants_used = NULL;
  1491. kfree(info->shadow[i].sg);
  1492. info->shadow[i].sg = NULL;
  1493. kfree(info->shadow[i].indirect_grants);
  1494. info->shadow[i].indirect_grants = NULL;
  1495. }
  1496. if (!list_empty(&info->indirect_pages)) {
  1497. struct page *indirect_page, *n;
  1498. list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
  1499. list_del(&indirect_page->lru);
  1500. __free_page(indirect_page);
  1501. }
  1502. }
  1503. return -ENOMEM;
  1504. }
  1505. /*
  1506. * Invoked when the backend is finally 'ready' (and has told produced
  1507. * the details about the physical device - #sectors, size, etc).
  1508. */
  1509. static void blkfront_connect(struct blkfront_info *info)
  1510. {
  1511. unsigned long long sectors;
  1512. unsigned long sector_size;
  1513. unsigned int physical_sector_size;
  1514. unsigned int binfo;
  1515. int err;
  1516. int barrier, flush, discard, persistent;
  1517. switch (info->connected) {
  1518. case BLKIF_STATE_CONNECTED:
  1519. /*
  1520. * Potentially, the back-end may be signalling
  1521. * a capacity change; update the capacity.
  1522. */
  1523. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1524. "sectors", "%Lu", &sectors);
  1525. if (XENBUS_EXIST_ERR(err))
  1526. return;
  1527. printk(KERN_INFO "Setting capacity to %Lu\n",
  1528. sectors);
  1529. set_capacity(info->gd, sectors);
  1530. revalidate_disk(info->gd);
  1531. return;
  1532. case BLKIF_STATE_SUSPENDED:
  1533. /*
  1534. * If we are recovering from suspension, we need to wait
  1535. * for the backend to announce it's features before
  1536. * reconnecting, at least we need to know if the backend
  1537. * supports indirect descriptors, and how many.
  1538. */
  1539. blkif_recover(info);
  1540. return;
  1541. default:
  1542. break;
  1543. }
  1544. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  1545. __func__, info->xbdev->otherend);
  1546. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1547. "sectors", "%llu", &sectors,
  1548. "info", "%u", &binfo,
  1549. "sector-size", "%lu", &sector_size,
  1550. NULL);
  1551. if (err) {
  1552. xenbus_dev_fatal(info->xbdev, err,
  1553. "reading backend fields at %s",
  1554. info->xbdev->otherend);
  1555. return;
  1556. }
  1557. /*
  1558. * physcial-sector-size is a newer field, so old backends may not
  1559. * provide this. Assume physical sector size to be the same as
  1560. * sector_size in that case.
  1561. */
  1562. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1563. "physical-sector-size", "%u", &physical_sector_size);
  1564. if (err != 1)
  1565. physical_sector_size = sector_size;
  1566. info->feature_flush = 0;
  1567. info->flush_op = 0;
  1568. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1569. "feature-barrier", "%d", &barrier,
  1570. NULL);
  1571. /*
  1572. * If there's no "feature-barrier" defined, then it means
  1573. * we're dealing with a very old backend which writes
  1574. * synchronously; nothing to do.
  1575. *
  1576. * If there are barriers, then we use flush.
  1577. */
  1578. if (!err && barrier) {
  1579. info->feature_flush = REQ_FLUSH | REQ_FUA;
  1580. info->flush_op = BLKIF_OP_WRITE_BARRIER;
  1581. }
  1582. /*
  1583. * And if there is "feature-flush-cache" use that above
  1584. * barriers.
  1585. */
  1586. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1587. "feature-flush-cache", "%d", &flush,
  1588. NULL);
  1589. if (!err && flush) {
  1590. info->feature_flush = REQ_FLUSH;
  1591. info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
  1592. }
  1593. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1594. "feature-discard", "%d", &discard,
  1595. NULL);
  1596. if (!err && discard)
  1597. blkfront_setup_discard(info);
  1598. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1599. "feature-persistent", "%u", &persistent,
  1600. NULL);
  1601. if (err)
  1602. info->feature_persistent = 0;
  1603. else
  1604. info->feature_persistent = persistent;
  1605. err = blkfront_setup_indirect(info);
  1606. if (err) {
  1607. xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
  1608. info->xbdev->otherend);
  1609. return;
  1610. }
  1611. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
  1612. physical_sector_size);
  1613. if (err) {
  1614. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  1615. info->xbdev->otherend);
  1616. return;
  1617. }
  1618. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1619. /* Kick pending requests. */
  1620. spin_lock_irq(&info->io_lock);
  1621. info->connected = BLKIF_STATE_CONNECTED;
  1622. kick_pending_request_queues(info);
  1623. spin_unlock_irq(&info->io_lock);
  1624. add_disk(info->gd);
  1625. info->is_ready = 1;
  1626. }
  1627. /**
  1628. * Callback received when the backend's state changes.
  1629. */
  1630. static void blkback_changed(struct xenbus_device *dev,
  1631. enum xenbus_state backend_state)
  1632. {
  1633. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1634. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  1635. switch (backend_state) {
  1636. case XenbusStateInitialising:
  1637. case XenbusStateInitWait:
  1638. case XenbusStateInitialised:
  1639. case XenbusStateReconfiguring:
  1640. case XenbusStateReconfigured:
  1641. case XenbusStateUnknown:
  1642. case XenbusStateClosed:
  1643. break;
  1644. case XenbusStateConnected:
  1645. blkfront_connect(info);
  1646. break;
  1647. case XenbusStateClosing:
  1648. blkfront_closing(info);
  1649. break;
  1650. }
  1651. }
  1652. static int blkfront_remove(struct xenbus_device *xbdev)
  1653. {
  1654. struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
  1655. struct block_device *bdev = NULL;
  1656. struct gendisk *disk;
  1657. dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
  1658. blkif_free(info, 0);
  1659. mutex_lock(&info->mutex);
  1660. disk = info->gd;
  1661. if (disk)
  1662. bdev = bdget_disk(disk, 0);
  1663. info->xbdev = NULL;
  1664. mutex_unlock(&info->mutex);
  1665. if (!bdev) {
  1666. kfree(info);
  1667. return 0;
  1668. }
  1669. /*
  1670. * The xbdev was removed before we reached the Closed
  1671. * state. See if it's safe to remove the disk. If the bdev
  1672. * isn't closed yet, we let release take care of it.
  1673. */
  1674. mutex_lock(&bdev->bd_mutex);
  1675. info = disk->private_data;
  1676. dev_warn(disk_to_dev(disk),
  1677. "%s was hot-unplugged, %d stale handles\n",
  1678. xbdev->nodename, bdev->bd_openers);
  1679. if (info && !bdev->bd_openers) {
  1680. xlvbd_release_gendisk(info);
  1681. disk->private_data = NULL;
  1682. kfree(info);
  1683. }
  1684. mutex_unlock(&bdev->bd_mutex);
  1685. bdput(bdev);
  1686. return 0;
  1687. }
  1688. static int blkfront_is_ready(struct xenbus_device *dev)
  1689. {
  1690. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1691. return info->is_ready && info->xbdev;
  1692. }
  1693. static int blkif_open(struct block_device *bdev, fmode_t mode)
  1694. {
  1695. struct gendisk *disk = bdev->bd_disk;
  1696. struct blkfront_info *info;
  1697. int err = 0;
  1698. mutex_lock(&blkfront_mutex);
  1699. info = disk->private_data;
  1700. if (!info) {
  1701. /* xbdev gone */
  1702. err = -ERESTARTSYS;
  1703. goto out;
  1704. }
  1705. mutex_lock(&info->mutex);
  1706. if (!info->gd)
  1707. /* xbdev is closed */
  1708. err = -ERESTARTSYS;
  1709. mutex_unlock(&info->mutex);
  1710. out:
  1711. mutex_unlock(&blkfront_mutex);
  1712. return err;
  1713. }
  1714. static void blkif_release(struct gendisk *disk, fmode_t mode)
  1715. {
  1716. struct blkfront_info *info = disk->private_data;
  1717. struct block_device *bdev;
  1718. struct xenbus_device *xbdev;
  1719. mutex_lock(&blkfront_mutex);
  1720. bdev = bdget_disk(disk, 0);
  1721. if (bdev->bd_openers)
  1722. goto out;
  1723. /*
  1724. * Check if we have been instructed to close. We will have
  1725. * deferred this request, because the bdev was still open.
  1726. */
  1727. mutex_lock(&info->mutex);
  1728. xbdev = info->xbdev;
  1729. if (xbdev && xbdev->state == XenbusStateClosing) {
  1730. /* pending switch to state closed */
  1731. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1732. xlvbd_release_gendisk(info);
  1733. xenbus_frontend_closed(info->xbdev);
  1734. }
  1735. mutex_unlock(&info->mutex);
  1736. if (!xbdev) {
  1737. /* sudden device removal */
  1738. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1739. xlvbd_release_gendisk(info);
  1740. disk->private_data = NULL;
  1741. kfree(info);
  1742. }
  1743. out:
  1744. bdput(bdev);
  1745. mutex_unlock(&blkfront_mutex);
  1746. }
  1747. static const struct block_device_operations xlvbd_block_fops =
  1748. {
  1749. .owner = THIS_MODULE,
  1750. .open = blkif_open,
  1751. .release = blkif_release,
  1752. .getgeo = blkif_getgeo,
  1753. .ioctl = blkif_ioctl,
  1754. };
  1755. static const struct xenbus_device_id blkfront_ids[] = {
  1756. { "vbd" },
  1757. { "" }
  1758. };
  1759. static DEFINE_XENBUS_DRIVER(blkfront, ,
  1760. .probe = blkfront_probe,
  1761. .remove = blkfront_remove,
  1762. .resume = blkfront_resume,
  1763. .otherend_changed = blkback_changed,
  1764. .is_ready = blkfront_is_ready,
  1765. );
  1766. static int __init xlblk_init(void)
  1767. {
  1768. int ret;
  1769. if (!xen_domain())
  1770. return -ENODEV;
  1771. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  1772. return -ENODEV;
  1773. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  1774. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  1775. XENVBD_MAJOR, DEV_NAME);
  1776. return -ENODEV;
  1777. }
  1778. ret = xenbus_register_frontend(&blkfront_driver);
  1779. if (ret) {
  1780. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1781. return ret;
  1782. }
  1783. return 0;
  1784. }
  1785. module_init(xlblk_init);
  1786. static void __exit xlblk_exit(void)
  1787. {
  1788. xenbus_unregister_driver(&blkfront_driver);
  1789. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1790. kfree(minors);
  1791. }
  1792. module_exit(xlblk_exit);
  1793. MODULE_DESCRIPTION("Xen virtual block device frontend");
  1794. MODULE_LICENSE("GPL");
  1795. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  1796. MODULE_ALIAS("xen:vbd");
  1797. MODULE_ALIAS("xenblk");