/
KernelDevice.cc
868 lines (788 loc) · 23.8 KB
/
KernelDevice.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2014 Red Hat
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "KernelDevice.h"
#include "include/types.h"
#include "include/compat.h"
#include "include/stringify.h"
#include "common/errno.h"
#include "common/debug.h"
#include "common/blkdev.h"
#include "common/align.h"
#include "common/blkdev.h"
#define dout_context cct
#define dout_subsys ceph_subsys_bdev
#undef dout_prefix
#define dout_prefix *_dout << "bdev(" << this << " " << path << ") "
KernelDevice::KernelDevice(CephContext* cct, aio_callback_t cb, void *cbpriv)
: BlockDevice(cct),
fd_direct(-1),
fd_buffered(-1),
size(0), block_size(0),
fs(NULL), aio(false), dio(false),
debug_lock("KernelDevice::debug_lock"),
aio_queue(cct->_conf->bdev_aio_max_queue_depth),
aio_callback(cb),
aio_callback_priv(cbpriv),
aio_stop(false),
aio_thread(this),
injecting_crash(0)
{
}
int KernelDevice::_lock()
{
struct flock l;
memset(&l, 0, sizeof(l));
l.l_type = F_WRLCK;
l.l_whence = SEEK_SET;
int r = ::fcntl(fd_direct, F_SETLK, &l);
if (r < 0)
return -errno;
return 0;
}
int KernelDevice::open(const string& p)
{
path = p;
int r = 0;
dout(1) << __func__ << " path " << path << dendl;
fd_direct = ::open(path.c_str(), O_RDWR | O_DIRECT | O_CLOEXEC);
if (fd_direct < 0) {
r = -errno;
derr << __func__ << " open got: " << cpp_strerror(r) << dendl;
return r;
}
fd_buffered = ::open(path.c_str(), O_RDWR | O_CLOEXEC);
if (fd_buffered < 0) {
r = -errno;
derr << __func__ << " open got: " << cpp_strerror(r) << dendl;
goto out_direct;
}
dio = true;
aio = cct->_conf->bdev_aio;
if (!aio) {
assert(0 == "non-aio not supported");
}
// disable readahead as it will wreak havoc on our mix of
// directio/aio and buffered io.
r = posix_fadvise(fd_buffered, 0, 0, POSIX_FADV_RANDOM);
if (r) {
r = -r;
derr << __func__ << " open got: " << cpp_strerror(r) << dendl;
goto out_fail;
}
r = _lock();
if (r < 0) {
derr << __func__ << " failed to lock " << path << ": " << cpp_strerror(r)
<< dendl;
goto out_fail;
}
struct stat st;
r = ::fstat(fd_direct, &st);
if (r < 0) {
r = -errno;
derr << __func__ << " fstat got " << cpp_strerror(r) << dendl;
goto out_fail;
}
// Operate as though the block size is 4 KB. The backing file
// blksize doesn't strictly matter except that some file systems may
// require a read/modify/write if we write something smaller than
// it.
block_size = cct->_conf->bdev_block_size;
if (block_size != (unsigned)st.st_blksize) {
dout(1) << __func__ << " backing device/file reports st_blksize "
<< st.st_blksize << ", using bdev_block_size "
<< block_size << " anyway" << dendl;
}
if (S_ISBLK(st.st_mode)) {
int64_t s;
r = get_block_device_size(fd_direct, &s);
if (r < 0) {
goto out_fail;
}
size = s;
} else {
size = st.st_size;
}
if (cct->_conf->get_val<bool>("bdev_inject_bad_size")) {
derr << "injecting bad size; actual 0x" << std::hex << size
<< " but using 0x" << (size & ~block_size) << std::dec << dendl;
size &= ~(block_size);
}
{
char partition[PATH_MAX], devname[PATH_MAX];
r = get_device_by_fd(fd_buffered, partition, devname, sizeof(devname));
if (r < 0) {
derr << "unable to get device name for " << path << ": "
<< cpp_strerror(r) << dendl;
rotational = true;
} else {
dout(20) << __func__ << " devname " << devname << dendl;
rotational = block_device_is_rotational(devname);
}
}
r = _aio_start();
if (r < 0) {
goto out_fail;
}
fs = FS::create_by_fd(fd_direct);
assert(fs);
// round size down to an even block
size &= ~(block_size - 1);
dout(1) << __func__
<< " size " << size
<< " (0x" << std::hex << size << std::dec << ", "
<< byte_u_t(size) << ")"
<< " block_size " << block_size
<< " (" << byte_u_t(block_size) << ")"
<< " " << (rotational ? "rotational" : "non-rotational")
<< dendl;
return 0;
out_fail:
VOID_TEMP_FAILURE_RETRY(::close(fd_buffered));
fd_buffered = -1;
out_direct:
VOID_TEMP_FAILURE_RETRY(::close(fd_direct));
fd_direct = -1;
return r;
}
void KernelDevice::close()
{
dout(1) << __func__ << dendl;
_aio_stop();
assert(fs);
delete fs;
fs = NULL;
assert(fd_direct >= 0);
VOID_TEMP_FAILURE_RETRY(::close(fd_direct));
fd_direct = -1;
assert(fd_buffered >= 0);
VOID_TEMP_FAILURE_RETRY(::close(fd_buffered));
fd_buffered = -1;
path.clear();
}
static string get_dev_property(const char *dev, const char *property)
{
char val[1024] = {0};
get_block_device_string_property(dev, property, val, sizeof(val));
return val;
}
int KernelDevice::collect_metadata(string prefix, map<string,string> *pm) const
{
(*pm)[prefix + "rotational"] = stringify((int)(bool)rotational);
(*pm)[prefix + "size"] = stringify(get_size());
(*pm)[prefix + "block_size"] = stringify(get_block_size());
(*pm)[prefix + "driver"] = "KernelDevice";
if (rotational) {
(*pm)[prefix + "type"] = "hdd";
} else {
(*pm)[prefix + "type"] = "ssd";
}
struct stat st;
int r = ::fstat(fd_buffered, &st);
if (r < 0)
return -errno;
if (S_ISBLK(st.st_mode)) {
(*pm)[prefix + "access_mode"] = "blk";
char partition_path[PATH_MAX];
char dev_node[PATH_MAX];
int rc = get_device_by_fd(fd_buffered, partition_path, dev_node, PATH_MAX);
switch (rc) {
case -EOPNOTSUPP:
case -EINVAL:
(*pm)[prefix + "partition_path"] = "unknown";
(*pm)[prefix + "dev_node"] = "unknown";
break;
case -ENODEV:
(*pm)[prefix + "partition_path"] = string(partition_path);
(*pm)[prefix + "dev_node"] = "unknown";
break;
default:
{
(*pm)[prefix + "partition_path"] = string(partition_path);
(*pm)[prefix + "dev_node"] = string(dev_node);
(*pm)[prefix + "model"] = get_dev_property(dev_node, "device/model");
(*pm)[prefix + "dev"] = get_dev_property(dev_node, "dev");
// nvme exposes a serial number
string serial = get_dev_property(dev_node, "device/serial");
if (serial.length()) {
(*pm)[prefix + "serial"] = serial;
}
// nvme has a device/device/* structure; infer from that. there
// is probably a better way?
string nvme_vendor = get_dev_property(dev_node, "device/device/vendor");
if (nvme_vendor.length()) {
(*pm)[prefix + "type"] = "nvme";
}
}
}
} else {
(*pm)[prefix + "access_mode"] = "file";
(*pm)[prefix + "path"] = path;
}
return 0;
}
int KernelDevice::flush()
{
// protect flush with a mutex. note that we are not really protecting
// data here. instead, we're ensuring that if any flush() caller
// sees that io_since_flush is true, they block any racing callers
// until the flush is observed. that allows racing threads to be
// calling flush while still ensuring that *any* of them that got an
// aio completion notification will not return before that aio is
// stable on disk: whichever thread sees the flag first will block
// followers until the aio is stable.
std::lock_guard<std::mutex> l(flush_mutex);
bool expect = true;
if (!io_since_flush.compare_exchange_strong(expect, false)) {
dout(10) << __func__ << " no-op (no ios since last flush), flag is "
<< (int)io_since_flush.load() << dendl;
return 0;
}
dout(10) << __func__ << " start" << dendl;
if (cct->_conf->bdev_inject_crash) {
++injecting_crash;
// sleep for a moment to give other threads a chance to submit or
// wait on io that races with a flush.
derr << __func__ << " injecting crash. first we sleep..." << dendl;
sleep(cct->_conf->bdev_inject_crash_flush_delay);
derr << __func__ << " and now we die" << dendl;
cct->_log->flush();
_exit(1);
}
utime_t start = ceph_clock_now();
int r = ::fdatasync(fd_direct);
utime_t end = ceph_clock_now();
utime_t dur = end - start;
if (r < 0) {
r = -errno;
derr << __func__ << " fdatasync got: " << cpp_strerror(r) << dendl;
ceph_abort();
}
dout(5) << __func__ << " in " << dur << dendl;;
return r;
}
int KernelDevice::_aio_start()
{
if (aio) {
dout(10) << __func__ << dendl;
int r = aio_queue.init();
if (r < 0) {
if (r == -EAGAIN) {
derr << __func__ << " io_setup(2) failed with EAGAIN; "
<< "try increasing /proc/sys/fs/aio-max-nr" << dendl;
} else {
derr << __func__ << " io_setup(2) failed: " << cpp_strerror(r) << dendl;
}
return r;
}
aio_thread.create("bstore_aio");
}
return 0;
}
void KernelDevice::_aio_stop()
{
if (aio) {
dout(10) << __func__ << dendl;
aio_stop = true;
aio_thread.join();
aio_stop = false;
aio_queue.shutdown();
}
}
static bool is_expected_ioerr(const int r)
{
// https://lxr.missinglinkelectronics.com/linux+v4.15/block/blk-core.c#L135
return (r == -EOPNOTSUPP || r == -ETIMEDOUT || r == -ENOSPC ||
r == -ENOLINK || r == -EREMOTEIO || r == -EBADE ||
r == -ENODATA || r == -EILSEQ || r == -ENOMEM ||
r == -EAGAIN || r == -EREMCHG || r == -EIO);
}
void KernelDevice::_aio_thread()
{
dout(10) << __func__ << " start" << dendl;
int inject_crash_count = 0;
while (!aio_stop) {
dout(40) << __func__ << " polling" << dendl;
int max = cct->_conf->bdev_aio_reap_max;
aio_t *aio[max];
int r = aio_queue.get_next_completed(cct->_conf->bdev_aio_poll_ms,
aio, max);
if (r < 0) {
derr << __func__ << " got " << cpp_strerror(r) << dendl;
assert(0 == "got unexpected error from io_getevents");
}
if (r > 0) {
dout(30) << __func__ << " got " << r << " completed aios" << dendl;
for (int i = 0; i < r; ++i) {
IOContext *ioc = static_cast<IOContext*>(aio[i]->priv);
_aio_log_finish(ioc, aio[i]->offset, aio[i]->length);
if (aio[i]->queue_item.is_linked()) {
std::lock_guard<std::mutex> l(debug_queue_lock);
debug_aio_unlink(*aio[i]);
}
// set flag indicating new ios have completed. we do this *before*
// any completion or notifications so that any user flush() that
// follows the observed io completion will include this io. Note
// that an earlier, racing flush() could observe and clear this
// flag, but that also ensures that the IO will be stable before the
// later flush() occurs.
io_since_flush.store(true);
long r = aio[i]->get_return_value();
if (r < 0) {
derr << __func__ << " got r=" << r << " (" << cpp_strerror(r) << ")"
<< dendl;
if (ioc->allow_eio && is_expected_ioerr(r)) {
derr << __func__ << " translating the error to EIO for upper layer"
<< dendl;
ioc->set_return_value(-EIO);
} else {
assert(0 == "got unexpected error from aio_t::get_return_value. "
"This may suggest HW issue. Please check your dmesg!");
}
} else if (aio[i]->length != (uint64_t)r) {
derr << "aio to " << aio[i]->offset << "~" << aio[i]->length
<< " but returned: " << r << dendl;
assert(0 == "unexpected aio error");
}
dout(10) << __func__ << " finished aio " << aio[i] << " r " << r
<< " ioc " << ioc
<< " with " << (ioc->num_running.load() - 1)
<< " aios left" << dendl;
// NOTE: once num_running and we either call the callback or
// call aio_wake we cannot touch ioc or aio[] as the caller
// may free it.
if (ioc->priv) {
if (--ioc->num_running == 0) {
aio_callback(aio_callback_priv, ioc->priv);
}
} else {
ioc->try_aio_wake();
}
}
}
if (cct->_conf->bdev_debug_aio) {
utime_t now = ceph_clock_now();
std::lock_guard<std::mutex> l(debug_queue_lock);
if (debug_oldest) {
if (debug_stall_since == utime_t()) {
debug_stall_since = now;
} else {
utime_t cutoff = now;
cutoff -= cct->_conf->bdev_debug_aio_suicide_timeout;
if (debug_stall_since < cutoff) {
derr << __func__ << " stalled aio " << debug_oldest
<< " since " << debug_stall_since << ", timeout is "
<< cct->_conf->bdev_debug_aio_suicide_timeout
<< "s, suicide" << dendl;
assert(0 == "stalled aio... buggy kernel or bad device?");
}
}
}
}
reap_ioc();
if (cct->_conf->bdev_inject_crash) {
++inject_crash_count;
if (inject_crash_count * cct->_conf->bdev_aio_poll_ms / 1000 >
cct->_conf->bdev_inject_crash + cct->_conf->bdev_inject_crash_flush_delay) {
derr << __func__ << " bdev_inject_crash trigger from aio thread"
<< dendl;
cct->_log->flush();
_exit(1);
}
}
}
reap_ioc();
dout(10) << __func__ << " end" << dendl;
}
void KernelDevice::_aio_log_start(
IOContext *ioc,
uint64_t offset,
uint64_t length)
{
dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length
<< std::dec << dendl;
if (cct->_conf->bdev_debug_inflight_ios) {
Mutex::Locker l(debug_lock);
if (debug_inflight.intersects(offset, length)) {
derr << __func__ << " inflight overlap of 0x"
<< std::hex
<< offset << "~" << length << std::dec
<< " with " << debug_inflight << dendl;
ceph_abort();
}
debug_inflight.insert(offset, length);
}
}
void KernelDevice::debug_aio_link(aio_t& aio)
{
if (debug_queue.empty()) {
debug_oldest = &aio;
}
debug_queue.push_back(aio);
}
void KernelDevice::debug_aio_unlink(aio_t& aio)
{
if (aio.queue_item.is_linked()) {
debug_queue.erase(debug_queue.iterator_to(aio));
if (debug_oldest == &aio) {
if (debug_queue.empty()) {
debug_oldest = nullptr;
} else {
debug_oldest = &debug_queue.front();
}
debug_stall_since = utime_t();
}
}
}
void KernelDevice::_aio_log_finish(
IOContext *ioc,
uint64_t offset,
uint64_t length)
{
dout(20) << __func__ << " " << aio << " 0x"
<< std::hex << offset << "~" << length << std::dec << dendl;
if (cct->_conf->bdev_debug_inflight_ios) {
Mutex::Locker l(debug_lock);
debug_inflight.erase(offset, length);
}
}
void KernelDevice::aio_submit(IOContext *ioc)
{
dout(20) << __func__ << " ioc " << ioc
<< " pending " << ioc->num_pending.load()
<< " running " << ioc->num_running.load()
<< dendl;
if (ioc->num_pending.load() == 0) {
return;
}
// move these aside, and get our end iterator position now, as the
// aios might complete as soon as they are submitted and queue more
// wal aio's.
list<aio_t>::iterator e = ioc->running_aios.begin();
ioc->running_aios.splice(e, ioc->pending_aios);
int pending = ioc->num_pending.load();
ioc->num_running += pending;
ioc->num_pending -= pending;
assert(ioc->num_pending.load() == 0); // we should be only thread doing this
assert(ioc->pending_aios.size() == 0);
if (cct->_conf->bdev_debug_aio) {
list<aio_t>::iterator p = ioc->running_aios.begin();
while (p != e) {
dout(30) << __func__ << " " << *p << dendl;
std::lock_guard<std::mutex> l(debug_queue_lock);
debug_aio_link(*p++);
}
}
void *priv = static_cast<void*>(ioc);
int r, retries = 0;
r = aio_queue.submit_batch(ioc->running_aios.begin(), e,
ioc->num_running.load(), priv, &retries);
if (retries)
derr << __func__ << " retries " << retries << dendl;
if (r < 0) {
derr << " aio submit got " << cpp_strerror(r) << dendl;
assert(r == 0);
}
}
int KernelDevice::_sync_write(uint64_t off, bufferlist &bl, bool buffered)
{
uint64_t len = bl.length();
dout(5) << __func__ << " 0x" << std::hex << off << "~" << len
<< std::dec << " buffered" << dendl;
if (cct->_conf->bdev_inject_crash &&
rand() % cct->_conf->bdev_inject_crash == 0) {
derr << __func__ << " bdev_inject_crash: dropping io 0x" << std::hex
<< off << "~" << len << std::dec << dendl;
++injecting_crash;
return 0;
}
vector<iovec> iov;
bl.prepare_iov(&iov);
int r = ::pwritev(buffered ? fd_buffered : fd_direct,
&iov[0], iov.size(), off);
if (r < 0) {
r = -errno;
derr << __func__ << " pwritev error: " << cpp_strerror(r) << dendl;
return r;
}
if (buffered) {
// initiate IO and wait till it completes
r = ::sync_file_range(fd_buffered, off, len, SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER|SYNC_FILE_RANGE_WAIT_BEFORE);
if (r < 0) {
r = -errno;
derr << __func__ << " sync_file_range error: " << cpp_strerror(r) << dendl;
return r;
}
}
io_since_flush.store(true);
return 0;
}
int KernelDevice::write(
uint64_t off,
bufferlist &bl,
bool buffered)
{
uint64_t len = bl.length();
dout(20) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
<< (buffered ? " (buffered)" : " (direct)")
<< dendl;
assert(off % block_size == 0);
assert(len % block_size == 0);
assert(len > 0);
assert(off < size);
assert(off + len <= size);
if ((!buffered || bl.get_num_buffers() >= IOV_MAX) &&
bl.rebuild_aligned_size_and_memory(block_size, block_size, IOV_MAX)) {
dout(20) << __func__ << " rebuilding buffer to be aligned" << dendl;
}
dout(40) << "data: ";
bl.hexdump(*_dout);
*_dout << dendl;
return _sync_write(off, bl, buffered);
}
int KernelDevice::aio_write(
uint64_t off,
bufferlist &bl,
IOContext *ioc,
bool buffered)
{
uint64_t len = bl.length();
dout(20) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
<< (buffered ? " (buffered)" : " (direct)")
<< dendl;
assert(off % block_size == 0);
assert(len % block_size == 0);
assert(len > 0);
assert(off < size);
assert(off + len <= size);
if ((!buffered || bl.get_num_buffers() >= IOV_MAX) &&
bl.rebuild_aligned_size_and_memory(block_size, block_size, IOV_MAX)) {
dout(20) << __func__ << " rebuilding buffer to be aligned" << dendl;
}
dout(40) << "data: ";
bl.hexdump(*_dout);
*_dout << dendl;
_aio_log_start(ioc, off, len);
#ifdef HAVE_LIBAIO
if (aio && dio && !buffered) {
if (cct->_conf->bdev_inject_crash &&
rand() % cct->_conf->bdev_inject_crash == 0) {
derr << __func__ << " bdev_inject_crash: dropping io 0x" << std::hex
<< off << "~" << len << std::dec
<< dendl;
// generate a real io so that aio_wait behaves properly, but make it
// a read instead of write, and toss the result.
ioc->pending_aios.push_back(aio_t(ioc, fd_direct));
++ioc->num_pending;
auto& aio = ioc->pending_aios.back();
aio.pread(off, len);
++injecting_crash;
} else {
if (bl.length() <= RW_IO_MAX) {
// fast path (non-huge write)
ioc->pending_aios.push_back(aio_t(ioc, fd_direct));
++ioc->num_pending;
auto& aio = ioc->pending_aios.back();
bl.prepare_iov(&aio.iov);
aio.bl.claim_append(bl);
aio.pwritev(off, len);
dout(30) << aio << dendl;
dout(5) << __func__ << " 0x" << std::hex << off << "~" << len
<< std::dec << " aio " << &aio << dendl;
} else {
// write in RW_IO_MAX-sized chunks
uint64_t prev_len = 0;
while (prev_len < bl.length()) {
bufferlist tmp;
if (prev_len + RW_IO_MAX < bl.length()) {
tmp.substr_of(bl, prev_len, RW_IO_MAX);
} else {
tmp.substr_of(bl, prev_len, bl.length() - prev_len);
}
auto len = tmp.length();
ioc->pending_aios.push_back(aio_t(ioc, fd_direct));
++ioc->num_pending;
auto& aio = ioc->pending_aios.back();
tmp.prepare_iov(&aio.iov);
aio.bl.claim_append(tmp);
aio.pwritev(off + prev_len, len);
dout(30) << aio << dendl;
dout(5) << __func__ << " 0x" << std::hex << off + prev_len
<< "~" << len
<< std::dec << " aio " << &aio << " (piece)" << dendl;
prev_len += len;
}
}
}
} else
#endif
{
int r = _sync_write(off, bl, buffered);
_aio_log_finish(ioc, off, len);
if (r < 0)
return r;
}
return 0;
}
int KernelDevice::read(uint64_t off, uint64_t len, bufferlist *pbl,
IOContext *ioc,
bool buffered)
{
dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
<< (buffered ? " (buffered)" : " (direct)")
<< dendl;
assert(off % block_size == 0);
assert(len % block_size == 0);
assert(len > 0);
assert(off < size);
assert(off + len <= size);
_aio_log_start(ioc, off, len);
bufferptr p = buffer::create_page_aligned(len);
int r = ::pread(buffered ? fd_buffered : fd_direct,
p.c_str(), len, off);
if (r < 0) {
if (ioc->allow_eio && is_expected_ioerr(r)) {
r = -EIO;
} else {
r = -errno;
}
goto out;
}
assert((uint64_t)r == len);
pbl->push_back(std::move(p));
dout(40) << "data: ";
pbl->hexdump(*_dout);
*_dout << dendl;
out:
_aio_log_finish(ioc, off, len);
return r < 0 ? r : 0;
}
int KernelDevice::aio_read(
uint64_t off,
uint64_t len,
bufferlist *pbl,
IOContext *ioc)
{
dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
<< dendl;
int r = 0;
#ifdef HAVE_LIBAIO
if (aio && dio) {
_aio_log_start(ioc, off, len);
ioc->pending_aios.push_back(aio_t(ioc, fd_direct));
++ioc->num_pending;
aio_t& aio = ioc->pending_aios.back();
aio.pread(off, len);
dout(30) << aio << dendl;
pbl->append(aio.bl);
dout(5) << __func__ << " 0x" << std::hex << off << "~" << len
<< std::dec << " aio " << &aio << dendl;
} else
#endif
{
r = read(off, len, pbl, ioc, false);
}
return r;
}
int KernelDevice::direct_read_unaligned(uint64_t off, uint64_t len, char *buf)
{
uint64_t aligned_off = align_down(off, block_size);
uint64_t aligned_len = align_up(off+len, block_size) - aligned_off;
bufferptr p = buffer::create_page_aligned(aligned_len);
int r = 0;
r = ::pread(fd_direct, p.c_str(), aligned_len, aligned_off);
if (r < 0) {
r = -errno;
derr << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
<< " error: " << cpp_strerror(r) << dendl;
goto out;
}
assert((uint64_t)r == aligned_len);
memcpy(buf, p.c_str() + (off - aligned_off), len);
dout(40) << __func__ << " data: ";
bufferlist bl;
bl.append(buf, len);
bl.hexdump(*_dout);
*_dout << dendl;
out:
return r < 0 ? r : 0;
}
int KernelDevice::read_random(uint64_t off, uint64_t len, char *buf,
bool buffered)
{
dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
<< dendl;
assert(len > 0);
assert(off < size);
assert(off + len <= size);
int r = 0;
//if it's direct io and unaligned, we have to use a internal buffer
if (!buffered && ((off % block_size != 0)
|| (len % block_size != 0)
|| (uintptr_t(buf) % CEPH_PAGE_SIZE != 0)))
return direct_read_unaligned(off, len, buf);
if (buffered) {
//buffered read
char *t = buf;
uint64_t left = len;
while (left > 0) {
r = ::pread(fd_buffered, t, left, off);
if (r < 0) {
r = -errno;
derr << __func__ << " 0x" << std::hex << off << "~" << left
<< std::dec << " error: " << cpp_strerror(r) << dendl;
goto out;
}
off += r;
t += r;
left -= r;
}
} else {
//direct and aligned read
r = ::pread(fd_direct, buf, len, off);
if (r < 0) {
r = -errno;
derr << __func__ << " direct_aligned_read" << " 0x" << std::hex
<< off << "~" << left << std::dec << " error: " << cpp_strerror(r)
<< dendl;
goto out;
}
assert((uint64_t)r == len);
}
dout(40) << __func__ << " data: ";
bufferlist bl;
bl.append(buf, len);
bl.hexdump(*_dout);
*_dout << dendl;
out:
return r < 0 ? r : 0;
}
int KernelDevice::invalidate_cache(uint64_t off, uint64_t len)
{
dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
<< dendl;
assert(off % block_size == 0);
assert(len % block_size == 0);
int r = posix_fadvise(fd_buffered, off, len, POSIX_FADV_DONTNEED);
if (r) {
r = -r;
derr << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
<< " error: " << cpp_strerror(r) << dendl;
}
return r;
}