diff --git a/src/blk/kernel/KernelDevice.cc b/src/blk/kernel/KernelDevice.cc index 0aa4dd01d30..58142b3c106 100644 --- a/src/blk/kernel/KernelDevice.cc +++ b/src/blk/kernel/KernelDevice.cc @@ -85,25 +85,36 @@ KernelDevice::KernelDevice(CephContext* cct, aio_callback_t cb, void *cbpriv, ai int KernelDevice::_lock() { dout(10) << __func__ << " " << fd_directs[WRITE_LIFE_NOT_SET] << dendl; - utime_t sleeptime; - sleeptime.set_from_double(cct->_conf->bdev_flock_retry_interval); - // When the block changes, systemd-udevd will open the block, // read some information and close it. Then a failure occurs here. // So we need to try again here. - for (int i = 0; i < cct->_conf->bdev_flock_retry + 1; i++) { - int r = ::flock(fd_directs[WRITE_LIFE_NOT_SET], LOCK_EX | LOCK_NB); - if (r < 0 && errno == EAGAIN) { - dout(1) << __func__ << " flock busy on " << path << dendl; - sleeptime.sleep(); - } else if (r < 0) { - derr << __func__ << " flock failed on " << path << dendl; - break; - } else { + int fd = fd_directs[WRITE_LIFE_NOT_SET]; + uint64_t nr_tries = 0; + for (;;) { + struct flock fl = { F_WRLCK, + SEEK_SET }; + int r = ::fcntl(fd, F_OFD_SETLK, &fl); + if (r < 0) { + if (errno == EINVAL) { + r = ::flock(fd, LOCK_EX | LOCK_NB); + } + } + if (r == 0) { return 0; } + if (errno != EAGAIN) { + return -errno; + } + dout(1) << __func__ << " flock busy on " << path << dendl; + if (const uint64_t max_retry = + cct->_conf.get_val("bdev_flock_retry"); + max_retry > 0 && nr_tries++ == max_retry) { + return -EAGAIN; + } + double retry_interval = + cct->_conf.get_val("bdev_flock_retry_interval"); + std::this_thread::sleep_for(ceph::make_timespan(retry_interval)); } - return -errno; } int KernelDevice::open(const string& p) diff --git a/src/common/legacy_config_opts.h b/src/common/legacy_config_opts.h index ea16bc85cd7..32e6838f68f 100644 --- a/src/common/legacy_config_opts.h +++ b/src/common/legacy_config_opts.h @@ -897,8 +897,6 @@ OPTION(bdev_debug_aio_log_age, OPT_DOUBLE) OPTION(bdev_nvme_unbind_from_kernel, OPT_BOOL) OPTION(bdev_enable_discard, OPT_BOOL) OPTION(bdev_async_discard, OPT_BOOL) -OPTION(bdev_flock_retry_interval, OPT_FLOAT) -OPTION(bdev_flock_retry, OPT_INT) OPTION(objectstore_blackhole, OPT_BOOL) diff --git a/src/common/options.cc b/src/common/options.cc index 286d5bf7719..5ff30939968 100644 --- a/src/common/options.cc +++ b/src/common/options.cc @@ -4070,9 +4070,13 @@ std::vector