xen-blkfront.c 55 KB

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