[libvirt] [PATCH] Fix disk device ordering

When parsing the domain xml, disks are supposed to be rearranged in an alphabetic order based on bus type and target. The reordering code had a flaw though, in that it would always put the first disk it encountered at the head of the list, and had no way of putting new entries in front of it. The attached patch reworks the code to compare the new disk against the current disk entry (rather than the next entry like the existing code). Seems to pass all my tests. Thanks, Cole commit ce3abbb62327672f756848efec7c95275fa797d6 Author: Cole (Work Acct) <crobinso@localhost.localdomain> Date: Thu Aug 21 23:04:11 2008 -0400 Fix disk ordering when parsing domain xml. diff --git a/src/domain_conf.c b/src/domain_conf.c index ed6cc8b..3c61039 100644 --- a/src/domain_conf.c +++ b/src/domain_conf.c @@ -1949,25 +1949,27 @@ static virDomainDefPtr virDomainDefParseXML(virConnectPtr conn, goto error; /* Maintain list in sorted order according to target device name */ - if (def->disks == NULL) { - disk->next = def->disks; - def->disks = disk; - } else { - virDomainDiskDefPtr ptr = def->disks; - while (ptr) { - if (ptr->next && STREQ(disk->dst, ptr->next->dst)) { - virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR, - _("duplicate disk target '%s'"), - disk->dst); - goto error; - } - if (!ptr->next || virDomainDiskCompare(disk, ptr->next) < 0) { - disk->next = ptr->next; - ptr->next = disk; - break; - } - ptr = ptr->next; + virDomainDiskDefPtr ptr = def->disks; + virDomainDiskDefPtr *prev = &(def->disks); + while (ptr) { + if (STREQ(disk->dst, ptr->dst)) { + virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR, + _("duplicate disk target '%s'"), + disk->dst); + goto error; + } + if (virDomainDiskCompare(disk, ptr) < 0) { + disk->next = ptr; + *prev = disk; + break; } + prev = &(ptr->next); + ptr = ptr->next; + } + + if (!ptr) { + disk->next = ptr; + *prev = disk; } } VIR_FREE(nodes);

On Thu, Aug 21, 2008 at 11:21:15PM -0400, Cole Robinson wrote:
When parsing the domain xml, disks are supposed to be rearranged in an alphabetic order based on bus type and target. The reordering code had a flaw though, in that it would always put the first disk it encountered at the head of the list, and had no way of putting new entries in front of it.
The attached patch reworks the code to compare the new disk against the current disk entry (rather than the next entry like the existing code). Seems to pass all my tests.
Urgh, the perils of writing everything in C. +1, this code looks correct to me. Rich. -- Richard Jones, Emerging Technologies, Red Hat http://et.redhat.com/~rjones Read my OCaml programming blog: http://camltastic.blogspot.com/ Fedora now supports 60 OCaml packages (the OPEN alternative to F#) http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora

On Thu, Aug 21, 2008 at 11:21:15PM -0400, Cole Robinson wrote:
When parsing the domain xml, disks are supposed to be rearranged in an alphabetic order based on bus type and target. The reordering code had a flaw though, in that it would always put the first disk it encountered at the head of the list, and had no way of putting new entries in front of it.
Hmm, yes good catch.
The attached patch reworks the code to compare the new disk against the current disk entry (rather than the next entry like the existing code). Seems to pass all my tests.
If the test suite passes that's good enough for me - we have a test case which explicitly does lots of disks with different buses & names for this. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

Daniel P. Berrange wrote:
On Thu, Aug 21, 2008 at 11:21:15PM -0400, Cole Robinson wrote:
When parsing the domain xml, disks are supposed to be rearranged in an alphabetic order based on bus type and target. The reordering code had a flaw though, in that it would always put the first disk it encountered at the head of the list, and had no way of putting new entries in front of it.
Hmm, yes good catch.
The attached patch reworks the code to compare the new disk against the current disk entry (rather than the next entry like the existing code). Seems to pass all my tests.
If the test suite passes that's good enough for me - we have a test case which explicitly does lots of disks with different buses & names for this.
Daniel
Okay, I combined this with the previous patch to check for target collisions and committed it. Thanks, Cole
participants (3)
-
Cole Robinson
-
Daniel P. Berrange
-
Richard W.M. Jones