-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice.cc
More file actions
574 lines (491 loc) · 14.7 KB
/
device.cc
File metadata and controls
574 lines (491 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/* device.cc -*- C++ -*- Copyright (c) 2006 Joshua Oreman
* The rawpod tools, of which this file is a part, are licensed
* under the GNU General Public License. See the file COPYING in the
* source distribution for details.
*/
// If you uncomment this, reads will get about three times
// faster but writes will get about 50 times slower.
/* #define RAW_DEVICE_ACCESS */
#ifdef __APPLE__
#include <sys/disk.h>
#endif
#include "vfs.h"
#ifdef WIN32
#include <windows.h>
#include <winioctl.h>
#else
#include <fcntl.h> /* get the *nix O_* constants, instead of ours */
#include <sys/ioctl.h>
#define DONT_REDEFINE_OPEN_CONSTANTS
#endif
#include "device.h"
struct cow_hdr
{
#define COW_MAGIC 0x574F4372 /* 'rCOW' */
u32 magic;
u32 dummy;
u64 blocks;
} __attribute__ ((packed));
#define COW_HDR_SIZE 16
#ifdef WIN32
/**************************************************************/
LocalFile::LocalFile (const char *path, int flags) {
int access = 0;
if (flags & OPEN_READ) access |= GENERIC_READ;
if (flags & OPEN_WRITE) access |= GENERIC_WRITE;
TCHAR *tpath = (TCHAR *)malloc (strlen (path) * sizeof (TCHAR) + 2);
for (int i = 0; i < strlen (path); i++) tpath[i] = path[i];
tpath[strlen(path)] = 0;
if (flags & OPEN_CREATE) DeleteFile (tpath);
_fh = CreateFile (tpath, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, (flags & OPEN_CREATE)? CREATE_ALWAYS : OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
free (tpath);
}
LocalFile::~LocalFile()
{
close();
}
int LocalFile::read (void *buf, int len) {
DWORD lr;
if (!ReadFile (_fh, buf, len, &lr, NULL))
return -GetLastError();
return lr;
}
int LocalFile::write (const void *buf, int len) {
DWORD lw;
if (!WriteFile (_fh, buf, len, &lw, NULL))
return -GetLastError();
return lw;
}
s64 LocalFile::lseek (s64 off, int whence) {
LARGE_INTEGER newptr;
LARGE_INTEGER li_off;
li_off.QuadPart = off;
if (!SetFilePointerEx (_fh, li_off, &newptr, whence))
return -GetLastError();
return newptr.QuadPart;
}
int LocalFile::ioctl (unsigned long code, void *dataPtr, size_t dataSize) {
DWORD junk;
if (!DeviceIoControl (_fh, code, NULL, 0, dataPtr, dataSize, &junk, NULL))
return -GetLastError();
return 0;
}
int LocalFile::error() {
if (GetLastError() == NO_ERROR)
return 0;
return GetLastError();
}
int LocalFile::close() {
CloseHandle (_fh);
return 0;
}
#else
#ifdef RAW_DEVICE_ACCESS
#include <malloc.h>
#include <string.h> /* memcpy */
#endif
/**************************************************************/
LocalFile::LocalFile (const char *file, int flags) {
int mode = 0;
if (flags & OPEN_READ) {
if (flags & OPEN_WRITE) mode |= O_RDWR;
else mode |= O_RDONLY;
} else if (flags & OPEN_WRITE) mode |= O_WRONLY;
if (flags & OPEN_CREATE) mode |= O_CREAT | O_TRUNC;
#ifdef RAW_DEVICE_ACCESS
if (flags & OPEN_DEV) mode |= O_DIRECT;
#endif
_fd = open (file, mode, 0666);
}
LocalFile::~LocalFile() {
::close (_fd);
}
int LocalFile::read (void *buf, int len) {
return ::read (_fd, buf, len);
}
int LocalFile::write (const void *buf, int len) {
return ::write (_fd, buf, len);
}
s64 LocalFile::lseek (s64 off, int whence) {
return ::lseek (_fd, off, whence);
}
int LocalFile::ioctl (unsigned long code, void *dataPtr, size_t) {
return ::ioctl (_fd, code, dataPtr);
}
int LocalFile::error() {
if (_fd < 0) return errno;
return 0;
}
int LocalFile::close()
{
return ::close (_fd);
}
#endif
/**************************************************************/
#include <sys/time.h>
BlockCache *BlockCache::_cache_head = 0;
bool BlockCache::_disabled = false;
int BlockCache::_comint = 5;
BlockCache::BlockCache (int nsec, int ssize)
: _nsec (nsec), _ssize (ssize), _log_ssize (0)
{
int t = 1;
while (t < _ssize) {
t <<= 1;
_log_ssize++;
}
_ssize = (1 << _log_ssize);
_cache = (char *)malloc (_nsec * _ssize);
_sectors = new u64[_nsec];
_atimes = new u64[_nsec];
_mtimes = new u64[_nsec];
memset (_sectors, 0, _nsec * sizeof(u64));
memset (_atimes, 0, _nsec * sizeof(u64));
memset (_mtimes, 0, _nsec * sizeof(u64));
if (!_cache_head) _cache_head = this;
else {
BlockCache *cur = _cache_head;
while (cur->_cache_next) cur = cur->_cache_next;
cur->_cache_next = this;
}
_cache_next = 0;
}
BlockCache::~BlockCache()
{
BlockCache *cur = _cache_head;
if (cur == this) _cache_head = this->_cache_next;
else {
while (cur && cur->_cache_next != this) cur = cur->_cache_next;
if (cur) cur->_cache_next = this->_cache_next;
}
invalidate();
delete[] _sectors;
delete[] _atimes;
delete[] _mtimes;
free (_cache);
}
void BlockCache::invalidate()
{
memset (_atimes, 0, _nsec * sizeof(u64));
memset (_mtimes, 0, _nsec * sizeof(u64));
}
#ifdef WIN32
int gettimeofday (struct timeval *tv, void* tz)
{
union {
unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
FILETIME ft;
} now;
GetSystemTimeAsFileTime (&now.ft);
tv->tv_usec = (long) ((now.ns100 / 10LL) % 1000000LL);
tv->tv_sec = (long) ((now.ns100 - 116444736000000000LL) / 10000000LL);
return 0;
}
#endif
s64 BlockCache::getTimeval()
{
struct timeval tv;
gettimeofday (&tv, NULL);
return ((s64)tv.tv_sec << 20) + tv.tv_usec;
}
int BlockCache::dirtySectors()
{
int ret = 0;
for (int i = 0; i < _nsec; i++) {
if (_mtimes[i]) ret++;
}
return ret;
}
int BlockCache::isDirty (int idx)
{
return !!_mtimes[idx];
}
int BlockCache::flushIndex (int idx)
{
int ret;
if (_mtimes[idx]) {
ret = doRawWrite (_cache + (idx << _log_ssize), _sectors[idx]);
if (ret < 0) return ret;
_mtimes[idx] = 0;
}
return 0;
}
int BlockCache::flushOlderThan (s64 us)
{
if (_disabled) return 0;
int ret;
for (int i = 0; i < _nsec; i++) {
if (_mtimes[i] && ((us < 0) || (_mtimes[i] < (u64)us))) {
ret = doRawWrite (_cache + (i << _log_ssize), _sectors[i]);
if (ret < 0) return ret;
_mtimes[i] = 0;
}
}
return 0;
}
int BlockCache::doRead (void *buf, u64 sec)
{
if (_disabled) return doRawRead (buf, sec);
int lruIdx = 0;
u64 lruTime = _atimes[0];
int ret;
for (int i = 0; i < _nsec; i++) {
if ((_sectors[i] == sec) && (_atimes[i] != 0)) {
memcpy (buf, _cache + (i << _log_ssize), _ssize);
_atimes[i] = getTimeval();
return 0;
}
if (lruTime && (_atimes[i] < lruTime)) {
lruIdx = i;
lruTime = _atimes[i];
}
}
if (_mtimes[lruIdx] != 0) {
ret = doRawWrite (_cache + (lruIdx << _log_ssize), _sectors[lruIdx]);
if (ret < 0) return ret;
_mtimes[lruIdx] = 0;
}
ret = doRawRead (buf, sec);
if (ret < 0) return ret;
_sectors[lruIdx] = sec;
_atimes[lruIdx] = getTimeval();
memcpy (_cache + (lruIdx << _log_ssize), buf, _ssize);
return 0;
}
int BlockCache::doWrite (const void *buf, u64 sec)
{
if (_disabled) return doRawWrite (buf, sec);
int lruIdx = 0;
u64 lruTime = _atimes[0];
int ret;
ret = flushOlderThan (getTimeval() - (_comint << 20));
if (ret < 0) return ret;
for (int i = 0; i < _nsec; i++) {
if ((_sectors[i] == sec) && _atimes[i]) {
if (memcmp (_cache + (i << _log_ssize), buf, _ssize) != 0) {
memcpy (_cache + (i << _log_ssize), buf, _ssize);
_mtimes[i] = _atimes[i] = getTimeval();
return 0;
} else {
_atimes[i] = getTimeval();
return 0;
}
}
if (lruTime && (_atimes[i] < lruTime)) {
lruIdx = i;
lruTime = _atimes[i];
}
}
if (_mtimes[lruIdx] != 0) {
ret = doRawWrite (_cache + (lruIdx << _log_ssize), _sectors[lruIdx]);
if (ret < 0) return ret;
_mtimes[lruIdx] = 0;
}
_sectors[lruIdx] = sec;
_atimes[lruIdx] = _mtimes[lruIdx] = getTimeval();
memcpy (_cache + (lruIdx << _log_ssize), buf, _ssize);
return 0;
}
void BlockCache::cleanup()
{
while (_cache_head) {
_cache_head->flush();
delete _cache_head;
}
}
struct CacheCleaner { ~CacheCleaner() { BlockCache::cleanup(); } } __cleaner;
/**************************************************************/
int LocalDir::readdir (struct VFS::dirent *de)
{
if (!_dp) return 0;
struct dirent *d = ::readdir (_dp);
if (!d) return 0;
de->d_ino = 0;
strcpy (de->d_name, d->d_name);
return sizeof(*de);
}
/**************************************************************/
const char *LocalRawDevice::_override = 0;
const char *LocalRawDevice::_cowfile = 0;
int LocalRawDevice::_cachesize = 16384;
LocalRawDevice::LocalRawDevice (int n, bool writable)
: BlockDevice (), BlockCache (_cachesize)
{
#ifdef WIN32
char drive[] = "\\\\.\\PhysicalDriveN";
drive[17] = n + '0';
#elif defined (__APPLE__)
char drive[] = "/dev/rdiskX";
drive[10] = n + '0';
#else
char drive[] = "/dev/sdX";
drive[7] = n + 'a';
#endif
setBlocksize (512);
if (_override)
_f = new LocalFile (_override, OPEN_READ | (writable?OPEN_WRITE:0) );
else
_f = new LocalFile (drive, OPEN_READ | OPEN_DEV | (writable?OPEN_WRITE:0) );
if (_f->error()) _valid = -_f->error();
else {
_valid = 1;
#if defined(__APPLE__)
if (!_override) {
u64 sectors = 0;
_f->ioctl (DKIOCGETBLOCKCOUNT, §ors, sizeof(sectors));
setSize (sectors);
} else
#elif defined(WIN32)
if (!_override) {
DISK_GEOMETRY geo;
u64 size = 0;
if (_f->ioctl (IOCTL_DISK_GET_DRIVE_GEOMETRY, &geo, sizeof(geo)) == 0)
size = (geo.Cylinders.QuadPart *
(u64)geo.TracksPerCylinder *
(u64)geo.SectorsPerTrack *
(u64)geo.BytesPerSector);
setSize (size >> 9);
} else
#endif
{
s64 offset = _f->lseek (0, SEEK_END);
_f->lseek (0, SEEK_SET);
setSize ((offset <= 0)? 0 : (offset >> _blocksize_bits));
}
}
if (_valid > 0 && _cowfile) {
_wf = new LocalFile (_cowfile, OPEN_READ | OPEN_WRITE);
if (_wf->error()) {
_wf = new LocalFile (_cowfile, OPEN_READ | OPEN_WRITE | OPEN_CREATE);
if (_wf->error()) {
fprintf (stderr, "Warning: could not open or create %s, COW disabled\n", _cowfile);
delete _wf;
_wf = _f;
} else {
struct cow_hdr hdr;
hdr.magic = COW_MAGIC;
hdr.dummy = 0;
hdr.blocks = _blocks;
_wf->write (&hdr, sizeof(hdr));
}
} else {
struct cow_hdr hdr;
_wf->read (&hdr, sizeof(hdr));
if (hdr.magic != COW_MAGIC) {
fprintf (stderr, "Warning: %s is not a COW file; COW disabled\n", _cowfile);
delete _wf;
_wf = _f;
} else if (hdr.blocks != _blocks) {
fprintf (stderr, "Warning: %s is not a COW file for this device; COW disabled.\n"
" This device is %lld blocks; the COW file is for a device of %lld blocks.\n",
_cowfile, _blocks, hdr.blocks);
delete _wf;
_wf = _f;
}
}
} else _wf = _f;
}
LocalRawDevice::~LocalRawDevice()
{
BlockCache::flush();
if (_wf != _f) delete _wf;
delete _f;
}
int LocalRawDevice::doRawRead (void *buf, u64 sec)
{
#ifdef RAW_DEVICE_ACCESS
static void *alignedbuf = 0;
if (!alignedbuf)
alignedbuf = memalign (512, 512);
#endif
s64 err;
if (_valid <= 0) return _valid;
// Check the COW file first
if (_wf != _f) {
if (_wf->lseek (COW_HDR_SIZE + (sec >> 3), SEEK_SET) >= 0) {
unsigned char bits;
if ((err = _wf->read (&bits, 1)) > 0) {
// Got that block?
if (bits & (1 << (sec & 7))) {
if (_wf->lseek (COW_HDR_SIZE + (_blocks >> 3) + (sec << 9), SEEK_SET) >= 0) {
if ((err = _wf->read (buf, 512)) == 512) return 0;
return err;
}
}
}
}
}
// Else read from the real dev
if ((err = _f->lseek (sec << 9, SEEK_SET)) < 0) return err;
#ifdef RAW_DEVICE_ACCESS
if ((err = _f->read (alignedbuf, 512) < 0)) return err;
memcpy (buf, alignedbuf, 512);
#else
if ((err = _f->read (buf, 512) < 0)) return err;
#endif
return 0;
}
int LocalRawDevice::doRawWrite (const void *buf, u64 sec)
{
#ifdef RAW_DEVICE_ACCESS
static void *alignedbuf = 0;
if (!alignedbuf)
alignedbuf = memalign (512, 512);
memcpy (alignedbuf, buf, 512);
#endif
if (_valid <= 0) return _valid;
if (_wf != _f) {
s64 err;
// Write the block
if ((err = _wf->lseek (COW_HDR_SIZE + (_blocks >> 3) + (sec << 9), SEEK_SET)) < 0) return err;
if ((err = _wf->write (buf, 512) < 0)) return err;
// Mark the block as used in the COW
unsigned char bits;
if ((err = _wf->lseek (COW_HDR_SIZE + (sec >> 3), SEEK_SET)) < 0) return err;
if ((err = _wf->read (&bits, 1)) != 1) return err;
bits |= (1 << (sec & 7));
if ((err = _wf->lseek (COW_HDR_SIZE + (sec >> 3), SEEK_SET)) < 0) return err;
if ((err = _wf->write (&bits, 1)) != 1) return err;
return 0;
}
s64 err;
if (_valid <= 0) return _valid;
if ((err = _f->lseek (sec << 9, SEEK_SET)) < 0) return err;
#ifdef RAW_DEVICE_ACCESS
if ((err = _f->write (alignedbuf, 512) < 0)) return err;
#else
if ((err = _f->write (buf, 512) < 0)) return err;
#endif
return 0;
}
/**************************************************************/
PartitionDevice::PartitionDevice (VFS::Device *dev, u64 startsec, u64 nsecs)
: _dev (dev), _start (startsec), _length (nsecs)
{
setBlocksize (512);
setSize (_length);
}
int PartitionDevice::doRead (void *buf, u64 sec)
{
s64 err;
if (sec >= _length)
return 0;
if ((err = _dev->lseek ((_start + sec) << 9, SEEK_SET)) < 0)
return err;
if ((err = _dev->read (buf, 512)) < 0)
return err;
return 0;
}
int PartitionDevice::doWrite (const void *buf, u64 sec)
{
s64 err;
if (sec >= _length)
return 0;
if ((err = _dev->lseek ((_start + sec) << 9, SEEK_SET)) < 0)
return err;
if ((err = _dev->write (buf, 512)) < 0)
return err;
return 0;
}