xen-blkfront.c 42 KB

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