xen-blkfront.c 52 KB

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