1 #include "ceph_debug.h"
5 #include <linux/random.h>
6 #include <linux/slab.h>
7 #include <linux/types.h>
10 #include "messenger.h"
17 * choose a random mds that is "up" (i.e. has a state > 0), or -1.
19 int ceph_mdsmap_get_random_mds(struct ceph_mdsmap
*m
)
26 for (i
= 0; i
< m
->m_max_mds
; i
++)
27 if (m
->m_info
[i
].state
> 0)
33 get_random_bytes(&r
, 1);
36 for (i
= 0; n
> 0; i
++, n
--)
37 while (m
->m_info
[i
].state
<= 0)
46 * Ignore any fields we don't care about (there are quite a few of
49 struct ceph_mdsmap
*ceph_mdsmap_decode(void **p
, void *end
)
51 struct ceph_mdsmap
*m
;
52 const void *start
= *p
;
57 m
= kzalloc(sizeof(*m
), GFP_NOFS
);
59 return ERR_PTR(-ENOMEM
);
61 ceph_decode_16_safe(p
, end
, version
, bad
);
63 ceph_decode_need(p
, end
, 8*sizeof(u32
) + sizeof(u64
), bad
);
64 m
->m_epoch
= ceph_decode_32(p
);
65 m
->m_client_epoch
= ceph_decode_32(p
);
66 m
->m_last_failure
= ceph_decode_32(p
);
67 m
->m_root
= ceph_decode_32(p
);
68 m
->m_session_timeout
= ceph_decode_32(p
);
69 m
->m_session_autoclose
= ceph_decode_32(p
);
70 m
->m_max_file_size
= ceph_decode_64(p
);
71 m
->m_max_mds
= ceph_decode_32(p
);
73 m
->m_info
= kcalloc(m
->m_max_mds
, sizeof(*m
->m_info
), GFP_NOFS
);
74 if (m
->m_info
== NULL
)
77 /* pick out active nodes from mds_info (state > 0) */
78 n
= ceph_decode_32(p
);
79 for (i
= 0; i
< n
; i
++) {
85 struct ceph_entity_addr addr
;
86 u32 num_export_targets
;
87 void *pexport_targets
= NULL
;
89 ceph_decode_need(p
, end
, sizeof(u64
)*2 + 1 + sizeof(u32
), bad
);
90 global_id
= ceph_decode_64(p
);
91 infoversion
= ceph_decode_8(p
);
93 namelen
= ceph_decode_32(p
); /* skip mds name */
96 ceph_decode_need(p
, end
,
97 4*sizeof(u32
) + sizeof(u64
) +
98 sizeof(addr
) + sizeof(struct ceph_timespec
),
100 mds
= ceph_decode_32(p
);
101 inc
= ceph_decode_32(p
);
102 state
= ceph_decode_32(p
);
103 state_seq
= ceph_decode_64(p
);
104 ceph_decode_copy(p
, &addr
, sizeof(addr
));
105 ceph_decode_addr(&addr
);
106 *p
+= sizeof(struct ceph_timespec
);
108 ceph_decode_32_safe(p
, end
, namelen
, bad
);
110 if (infoversion
>= 2) {
111 ceph_decode_32_safe(p
, end
, num_export_targets
, bad
);
112 pexport_targets
= *p
;
113 *p
+= num_export_targets
* sizeof(u32
);
115 num_export_targets
= 0;
118 dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n",
119 i
+1, n
, global_id
, mds
, inc
, pr_addr(&addr
.in_addr
),
120 ceph_mds_state_name(state
));
121 if (mds
>= 0 && mds
< m
->m_max_mds
&& state
> 0) {
122 m
->m_info
[mds
].global_id
= global_id
;
123 m
->m_info
[mds
].state
= state
;
124 m
->m_info
[mds
].addr
= addr
;
125 m
->m_info
[mds
].num_export_targets
= num_export_targets
;
126 if (num_export_targets
) {
127 m
->m_info
[mds
].export_targets
=
128 kcalloc(num_export_targets
, sizeof(u32
),
130 for (j
= 0; j
< num_export_targets
; j
++)
131 m
->m_info
[mds
].export_targets
[j
] =
132 ceph_decode_32(&pexport_targets
);
134 m
->m_info
[mds
].export_targets
= NULL
;
140 ceph_decode_32_safe(p
, end
, n
, bad
);
141 m
->m_num_data_pg_pools
= n
;
142 m
->m_data_pg_pools
= kcalloc(n
, sizeof(u32
), GFP_NOFS
);
143 if (!m
->m_data_pg_pools
)
145 ceph_decode_need(p
, end
, sizeof(u32
)*(n
+1), bad
);
146 for (i
= 0; i
< n
; i
++)
147 m
->m_data_pg_pools
[i
] = ceph_decode_32(p
);
148 m
->m_cas_pg_pool
= ceph_decode_32(p
);
150 /* ok, we don't care about the rest. */
151 dout("mdsmap_decode success epoch %u\n", m
->m_epoch
);
157 pr_err("corrupt mdsmap\n");
158 print_hex_dump(KERN_DEBUG
, "mdsmap: ",
159 DUMP_PREFIX_OFFSET
, 16, 1,
160 start
, end
- start
, true);
161 ceph_mdsmap_destroy(m
);
162 return ERR_PTR(-EINVAL
);
165 void ceph_mdsmap_destroy(struct ceph_mdsmap
*m
)
169 for (i
= 0; i
< m
->m_max_mds
; i
++)
170 kfree(m
->m_info
[i
].export_targets
);
172 kfree(m
->m_data_pg_pools
);