+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);
+ osd_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);
+}
+