xen-blkfront.c 43 KB

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