xen-blkfront.c 43 KB

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