forked from ronomon/direct-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.cc
577 lines (542 loc) · 16.4 KB
/
binding.cc
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
574
575
576
577
#include <nan.h>
#include <stdint.h>
#if defined(__APPLE__)
#include <sys/disk.h>
#elif defined(_WIN32)
#include <io.h>
#include <malloc.h>
#include <winioctl.h>
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel_)
#include <sys/disk.h>
#else
#include <linux/fs.h>
#include <scsi/sg.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#endif
#define SERIAL_NUMBER_MAX 256
void free_aligned_buffer(char *buffer, void *hint) {
#if defined(_WIN32)
_aligned_free(buffer);
#else
free(buffer);
#endif
}
int read_serial_number(int fd, char *serial_number, int &serial_number_size) {
#if defined(__APPLE__)
return 0;
#elif defined(_WIN32)
return 0;
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel_)
if (ioctl(fd, DIOCGIDENT, serial_number) != 0) return -1;
serial_number_size = strlen(serial_number);
return 0;
#else
int version;
// SATA-only alternative: http://www.cplusplus.com/forum/general/191532
if (ioctl(fd, SG_GET_VERSION_NUM, &version) != 0) {
printf("Error: read_serial_number: SG_GET_VERSION_NUM\n");
return -1;
}
if (version < 30000) {
printf("Error: read_serial_number: version < 30000\n");
return -2;
}
sg_io_hdr_t io_hdr;
unsigned char dxferp[255];
unsigned char cmdp[6] = { 0x12, 0x01, 0x80, 0x00, sizeof(dxferp), 0x00 };
unsigned char sbp[32];
int length;
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
// See: http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/sg_io_hdr_t.html
io_hdr.interface_id = 'S';
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
io_hdr.cmd_len = sizeof(cmdp);
io_hdr.mx_sb_len = sizeof(sbp);
io_hdr.dxfer_len = sizeof(dxferp);
io_hdr.dxferp = dxferp;
io_hdr.cmdp = cmdp;
io_hdr.sbp = sbp;
io_hdr.timeout = 5000;
if (ioctl(fd, SG_IO, &io_hdr) != 0) {
printf("Error: read_serial_number: SG_IO\n");
return -3;
}
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
printf("Error: read_serial_number: SG_INFO_OK_MASK\n");
return -4;
}
if (io_hdr.masked_status) {
printf("Error: read_serial_number: status=0x%x\n", io_hdr.status);
printf("Error: read_serial_number: masked=0x%x\n", io_hdr.masked_status);
return -5;
}
if (io_hdr.host_status) {
printf("Error: read_serial_number: host=0x%x\n", io_hdr.host_status);
return -6;
}
if (io_hdr.driver_status) {
printf("Error: read_serial_number: driver=0x%x\n", io_hdr.driver_status);
return -7;
}
if (dxferp[1] != 0x80) {
printf("Error: read_serial_number: dxferp[1] != 0x80\n");
return -8;
}
length = SERIAL_NUMBER_MAX;
if (dxferp[3] < length) length = dxferp[3];
while (serial_number_size < length) {
serial_number[serial_number_size] = (char) dxferp[4 + serial_number_size];
serial_number_size++;
}
return 0;
#endif
}
class GetBlockDeviceWorker : public Nan::AsyncWorker {
public:
GetBlockDeviceWorker(
const int fd,
Nan::Callback *callback
) : Nan::AsyncWorker(callback),
fd(fd) {}
~GetBlockDeviceWorker() {}
void Execute() {
#if !defined(_WIN32)
// WIN32: We know of no way to assert that a handle is a block device.
struct stat st;
if (fstat(fd, &st) == -1) {
return SetErrorMessage("fstat failed");
}
// FreeBSD has character devices and no block devices:
if (
(st.st_mode & S_IFMT) != S_IFBLK &&
(st.st_mode & S_IFMT) != S_IFCHR
) {
return SetErrorMessage("fd is not a block or character device");
}
#endif
#if defined(__APPLE__)
// https://opensource.apple.com/source/xnu/xnu-1699.26.8/bsd/sys/disk.h
uint32_t _logical_sector_size;
uint32_t _physical_sector_size;
uint64_t _logical_sectors;
if (ioctl(fd, DKIOCGETBLOCKSIZE, &_logical_sector_size) == -1) {
return SetErrorMessage("DKIOCGETBLOCKSIZE failed");
}
if (ioctl(fd, DKIOCGETPHYSICALBLOCKSIZE, &_physical_sector_size) == -1) {
return SetErrorMessage("DKIOCGETPHYSICALBLOCKSIZE failed");
}
if (ioctl(fd, DKIOCGETBLOCKCOUNT, &_logical_sectors) == -1) {
return SetErrorMessage("DKIOCGETBLOCKCOUNT failed");
}
logical_sector_size = (uint64_t) _logical_sector_size;
physical_sector_size = (uint64_t) _physical_sector_size;
size = logical_sector_size * _logical_sectors;
#elif defined(_WIN32)
HANDLE handle = uv_get_osfhandle(fd);
if (handle == INVALID_HANDLE_VALUE) {
return SetErrorMessage("EBADF: bad file descriptor");
}
STORAGE_PROPERTY_QUERY query;
STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR alignment;
DISK_GEOMETRY_EX geometry;
DWORD bytes;
ZeroMemory(&query, sizeof(query));
ZeroMemory(&alignment, sizeof(alignment));
ZeroMemory(&geometry, sizeof(geometry));
query.QueryType = PropertyStandardQuery;
query.PropertyId = StorageAccessAlignmentProperty;
if (
DeviceIoControl(
handle,
IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof(query),
&alignment,
sizeof(alignment),
&bytes,
NULL
)
) {
logical_sector_size = (uint64_t) alignment.BytesPerLogicalSector;
physical_sector_size = (uint64_t) alignment.BytesPerPhysicalSector;
} else {
return SetErrorMessage("IOCTL_STORAGE_QUERY_PROPERTY failed");
}
if (
DeviceIoControl(
handle,
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL,
0,
&geometry,
sizeof(geometry),
&bytes,
NULL
)
) {
size = static_cast<uint64_t>(geometry.DiskSize.QuadPart);
} else {
return SetErrorMessage("IOCTL_DISK_GET_DRIVE_GEOMETRY_EX failed");
}
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel_)
// https://github.com/freebsd/freebsd/blob/master/sys/sys/disk.h
unsigned int _logical_sector_size;
off_t _physical_sector_size;
off_t _size;
if (ioctl(fd, DIOCGSECTORSIZE, &_logical_sector_size) == -1) {
return SetErrorMessage("DIOCGSECTORSIZE failed");
}
if (ioctl(fd, DIOCGSTRIPESIZE, &_physical_sector_size) == -1) {
return SetErrorMessage("DIOCGSTRIPESIZE failed");
}
if (ioctl(fd, DIOCGMEDIASIZE, &_size) == -1) {
return SetErrorMessage("DIOCGMEDIASIZE failed");
}
logical_sector_size = (uint64_t) _logical_sector_size;
physical_sector_size = (uint64_t) _physical_sector_size;
size = (uint64_t) _size;
#else
// https://github.com/torvalds/linux/blob/master/block/ioctl.c
// We must use the correct type according to the control code passed:
// https://stackoverflow.com/questions/19747663/where-are-ioctl-
// parameters-such-as-0x1268-blksszget-actually-specified
int _logical_sector_size;
unsigned int _physical_sector_size;
if (ioctl(fd, BLKSSZGET, &_logical_sector_size) == -1) {
return SetErrorMessage("BLKSSZGET failed");
}
if (ioctl(fd, BLKPBSZGET, &_physical_sector_size) == -1) {
return SetErrorMessage("BLKPBSZGET failed");
}
// The kernel expects size to be a u64 and defines u64 as uint64_t:
// https://github.com/torvalds/linux/blob/master/tools/include/linux/types.h
if (ioctl(fd, BLKGETSIZE64, &size) == -1) {
return SetErrorMessage("BLKGETSIZE64 failed");
}
logical_sector_size = (uint64_t) _logical_sector_size;
physical_sector_size = (uint64_t) _physical_sector_size;
#endif
if (read_serial_number(fd, serial_number, serial_number_size) != 0) {
return SetErrorMessage("read_serial_number failed");
}
}
void HandleOKCallback () {
Nan::HandleScope scope;
v8::Local<v8::Object> device = Nan::New<v8::Object>();
Nan::Set(
device,
Nan::New<v8::String>("logicalSectorSize").ToLocalChecked(),
Nan::New<v8::Number>(static_cast<double>(logical_sector_size))
);
Nan::Set(
device,
Nan::New<v8::String>("physicalSectorSize").ToLocalChecked(),
Nan::New<v8::Number>(static_cast<double>(physical_sector_size))
);
Nan::Set(
device,
Nan::New<v8::String>("serialNumber").ToLocalChecked(),
Nan::New<v8::String>(serial_number, serial_number_size).ToLocalChecked()
);
Nan::Set(
device,
Nan::New<v8::String>("size").ToLocalChecked(),
Nan::New<v8::Number>(static_cast<double>(size))
);
v8::Local<v8::Value> argv[] = {
Nan::Undefined(),
device
};
callback->Call(2, argv, async_resource);
}
private:
const int fd;
uint64_t logical_sector_size;
uint64_t physical_sector_size;
uint64_t size;
char serial_number[SERIAL_NUMBER_MAX];
int serial_number_size = 0;
};
#if defined(__APPLE__)
class SetF_NOCACHEWorker : public Nan::AsyncWorker {
public:
SetF_NOCACHEWorker(
const int fd,
const int value,
Nan::Callback *callback
) : Nan::AsyncWorker(callback),
fd(fd),
value(value) {}
~SetF_NOCACHEWorker() {}
void Execute() {
if (fcntl(fd, F_NOCACHE, value) != 0) {
if (errno == EBADF) {
SetErrorMessage("EBADF: bad file descriptor, fcntl");
} else {
SetErrorMessage("unexpected error, fcntl");
}
};
}
void HandleOKCallback() {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Undefined()
};
callback->Call(1, argv, async_resource);
}
private:
const int fd;
const int value;
};
#endif
#if !defined(_WIN32)
class SetFlockWorker : public Nan::AsyncWorker {
public:
SetFlockWorker(
const int fd,
const int value,
Nan::Callback *callback
) : Nan::AsyncWorker(callback),
fd(fd),
value(value) {}
~SetFlockWorker() {}
void Execute() {
// Place an exclusive lock. Only one process may hold an exclusive lock for
// a given file at a given time. A call to flock() may block if an
// incompatible lock is held by another process. We use LOCK_NB to make a
// non-blocking request.
int result = flock(fd, value == 0 ? LOCK_UN : LOCK_EX | LOCK_NB);
if (result != 0) {
if (errno == EWOULDBLOCK) {
SetErrorMessage("EWOULDBLOCK, the file is already locked");
} else if (errno == EBADF) {
SetErrorMessage("EBADF, fd is an invalid file descriptor");
} else if (errno == EINTR) {
SetErrorMessage("EINTR, the call was interrupted by a signal");
} else if (errno == EINVAL) {
SetErrorMessage("EINVAL, fd does not refer to a file");
} else if (errno == ENOTSUP) {
SetErrorMessage("ENOTSUP, fd is not of the correct type");
} else {
SetErrorMessage("unable to obtain an exclusive lock");
}
}
}
void HandleOKCallback () {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Undefined()
};
callback->Call(1, argv, async_resource);
}
private:
const int fd;
const int value;
};
#endif
#if defined(_WIN32)
class SetFSCTL_LOCK_VOLUMEWorker : public Nan::AsyncWorker {
public:
SetFSCTL_LOCK_VOLUMEWorker(
const int fd,
const int value,
Nan::Callback *callback
) : Nan::AsyncWorker(callback),
fd(fd),
value(value) {}
~SetFSCTL_LOCK_VOLUMEWorker() {}
void Execute() {
HANDLE handle = uv_get_osfhandle(fd);
if (handle == INVALID_HANDLE_VALUE) {
return SetErrorMessage("EBADF: bad file descriptor");
}
DWORD bytes;
if (
!DeviceIoControl(
handle,
value ? FSCTL_LOCK_VOLUME : FSCTL_UNLOCK_VOLUME,
NULL,
0,
NULL,
0,
&bytes,
NULL
)
) {
if (value) {
return SetErrorMessage("FSCTL_LOCK_VOLUME failed");
} else {
return SetErrorMessage("FSCTL_UNLOCK_VOLUME failed");
}
}
}
void HandleOKCallback() {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Undefined()
};
callback->Call(1, argv, async_resource);
}
private:
const int fd;
const int value;
};
#endif
NAN_METHOD(getAlignedBuffer) {
if (
info.Length() != 2 ||
!info[0]->IsUint32() ||
!info[1]->IsUint32()
) {
return Nan::ThrowError(
"bad arguments, expected: (uint32 size, uint32 alignment)"
);
}
const size_t size = info[0]->Uint32Value();
const size_t alignment = info[1]->Uint32Value();
if (size == 0) return Nan::ThrowError("size must not be 0");
if (alignment == 0) return Nan::ThrowError("alignment must not be 0");
if (alignment & (alignment - 1)) {
return Nan::ThrowError("alignment must be a power of 2");
}
if (alignment % sizeof(void *)) {
return Nan::ThrowError("alignment must be a multiple of pointer size");
}
#if defined(_WIN32)
void *ptr = _aligned_malloc(size, alignment);
if (ptr == NULL) return Nan::ThrowError("insufficient memory");
#else
void *ptr;
int result = posix_memalign(&ptr, alignment, size);
if (result == EINVAL) return Nan::ThrowError("bad alignment argument");
if (result == ENOMEM) return Nan::ThrowError("insufficient memory");
if (result != 0) return Nan::ThrowError("unexpected error, posix_memalign");
#endif
info.GetReturnValue().Set(
Nan::NewBuffer(
(char *) ptr,
size,
free_aligned_buffer,
NULL
).ToLocalChecked()
);
}
NAN_METHOD(getBlockDevice) {
if (
info.Length() != 2 ||
!info[0]->IsUint32() ||
!info[1]->IsFunction()
) {
return Nan::ThrowError(
"bad arguments, expected: (uint32 fd, function callback)"
);
}
const int fd = info[0]->Uint32Value();
Nan::Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
Nan::AsyncQueueWorker(new GetBlockDeviceWorker(fd, callback));
}
NAN_METHOD(setF_NOCACHE) {
#if defined(__APPLE__)
if (
info.Length() != 3 ||
!info[0]->IsUint32() ||
!info[1]->IsUint32() ||
!info[2]->IsFunction()
) {
return Nan::ThrowError(
"bad arguments, expected: (uint32 fd, uint32 value, function callback)"
);
}
const int fd = info[0]->Uint32Value();
const int value = info[1]->Uint32Value();
if (value != 0 && value != 1) return Nan::ThrowError("value must be 0 or 1");
Nan::Callback *callback = new Nan::Callback(info[2].As<v8::Function>());
Nan::AsyncQueueWorker(new SetF_NOCACHEWorker(fd, value, callback));
#else
return Nan::ThrowError("only supported on mac os");
#endif
}
NAN_METHOD(setFlock) {
#if defined(_WIN32)
return Nan::ThrowError("not supported on windows");
#else
if (
info.Length() != 3 ||
!info[0]->IsUint32() ||
!info[1]->IsUint32() ||
!info[2]->IsFunction()
) {
return Nan::ThrowError(
"bad arguments, expected: (uint32 fd, uint32 value, function callback)"
);
}
const int fd = info[0]->Uint32Value();
const int value = info[1]->Uint32Value();
if (value != 0 && value != 1) return Nan::ThrowError("value must be 0 or 1");
Nan::Callback *callback = new Nan::Callback(info[2].As<v8::Function>());
Nan::AsyncQueueWorker(new SetFlockWorker(fd, value, callback));
#endif
}
NAN_METHOD(setFSCTL_LOCK_VOLUME) {
#if defined(_WIN32)
if (
info.Length() != 3 ||
!info[0]->IsUint32() ||
!info[1]->IsUint32() ||
!info[2]->IsFunction()
) {
return Nan::ThrowError(
"bad arguments, expected: (uint32 fd, uint32 value, function callback)"
);
}
const int fd = info[0]->Uint32Value();
const int value = info[1]->Uint32Value();
if (value != 0 && value != 1) return Nan::ThrowError("value must be 0 or 1");
Nan::Callback *callback = new Nan::Callback(info[2].As<v8::Function>());
Nan::AsyncQueueWorker(new SetFSCTL_LOCK_VOLUMEWorker(fd, value, callback));
#else
return Nan::ThrowError("only supported on windows");
#endif
}
NAN_MODULE_INIT(Init) {
// On Windows, libuv maps these flags as follows:
// UV_FS_O_DIRECT > FILE_FLAG_NO_BUFFERING
// UV_FS_O_DSYNC > FILE_FLAG_WRITE_THROUGH
// UV_FS_O_EXLOCK > SHARING MODE = 0
// UV_FS_O_SYNC > FILE_FLAG_WRITE_THROUGH
#if defined(_WIN32) && defined(UV_FS_O_DIRECT) && !defined(O_DIRECT)
# define O_DIRECT UV_FS_O_DIRECT
#endif
#if defined(_WIN32) && defined(UV_FS_O_DSYNC) && !defined(O_DSYNC)
# define O_DSYNC UV_FS_O_DSYNC
#endif
#if defined(_WIN32) && defined(UV_FS_O_EXLOCK) && !defined(O_EXLOCK)
# define O_EXLOCK UV_FS_O_EXLOCK
#endif
#if defined(_WIN32) && defined(UV_FS_O_SYNC) && !defined(O_SYNC)
# define O_SYNC UV_FS_O_SYNC
#endif
#if defined(O_DIRECT)
NODE_DEFINE_CONSTANT(target, O_DIRECT);
#endif
#if defined(O_DSYNC)
NODE_DEFINE_CONSTANT(target, O_DSYNC);
#endif
#if defined(O_EXCL) && !defined(_WIN32)
NODE_DEFINE_CONSTANT(target, O_EXCL);
#endif
#if defined(O_EXLOCK)
NODE_DEFINE_CONSTANT(target, O_EXLOCK);
#endif
#if defined(O_SYNC)
NODE_DEFINE_CONSTANT(target, O_SYNC);
#endif
NAN_EXPORT(target, getAlignedBuffer);
NAN_EXPORT(target, getBlockDevice);
NAN_EXPORT(target, setF_NOCACHE);
NAN_EXPORT(target, setFlock);
NAN_EXPORT(target, setFSCTL_LOCK_VOLUME);
}
NODE_MODULE(binding, Init)
// S.D.G.