0e338e9c40c66d18f84776d34364580a90fb810a
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/logging.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::notifyFork()
70 if (initialized
&& !readonly
)
71 panic("Attempting to fork system with read-write raw disk image.");
73 const Params
*p(dynamic_cast<const Params
*>(params()));
75 open(p
->image_file
, p
->read_only
);
79 RawDiskImage::open(const string
&filename
, bool rd_only
)
81 if (!filename
.empty()) {
86 ios::openmode mode
= ios::in
| ios::binary
;
89 stream
.open(file
.c_str(), mode
);
90 if (!stream
.is_open())
91 panic("Error opening %s", filename
);
102 RawDiskImage::size() const
104 if (disk_size
== 0) {
105 if (!stream
.is_open())
106 panic("file not open!\n");
107 stream
.seekg(0, ios::end
);
108 disk_size
= stream
.tellg();
111 return disk_size
/ SectorSize
;
115 RawDiskImage::read(uint8_t *data
, std::streampos offset
) const
118 panic("RawDiskImage not initialized");
120 if (!stream
.is_open())
121 panic("file not open!\n");
123 stream
.seekg(offset
* SectorSize
, ios::beg
);
125 panic("Could not seek to location in file");
127 streampos pos
= stream
.tellg();
128 stream
.read((char *)data
, SectorSize
);
130 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
131 DDUMP(DiskImageRead
, data
, SectorSize
);
133 return stream
.tellg() - pos
;
137 RawDiskImage::write(const uint8_t *data
, std::streampos offset
)
140 panic("RawDiskImage not initialized");
143 panic("Cannot write to a read only disk image");
145 if (!stream
.is_open())
146 panic("file not open!\n");
148 stream
.seekp(offset
* SectorSize
, ios::beg
);
150 panic("Could not seek to location in file");
152 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
153 DDUMP(DiskImageWrite
, data
, SectorSize
);
155 streampos pos
= stream
.tellp();
156 stream
.write((const char *)data
, SectorSize
);
157 return stream
.tellp() - pos
;
161 RawDiskImageParams::create()
163 return new RawDiskImage(this);
166 ////////////////////////////////////////////////////////////////////////
168 // Copy on Write Disk image
170 const uint32_t CowDiskImage::VersionMajor
= 1;
171 const uint32_t CowDiskImage::VersionMinor
= 0;
173 class CowDiskCallback
: public Callback
179 CowDiskCallback(CowDiskImage
*i
) : image(i
) {}
180 void process() { image
->save(); delete this; }
183 CowDiskImage::CowDiskImage(const Params
*p
)
184 : DiskImage(p
), filename(p
->image_file
), child(p
->child
), table(NULL
)
186 if (filename
.empty()) {
187 initSectorTable(p
->table_size
);
189 if (!open(filename
)) {
191 fatal("could not open read-only file");
192 initSectorTable(p
->table_size
);
196 registerExitCallback(new CowDiskCallback(this));
200 CowDiskImage::~CowDiskImage()
202 SectorTable::iterator i
= table
->begin();
203 SectorTable::iterator end
= table
->end();
212 CowDiskImage::notifyFork()
214 if (!dynamic_cast<const Params
*>(params())->read_only
&&
216 inform("Disabling saving of COW image in forked child process.\n");
222 SafeRead(ifstream
&stream
, void *data
, int count
)
224 stream
.read((char *)data
, count
);
225 if (!stream
.is_open())
226 panic("file not open");
229 panic("premature end-of-file");
231 if (stream
.bad() || stream
.fail())
232 panic("error reading cowdisk image");
237 SafeRead(ifstream
&stream
, T
&data
)
239 SafeRead(stream
, &data
, sizeof(data
));
244 SafeReadSwap(ifstream
&stream
, T
&data
)
246 SafeRead(stream
, &data
, sizeof(data
));
247 data
= letoh(data
); //is this the proper byte order conversion?
251 CowDiskImage::open(const string
&file
)
253 ifstream
stream(file
.c_str());
254 if (!stream
.is_open())
257 if (stream
.fail() || stream
.bad())
258 panic("Error opening %s", file
);
261 SafeRead(stream
, magic
);
263 if (memcmp(&magic
, "COWDISK!", sizeof(magic
)) != 0)
264 panic("Could not open %s: Invalid magic", file
);
266 uint32_t major
, minor
;
267 SafeReadSwap(stream
, major
);
268 SafeReadSwap(stream
, minor
);
270 if (major
!= VersionMajor
&& minor
!= VersionMinor
)
271 panic("Could not open %s: invalid version %d.%d != %d.%d",
272 file
, major
, minor
, VersionMajor
, VersionMinor
);
274 uint64_t sector_count
;
275 SafeReadSwap(stream
, sector_count
);
276 table
= new SectorTable(sector_count
);
279 for (uint64_t i
= 0; i
< sector_count
; i
++) {
281 SafeReadSwap(stream
, offset
);
283 Sector
*sector
= new Sector
;
284 SafeRead(stream
, sector
, sizeof(Sector
));
286 assert(table
->find(offset
) == table
->end());
287 (*table
)[offset
] = sector
;
297 CowDiskImage::initSectorTable(int hash_size
)
299 table
= new SectorTable(hash_size
);
305 SafeWrite(ofstream
&stream
, const void *data
, int count
)
307 stream
.write((const char *)data
, count
);
308 if (!stream
.is_open())
309 panic("file not open");
312 panic("premature end-of-file");
314 if (stream
.bad() || stream
.fail())
315 panic("error reading cowdisk image");
320 SafeWrite(ofstream
&stream
, const T
&data
)
322 SafeWrite(stream
, &data
, sizeof(data
));
327 SafeWriteSwap(ofstream
&stream
, const T
&data
)
329 T swappeddata
= letoh(data
); //is this the proper byte order conversion?
330 SafeWrite(stream
, &swappeddata
, sizeof(data
));
333 CowDiskImage::save() const
335 // filename will be set to the empty string to disable saving of
336 // the COW image in a forked child process. Save will still be
337 // called because there is no easy way to unregister the exit
339 if (!filename
.empty())
343 CowDiskImage::save(const string
&file
) const
346 panic("RawDiskImage not initialized");
348 ofstream
stream(file
.c_str());
349 if (!stream
.is_open() || stream
.fail() || stream
.bad())
350 panic("Error opening %s", file
);
353 memcpy(&magic
, "COWDISK!", sizeof(magic
));
354 SafeWrite(stream
, magic
);
356 SafeWriteSwap(stream
, (uint32_t)VersionMajor
);
357 SafeWriteSwap(stream
, (uint32_t)VersionMinor
);
358 SafeWriteSwap(stream
, (uint64_t)table
->size());
360 uint64_t size
= table
->size();
361 SectorTable::iterator iter
= table
->begin();
362 SectorTable::iterator end
= table
->end();
364 for (uint64_t i
= 0; i
< size
; i
++) {
366 panic("Incorrect Table Size during save of COW disk image");
368 SafeWriteSwap(stream
, (uint64_t)(*iter
).first
);
369 SafeWrite(stream
, (*iter
).second
->data
, sizeof(Sector
));
377 CowDiskImage::writeback()
379 SectorTable::iterator i
= table
->begin();
380 SectorTable::iterator end
= table
->end();
383 child
->write((*i
).second
->data
, (*i
).first
);
389 CowDiskImage::size() const
390 { return child
->size(); }
393 CowDiskImage::read(uint8_t *data
, std::streampos offset
) const
396 panic("CowDiskImage not initialized");
399 panic("access out of bounds");
401 SectorTable::const_iterator i
= table
->find(offset
);
402 if (i
== table
->end())
403 return child
->read(data
, offset
);
405 memcpy(data
, (*i
).second
->data
, SectorSize
);
406 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
407 DDUMP(DiskImageRead
, data
, SectorSize
);
413 CowDiskImage::write(const uint8_t *data
, std::streampos offset
)
416 panic("RawDiskImage not initialized");
419 panic("access out of bounds");
421 SectorTable::iterator i
= table
->find(offset
);
422 if (i
== table
->end()) {
423 Sector
*sector
= new Sector
;
424 memcpy(sector
, data
, SectorSize
);
425 table
->insert(make_pair(offset
, sector
));
427 memcpy((*i
).second
->data
, data
, SectorSize
);
430 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
431 DDUMP(DiskImageWrite
, data
, SectorSize
);
437 CowDiskImage::serialize(CheckpointOut
&cp
) const
439 string cowFilename
= name() + ".cow";
440 SERIALIZE_SCALAR(cowFilename
);
441 save(CheckpointIn::dir() + "/" + cowFilename
);
445 CowDiskImage::unserialize(CheckpointIn
&cp
)
448 UNSERIALIZE_SCALAR(cowFilename
);
449 cowFilename
= cp
.cptDir
+ "/" + cowFilename
;
454 CowDiskImageParams::create()
456 return new CowDiskImage(this);