vbd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /******************************************************************************
  2. * Routines for managing virtual block devices (VBDs).
  3. *
  4. * Copyright (c) 2003-2005, Keir Fraser & Steve Hand
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation; or, when distributed
  9. * separately from the Linux kernel or incorporated into other
  10. * software packages, subject to the following license:
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this source file (the "Software"), to deal in the Software without
  14. * restriction, including without limitation the rights to use, copy, modify,
  15. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  16. * and to permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  28. * IN THE SOFTWARE.
  29. */
  30. #include "common.h"
  31. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  32. (_v)->bdev->bd_part->nr_sects : get_capacity((_v)->bdev->bd_disk))
  33. unsigned long long vbd_size(struct vbd *vbd)
  34. {
  35. return vbd_sz(vbd);
  36. }
  37. unsigned int vbd_info(struct vbd *vbd)
  38. {
  39. return vbd->type | (vbd->readonly?VDISK_READONLY:0);
  40. }
  41. unsigned long vbd_secsize(struct vbd *vbd)
  42. {
  43. return bdev_logical_block_size(vbd->bdev);
  44. }
  45. int vbd_create(blkif_t *blkif, blkif_vdev_t handle, unsigned major,
  46. unsigned minor, int readonly, int cdrom)
  47. {
  48. struct vbd *vbd;
  49. struct block_device *bdev;
  50. vbd = &blkif->vbd;
  51. vbd->handle = handle;
  52. vbd->readonly = readonly;
  53. vbd->type = 0;
  54. vbd->pdevice = MKDEV(major, minor);
  55. bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
  56. FMODE_READ : FMODE_WRITE, NULL);
  57. if (IS_ERR(bdev)) {
  58. DPRINTK("vbd_creat: device %08x could not be opened.\n",
  59. vbd->pdevice);
  60. return -ENOENT;
  61. }
  62. vbd->bdev = bdev;
  63. vbd->size = vbd_size(vbd);
  64. if (vbd->bdev->bd_disk == NULL) {
  65. DPRINTK("vbd_creat: device %08x doesn't exist.\n",
  66. vbd->pdevice);
  67. vbd_free(vbd);
  68. return -ENOENT;
  69. }
  70. if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
  71. vbd->type |= VDISK_CDROM;
  72. if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
  73. vbd->type |= VDISK_REMOVABLE;
  74. DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
  75. handle, blkif->domid);
  76. return 0;
  77. }
  78. void vbd_free(struct vbd *vbd)
  79. {
  80. if (vbd->bdev)
  81. blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
  82. vbd->bdev = NULL;
  83. }
  84. int vbd_translate(struct phys_req *req, blkif_t *blkif, int operation)
  85. {
  86. struct vbd *vbd = &blkif->vbd;
  87. int rc = -EACCES;
  88. if ((operation != READ) && vbd->readonly)
  89. goto out;
  90. if (unlikely((req->sector_number + req->nr_sects) > vbd_sz(vbd)))
  91. goto out;
  92. req->dev = vbd->pdevice;
  93. req->bdev = vbd->bdev;
  94. rc = 0;
  95. out:
  96. return rc;
  97. }
  98. void vbd_resize(blkif_t *blkif)
  99. {
  100. struct vbd *vbd = &blkif->vbd;
  101. struct xenbus_transaction xbt;
  102. int err;
  103. struct xenbus_device *dev = blkback_xenbus(blkif->be);
  104. unsigned long long new_size = vbd_size(vbd);
  105. printk(KERN_INFO "VBD Resize: Domid: %d, Device: (%d, %d)\n",
  106. blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
  107. printk(KERN_INFO "VBD Resize: new size %Lu\n", new_size);
  108. vbd->size = new_size;
  109. again:
  110. err = xenbus_transaction_start(&xbt);
  111. if (err) {
  112. printk(KERN_WARNING "Error starting transaction");
  113. return;
  114. }
  115. err = xenbus_printf(xbt, dev->nodename, "sectors", "%Lu",
  116. vbd_size(vbd));
  117. if (err) {
  118. printk(KERN_WARNING "Error writing new size");
  119. goto abort;
  120. }
  121. /*
  122. * Write the current state; we will use this to synchronize
  123. * the front-end. If the current state is "connected" the
  124. * front-end will get the new size information online.
  125. */
  126. err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
  127. if (err) {
  128. printk(KERN_WARNING "Error writing the state");
  129. goto abort;
  130. }
  131. err = xenbus_transaction_end(xbt, 0);
  132. if (err == -EAGAIN)
  133. goto again;
  134. if (err)
  135. printk(KERN_WARNING "Error ending transaction");
  136. abort:
  137. xenbus_transaction_end(xbt, 1);
  138. }