# HG changeset patch # User Matthias Goergens # Date 1275058713 -3600 # Node ID d021b4d997758c8984a23e1d8003a38392e83bff # Parent 3cd8cfb665698b5d4f909121dc0baf4f97a628f9 imported patch mlvm-checksum-fix diff --git a/mlvm/pv.ml b/mlvm/pv.ml --- a/mlvm/pv.ml +++ b/mlvm/pv.ml @@ -300,38 +300,40 @@ module MDAHeader = struct if written <> Constants.sector_size then failwith "Wrote short!"; Unix.close fd - let read_md dev mdah n = - (* debug *) - let oc = open_out "/tmp/hdr" in - Printf.fprintf oc "%s\n%!" (to_ascii mdah); - close_out oc; + let read_md dev mdah n = + (* debug *) + let oc = open_out "/tmp/hdr" in + Printf.fprintf oc "%s\n%!" (to_ascii mdah); + close_out oc; - let locn = List.nth mdah.mdah_raw_locns n in - let fd = - if !Constants.dummy_mode then begin - Unix.openfile (dummy_fname dev "md") [Unix.O_RDONLY] 0o000 - end else begin - let fd = Unix.openfile dev [Unix.O_RDONLY] 0o000 in - ignore(Unix.LargeFile.lseek fd (Int64.add mdah.mdah_start locn.mrl_offset) Unix.SEEK_SET); - fd - end - in - let md = - if(Int64.add locn.mrl_offset locn.mrl_size > mdah.mdah_size) - then - let firstbit = Int64.to_int (Int64.sub mdah.mdah_size locn.mrl_offset) in - let firstbitstr = really_read fd firstbit in - let secondbit = (Int64.to_int locn.mrl_size) - firstbit - 1 in - if not !Constants.dummy_mode then ignore(Unix.LargeFile.lseek fd (Int64.add mdah.mdah_start 512L) Unix.SEEK_SET); - let secondbitstr = really_read fd secondbit in - firstbitstr ^ secondbitstr - else - really_read fd (Int64.to_int locn.mrl_size - 1) in - let checksum = Crc.crc md Crc.initial_crc in - Unix.close fd; - if checksum <> locn.mrl_checksum then - Printf.fprintf stderr "Checksum invalid in metadata: Found %lx, expecting %lx\n" checksum locn.mrl_checksum; - md + let locn = List.nth mdah.mdah_raw_locns n in + let fd = + if !Constants.dummy_mode then begin + Unix.openfile (dummy_fname dev "md") [Unix.O_RDONLY] 0o000 + end else begin + let fd = Unix.openfile dev [Unix.O_RDONLY] 0o000 in + ignore(Unix.LargeFile.lseek fd (Int64.add mdah.mdah_start locn.mrl_offset) Unix.SEEK_SET); + fd + end + in + let md = + (* Include terminating \0 in this string. + * The checksum calculation in original lvm does so, too.*) + if(Int64.add locn.mrl_offset locn.mrl_size > mdah.mdah_size) + then (* wrap around *) + let firstbit = Int64.to_int (Int64.sub mdah.mdah_size locn.mrl_offset) in + let firstbitstr = really_read fd firstbit in + let secondbit = (Int64.to_int locn.mrl_size) - firstbit in + if not !Constants.dummy_mode then ignore(Unix.LargeFile.lseek fd (Int64.add mdah.mdah_start 512L) Unix.SEEK_SET); + let secondbitstr = really_read fd secondbit in + firstbitstr ^ secondbitstr + else + really_read fd (Int64.to_int locn.mrl_size) in + let checksum = Crc.crc md Crc.initial_crc in + Unix.close fd; + if checksum <> locn.mrl_checksum then + Printf.fprintf stderr "Checksum invalid in metadata: Found %lx, expecting %lx\n" checksum locn.mrl_checksum; + md let write_md device mdah md = (* Find the current raw location of the metadata, assuming there's only one copy *)