xen-blkfront.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  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 physical_sector_size,
  524. unsigned int segments)
  525. {
  526. struct request_queue *rq;
  527. struct blkfront_info *info = gd->private_data;
  528. rq = blk_init_queue(do_blkif_request, &info->io_lock);
  529. if (rq == NULL)
  530. return -1;
  531. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
  532. if (info->feature_discard) {
  533. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
  534. blk_queue_max_discard_sectors(rq, get_capacity(gd));
  535. rq->limits.discard_granularity = info->discard_granularity;
  536. rq->limits.discard_alignment = info->discard_alignment;
  537. if (info->feature_secdiscard)
  538. queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
  539. }
  540. /* Hard sector size and max sectors impersonate the equiv. hardware. */
  541. blk_queue_logical_block_size(rq, sector_size);
  542. blk_queue_physical_block_size(rq, physical_sector_size);
  543. blk_queue_max_hw_sectors(rq, (segments * PAGE_SIZE) / 512);
  544. /* Each segment in a request is up to an aligned page in size. */
  545. blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
  546. blk_queue_max_segment_size(rq, PAGE_SIZE);
  547. /* Ensure a merged request will fit in a single I/O ring slot. */
  548. blk_queue_max_segments(rq, segments);
  549. /* Make sure buffer addresses are sector-aligned. */
  550. blk_queue_dma_alignment(rq, 511);
  551. /* Make sure we don't use bounce buffers. */
  552. blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
  553. gd->queue = rq;
  554. return 0;
  555. }
  556. static void xlvbd_flush(struct blkfront_info *info)
  557. {
  558. blk_queue_flush(info->rq, info->feature_flush);
  559. printk(KERN_INFO "blkfront: %s: %s: %s %s %s %s %s\n",
  560. info->gd->disk_name,
  561. info->flush_op == BLKIF_OP_WRITE_BARRIER ?
  562. "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
  563. "flush diskcache" : "barrier or flush"),
  564. info->feature_flush ? "enabled;" : "disabled;",
  565. "persistent grants:",
  566. info->feature_persistent ? "enabled;" : "disabled;",
  567. "indirect descriptors:",
  568. info->max_indirect_segments ? "enabled;" : "disabled;");
  569. }
  570. static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
  571. {
  572. int major;
  573. major = BLKIF_MAJOR(vdevice);
  574. *minor = BLKIF_MINOR(vdevice);
  575. switch (major) {
  576. case XEN_IDE0_MAJOR:
  577. *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
  578. *minor = ((*minor / 64) * PARTS_PER_DISK) +
  579. EMULATED_HD_DISK_MINOR_OFFSET;
  580. break;
  581. case XEN_IDE1_MAJOR:
  582. *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
  583. *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
  584. EMULATED_HD_DISK_MINOR_OFFSET;
  585. break;
  586. case XEN_SCSI_DISK0_MAJOR:
  587. *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
  588. *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
  589. break;
  590. case XEN_SCSI_DISK1_MAJOR:
  591. case XEN_SCSI_DISK2_MAJOR:
  592. case XEN_SCSI_DISK3_MAJOR:
  593. case XEN_SCSI_DISK4_MAJOR:
  594. case XEN_SCSI_DISK5_MAJOR:
  595. case XEN_SCSI_DISK6_MAJOR:
  596. case XEN_SCSI_DISK7_MAJOR:
  597. *offset = (*minor / PARTS_PER_DISK) +
  598. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
  599. EMULATED_SD_DISK_NAME_OFFSET;
  600. *minor = *minor +
  601. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
  602. EMULATED_SD_DISK_MINOR_OFFSET;
  603. break;
  604. case XEN_SCSI_DISK8_MAJOR:
  605. case XEN_SCSI_DISK9_MAJOR:
  606. case XEN_SCSI_DISK10_MAJOR:
  607. case XEN_SCSI_DISK11_MAJOR:
  608. case XEN_SCSI_DISK12_MAJOR:
  609. case XEN_SCSI_DISK13_MAJOR:
  610. case XEN_SCSI_DISK14_MAJOR:
  611. case XEN_SCSI_DISK15_MAJOR:
  612. *offset = (*minor / PARTS_PER_DISK) +
  613. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
  614. EMULATED_SD_DISK_NAME_OFFSET;
  615. *minor = *minor +
  616. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
  617. EMULATED_SD_DISK_MINOR_OFFSET;
  618. break;
  619. case XENVBD_MAJOR:
  620. *offset = *minor / PARTS_PER_DISK;
  621. break;
  622. default:
  623. printk(KERN_WARNING "blkfront: your disk configuration is "
  624. "incorrect, please use an xvd device instead\n");
  625. return -ENODEV;
  626. }
  627. return 0;
  628. }
  629. static char *encode_disk_name(char *ptr, unsigned int n)
  630. {
  631. if (n >= 26)
  632. ptr = encode_disk_name(ptr, n / 26 - 1);
  633. *ptr = 'a' + n % 26;
  634. return ptr + 1;
  635. }
  636. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  637. struct blkfront_info *info,
  638. u16 vdisk_info, u16 sector_size,
  639. unsigned int physical_sector_size)
  640. {
  641. struct gendisk *gd;
  642. int nr_minors = 1;
  643. int err;
  644. unsigned int offset;
  645. int minor;
  646. int nr_parts;
  647. char *ptr;
  648. BUG_ON(info->gd != NULL);
  649. BUG_ON(info->rq != NULL);
  650. if ((info->vdevice>>EXT_SHIFT) > 1) {
  651. /* this is above the extended range; something is wrong */
  652. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  653. return -ENODEV;
  654. }
  655. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  656. err = xen_translate_vdev(info->vdevice, &minor, &offset);
  657. if (err)
  658. return err;
  659. nr_parts = PARTS_PER_DISK;
  660. } else {
  661. minor = BLKIF_MINOR_EXT(info->vdevice);
  662. nr_parts = PARTS_PER_EXT_DISK;
  663. offset = minor / nr_parts;
  664. if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
  665. printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
  666. "emulated IDE disks,\n\t choose an xvd device name"
  667. "from xvde on\n", info->vdevice);
  668. }
  669. if (minor >> MINORBITS) {
  670. pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
  671. info->vdevice, minor);
  672. return -ENODEV;
  673. }
  674. if ((minor % nr_parts) == 0)
  675. nr_minors = nr_parts;
  676. err = xlbd_reserve_minors(minor, nr_minors);
  677. if (err)
  678. goto out;
  679. err = -ENODEV;
  680. gd = alloc_disk(nr_minors);
  681. if (gd == NULL)
  682. goto release;
  683. strcpy(gd->disk_name, DEV_NAME);
  684. ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
  685. BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
  686. if (nr_minors > 1)
  687. *ptr = 0;
  688. else
  689. snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
  690. "%d", minor & (nr_parts - 1));
  691. gd->major = XENVBD_MAJOR;
  692. gd->first_minor = minor;
  693. gd->fops = &xlvbd_block_fops;
  694. gd->private_data = info;
  695. gd->driverfs_dev = &(info->xbdev->dev);
  696. set_capacity(gd, capacity);
  697. if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
  698. info->max_indirect_segments ? :
  699. BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  700. del_gendisk(gd);
  701. goto release;
  702. }
  703. info->rq = gd->queue;
  704. info->gd = gd;
  705. xlvbd_flush(info);
  706. if (vdisk_info & VDISK_READONLY)
  707. set_disk_ro(gd, 1);
  708. if (vdisk_info & VDISK_REMOVABLE)
  709. gd->flags |= GENHD_FL_REMOVABLE;
  710. if (vdisk_info & VDISK_CDROM)
  711. gd->flags |= GENHD_FL_CD;
  712. return 0;
  713. release:
  714. xlbd_release_minors(minor, nr_minors);
  715. out:
  716. return err;
  717. }
  718. static void xlvbd_release_gendisk(struct blkfront_info *info)
  719. {
  720. unsigned int minor, nr_minors;
  721. unsigned long flags;
  722. if (info->rq == NULL)
  723. return;
  724. spin_lock_irqsave(&info->io_lock, flags);
  725. /* No more blkif_request(). */
  726. blk_stop_queue(info->rq);
  727. /* No more gnttab callback work. */
  728. gnttab_cancel_free_callback(&info->callback);
  729. spin_unlock_irqrestore(&info->io_lock, flags);
  730. /* Flush gnttab callback work. Must be done with no locks held. */
  731. flush_work(&info->work);
  732. del_gendisk(info->gd);
  733. minor = info->gd->first_minor;
  734. nr_minors = info->gd->minors;
  735. xlbd_release_minors(minor, nr_minors);
  736. blk_cleanup_queue(info->rq);
  737. info->rq = NULL;
  738. put_disk(info->gd);
  739. info->gd = NULL;
  740. }
  741. static void kick_pending_request_queues(struct blkfront_info *info)
  742. {
  743. if (!RING_FULL(&info->ring)) {
  744. /* Re-enable calldowns. */
  745. blk_start_queue(info->rq);
  746. /* Kick things off immediately. */
  747. do_blkif_request(info->rq);
  748. }
  749. }
  750. static void blkif_restart_queue(struct work_struct *work)
  751. {
  752. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  753. spin_lock_irq(&info->io_lock);
  754. if (info->connected == BLKIF_STATE_CONNECTED)
  755. kick_pending_request_queues(info);
  756. spin_unlock_irq(&info->io_lock);
  757. }
  758. static void blkif_free(struct blkfront_info *info, int suspend)
  759. {
  760. struct grant *persistent_gnt;
  761. struct grant *n;
  762. int i, j, segs;
  763. /* Prevent new requests being issued until we fix things up. */
  764. spin_lock_irq(&info->io_lock);
  765. info->connected = suspend ?
  766. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  767. /* No more blkif_request(). */
  768. if (info->rq)
  769. blk_stop_queue(info->rq);
  770. /* Remove all persistent grants */
  771. if (!list_empty(&info->persistent_gnts)) {
  772. list_for_each_entry_safe(persistent_gnt, n,
  773. &info->persistent_gnts, node) {
  774. list_del(&persistent_gnt->node);
  775. if (persistent_gnt->gref != GRANT_INVALID_REF) {
  776. gnttab_end_foreign_access(persistent_gnt->gref,
  777. 0, 0UL);
  778. info->persistent_gnts_c--;
  779. }
  780. __free_page(pfn_to_page(persistent_gnt->pfn));
  781. kfree(persistent_gnt);
  782. }
  783. }
  784. BUG_ON(info->persistent_gnts_c != 0);
  785. for (i = 0; i < BLK_RING_SIZE; i++) {
  786. /*
  787. * Clear persistent grants present in requests already
  788. * on the shared ring
  789. */
  790. if (!info->shadow[i].request)
  791. goto free_shadow;
  792. segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
  793. info->shadow[i].req.u.indirect.nr_segments :
  794. info->shadow[i].req.u.rw.nr_segments;
  795. for (j = 0; j < segs; j++) {
  796. persistent_gnt = info->shadow[i].grants_used[j];
  797. gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
  798. __free_page(pfn_to_page(persistent_gnt->pfn));
  799. kfree(persistent_gnt);
  800. }
  801. if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
  802. /*
  803. * If this is not an indirect operation don't try to
  804. * free indirect segments
  805. */
  806. goto free_shadow;
  807. for (j = 0; j < INDIRECT_GREFS(segs); j++) {
  808. persistent_gnt = info->shadow[i].indirect_grants[j];
  809. gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
  810. __free_page(pfn_to_page(persistent_gnt->pfn));
  811. kfree(persistent_gnt);
  812. }
  813. free_shadow:
  814. kfree(info->shadow[i].grants_used);
  815. info->shadow[i].grants_used = NULL;
  816. kfree(info->shadow[i].indirect_grants);
  817. info->shadow[i].indirect_grants = NULL;
  818. kfree(info->shadow[i].sg);
  819. info->shadow[i].sg = NULL;
  820. }
  821. /* No more gnttab callback work. */
  822. gnttab_cancel_free_callback(&info->callback);
  823. spin_unlock_irq(&info->io_lock);
  824. /* Flush gnttab callback work. Must be done with no locks held. */
  825. flush_work(&info->work);
  826. /* Free resources associated with old device channel. */
  827. if (info->ring_ref != GRANT_INVALID_REF) {
  828. gnttab_end_foreign_access(info->ring_ref, 0,
  829. (unsigned long)info->ring.sring);
  830. info->ring_ref = GRANT_INVALID_REF;
  831. info->ring.sring = NULL;
  832. }
  833. if (info->irq)
  834. unbind_from_irqhandler(info->irq, info);
  835. info->evtchn = info->irq = 0;
  836. }
  837. static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
  838. struct blkif_response *bret)
  839. {
  840. int i = 0;
  841. struct scatterlist *sg;
  842. char *bvec_data;
  843. void *shared_data;
  844. int nseg;
  845. nseg = s->req.operation == BLKIF_OP_INDIRECT ?
  846. s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
  847. if (bret->operation == BLKIF_OP_READ) {
  848. /*
  849. * Copy the data received from the backend into the bvec.
  850. * Since bv_offset can be different than 0, and bv_len different
  851. * than PAGE_SIZE, we have to keep track of the current offset,
  852. * to be sure we are copying the data from the right shared page.
  853. */
  854. for_each_sg(s->sg, sg, nseg, i) {
  855. BUG_ON(sg->offset + sg->length > PAGE_SIZE);
  856. shared_data = kmap_atomic(
  857. pfn_to_page(s->grants_used[i]->pfn));
  858. bvec_data = kmap_atomic(sg_page(sg));
  859. memcpy(bvec_data + sg->offset,
  860. shared_data + sg->offset,
  861. sg->length);
  862. kunmap_atomic(bvec_data);
  863. kunmap_atomic(shared_data);
  864. }
  865. }
  866. /* Add the persistent grant into the list of free grants */
  867. for (i = 0; i < nseg; i++) {
  868. list_add(&s->grants_used[i]->node, &info->persistent_gnts);
  869. info->persistent_gnts_c++;
  870. }
  871. if (s->req.operation == BLKIF_OP_INDIRECT) {
  872. for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
  873. list_add(&s->indirect_grants[i]->node, &info->persistent_gnts);
  874. info->persistent_gnts_c++;
  875. }
  876. }
  877. }
  878. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  879. {
  880. struct request *req;
  881. struct blkif_response *bret;
  882. RING_IDX i, rp;
  883. unsigned long flags;
  884. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  885. int error;
  886. spin_lock_irqsave(&info->io_lock, flags);
  887. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  888. spin_unlock_irqrestore(&info->io_lock, flags);
  889. return IRQ_HANDLED;
  890. }
  891. again:
  892. rp = info->ring.sring->rsp_prod;
  893. rmb(); /* Ensure we see queued responses up to 'rp'. */
  894. for (i = info->ring.rsp_cons; i != rp; i++) {
  895. unsigned long id;
  896. bret = RING_GET_RESPONSE(&info->ring, i);
  897. id = bret->id;
  898. /*
  899. * The backend has messed up and given us an id that we would
  900. * never have given to it (we stamp it up to BLK_RING_SIZE -
  901. * look in get_id_from_freelist.
  902. */
  903. if (id >= BLK_RING_SIZE) {
  904. WARN(1, "%s: response to %s has incorrect id (%ld)\n",
  905. info->gd->disk_name, op_name(bret->operation), id);
  906. /* We can't safely get the 'struct request' as
  907. * the id is busted. */
  908. continue;
  909. }
  910. req = info->shadow[id].request;
  911. if (bret->operation != BLKIF_OP_DISCARD)
  912. blkif_completion(&info->shadow[id], info, bret);
  913. if (add_id_to_freelist(info, id)) {
  914. WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
  915. info->gd->disk_name, op_name(bret->operation), id);
  916. continue;
  917. }
  918. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  919. switch (bret->operation) {
  920. case BLKIF_OP_DISCARD:
  921. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  922. struct request_queue *rq = info->rq;
  923. printk(KERN_WARNING "blkfront: %s: %s op failed\n",
  924. info->gd->disk_name, op_name(bret->operation));
  925. error = -EOPNOTSUPP;
  926. info->feature_discard = 0;
  927. info->feature_secdiscard = 0;
  928. queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
  929. queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
  930. }
  931. __blk_end_request_all(req, error);
  932. break;
  933. case BLKIF_OP_FLUSH_DISKCACHE:
  934. case BLKIF_OP_WRITE_BARRIER:
  935. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  936. printk(KERN_WARNING "blkfront: %s: %s op failed\n",
  937. info->gd->disk_name, op_name(bret->operation));
  938. error = -EOPNOTSUPP;
  939. }
  940. if (unlikely(bret->status == BLKIF_RSP_ERROR &&
  941. info->shadow[id].req.u.rw.nr_segments == 0)) {
  942. printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
  943. info->gd->disk_name, op_name(bret->operation));
  944. error = -EOPNOTSUPP;
  945. }
  946. if (unlikely(error)) {
  947. if (error == -EOPNOTSUPP)
  948. error = 0;
  949. info->feature_flush = 0;
  950. info->flush_op = 0;
  951. xlvbd_flush(info);
  952. }
  953. /* fall through */
  954. case BLKIF_OP_READ:
  955. case BLKIF_OP_WRITE:
  956. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  957. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  958. "request: %x\n", bret->status);
  959. __blk_end_request_all(req, error);
  960. break;
  961. default:
  962. BUG();
  963. }
  964. }
  965. info->ring.rsp_cons = i;
  966. if (i != info->ring.req_prod_pvt) {
  967. int more_to_do;
  968. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  969. if (more_to_do)
  970. goto again;
  971. } else
  972. info->ring.sring->rsp_event = i + 1;
  973. kick_pending_request_queues(info);
  974. spin_unlock_irqrestore(&info->io_lock, flags);
  975. return IRQ_HANDLED;
  976. }
  977. static int setup_blkring(struct xenbus_device *dev,
  978. struct blkfront_info *info)
  979. {
  980. struct blkif_sring *sring;
  981. int err;
  982. info->ring_ref = GRANT_INVALID_REF;
  983. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  984. if (!sring) {
  985. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  986. return -ENOMEM;
  987. }
  988. SHARED_RING_INIT(sring);
  989. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  990. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  991. if (err < 0) {
  992. free_page((unsigned long)sring);
  993. info->ring.sring = NULL;
  994. goto fail;
  995. }
  996. info->ring_ref = err;
  997. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  998. if (err)
  999. goto fail;
  1000. err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
  1001. "blkif", info);
  1002. if (err <= 0) {
  1003. xenbus_dev_fatal(dev, err,
  1004. "bind_evtchn_to_irqhandler failed");
  1005. goto fail;
  1006. }
  1007. info->irq = err;
  1008. return 0;
  1009. fail:
  1010. blkif_free(info, 0);
  1011. return err;
  1012. }
  1013. /* Common code used when first setting up, and when resuming. */
  1014. static int talk_to_blkback(struct xenbus_device *dev,
  1015. struct blkfront_info *info)
  1016. {
  1017. const char *message = NULL;
  1018. struct xenbus_transaction xbt;
  1019. int err;
  1020. /* Create shared ring, alloc event channel. */
  1021. err = setup_blkring(dev, info);
  1022. if (err)
  1023. goto out;
  1024. again:
  1025. err = xenbus_transaction_start(&xbt);
  1026. if (err) {
  1027. xenbus_dev_fatal(dev, err, "starting transaction");
  1028. goto destroy_blkring;
  1029. }
  1030. err = xenbus_printf(xbt, dev->nodename,
  1031. "ring-ref", "%u", info->ring_ref);
  1032. if (err) {
  1033. message = "writing ring-ref";
  1034. goto abort_transaction;
  1035. }
  1036. err = xenbus_printf(xbt, dev->nodename,
  1037. "event-channel", "%u", info->evtchn);
  1038. if (err) {
  1039. message = "writing event-channel";
  1040. goto abort_transaction;
  1041. }
  1042. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  1043. XEN_IO_PROTO_ABI_NATIVE);
  1044. if (err) {
  1045. message = "writing protocol";
  1046. goto abort_transaction;
  1047. }
  1048. err = xenbus_printf(xbt, dev->nodename,
  1049. "feature-persistent", "%u", 1);
  1050. if (err)
  1051. dev_warn(&dev->dev,
  1052. "writing persistent grants feature to xenbus");
  1053. err = xenbus_transaction_end(xbt, 0);
  1054. if (err) {
  1055. if (err == -EAGAIN)
  1056. goto again;
  1057. xenbus_dev_fatal(dev, err, "completing transaction");
  1058. goto destroy_blkring;
  1059. }
  1060. xenbus_switch_state(dev, XenbusStateInitialised);
  1061. return 0;
  1062. abort_transaction:
  1063. xenbus_transaction_end(xbt, 1);
  1064. if (message)
  1065. xenbus_dev_fatal(dev, err, "%s", message);
  1066. destroy_blkring:
  1067. blkif_free(info, 0);
  1068. out:
  1069. return err;
  1070. }
  1071. /**
  1072. * Entry point to this code when a new device is created. Allocate the basic
  1073. * structures and the ring buffer for communication with the backend, and
  1074. * inform the backend of the appropriate details for those. Switch to
  1075. * Initialised state.
  1076. */
  1077. static int blkfront_probe(struct xenbus_device *dev,
  1078. const struct xenbus_device_id *id)
  1079. {
  1080. int err, vdevice, i;
  1081. struct blkfront_info *info;
  1082. /* FIXME: Use dynamic device id if this is not set. */
  1083. err = xenbus_scanf(XBT_NIL, dev->nodename,
  1084. "virtual-device", "%i", &vdevice);
  1085. if (err != 1) {
  1086. /* go looking in the extended area instead */
  1087. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  1088. "%i", &vdevice);
  1089. if (err != 1) {
  1090. xenbus_dev_fatal(dev, err, "reading virtual-device");
  1091. return err;
  1092. }
  1093. }
  1094. if (xen_hvm_domain()) {
  1095. char *type;
  1096. int len;
  1097. /* no unplug has been done: do not hook devices != xen vbds */
  1098. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
  1099. int major;
  1100. if (!VDEV_IS_EXTENDED(vdevice))
  1101. major = BLKIF_MAJOR(vdevice);
  1102. else
  1103. major = XENVBD_MAJOR;
  1104. if (major != XENVBD_MAJOR) {
  1105. printk(KERN_INFO
  1106. "%s: HVM does not support vbd %d as xen block device\n",
  1107. __FUNCTION__, vdevice);
  1108. return -ENODEV;
  1109. }
  1110. }
  1111. /* do not create a PV cdrom device if we are an HVM guest */
  1112. type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
  1113. if (IS_ERR(type))
  1114. return -ENODEV;
  1115. if (strncmp(type, "cdrom", 5) == 0) {
  1116. kfree(type);
  1117. return -ENODEV;
  1118. }
  1119. kfree(type);
  1120. }
  1121. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1122. if (!info) {
  1123. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  1124. return -ENOMEM;
  1125. }
  1126. mutex_init(&info->mutex);
  1127. spin_lock_init(&info->io_lock);
  1128. info->xbdev = dev;
  1129. info->vdevice = vdevice;
  1130. INIT_LIST_HEAD(&info->persistent_gnts);
  1131. info->persistent_gnts_c = 0;
  1132. info->connected = BLKIF_STATE_DISCONNECTED;
  1133. INIT_WORK(&info->work, blkif_restart_queue);
  1134. for (i = 0; i < BLK_RING_SIZE; i++)
  1135. info->shadow[i].req.u.rw.id = i+1;
  1136. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  1137. /* Front end dir is a number, which is used as the id. */
  1138. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  1139. dev_set_drvdata(&dev->dev, info);
  1140. err = talk_to_blkback(dev, info);
  1141. if (err) {
  1142. kfree(info);
  1143. dev_set_drvdata(&dev->dev, NULL);
  1144. return err;
  1145. }
  1146. return 0;
  1147. }
  1148. /*
  1149. * This is a clone of md_trim_bio, used to split a bio into smaller ones
  1150. */
  1151. static void trim_bio(struct bio *bio, int offset, int size)
  1152. {
  1153. /* 'bio' is a cloned bio which we need to trim to match
  1154. * the given offset and size.
  1155. * This requires adjusting bi_sector, bi_size, and bi_io_vec
  1156. */
  1157. int i;
  1158. struct bio_vec *bvec;
  1159. int sofar = 0;
  1160. size <<= 9;
  1161. if (offset == 0 && size == bio->bi_size)
  1162. return;
  1163. bio->bi_sector += offset;
  1164. bio->bi_size = size;
  1165. offset <<= 9;
  1166. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  1167. while (bio->bi_idx < bio->bi_vcnt &&
  1168. bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
  1169. /* remove this whole bio_vec */
  1170. offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
  1171. bio->bi_idx++;
  1172. }
  1173. if (bio->bi_idx < bio->bi_vcnt) {
  1174. bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
  1175. bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
  1176. }
  1177. /* avoid any complications with bi_idx being non-zero*/
  1178. if (bio->bi_idx) {
  1179. memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
  1180. (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
  1181. bio->bi_vcnt -= bio->bi_idx;
  1182. bio->bi_idx = 0;
  1183. }
  1184. /* Make sure vcnt and last bv are not too big */
  1185. bio_for_each_segment(bvec, bio, i) {
  1186. if (sofar + bvec->bv_len > size)
  1187. bvec->bv_len = size - sofar;
  1188. if (bvec->bv_len == 0) {
  1189. bio->bi_vcnt = i;
  1190. break;
  1191. }
  1192. sofar += bvec->bv_len;
  1193. }
  1194. }
  1195. static void split_bio_end(struct bio *bio, int error)
  1196. {
  1197. struct split_bio *split_bio = bio->bi_private;
  1198. if (error)
  1199. split_bio->err = error;
  1200. if (atomic_dec_and_test(&split_bio->pending)) {
  1201. split_bio->bio->bi_phys_segments = 0;
  1202. bio_endio(split_bio->bio, split_bio->err);
  1203. kfree(split_bio);
  1204. }
  1205. bio_put(bio);
  1206. }
  1207. static int blkif_recover(struct blkfront_info *info)
  1208. {
  1209. int i;
  1210. struct request *req, *n;
  1211. struct blk_shadow *copy;
  1212. int rc;
  1213. struct bio *bio, *cloned_bio;
  1214. struct bio_list bio_list, merge_bio;
  1215. unsigned int segs, offset;
  1216. int pending, size;
  1217. struct split_bio *split_bio;
  1218. struct list_head requests;
  1219. /* Stage 1: Make a safe copy of the shadow state. */
  1220. copy = kmemdup(info->shadow, sizeof(info->shadow),
  1221. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  1222. if (!copy)
  1223. return -ENOMEM;
  1224. /* Stage 2: Set up free list. */
  1225. memset(&info->shadow, 0, sizeof(info->shadow));
  1226. for (i = 0; i < BLK_RING_SIZE; i++)
  1227. info->shadow[i].req.u.rw.id = i+1;
  1228. info->shadow_free = info->ring.req_prod_pvt;
  1229. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  1230. rc = blkfront_setup_indirect(info);
  1231. if (rc) {
  1232. kfree(copy);
  1233. return rc;
  1234. }
  1235. segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
  1236. blk_queue_max_segments(info->rq, segs);
  1237. bio_list_init(&bio_list);
  1238. INIT_LIST_HEAD(&requests);
  1239. for (i = 0; i < BLK_RING_SIZE; i++) {
  1240. /* Not in use? */
  1241. if (!copy[i].request)
  1242. continue;
  1243. /*
  1244. * Get the bios in the request so we can re-queue them.
  1245. */
  1246. if (copy[i].request->cmd_flags &
  1247. (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
  1248. /*
  1249. * Flush operations don't contain bios, so
  1250. * we need to requeue the whole request
  1251. */
  1252. list_add(&copy[i].request->queuelist, &requests);
  1253. continue;
  1254. }
  1255. merge_bio.head = copy[i].request->bio;
  1256. merge_bio.tail = copy[i].request->biotail;
  1257. bio_list_merge(&bio_list, &merge_bio);
  1258. copy[i].request->bio = NULL;
  1259. blk_put_request(copy[i].request);
  1260. }
  1261. kfree(copy);
  1262. /*
  1263. * Empty the queue, this is important because we might have
  1264. * requests in the queue with more segments than what we
  1265. * can handle now.
  1266. */
  1267. spin_lock_irq(&info->io_lock);
  1268. while ((req = blk_fetch_request(info->rq)) != NULL) {
  1269. if (req->cmd_flags &
  1270. (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
  1271. list_add(&req->queuelist, &requests);
  1272. continue;
  1273. }
  1274. merge_bio.head = req->bio;
  1275. merge_bio.tail = req->biotail;
  1276. bio_list_merge(&bio_list, &merge_bio);
  1277. req->bio = NULL;
  1278. if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
  1279. pr_alert("diskcache flush request found!\n");
  1280. __blk_put_request(info->rq, req);
  1281. }
  1282. spin_unlock_irq(&info->io_lock);
  1283. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1284. spin_lock_irq(&info->io_lock);
  1285. /* Now safe for us to use the shared ring */
  1286. info->connected = BLKIF_STATE_CONNECTED;
  1287. /* Kick any other new requests queued since we resumed */
  1288. kick_pending_request_queues(info);
  1289. list_for_each_entry_safe(req, n, &requests, queuelist) {
  1290. /* Requeue pending requests (flush or discard) */
  1291. list_del_init(&req->queuelist);
  1292. BUG_ON(req->nr_phys_segments > segs);
  1293. blk_requeue_request(info->rq, req);
  1294. }
  1295. spin_unlock_irq(&info->io_lock);
  1296. while ((bio = bio_list_pop(&bio_list)) != NULL) {
  1297. /* Traverse the list of pending bios and re-queue them */
  1298. if (bio_segments(bio) > segs) {
  1299. /*
  1300. * This bio has more segments than what we can
  1301. * handle, we have to split it.
  1302. */
  1303. pending = (bio_segments(bio) + segs - 1) / segs;
  1304. split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
  1305. BUG_ON(split_bio == NULL);
  1306. atomic_set(&split_bio->pending, pending);
  1307. split_bio->bio = bio;
  1308. for (i = 0; i < pending; i++) {
  1309. offset = (i * segs * PAGE_SIZE) >> 9;
  1310. size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
  1311. (unsigned int)(bio->bi_size >> 9) - offset);
  1312. cloned_bio = bio_clone(bio, GFP_NOIO);
  1313. BUG_ON(cloned_bio == NULL);
  1314. trim_bio(cloned_bio, offset, size);
  1315. cloned_bio->bi_private = split_bio;
  1316. cloned_bio->bi_end_io = split_bio_end;
  1317. submit_bio(cloned_bio->bi_rw, cloned_bio);
  1318. }
  1319. /*
  1320. * Now we have to wait for all those smaller bios to
  1321. * end, so we can also end the "parent" bio.
  1322. */
  1323. continue;
  1324. }
  1325. /* We don't need to split this bio */
  1326. submit_bio(bio->bi_rw, bio);
  1327. }
  1328. return 0;
  1329. }
  1330. /**
  1331. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1332. * driver restart. We tear down our blkif structure and recreate it, but
  1333. * leave the device-layer structures intact so that this is transparent to the
  1334. * rest of the kernel.
  1335. */
  1336. static int blkfront_resume(struct xenbus_device *dev)
  1337. {
  1338. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1339. int err;
  1340. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  1341. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  1342. err = talk_to_blkback(dev, info);
  1343. /*
  1344. * We have to wait for the backend to switch to
  1345. * connected state, since we want to read which
  1346. * features it supports.
  1347. */
  1348. return err;
  1349. }
  1350. static void
  1351. blkfront_closing(struct blkfront_info *info)
  1352. {
  1353. struct xenbus_device *xbdev = info->xbdev;
  1354. struct block_device *bdev = NULL;
  1355. mutex_lock(&info->mutex);
  1356. if (xbdev->state == XenbusStateClosing) {
  1357. mutex_unlock(&info->mutex);
  1358. return;
  1359. }
  1360. if (info->gd)
  1361. bdev = bdget_disk(info->gd, 0);
  1362. mutex_unlock(&info->mutex);
  1363. if (!bdev) {
  1364. xenbus_frontend_closed(xbdev);
  1365. return;
  1366. }
  1367. mutex_lock(&bdev->bd_mutex);
  1368. if (bdev->bd_openers) {
  1369. xenbus_dev_error(xbdev, -EBUSY,
  1370. "Device in use; refusing to close");
  1371. xenbus_switch_state(xbdev, XenbusStateClosing);
  1372. } else {
  1373. xlvbd_release_gendisk(info);
  1374. xenbus_frontend_closed(xbdev);
  1375. }
  1376. mutex_unlock(&bdev->bd_mutex);
  1377. bdput(bdev);
  1378. }
  1379. static void blkfront_setup_discard(struct blkfront_info *info)
  1380. {
  1381. int err;
  1382. char *type;
  1383. unsigned int discard_granularity;
  1384. unsigned int discard_alignment;
  1385. unsigned int discard_secure;
  1386. type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
  1387. if (IS_ERR(type))
  1388. return;
  1389. info->feature_secdiscard = 0;
  1390. if (strncmp(type, "phy", 3) == 0) {
  1391. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1392. "discard-granularity", "%u", &discard_granularity,
  1393. "discard-alignment", "%u", &discard_alignment,
  1394. NULL);
  1395. if (!err) {
  1396. info->feature_discard = 1;
  1397. info->discard_granularity = discard_granularity;
  1398. info->discard_alignment = discard_alignment;
  1399. }
  1400. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1401. "discard-secure", "%d", &discard_secure,
  1402. NULL);
  1403. if (!err)
  1404. info->feature_secdiscard = discard_secure;
  1405. } else if (strncmp(type, "file", 4) == 0)
  1406. info->feature_discard = 1;
  1407. kfree(type);
  1408. }
  1409. static int blkfront_setup_indirect(struct blkfront_info *info)
  1410. {
  1411. unsigned int indirect_segments, segs;
  1412. int err, i;
  1413. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1414. "feature-max-indirect-segments", "%u", &indirect_segments,
  1415. NULL);
  1416. if (err) {
  1417. info->max_indirect_segments = 0;
  1418. segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  1419. } else {
  1420. info->max_indirect_segments = min(indirect_segments,
  1421. xen_blkif_max_segments);
  1422. segs = info->max_indirect_segments;
  1423. }
  1424. err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE);
  1425. if (err)
  1426. goto out_of_memory;
  1427. for (i = 0; i < BLK_RING_SIZE; i++) {
  1428. info->shadow[i].grants_used = kzalloc(
  1429. sizeof(info->shadow[i].grants_used[0]) * segs,
  1430. GFP_NOIO);
  1431. info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * segs, GFP_NOIO);
  1432. if (info->max_indirect_segments)
  1433. info->shadow[i].indirect_grants = kzalloc(
  1434. sizeof(info->shadow[i].indirect_grants[0]) *
  1435. INDIRECT_GREFS(segs),
  1436. GFP_NOIO);
  1437. if ((info->shadow[i].grants_used == NULL) ||
  1438. (info->shadow[i].sg == NULL) ||
  1439. (info->max_indirect_segments &&
  1440. (info->shadow[i].indirect_grants == NULL)))
  1441. goto out_of_memory;
  1442. sg_init_table(info->shadow[i].sg, segs);
  1443. }
  1444. return 0;
  1445. out_of_memory:
  1446. for (i = 0; i < BLK_RING_SIZE; i++) {
  1447. kfree(info->shadow[i].grants_used);
  1448. info->shadow[i].grants_used = NULL;
  1449. kfree(info->shadow[i].sg);
  1450. info->shadow[i].sg = NULL;
  1451. kfree(info->shadow[i].indirect_grants);
  1452. info->shadow[i].indirect_grants = NULL;
  1453. }
  1454. return -ENOMEM;
  1455. }
  1456. /*
  1457. * Invoked when the backend is finally 'ready' (and has told produced
  1458. * the details about the physical device - #sectors, size, etc).
  1459. */
  1460. static void blkfront_connect(struct blkfront_info *info)
  1461. {
  1462. unsigned long long sectors;
  1463. unsigned long sector_size;
  1464. unsigned int physical_sector_size;
  1465. unsigned int binfo;
  1466. int err;
  1467. int barrier, flush, discard, persistent;
  1468. switch (info->connected) {
  1469. case BLKIF_STATE_CONNECTED:
  1470. /*
  1471. * Potentially, the back-end may be signalling
  1472. * a capacity change; update the capacity.
  1473. */
  1474. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1475. "sectors", "%Lu", &sectors);
  1476. if (XENBUS_EXIST_ERR(err))
  1477. return;
  1478. printk(KERN_INFO "Setting capacity to %Lu\n",
  1479. sectors);
  1480. set_capacity(info->gd, sectors);
  1481. revalidate_disk(info->gd);
  1482. return;
  1483. case BLKIF_STATE_SUSPENDED:
  1484. /*
  1485. * If we are recovering from suspension, we need to wait
  1486. * for the backend to announce it's features before
  1487. * reconnecting, at least we need to know if the backend
  1488. * supports indirect descriptors, and how many.
  1489. */
  1490. blkif_recover(info);
  1491. return;
  1492. default:
  1493. break;
  1494. }
  1495. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  1496. __func__, info->xbdev->otherend);
  1497. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1498. "sectors", "%llu", &sectors,
  1499. "info", "%u", &binfo,
  1500. "sector-size", "%lu", &sector_size,
  1501. NULL);
  1502. if (err) {
  1503. xenbus_dev_fatal(info->xbdev, err,
  1504. "reading backend fields at %s",
  1505. info->xbdev->otherend);
  1506. return;
  1507. }
  1508. /*
  1509. * physcial-sector-size is a newer field, so old backends may not
  1510. * provide this. Assume physical sector size to be the same as
  1511. * sector_size in that case.
  1512. */
  1513. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1514. "physical-sector-size", "%u", &physical_sector_size);
  1515. if (err != 1)
  1516. physical_sector_size = sector_size;
  1517. info->feature_flush = 0;
  1518. info->flush_op = 0;
  1519. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1520. "feature-barrier", "%d", &barrier,
  1521. NULL);
  1522. /*
  1523. * If there's no "feature-barrier" defined, then it means
  1524. * we're dealing with a very old backend which writes
  1525. * synchronously; nothing to do.
  1526. *
  1527. * If there are barriers, then we use flush.
  1528. */
  1529. if (!err && barrier) {
  1530. info->feature_flush = REQ_FLUSH | REQ_FUA;
  1531. info->flush_op = BLKIF_OP_WRITE_BARRIER;
  1532. }
  1533. /*
  1534. * And if there is "feature-flush-cache" use that above
  1535. * barriers.
  1536. */
  1537. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1538. "feature-flush-cache", "%d", &flush,
  1539. NULL);
  1540. if (!err && flush) {
  1541. info->feature_flush = REQ_FLUSH;
  1542. info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
  1543. }
  1544. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1545. "feature-discard", "%d", &discard,
  1546. NULL);
  1547. if (!err && discard)
  1548. blkfront_setup_discard(info);
  1549. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1550. "feature-persistent", "%u", &persistent,
  1551. NULL);
  1552. if (err)
  1553. info->feature_persistent = 0;
  1554. else
  1555. info->feature_persistent = persistent;
  1556. err = blkfront_setup_indirect(info);
  1557. if (err) {
  1558. xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
  1559. info->xbdev->otherend);
  1560. return;
  1561. }
  1562. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
  1563. physical_sector_size);
  1564. if (err) {
  1565. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  1566. info->xbdev->otherend);
  1567. return;
  1568. }
  1569. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1570. /* Kick pending requests. */
  1571. spin_lock_irq(&info->io_lock);
  1572. info->connected = BLKIF_STATE_CONNECTED;
  1573. kick_pending_request_queues(info);
  1574. spin_unlock_irq(&info->io_lock);
  1575. add_disk(info->gd);
  1576. info->is_ready = 1;
  1577. }
  1578. /**
  1579. * Callback received when the backend's state changes.
  1580. */
  1581. static void blkback_changed(struct xenbus_device *dev,
  1582. enum xenbus_state backend_state)
  1583. {
  1584. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1585. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  1586. switch (backend_state) {
  1587. case XenbusStateInitialising:
  1588. case XenbusStateInitWait:
  1589. case XenbusStateInitialised:
  1590. case XenbusStateReconfiguring:
  1591. case XenbusStateReconfigured:
  1592. case XenbusStateUnknown:
  1593. case XenbusStateClosed:
  1594. break;
  1595. case XenbusStateConnected:
  1596. blkfront_connect(info);
  1597. break;
  1598. case XenbusStateClosing:
  1599. blkfront_closing(info);
  1600. break;
  1601. }
  1602. }
  1603. static int blkfront_remove(struct xenbus_device *xbdev)
  1604. {
  1605. struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
  1606. struct block_device *bdev = NULL;
  1607. struct gendisk *disk;
  1608. dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
  1609. blkif_free(info, 0);
  1610. mutex_lock(&info->mutex);
  1611. disk = info->gd;
  1612. if (disk)
  1613. bdev = bdget_disk(disk, 0);
  1614. info->xbdev = NULL;
  1615. mutex_unlock(&info->mutex);
  1616. if (!bdev) {
  1617. kfree(info);
  1618. return 0;
  1619. }
  1620. /*
  1621. * The xbdev was removed before we reached the Closed
  1622. * state. See if it's safe to remove the disk. If the bdev
  1623. * isn't closed yet, we let release take care of it.
  1624. */
  1625. mutex_lock(&bdev->bd_mutex);
  1626. info = disk->private_data;
  1627. dev_warn(disk_to_dev(disk),
  1628. "%s was hot-unplugged, %d stale handles\n",
  1629. xbdev->nodename, bdev->bd_openers);
  1630. if (info && !bdev->bd_openers) {
  1631. xlvbd_release_gendisk(info);
  1632. disk->private_data = NULL;
  1633. kfree(info);
  1634. }
  1635. mutex_unlock(&bdev->bd_mutex);
  1636. bdput(bdev);
  1637. return 0;
  1638. }
  1639. static int blkfront_is_ready(struct xenbus_device *dev)
  1640. {
  1641. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1642. return info->is_ready && info->xbdev;
  1643. }
  1644. static int blkif_open(struct block_device *bdev, fmode_t mode)
  1645. {
  1646. struct gendisk *disk = bdev->bd_disk;
  1647. struct blkfront_info *info;
  1648. int err = 0;
  1649. mutex_lock(&blkfront_mutex);
  1650. info = disk->private_data;
  1651. if (!info) {
  1652. /* xbdev gone */
  1653. err = -ERESTARTSYS;
  1654. goto out;
  1655. }
  1656. mutex_lock(&info->mutex);
  1657. if (!info->gd)
  1658. /* xbdev is closed */
  1659. err = -ERESTARTSYS;
  1660. mutex_unlock(&info->mutex);
  1661. out:
  1662. mutex_unlock(&blkfront_mutex);
  1663. return err;
  1664. }
  1665. static int blkif_release(struct gendisk *disk, fmode_t mode)
  1666. {
  1667. struct blkfront_info *info = disk->private_data;
  1668. struct block_device *bdev;
  1669. struct xenbus_device *xbdev;
  1670. mutex_lock(&blkfront_mutex);
  1671. bdev = bdget_disk(disk, 0);
  1672. if (bdev->bd_openers)
  1673. goto out;
  1674. /*
  1675. * Check if we have been instructed to close. We will have
  1676. * deferred this request, because the bdev was still open.
  1677. */
  1678. mutex_lock(&info->mutex);
  1679. xbdev = info->xbdev;
  1680. if (xbdev && xbdev->state == XenbusStateClosing) {
  1681. /* pending switch to state closed */
  1682. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1683. xlvbd_release_gendisk(info);
  1684. xenbus_frontend_closed(info->xbdev);
  1685. }
  1686. mutex_unlock(&info->mutex);
  1687. if (!xbdev) {
  1688. /* sudden device removal */
  1689. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1690. xlvbd_release_gendisk(info);
  1691. disk->private_data = NULL;
  1692. kfree(info);
  1693. }
  1694. out:
  1695. bdput(bdev);
  1696. mutex_unlock(&blkfront_mutex);
  1697. return 0;
  1698. }
  1699. static const struct block_device_operations xlvbd_block_fops =
  1700. {
  1701. .owner = THIS_MODULE,
  1702. .open = blkif_open,
  1703. .release = blkif_release,
  1704. .getgeo = blkif_getgeo,
  1705. .ioctl = blkif_ioctl,
  1706. };
  1707. static const struct xenbus_device_id blkfront_ids[] = {
  1708. { "vbd" },
  1709. { "" }
  1710. };
  1711. static DEFINE_XENBUS_DRIVER(blkfront, ,
  1712. .probe = blkfront_probe,
  1713. .remove = blkfront_remove,
  1714. .resume = blkfront_resume,
  1715. .otherend_changed = blkback_changed,
  1716. .is_ready = blkfront_is_ready,
  1717. );
  1718. static int __init xlblk_init(void)
  1719. {
  1720. int ret;
  1721. if (!xen_domain())
  1722. return -ENODEV;
  1723. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  1724. return -ENODEV;
  1725. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  1726. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  1727. XENVBD_MAJOR, DEV_NAME);
  1728. return -ENODEV;
  1729. }
  1730. ret = xenbus_register_frontend(&blkfront_driver);
  1731. if (ret) {
  1732. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1733. return ret;
  1734. }
  1735. return 0;
  1736. }
  1737. module_init(xlblk_init);
  1738. static void __exit xlblk_exit(void)
  1739. {
  1740. xenbus_unregister_driver(&blkfront_driver);
  1741. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1742. kfree(minors);
  1743. }
  1744. module_exit(xlblk_exit);
  1745. MODULE_DESCRIPTION("Xen virtual block device frontend");
  1746. MODULE_LICENSE("GPL");
  1747. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  1748. MODULE_ALIAS("xen:vbd");
  1749. MODULE_ALIAS("xenblk");