Whamcloud - gitweb
LU-4543 osd: return actual hash value for a record
[fs/lustre-release.git] / lustre / osd-zfs / osd_index.c
index f597e02..1e7c441 100644 (file)
@@ -28,7 +28,7 @@
  * Use is subject to license terms.
  */
 /*
- * Copyright (c) 2012, Intel Corporation.
+ * Copyright (c) 2012, 2013, Intel Corporation.
  * Use is subject to license terms.
  */
 /*
  * Author: Mike Pershin <tappro@whamcloud.com>
  */
 
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
 #define DEBUG_SUBSYSTEM S_OSD
 
 #include <lustre_ver.h>
 #include <libcfs/libcfs.h>
-#include <lustre_fsfilt.h>
 #include <obd_support.h>
 #include <lustre_net.h>
 #include <obd.h>
@@ -190,7 +186,7 @@ static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
                len = (len + align) & ~align;
 
                lt = (void *)ent->lde_name + len;
-               lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
+               lt->lt_type = cpu_to_le16(DTTOIF(type));
                ent->lde_attrs |= LUDA_TYPE;
        }
 
@@ -476,6 +472,41 @@ static inline void osd_object_put(const struct lu_env *env,
        lu_object_put(env, &obj->oo_dt.do_lu);
 }
 
+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);
+
+       rc = osd_fld_lookup(env, osd, seq, range);
+       if (rc != 0) {
+               CERROR("%s: Can not lookup fld for "LPX64"\n",
+                      osd_name(osd), seq);
+               RETURN(0);
+       }
+
+       RETURN(ss->ss_node_id == range->lsr_index);
+}
+
+static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
+                         struct lu_fid *fid)
+{
+       ENTRY;
+
+       if (!fid_is_norm(fid) && !fid_is_root(fid))
+               RETURN(0);
+
+       if (osd_seq_exists(env, osd, fid_seq(fid)))
+               RETURN(0);
+
+       RETURN(1);
+}
+
 /**
  *      Inserts (key, value) pair in \a directory object.
  *
@@ -499,7 +530,7 @@ static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
        struct osd_device   *osd = osd_obj2dev(parent);
        struct lu_fid       *fid = (struct lu_fid *)rec;
        struct osd_thandle  *oh;
-       struct osd_object   *child;
+       struct osd_object   *child = NULL;
        __u32                attr;
        char                *name = (char *)key;
        int                  rc;
@@ -514,55 +545,60 @@ static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
        LASSERT(th != NULL);
        oh = container_of0(th, struct osd_thandle, ot_super);
 
-       child = osd_object_find(env, dt, fid);
-       if (IS_ERR(child))
-               RETURN(PTR_ERR(child));
-
-/*
- * to simulate old Orion setups with ./..  stored in the directories
- */
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 91, 0)
-#define OSD_ZFS_INSERT_DOTS_FOR_TESTING__
-#endif
+       rc = osd_remote_fid(env, osd, fid);
+       if (rc < 0) {
+               CERROR("%s: Can not find object "DFID": rc = %d\n",
+                      osd->od_svname, PFID(fid), rc);
+               RETURN(rc);
+       }
 
-       LASSERT(child->oo_db);
-       if (name[0] == '.') {
-               if (name[1] == 0) {
-                       /* do not store ".", instead generate it
-                        * during iteration */
-#ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
-                       GOTO(out, rc = 0);
-#endif
-               } else if (name[1] == '.' && name[2] == 0) {
-                       /* 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),
-                                                 &child->oo_db->db_object,
-                                                 8, oh);
-
-#ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
-                       GOTO(out, rc);
-#endif
+       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);
+       } else {
+               /*
+                * To simulate old Orion setups with ./..  stored in the
+                * directories
+                */
+               /* Insert local entry */
+               child = osd_object_find(env, dt, fid);
+               if (IS_ERR(child))
+                       RETURN(PTR_ERR(child));
+
+               LASSERT(child->oo_db);
+               if (name[0] == '.') {
+                       if (name[1] == 0) {
+                               /* do not store ".", instead generate it
+                                * during iteration */
+                               GOTO(out, rc = 0);
+                       } else if (name[1] == '.' && name[2] == 0) {
+                               /* 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),
+                                                &child->oo_db->db_object,
+                                                8, oh);
+                               GOTO(out, rc);
+                       }
                }
+               CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
+               CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
+               attr = child->oo_dt.do_lu.lo_header ->loh_attr;
+               oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
+               oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
        }
 
-       CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
-       CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
-       attr = child->oo_dt.do_lu.lo_header ->loh_attr;
-       oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
-       oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
        oti->oti_zde.lzd_fid = *fid;
-
        /* Insert (key,oid) into ZAP */
        rc = -zap_add(osd->od_objset.os, parent->oo_db->db_object,
                      (char *)key, 8, sizeof(oti->oti_zde) / 8,
                      (void *)&oti->oti_zde, oh->ot_tx);
 
-#ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
 out:
-#endif
-       osd_object_put(env, child);
+       if (child != NULL)
+               osd_object_put(env, child);
 
        RETURN(rc);
 }
@@ -608,10 +644,9 @@ static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
        LASSERT(th != NULL);
        oh = container_of0(th, struct osd_thandle, ot_super);
 
-#ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
        /*
-        * in Orion . and .. were stored in the directory (not generated up on
-        * request as now. we preserve them for backward compatibility
+        * In Orion . and .. were stored in the directory (not generated upon
+        * request as now). we preserve them for backward compatibility
         */
        if (name[0] == '.') {
                if (name[1] == 0) {
@@ -620,7 +655,6 @@ static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
                        RETURN(0);
                }
        }
-#endif
 
        /* Remove key from the ZAP */
        rc = -zap_remove(osd->od_objset.os, zap_db->db_object,
@@ -807,7 +841,7 @@ 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, 91, 0)
+#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)) {
@@ -840,7 +874,7 @@ 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, 99, 0)
+#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)) {
@@ -898,11 +932,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(udmu_zap_cursor_serialize(it->ozi_zc));
        namelen = strlen(za->za_name);
        if (namelen > NAME_MAX)
                GOTO(out, rc = -EOVERFLOW);
@@ -964,14 +998,10 @@ static int osd_dir_it_load(const struct lu_env *env,
        int                rc;
        ENTRY;
 
-       if (it->ozi_pos != 0) {
-               /* the cursor wasn't at the beginning
-                * so we should reset ZAP cursor as well */
-               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);
-       }
+       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);
 
        if (hash <= 2) {
                it->ozi_pos = hash;
@@ -1011,20 +1041,44 @@ static struct dt_index_operations osd_dir_ops = {
 
 /*
  * Primitives for index files using binary keys.
- * XXX: only 64-bit keys are supported for now.
  */
 
+/* key integer_size is 8 */
+static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
+                                 const struct dt_key *src)
+{
+       int size;
+
+       LASSERT(dst);
+       LASSERT(src);
+
+       /* align keysize to 64bit */
+       size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
+       size *= sizeof(__u64);
+
+       LASSERT(size <= MAXNAMELEN);
+
+       if (unlikely(size > o->oo_keysize))
+               memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
+       memcpy(dst, (const char *)src, o->oo_keysize);
+
+       return (size/sizeof(__u64));
+}
+
 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
                        struct dt_rec *rec, const struct dt_key *key,
                        struct lustre_capa *capa)
 {
        struct osd_object *obj = osd_dt_obj(dt);
        struct osd_device *osd = osd_obj2dev(obj);
+       __u64             *k = osd_oti_get(env)->oti_key64;
        int                rc;
        ENTRY;
 
+       rc = osd_prepare_key_uint64(obj, k, key);
+
        rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
-                               (const __u64 *)key, 1, 8, obj->oo_recsize,
+                               k, rc, obj->oo_recusize, obj->oo_recsize,
                                (void *)rec);
        RETURN(rc == 0 ? 1 : rc);
 }
@@ -1062,6 +1116,7 @@ static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
        struct osd_object  *obj = osd_dt_obj(dt);
        struct osd_device  *osd = osd_obj2dev(obj);
        struct osd_thandle *oh;
+       __u64              *k = osd_oti_get(env)->oti_key64;
        int                 rc;
        ENTRY;
 
@@ -1072,9 +1127,11 @@ 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_uint64(obj, k, key);
+
        /* Insert (key,oid) into ZAP */
        rc = -zap_add_uint64(osd->od_objset.os, obj->oo_db->db_object,
-                            (const __u64 *)key, 1, 8, obj->oo_recsize,
+                            k, rc, obj->oo_recusize, obj->oo_recsize,
                             (void *)rec, oh->ot_tx);
        RETURN(rc);
 }
@@ -1106,6 +1163,7 @@ static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
        struct osd_object  *obj = osd_dt_obj(dt);
        struct osd_device  *osd = osd_obj2dev(obj);
        struct osd_thandle *oh;
+       __u64              *k = osd_oti_get(env)->oti_key64;
        int                 rc;
        ENTRY;
 
@@ -1113,9 +1171,11 @@ 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_uint64(obj, k, key);
+
        /* Remove binary key from the ZAP */
        rc = -zap_remove_uint64(osd->od_objset.os, obj->oo_db->db_object,
-                               (const __u64 *)key, 1, oh->ot_tx);
+                               k, rc, oh->ot_tx);
        RETURN(rc);
 }
 
@@ -1130,8 +1190,11 @@ static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
        LASSERT(it);
        LASSERT(it->ozi_zc);
 
-       /* XXX: API is broken at the moment */
-       LASSERT(*((const __u64 *)key) == 0);
+       /*
+        * 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));
 
        zap_cursor_fini(it->ozi_zc);
        memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
@@ -1169,6 +1232,7 @@ static struct dt_key *osd_index_it_key(const struct lu_env *env,
                                       const struct dt_it *di)
 {
        struct osd_zap_it *it = (struct osd_zap_it *)di;
+       struct osd_object *obj = it->ozi_obj;
        zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
        int                rc = 0;
        ENTRY;
@@ -1179,7 +1243,7 @@ static struct dt_key *osd_index_it_key(const struct lu_env *env,
                RETURN(ERR_PTR(rc));
 
        /* the binary key is stored in the name */
-       it->ozi_key = *((__u64 *)za->za_name);
+       memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
 
        RETURN((struct dt_key *)&it->ozi_key);
 }
@@ -1187,8 +1251,9 @@ static struct dt_key *osd_index_it_key(const struct lu_env *env,
 static int osd_index_it_key_size(const struct lu_env *env,
                                const struct dt_it *di)
 {
-       /* we only support 64-bit binary keys for the time being */
-       RETURN(sizeof(__u64));
+       struct osd_zap_it *it = (struct osd_zap_it *)di;
+       struct osd_object *obj = it->ozi_obj;
+       RETURN(obj->oo_keysize);
 }
 
 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
@@ -1198,6 +1263,7 @@ static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
        struct osd_zap_it *it = (struct osd_zap_it *)di;
        struct osd_object *obj = it->ozi_obj;
        struct osd_device *osd = osd_obj2dev(obj);
+       __u64             *k = osd_oti_get(env)->oti_key64;
        int                rc;
        ENTRY;
 
@@ -1206,9 +1272,11 @@ static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
        if (rc)
                RETURN(rc);
 
+       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,
-                               (const __u64 *)za->za_name, 1, 8,
-                               obj->oo_recsize, (void *)rec);
+                               k, rc, obj->oo_recusize, obj->oo_recsize,
+                               (void *)rec);
        RETURN(rc);
 }
 
@@ -1304,10 +1372,9 @@ int osd_index_try(const struct lu_env *env, struct dt_object *dt,
                if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
                        RETURN(-EINVAL);
 
-               /* Although the zap_*_uint64() primitives support large keys, we
-                * limit ourselves to 64-bit keys for now */
-               if (feat->dif_keysize_max != sizeof(__u64) ||
-                   feat->dif_keysize_min != sizeof(__u64))
+               if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
+                       RETURN(-E2BIG);
+               if (feat->dif_keysize_max != feat->dif_keysize_min)
                        RETURN(-EINVAL);
 
                /* As for the record size, it should be a multiple of 8 bytes
@@ -1315,11 +1382,18 @@ int osd_index_try(const struct lu_env *env, struct dt_object *dt,
                 */
                if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
                        RETURN(-E2BIG);
-               if (feat->dif_recsize_max != feat->dif_recsize_min ||
-                   (feat->dif_recsize_max & (sizeof(__u64) - 1)))
+               if (feat->dif_recsize_max != feat->dif_recsize_min)
                        RETURN(-EINVAL);
 
-               obj->oo_recsize = feat->dif_recsize_max / sizeof(__u64);
+               obj->oo_keysize = feat->dif_keysize_max;
+               obj->oo_recsize = feat->dif_recsize_max;
+               obj->oo_recusize = 1;
+
+               /* ZFS prefers to work with array of 64bits */
+               if ((obj->oo_recsize & 7) == 0) {
+                       obj->oo_recsize >>= 3;
+                       obj->oo_recusize = 8;
+               }
                dt->do_index_ops = &osd_index_ops;
        }