Skip to content

Commit f7b52fc

Browse files
authored
Merge pull request ceph#55287 from ajarr/wip-64139
rbd-nbd: fix resize of images mapped using netlink Reviewed-by: Ilya Dryomov <[email protected]>
2 parents a70dc48 + 1712b95 commit f7b52fc

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

qa/workunits/rbd/rbd-nbd.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,11 @@ provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} |
202202
used=`rbd -p ${POOL} --format xml du ${IMAGE} |
203203
$XMLSTARLET sel -t -m "//stats/images/image/used_size" -v .`
204204
[ "${used}" -lt "${provisioned}" ]
205+
unmap_device ${DEV} ${PID}
205206

206207
# resize test
208+
DEV=`_sudo rbd device -t nbd -o try-netlink map ${POOL}/${IMAGE}`
209+
get_pid ${POOL}
207210
devname=$(basename ${DEV})
208211
blocks=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions)
209212
test -n "${blocks}"
@@ -216,9 +219,9 @@ rbd resize ${POOL}/${IMAGE} --allow-shrink --size ${SIZE}M
216219
blocks2=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions)
217220
test -n "${blocks2}"
218221
test ${blocks2} -eq ${blocks}
222+
unmap_device ${DEV} ${PID}
219223

220224
# read-only option test
221-
unmap_device ${DEV} ${PID}
222225
DEV=`_sudo rbd --device-type nbd map --read-only ${POOL}/${IMAGE}`
223226
PID=$(rbd device --device-type nbd list | awk -v pool=${POOL} -v img=${IMAGE} -v dev=${DEV} \
224227
'$2 == pool && $3 == img && $5 == dev {print $1}')

src/tools/rbd_nbd/rbd-nbd.cc

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ static EventSocket terminate_event_sock;
194194
static int parse_args(vector<const char*>& args, std::ostream *err_msg,
195195
Config *cfg);
196196
static int netlink_disconnect(int index);
197-
static int netlink_resize(int nbd_index, uint64_t size);
197+
static int netlink_resize(int nbd_index, const std::string& cookie,
198+
uint64_t size);
198199

199200
static int run_quiesce_hook(const std::string &quiesce_hook,
200201
const std::string &devpath,
@@ -744,6 +745,7 @@ class NBDWatchCtx : public librbd::UpdateWatchCtx
744745
ceph::mutex lock = ceph::make_mutex("NBDWatchCtx::Locker");
745746
bool notify = false;
746747
bool terminated = false;
748+
std::string cookie;
747749

748750
bool wait_notify() {
749751
dout(10) << __func__ << dendl;
@@ -779,11 +781,11 @@ class NBDWatchCtx : public librbd::UpdateWatchCtx
779781
<< dendl;
780782
}
781783
if (use_netlink) {
782-
ret = netlink_resize(nbd_index, new_size);
784+
ret = netlink_resize(nbd_index, cookie, new_size);
783785
} else {
784786
ret = ioctl(fd, NBD_SET_SIZE, new_size);
785787
if (ret < 0) {
786-
derr << "resize failed: " << cpp_strerror(errno) << dendl;
788+
derr << "ioctl resize failed: " << cpp_strerror(errno) << dendl;
787789
}
788790
}
789791
if (!ret) {
@@ -805,13 +807,15 @@ class NBDWatchCtx : public librbd::UpdateWatchCtx
805807
bool _use_netlink,
806808
librados::IoCtx &_io_ctx,
807809
librbd::Image &_image,
808-
unsigned long _size)
810+
unsigned long _size,
811+
std::string _cookie)
809812
: fd(_fd)
810813
, nbd_index(_nbd_index)
811814
, use_netlink(_use_netlink)
812815
, io_ctx(_io_ctx)
813816
, image(_image)
814817
, size(_size)
818+
, cookie(std::move(_cookie))
815819
{
816820
handle_notify_thread = make_named_thread("rbd_handle_notify",
817821
&NBDWatchCtx::handle_notify_entry,
@@ -1319,38 +1323,42 @@ static int netlink_disconnect_by_path(const std::string& devpath)
13191323
return netlink_disconnect(index);
13201324
}
13211325

1322-
static int netlink_resize(int nbd_index, uint64_t size)
1326+
static int netlink_resize(int nbd_index, const std::string& cookie,
1327+
uint64_t size)
13231328
{
13241329
struct nl_sock *sock;
13251330
struct nl_msg *msg;
13261331
int nl_id, ret;
13271332

13281333
sock = netlink_init(&nl_id);
13291334
if (!sock) {
1330-
cerr << "rbd-nbd: Netlink interface not supported." << std::endl;
1331-
return 1;
1335+
derr << __func__ << ": netlink interface not supported" << dendl;
1336+
return -EINVAL;
13321337
}
13331338

13341339
nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, genl_handle_msg, NULL);
13351340

13361341
msg = nlmsg_alloc();
13371342
if (!msg) {
1338-
cerr << "rbd-nbd: Could not allocate netlink message." << std::endl;
1343+
derr << __func__ << ": could not allocate netlink message" << dendl;
13391344
goto free_sock;
13401345
}
13411346

13421347
if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl_id, 0, 0,
13431348
NBD_CMD_RECONFIGURE, 0)) {
1344-
cerr << "rbd-nbd: Could not setup message." << std::endl;
1349+
derr << __func__ << ": could not setup netlink message" << dendl;
13451350
goto free_msg;
13461351
}
13471352

13481353
NLA_PUT_U32(msg, NBD_ATTR_INDEX, nbd_index);
13491354
NLA_PUT_U64(msg, NBD_ATTR_SIZE_BYTES, size);
1355+
if (!cookie.empty())
1356+
NLA_PUT_STRING(msg, NBD_ATTR_BACKEND_IDENTIFIER, cookie.c_str());
13501357

13511358
ret = nl_send_sync(sock, msg);
13521359
if (ret < 0) {
1353-
cerr << "rbd-nbd: netlink resize failed: " << nl_geterror(ret) << std::endl;
1360+
derr << __func__ << ": netlink resize failed: " << nl_geterror(ret)
1361+
<< dendl;
13541362
goto free_sock;
13551363
}
13561364

@@ -1896,7 +1904,7 @@ static int do_map(int argc, const char *argv[], Config *cfg, bool reconnect)
18961904
uint64_t handle;
18971905

18981906
NBDWatchCtx watch_ctx(nbd, nbd_index, use_netlink, io_ctx, image,
1899-
info.size);
1907+
info.size, cfg->cookie);
19001908
r = image.update_watch(&watch_ctx, &handle);
19011909
if (r < 0)
19021910
goto close_nbd;

0 commit comments

Comments
 (0)