xen-blkfront.c 53 KB

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