xen-blkfront.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  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;
  669. /* Prevent new requests being issued until we fix things up. */
  670. spin_lock_irq(&info->io_lock);
  671. info->connected = suspend ?
  672. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  673. /* No more blkif_request(). */
  674. if (info->rq)
  675. blk_stop_queue(info->rq);
  676. /* Remove all persistent grants */
  677. if (info->persistent_gnts_c) {
  678. all_gnts = llist_del_all(&info->persistent_gnts);
  679. llist_for_each_entry(persistent_gnt, all_gnts, node) {
  680. gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
  681. __free_page(pfn_to_page(persistent_gnt->pfn));
  682. kfree(persistent_gnt);
  683. }
  684. info->persistent_gnts_c = 0;
  685. }
  686. /* No more gnttab callback work. */
  687. gnttab_cancel_free_callback(&info->callback);
  688. spin_unlock_irq(&info->io_lock);
  689. /* Flush gnttab callback work. Must be done with no locks held. */
  690. flush_work(&info->work);
  691. /* Free resources associated with old device channel. */
  692. if (info->ring_ref != GRANT_INVALID_REF) {
  693. gnttab_end_foreign_access(info->ring_ref, 0,
  694. (unsigned long)info->ring.sring);
  695. info->ring_ref = GRANT_INVALID_REF;
  696. info->ring.sring = NULL;
  697. }
  698. if (info->irq)
  699. unbind_from_irqhandler(info->irq, info);
  700. info->evtchn = info->irq = 0;
  701. }
  702. static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
  703. struct blkif_response *bret)
  704. {
  705. int i;
  706. struct bio_vec *bvec;
  707. struct req_iterator iter;
  708. unsigned long flags;
  709. char *bvec_data;
  710. void *shared_data;
  711. unsigned int offset = 0;
  712. if (bret->operation == BLKIF_OP_READ) {
  713. /*
  714. * Copy the data received from the backend into the bvec.
  715. * Since bv_offset can be different than 0, and bv_len different
  716. * than PAGE_SIZE, we have to keep track of the current offset,
  717. * to be sure we are copying the data from the right shared page.
  718. */
  719. rq_for_each_segment(bvec, s->request, iter) {
  720. BUG_ON((bvec->bv_offset + bvec->bv_len) > PAGE_SIZE);
  721. i = offset >> PAGE_SHIFT;
  722. BUG_ON(i >= s->req.u.rw.nr_segments);
  723. shared_data = kmap_atomic(
  724. pfn_to_page(s->grants_used[i]->pfn));
  725. bvec_data = bvec_kmap_irq(bvec, &flags);
  726. memcpy(bvec_data, shared_data + bvec->bv_offset,
  727. bvec->bv_len);
  728. bvec_kunmap_irq(bvec_data, &flags);
  729. kunmap_atomic(shared_data);
  730. offset += bvec->bv_len;
  731. }
  732. }
  733. /* Add the persistent grant into the list of free grants */
  734. for (i = 0; i < s->req.u.rw.nr_segments; i++) {
  735. llist_add(&s->grants_used[i]->node, &info->persistent_gnts);
  736. info->persistent_gnts_c++;
  737. }
  738. }
  739. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  740. {
  741. struct request *req;
  742. struct blkif_response *bret;
  743. RING_IDX i, rp;
  744. unsigned long flags;
  745. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  746. int error;
  747. spin_lock_irqsave(&info->io_lock, flags);
  748. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  749. spin_unlock_irqrestore(&info->io_lock, flags);
  750. return IRQ_HANDLED;
  751. }
  752. again:
  753. rp = info->ring.sring->rsp_prod;
  754. rmb(); /* Ensure we see queued responses up to 'rp'. */
  755. for (i = info->ring.rsp_cons; i != rp; i++) {
  756. unsigned long id;
  757. bret = RING_GET_RESPONSE(&info->ring, i);
  758. id = bret->id;
  759. /*
  760. * The backend has messed up and given us an id that we would
  761. * never have given to it (we stamp it up to BLK_RING_SIZE -
  762. * look in get_id_from_freelist.
  763. */
  764. if (id >= BLK_RING_SIZE) {
  765. WARN(1, "%s: response to %s has incorrect id (%ld)\n",
  766. info->gd->disk_name, op_name(bret->operation), id);
  767. /* We can't safely get the 'struct request' as
  768. * the id is busted. */
  769. continue;
  770. }
  771. req = info->shadow[id].request;
  772. if (bret->operation != BLKIF_OP_DISCARD)
  773. blkif_completion(&info->shadow[id], info, bret);
  774. if (add_id_to_freelist(info, id)) {
  775. WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
  776. info->gd->disk_name, op_name(bret->operation), id);
  777. continue;
  778. }
  779. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  780. switch (bret->operation) {
  781. case BLKIF_OP_DISCARD:
  782. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  783. struct request_queue *rq = info->rq;
  784. printk(KERN_WARNING "blkfront: %s: %s op failed\n",
  785. info->gd->disk_name, op_name(bret->operation));
  786. error = -EOPNOTSUPP;
  787. info->feature_discard = 0;
  788. info->feature_secdiscard = 0;
  789. queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
  790. queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
  791. }
  792. __blk_end_request_all(req, error);
  793. break;
  794. case BLKIF_OP_FLUSH_DISKCACHE:
  795. case BLKIF_OP_WRITE_BARRIER:
  796. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  797. printk(KERN_WARNING "blkfront: %s: %s op failed\n",
  798. info->gd->disk_name, op_name(bret->operation));
  799. error = -EOPNOTSUPP;
  800. }
  801. if (unlikely(bret->status == BLKIF_RSP_ERROR &&
  802. info->shadow[id].req.u.rw.nr_segments == 0)) {
  803. printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
  804. info->gd->disk_name, op_name(bret->operation));
  805. error = -EOPNOTSUPP;
  806. }
  807. if (unlikely(error)) {
  808. if (error == -EOPNOTSUPP)
  809. error = 0;
  810. info->feature_flush = 0;
  811. info->flush_op = 0;
  812. xlvbd_flush(info);
  813. }
  814. /* fall through */
  815. case BLKIF_OP_READ:
  816. case BLKIF_OP_WRITE:
  817. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  818. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  819. "request: %x\n", bret->status);
  820. __blk_end_request_all(req, error);
  821. break;
  822. default:
  823. BUG();
  824. }
  825. }
  826. info->ring.rsp_cons = i;
  827. if (i != info->ring.req_prod_pvt) {
  828. int more_to_do;
  829. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  830. if (more_to_do)
  831. goto again;
  832. } else
  833. info->ring.sring->rsp_event = i + 1;
  834. kick_pending_request_queues(info);
  835. spin_unlock_irqrestore(&info->io_lock, flags);
  836. return IRQ_HANDLED;
  837. }
  838. static int setup_blkring(struct xenbus_device *dev,
  839. struct blkfront_info *info)
  840. {
  841. struct blkif_sring *sring;
  842. int err;
  843. info->ring_ref = GRANT_INVALID_REF;
  844. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  845. if (!sring) {
  846. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  847. return -ENOMEM;
  848. }
  849. SHARED_RING_INIT(sring);
  850. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  851. sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  852. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  853. if (err < 0) {
  854. free_page((unsigned long)sring);
  855. info->ring.sring = NULL;
  856. goto fail;
  857. }
  858. info->ring_ref = err;
  859. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  860. if (err)
  861. goto fail;
  862. err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
  863. "blkif", info);
  864. if (err <= 0) {
  865. xenbus_dev_fatal(dev, err,
  866. "bind_evtchn_to_irqhandler failed");
  867. goto fail;
  868. }
  869. info->irq = err;
  870. return 0;
  871. fail:
  872. blkif_free(info, 0);
  873. return err;
  874. }
  875. /* Common code used when first setting up, and when resuming. */
  876. static int talk_to_blkback(struct xenbus_device *dev,
  877. struct blkfront_info *info)
  878. {
  879. const char *message = NULL;
  880. struct xenbus_transaction xbt;
  881. int err;
  882. /* Create shared ring, alloc event channel. */
  883. err = setup_blkring(dev, info);
  884. if (err)
  885. goto out;
  886. again:
  887. err = xenbus_transaction_start(&xbt);
  888. if (err) {
  889. xenbus_dev_fatal(dev, err, "starting transaction");
  890. goto destroy_blkring;
  891. }
  892. err = xenbus_printf(xbt, dev->nodename,
  893. "ring-ref", "%u", info->ring_ref);
  894. if (err) {
  895. message = "writing ring-ref";
  896. goto abort_transaction;
  897. }
  898. err = xenbus_printf(xbt, dev->nodename,
  899. "event-channel", "%u", info->evtchn);
  900. if (err) {
  901. message = "writing event-channel";
  902. goto abort_transaction;
  903. }
  904. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  905. XEN_IO_PROTO_ABI_NATIVE);
  906. if (err) {
  907. message = "writing protocol";
  908. goto abort_transaction;
  909. }
  910. err = xenbus_printf(xbt, dev->nodename,
  911. "feature-persistent", "%u", 1);
  912. if (err)
  913. dev_warn(&dev->dev,
  914. "writing persistent grants feature to xenbus");
  915. err = xenbus_transaction_end(xbt, 0);
  916. if (err) {
  917. if (err == -EAGAIN)
  918. goto again;
  919. xenbus_dev_fatal(dev, err, "completing transaction");
  920. goto destroy_blkring;
  921. }
  922. xenbus_switch_state(dev, XenbusStateInitialised);
  923. return 0;
  924. abort_transaction:
  925. xenbus_transaction_end(xbt, 1);
  926. if (message)
  927. xenbus_dev_fatal(dev, err, "%s", message);
  928. destroy_blkring:
  929. blkif_free(info, 0);
  930. out:
  931. return err;
  932. }
  933. /**
  934. * Entry point to this code when a new device is created. Allocate the basic
  935. * structures and the ring buffer for communication with the backend, and
  936. * inform the backend of the appropriate details for those. Switch to
  937. * Initialised state.
  938. */
  939. static int blkfront_probe(struct xenbus_device *dev,
  940. const struct xenbus_device_id *id)
  941. {
  942. int err, vdevice, i;
  943. struct blkfront_info *info;
  944. /* FIXME: Use dynamic device id if this is not set. */
  945. err = xenbus_scanf(XBT_NIL, dev->nodename,
  946. "virtual-device", "%i", &vdevice);
  947. if (err != 1) {
  948. /* go looking in the extended area instead */
  949. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  950. "%i", &vdevice);
  951. if (err != 1) {
  952. xenbus_dev_fatal(dev, err, "reading virtual-device");
  953. return err;
  954. }
  955. }
  956. if (xen_hvm_domain()) {
  957. char *type;
  958. int len;
  959. /* no unplug has been done: do not hook devices != xen vbds */
  960. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
  961. int major;
  962. if (!VDEV_IS_EXTENDED(vdevice))
  963. major = BLKIF_MAJOR(vdevice);
  964. else
  965. major = XENVBD_MAJOR;
  966. if (major != XENVBD_MAJOR) {
  967. printk(KERN_INFO
  968. "%s: HVM does not support vbd %d as xen block device\n",
  969. __FUNCTION__, vdevice);
  970. return -ENODEV;
  971. }
  972. }
  973. /* do not create a PV cdrom device if we are an HVM guest */
  974. type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
  975. if (IS_ERR(type))
  976. return -ENODEV;
  977. if (strncmp(type, "cdrom", 5) == 0) {
  978. kfree(type);
  979. return -ENODEV;
  980. }
  981. kfree(type);
  982. }
  983. info = kzalloc(sizeof(*info), GFP_KERNEL);
  984. if (!info) {
  985. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  986. return -ENOMEM;
  987. }
  988. mutex_init(&info->mutex);
  989. spin_lock_init(&info->io_lock);
  990. info->xbdev = dev;
  991. info->vdevice = vdevice;
  992. init_llist_head(&info->persistent_gnts);
  993. info->persistent_gnts_c = 0;
  994. info->connected = BLKIF_STATE_DISCONNECTED;
  995. INIT_WORK(&info->work, blkif_restart_queue);
  996. for (i = 0; i < BLK_RING_SIZE; i++)
  997. info->shadow[i].req.u.rw.id = i+1;
  998. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  999. /* Front end dir is a number, which is used as the id. */
  1000. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  1001. dev_set_drvdata(&dev->dev, info);
  1002. err = talk_to_blkback(dev, info);
  1003. if (err) {
  1004. kfree(info);
  1005. dev_set_drvdata(&dev->dev, NULL);
  1006. return err;
  1007. }
  1008. return 0;
  1009. }
  1010. static int blkif_recover(struct blkfront_info *info)
  1011. {
  1012. int i;
  1013. struct blkif_request *req;
  1014. struct blk_shadow *copy;
  1015. int j;
  1016. /* Stage 1: Make a safe copy of the shadow state. */
  1017. copy = kmalloc(sizeof(info->shadow),
  1018. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  1019. if (!copy)
  1020. return -ENOMEM;
  1021. memcpy(copy, info->shadow, sizeof(info->shadow));
  1022. /* Stage 2: Set up free list. */
  1023. memset(&info->shadow, 0, sizeof(info->shadow));
  1024. for (i = 0; i < BLK_RING_SIZE; i++)
  1025. info->shadow[i].req.u.rw.id = i+1;
  1026. info->shadow_free = info->ring.req_prod_pvt;
  1027. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  1028. /* Stage 3: Find pending requests and requeue them. */
  1029. for (i = 0; i < BLK_RING_SIZE; i++) {
  1030. /* Not in use? */
  1031. if (!copy[i].request)
  1032. continue;
  1033. /* Grab a request slot and copy shadow state into it. */
  1034. req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  1035. *req = copy[i].req;
  1036. /* We get a new request id, and must reset the shadow state. */
  1037. req->u.rw.id = get_id_from_freelist(info);
  1038. memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
  1039. if (req->operation != BLKIF_OP_DISCARD) {
  1040. /* Rewrite any grant references invalidated by susp/resume. */
  1041. for (j = 0; j < req->u.rw.nr_segments; j++)
  1042. gnttab_grant_foreign_access_ref(
  1043. req->u.rw.seg[j].gref,
  1044. info->xbdev->otherend_id,
  1045. pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]),
  1046. 0);
  1047. }
  1048. info->shadow[req->u.rw.id].req = *req;
  1049. info->ring.req_prod_pvt++;
  1050. }
  1051. kfree(copy);
  1052. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1053. spin_lock_irq(&info->io_lock);
  1054. /* Now safe for us to use the shared ring */
  1055. info->connected = BLKIF_STATE_CONNECTED;
  1056. /* Send off requeued requests */
  1057. flush_requests(info);
  1058. /* Kick any other new requests queued since we resumed */
  1059. kick_pending_request_queues(info);
  1060. spin_unlock_irq(&info->io_lock);
  1061. return 0;
  1062. }
  1063. /**
  1064. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1065. * driver restart. We tear down our blkif structure and recreate it, but
  1066. * leave the device-layer structures intact so that this is transparent to the
  1067. * rest of the kernel.
  1068. */
  1069. static int blkfront_resume(struct xenbus_device *dev)
  1070. {
  1071. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1072. int err;
  1073. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  1074. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  1075. err = talk_to_blkback(dev, info);
  1076. if (info->connected == BLKIF_STATE_SUSPENDED && !err)
  1077. err = blkif_recover(info);
  1078. return err;
  1079. }
  1080. static void
  1081. blkfront_closing(struct blkfront_info *info)
  1082. {
  1083. struct xenbus_device *xbdev = info->xbdev;
  1084. struct block_device *bdev = NULL;
  1085. mutex_lock(&info->mutex);
  1086. if (xbdev->state == XenbusStateClosing) {
  1087. mutex_unlock(&info->mutex);
  1088. return;
  1089. }
  1090. if (info->gd)
  1091. bdev = bdget_disk(info->gd, 0);
  1092. mutex_unlock(&info->mutex);
  1093. if (!bdev) {
  1094. xenbus_frontend_closed(xbdev);
  1095. return;
  1096. }
  1097. mutex_lock(&bdev->bd_mutex);
  1098. if (bdev->bd_openers) {
  1099. xenbus_dev_error(xbdev, -EBUSY,
  1100. "Device in use; refusing to close");
  1101. xenbus_switch_state(xbdev, XenbusStateClosing);
  1102. } else {
  1103. xlvbd_release_gendisk(info);
  1104. xenbus_frontend_closed(xbdev);
  1105. }
  1106. mutex_unlock(&bdev->bd_mutex);
  1107. bdput(bdev);
  1108. }
  1109. static void blkfront_setup_discard(struct blkfront_info *info)
  1110. {
  1111. int err;
  1112. char *type;
  1113. unsigned int discard_granularity;
  1114. unsigned int discard_alignment;
  1115. unsigned int discard_secure;
  1116. type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
  1117. if (IS_ERR(type))
  1118. return;
  1119. info->feature_secdiscard = 0;
  1120. if (strncmp(type, "phy", 3) == 0) {
  1121. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1122. "discard-granularity", "%u", &discard_granularity,
  1123. "discard-alignment", "%u", &discard_alignment,
  1124. NULL);
  1125. if (!err) {
  1126. info->feature_discard = 1;
  1127. info->discard_granularity = discard_granularity;
  1128. info->discard_alignment = discard_alignment;
  1129. }
  1130. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1131. "discard-secure", "%d", &discard_secure,
  1132. NULL);
  1133. if (!err)
  1134. info->feature_secdiscard = discard_secure;
  1135. } else if (strncmp(type, "file", 4) == 0)
  1136. info->feature_discard = 1;
  1137. kfree(type);
  1138. }
  1139. /*
  1140. * Invoked when the backend is finally 'ready' (and has told produced
  1141. * the details about the physical device - #sectors, size, etc).
  1142. */
  1143. static void blkfront_connect(struct blkfront_info *info)
  1144. {
  1145. unsigned long long sectors;
  1146. unsigned long sector_size;
  1147. unsigned int binfo;
  1148. int err;
  1149. int barrier, flush, discard, persistent;
  1150. switch (info->connected) {
  1151. case BLKIF_STATE_CONNECTED:
  1152. /*
  1153. * Potentially, the back-end may be signalling
  1154. * a capacity change; update the capacity.
  1155. */
  1156. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1157. "sectors", "%Lu", &sectors);
  1158. if (XENBUS_EXIST_ERR(err))
  1159. return;
  1160. printk(KERN_INFO "Setting capacity to %Lu\n",
  1161. sectors);
  1162. set_capacity(info->gd, sectors);
  1163. revalidate_disk(info->gd);
  1164. /* fall through */
  1165. case BLKIF_STATE_SUSPENDED:
  1166. return;
  1167. default:
  1168. break;
  1169. }
  1170. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  1171. __func__, info->xbdev->otherend);
  1172. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1173. "sectors", "%llu", &sectors,
  1174. "info", "%u", &binfo,
  1175. "sector-size", "%lu", &sector_size,
  1176. NULL);
  1177. if (err) {
  1178. xenbus_dev_fatal(info->xbdev, err,
  1179. "reading backend fields at %s",
  1180. info->xbdev->otherend);
  1181. return;
  1182. }
  1183. info->feature_flush = 0;
  1184. info->flush_op = 0;
  1185. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1186. "feature-barrier", "%d", &barrier,
  1187. NULL);
  1188. /*
  1189. * If there's no "feature-barrier" defined, then it means
  1190. * we're dealing with a very old backend which writes
  1191. * synchronously; nothing to do.
  1192. *
  1193. * If there are barriers, then we use flush.
  1194. */
  1195. if (!err && barrier) {
  1196. info->feature_flush = REQ_FLUSH | REQ_FUA;
  1197. info->flush_op = BLKIF_OP_WRITE_BARRIER;
  1198. }
  1199. /*
  1200. * And if there is "feature-flush-cache" use that above
  1201. * barriers.
  1202. */
  1203. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1204. "feature-flush-cache", "%d", &flush,
  1205. NULL);
  1206. if (!err && flush) {
  1207. info->feature_flush = REQ_FLUSH;
  1208. info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
  1209. }
  1210. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1211. "feature-discard", "%d", &discard,
  1212. NULL);
  1213. if (!err && discard)
  1214. blkfront_setup_discard(info);
  1215. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1216. "feature-persistent", "%u", &persistent,
  1217. NULL);
  1218. if (err)
  1219. info->feature_persistent = 0;
  1220. else
  1221. info->feature_persistent = persistent;
  1222. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
  1223. if (err) {
  1224. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  1225. info->xbdev->otherend);
  1226. return;
  1227. }
  1228. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1229. /* Kick pending requests. */
  1230. spin_lock_irq(&info->io_lock);
  1231. info->connected = BLKIF_STATE_CONNECTED;
  1232. kick_pending_request_queues(info);
  1233. spin_unlock_irq(&info->io_lock);
  1234. add_disk(info->gd);
  1235. info->is_ready = 1;
  1236. }
  1237. /**
  1238. * Callback received when the backend's state changes.
  1239. */
  1240. static void blkback_changed(struct xenbus_device *dev,
  1241. enum xenbus_state backend_state)
  1242. {
  1243. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1244. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  1245. switch (backend_state) {
  1246. case XenbusStateInitialising:
  1247. case XenbusStateInitWait:
  1248. case XenbusStateInitialised:
  1249. case XenbusStateReconfiguring:
  1250. case XenbusStateReconfigured:
  1251. case XenbusStateUnknown:
  1252. case XenbusStateClosed:
  1253. break;
  1254. case XenbusStateConnected:
  1255. blkfront_connect(info);
  1256. break;
  1257. case XenbusStateClosing:
  1258. blkfront_closing(info);
  1259. break;
  1260. }
  1261. }
  1262. static int blkfront_remove(struct xenbus_device *xbdev)
  1263. {
  1264. struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
  1265. struct block_device *bdev = NULL;
  1266. struct gendisk *disk;
  1267. dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
  1268. blkif_free(info, 0);
  1269. mutex_lock(&info->mutex);
  1270. disk = info->gd;
  1271. if (disk)
  1272. bdev = bdget_disk(disk, 0);
  1273. info->xbdev = NULL;
  1274. mutex_unlock(&info->mutex);
  1275. if (!bdev) {
  1276. kfree(info);
  1277. return 0;
  1278. }
  1279. /*
  1280. * The xbdev was removed before we reached the Closed
  1281. * state. See if it's safe to remove the disk. If the bdev
  1282. * isn't closed yet, we let release take care of it.
  1283. */
  1284. mutex_lock(&bdev->bd_mutex);
  1285. info = disk->private_data;
  1286. dev_warn(disk_to_dev(disk),
  1287. "%s was hot-unplugged, %d stale handles\n",
  1288. xbdev->nodename, bdev->bd_openers);
  1289. if (info && !bdev->bd_openers) {
  1290. xlvbd_release_gendisk(info);
  1291. disk->private_data = NULL;
  1292. kfree(info);
  1293. }
  1294. mutex_unlock(&bdev->bd_mutex);
  1295. bdput(bdev);
  1296. return 0;
  1297. }
  1298. static int blkfront_is_ready(struct xenbus_device *dev)
  1299. {
  1300. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1301. return info->is_ready && info->xbdev;
  1302. }
  1303. static int blkif_open(struct block_device *bdev, fmode_t mode)
  1304. {
  1305. struct gendisk *disk = bdev->bd_disk;
  1306. struct blkfront_info *info;
  1307. int err = 0;
  1308. mutex_lock(&blkfront_mutex);
  1309. info = disk->private_data;
  1310. if (!info) {
  1311. /* xbdev gone */
  1312. err = -ERESTARTSYS;
  1313. goto out;
  1314. }
  1315. mutex_lock(&info->mutex);
  1316. if (!info->gd)
  1317. /* xbdev is closed */
  1318. err = -ERESTARTSYS;
  1319. mutex_unlock(&info->mutex);
  1320. out:
  1321. mutex_unlock(&blkfront_mutex);
  1322. return err;
  1323. }
  1324. static int blkif_release(struct gendisk *disk, fmode_t mode)
  1325. {
  1326. struct blkfront_info *info = disk->private_data;
  1327. struct block_device *bdev;
  1328. struct xenbus_device *xbdev;
  1329. mutex_lock(&blkfront_mutex);
  1330. bdev = bdget_disk(disk, 0);
  1331. if (bdev->bd_openers)
  1332. goto out;
  1333. /*
  1334. * Check if we have been instructed to close. We will have
  1335. * deferred this request, because the bdev was still open.
  1336. */
  1337. mutex_lock(&info->mutex);
  1338. xbdev = info->xbdev;
  1339. if (xbdev && xbdev->state == XenbusStateClosing) {
  1340. /* pending switch to state closed */
  1341. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1342. xlvbd_release_gendisk(info);
  1343. xenbus_frontend_closed(info->xbdev);
  1344. }
  1345. mutex_unlock(&info->mutex);
  1346. if (!xbdev) {
  1347. /* sudden device removal */
  1348. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1349. xlvbd_release_gendisk(info);
  1350. disk->private_data = NULL;
  1351. kfree(info);
  1352. }
  1353. out:
  1354. bdput(bdev);
  1355. mutex_unlock(&blkfront_mutex);
  1356. return 0;
  1357. }
  1358. static const struct block_device_operations xlvbd_block_fops =
  1359. {
  1360. .owner = THIS_MODULE,
  1361. .open = blkif_open,
  1362. .release = blkif_release,
  1363. .getgeo = blkif_getgeo,
  1364. .ioctl = blkif_ioctl,
  1365. };
  1366. static const struct xenbus_device_id blkfront_ids[] = {
  1367. { "vbd" },
  1368. { "" }
  1369. };
  1370. static DEFINE_XENBUS_DRIVER(blkfront, ,
  1371. .probe = blkfront_probe,
  1372. .remove = blkfront_remove,
  1373. .resume = blkfront_resume,
  1374. .otherend_changed = blkback_changed,
  1375. .is_ready = blkfront_is_ready,
  1376. );
  1377. static int __init xlblk_init(void)
  1378. {
  1379. int ret;
  1380. if (!xen_domain())
  1381. return -ENODEV;
  1382. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  1383. return -ENODEV;
  1384. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  1385. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  1386. XENVBD_MAJOR, DEV_NAME);
  1387. return -ENODEV;
  1388. }
  1389. ret = xenbus_register_frontend(&blkfront_driver);
  1390. if (ret) {
  1391. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1392. return ret;
  1393. }
  1394. return 0;
  1395. }
  1396. module_init(xlblk_init);
  1397. static void __exit xlblk_exit(void)
  1398. {
  1399. xenbus_unregister_driver(&blkfront_driver);
  1400. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1401. kfree(minors);
  1402. }
  1403. module_exit(xlblk_exit);
  1404. MODULE_DESCRIPTION("Xen virtual block device frontend");
  1405. MODULE_LICENSE("GPL");
  1406. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  1407. MODULE_ALIAS("xen:vbd");
  1408. MODULE_ALIAS("xenblk");