提交 f08e6883 编写于 作者: M Michal Privoznik

virhostdev: Use VIR_AUTOUNREF more

There are couple of functions which get shorter after the
treatment.
Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
上级 b2985cfe
...@@ -143,49 +143,49 @@ virHostdevManagerDispose(void *obj) ...@@ -143,49 +143,49 @@ virHostdevManagerDispose(void *obj)
static virHostdevManagerPtr static virHostdevManagerPtr
virHostdevManagerNew(void) virHostdevManagerNew(void)
{ {
virHostdevManagerPtr hostdevMgr; VIR_AUTOUNREF(virHostdevManagerPtr) hostdevMgr = NULL;
bool privileged = geteuid() == 0; bool privileged = geteuid() == 0;
if (!(hostdevMgr = virObjectNew(virHostdevManagerClass))) if (!(hostdevMgr = virObjectNew(virHostdevManagerClass)))
return NULL; return NULL;
if (!(hostdevMgr->activePCIHostdevs = virPCIDeviceListNew())) if (!(hostdevMgr->activePCIHostdevs = virPCIDeviceListNew()))
goto error; return NULL;
if (!(hostdevMgr->activeUSBHostdevs = virUSBDeviceListNew())) if (!(hostdevMgr->activeUSBHostdevs = virUSBDeviceListNew()))
goto error; return NULL;
if (!(hostdevMgr->inactivePCIHostdevs = virPCIDeviceListNew())) if (!(hostdevMgr->inactivePCIHostdevs = virPCIDeviceListNew()))
goto error; return NULL;
if (!(hostdevMgr->activeSCSIHostdevs = virSCSIDeviceListNew())) if (!(hostdevMgr->activeSCSIHostdevs = virSCSIDeviceListNew()))
goto error; return NULL;
if (!(hostdevMgr->activeSCSIVHostHostdevs = virSCSIVHostDeviceListNew())) if (!(hostdevMgr->activeSCSIVHostHostdevs = virSCSIVHostDeviceListNew()))
goto error; return NULL;
if (!(hostdevMgr->activeMediatedHostdevs = virMediatedDeviceListNew())) if (!(hostdevMgr->activeMediatedHostdevs = virMediatedDeviceListNew()))
goto error; return NULL;
if (privileged) { if (privileged) {
if (VIR_STRDUP(hostdevMgr->stateDir, HOSTDEV_STATE_DIR) < 0) if (VIR_STRDUP(hostdevMgr->stateDir, HOSTDEV_STATE_DIR) < 0)
goto error; return NULL;
if (virFileMakePath(hostdevMgr->stateDir) < 0) { if (virFileMakePath(hostdevMgr->stateDir) < 0) {
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
_("Failed to create state dir '%s'"), _("Failed to create state dir '%s'"),
hostdevMgr->stateDir); hostdevMgr->stateDir);
goto error; return NULL;
} }
} else { } else {
VIR_AUTOFREE(char *) rundir = NULL; VIR_AUTOFREE(char *) rundir = NULL;
mode_t old_umask; mode_t old_umask;
if (!(rundir = virGetUserRuntimeDirectory())) if (!(rundir = virGetUserRuntimeDirectory()))
goto error; return NULL;
if (virAsprintf(&hostdevMgr->stateDir, "%s/hostdevmgr", rundir) < 0) if (virAsprintf(&hostdevMgr->stateDir, "%s/hostdevmgr", rundir) < 0)
goto error; return NULL;
old_umask = umask(077); old_umask = umask(077);
...@@ -194,16 +194,12 @@ virHostdevManagerNew(void) ...@@ -194,16 +194,12 @@ virHostdevManagerNew(void)
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
_("Failed to create state dir '%s'"), _("Failed to create state dir '%s'"),
hostdevMgr->stateDir); hostdevMgr->stateDir);
goto error; return NULL;
} }
umask(old_umask); umask(old_umask);
} }
return hostdevMgr; VIR_RETURN_PTR(hostdevMgr);
error:
virObjectUnref(hostdevMgr);
return NULL;
} }
virHostdevManagerPtr virHostdevManagerPtr
...@@ -218,7 +214,7 @@ virHostdevManagerGetDefault(void) ...@@ -218,7 +214,7 @@ virHostdevManagerGetDefault(void)
static virPCIDeviceListPtr static virPCIDeviceListPtr
virHostdevGetPCIHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs) virHostdevGetPCIHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
{ {
virPCIDeviceListPtr pcidevs; VIR_AUTOUNREF(virPCIDeviceListPtr) pcidevs = NULL;
size_t i; size_t i;
if (!(pcidevs = virPCIDeviceListNew())) if (!(pcidevs = virPCIDeviceListNew()))
...@@ -236,10 +232,8 @@ virHostdevGetPCIHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs) ...@@ -236,10 +232,8 @@ virHostdevGetPCIHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
pci = virPCIDeviceNew(pcisrc->addr.domain, pcisrc->addr.bus, pci = virPCIDeviceNew(pcisrc->addr.domain, pcisrc->addr.bus,
pcisrc->addr.slot, pcisrc->addr.function); pcisrc->addr.slot, pcisrc->addr.function);
if (!pci) { if (!pci)
virObjectUnref(pcidevs);
return NULL; return NULL;
}
virPCIDeviceSetManaged(pci, hostdev->managed); virPCIDeviceSetManaged(pci, hostdev->managed);
...@@ -250,14 +244,12 @@ virHostdevGetPCIHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs) ...@@ -250,14 +244,12 @@ virHostdevGetPCIHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
else else
virPCIDeviceSetStubDriver(pci, VIR_PCI_STUB_DRIVER_KVM); virPCIDeviceSetStubDriver(pci, VIR_PCI_STUB_DRIVER_KVM);
if (virPCIDeviceListAdd(pcidevs, pci) < 0) { if (virPCIDeviceListAdd(pcidevs, pci) < 0)
virObjectUnref(pcidevs);
return NULL; return NULL;
}
pci = NULL; pci = NULL;
} }
return pcidevs; VIR_RETURN_PTR(pcidevs);
} }
...@@ -630,7 +622,7 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr, ...@@ -630,7 +622,7 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr,
int nhostdevs, int nhostdevs,
unsigned int flags) unsigned int flags)
{ {
virPCIDeviceListPtr pcidevs = NULL; VIR_AUTOUNREF(virPCIDeviceListPtr) pcidevs = NULL;
int last_processed_hostdev_vf = -1; int last_processed_hostdev_vf = -1;
size_t i; size_t i;
int ret = -1; int ret = -1;
...@@ -914,7 +906,6 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr, ...@@ -914,7 +906,6 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr,
cleanup: cleanup:
virObjectUnlock(mgr->activePCIHostdevs); virObjectUnlock(mgr->activePCIHostdevs);
virObjectUnlock(mgr->inactivePCIHostdevs); virObjectUnlock(mgr->inactivePCIHostdevs);
virObjectUnref(pcidevs);
return ret; return ret;
} }
...@@ -957,7 +948,7 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr mgr, ...@@ -957,7 +948,7 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr mgr,
int nhostdevs, int nhostdevs,
const char *oldStateDir) const char *oldStateDir)
{ {
virPCIDeviceListPtr pcidevs; VIR_AUTOUNREF(virPCIDeviceListPtr) pcidevs = NULL;
size_t i; size_t i;
if (!nhostdevs) if (!nhostdevs)
...@@ -1406,7 +1397,7 @@ virHostdevFindUSBDevice(virDomainHostdevDefPtr hostdev, ...@@ -1406,7 +1397,7 @@ virHostdevFindUSBDevice(virDomainHostdevDefPtr hostdev,
* automatically found before. * automatically found before.
*/ */
if (vendor) { if (vendor) {
virUSBDeviceListPtr devs; VIR_AUTOUNREF(virUSBDeviceListPtr) devs = NULL;
rc = virUSBDeviceFindByVendor(vendor, product, NULL, mandatory, &devs); rc = virUSBDeviceFindByVendor(vendor, product, NULL, mandatory, &devs);
if (rc < 0) if (rc < 0)
...@@ -1416,7 +1407,6 @@ virHostdevFindUSBDevice(virDomainHostdevDefPtr hostdev, ...@@ -1416,7 +1407,6 @@ virHostdevFindUSBDevice(virDomainHostdevDefPtr hostdev,
*usb = virUSBDeviceListGet(devs, 0); *usb = virUSBDeviceListGet(devs, 0);
virUSBDeviceListSteal(devs, *usb); virUSBDeviceListSteal(devs, *usb);
} }
virObjectUnref(devs);
if (rc == 0) { if (rc == 0) {
goto out; goto out;
...@@ -1466,8 +1456,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr, ...@@ -1466,8 +1456,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr,
unsigned int flags) unsigned int flags)
{ {
size_t i; size_t i;
int ret = -1; VIR_AUTOUNREF(virUSBDeviceListPtr) list = NULL;
virUSBDeviceListPtr list;
virUSBDevicePtr tmp; virUSBDevicePtr tmp;
bool coldBoot = !!(flags & VIR_HOSTDEV_COLD_BOOT); bool coldBoot = !!(flags & VIR_HOSTDEV_COLD_BOOT);
...@@ -1480,7 +1469,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr, ...@@ -1480,7 +1469,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr,
* loop. See virHostdevPreparePCIDevices() * loop. See virHostdevPreparePCIDevices()
*/ */
if (!(list = virUSBDeviceListNew())) if (!(list = virUSBDeviceListNew()))
goto cleanup; return -1;
/* Loop 1: build temporary list /* Loop 1: build temporary list
*/ */
...@@ -1500,11 +1489,11 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr, ...@@ -1500,11 +1489,11 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr,
required = false; required = false;
if (virHostdevFindUSBDevice(hostdev, required, &usb) < 0) if (virHostdevFindUSBDevice(hostdev, required, &usb) < 0)
goto cleanup; return -1;
if (usb && virUSBDeviceListAdd(list, &usb) < 0) { if (usb && virUSBDeviceListAdd(list, &usb) < 0) {
virUSBDeviceFree(usb); virUSBDeviceFree(usb);
goto cleanup; return -1;
} }
} }
...@@ -1513,7 +1502,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr, ...@@ -1513,7 +1502,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr,
* wrong, perform rollback. * wrong, perform rollback.
*/ */
if (virHostdevMarkUSBDevices(mgr, drv_name, dom_name, list) < 0) if (virHostdevMarkUSBDevices(mgr, drv_name, dom_name, list) < 0)
goto cleanup; return -1;
/* Loop 2: Temporary list was successfully merged with /* Loop 2: Temporary list was successfully merged with
* driver list, so steal all items to avoid freeing them * driver list, so steal all items to avoid freeing them
...@@ -1524,11 +1513,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr, ...@@ -1524,11 +1513,7 @@ virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr,
virUSBDeviceListSteal(list, tmp); virUSBDeviceListSteal(list, tmp);
} }
ret = 0; return 0;
cleanup:
virObjectUnref(list);
return ret;
} }
static int static int
...@@ -1568,7 +1553,7 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr, ...@@ -1568,7 +1553,7 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr,
{ {
size_t i, j; size_t i, j;
int count; int count;
virSCSIDeviceListPtr list; VIR_AUTOUNREF(virSCSIDeviceListPtr) list = NULL;
virSCSIDevicePtr tmp; virSCSIDevicePtr tmp;
if (!nhostdevs) if (!nhostdevs)
...@@ -1580,7 +1565,7 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr, ...@@ -1580,7 +1565,7 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr,
* loop. See virHostdevPreparePCIDevices() * loop. See virHostdevPreparePCIDevices()
*/ */
if (!(list = virSCSIDeviceListNew())) if (!(list = virSCSIDeviceListNew()))
goto cleanup; return -1;
/* Loop 1: build temporary list */ /* Loop 1: build temporary list */
for (i = 0; i < nhostdevs; i++) { for (i = 0; i < nhostdevs; i++) {
...@@ -1594,7 +1579,7 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr, ...@@ -1594,7 +1579,7 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr,
continue; /* Not supported for iSCSI */ continue; /* Not supported for iSCSI */
} else { } else {
if (virHostdevPrepareSCSIHostDevices(hostdev, scsisrc, list) < 0) if (virHostdevPrepareSCSIHostDevices(hostdev, scsisrc, list) < 0)
goto cleanup; return -1;
} }
} }
...@@ -1645,7 +1630,6 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr, ...@@ -1645,7 +1630,6 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr,
virSCSIDeviceListSteal(list, tmp); virSCSIDeviceListSteal(list, tmp);
} }
virObjectUnref(list);
return 0; return 0;
error: error:
...@@ -1654,8 +1638,6 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr, ...@@ -1654,8 +1638,6 @@ virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr,
virSCSIDeviceListSteal(mgr->activeSCSIHostdevs, tmp); virSCSIDeviceListSteal(mgr->activeSCSIHostdevs, tmp);
} }
virObjectUnlock(mgr->activeSCSIHostdevs); virObjectUnlock(mgr->activeSCSIHostdevs);
cleanup:
virObjectUnref(list);
return -1; return -1;
} }
...@@ -1668,7 +1650,7 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr, ...@@ -1668,7 +1650,7 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
{ {
size_t i, j; size_t i, j;
int count; int count;
virSCSIVHostDeviceListPtr list; VIR_AUTOUNREF(virSCSIVHostDeviceListPtr) list = NULL;
virSCSIVHostDevicePtr host, tmp; virSCSIVHostDevicePtr host, tmp;
if (!nhostdevs) if (!nhostdevs)
...@@ -1680,7 +1662,7 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr, ...@@ -1680,7 +1662,7 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
* loop. See virHostdevPreparePCIDevices() * loop. See virHostdevPreparePCIDevices()
*/ */
if (!(list = virSCSIVHostDeviceListNew())) if (!(list = virSCSIVHostDeviceListNew()))
goto cleanup; return -1;
/* Loop 1: build temporary list */ /* Loop 1: build temporary list */
for (i = 0; i < nhostdevs; i++) { for (i = 0; i < nhostdevs; i++) {
...@@ -1695,11 +1677,11 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr, ...@@ -1695,11 +1677,11 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
continue; /* Not supported */ continue; /* Not supported */
if (!(host = virSCSIVHostDeviceNew(hostsrc->wwpn))) if (!(host = virSCSIVHostDeviceNew(hostsrc->wwpn)))
goto cleanup; return -1;
if (virSCSIVHostDeviceListAdd(list, host) < 0) { if (virSCSIVHostDeviceListAdd(list, host) < 0) {
virSCSIVHostDeviceFree(host); virSCSIVHostDeviceFree(host);
goto cleanup; return -1;
} }
} }
...@@ -1742,7 +1724,6 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr, ...@@ -1742,7 +1724,6 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
virSCSIVHostDeviceListSteal(list, tmp); virSCSIVHostDeviceListSteal(list, tmp);
} }
virObjectUnref(list);
return 0; return 0;
error: error:
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
...@@ -1750,8 +1731,6 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr, ...@@ -1750,8 +1731,6 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
virSCSIVHostDeviceListSteal(mgr->activeSCSIVHostHostdevs, tmp); virSCSIVHostDeviceListSteal(mgr->activeSCSIVHostHostdevs, tmp);
} }
virObjectUnlock(mgr->activeSCSIVHostHostdevs); virObjectUnlock(mgr->activeSCSIVHostHostdevs);
cleanup:
virObjectUnref(list);
return -1; return -1;
} }
...@@ -1764,8 +1743,7 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr, ...@@ -1764,8 +1743,7 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr,
int nhostdevs) int nhostdevs)
{ {
size_t i; size_t i;
int ret = -1; VIR_AUTOUNREF(virMediatedDeviceListPtr) list = NULL;
virMediatedDeviceListPtr list;
if (!nhostdevs) if (!nhostdevs)
return 0; return 0;
...@@ -1775,7 +1753,7 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr, ...@@ -1775,7 +1753,7 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr,
* A device is appended to the driver list after a series of preparations. * A device is appended to the driver list after a series of preparations.
*/ */
if (!(list = virMediatedDeviceListNew())) if (!(list = virMediatedDeviceListNew()))
goto cleanup; return -1;
/* Loop 1: Build a temporary list of ALL mediated devices. */ /* Loop 1: Build a temporary list of ALL mediated devices. */
for (i = 0; i < nhostdevs; i++) { for (i = 0; i < nhostdevs; i++) {
...@@ -1787,11 +1765,11 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr, ...@@ -1787,11 +1765,11 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr,
continue; continue;
if (!(mdev = virMediatedDeviceNew(src->uuidstr, src->model))) if (!(mdev = virMediatedDeviceNew(src->uuidstr, src->model)))
goto cleanup; return -1;
if (virMediatedDeviceListAdd(list, &mdev) < 0) { if (virMediatedDeviceListAdd(list, &mdev) < 0) {
virMediatedDeviceFree(mdev); virMediatedDeviceFree(mdev);
goto cleanup; return -1;
} }
} }
...@@ -1800,7 +1778,7 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr, ...@@ -1800,7 +1778,7 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr,
*/ */
if (virMediatedDeviceListMarkDevices(mgr->activeMediatedHostdevs, if (virMediatedDeviceListMarkDevices(mgr->activeMediatedHostdevs,
list, drv_name, dom_name) < 0) list, drv_name, dom_name) < 0)
goto cleanup; return -1;
/* Loop 2: Temporary list was successfully merged with /* Loop 2: Temporary list was successfully merged with
* driver list, so steal all items to avoid freeing them * driver list, so steal all items to avoid freeing them
...@@ -1811,10 +1789,7 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr, ...@@ -1811,10 +1789,7 @@ virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr,
virMediatedDeviceListSteal(list, tmp); virMediatedDeviceListSteal(list, tmp);
} }
ret = 0; return 0;
cleanup:
virObjectUnref(list);
return ret;
} }
void void
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册