Skip to content

Commit 757856d

Browse files
committed
libceph: enable ceph in a non-default network namespace
Grab a reference on a network namespace of the 'rbd map' (in case of rbd) or 'mount' (in case of ceph) process and use that to open sockets instead of always using init_net and bailing if network namespace is anything but init_net. Be careful to not share struct ceph_client instances between different namespaces and don't add any code in the !CONFIG_NET_NS case. This is based on a patch from Hong Zhiguo <zhiguohong@tencent.com>. Signed-off-by: Ilya Dryomov <idryomov@gmail.com> Reviewed-by: Sage Weil <sage@redhat.com>
1 parent d770e55 commit 757856d

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

‎include/linux/ceph/messenger.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/radix-tree.h>
99
#include <linux/uio.h>
1010
#include <linux/workqueue.h>
11+
#include <net/net_namespace.h>
1112

1213
#include <linux/ceph/types.h>
1314
#include <linux/ceph/buffer.h>
@@ -56,6 +57,7 @@ struct ceph_messenger {
5657
struct ceph_entity_addr my_enc_addr;
5758

5859
atomic_t stopping;
60+
possible_net_t net;
5961
bool nocrc;
6062
bool tcp_nodelay;
6163

@@ -267,6 +269,7 @@ extern void ceph_messenger_init(struct ceph_messenger *msgr,
267269
u64 required_features,
268270
bool nocrc,
269271
bool tcp_nodelay);
272+
extern void ceph_messenger_fini(struct ceph_messenger *msgr);
270273

271274
extern void ceph_con_init(struct ceph_connection *con, void *private,
272275
const struct ceph_connection_operations *ops,

‎net/ceph/ceph_common.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
#include <keys/ceph-type.h>
1010
#include <linux/module.h>
1111
#include <linux/mount.h>
12+
#include <linux/nsproxy.h>
1213
#include <linux/parser.h>
1314
#include <linux/sched.h>
1415
#include <linux/seq_file.h>
1516
#include <linux/slab.h>
1617
#include <linux/statfs.h>
1718
#include <linux/string.h>
1819
#include <linux/vmalloc.h>
19-
#include <linux/nsproxy.h>
20-
#include <net/net_namespace.h>
2120

2221

2322
#include <linux/ceph/ceph_features.h>
@@ -131,6 +130,13 @@ int ceph_compare_options(struct ceph_options *new_opt,
131130
int i;
132131
int ret;
133132

133+
/*
134+
* Don't bother comparing options if network namespaces don't
135+
* match.
136+
*/
137+
if (!net_eq(current->nsproxy->net_ns, read_pnet(&client->msgr.net)))
138+
return -1;
139+
134140
ret = memcmp(opt1, opt2, ofs);
135141
if (ret)
136142
return ret;
@@ -335,9 +341,6 @@ ceph_parse_options(char *options, const char *dev_name,
335341
int err = -ENOMEM;
336342
substring_t argstr[MAX_OPT_ARGS];
337343

338-
if (current->nsproxy->net_ns != &init_net)
339-
return ERR_PTR(-EINVAL);
340-
341344
opt = kzalloc(sizeof(*opt), GFP_KERNEL);
342345
if (!opt)
343346
return ERR_PTR(-ENOMEM);
@@ -608,6 +611,7 @@ struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private,
608611
fail_monc:
609612
ceph_monc_stop(&client->monc);
610613
fail:
614+
ceph_messenger_fini(&client->msgr);
611615
kfree(client);
612616
return ERR_PTR(err);
613617
}
@@ -621,8 +625,8 @@ void ceph_destroy_client(struct ceph_client *client)
621625

622626
/* unmount */
623627
ceph_osdc_stop(&client->osdc);
624-
625628
ceph_monc_stop(&client->monc);
629+
ceph_messenger_fini(&client->msgr);
626630

627631
ceph_debugfs_client_cleanup(client);
628632

‎net/ceph/messenger.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/inet.h>
77
#include <linux/kthread.h>
88
#include <linux/net.h>
9+
#include <linux/nsproxy.h>
910
#include <linux/slab.h>
1011
#include <linux/socket.h>
1112
#include <linux/string.h>
@@ -479,7 +480,7 @@ static int ceph_tcp_connect(struct ceph_connection *con)
479480
int ret;
480481

481482
BUG_ON(con->sock);
482-
ret = sock_create_kern(&init_net, con->peer_addr.in_addr.ss_family,
483+
ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
483484
SOCK_STREAM, IPPROTO_TCP, &sock);
484485
if (ret)
485486
return ret;
@@ -2944,11 +2945,18 @@ void ceph_messenger_init(struct ceph_messenger *msgr,
29442945
msgr->tcp_nodelay = tcp_nodelay;
29452946

29462947
atomic_set(&msgr->stopping, 0);
2948+
write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
29472949

29482950
dout("%s %p\n", __func__, msgr);
29492951
}
29502952
EXPORT_SYMBOL(ceph_messenger_init);
29512953

2954+
void ceph_messenger_fini(struct ceph_messenger *msgr)
2955+
{
2956+
put_net(read_pnet(&msgr->net));
2957+
}
2958+
EXPORT_SYMBOL(ceph_messenger_fini);
2959+
29522960
static void clear_standby(struct ceph_connection *con)
29532961
{
29542962
/* come back from STANDBY? */

0 commit comments

Comments
 (0)