xen-blkfront.c 57 KB

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