~serge-hallyn/ubuntu/raring/libvirt/libvirt-hugepages

« back to all changes in this revision

Viewing changes to src/storage/storage_driver.c

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-13 15:44:12 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20120513154412-fgmn5sxqdzgnzlx3
Tags: 0.9.12-0ubuntu1
* New upstream version:
  * Synchronize with debian packaging:
    - debian/control: Update build depends.
    - debian/libvirt-bin.postrm: Cleanup /var/log/libvirt
      on purge.
    - Bump standards verson (no changes).
    - debian/patches/Don-t-fail-if-we-can-t-setup-avahi.patch: Added
  * Dropped patches:
    - debian/patches/Debianize-libvirt-guests.patch
    - debian/patches/rewrite-lxc-controller-eof-handling-yet-again
    - debian/patches/ubuntu/libnl13.patch
    - debian/patches/ubuntu/fix-lxc-startup-error.patch
    - debian/patches/ubuntu/fix-bridge-fd.patch
    - debian/patches/ubuntu/skip-labelling-network-disks.patch
    - debian/patches/ubuntu/xen-xend-shutdown-detection.patch
    - debian/patches/ubuntu/xen-config-no-vfb-for-hvm.patch
    - debian/patches/debian/Disable-daemon-start-test.patch
    - debian/patches/debian/Disable-gnulib-s-test-nonplocking-pipe.sh.patch
    - debian/patches/ubuntu/9006-default-config-test-case.patch
    - debian/patches/fix-block-migration.patch
    - debian/patches/ubuntu/9022-qemu-unescape-HMP-commands-before-converting-them-to.patch
    - debian/patches/ubuntu/9023-qemu-change-rbd-auth_supported-separation-character-.patch
    - debian/patches/ubuntu/9024-qemu-allow-snapshotting-of-sheepdog-and-rbd-disks.patch
    - debian/patches/9025-qemu-change-rbd-auth_supported-separation-character-.patch
    - debian/patches/ubuntu/arm-gcc-workaround.patch
  * Rediffed:
    - debian/patches/Allow-libvirt-group-to-access-the-socket.patch
    - debian/patches/Disable-failing-virnetsockettest.patch
    - debian/patches/dnsmasq-as-priv-user
    - debian/patches/9002-better_default_uri_virsh.patch
  * debian/control: Add libnl-route-3-dev ass a build depends.
  * debian/patches/libnl3-build-fix.patch: Fix build with libnl3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * storage_driver.c: core driver for storage APIs
3
3
 *
4
 
 * Copyright (C) 2006-2011 Red Hat, Inc.
 
4
 * Copyright (C) 2006-2012 Red Hat, Inc.
5
5
 * Copyright (C) 2006-2008 Daniel P. Berrange
6
6
 *
7
7
 * This library is free software; you can redistribute it and/or
652
652
    if (unlink(pool->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
653
653
        char ebuf[1024];
654
654
        VIR_ERROR(_("Failed to delete autostart link '%s': %s"),
655
 
                   pool->autostartLink, virStrerror(errno, ebuf, sizeof ebuf));
 
655
                  pool->autostartLink, virStrerror(errno, ebuf, sizeof(ebuf)));
656
656
    }
657
657
 
658
658
    VIR_FREE(pool->configFile);
1695
1695
    return ret;
1696
1696
}
1697
1697
 
1698
 
 
 
1698
static int
 
1699
storageVolumeResize(virStorageVolPtr obj,
 
1700
                    unsigned long long capacity,
 
1701
                    unsigned int flags)
 
1702
{
 
1703
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
 
1704
    virStorageBackendPtr backend;
 
1705
    virStoragePoolObjPtr pool = NULL;
 
1706
    virStorageVolDefPtr vol = NULL;
 
1707
    unsigned long long abs_capacity;
 
1708
    int ret = -1;
 
1709
 
 
1710
    virCheckFlags(VIR_STORAGE_VOL_RESIZE_DELTA, -1);
 
1711
 
 
1712
    storageDriverLock(driver);
 
1713
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
 
1714
    storageDriverUnlock(driver);
 
1715
 
 
1716
    if (!pool) {
 
1717
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
 
1718
                              _("no storage pool with matching uuid"));
 
1719
        goto out;
 
1720
    }
 
1721
 
 
1722
    if (!virStoragePoolObjIsActive(pool)) {
 
1723
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
 
1724
                              _("storage pool is not active"));
 
1725
        goto out;
 
1726
    }
 
1727
 
 
1728
    if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
 
1729
        goto out;
 
1730
 
 
1731
    vol = virStorageVolDefFindByName(pool, obj->name);
 
1732
 
 
1733
    if (vol == NULL) {
 
1734
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
 
1735
                              _("no storage vol with matching name '%s'"),
 
1736
                              obj->name);
 
1737
        goto out;
 
1738
    }
 
1739
 
 
1740
    if (vol->building) {
 
1741
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
 
1742
                              _("volume '%s' is still being allocated."),
 
1743
                              vol->name);
 
1744
        goto out;
 
1745
    }
 
1746
 
 
1747
    if (flags & VIR_STORAGE_VOL_RESIZE_DELTA) {
 
1748
        abs_capacity = vol->capacity + capacity;
 
1749
        flags &= ~VIR_STORAGE_VOL_RESIZE_DELTA;
 
1750
    } else {
 
1751
        abs_capacity = capacity;
 
1752
    }
 
1753
 
 
1754
    if (abs_capacity < vol->allocation) {
 
1755
        virStorageReportError(VIR_ERR_INVALID_ARG,
 
1756
                              _("can't shrink capacity below "
 
1757
                                "existing allocation"));
 
1758
        goto out;
 
1759
    }
 
1760
 
 
1761
    if (abs_capacity > vol->capacity + pool->def->available) {
 
1762
        virStorageReportError(VIR_ERR_OPERATION_FAILED,
 
1763
                              _("Not enough space left on storage pool"));
 
1764
        goto out;
 
1765
    }
 
1766
 
 
1767
    if (!backend->resizeVol) {
 
1768
        virStorageReportError(VIR_ERR_NO_SUPPORT,
 
1769
                              _("storage pool does not support changing of "
 
1770
                                "volume capacity"));
 
1771
        goto out;
 
1772
    }
 
1773
 
 
1774
    if (backend->resizeVol(obj->conn, pool, vol, abs_capacity, flags) < 0)
 
1775
        goto out;
 
1776
 
 
1777
   vol->capacity = abs_capacity;
 
1778
   ret = 0;
 
1779
 
 
1780
out:
 
1781
    if (pool)
 
1782
        virStoragePoolObjUnlock(pool);
 
1783
 
 
1784
    return ret;
 
1785
}
1699
1786
 
1700
1787
/* If the volume we're wiping is already a sparse file, we simply
1701
1788
 * truncate and extend it to its original size, filling it with
1801
1888
 
1802
1889
 
1803
1890
static int
1804
 
storageVolumeWipeInternal(virStorageVolDefPtr def)
 
1891
storageVolumeWipeInternal(virStorageVolDefPtr def,
 
1892
                          unsigned int algorithm)
1805
1893
{
1806
1894
    int ret = -1, fd = -1;
1807
1895
    struct stat st;
1808
1896
    char *writebuf = NULL;
1809
1897
    size_t bytes_wiped = 0;
 
1898
    virCommandPtr cmd = NULL;
1810
1899
 
1811
 
    VIR_DEBUG("Wiping volume with path '%s'", def->target.path);
 
1900
    VIR_DEBUG("Wiping volume with path '%s' and algorithm %u",
 
1901
              def->target.path, algorithm);
1812
1902
 
1813
1903
    fd = open(def->target.path, O_RDWR);
1814
1904
    if (fd == -1) {
1825
1915
        goto out;
1826
1916
    }
1827
1917
 
1828
 
    if (S_ISREG(st.st_mode) && st.st_blocks < (st.st_size / DEV_BSIZE)) {
1829
 
        ret = storageVolumeZeroSparseFile(def, st.st_size, fd);
 
1918
    if (algorithm != VIR_STORAGE_VOL_WIPE_ALG_ZERO) {
 
1919
        const char *alg_char ATTRIBUTE_UNUSED = NULL;
 
1920
        switch (algorithm) {
 
1921
        case VIR_STORAGE_VOL_WIPE_ALG_NNSA:
 
1922
            alg_char = "nnsa";
 
1923
            break;
 
1924
        case VIR_STORAGE_VOL_WIPE_ALG_DOD:
 
1925
            alg_char = "dod";
 
1926
            break;
 
1927
        case VIR_STORAGE_VOL_WIPE_ALG_BSI:
 
1928
            alg_char = "bsi";
 
1929
            break;
 
1930
        case VIR_STORAGE_VOL_WIPE_ALG_GUTMANN:
 
1931
            alg_char = "gutmann";
 
1932
            break;
 
1933
        case VIR_STORAGE_VOL_WIPE_ALG_SCHNEIER:
 
1934
            alg_char = "schneier";
 
1935
            break;
 
1936
        case VIR_STORAGE_VOL_WIPE_ALG_PFITZNER7:
 
1937
            alg_char = "pfitzner7";
 
1938
            break;
 
1939
        case VIR_STORAGE_VOL_WIPE_ALG_PFITZNER33:
 
1940
            alg_char = "pfitzner33";
 
1941
            break;
 
1942
        case VIR_STORAGE_VOL_WIPE_ALG_RANDOM:
 
1943
            alg_char = "random";
 
1944
            break;
 
1945
        default:
 
1946
            virStorageReportError(VIR_ERR_INVALID_ARG,
 
1947
                                  _("unsupported algorithm %d"),
 
1948
                                  algorithm);
 
1949
        }
 
1950
        cmd = virCommandNew(SCRUB);
 
1951
        virCommandAddArgList(cmd, "-f", "-p", alg_char,
 
1952
                             def->target.path, NULL);
 
1953
 
 
1954
        if (virCommandRun(cmd, NULL) < 0)
 
1955
            goto out;
 
1956
 
 
1957
        ret = 0;
 
1958
        goto out;
1830
1959
    } else {
1831
 
 
1832
 
        if (VIR_ALLOC_N(writebuf, st.st_blksize) != 0) {
1833
 
            virReportOOMError();
1834
 
            goto out;
 
1960
        if (S_ISREG(st.st_mode) && st.st_blocks < (st.st_size / DEV_BSIZE)) {
 
1961
            ret = storageVolumeZeroSparseFile(def, st.st_size, fd);
 
1962
        } else {
 
1963
 
 
1964
            if (VIR_ALLOC_N(writebuf, st.st_blksize) != 0) {
 
1965
                virReportOOMError();
 
1966
                goto out;
 
1967
            }
 
1968
 
 
1969
            ret = storageWipeExtent(def,
 
1970
                                    fd,
 
1971
                                    0,
 
1972
                                    def->allocation,
 
1973
                                    writebuf,
 
1974
                                    st.st_blksize,
 
1975
                                    &bytes_wiped);
1835
1976
        }
1836
 
 
1837
 
        ret = storageWipeExtent(def,
1838
 
                                fd,
1839
 
                                0,
1840
 
                                def->allocation,
1841
 
                                writebuf,
1842
 
                                st.st_blksize,
1843
 
                                &bytes_wiped);
1844
1977
    }
1845
1978
 
1846
1979
out:
 
1980
    virCommandFree(cmd);
1847
1981
    VIR_FREE(writebuf);
1848
 
 
1849
1982
    VIR_FORCE_CLOSE(fd);
1850
 
 
1851
 
    return ret;
1852
 
}
1853
 
 
 
1983
    return ret;
 
1984
}
 
1985
 
 
1986
 
 
1987
static int
 
1988
storageVolumeWipePattern(virStorageVolPtr obj,
 
1989
                         unsigned int algorithm,
 
1990
                         unsigned int flags)
 
1991
{
 
1992
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
 
1993
    virStoragePoolObjPtr pool = NULL;
 
1994
    virStorageVolDefPtr vol = NULL;
 
1995
    int ret = -1;
 
1996
 
 
1997
    virCheckFlags(0, -1);
 
1998
 
 
1999
    if (algorithm >= VIR_STORAGE_VOL_WIPE_ALG_LAST) {
 
2000
        virStorageReportError(VIR_ERR_INVALID_ARG,
 
2001
                              _("wiping algorithm %d not supported"),
 
2002
                              algorithm);
 
2003
        return -1;
 
2004
    }
 
2005
 
 
2006
    storageDriverLock(driver);
 
2007
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
 
2008
    storageDriverUnlock(driver);
 
2009
 
 
2010
    if (!pool) {
 
2011
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
 
2012
                              "%s", _("no storage pool with matching uuid"));
 
2013
        goto out;
 
2014
    }
 
2015
 
 
2016
    if (!virStoragePoolObjIsActive(pool)) {
 
2017
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
 
2018
                              "%s", _("storage pool is not active"));
 
2019
        goto out;
 
2020
    }
 
2021
 
 
2022
    vol = virStorageVolDefFindByName(pool, obj->name);
 
2023
 
 
2024
    if (vol == NULL) {
 
2025
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
 
2026
                             _("no storage vol with matching name '%s'"),
 
2027
                              obj->name);
 
2028
        goto out;
 
2029
    }
 
2030
 
 
2031
    if (vol->building) {
 
2032
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
 
2033
                              _("volume '%s' is still being allocated."),
 
2034
                              vol->name);
 
2035
        goto out;
 
2036
    }
 
2037
 
 
2038
    if (storageVolumeWipeInternal(vol, algorithm) == -1) {
 
2039
        goto out;
 
2040
    }
 
2041
 
 
2042
    ret = 0;
 
2043
 
 
2044
out:
 
2045
    if (pool) {
 
2046
        virStoragePoolObjUnlock(pool);
 
2047
    }
 
2048
 
 
2049
    return ret;
 
2050
 
 
2051
}
1854
2052
 
1855
2053
static int
1856
2054
storageVolumeWipe(virStorageVolPtr obj,
1857
2055
                  unsigned int flags)
1858
2056
{
1859
 
    virStorageDriverStatePtr driver = obj->conn->storagePrivateData;
1860
 
    virStoragePoolObjPtr pool = NULL;
1861
 
    virStorageVolDefPtr vol = NULL;
1862
 
    int ret = -1;
1863
 
 
1864
 
    virCheckFlags(0, -1);
1865
 
 
1866
 
    storageDriverLock(driver);
1867
 
    pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
1868
 
    storageDriverUnlock(driver);
1869
 
 
1870
 
    if (!pool) {
1871
 
        virStorageReportError(VIR_ERR_NO_STORAGE_POOL,
1872
 
                              "%s", _("no storage pool with matching uuid"));
1873
 
        goto out;
1874
 
    }
1875
 
 
1876
 
    if (!virStoragePoolObjIsActive(pool)) {
1877
 
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1878
 
                              "%s", _("storage pool is not active"));
1879
 
        goto out;
1880
 
    }
1881
 
 
1882
 
    vol = virStorageVolDefFindByName(pool, obj->name);
1883
 
 
1884
 
    if (vol == NULL) {
1885
 
        virStorageReportError(VIR_ERR_NO_STORAGE_VOL,
1886
 
                             _("no storage vol with matching name '%s'"),
1887
 
                              obj->name);
1888
 
        goto out;
1889
 
    }
1890
 
 
1891
 
    if (vol->building) {
1892
 
        virStorageReportError(VIR_ERR_OPERATION_INVALID,
1893
 
                              _("volume '%s' is still being allocated."),
1894
 
                              vol->name);
1895
 
        goto out;
1896
 
    }
1897
 
 
1898
 
    if (storageVolumeWipeInternal(vol) == -1) {
1899
 
        goto out;
1900
 
    }
1901
 
 
1902
 
    ret = 0;
1903
 
 
1904
 
out:
1905
 
    if (pool) {
1906
 
        virStoragePoolObjUnlock(pool);
1907
 
    }
1908
 
 
1909
 
    return ret;
1910
 
 
 
2057
    return storageVolumeWipePattern(obj, VIR_STORAGE_VOL_WIPE_ALG_ZERO, flags);
1911
2058
}
1912
2059
 
1913
2060
static int
2175
2322
    .volUpload = storageVolumeUpload, /* 0.9.0 */
2176
2323
    .volDelete = storageVolumeDelete, /* 0.4.0 */
2177
2324
    .volWipe = storageVolumeWipe, /* 0.8.0 */
 
2325
    .volWipePattern = storageVolumeWipePattern, /* 0.9.10 */
2178
2326
    .volGetInfo = storageVolumeGetInfo, /* 0.4.0 */
2179
2327
    .volGetXMLDesc = storageVolumeGetXMLDesc, /* 0.4.0 */
2180
2328
    .volGetPath = storageVolumeGetPath, /* 0.4.0 */
 
2329
    .volResize = storageVolumeResize, /* 0.9.10 */
2181
2330
 
2182
2331
    .poolIsActive = storagePoolIsActive, /* 0.7.3 */
2183
2332
    .poolIsPersistent = storagePoolIsPersistent, /* 0.7.3 */