xen-blkfront.c 44 KB

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