Whamcloud - gitweb
LU-5855 lfsck: misc fixes for zfs-based backend
[fs/lustre-release.git] / lustre / osd-zfs / osd_index.c
index 29b0d68..efd2117 100644 (file)
 #include <sys/sa_impl.h>
 #include <sys/txg.h>
 
+static inline int osd_object_is_zap(dmu_buf_t *db)
+{
+       dmu_buf_impl_t *dbi = (dmu_buf_impl_t *) db;
+       dnode_t *dn;
+       int rc;
+
+       DB_DNODE_ENTER(dbi);
+       dn = DB_DNODE(dbi);
+       rc = (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
+                       dn->dn_type == DMU_OT_USERGROUP_USED);
+       DB_DNODE_EXIT(dbi);
+
+       return rc;
+}
+
+/* We don't actually have direct access to the zap_hashbits() function
+ * so just pretend like we do for now.  If this ever breaks we can look at
+ * it at that time. */
+#define zap_hashbits(zc) 48
+/*
+ * ZFS hash format:
+ * | cd (16 bits) | hash (48 bits) |
+ * we need it in other form:
+ * |0| hash (48 bit) | cd (15 bit) |
+ * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
+ * the readdir hashes from multiple directory stripes uniformly on the client.
+ * Another point is sign bit, the hash range should be in [0, 2^63-1] because
+ * loff_t (for llseek) needs to be a positive value.  This means the "cd" field
+ * should only be the low 15 bits.
+ */
+uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
+{
+       uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
+
+       return (zfs_hash >> zap_hashbits(zc)) |
+               (zfs_hash << (63 - zap_hashbits(zc)));
+}
+
+void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
+                                   uint64_t id, uint64_t dirhash)
+{
+       uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
+               (dirhash >> (63 - zap_hashbits(zc)));
+
+       zap_cursor_init_serialized(zc, os, id, zfs_hash);
+}
+
+int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
+                       uint64_t id, uint64_t dirhash)
+{
+       zap_cursor_t *t;
+
+       OBD_ALLOC_PTR(t);
+       if (unlikely(t == NULL))
+               return -ENOMEM;
+
+       osd_zap_cursor_init_serialized(t, os, id, dirhash);
+       *zc = t;
+
+       return 0;
+}
+
+void osd_zap_cursor_fini(zap_cursor_t *zc)
+{
+       zap_cursor_fini(zc);
+       OBD_FREE_PTR(zc);
+}
+
+static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
+                                                struct osd_object *o,
+                                                uint64_t dirhash)
+{
+       struct osd_device *d = osd_obj2dev(o);
+       zap_cursor_init_serialized(zc, d->od_os, o->oo_db->db_object, dirhash);
+}
+
+static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
+                       uint64_t dirhash)
+{
+       struct osd_device *d = osd_obj2dev(o);
+       return osd_zap_cursor_init(zc, d->od_os, o->oo_db->db_object, dirhash);
+}
+
 static struct dt_it *osd_index_it_init(const struct lu_env *env,
                                       struct dt_object *dt,
                                       __u32 unused,
@@ -75,22 +158,35 @@ static struct dt_it *osd_index_it_init(const struct lu_env *env,
        struct osd_thread_info  *info = osd_oti_get(env);
        struct osd_zap_it       *it;
        struct osd_object       *obj = osd_dt_obj(dt);
-       struct osd_device       *osd = osd_obj2dev(obj);
        struct lu_object        *lo  = &dt->do_lu;
+       int                      rc;
        ENTRY;
 
        /* XXX: check capa ? */
 
        LASSERT(lu_object_exists(lo));
        LASSERT(obj->oo_db);
-       LASSERT(udmu_object_is_zap(obj->oo_db));
+       LASSERT(osd_object_is_zap(obj->oo_db));
        LASSERT(info);
 
-       it = &info->oti_it_zap;
+       if (info->oti_it_inline) {
+               OBD_ALLOC_PTR(it);
+               if (it == NULL)
+                       RETURN(ERR_PTR(-ENOMEM));
+       } else {
+               it = &info->oti_it_zap;
+               info->oti_it_inline = 1;
+       }
 
-       if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
-                                obj->oo_db->db_object, 0))
-               RETURN(ERR_PTR(-ENOMEM));
+       rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
+       if (rc != 0) {
+               if (it != &info->oti_it_zap)
+                       OBD_FREE_PTR(it);
+               else
+                       info->oti_it_inline = 0;
+
+               RETURN(ERR_PTR(rc));
+       }
 
        it->ozi_obj   = obj;
        it->ozi_capa  = capa;
@@ -102,8 +198,9 @@ static struct dt_it *osd_index_it_init(const struct lu_env *env,
 
 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
 {
-       struct osd_zap_it *it = (struct osd_zap_it *)di;
-       struct osd_object *obj;
+       struct osd_thread_info  *info   = osd_oti_get(env);
+       struct osd_zap_it       *it     = (struct osd_zap_it *)di;
+       struct osd_object       *obj;
        ENTRY;
 
        LASSERT(it);
@@ -111,8 +208,12 @@ static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
 
        obj = it->ozi_obj;
 
-       udmu_zap_cursor_fini(it->ozi_zc);
+       osd_zap_cursor_fini(it->ozi_zc);
        lu_object_put(env, &obj->oo_dt.do_lu);
+       if (it != &info->oti_it_zap)
+               OBD_FREE_PTR(it);
+       else
+               info->oti_it_inline = 0;
 
        EXIT;
 }
@@ -124,57 +225,6 @@ static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
         *      next/finish. */
 }
 
-int udmu_zap_cursor_retrieve_key(const struct lu_env *env,
-                                zap_cursor_t *zc, char *key, int max)
-{
-       zap_attribute_t *za = &osd_oti_get(env)->oti_za;
-       int              err;
-
-       if ((err = zap_cursor_retrieve(zc, za)))
-               return err;
-
-       if (key)
-               strcpy(key, za->za_name);
-
-       return 0;
-}
-
-/*
- * zap_cursor_retrieve read from current record.
- * to read bytes we need to call zap_lookup explicitly.
- */
-int udmu_zap_cursor_retrieve_value(const struct lu_env *env,
-               zap_cursor_t *zc,  char *buf,
-               int buf_size, int *bytes_read)
-{
-       zap_attribute_t *za = &osd_oti_get(env)->oti_za;
-       int err, actual_size;
-
-       if ((err = zap_cursor_retrieve(zc, za)))
-               return err;
-
-       if (za->za_integer_length <= 0)
-               return (ERANGE);
-
-       actual_size = za->za_integer_length * za->za_num_integers;
-
-       if (actual_size > buf_size) {
-               actual_size = buf_size;
-               buf_size = actual_size / za->za_integer_length;
-       } else {
-               buf_size = za->za_num_integers;
-       }
-
-       err = -zap_lookup(zc->zc_objset, zc->zc_zapobj,
-                       za->za_name, za->za_integer_length,
-                       buf_size, buf);
-
-       if (!err)
-               *bytes_read = actual_size;
-
-       return err;
-}
-
 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
                                       int len, __u16 type)
 {
@@ -202,8 +252,8 @@ static int osd_find_parent_by_dnode(const struct lu_env *env,
                                    struct dt_object *o,
                                    struct lu_fid *fid)
 {
+       struct osd_device       *osd = osd_obj2dev(osd_dt_obj(o));
        struct lustre_mdt_attrs *lma;
-       udmu_objset_t           *uos = &osd_obj2dev(osd_dt_obj(o))->od_objset;
        struct lu_buf            buf;
        sa_handle_t             *sa_hdl;
        nvlist_t                *nvbuf = NULL;
@@ -214,19 +264,19 @@ static int osd_find_parent_by_dnode(const struct lu_env *env,
 
        /* first of all, get parent dnode from own attributes */
        LASSERT(osd_dt_obj(o)->oo_db);
-       rc = -sa_handle_get(uos->os, osd_dt_obj(o)->oo_db->db_object,
+       rc = -sa_handle_get(osd->od_os, osd_dt_obj(o)->oo_db->db_object,
                            NULL, SA_HDL_PRIVATE, &sa_hdl);
        if (rc)
                RETURN(rc);
 
        dnode = ZFS_NO_OBJECT;
-       rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(uos), &dnode, 8);
+       rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
        sa_handle_destroy(sa_hdl);
        if (rc)
                RETURN(rc);
 
        /* now get EA buffer */
-       rc = __osd_xattr_load(uos, dnode, &nvbuf);
+       rc = __osd_xattr_load(osd, dnode, &nvbuf);
        if (rc)
                GOTO(regular, rc);
 
@@ -246,12 +296,12 @@ regular:
        /* no LMA attribute in SA, let's try regular EA */
 
        /* first of all, get parent dnode storing regular EA */
-       rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
+       rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
        if (rc)
                GOTO(out, rc);
 
        dnode = ZFS_NO_OBJECT;
-       rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(uos), &dnode, 8);
+       rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &dnode, 8);
        sa_handle_destroy(sa_hdl);
        if (rc)
                GOTO(out, rc);
@@ -261,7 +311,7 @@ regular:
        buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
 
        /* now try to find LMA */
-       rc = __osd_xattr_get_large(env, uos, dnode, &buf,
+       rc = __osd_xattr_get_large(env, osd, dnode, &buf,
                                   XATTR_NAME_LMA, &size);
        if (rc == 0 && size >= sizeof(*lma)) {
                lma = buf.lb_buf;
@@ -361,7 +411,7 @@ static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
        int                 rc;
        ENTRY;
 
-       LASSERT(udmu_object_is_zap(obj->oo_db));
+       LASSERT(osd_object_is_zap(obj->oo_db));
 
        if (name[0] == '.') {
                if (name[1] == 0) {
@@ -374,7 +424,7 @@ static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
                }
        }
 
-       rc = -zap_lookup(osd->od_objset.os, obj->oo_db->db_object,
+       rc = -zap_lookup(osd->od_os, obj->oo_db->db_object,
                         (char *)key, 8, sizeof(oti->oti_zde) / 8,
                         (void *)&oti->oti_zde);
        memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
@@ -390,16 +440,20 @@ static int osd_declare_dir_insert(const struct lu_env *env,
 {
        struct osd_object  *obj = osd_dt_obj(dt);
        struct osd_thandle *oh;
+       uint64_t object;
        ENTRY;
 
        LASSERT(th != NULL);
        oh = container_of0(th, struct osd_thandle, ot_super);
 
-       LASSERT(obj->oo_db);
-       LASSERT(udmu_object_is_zap(obj->oo_db));
+       /* This is for inserting dot/dotdot for new created dir. */
+       if (obj->oo_db == NULL)
+               object = DMU_NEW_OBJECT;
+       else
+               object = obj->oo_db->db_object;
 
-       dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
-       dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
+       dmu_tx_hold_bonus(oh->ot_tx, object);
+       dmu_tx_hold_zap(oh->ot_tx, object, TRUE, (char *)key);
 
        RETURN(0);
 }
@@ -472,21 +526,17 @@ static inline void osd_object_put(const struct lu_env *env,
        lu_object_put(env, &obj->oo_dt.do_lu);
 }
 
-static int osd_mdt_seq_exists(const struct lu_env *env, struct osd_device *osd,
-                             obd_seq seq)
+static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
+                         obd_seq seq)
 {
        struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
        struct seq_server_site  *ss = osd_seq_site(osd);
        int                     rc;
        ENTRY;
 
-       if (ss == NULL)
-               RETURN(1);
+       LASSERT(ss != NULL);
+       LASSERT(ss->ss_server_fld != NULL);
 
-       /* XXX: currently, each MDT only store avaible sequence on disk,
-        * and no allocated sequences information on disk, so it has to
-        * lookup FLDB. It probably makes more sense also store allocated
-        * sequence locally, so we do not need do remote FLDB lookup in OSD */
        rc = osd_fld_lookup(env, osd, seq, range);
        if (rc != 0) {
                CERROR("%s: Can not lookup fld for "LPX64"\n",
@@ -498,15 +548,23 @@ static int osd_mdt_seq_exists(const struct lu_env *env, struct osd_device *osd,
 }
 
 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
-                         struct lu_fid *fid)
+                         const struct lu_fid *fid)
 {
+       struct seq_server_site  *ss = osd_seq_site(osd);
        ENTRY;
 
-       if (!fid_is_norm(fid) && !fid_is_root(fid))
+       /* FID seqs not in FLDB, must be local seq */
+       if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
                RETURN(0);
 
-       /* Currently, it only used to check FID on MDT */
-       if (osd_mdt_seq_exists(env, osd, fid_seq(fid)))
+       /* If FLD is not being initialized yet, it only happens during the
+        * initialization, likely during mgs initialization, and we assume
+        * this is local FID. */
+       if (ss == NULL || ss->ss_server_fld == NULL)
+               RETURN(0);
+
+       /* Only check the local FLDB here */
+       if (osd_seq_exists(env, osd, fid_seq(fid)))
                RETURN(0);
 
        RETURN(1);
@@ -533,7 +591,8 @@ static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
        struct osd_thread_info *oti = osd_oti_get(env);
        struct osd_object   *parent = osd_dt_obj(dt);
        struct osd_device   *osd = osd_obj2dev(parent);
-       struct lu_fid       *fid = (struct lu_fid *)rec;
+       struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
+       const struct lu_fid *fid = rec1->rec_fid;
        struct osd_thandle  *oh;
        struct osd_object   *child = NULL;
        __u32                attr;
@@ -542,7 +601,7 @@ static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
        ENTRY;
 
        LASSERT(parent->oo_db);
-       LASSERT(udmu_object_is_zap(parent->oo_db));
+       LASSERT(osd_object_is_zap(parent->oo_db));
 
        LASSERT(dt_object_exists(dt));
        LASSERT(osd_invariant(parent));
@@ -560,7 +619,7 @@ static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
        if (unlikely(rc == 1)) {
                /* Insert remote entry */
                memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
-               oti->oti_zde.lzd_reg.zde_type = IFTODT(S_IFDIR & S_IFMT);
+               oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
        } else {
                /*
                 * To simulate old Orion setups with ./..  stored in the
@@ -578,13 +637,25 @@ static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
                                 * during iteration */
                                GOTO(out, rc = 0);
                        } else if (name[1] == '.' && name[2] == 0) {
+                               if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
+                                       struct lu_fid tfid = *fid;
+
+                                       osd_object_put(env, child);
+                                       tfid.f_oid--;
+                                       child = osd_object_find(env, dt, &tfid);
+                                       if (IS_ERR(child))
+                                               RETURN(PTR_ERR(child));
+
+                                       LASSERT(child->oo_db);
+                               }
+
                                /* update parent dnode in the child.
                                 * later it will be used to generate ".." */
-                               udmu_objset_t *uos = &osd->od_objset;
                                rc = osd_object_sa_update(parent,
-                                                SA_ZPL_PARENT(uos),
+                                                SA_ZPL_PARENT(osd),
                                                 &child->oo_db->db_object,
                                                 8, oh);
+
                                GOTO(out, rc);
                        }
                }
@@ -597,9 +668,15 @@ static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
 
        oti->oti_zde.lzd_fid = *fid;
        /* Insert (key,oid) into ZAP */
-       rc = -zap_add(osd->od_objset.os, parent->oo_db->db_object,
+       rc = -zap_add(osd->od_os, parent->oo_db->db_object,
                      (char *)key, 8, sizeof(oti->oti_zde) / 8,
                      (void *)&oti->oti_zde, oh->ot_tx);
+       if (unlikely(rc == -EEXIST &&
+                    name[0] == '.' && name[1] == '.' && name[2] == 0))
+               /* Update (key,oid) in ZAP */
+               rc = -zap_update(osd->od_os, parent->oo_db->db_object,
+                               (char *)key, 8, sizeof(oti->oti_zde) / 8,
+                               (void *)&oti->oti_zde, oh->ot_tx);
 
 out:
        if (child != NULL)
@@ -624,7 +701,7 @@ static int osd_declare_dir_delete(const struct lu_env *env,
        oh = container_of0(th, struct osd_thandle, ot_super);
 
        LASSERT(obj->oo_db);
-       LASSERT(udmu_object_is_zap(obj->oo_db));
+       LASSERT(osd_object_is_zap(obj->oo_db));
 
        dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
 
@@ -644,7 +721,7 @@ static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
        ENTRY;
 
        LASSERT(obj->oo_db);
-       LASSERT(udmu_object_is_zap(obj->oo_db));
+       LASSERT(osd_object_is_zap(obj->oo_db));
 
        LASSERT(th != NULL);
        oh = container_of0(th, struct osd_thandle, ot_super);
@@ -662,14 +739,9 @@ static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
        }
 
        /* Remove key from the ZAP */
-       rc = -zap_remove(osd->od_objset.os, zap_db->db_object,
+       rc = -zap_remove(osd->od_os, zap_db->db_object,
                         (char *) key, oh->ot_tx);
 
-#if LUSTRE_VERSION_CODE <= OBD_OCD_VERSION(2, 4, 53, 0)
-       if (unlikely(rc == -ENOENT && name[0] == '.' &&
-           (name[1] == 0 || (name[1] == '.' && name[2] == 0))))
-               rc = 0;
-#endif
        if (unlikely(rc && rc != -ENOENT))
                CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
 
@@ -705,7 +777,6 @@ static int osd_dir_it_get(const struct lu_env *env,
 {
        struct osd_zap_it *it = (struct osd_zap_it *)di;
        struct osd_object *obj = it->ozi_obj;
-       struct osd_device *osd = osd_obj2dev(obj);
        char              *name = (char *)key;
        int                rc;
        ENTRY;
@@ -713,11 +784,9 @@ static int osd_dir_it_get(const struct lu_env *env,
        LASSERT(it);
        LASSERT(it->ozi_zc);
 
-       udmu_zap_cursor_fini(it->ozi_zc);
-
-       if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
-                                obj->oo_db->db_object, 0))
-               RETURN(-ENOMEM);
+       /* reset the cursor */
+       zap_cursor_fini(it->ozi_zc);
+       osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
 
        /* XXX: implementation of the API is broken at the moment */
        LASSERT(((const char *)key)[0] == 0);
@@ -796,6 +865,8 @@ static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
        zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
        int                rc;
 
+       ENTRY;
+
        /* temp. storage should be enough for any key supported by ZFS */
        CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
 
@@ -808,9 +879,10 @@ static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
                it->ozi_pos++;
                if (it->ozi_pos <=2)
                        RETURN(0);
-       }
 
-       zap_cursor_advance(it->ozi_zc);
+       } else {
+               zap_cursor_advance(it->ozi_zc);
+       }
 
        /*
         * According to current API we need to return error if its last entry.
@@ -846,19 +918,6 @@ static struct dt_key *osd_dir_it_key(const struct lu_env *env,
 
        strcpy(it->ozi_name, za->za_name);
 
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
-       if (za->za_name[0] == '.') {
-               if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
-                   za->za_name[2] == 0)) {
-                       /* we should not get onto . and ..
-                        * stored in the directory. ->next() and
-                        * other methods should prevent this
-                        */
-                       LBUG();
-               }
-       }
-#endif
-
        RETURN((struct dt_key *)it->ozi_name);
 }
 
@@ -879,18 +938,6 @@ static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
        if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
                rc = strlen(za->za_name);
 
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
-       if (rc == 0 && za->za_name[0] == '.') {
-               if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
-                   za->za_name[2] == 0)) {
-                       /* we should not get onto . and ..
-                        * stored in the directory. ->next() and
-                        * other methods should prevent this
-                        */
-                       LBUG();
-               }
-       }
-#endif
        RETURN(rc);
 }
 
@@ -937,11 +984,11 @@ static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
 
        LASSERT(lde);
 
-       lde->lde_hash = cpu_to_le64(udmu_zap_cursor_serialize(it->ozi_zc));
-
-       if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
+       rc = -zap_cursor_retrieve(it->ozi_zc, za);
+       if (unlikely(rc != 0))
                GOTO(out, rc);
 
+       lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
        namelen = strlen(za->za_name);
        if (namelen > NAME_MAX)
                GOTO(out, rc = -EOVERFLOW);
@@ -973,6 +1020,45 @@ out:
        RETURN(rc);
 }
 
+static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
+                              __u32 attr)
+{
+       struct osd_zap_it   *it = (struct osd_zap_it *)di;
+       zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
+       size_t               namelen = 0;
+       int                  rc;
+       ENTRY;
+
+       if (it->ozi_pos <= 1)
+               namelen = 1;
+       else if (it->ozi_pos == 2)
+               namelen = 2;
+
+       if (namelen > 0) {
+               rc = lu_dirent_calc_size(namelen, attr);
+               RETURN(rc);
+       }
+
+       rc = -zap_cursor_retrieve(it->ozi_zc, za);
+       if (unlikely(rc != 0))
+               RETURN(rc);
+
+       if (za->za_integer_length != 8 || za->za_num_integers < 3) {
+               CERROR("%s: unsupported direntry format: %d %d\n",
+                      osd_obj2dev(it->ozi_obj)->od_svname,
+                      za->za_integer_length, (int)za->za_num_integers);
+               RETURN(-EIO);
+       }
+
+       namelen = strlen(za->za_name);
+       if (namelen > NAME_MAX)
+               RETURN(-EOVERFLOW);
+
+       rc = lu_dirent_calc_size(namelen, attr);
+
+       RETURN(rc);
+}
+
 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
 {
        struct osd_zap_it *it = (struct osd_zap_it *)di;
@@ -982,7 +1068,7 @@ static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
        if (it->ozi_pos <= 2)
                pos = it->ozi_pos;
        else
-               pos = udmu_zap_cursor_serialize(it->ozi_zc);
+               pos = osd_zap_cursor_serialize(it->ozi_zc);
 
        RETURN(pos);
 }
@@ -998,15 +1084,13 @@ static int osd_dir_it_load(const struct lu_env *env,
 {
        struct osd_zap_it *it = (struct osd_zap_it *)di;
        struct osd_object *obj = it->ozi_obj;
-       struct osd_device *osd = osd_obj2dev(obj);
        zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
        int                rc;
        ENTRY;
 
-       udmu_zap_cursor_fini(it->ozi_zc);
-       if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
-                                obj->oo_db->db_object, hash))
-               RETURN(-ENOMEM);
+       /* reset the cursor */
+       zap_cursor_fini(it->ozi_zc);
+       osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
 
        if (hash <= 2) {
                it->ozi_pos = hash;
@@ -1024,7 +1108,7 @@ static int osd_dir_it_load(const struct lu_env *env,
        RETURN(rc);
 }
 
-static struct dt_index_operations osd_dir_ops = {
+struct dt_index_operations osd_dir_ops = {
        .dio_lookup         = osd_dir_lookup,
        .dio_declare_insert = osd_declare_dir_insert,
        .dio_insert         = osd_dir_insert,
@@ -1039,6 +1123,7 @@ static struct dt_index_operations osd_dir_ops = {
                .key      = osd_dir_it_key,
                .key_size = osd_dir_it_key_size,
                .rec      = osd_dir_it_rec,
+               .rec_size = osd_dir_it_rec_size,
                .store    = osd_dir_it_store,
                .load     = osd_dir_it_load
        }
@@ -1048,8 +1133,9 @@ static struct dt_index_operations osd_dir_ops = {
  * Primitives for index files using binary keys.
  */
 
-static int osd_prepare_key(struct osd_object *o, __u64 *dst,
-                          const struct dt_key *src)
+/* key integer_size is 8 */
+static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
+                                 const struct dt_key *src)
 {
        int size;
 
@@ -1066,7 +1152,7 @@ static int osd_prepare_key(struct osd_object *o, __u64 *dst,
                memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
        memcpy(dst, (const char *)src, o->oo_keysize);
 
-       return size;
+       return (size/sizeof(__u64));
 }
 
 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
@@ -1079,9 +1165,9 @@ static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
        int                rc;
        ENTRY;
 
-       rc = osd_prepare_key(obj, k, key);
+       rc = osd_prepare_key_uint64(obj, k, key);
 
-       rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
+       rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
                                k, rc, obj->oo_recusize, obj->oo_recsize,
                                (void *)rec);
        RETURN(rc == 0 ? 1 : rc);
@@ -1131,10 +1217,10 @@ static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
 
        oh = container_of0(th, struct osd_thandle, ot_super);
 
-       rc = osd_prepare_key(obj, k, key);
+       rc = osd_prepare_key_uint64(obj, k, key);
 
        /* Insert (key,oid) into ZAP */
-       rc = -zap_add_uint64(osd->od_objset.os, obj->oo_db->db_object,
+       rc = -zap_add_uint64(osd->od_os, obj->oo_db->db_object,
                             k, rc, obj->oo_recusize, obj->oo_recsize,
                             (void *)rec, oh->ot_tx);
        RETURN(rc);
@@ -1175,10 +1261,10 @@ static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
        LASSERT(th != NULL);
        oh = container_of0(th, struct osd_thandle, ot_super);
 
-       rc = osd_prepare_key(obj, k, key);
+       rc = osd_prepare_key_uint64(obj, k, key);
 
        /* Remove binary key from the ZAP */
-       rc = -zap_remove_uint64(osd->od_objset.os, obj->oo_db->db_object,
+       rc = -zap_remove_uint64(osd->od_os, obj->oo_db->db_object,
                                k, rc, oh->ot_tx);
        RETURN(rc);
 }
@@ -1198,11 +1284,11 @@ static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
         * XXX: we need a binary version of zap_cursor_move_to_key()
         *      to implement this API */
        if (*((const __u64 *)key) != 0)
-               CERROR("NOT IMPLEMETED YET (move to %Lx)\n", *((__u64 *)key));
+               CERROR("NOT IMPLEMETED YET (move to "LPX64")\n",
+                      *((__u64 *)key));
 
        zap_cursor_fini(it->ozi_zc);
-       memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
-       zap_cursor_init(it->ozi_zc, osd->od_objset.os, obj->oo_db->db_object);
+       zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_db->db_object);
        it->ozi_reset = 1;
 
        RETURN(+1);
@@ -1276,9 +1362,9 @@ static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
        if (rc)
                RETURN(rc);
 
-       rc = osd_prepare_key(obj, k, (const struct dt_key *)za->za_name);
+       rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
 
-       rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
+       rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
                                k, rc, obj->oo_recusize, obj->oo_recsize,
                                (void *)rec);
        RETURN(rc);
@@ -1303,12 +1389,9 @@ static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
        int                rc;
        ENTRY;
 
-       /* close the current cursor */
+       /* reset the cursor */
        zap_cursor_fini(it->ozi_zc);
-
-       /* create a new one starting at hash */
-       memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
-       zap_cursor_init_serialized(it->ozi_zc, osd->od_objset.os,
+       zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
                                   obj->oo_db->db_object, hash);
        it->ozi_reset = 0;
 
@@ -1341,6 +1424,214 @@ static struct dt_index_operations osd_index_ops = {
        }
 };
 
+struct osd_metadnode_it {
+       struct osd_device       *mit_dev;
+       __u64                    mit_pos;
+       struct lu_fid            mit_fid;
+       int                      mit_prefetched;
+       __u64                    mit_prefetched_dnode;
+};
+
+static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
+                                           struct dt_object *dt, __u32 attr,
+                                           struct lustre_capa *capa)
+{
+       struct osd_device       *dev   = osd_dev(dt->do_lu.lo_dev);
+       struct osd_metadnode_it *it;
+       ENTRY;
+
+       OBD_ALLOC_PTR(it);
+       if (unlikely(it == NULL))
+               RETURN(ERR_PTR(-ENOMEM));
+
+       it->mit_dev = dev;
+
+       /* XXX: dmu_object_next() does NOT find dnodes allocated
+        *      in the current non-committed txg, so we force txg
+        *      commit to find all existing dnodes ... */
+       txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
+
+       RETURN((struct dt_it *)it);
+}
+
+static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
+{
+       struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
+
+       OBD_FREE_PTR(it);
+}
+
+static int osd_zfs_otable_it_get(const struct lu_env *env,
+                                struct dt_it *di, const struct dt_key *key)
+{
+       return 0;
+}
+
+static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
+{
+}
+
+#define OTABLE_PREFETCH                256
+
+static void osd_zfs_otable_prefetch(const struct lu_env *env,
+                                   struct osd_metadnode_it *it)
+{
+       struct osd_device       *dev = it->mit_dev;
+       int                      rc;
+
+       /* can go negative on the very first access to the iterator
+        * or if some non-Lustre objects were found */
+       if (unlikely(it->mit_prefetched < 0))
+               it->mit_prefetched = 0;
+
+       if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
+               return;
+
+       if (it->mit_prefetched_dnode == 0)
+               it->mit_prefetched_dnode = it->mit_pos;
+
+       while (it->mit_prefetched < OTABLE_PREFETCH) {
+               rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
+                                     B_FALSE, 0);
+               if (unlikely(rc != 0))
+                       break;
+
+               /* dmu_prefetch() was exported in 0.6.2, if you use with
+                * an older release, just comment it out - this is an
+                * optimization */
+               dmu_prefetch(dev->od_os, it->mit_prefetched_dnode, 0, 0);
+
+               it->mit_prefetched++;
+       }
+}
+
+static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
+{
+       struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
+       struct lustre_mdt_attrs *lma;
+       struct osd_device       *dev = it->mit_dev;
+       nvlist_t                *nvbuf = NULL;
+       uchar_t                 *v;
+       __u64                    dnode;
+       int                      rc, s;
+
+       memset(&it->mit_fid, 0, sizeof(it->mit_fid));
+
+       dnode = it->mit_pos;
+       do {
+               rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
+               if (unlikely(rc != 0))
+                       GOTO(out, rc = 1);
+               it->mit_prefetched--;
+
+               /* LMA is required for this to be a Lustre object.
+                * If there is no xattr skip it. */
+               rc = __osd_xattr_load(dev, it->mit_pos, &nvbuf);
+               if (unlikely(rc != 0))
+                       continue;
+
+               LASSERT(nvbuf != NULL);
+               rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
+               if (likely(rc == 0)) {
+                       /* Lustre object */
+                       lma = (struct lustre_mdt_attrs *)v;
+                       lustre_lma_swab(lma);
+                       it->mit_fid = lma->lma_self_fid;
+                       nvlist_free(nvbuf);
+                       break;
+               } else {
+                       /* not a Lustre object, try next one */
+                       nvlist_free(nvbuf);
+               }
+
+       } while (1);
+
+
+       /* we aren't prefetching in the above loop because the number of
+        * non-Lustre objects is very small and we will be repeating very
+        * rare. in case we want to use this to iterate over non-Lustre
+        * objects (i.e. when we convert regular ZFS in Lustre) it makes
+        * sense to initiate prefetching in the loop */
+
+       /* 0 - there are more items, +1 - the end */
+       if (likely(rc == 0))
+               osd_zfs_otable_prefetch(env, it);
+
+       CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
+              it->mit_pos, PFID(&it->mit_fid), rc);
+
+out:
+       return rc;
+}
+
+static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
+                                           const struct dt_it *di)
+{
+       return NULL;
+}
+
+static int osd_zfs_otable_it_key_size(const struct lu_env *env,
+                                     const struct dt_it *di)
+{
+       return sizeof(__u64);
+}
+
+static int osd_zfs_otable_it_rec(const struct lu_env *env,
+                                const struct dt_it *di,
+                                struct dt_rec *rec, __u32 attr)
+{
+       struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
+       struct lu_fid *fid = (struct lu_fid *)rec;
+       ENTRY;
+
+       *fid = it->mit_fid;
+
+       RETURN(0);
+}
+
+
+static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
+                                    const struct dt_it *di)
+{
+       struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
+
+       return it->mit_pos;
+}
+
+static int osd_zfs_otable_it_load(const struct lu_env *env,
+                                 const struct dt_it *di, __u64 hash)
+{
+       struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
+
+       it->mit_pos = hash;
+       it->mit_prefetched = 0;
+       it->mit_prefetched_dnode = 0;
+
+       return osd_zfs_otable_it_next(env, (struct dt_it *)di);
+}
+
+static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
+                                    const struct dt_it *di, void *key_rec)
+{
+       return 0;
+}
+
+const struct dt_index_operations osd_zfs_otable_ops = {
+       .dio_it = {
+               .init     = osd_zfs_otable_it_init,
+               .fini     = osd_zfs_otable_it_fini,
+               .get      = osd_zfs_otable_it_get,
+               .put      = osd_zfs_otable_it_put,
+               .next     = osd_zfs_otable_it_next,
+               .key      = osd_zfs_otable_it_key,
+               .key_size = osd_zfs_otable_it_key_size,
+               .rec      = osd_zfs_otable_it_rec,
+               .store    = osd_zfs_otable_it_store,
+               .load     = osd_zfs_otable_it_load,
+               .key_rec  = osd_zfs_otable_it_key_rec,
+       }
+};
+
 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
                const struct dt_index_features *feat)
 {
@@ -1356,20 +1647,21 @@ int osd_index_try(const struct lu_env *env, struct dt_object *dt,
        if (feat->dif_flags & DT_IND_RANGE)
                RETURN(-ERANGE);
 
-       if (unlikely(feat == &dt_otable_features))
-               /* do not support oi scrub yet. */
-               RETURN(-ENOTSUPP);
+       if (unlikely(feat == &dt_otable_features)) {
+               dt->do_index_ops = &osd_zfs_otable_ops;
+               RETURN(0);
+       }
 
        LASSERT(obj->oo_db != NULL);
        if (likely(feat == &dt_directory_features)) {
-               if (udmu_object_is_zap(obj->oo_db))
+               if (osd_object_is_zap(obj->oo_db))
                        dt->do_index_ops = &osd_dir_ops;
                else
                        RETURN(-ENOTDIR);
        } else if (unlikely(feat == &dt_acct_features)) {
                LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
                dt->do_index_ops = &osd_acct_index_ops;
-       } else if (udmu_object_is_zap(obj->oo_db) &&
+       } else if (osd_object_is_zap(obj->oo_db) &&
                   dt->do_index_ops == NULL) {
                /* For index file, we don't support variable key & record sizes
                 * and the key has to be unique */
@@ -1403,4 +1695,3 @@ int osd_index_try(const struct lu_env *env, struct dt_object *dt,
 
        RETURN(0);
 }
-