2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
32 * Disk Image Definitions
35 #include "dev/storage/disk_image.hh"
37 #include <sys/types.h>
46 #include "base/callback.hh"
47 #include "base/misc.hh"
48 #include "base/trace.hh"
49 #include "debug/DiskImageRead.hh"
50 #include "debug/DiskImageWrite.hh"
51 #include "sim/byteswap.hh"
52 #include "sim/sim_exit.hh"
56 ////////////////////////////////////////////////////////////////////////
60 RawDiskImage::RawDiskImage(const Params
* p
)
61 : DiskImage(p
), disk_size(0)
62 { open(p
->image_file
, p
->read_only
); }
64 RawDiskImage::~RawDiskImage()
68 RawDiskImage::open(const string
&filename
, bool rd_only
)
70 if (!filename
.empty()) {
75 ios::openmode mode
= ios::in
| ios::binary
;
78 stream
.open(file
.c_str(), mode
);
79 if (!stream
.is_open())
80 panic("Error opening %s", filename
);
91 RawDiskImage::size() const
94 if (!stream
.is_open())
95 panic("file not open!\n");
96 stream
.seekg(0, ios::end
);
97 disk_size
= stream
.tellg();
100 return disk_size
/ SectorSize
;
104 RawDiskImage::read(uint8_t *data
, std::streampos offset
) const
107 panic("RawDiskImage not initialized");
109 if (!stream
.is_open())
110 panic("file not open!\n");
112 stream
.seekg(offset
* SectorSize
, ios::beg
);
114 panic("Could not seek to location in file");
116 streampos pos
= stream
.tellg();
117 stream
.read((char *)data
, SectorSize
);
119 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
120 DDUMP(DiskImageRead
, data
, SectorSize
);
122 return stream
.tellg() - pos
;
126 RawDiskImage::write(const uint8_t *data
, std::streampos offset
)
129 panic("RawDiskImage not initialized");
132 panic("Cannot write to a read only disk image");
134 if (!stream
.is_open())
135 panic("file not open!\n");
137 stream
.seekp(offset
* SectorSize
, ios::beg
);
139 panic("Could not seek to location in file");
141 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
142 DDUMP(DiskImageWrite
, data
, SectorSize
);
144 streampos pos
= stream
.tellp();
145 stream
.write((const char *)data
, SectorSize
);
146 return stream
.tellp() - pos
;
150 RawDiskImageParams::create()
152 return new RawDiskImage(this);
155 ////////////////////////////////////////////////////////////////////////
157 // Copy on Write Disk image
159 const uint32_t CowDiskImage::VersionMajor
= 1;
160 const uint32_t CowDiskImage::VersionMinor
= 0;
162 class CowDiskCallback
: public Callback
168 CowDiskCallback(CowDiskImage
*i
) : image(i
) {}
169 void process() { image
->save(); delete this; }
172 CowDiskImage::CowDiskImage(const Params
*p
)
173 : DiskImage(p
), filename(p
->image_file
), child(p
->child
), table(NULL
)
175 if (filename
.empty()) {
176 initSectorTable(p
->table_size
);
178 if (!open(filename
)) {
180 fatal("could not open read-only file");
181 initSectorTable(p
->table_size
);
185 registerExitCallback(new CowDiskCallback(this));
189 CowDiskImage::~CowDiskImage()
191 SectorTable::iterator i
= table
->begin();
192 SectorTable::iterator end
= table
->end();
201 SafeRead(ifstream
&stream
, void *data
, int count
)
203 stream
.read((char *)data
, count
);
204 if (!stream
.is_open())
205 panic("file not open");
208 panic("premature end-of-file");
210 if (stream
.bad() || stream
.fail())
211 panic("error reading cowdisk image");
216 SafeRead(ifstream
&stream
, T
&data
)
218 SafeRead(stream
, &data
, sizeof(data
));
223 SafeReadSwap(ifstream
&stream
, T
&data
)
225 SafeRead(stream
, &data
, sizeof(data
));
226 data
= letoh(data
); //is this the proper byte order conversion?
230 CowDiskImage::open(const string
&file
)
232 ifstream
stream(file
.c_str());
233 if (!stream
.is_open())
236 if (stream
.fail() || stream
.bad())
237 panic("Error opening %s", file
);
240 SafeRead(stream
, magic
);
242 if (memcmp(&magic
, "COWDISK!", sizeof(magic
)) != 0)
243 panic("Could not open %s: Invalid magic", file
);
245 uint32_t major
, minor
;
246 SafeReadSwap(stream
, major
);
247 SafeReadSwap(stream
, minor
);
249 if (major
!= VersionMajor
&& minor
!= VersionMinor
)
250 panic("Could not open %s: invalid version %d.%d != %d.%d",
251 file
, major
, minor
, VersionMajor
, VersionMinor
);
253 uint64_t sector_count
;
254 SafeReadSwap(stream
, sector_count
);
255 table
= new SectorTable(sector_count
);
258 for (uint64_t i
= 0; i
< sector_count
; i
++) {
260 SafeReadSwap(stream
, offset
);
262 Sector
*sector
= new Sector
;
263 SafeRead(stream
, sector
, sizeof(Sector
));
265 assert(table
->find(offset
) == table
->end());
266 (*table
)[offset
] = sector
;
276 CowDiskImage::initSectorTable(int hash_size
)
278 table
= new SectorTable(hash_size
);
284 SafeWrite(ofstream
&stream
, const void *data
, int count
)
286 stream
.write((const char *)data
, count
);
287 if (!stream
.is_open())
288 panic("file not open");
291 panic("premature end-of-file");
293 if (stream
.bad() || stream
.fail())
294 panic("error reading cowdisk image");
299 SafeWrite(ofstream
&stream
, const T
&data
)
301 SafeWrite(stream
, &data
, sizeof(data
));
306 SafeWriteSwap(ofstream
&stream
, const T
&data
)
308 T swappeddata
= letoh(data
); //is this the proper byte order conversion?
309 SafeWrite(stream
, &swappeddata
, sizeof(data
));
312 CowDiskImage::save() const
318 CowDiskImage::save(const string
&file
) const
321 panic("RawDiskImage not initialized");
323 ofstream
stream(file
.c_str());
324 if (!stream
.is_open() || stream
.fail() || stream
.bad())
325 panic("Error opening %s", file
);
328 memcpy(&magic
, "COWDISK!", sizeof(magic
));
329 SafeWrite(stream
, magic
);
331 SafeWriteSwap(stream
, (uint32_t)VersionMajor
);
332 SafeWriteSwap(stream
, (uint32_t)VersionMinor
);
333 SafeWriteSwap(stream
, (uint64_t)table
->size());
335 uint64_t size
= table
->size();
336 SectorTable::iterator iter
= table
->begin();
337 SectorTable::iterator end
= table
->end();
339 for (uint64_t i
= 0; i
< size
; i
++) {
341 panic("Incorrect Table Size during save of COW disk image");
343 SafeWriteSwap(stream
, (uint64_t)(*iter
).first
);
344 SafeWrite(stream
, (*iter
).second
->data
, sizeof(Sector
));
352 CowDiskImage::writeback()
354 SectorTable::iterator i
= table
->begin();
355 SectorTable::iterator end
= table
->end();
358 child
->write((*i
).second
->data
, (*i
).first
);
364 CowDiskImage::size() const
365 { return child
->size(); }
368 CowDiskImage::read(uint8_t *data
, std::streampos offset
) const
371 panic("CowDiskImage not initialized");
374 panic("access out of bounds");
376 SectorTable::const_iterator i
= table
->find(offset
);
377 if (i
== table
->end())
378 return child
->read(data
, offset
);
380 memcpy(data
, (*i
).second
->data
, SectorSize
);
381 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
382 DDUMP(DiskImageRead
, data
, SectorSize
);
388 CowDiskImage::write(const uint8_t *data
, std::streampos offset
)
391 panic("RawDiskImage not initialized");
394 panic("access out of bounds");
396 SectorTable::iterator i
= table
->find(offset
);
397 if (i
== table
->end()) {
398 Sector
*sector
= new Sector
;
399 memcpy(sector
, data
, SectorSize
);
400 table
->insert(make_pair(offset
, sector
));
402 memcpy((*i
).second
->data
, data
, SectorSize
);
405 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
406 DDUMP(DiskImageWrite
, data
, SectorSize
);
412 CowDiskImage::serialize(CheckpointOut
&cp
) const
414 string cowFilename
= name() + ".cow";
415 SERIALIZE_SCALAR(cowFilename
);
416 save(CheckpointIn::dir() + "/" + cowFilename
);
420 CowDiskImage::unserialize(CheckpointIn
&cp
)
423 UNSERIALIZE_SCALAR(cowFilename
);
424 cowFilename
= cp
.cptDir
+ "/" + cowFilename
;
429 CowDiskImageParams::create()
431 return new CowDiskImage(this);