diff --git a/src/cls/rbd/cls_rbd.cc b/src/cls/rbd/cls_rbd.cc index 2ab186bf6aa..43c717de3fd 100644 --- a/src/cls/rbd/cls_rbd.cc +++ b/src/cls/rbd/cls_rbd.cc @@ -2630,7 +2630,7 @@ static const string metadata_name_from_key(const string &key) * Input: * @param start_after which name to begin listing after * (use the empty string to start at the beginning) - * @param max_return the maximum number of names to list(if 0 means no limit) + * @param max_return the maximum number of names to list * Output: * @param value @@ -2649,33 +2649,33 @@ int metadata_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out) return -EINVAL; } + // TODO remove implicit support for zero during the N-release + if (max_return == 0) { + max_return = RBD_MAX_KEYS_READ; + } + map data; string last_read = metadata_key_for_name(start_after); - int max_read = max_return ? MIN(RBD_MAX_KEYS_READ, max_return) : RBD_MAX_KEYS_READ; - bool more; + bool more = true; - do { + while (more && data.size() < max_return) { map raw_data; + int max_read = MIN(RBD_MAX_KEYS_READ, max_return - data.size()); int r = cls_cxx_map_get_vals(hctx, last_read, RBD_METADATA_KEY_PREFIX, max_read, &raw_data, &more); if (r < 0) { CLS_ERR("failed to read the vals off of disk: %s", cpp_strerror(r).c_str()); return r; } - if (raw_data.empty()) - break; - map::iterator it = raw_data.begin(); - for (; it != raw_data.end(); ++it) - data[metadata_name_from_key(it->first)].swap(it->second); + for (auto& kv : raw_data) { + data[metadata_name_from_key(kv.first)].swap(kv.second); + } - if (!more) - break; - - last_read = raw_data.rbegin()->first; - if (max_return) - max_read = MIN(RBD_MAX_KEYS_READ, max_return - data.size()); - } while (more); + if (!raw_data.empty()) { + last_read = raw_data.rbegin()->first; + } + } ::encode(data, *out); return 0;