]> git.proxmox.com Git - ceph.git/blame - ceph/src/os/bluestore/KernelDevice.cc
update sources to v12.2.3
[ceph.git] / ceph / src / os / bluestore / KernelDevice.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2014 Red Hat
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#include <unistd.h>
16#include <stdlib.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20
21#include "KernelDevice.h"
22#include "include/types.h"
23#include "include/compat.h"
24#include "include/stringify.h"
25#include "common/errno.h"
26#include "common/debug.h"
27#include "common/blkdev.h"
28#include "common/align.h"
29#include "common/blkdev.h"
30
31#define dout_context cct
32#define dout_subsys ceph_subsys_bdev
33#undef dout_prefix
34#define dout_prefix *_dout << "bdev(" << this << " " << path << ") "
35
36KernelDevice::KernelDevice(CephContext* cct, aio_callback_t cb, void *cbpriv)
37 : BlockDevice(cct),
38 fd_direct(-1),
39 fd_buffered(-1),
40 size(0), block_size(0),
41 fs(NULL), aio(false), dio(false),
42 debug_lock("KernelDevice::debug_lock"),
43 aio_queue(cct->_conf->bdev_aio_max_queue_depth),
44 aio_callback(cb),
45 aio_callback_priv(cbpriv),
46 aio_stop(false),
47 aio_thread(this),
48 injecting_crash(0)
49{
50}
51
52int KernelDevice::_lock()
53{
54 struct flock l;
55 memset(&l, 0, sizeof(l));
56 l.l_type = F_WRLCK;
57 l.l_whence = SEEK_SET;
58 int r = ::fcntl(fd_direct, F_SETLK, &l);
59 if (r < 0)
60 return -errno;
61 return 0;
62}
63
64int KernelDevice::open(const string& p)
65{
66 path = p;
67 int r = 0;
68 dout(1) << __func__ << " path " << path << dendl;
69
70 fd_direct = ::open(path.c_str(), O_RDWR | O_DIRECT);
71 if (fd_direct < 0) {
72 r = -errno;
73 derr << __func__ << " open got: " << cpp_strerror(r) << dendl;
74 return r;
75 }
76 fd_buffered = ::open(path.c_str(), O_RDWR);
77 if (fd_buffered < 0) {
78 r = -errno;
79 derr << __func__ << " open got: " << cpp_strerror(r) << dendl;
80 goto out_direct;
81 }
82 dio = true;
83 aio = cct->_conf->bdev_aio;
84 if (!aio) {
85 assert(0 == "non-aio not supported");
86 }
87
88 // disable readahead as it will wreak havoc on our mix of
89 // directio/aio and buffered io.
90 r = posix_fadvise(fd_buffered, 0, 0, POSIX_FADV_RANDOM);
91 if (r) {
92 r = -r;
93 derr << __func__ << " open got: " << cpp_strerror(r) << dendl;
94 goto out_fail;
95 }
96
97 r = _lock();
98 if (r < 0) {
99 derr << __func__ << " failed to lock " << path << ": " << cpp_strerror(r)
100 << dendl;
101 goto out_fail;
102 }
103
104 struct stat st;
105 r = ::fstat(fd_direct, &st);
106 if (r < 0) {
107 r = -errno;
108 derr << __func__ << " fstat got " << cpp_strerror(r) << dendl;
109 goto out_fail;
110 }
111
112 // Operate as though the block size is 4 KB. The backing file
113 // blksize doesn't strictly matter except that some file systems may
114 // require a read/modify/write if we write something smaller than
115 // it.
116 block_size = cct->_conf->bdev_block_size;
117 if (block_size != (unsigned)st.st_blksize) {
118 dout(1) << __func__ << " backing device/file reports st_blksize "
119 << st.st_blksize << ", using bdev_block_size "
120 << block_size << " anyway" << dendl;
121 }
122
123 if (S_ISBLK(st.st_mode)) {
124 int64_t s;
125 r = get_block_device_size(fd_direct, &s);
126 if (r < 0) {
127 goto out_fail;
128 }
129 size = s;
130 } else {
131 size = st.st_size;
132 }
3efd9988
FG
133 if (cct->_conf->get_val<bool>("bdev_inject_bad_size")) {
134 derr << "injecting bad size; actual 0x" << std::hex << size
135 << " but using 0x" << (size & ~block_size) << std::dec << dendl;
136 size &= ~(block_size);
137 }
7c673cae
FG
138
139 {
140 char partition[PATH_MAX], devname[PATH_MAX];
141 r = get_device_by_fd(fd_buffered, partition, devname, sizeof(devname));
142 if (r < 0) {
143 derr << "unable to get device name for " << path << ": "
144 << cpp_strerror(r) << dendl;
145 rotational = true;
146 } else {
147 dout(20) << __func__ << " devname " << devname << dendl;
148 rotational = block_device_is_rotational(devname);
149 }
150 }
151
31f18b77
FG
152 r = _aio_start();
153 if (r < 0) {
154 goto out_fail;
155 }
156
7c673cae
FG
157 fs = FS::create_by_fd(fd_direct);
158 assert(fs);
159
160 // round size down to an even block
161 size &= ~(block_size - 1);
162
7c673cae
FG
163 dout(1) << __func__
164 << " size " << size
165 << " (0x" << std::hex << size << std::dec << ", "
166 << pretty_si_t(size) << "B)"
167 << " block_size " << block_size
168 << " (" << pretty_si_t(block_size) << "B)"
169 << " " << (rotational ? "rotational" : "non-rotational")
170 << dendl;
171 return 0;
172
173 out_fail:
174 VOID_TEMP_FAILURE_RETRY(::close(fd_buffered));
175 fd_buffered = -1;
176 out_direct:
177 VOID_TEMP_FAILURE_RETRY(::close(fd_direct));
178 fd_direct = -1;
179 return r;
180}
181
182void KernelDevice::close()
183{
184 dout(1) << __func__ << dendl;
185 _aio_stop();
186
187 assert(fs);
188 delete fs;
189 fs = NULL;
190
191 assert(fd_direct >= 0);
192 VOID_TEMP_FAILURE_RETRY(::close(fd_direct));
193 fd_direct = -1;
194
195 assert(fd_buffered >= 0);
196 VOID_TEMP_FAILURE_RETRY(::close(fd_buffered));
197 fd_buffered = -1;
198
199 path.clear();
200}
201
202static string get_dev_property(const char *dev, const char *property)
203{
204 char val[1024] = {0};
205 get_block_device_string_property(dev, property, val, sizeof(val));
206 return val;
207}
208
209int KernelDevice::collect_metadata(string prefix, map<string,string> *pm) const
210{
211 (*pm)[prefix + "rotational"] = stringify((int)(bool)rotational);
212 (*pm)[prefix + "size"] = stringify(get_size());
213 (*pm)[prefix + "block_size"] = stringify(get_block_size());
214 (*pm)[prefix + "driver"] = "KernelDevice";
215 if (rotational) {
216 (*pm)[prefix + "type"] = "hdd";
217 } else {
218 (*pm)[prefix + "type"] = "ssd";
219 }
220
221 struct stat st;
222 int r = ::fstat(fd_buffered, &st);
223 if (r < 0)
224 return -errno;
225 if (S_ISBLK(st.st_mode)) {
226 (*pm)[prefix + "access_mode"] = "blk";
227 char partition_path[PATH_MAX];
228 char dev_node[PATH_MAX];
229 int rc = get_device_by_fd(fd_buffered, partition_path, dev_node, PATH_MAX);
230 switch (rc) {
231 case -EOPNOTSUPP:
232 case -EINVAL:
233 (*pm)[prefix + "partition_path"] = "unknown";
234 (*pm)[prefix + "dev_node"] = "unknown";
235 break;
236 case -ENODEV:
237 (*pm)[prefix + "partition_path"] = string(partition_path);
238 (*pm)[prefix + "dev_node"] = "unknown";
239 break;
240 default:
241 {
242 (*pm)[prefix + "partition_path"] = string(partition_path);
243 (*pm)[prefix + "dev_node"] = string(dev_node);
244 (*pm)[prefix + "model"] = get_dev_property(dev_node, "device/model");
245 (*pm)[prefix + "dev"] = get_dev_property(dev_node, "dev");
246
247 // nvme exposes a serial number
248 string serial = get_dev_property(dev_node, "device/serial");
249 if (serial.length()) {
250 (*pm)[prefix + "serial"] = serial;
251 }
252
253 // nvme has a device/device/* structure; infer from that. there
254 // is probably a better way?
255 string nvme_vendor = get_dev_property(dev_node, "device/device/vendor");
256 if (nvme_vendor.length()) {
257 (*pm)[prefix + "type"] = "nvme";
258 }
259 }
260 }
261 } else {
262 (*pm)[prefix + "access_mode"] = "file";
263 (*pm)[prefix + "path"] = path;
264 }
265 return 0;
266}
267
268int KernelDevice::flush()
269{
31f18b77 270 // protect flush with a mutex. note that we are not really protecting
7c673cae
FG
271 // data here. instead, we're ensuring that if any flush() caller
272 // sees that io_since_flush is true, they block any racing callers
273 // until the flush is observed. that allows racing threads to be
274 // calling flush while still ensuring that *any* of them that got an
275 // aio completion notification will not return before that aio is
276 // stable on disk: whichever thread sees the flag first will block
277 // followers until the aio is stable.
278 std::lock_guard<std::mutex> l(flush_mutex);
279
280 bool expect = true;
281 if (!io_since_flush.compare_exchange_strong(expect, false)) {
282 dout(10) << __func__ << " no-op (no ios since last flush), flag is "
283 << (int)io_since_flush.load() << dendl;
284 return 0;
285 }
286
287 dout(10) << __func__ << " start" << dendl;
288 if (cct->_conf->bdev_inject_crash) {
289 ++injecting_crash;
290 // sleep for a moment to give other threads a chance to submit or
291 // wait on io that races with a flush.
292 derr << __func__ << " injecting crash. first we sleep..." << dendl;
293 sleep(cct->_conf->bdev_inject_crash_flush_delay);
294 derr << __func__ << " and now we die" << dendl;
295 cct->_log->flush();
296 _exit(1);
297 }
298 utime_t start = ceph_clock_now();
299 int r = ::fdatasync(fd_direct);
300 utime_t end = ceph_clock_now();
301 utime_t dur = end - start;
302 if (r < 0) {
303 r = -errno;
304 derr << __func__ << " fdatasync got: " << cpp_strerror(r) << dendl;
305 ceph_abort();
306 }
307 dout(5) << __func__ << " in " << dur << dendl;;
308 return r;
309}
310
311int KernelDevice::_aio_start()
312{
313 if (aio) {
314 dout(10) << __func__ << dendl;
315 int r = aio_queue.init();
316 if (r < 0) {
31f18b77
FG
317 if (r == -EAGAIN) {
318 derr << __func__ << " io_setup(2) failed with EAGAIN; "
319 << "try increasing /proc/sys/fs/aio-max-nr" << dendl;
320 } else {
321 derr << __func__ << " io_setup(2) failed: " << cpp_strerror(r) << dendl;
322 }
7c673cae
FG
323 return r;
324 }
325 aio_thread.create("bstore_aio");
326 }
327 return 0;
328}
329
330void KernelDevice::_aio_stop()
331{
332 if (aio) {
333 dout(10) << __func__ << dendl;
334 aio_stop = true;
335 aio_thread.join();
336 aio_stop = false;
337 aio_queue.shutdown();
338 }
339}
340
341void KernelDevice::_aio_thread()
342{
343 dout(10) << __func__ << " start" << dendl;
344 int inject_crash_count = 0;
345 while (!aio_stop) {
346 dout(40) << __func__ << " polling" << dendl;
224ce89b 347 int max = cct->_conf->bdev_aio_reap_max;
7c673cae
FG
348 aio_t *aio[max];
349 int r = aio_queue.get_next_completed(cct->_conf->bdev_aio_poll_ms,
350 aio, max);
351 if (r < 0) {
352 derr << __func__ << " got " << cpp_strerror(r) << dendl;
b32b8144 353 assert(0 == "got unexpected error from io_getevents");
7c673cae
FG
354 }
355 if (r > 0) {
356 dout(30) << __func__ << " got " << r << " completed aios" << dendl;
357 for (int i = 0; i < r; ++i) {
358 IOContext *ioc = static_cast<IOContext*>(aio[i]->priv);
359 _aio_log_finish(ioc, aio[i]->offset, aio[i]->length);
360 if (aio[i]->queue_item.is_linked()) {
361 std::lock_guard<std::mutex> l(debug_queue_lock);
362 debug_aio_unlink(*aio[i]);
363 }
364
365 // set flag indicating new ios have completed. we do this *before*
366 // any completion or notifications so that any user flush() that
367 // follows the observed io completion will include this io. Note
368 // that an earlier, racing flush() could observe and clear this
369 // flag, but that also ensures that the IO will be stable before the
370 // later flush() occurs.
371 io_since_flush.store(true);
372
373 int r = aio[i]->get_return_value();
b32b8144
FG
374 if (r < 0) {
375 derr << __func__ << " got " << cpp_strerror(r) << dendl;
376 if (ioc->allow_eio && r == -EIO) {
377 ioc->set_return_value(r);
378 } else {
379 assert(0 == "got unexpected error from io_getevents");
380 }
381 } else if (aio[i]->length != (uint64_t)r) {
382 derr << "aio to " << aio[i]->offset << "~" << aio[i]->length
383 << " but returned: " << r << dendl;
384 assert(0 == "unexpected aio error");
385 }
386
387 dout(10) << __func__ << " finished aio " << aio[i] << " r " << r
388 << " ioc " << ioc
389 << " with " << (ioc->num_running.load() - 1)
390 << " aios left" << dendl;
7c673cae
FG
391
392 // NOTE: once num_running and we either call the callback or
393 // call aio_wake we cannot touch ioc or aio[] as the caller
394 // may free it.
395 if (ioc->priv) {
396 if (--ioc->num_running == 0) {
397 aio_callback(aio_callback_priv, ioc->priv);
398 }
399 } else {
31f18b77 400 ioc->try_aio_wake();
7c673cae
FG
401 }
402 }
403 }
404 if (cct->_conf->bdev_debug_aio) {
405 utime_t now = ceph_clock_now();
406 std::lock_guard<std::mutex> l(debug_queue_lock);
407 if (debug_oldest) {
408 if (debug_stall_since == utime_t()) {
409 debug_stall_since = now;
410 } else {
411 utime_t cutoff = now;
412 cutoff -= cct->_conf->bdev_debug_aio_suicide_timeout;
413 if (debug_stall_since < cutoff) {
414 derr << __func__ << " stalled aio " << debug_oldest
415 << " since " << debug_stall_since << ", timeout is "
416 << cct->_conf->bdev_debug_aio_suicide_timeout
417 << "s, suicide" << dendl;
418 assert(0 == "stalled aio... buggy kernel or bad device?");
419 }
420 }
421 }
422 }
423 reap_ioc();
424 if (cct->_conf->bdev_inject_crash) {
425 ++inject_crash_count;
426 if (inject_crash_count * cct->_conf->bdev_aio_poll_ms / 1000 >
427 cct->_conf->bdev_inject_crash + cct->_conf->bdev_inject_crash_flush_delay) {
428 derr << __func__ << " bdev_inject_crash trigger from aio thread"
429 << dendl;
430 cct->_log->flush();
431 _exit(1);
432 }
433 }
434 }
435 reap_ioc();
436 dout(10) << __func__ << " end" << dendl;
437}
438
439void KernelDevice::_aio_log_start(
440 IOContext *ioc,
441 uint64_t offset,
442 uint64_t length)
443{
444 dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length
445 << std::dec << dendl;
446 if (cct->_conf->bdev_debug_inflight_ios) {
447 Mutex::Locker l(debug_lock);
448 if (debug_inflight.intersects(offset, length)) {
449 derr << __func__ << " inflight overlap of 0x"
450 << std::hex
451 << offset << "~" << length << std::dec
452 << " with " << debug_inflight << dendl;
453 ceph_abort();
454 }
455 debug_inflight.insert(offset, length);
456 }
457}
458
459void KernelDevice::debug_aio_link(aio_t& aio)
460{
461 if (debug_queue.empty()) {
462 debug_oldest = &aio;
463 }
464 debug_queue.push_back(aio);
465}
466
467void KernelDevice::debug_aio_unlink(aio_t& aio)
468{
469 if (aio.queue_item.is_linked()) {
470 debug_queue.erase(debug_queue.iterator_to(aio));
471 if (debug_oldest == &aio) {
472 if (debug_queue.empty()) {
473 debug_oldest = nullptr;
474 } else {
475 debug_oldest = &debug_queue.front();
476 }
477 debug_stall_since = utime_t();
478 }
479 }
480}
481
482void KernelDevice::_aio_log_finish(
483 IOContext *ioc,
484 uint64_t offset,
485 uint64_t length)
486{
487 dout(20) << __func__ << " " << aio << " 0x"
488 << std::hex << offset << "~" << length << std::dec << dendl;
489 if (cct->_conf->bdev_debug_inflight_ios) {
490 Mutex::Locker l(debug_lock);
491 debug_inflight.erase(offset, length);
492 }
493}
494
495void KernelDevice::aio_submit(IOContext *ioc)
496{
497 dout(20) << __func__ << " ioc " << ioc
498 << " pending " << ioc->num_pending.load()
499 << " running " << ioc->num_running.load()
500 << dendl;
224ce89b 501
7c673cae
FG
502 if (ioc->num_pending.load() == 0) {
503 return;
504 }
224ce89b 505
7c673cae
FG
506 // move these aside, and get our end iterator position now, as the
507 // aios might complete as soon as they are submitted and queue more
508 // wal aio's.
509 list<aio_t>::iterator e = ioc->running_aios.begin();
510 ioc->running_aios.splice(e, ioc->pending_aios);
7c673cae
FG
511
512 int pending = ioc->num_pending.load();
513 ioc->num_running += pending;
514 ioc->num_pending -= pending;
515 assert(ioc->num_pending.load() == 0); // we should be only thread doing this
224ce89b
WB
516 assert(ioc->pending_aios.size() == 0);
517
518 if (cct->_conf->bdev_debug_aio) {
519 list<aio_t>::iterator p = ioc->running_aios.begin();
520 while (p != e) {
521 for (auto& io : p->iov)
522 dout(30) << __func__ << " iov " << (void*)io.iov_base
523 << " len " << io.iov_len << dendl;
7c673cae 524
7c673cae 525 std::lock_guard<std::mutex> l(debug_queue_lock);
224ce89b 526 debug_aio_link(*p++);
7c673cae
FG
527 }
528 }
224ce89b
WB
529
530 void *priv = static_cast<void*>(ioc);
531 int r, retries = 0;
532 r = aio_queue.submit_batch(ioc->running_aios.begin(), e,
533 ioc->num_running.load(), priv, &retries);
534
535 if (retries)
536 derr << __func__ << " retries " << retries << dendl;
537 if (r < 0) {
538 derr << " aio submit got " << cpp_strerror(r) << dendl;
539 assert(r == 0);
540 }
7c673cae
FG
541}
542
543int KernelDevice::_sync_write(uint64_t off, bufferlist &bl, bool buffered)
544{
545 uint64_t len = bl.length();
546 dout(5) << __func__ << " 0x" << std::hex << off << "~" << len
547 << std::dec << " buffered" << dendl;
548 if (cct->_conf->bdev_inject_crash &&
549 rand() % cct->_conf->bdev_inject_crash == 0) {
550 derr << __func__ << " bdev_inject_crash: dropping io 0x" << std::hex
551 << off << "~" << len << std::dec << dendl;
552 ++injecting_crash;
553 return 0;
554 }
555 vector<iovec> iov;
556 bl.prepare_iov(&iov);
557 int r = ::pwritev(buffered ? fd_buffered : fd_direct,
558 &iov[0], iov.size(), off);
559
560 if (r < 0) {
561 r = -errno;
562 derr << __func__ << " pwritev error: " << cpp_strerror(r) << dendl;
563 return r;
564 }
565 if (buffered) {
566 // initiate IO (but do not wait)
567 r = ::sync_file_range(fd_buffered, off, len, SYNC_FILE_RANGE_WRITE);
568 if (r < 0) {
569 r = -errno;
570 derr << __func__ << " sync_file_range error: " << cpp_strerror(r) << dendl;
571 return r;
572 }
573 }
31f18b77
FG
574
575 io_since_flush.store(true);
576
7c673cae
FG
577 return 0;
578}
579
580int KernelDevice::write(
581 uint64_t off,
582 bufferlist &bl,
583 bool buffered)
584{
585 uint64_t len = bl.length();
586 dout(20) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
587 << (buffered ? " (buffered)" : " (direct)")
588 << dendl;
589 assert(off % block_size == 0);
590 assert(len % block_size == 0);
591 assert(len > 0);
592 assert(off < size);
593 assert(off + len <= size);
594
595 if ((!buffered || bl.get_num_buffers() >= IOV_MAX) &&
b32b8144 596 bl.rebuild_aligned_size_and_memory(block_size, block_size, IOV_MAX)) {
7c673cae
FG
597 dout(20) << __func__ << " rebuilding buffer to be aligned" << dendl;
598 }
599 dout(40) << "data: ";
600 bl.hexdump(*_dout);
601 *_dout << dendl;
602
603 return _sync_write(off, bl, buffered);
604}
605
606int KernelDevice::aio_write(
607 uint64_t off,
608 bufferlist &bl,
609 IOContext *ioc,
610 bool buffered)
611{
612 uint64_t len = bl.length();
613 dout(20) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
614 << (buffered ? " (buffered)" : " (direct)")
615 << dendl;
616 assert(off % block_size == 0);
617 assert(len % block_size == 0);
618 assert(len > 0);
619 assert(off < size);
620 assert(off + len <= size);
621
622 if ((!buffered || bl.get_num_buffers() >= IOV_MAX) &&
b32b8144 623 bl.rebuild_aligned_size_and_memory(block_size, block_size, IOV_MAX)) {
7c673cae
FG
624 dout(20) << __func__ << " rebuilding buffer to be aligned" << dendl;
625 }
626 dout(40) << "data: ";
627 bl.hexdump(*_dout);
628 *_dout << dendl;
629
630 _aio_log_start(ioc, off, len);
631
632#ifdef HAVE_LIBAIO
633 if (aio && dio && !buffered) {
634 ioc->pending_aios.push_back(aio_t(ioc, fd_direct));
635 ++ioc->num_pending;
636 aio_t& aio = ioc->pending_aios.back();
637 if (cct->_conf->bdev_inject_crash &&
638 rand() % cct->_conf->bdev_inject_crash == 0) {
639 derr << __func__ << " bdev_inject_crash: dropping io 0x" << std::hex
640 << off << "~" << len << std::dec
641 << dendl;
642 // generate a real io so that aio_wait behaves properly, but make it
643 // a read instead of write, and toss the result.
644 aio.pread(off, len);
645 ++injecting_crash;
646 } else {
647 bl.prepare_iov(&aio.iov);
648 for (unsigned i=0; i<aio.iov.size(); ++i) {
649 dout(30) << "aio " << i << " " << aio.iov[i].iov_base
650 << " " << aio.iov[i].iov_len << dendl;
651 }
652 aio.bl.claim_append(bl);
653 aio.pwritev(off, len);
654 }
655 dout(5) << __func__ << " 0x" << std::hex << off << "~" << len
656 << std::dec << " aio " << &aio << dendl;
657 } else
658#endif
659 {
660 int r = _sync_write(off, bl, buffered);
661 _aio_log_finish(ioc, off, len);
662 if (r < 0)
663 return r;
664 }
665 return 0;
666}
667
668int KernelDevice::read(uint64_t off, uint64_t len, bufferlist *pbl,
669 IOContext *ioc,
670 bool buffered)
671{
672 dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
673 << (buffered ? " (buffered)" : " (direct)")
674 << dendl;
675 assert(off % block_size == 0);
676 assert(len % block_size == 0);
677 assert(len > 0);
678 assert(off < size);
679 assert(off + len <= size);
680
681 _aio_log_start(ioc, off, len);
682
683 bufferptr p = buffer::create_page_aligned(len);
684 int r = ::pread(buffered ? fd_buffered : fd_direct,
685 p.c_str(), len, off);
686 if (r < 0) {
687 r = -errno;
688 goto out;
689 }
690 assert((uint64_t)r == len);
691 pbl->push_back(std::move(p));
692
693 dout(40) << "data: ";
694 pbl->hexdump(*_dout);
695 *_dout << dendl;
696
697 out:
698 _aio_log_finish(ioc, off, len);
699 return r < 0 ? r : 0;
700}
701
702int KernelDevice::aio_read(
703 uint64_t off,
704 uint64_t len,
705 bufferlist *pbl,
706 IOContext *ioc)
707{
708 dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
709 << dendl;
710
711 int r = 0;
712#ifdef HAVE_LIBAIO
713 if (aio && dio) {
714 _aio_log_start(ioc, off, len);
715 ioc->pending_aios.push_back(aio_t(ioc, fd_direct));
716 ++ioc->num_pending;
717 aio_t& aio = ioc->pending_aios.back();
718 aio.pread(off, len);
719 for (unsigned i=0; i<aio.iov.size(); ++i) {
720 dout(30) << "aio " << i << " " << aio.iov[i].iov_base
721 << " " << aio.iov[i].iov_len << dendl;
722 }
723 pbl->append(aio.bl);
724 dout(5) << __func__ << " 0x" << std::hex << off << "~" << len
725 << std::dec << " aio " << &aio << dendl;
726 } else
727#endif
728 {
729 r = read(off, len, pbl, ioc, false);
730 }
731
732 return r;
733}
734
735int KernelDevice::direct_read_unaligned(uint64_t off, uint64_t len, char *buf)
736{
737 uint64_t aligned_off = align_down(off, block_size);
738 uint64_t aligned_len = align_up(off+len, block_size) - aligned_off;
739 bufferptr p = buffer::create_page_aligned(aligned_len);
740 int r = 0;
741
742 r = ::pread(fd_direct, p.c_str(), aligned_len, aligned_off);
743 if (r < 0) {
744 r = -errno;
745 derr << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
746 << " error: " << cpp_strerror(r) << dendl;
747 goto out;
748 }
749 assert((uint64_t)r == aligned_len);
750 memcpy(buf, p.c_str() + (off - aligned_off), len);
751
752 dout(40) << __func__ << " data: ";
753 bufferlist bl;
754 bl.append(buf, len);
755 bl.hexdump(*_dout);
756 *_dout << dendl;
757
758 out:
759 return r < 0 ? r : 0;
760}
761
762int KernelDevice::read_random(uint64_t off, uint64_t len, char *buf,
763 bool buffered)
764{
765 dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
766 << dendl;
767 assert(len > 0);
768 assert(off < size);
769 assert(off + len <= size);
770 int r = 0;
771
772 //if it's direct io and unaligned, we have to use a internal buffer
773 if (!buffered && ((off % block_size != 0)
774 || (len % block_size != 0)
775 || (uintptr_t(buf) % CEPH_PAGE_SIZE != 0)))
776 return direct_read_unaligned(off, len, buf);
777
778 if (buffered) {
779 //buffered read
780 char *t = buf;
781 uint64_t left = len;
782 while (left > 0) {
783 r = ::pread(fd_buffered, t, left, off);
784 if (r < 0) {
785 r = -errno;
786 derr << __func__ << " 0x" << std::hex << off << "~" << left
787 << std::dec << " error: " << cpp_strerror(r) << dendl;
788 goto out;
789 }
790 off += r;
791 t += r;
792 left -= r;
793 }
794 } else {
795 //direct and aligned read
796 r = ::pread(fd_direct, buf, len, off);
797 if (r < 0) {
798 r = -errno;
799 derr << __func__ << " direct_aligned_read" << " 0x" << std::hex
800 << off << "~" << left << std::dec << " error: " << cpp_strerror(r)
801 << dendl;
802 goto out;
803 }
804 assert((uint64_t)r == len);
805 }
806
807 dout(40) << __func__ << " data: ";
808 bufferlist bl;
809 bl.append(buf, len);
810 bl.hexdump(*_dout);
811 *_dout << dendl;
812
813 out:
814 return r < 0 ? r : 0;
815}
816
817int KernelDevice::invalidate_cache(uint64_t off, uint64_t len)
818{
819 dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
820 << dendl;
821 assert(off % block_size == 0);
822 assert(len % block_size == 0);
823 int r = posix_fadvise(fd_buffered, off, len, POSIX_FADV_DONTNEED);
824 if (r) {
825 r = -r;
826 derr << __func__ << " 0x" << std::hex << off << "~" << len << std::dec
827 << " error: " << cpp_strerror(r) << dendl;
828 }
829 return r;
830}
831