blob: 0bfceef575481d299ddb9fbfe8ec06a4e8e21167 [file] [log] [blame]
srs5694add79a62010-01-26 15:59:58 -05001//
2// C++ Interface: diskio (Unix components [Linux, FreeBSD, Mac OS X])
3//
4// Description: Class to handle low-level disk I/O for GPT fdisk
5//
6//
7// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12// This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
13// under the terms of the GNU GPL version 2, as detailed in the COPYING file.
14
15#define __STDC_LIMIT_MACROS
16#define __STDC_CONSTANT_MACROS
17
18#include <sys/ioctl.h>
srs5694fed16d02010-01-27 23:03:40 -050019#include <string.h>
srs5694add79a62010-01-26 15:59:58 -050020#include <string>
21#include <stdint.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <sys/stat.h>
srs569434882942012-03-23 12:49:15 -040025#include <unistd.h>
srs5694bf8950c2011-03-12 01:23:12 -050026
27#ifdef __linux__
28#include "linux/hdreg.h"
29#endif
30
srs5694add79a62010-01-26 15:59:58 -050031#include <iostream>
32
srs5694add79a62010-01-26 15:59:58 -050033#include "diskio.h"
34
35using namespace std;
36
37// Returns the official "real" name for a shortened version of same.
38// Trivial here; more important in Windows
39void DiskIO::MakeRealName(void) {
40 realFilename = userFilename;
41} // DiskIO::MakeRealName()
42
srs569455d92612010-03-07 22:16:07 -050043// Open the currently on-record file for reading. Returns 1 if the file is
44// already open or is opened by this call, 0 if opening the file doesn't
45// work.
srs5694add79a62010-01-26 15:59:58 -050046int DiskIO::OpenForRead(void) {
47 int shouldOpen = 1;
srs56948f1b2d62010-05-23 13:07:19 -040048 struct stat64 st;
srs5694add79a62010-01-26 15:59:58 -050049
50 if (isOpen) { // file is already open
51 if (openForWrite) {
52 Close();
53 } else {
54 shouldOpen = 0;
55 } // if/else
56 } // if
57
58 if (shouldOpen) {
59 fd = open(realFilename.c_str(), O_RDONLY);
60 if (fd == -1) {
srs569455d92612010-03-07 22:16:07 -050061 cerr << "Problem opening " << realFilename << " for reading! Error is " << errno << ".\n";
62 if (errno == EACCES) // User is probably not running as root
srs5694fed16d02010-01-27 23:03:40 -050063 cerr << "You must run this program as root or use sudo!\n";
srs569455d92612010-03-07 22:16:07 -050064 if (errno == ENOENT)
65 cerr << "The specified file does not exist!\n";
srs5694add79a62010-01-26 15:59:58 -050066 realFilename = "";
67 userFilename = "";
68 isOpen = 0;
69 openForWrite = 0;
70 } else {
srs56948f1b2d62010-05-23 13:07:19 -040071 isOpen = 0;
srs5694add79a62010-01-26 15:59:58 -050072 openForWrite = 0;
srs56948f1b2d62010-05-23 13:07:19 -040073 if (fstat64(fd, &st) == 0) {
74 if (S_ISDIR(st.st_mode))
75 cerr << "The specified path is a directory!\n";
76#ifndef __FreeBSD__
77 else if (S_ISCHR(st.st_mode))
78 cerr << "The specified path is a character device!\n";
79#endif
80 else if (S_ISFIFO(st.st_mode))
81 cerr << "The specified path is a FIFO!\n";
82 else if (S_ISSOCK(st.st_mode))
83 cerr << "The specified path is a socket!\n";
84 else
85 isOpen = 1;
86 } // if (fstat64()...)
srs5694add79a62010-01-26 15:59:58 -050087 } // if/else
88 } // if
89
90 return isOpen;
91} // DiskIO::OpenForRead(void)
92
93// An extended file-open function. This includes some system-specific checks.
94// Returns 1 if the file is open, 0 otherwise....
95int DiskIO::OpenForWrite(void) {
96 if ((isOpen) && (openForWrite))
97 return 1;
98
99 // Close the disk, in case it's already open for reading only....
100 Close();
101
102 // try to open the device; may fail....
103 fd = open(realFilename.c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
104#ifdef __APPLE__
105 // MacOS X requires a shared lock under some circumstances....
106 if (fd < 0) {
srs5694fed16d02010-01-27 23:03:40 -0500107 fd = open(realFilename.c_str(), O_WRONLY | O_SHLOCK);
srs5694add79a62010-01-26 15:59:58 -0500108 } // if
109#endif
110 if (fd >= 0) {
111 isOpen = 1;
112 openForWrite = 1;
113 } else {
114 isOpen = 0;
115 openForWrite = 0;
116 } // if/else
117 return isOpen;
118} // DiskIO::OpenForWrite(void)
119
120// Close the disk device. Note that this does NOT erase the stored filenames,
121// so the file can be re-opened without specifying the filename.
122void DiskIO::Close(void) {
123 if (isOpen)
srs569408bb0da2010-02-19 17:19:55 -0500124 if (close(fd) < 0)
125 cerr << "Warning! Problem closing file!\n";
srs5694add79a62010-01-26 15:59:58 -0500126 isOpen = 0;
127 openForWrite = 0;
128} // DiskIO::Close()
129
130// Returns block size of device pointed to by fd file descriptor. If the ioctl
131// returns an error condition, print a warning but return a value of SECTOR_SIZE
srs569455d92612010-03-07 22:16:07 -0500132// (512). If the disk can't be opened at all, return a value of 0.
srs5694add79a62010-01-26 15:59:58 -0500133int DiskIO::GetBlockSize(void) {
134 int err = -1, blockSize = 0;
135
136 // If disk isn't open, try to open it....
137 if (!isOpen) {
138 OpenForRead();
139 } // if
140
141 if (isOpen) {
142#ifdef __APPLE__
143 err = ioctl(fd, DKIOCGETBLOCKSIZE, &blockSize);
144#endif
srs569408bb0da2010-02-19 17:19:55 -0500145#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
srs5694add79a62010-01-26 15:59:58 -0500146 err = ioctl(fd, DIOCGSECTORSIZE, &blockSize);
147#endif
148#ifdef __linux__
149 err = ioctl(fd, BLKSSZGET, &blockSize);
150#endif
151
152 if (err == -1) {
153 blockSize = SECTOR_SIZE;
154 // ENOTTY = inappropriate ioctl; probably being called on a disk image
155 // file, so don't display the warning message....
156 // 32-bit code returns EINVAL, I don't know why. I know I'm treading on
157 // thin ice here, but it should be OK in all but very weird cases....
158 if ((errno != ENOTTY) && (errno != EINVAL)) {
srs5694fed16d02010-01-27 23:03:40 -0500159 cerr << "\aError " << errno << " when determining sector size! Setting sector size to "
160 << SECTOR_SIZE << "\n";
srs569455d92612010-03-07 22:16:07 -0500161 cout << "Disk device is " << realFilename << "\n";
srs5694add79a62010-01-26 15:59:58 -0500162 } // if
163 } // if (err == -1)
164 } // if (isOpen)
165
166 return (blockSize);
167} // DiskIO::GetBlockSize()
168
srs5694bf8950c2011-03-12 01:23:12 -0500169// Returns the number of heads, according to the kernel, or 255 if the
170// correct value can't be determined.
171uint32_t DiskIO::GetNumHeads(void) {
172 uint32_t numHeads = 255;
173
174#ifdef HDIO_GETGEO
175 struct hd_geometry geometry;
176
177 // If disk isn't open, try to open it....
178 if (!isOpen)
179 OpenForRead();
180
181 if (!ioctl(fd, HDIO_GETGEO, &geometry))
182 numHeads = (uint32_t) geometry.heads;
183#endif
184 return numHeads;
185} // DiskIO::GetNumHeads();
186
187// Returns the number of sectors per track, according to the kernel, or 63
188// if the correct value can't be determined.
189uint32_t DiskIO::GetNumSecsPerTrack(void) {
190 uint32_t numSecs = 63;
191
192 #ifdef HDIO_GETGEO
193 struct hd_geometry geometry;
194
195 // If disk isn't open, try to open it....
196 if (!isOpen)
197 OpenForRead();
198
199 if (!ioctl(fd, HDIO_GETGEO, &geometry))
200 numSecs = (uint32_t) geometry.sectors;
201 #endif
202 return numSecs;
203} // DiskIO::GetNumSecsPerTrack()
204
srs5694add79a62010-01-26 15:59:58 -0500205// Resync disk caches so the OS uses the new partition table. This code varies
206// a lot from one OS to another.
srs5694a17fe692011-09-10 20:30:20 -0400207// Returns 1 on success, 0 if the kernel continues to use the old partition table.
208// (Note that for most OSes, the default of 0 is returned because I've not yet
209// looked into how to test for success in the underlying system calls...)
210int DiskIO::DiskSync(void) {
211 int i, retval = 0, platformFound = 0;
srs5694add79a62010-01-26 15:59:58 -0500212
213 // If disk isn't open, try to open it....
214 if (!isOpen) {
215 OpenForRead();
216 } // if
217
218 if (isOpen) {
219 sync();
220#ifdef __APPLE__
srs5694fed16d02010-01-27 23:03:40 -0500221 cout << "Warning: The kernel may continue to use old or deleted partitions.\n"
222 << "You should reboot or remove the drive.\n";
srs5694add79a62010-01-26 15:59:58 -0500223 /* don't know if this helps
224 * it definitely will get things on disk though:
225 * http://topiks.org/mac-os-x/0321278542/ch12lev1sec8.html */
226 i = ioctl(fd, DKIOCSYNCHRONIZECACHE);
227 platformFound++;
228#endif
srs569408bb0da2010-02-19 17:19:55 -0500229#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
srs5694add79a62010-01-26 15:59:58 -0500230 sleep(2);
231 i = ioctl(fd, DIOCGFLUSH);
srs5694fed16d02010-01-27 23:03:40 -0500232 cout << "Warning: The kernel may continue to use old or deleted partitions.\n"
233 << "You should reboot or remove the drive.\n";
srs5694add79a62010-01-26 15:59:58 -0500234 platformFound++;
235#endif
236#ifdef __linux__
srs569400b6d7a2011-06-26 22:40:06 -0400237 sleep(1); // Theoretically unnecessary, but ioctl() fails sometimes if omitted....
238 fsync(fd);
srs5694add79a62010-01-26 15:59:58 -0500239 i = ioctl(fd, BLKRRPART);
srs5694a17fe692011-09-10 20:30:20 -0400240 if (i) {
srs5694fed16d02010-01-27 23:03:40 -0500241 cout << "Warning: The kernel is still using the old partition table.\n"
242 << "The new table will be used at the next reboot.\n";
srs5694a17fe692011-09-10 20:30:20 -0400243 } else {
244 retval = 1;
245 } // if/else
srs5694add79a62010-01-26 15:59:58 -0500246 platformFound++;
247#endif
248 if (platformFound == 0)
srs5694fed16d02010-01-27 23:03:40 -0500249 cerr << "Warning: Platform not recognized!\n";
srs5694add79a62010-01-26 15:59:58 -0500250 if (platformFound > 1)
srs5694fed16d02010-01-27 23:03:40 -0500251 cerr << "\nWarning: We seem to be running on multiple platforms!\n";
srs5694add79a62010-01-26 15:59:58 -0500252 } // if (isOpen)
srs5694a17fe692011-09-10 20:30:20 -0400253 return retval;
srs5694add79a62010-01-26 15:59:58 -0500254} // DiskIO::DiskSync()
255
256// Seek to the specified sector. Returns 1 on success, 0 on failure.
srs5694cb76c672010-02-11 22:22:22 -0500257// Note that seeking beyond the end of the file is NOT detected as a failure!
srs5694add79a62010-01-26 15:59:58 -0500258int DiskIO::Seek(uint64_t sector) {
259 int retval = 1;
260 off_t seekTo, sought;
261
262 // If disk isn't open, try to open it....
263 if (!isOpen) {
264 retval = OpenForRead();
265 } // if
266
267 if (isOpen) {
268 seekTo = sector * (uint64_t) GetBlockSize();
269 sought = lseek64(fd, seekTo, SEEK_SET);
270 if (sought != seekTo) {
271 retval = 0;
272 } // if
273 } // if
274 return retval;
275} // DiskIO::Seek()
276
277// A variant on the standard read() function. Done to work around
278// limitations in FreeBSD concerning the matching of the sector
279// size with the number of bytes read.
280// Returns the number of bytes read into buffer.
281int DiskIO::Read(void* buffer, int numBytes) {
srs5694cb76c672010-02-11 22:22:22 -0500282 int blockSize, numBlocks, retval = 0;
srs5694add79a62010-01-26 15:59:58 -0500283 char* tempSpace;
284
285 // If disk isn't open, try to open it....
286 if (!isOpen) {
287 OpenForRead();
288 } // if
289
290 if (isOpen) {
291 // Compute required space and allocate memory
292 blockSize = GetBlockSize();
293 if (numBytes <= blockSize) {
294 numBlocks = 1;
srs5694cb76c672010-02-11 22:22:22 -0500295 tempSpace = new char [blockSize];
srs5694add79a62010-01-26 15:59:58 -0500296 } else {
297 numBlocks = numBytes / blockSize;
srs5694cb76c672010-02-11 22:22:22 -0500298 if ((numBytes % blockSize) != 0)
299 numBlocks++;
300 tempSpace = new char [numBlocks * blockSize];
srs5694add79a62010-01-26 15:59:58 -0500301 } // if/else
srs56946aae2a92011-06-10 01:16:51 -0400302 if (tempSpace == NULL) {
303 cerr << "Unable to allocate memory in DiskIO::Read()! Terminating!\n";
304 exit(1);
305 } // if
srs5694add79a62010-01-26 15:59:58 -0500306
307 // Read the data into temporary space, then copy it to buffer
308 retval = read(fd, tempSpace, numBlocks * blockSize);
309 memcpy(buffer, tempSpace, numBytes);
srs5694add79a62010-01-26 15:59:58 -0500310
311 // Adjust the return value, if necessary....
312 if (((numBlocks * blockSize) != numBytes) && (retval > 0))
313 retval = numBytes;
314
srs5694cb76c672010-02-11 22:22:22 -0500315 delete[] tempSpace;
srs5694add79a62010-01-26 15:59:58 -0500316 } // if (isOpen)
317 return retval;
318} // DiskIO::Read()
319
320// A variant on the standard write() function. Done to work around
321// limitations in FreeBSD concerning the matching of the sector
322// size with the number of bytes read.
323// Returns the number of bytes written.
324int DiskIO::Write(void* buffer, int numBytes) {
325 int blockSize = 512, i, numBlocks, retval = 0;
326 char* tempSpace;
327
328 // If disk isn't open, try to open it....
329 if ((!isOpen) || (!openForWrite)) {
330 OpenForWrite();
331 } // if
332
333 if (isOpen) {
334 // Compute required space and allocate memory
335 blockSize = GetBlockSize();
336 if (numBytes <= blockSize) {
337 numBlocks = 1;
srs5694cb76c672010-02-11 22:22:22 -0500338 tempSpace = new char [blockSize];
srs5694add79a62010-01-26 15:59:58 -0500339 } else {
340 numBlocks = numBytes / blockSize;
341 if ((numBytes % blockSize) != 0) numBlocks++;
srs5694cb76c672010-02-11 22:22:22 -0500342 tempSpace = new char [numBlocks * blockSize];
srs5694add79a62010-01-26 15:59:58 -0500343 } // if/else
srs56946aae2a92011-06-10 01:16:51 -0400344 if (tempSpace == NULL) {
345 cerr << "Unable to allocate memory in DiskIO::Write()! Terminating!\n";
346 exit(1);
347 } // if
348
srs5694add79a62010-01-26 15:59:58 -0500349 // Copy the data to my own buffer, then write it
srs5694add79a62010-01-26 15:59:58 -0500350 memcpy(tempSpace, buffer, numBytes);
351 for (i = numBytes; i < numBlocks * blockSize; i++) {
352 tempSpace[i] = 0;
353 } // for
354 retval = write(fd, tempSpace, numBlocks * blockSize);
355
356 // Adjust the return value, if necessary....
357 if (((numBlocks * blockSize) != numBytes) && (retval > 0))
358 retval = numBytes;
359
srs5694cb76c672010-02-11 22:22:22 -0500360 delete[] tempSpace;
srs5694add79a62010-01-26 15:59:58 -0500361 } // if (isOpen)
362 return retval;
363} // DiskIO:Write()
364
365/**************************************************************************************
366 * *
367 * Below functions are lifted from various sources, as documented in comments before *
368 * each one. *
369 * *
370 **************************************************************************************/
371
372// The disksize function is taken from the Linux fdisk code and modified
373// greatly since then to enable FreeBSD and MacOS support, as well as to
374// return correct values for disk image files.
375uint64_t DiskIO::DiskSize(int *err) {
srs5694add79a62010-01-26 15:59:58 -0500376 uint64_t sectors = 0; // size in sectors
377 off_t bytes = 0; // size in bytes
378 struct stat64 st;
379 int platformFound = 0;
380
381 // If disk isn't open, try to open it....
382 if (!isOpen) {
383 OpenForRead();
384 } // if
385
386 if (isOpen) {
387 // Note to self: I recall testing a simplified version of
388 // this code, similar to what's in the __APPLE__ block,
389 // on Linux, but I had some problems. IIRC, it ran OK on 32-bit
390 // systems but not on 64-bit. Keep this in mind in case of
391 // 32/64-bit issues on MacOS....
392#ifdef __APPLE__
393 *err = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
394 platformFound++;
395#endif
srs569408bb0da2010-02-19 17:19:55 -0500396#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
srs5694add79a62010-01-26 15:59:58 -0500397 *err = ioctl(fd, DIOCGMEDIASIZE, &bytes);
srs569408bb0da2010-02-19 17:19:55 -0500398 long long b = GetBlockSize();
srs5694add79a62010-01-26 15:59:58 -0500399 sectors = bytes / b;
400 platformFound++;
401#endif
402#ifdef __linux__
srs569408bb0da2010-02-19 17:19:55 -0500403 long sz;
404 long long b;
srs5694add79a62010-01-26 15:59:58 -0500405 *err = ioctl(fd, BLKGETSIZE, &sz);
406 if (*err) {
407 sectors = sz = 0;
408 } // if
srs569464cbd172011-03-01 22:03:54 -0500409 if ((!*err) || (errno == EFBIG)) {
srs5694add79a62010-01-26 15:59:58 -0500410 *err = ioctl(fd, BLKGETSIZE64, &b);
411 if (*err || b == 0 || b == sz)
412 sectors = sz;
413 else
414 sectors = (b >> 9);
415 } // if
416 // Unintuitively, the above returns values in 512-byte blocks, no
417 // matter what the underlying device's block size. Correct for this....
418 sectors /= (GetBlockSize() / 512);
419 platformFound++;
420#endif
421 if (platformFound != 1)
srs5694fed16d02010-01-27 23:03:40 -0500422 cerr << "Warning! We seem to be running on no known platform!\n";
srs5694add79a62010-01-26 15:59:58 -0500423
424 // The above methods have failed, so let's assume it's a regular
425 // file (a QEMU image, dd backup, or what have you) and see what
426 // fstat() gives us....
427 if ((sectors == 0) || (*err == -1)) {
428 if (fstat64(fd, &st) == 0) {
srs56949a46b042011-03-15 00:34:10 -0400429 bytes = st.st_size;
srs5694add79a62010-01-26 15:59:58 -0500430 if ((bytes % UINT64_C(512)) != 0)
srs5694fed16d02010-01-27 23:03:40 -0500431 cerr << "Warning: File size is not a multiple of 512 bytes!"
432 << " Misbehavior is likely!\n\a";
srs5694add79a62010-01-26 15:59:58 -0500433 sectors = bytes / UINT64_C(512);
434 } // if
435 } // if
436 } // if (isOpen)
437 return sectors;
438} // DiskIO::DiskSize()