+static int osd_dirent_count(const struct lu_env *env, struct dt_object *dt,
+ u64 *count)
+{
+ struct osd_object *obj = osd_dt_obj(dt);
+ const struct dt_it_ops *iops;
+ struct dt_it *it;
+ int rc;
+
+ ENTRY;
+
+ LASSERT(S_ISDIR(obj->oo_inode->i_mode));
+ LASSERT(fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)));
+
+ if (obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
+ *count = obj->oo_dirent_count;
+ RETURN(0);
+ }
+
+ /* directory not initialized yet */
+ if (!dt->do_index_ops) {
+ *count = 0;
+ RETURN(0);
+ }
+
+ iops = &dt->do_index_ops->dio_it;
+ it = iops->init(env, dt, LUDA_64BITHASH);
+ if (IS_ERR(it))
+ RETURN(PTR_ERR(it));
+
+ rc = iops->load(env, it, 0);
+ if (rc < 0) {
+ if (rc == -ENODATA) {
+ rc = 0;
+ *count = 0;
+ }
+ GOTO(out, rc);
+ }
+ if (rc > 0)
+ rc = iops->next(env, it);
+
+ for (*count = 0; rc == 0 || rc == -ESTALE; rc = iops->next(env, it)) {
+ if (rc == -ESTALE)
+ continue;
+
+ if (iops->key_size(env, it) == 0)
+ continue;
+
+ (*count)++;
+ }
+ if (rc == 1) {
+ obj->oo_dirent_count = *count;
+ rc = 0;
+ }
+out:
+ iops->put(env, it);
+ iops->fini(env, it);
+
+ RETURN(rc);
+}
+