xen-blkfront.c 43 KB

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